package eu.dnetlib.enabling.aas.ismock;

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

import java.util.List;

import javax.xml.ws.wsaddressing.W3CEndpointReference;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

import eu.dnetlib.enabling.aas.is.ISConstants;
import eu.dnetlib.enabling.aas.is.ISUtils;
import eu.dnetlib.enabling.aas.service.A2Constants;
import eu.dnetlib.enabling.aas.utils.QueryProvider;
import eu.dnetlib.enabling.aas.utils.ResourcesGenerator;
import eu.dnetlib.enabling.aas.utils.SpringUtils;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;

/**
 * @author mhorst
 *
 */
public class MockISLookUpTest {

	ApplicationContext ctx;
	ISRegistryService registryService;
	ISLookUpService lookUpService;
	ResultSetService resultSetService;
	String profId = null;
	
	String resourceProfile = "<RESOURCE_PROFILE><HEADER>" +
		"<RESOURCE_KIND value=\""+A2Constants.RESOURCE_KIND_SECURITY_CONTEXT+"\"/>" +
		"<RESOURCE_TYPE value=\""+A2Constants.RESOURCE_TYPE_SECURITY_CONTEXT+"\"/></HEADER>" +
		"<BODY><CONFIGURATION>" +
		"<resourceId>dummyResource</resourceId>" +
		"<lastAccessTime>1177402454449</lastAccessTime>" +
		"</CONFIGURATION></BODY></RESOURCE_PROFILE>";
		
	@Before
	public void setUp() throws Exception {
		ctx = SpringUtils.getSpringContext(SpringUtils.DEFAULT_INTEGRATION_RESOURCE);
		registryService = (ISRegistryService) ctx.getBean("MockISRegistryService");
		lookUpService = (ISLookUpService) ctx.getBean("MockISLookUpService");
		resultSetService = (ResultSetService) ctx.getBean("MockResultSetService");
		profId = registryService.registerProfile(resourceProfile);
	}
	
	@After
	public void tearDown() throws Exception {
		if (registryService!=null)
			registryService.deleteProfile(profId);
	}

	@Test
	public void testGetResourceProfile() throws Exception {
		String foundProfile = lookUpService.getResourceProfile(profId);
		assertNotNull(foundProfile);
		assertFalse(resourceProfile.equals(foundProfile));
		System.out.println("Found profile:");
		System.out.println(foundProfile);
	}
	
	@Test
	public void testListResourceProfiles() throws Exception {
		String secProfId = null;
		try {
			String resourceId = "someResourceProfile";
			String securityProfile = ResourcesGenerator.generateSecProf(resourceId, null);
			secProfId = registryService.registerProfile(securityProfile);
			assertNotNull(secProfId);
			W3CEndpointReference epr = lookUpService.listResourceProfiles(A2Constants.RESOURCE_KIND_SECURITY_PROFILE, 
					null, A2Constants.RESOURCE_TYPE_SECURITY_PROFILE);
			assertNotNull(epr);
			String rsId = ISUtils.extractResultSetId(epr);
			assertNotNull(rsId);
			try {
				while (!ISConstants.RESULT_SET_STATUS_CLOSED.equals(resultSetService.getRSStatus(rsId))) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			} catch (ResultSetException e1) {
				e1.printStackTrace();
			}
			List<String> array1Result = null;
			 try {
				array1Result = resultSetService.getResult(ISUtils.extractResultSetId(rsId), 
						1, ISConstants.RESULT_SET_END_POSITION_UNSPECIFIED, null);
			} catch (ResultSetException e) {
				e.printStackTrace();
			}
			String[] results = null;
			if (array1Result!=null)
				results = array1Result.toArray(new String[0]);
			assertNotNull(results);
			assertTrue(results.length>0);
		} finally {
			if (secProfId!=null)
				registryService.deleteProfile(secProfId);
		}
	}
	
	@Test
	public void testGetResourceProfileByQuery() throws Exception {
		String resourceId = "dummyResource";
		String query = QueryProvider.findSecurityContextForResourceQuery(resourceId);
		String foundProfile = lookUpService.getResourceProfileByQuery(query);
		assertNotNull(foundProfile);
		assertFalse(resourceProfile.equals(foundProfile));
		System.out.println("Found profile:");
		System.out.println(foundProfile);
	}

}
