/**
 * 
 */
package com.sun.xacml.cond;

import java.util.List;

import oasis.names.tc.xacml._3_0.core.schema.wd_17.Request;

import org.junit.Assert;
import org.junit.Test;

import com.sun.xacml.EvaluationCtx;
import com.sun.xacml.cond.xacmlv3.EvaluationResult;
import com.thalesgroup.authzforce.pdp.core.test.utils.TestUtils;

/**
 * An abstract class to easily test a function evaluation, according to a given
 * function name, a list of arguments, and expected result. In order to perform
 * a function test, simply extend this class and give the test values on
 * construction.
 * 
 * @author Cyrille MARTINS (Thales)
 * 
 */
public abstract class AbstractFunctionTest {

	private final String functionName;
	private final List<Evaluatable> inputs;
	private final EvaluationResult expectedResult;

	private static final EvaluationCtx CTX = TestUtils
			.createContext(new Request());

	/**
	 * A factory to create functions
	 */
	protected static final FunctionFactory FUNCTION_FACTORY = FunctionFactory
			.getGeneralInstance();

	/**
	 * 
	 * @param functionName
	 *            The fully qualified name of the function to be tested. The
	 *            function must be supported by the StandardFunctionFactory.
	 * @param inputs
	 *            The list of the function arguments, in order.
	 * @param expectedResult
	 *            The expected function evaluation result, according to the
	 *            given inputs
	 */
	protected AbstractFunctionTest(final String functionName,
			final List<Evaluatable> inputs,
			final EvaluationResult expectedResult) throws Exception {
		this.functionName = functionName;
		this.inputs = inputs;
		this.expectedResult = expectedResult;
	}

	@Test
	public void testEvaluate() throws Exception {
		// Execution
		Function function = FUNCTION_FACTORY.createFunction(functionName);
		@SuppressWarnings({ "unchecked", "rawtypes" })
		EvaluationResult actualResult = function.evaluate((List) inputs, CTX);

		// Assertions
		Assert.assertEquals(expectedResult.indeterminate(),
				actualResult.indeterminate());
		if (expectedResult.getStatus() == null) {
			Assert.assertNull(actualResult.getStatus());
		} else {
			Assert.assertTrue(actualResult.getStatus().getCode()
					.containsAll(expectedResult.getStatus().getCode()));
		}
		Assert.assertEquals(expectedResult.getAttributeValue(),
				actualResult.getAttributeValue());
	}
}

@@ -242,37 +242,112 @@
 cml.
-cond.xacmlv3.EvaluationResult
+attr.BagAttribute;%0D%0Aimport com.sun.xacml.cond.xacmlv3.EvaluationResult;%0D%0Aimport com.sun.xacml.ctx.Status
 ;%0D%0Ai
@@ -2232,24 +2232,46 @@
 == null) %7B%0D%0A
+%09%09%09// Compare status%0D%0A
 %09%09%09Assert.as
@@ -2326,126 +2326,686 @@
 %0A%09%09%09
-Assert.assertTrue(actualResult.getStatus().getCode()%0D%0A%09%09%09%09%09.containsAll(expectedResult.getStatus().getCode()));%0D%0A%09%09%7D%0D%0A
+// Compare status codes%0D%0A%09%09%09Status expectedStatus = expectedResult.getStatus();%0D%0A%09%09%09Status actualStatus = actualResult.getStatus();%0D%0A%09%09%09Assert.assertTrue(actualStatus.getCode().containsAll(%0D%0A%09%09%09%09%09expectedStatus.getCode()));%0D%0A%09%09%7D%0D%0A%09%09if (expectedResult.getAttributeValue() != null%0D%0A%09%09%09%09&& expectedResult.getAttributeValue() instanceof BagAttribute) %7B%0D%0A%09%09%09// Compare bag contents (regardless of order)%0D%0A%09%09%09BagAttribute expectedBag = (BagAttribute) expectedResult%0D%0A%09%09%09%09%09.getAttributeValue();%0D%0A%09%09%09BagAttribute actualBag = (BagAttribute) actualResult%0D%0A%09%09%09%09%09.getAttributeValue();%0D%0A%09%09%09Assert.assertTrue(actualBag.containsAll(expectedBag));%0D%0A%09%09%7D else %7B%0D%0A%09%09%09// Compare attribute values%0D%0A%09
 %09%09As
@@ -3055,32 +3055,33 @@
 teValue(),%0D%0A%09%09%09%09
+%09
 actualResult.get
@@ -3100,15 +3100,20 @@
 ue());%0D%0A
+%09%09%7D%0D%0A
 %09%7D%0D%0A%7D%0D%0A

Improve test comparisons