package uk.ac.open.kmi.iserve.api;

import com.google.common.collect.Sets;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.open.kmi.iserve.discovery.api.ConceptMatcher;
import uk.ac.open.kmi.iserve.discovery.disco.impl.SparqlIndexedLogicConceptMatcher;
import uk.ac.open.kmi.iserve.discovery.disco.impl.SparqlLogicConceptMatcher;
import uk.ac.open.kmi.iserve.sal.manager.RegistryManager;

import java.net.URI;
import java.util.Set;

/**
 * iServeEngineTest
 *
 * @author <a href="mailto:carlos.pedrinaci@open.ac.uk">Carlos Pedrinaci</a> (KMi - The Open University)
 * @since 29/10/2013
 */
public class iServeEngineTest {

    private static final Logger log = LoggerFactory.getLogger(iServeEngineTest.class);

    @Test
    public void testCreationMethods() throws Exception {

        iServeEngine engine;
        RegistryManager registryManager;
        Set<URI> servicesList1;
        Set<URI> servicesList2;

        // Get default config
        engine = iServeEngineFactory.createEngine();
        Assert.assertNotNull(engine);
        log.info("Obtained an iServe Engine with default configuration - {}", engine);
        registryManager = engine.getRegistryManager();
        log.info("Obtained a Registry Manager - {}", registryManager);
        Assert.assertNotNull(registryManager);
        servicesList1 = registryManager.getServiceManager().listServices();
        Assert.assertNotNull(servicesList1);

        // Get one specifying the configuration
        engine = iServeEngineFactory.createEngine("config.properties");
        Assert.assertNotNull(engine);
        log.info("Obtained an iServe Engine with specific configuration file - {}", engine);
        registryManager = engine.getRegistryManager();
        log.info("Obtained a Registry Manager - {}", registryManager);
        Assert.assertNotNull(registryManager);
        servicesList2 = registryManager.getServiceManager().listServices();
        Assert.assertNotNull(servicesList1);

        Assert.assertEquals(servicesList1.size(), servicesList2.size());

        Sets.SetView<URI> setsDiff = Sets.difference(servicesList1, servicesList2);
        Assert.assertTrue(setsDiff.isEmpty());

    }

    @Test
    public void testGetRegistryManager() throws Exception {

        RegistryManager registryManager;

        iServeEngine engine = iServeEngineFactory.createEngine();

        registryManager = engine.getRegistryManager();
        log.info("Obtained a Registry Manager - {}", registryManager);
        Assert.assertNotNull(registryManager);
        Set<URI> servicesList1 = registryManager.getServiceManager().listServices();
        Assert.assertNotNull(servicesList1);

        registryManager = engine.getRegistryManager();
        log.info("Obtained a Registry Manager - {}", registryManager);
        Assert.assertNotNull(registryManager);
        Set<URI> servicesList2 = registryManager.getServiceManager().listServices();
        Assert.assertNotNull(servicesList2);

        Assert.assertEquals(servicesList1.size(), servicesList2.size());

        Sets.SetView<URI> setsDiff = Sets.difference(servicesList1, servicesList2);
        Assert.assertTrue(setsDiff.isEmpty());
    }

    @Test
    public void testGetConceptMatcher() throws Exception {

        ConceptMatcher matcher;

        iServeEngine engine = iServeEngineFactory.createEngine();

        // check we get one
        matcher = engine.getDefaultConceptMatcher();
        Assert.assertNotNull(matcher);
        log.info("Obtained a concept matcher - {}", matcher.toString());

        // Check we get one of the class requested
        matcher = engine.getConceptMatcher(SparqlLogicConceptMatcher.class.getName());
        Assert.assertNotNull(matcher);
        log.info("Obtained a SparqlLogicConceptMatcher - {}", matcher.toString());
        Assert.assertEquals(matcher.getClass(), SparqlLogicConceptMatcher.class);

        // Check we get one of the class requested
        matcher = engine.getConceptMatcher(SparqlIndexedLogicConceptMatcher.class
                .getName());
        Assert.assertNotNull(matcher);
        log.info("Obtained a SparqlIndexedLogicConceptMatcher - {}", matcher.toString());
        Assert.assertEquals(matcher.getClass(), SparqlIndexedLogicConceptMatcher.class);

    }

    @Test
    public void testListAvailableMatchers() throws Exception {

        iServeEngine engine = iServeEngineFactory.createEngine();

        Set<String> matchers = engine.listAvailableMatchers();
        log.info("Obtained matchers available - {}", matchers);
        Assert.assertNotNull(matchers);
        Assert.assertTrue(matchers.size() >= 2);

    }
}

@@ -1472,32 +1472,34 @@
 ervicesList1);%0A%0A
+//
         // Get o
@@ -1530,16 +1530,18 @@
 uration%0A
+//
         
@@ -1604,16 +1604,18 @@
 ties%22);%0A
+//
         
@@ -1636,32 +1636,34 @@
 otNull(engine);%0A
+//
         log.info
@@ -1731,32 +1731,34 @@
 - %7B%7D%22, engine);%0A
+//
         registry
@@ -1788,32 +1788,34 @@
 istryManager();%0A
+//
         log.info
@@ -1861,32 +1861,34 @@
 gistryManager);%0A
+//
         Assert.a
@@ -1910,32 +1910,34 @@
 gistryManager);%0A
+//
         services
@@ -1988,32 +1988,34 @@
 listServices();%0A
+//
         Assert.a
@@ -2035,33 +2035,37 @@
 servicesList1);%0A
-%0A
+//%0A//
         Assert.a
@@ -2113,33 +2113,37 @@
 sList2.size());%0A
-%0A
+//%0A//
         Sets.Set
@@ -2202,32 +2202,34 @@
 servicesList2);%0A
+//
         Assert.a
@@ -2255,25 +2255,24 @@
 isEmpty());%0A
-%0A
     %7D%0A%0A    @

Remove test as it was not portable.