package eu.dnetlib.enabling.is.registry;

import static org.junit.Assert.*; // NOPMD
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import javax.xml.xpath.XPathExpressionException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnit44Runner;

import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
import eu.dnetlib.enabling.tools.OpaqueResource;
import eu.dnetlib.enabling.tools.StaticServiceLocator;

/**
 * test compatibility pending resource manager implementation.
 * 
 * @author marko
 * 
 */
@RunWith(MockitoJUnit44Runner.class)
public class CompatPendingResourceManagerImplTest {

	/**
	 * instance under test.
	 */
	private transient CompatPendingResourceManagerImpl pendingManager;

	/**
	 * registry service mock.
	 */
	@Mock
	private transient ISRegistryService registryService;

	/**
	 * resource mock.
	 */
	@Mock
	private transient OpaqueResource resource;

	/**
	 * prepare instance for testing.
	 */
	@Before
	public void setUp() {
		pendingManager = new CompatPendingResourceManagerImpl();
		pendingManager.setRegistryLocator(new StaticServiceLocator<ISRegistryService>(registryService));
	}

	/**
	 * test that the application profile can be queried correctly.
	 * 
	 * @throws XPathExpressionException
	 *             shouldn't happen
	 */
	@Test
	public void testGetPendingKindForType() throws XPathExpressionException {
		assertEquals("check pending", "PendingRepositoryResources", pendingManager.getPendingKindForType("RepositoryServiceResourceType"));
		assertEquals("check pending", "PendingDSResources", pendingManager.getPendingKindForType("UserDSResourceType"));
	}
		
	/**
	 * check that unexistent types throw an exception.
	 * 
	 * @throws XPathExpressionException shouldn't happen
	 */
	@Test(expected = IllegalStateException.class)
	public void testGetPendingKindForNoSuchType() throws XPathExpressionException {
		pendingManager.getPendingKindForType("NoSuchType");
	}

	/**
	 * test that the application profile can be queried correctly.
	 * 
	 * @throws XPathExpressionException
	 *             shouldn't happen
	 */
	@Test
	public void testGetNormalKindForType() throws XPathExpressionException {
		assertEquals("check kind", "RepositoryServiceResources", pendingManager.getNormalKindForType("RepositoryServiceResourceType"));
		assertEquals("check kind", "UserDSResources", pendingManager.getNormalKindForType("UserDSResourceType"));
		assertNull("check unexisting type", pendingManager.getNormalKindForType("NoSuchType"));
	}

	/**
	 * test that the resource is correctly moved from the pending collection to the new collection.
	 * 
	 * @throws ISRegistryException
	 *             shoudn't happen
	 */
	@Test
	public void testSetValid() throws ISRegistryException {
		final String profId = "123";
		when(resource.getResourceId()).thenReturn("123");

		pendingManager.setValid(resource);

		verify(registryService).deleteProfile(profId);
		verify(registryService).registerProfile(anyString());
		
		assertNotNull("dummy", registryService);
	}
	
	/**
	 * test that the resource is correctly moved from the current collection to the correct pending collection.
	 * 
	 * @throws ISRegistryException
	 *             shoudn't happen
	 */
	@Test
	public void testSetPending() throws ISRegistryException {
		final String profId = "321";
		when(resource.getResourceId()).thenReturn("321");
		when(resource.getResourceType()).thenReturn("RepositoryServiceResourceType");
		
		pendingManager.setPending(resource);

		verify(registryService).deleteProfile(profId);
		verify(registryService).registerProfile(anyString());
		
		assertNotNull("dummy", registryService);
	}

}
