All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xacml4j.v20.Xacml20TestUtility Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package org.xacml4j.v20;

/*
 * #%L
 * Xacml4J Policy Unit Test Support
 * %%
 * Copyright (C) 2009 - 2014 Xacml4J.org
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;

import org.oasis.xacml.v20.jaxb.context.ResponseType;
import org.oasis.xacml.v20.jaxb.context.ResultType;
import org.oasis.xacml.v20.jaxb.context.StatusType;
import org.oasis.xacml.v20.jaxb.policy.AttributeAssignmentType;
import org.oasis.xacml.v20.jaxb.policy.ObligationType;
import org.oasis.xacml.v20.jaxb.policy.ObligationsType;
import org.xacml4j.v30.RequestContext;
import org.xacml4j.v30.marshal.jaxb.JAXBContextUtil;
import org.xacml4j.v30.marshal.jaxb.Xacml20RequestContextUnmarshaller;

import com.google.common.base.Supplier;
import com.google.common.io.Closeables;


public class Xacml20TestUtility
{
	private static final JAXBContext context = JAXBContextUtil.getInstance();
	private static final Xacml20RequestContextUnmarshaller requestUnmarshaller = new Xacml20RequestContextUnmarshaller();

	/** Private constructor for utility class */
	private Xacml20TestUtility() {}

	public static void assertResponse(ResponseType a, ResponseType b) {
		assertEquals(a.getResult().size(), b.getResult().size());
		List ar = a.getResult();
		List br = b.getResult();
		for(int i = 0; i < ar.size(); i++){
			assertResults(ar.get(i), br.get(i));
		}
	}

	public static void assertResults(ResultType a, ResultType b)
	{
		assertEquals(a.getDecision(), b.getDecision());
		assertEquals(a.getResourceId(), b.getResourceId());
		assertStatus(a.getStatus(), b.getStatus());
		assertObligations(a.getObligations(), b.getObligations());
	}

	public static void assertStatus(StatusType a, StatusType b)
	{
		assertEquals(a.getStatusCode().getValue(), b.getStatusCode().getValue());
	}

	public static void assertObligations(ObligationsType a, ObligationsType b)
	{
		if(a == null && b == null){
			return;
		}
		List oa = a.getObligation();
		List ob = a.getObligation();
		assertEquals(oa.size(), ob.size());
		Map aMap = toObligationMap(oa);
		Map bMap = toObligationMap(ob);

		assertTrue(aMap.keySet().containsAll(bMap.keySet()));
		assertTrue(bMap.keySet().containsAll(aMap.keySet()));

		for(String obligationId : aMap.keySet()){
			ObligationType obligationA = aMap.get(obligationId);
			ObligationType obligationB = bMap.get(obligationId);
			assertObligation(obligationA, obligationB);
		}
	}

	public static void assertObligation(ObligationType a, ObligationType b)
	{
		List aAttr = a.getAttributeAssignment();
		List bAttr = b.getAttributeAssignment();
		assertEquals(aAttr.size(), bAttr.size());
		Map aMap = toAttributeAssignmentMap(aAttr);
		Map bMap = toAttributeAssignmentMap(bAttr);
		assertTrue(aMap.keySet().containsAll(bMap.keySet()));
		assertTrue(bMap.keySet().containsAll(aMap.keySet()));
		for(String attributeId : aMap.keySet()){
			AttributeAssignmentType attrA = aMap.get(attributeId);
			AttributeAssignmentType attrB = bMap.get(attributeId);
			assertAttributeAssignment(attrA, attrB);
		}
	}

	public static void assertAttributeAssignment(AttributeAssignmentType a, AttributeAssignmentType b)
	{
		assertEquals(a.getAttributeId(), b.getAttributeId());
		assertEquals(a.getDataType(), b.getDataType());
		assertEquals(a.getContent(), b.getContent());
	}

	private static Map toObligationMap(
			List obligations)
	{
		Map map = new LinkedHashMap();
		for(ObligationType o : obligations){
			map.put(o.getObligationId(), o);
		}
		return map;
	}

	private static Map toAttributeAssignmentMap(
			List attributes)
	{
		Map map = new LinkedHashMap();
		for(AttributeAssignmentType a : attributes){
			map.put(a.getAttributeId(), a);
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	public static ResponseType getResponse(String resourcePath) throws Exception
	{
		InputStream in = null;
		try {
			in = getClasspathResource(resourcePath).get();
			assertNotNull(in);
			return ((JAXBElement) context.createUnmarshaller().unmarshal(in)).getValue();
		} finally {
			Closeables.closeQuietly(in);
		}
	}

	public static RequestContext getRequest(String resourcePath) throws Exception {
		InputStream in = null;
		try {
			in = getClasspathResource(resourcePath).get();
			return requestUnmarshaller.unmarshal(in);
		} finally {
			Closeables.closeQuietly(in);
		}
	}

	public static Supplier getClasspathResource(final String resourcePath) {
		return new Supplier() {
			@Override
			public InputStream get() {
				ClassLoader cl = Thread.currentThread().getContextClassLoader();
				return cl.getResourceAsStream(resourcePath);
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy