ValidateWithDomainWhitelistTest.java

/***************************************************************************
   Copyright 2014 Emily Estes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***************************************************************************/
package net.metanotion.formsauth;


import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.metanotion.json.JsonArray;
import net.metanotion.json.JsonObject;

public final class ValidateWithDomainWhitelistTest {
	private static final Logger logger = LoggerFactory.getLogger(ValidateWithDomainWhitelistTest.class);
	public static void main(final String[] args) throws Exception {
		final JsonObject testData = JsonObject.read(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));
		final JsonArray jsonWhiteList = (JsonArray) testData.get("domains");
		final ArrayList<String> whiteList  = new ArrayList<>();
		for(final Object o: jsonWhiteList) { whiteList.add(o.toString()); }
		final ValidateWithDomainWhitelist validator = new ValidateWithDomainWhitelist(whiteList);
		final JsonObject testPairs = (JsonObject) testData.get("testPairs");
		for(final String key: testPairs.keySet()) {
			boolean success = true;
			logger.debug("Testing: {} - expected {}", key, testPairs.get(key));
			try {
				validator.validate(key, null, null);
			} catch (final RuntimeException re) {
				success = false;
			}
			if(success != ((Boolean) testPairs.get(key)).booleanValue()) {
				throw new RuntimeException(
					"Test failed. Value '" + key + "' expected result was " + Boolean.toString((Boolean) testPairs.get(key)));
			}
		}
	}
}