package an.xacml.adapter.file;

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

import java.net.URI;

import javax.xml.ws.wsaddressing.W3CEndpointReference;
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Before;
import org.junit.Test;

import pl.edu.icm.yadda.aas.utils.FileUtils;
import an.xacml.adapter.DataAdapter;
import an.xacml.adapter.file.policy.FileAdapterPolicy;
import an.xml.XMLParserWrapper;
import edu.emory.mathcs.backport.java.util.Arrays;
import eu.dnetlib.enabling.aas.retrievers.resultset.IResultSetProvider;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import eu.dnetlib.enabling.resultset.ResultSetConstants;
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
import eu.dnetlib.enabling.tools.StaticServiceLocator;

/**
 * {@link ISLookupDataStore} test class.
 * @author mhorst
 *
 */
public class ISLookupDataStoreTest {

	ISLookupDataStore dataStore = null;
	
	StaticServiceLocator<ISLookUpService> lookupLocator = null;
	
	final String predefinedRSId = "predefinedRSId";
	
	@Before
	public void init() throws Exception {
		dataStore = new ISLookupDataStore();
		lookupLocator = new StaticServiceLocator<ISLookUpService>();
		dataStore.setLookupLocator(lookupLocator);
	}
	
	@SuppressWarnings("deprecation")
	@Test
	public void testLoadingPolicies() throws Exception {
		String profileContentPath = "classpath:an/xacml/adapter/file/dummy-policy-profile.xml";
		final String profileContent = FileUtils.getFileContent(profileContentPath);
		
//		setting lookupService mock into lookupLocator
		Mockery lookupMockery = new Mockery();
		final ISLookUpService lookupMockService = lookupMockery.mock(ISLookUpService.class);
		lookupLocator.setService(lookupMockService);
		lookupMockery.checking(new Expectations(){{
			one(lookupMockService).listResourceProfiles(
					with(any(String.class)),
					with(any(String.class)),
					with(any(String.class)));
			will(returnValue(new W3CEndpointReferenceBuilder().address("someAddr").build()));
		}});
		
//		defining resultset service mock
		Mockery rsMockery = new Mockery();
		final ResultSetService resultSetMockService = rsMockery.mock(ResultSetService.class);
		rsMockery.checking(new Expectations(){{
			one(resultSetMockService).getNumberOfElements(predefinedRSId);
			will(returnValue(1));
			one(resultSetMockService).getResult(predefinedRSId,
					ResultSetConstants.RESULT_SET_FIRST_ELEMENT, 1, 
					ResultSetConstants.RESULT_SET_REQUEST_MODE_WAITING);
			will(returnValue(Arrays.asList(new String[] {profileContent})));
		}});
		
//		setting dummy ResultSetProvider into retriever
		dataStore.setResultSetProvider(new IResultSetProvider() {
			public ResultSetService discover(W3CEndpointReference epr) {
				return resultSetMockService;
			}
			public String extractId(W3CEndpointReference epr) {
				return predefinedRSId;
			}
		});
		
//		setting system property pointing to xacml policy schema
		System.setProperty(XACMLParser.POLICY_KEY_DEFAULT_SCHEMA_FILE, 
        		FileUtils.resolveAbsolutePath("classpath:schemas/xacml-2.0-policy.xsd"));
//		required sun dom to process element type data, retrieved from schema
		System.setProperty(XMLParserWrapper.OPT_FORCE_SUNDOM, "true");
		
		DataAdapter[] result = dataStore.load();
		assertNotNull(result);
		assertEquals(1, result.length);
		assertTrue(result[0] instanceof FileAdapterPolicy);
		assertEquals(URI.create("urn:oasis:names:tc:xacml:2.0:conformance-test:IIA008:policy"),
				((FileAdapterPolicy)result[0]).getAttributeValueByName(ISLookupDataStore.ATTRIBUTE_POLICY_ID));
		
	}
	
}
