package eu.dnetlib.enabling.aas.ismock;

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

import java.util.Arrays;

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

import eu.dnetlib.enabling.aas.service.A2Constants;
import eu.dnetlib.enabling.aas.utils.SpringUtils;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;

public class MockISRegistryTest {
	
	ApplicationContext ctx;
	ISRegistryService registryService;
	ISLookUpService lookUpService;

	@Before
	public void setUp() throws Exception {
		ctx = SpringUtils.getSpringContext(SpringUtils.DEFAULT_INTEGRATION_RESOURCE);
		registryService = (ISRegistryService) ctx.getBean("MockISRegistryService");
		lookUpService = (ISLookUpService) ctx.getBean("MockISLookUpService");
	}

	@After
	public void tearDown() throws Exception {
	}

	String generateProfile() {
		return "<RESOURCE_PROFILE><HEADER>" +
				"<RESOURCE_KIND value=\""+A2Constants.RESOURCE_KIND_SECURITY_CONTEXT+"\"/>" +
				"<RESOURCE_TYPE value=\""+A2Constants.RESOURCE_TYPE_SECURITY_CONTEXT+"\"/></HEADER>" +
				"<BODY><CONFIGURATION>" +
				"<lastAccessTime>1177402454449</lastAccessTime>" +
				"</CONFIGURATION></BODY></RESOURCE_PROFILE>";
	}
	
	@Test
	public void testDeleteProfile() throws Exception {
		String resourceProfile = generateProfile();
		String profId = registryService.registerProfile(resourceProfile);
		assertNotNull(profId);
		String profile = lookUpService.getResourceProfile(profId);
		assertNotNull(profile);
		assertTrue(registryService.deleteProfile(profId));
		profile = lookUpService.getResourceProfile(profId);
		assertNull(profile);
	}

	@Test
	public void testDeleteProfiles() throws Exception {
		String resourceProfile = generateProfile();
		String profId1 = registryService.registerProfile(resourceProfile);
		assertNotNull(profId1);
		String profile1 = lookUpService.getResourceProfile(profId1);
		assertNotNull(profile1);
		String profId2 = registryService.registerProfile(resourceProfile);
		assertNotNull(profId2);
		String profile2 = lookUpService.getResourceProfile(profId2);
		assertNotNull(profile2);
		String[] arrayprofId = new String[2];
		arrayprofId[0] = profId1;
		arrayprofId[1] = profId2;
		assertTrue(registryService.deleteProfiles(Arrays.asList(arrayprofId)));
		profile1 = lookUpService.getResourceProfile(profId1);
		assertNull(profile1);
		profile2 = lookUpService.getResourceProfile(profId2);
		assertNull(profile2);
	}

	@Test
	public void testRegisterProfile() throws Exception {
		String resourceProfile = generateProfile();
		String profId = registryService.registerProfile(resourceProfile);
		assertNotNull(profId);
		String profile = lookUpService.getResourceProfile(profId);
		assertNotNull(profile);
		assertFalse(resourceProfile.equals(profile));
		System.out.println("Got profile:");
		System.out.println(profile);
		assertTrue(registryService.deleteProfile(profId));
	}

	@Test
	public void testUpdateProfile() throws Exception {
		String resourceProfile = generateProfile();
		String profId = registryService.registerProfile(resourceProfile);
		assertNotNull(profId);
		String profile = lookUpService.getResourceProfile(profId);
		assertNotNull(profile);
		assertFalse(resourceProfile.equals(profile));
		String updatedProfile="<RESOURCE_PROFILE><HEADER>" +
			"<RESOURCE_KIND value=\"AASResources\"/>" +
			"<RESOURCE_TYPE value=\"SecurityContextResourceType\"/></HEADER>" +
			"<BODY><CONFIGURATION>" +
			"" +
			"</CONFIGURATION></BODY></RESOURCE_PROFILE>";
		assertTrue(registryService.updateProfile(profId, updatedProfile, A2Constants.RESOURCE_TYPE_SECURITY_CONTEXT));
		String newProfile = lookUpService.getResourceProfile(profId);
		assertFalse(updatedProfile.equals(newProfile));
		assertTrue(registryService.deleteProfile(profId));
		profile = lookUpService.getResourceProfile(profId);
		assertNull(profile);
	}

}
