// Path: fcrepo-api-x-jena/src/main/java/org/fcrepo/apix/jena/Util.java
// public static WebResource rdfResource(final String uri, final String rdf, final String name) {
// 
//     return new WebResource() {
// 
//         @Override
//         public void close() throws Exception {
//             // nothing
//         }
// 
//         @Override
//         public URI uri() {
//             return uri != null ? URI.create(uri) : null;
//         }
// 
//         @Override
//         public InputStream representation() {
//             return IOUtils.toInputStream(rdf, Charset.forName("UTF-8"));
//         }
// 
//         @Override
//         public String name() {
//             return name;
//         }
// 
//         @Override
//         public String contentType() {
//             return "text/turtle";
//         }
//     };
// }
// 
// Path: fcrepo-api-x-jena/src/main/java/org/fcrepo/apix/jena/Util.java
// public static String triple(final String s, final String p, final String o) {
//     return String.format("<%s> <%s> <%s> .\n", s, p, o);
// }
// 
// Path: fcrepo-api-x-model/src/main/java/org/fcrepo/apix/model/Service.java
// public interface Service extends WebResource {
// 
//     /**
//      * The canonical URI for a service.
//      *
//      * @return Canonical URI. If none defined, then this is implicitly the URI of the service resource itself.
//      */
//     URI canonicalURI();
// }


import static org.fcrepo.apix.jena.Util.rdfResource;
import static org.fcrepo.apix.jena.Util.triple;
import static org.fcrepo.apix.model.Ontologies.RDF_TYPE;
import static org.fcrepo.apix.model.Ontologies.Service.CLASS_LDP_SERVICE_INSTANCE_REGISTRY;
import static org.fcrepo.apix.model.Ontologies.Service.CLASS_SERVICE;
import static org.fcrepo.apix.model.Ontologies.Service.PROP_CANONICAL;
import static org.fcrepo.apix.model.Ontologies.Service.PROP_HAS_ENDPOINT;
import static org.fcrepo.apix.model.Ontologies.Service.PROP_HAS_SERVICE_INSTANCE;
import static org.fcrepo.apix.model.Ontologies.Service.PROP_HAS_SERVICE_INSTANCE_REGISTRY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.fcrepo.apix.model.Service;
import org.fcrepo.apix.model.ServiceInstance;
import org.fcrepo.apix.model.WebResource;
import org.fcrepo.apix.model.components.Registry;
import org.fcrepo.apix.model.components.ServiceInstanceRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
    @SuppressWarnings("resource")
    @Test
    public void instancesOfTest() {
        final String SERVICE_INSTANCE_1 = "http://example.org/service#instance1";
        final String SERVICE_ENDPOINT_1 = "http://example.org/endpoints/1";
        final String SERVICE_INSTANCE_2 = "http://example.org/service#instance2";
        final String SERVICE_ENDPOINT_2 = "http://example.org/endpoints/2";
        final String SERVICE = "http://example.org/service#uri";
        final URI SERVICE_URI = URI.create(SERVICE);

        final WebResource resource = rdfResource(SERVICE,
                triple(SERVICE, RDF_TYPE, CLASS_SERVICE) +
                        triple(SERVICE, RDF_TYPE, CLASS_LDP_SERVICE_INSTANCE_REGISTRY) +
                        triple(SERVICE, PROP_HAS_SERVICE_INSTANCE_REGISTRY, SERVICE) +
                        triple(SERVICE, PROP_HAS_SERVICE_INSTANCE, SERVICE_INSTANCE_1) +
                        triple(SERVICE, PROP_HAS_SERVICE_INSTANCE, SERVICE_INSTANCE_2) +
                        triple(SERVICE_INSTANCE_1, PROP_HAS_ENDPOINT, SERVICE_ENDPOINT_1) +
                        triple(SERVICE_INSTANCE_2, PROP_HAS_ENDPOINT, SERVICE_ENDPOINT_2));

        // Emulate what happens if we do GETS with fragments
        when(delegate.get(SERVICE_URI)).thenReturn(resource);
        when(delegate.get(URI.create(SERVICE_INSTANCE_1))).thenReturn(resource);
        when(delegate.get(URI.create(SERVICE_INSTANCE_2))).thenReturn(resource);

        final ServiceInstanceRegistry instanceRegistry = toTest.instancesOf(toTest.getService(SERVICE_URI));
        assertEquals(2, instanceRegistry.instances().size());
        instanceRegistry.instances().stream().forEach(si -> assertEquals(1, si.endpoints().size()));
        final List<URI> instanceURIs = instanceRegistry.instances().stream().map(ServiceInstance::endpoints).flatMap(
                Collection::stream).collect(
                        Collectors.toList());
// Path: fcrepo-api-x-jena/src/main/java/org/fcrepo/apix/jena/Util.java
// public static WebResource rdfResource(final String uri, final String rdf, final String name) {
// 
//     return new WebResource() {
// 
//         @Override
//         public void close() throws Exception {
//             // nothing
//         }
// 
//         @Override
//         public URI uri() {
//             return uri != null ? URI.create(uri) : null;
//         }
// 
//         @Override
//         public InputStream representation() {
//             return IOUtils.toInputStream(rdf, Charset.forName("UTF-8"));
//         }
// 
//         @Override
//         public String name() {
//             return name;
//         }
// 
//         @Override
//         public String contentType() {
//             return "text/turtle";
//         }
//     };
// }
// 
// Path: fcrepo-api-x-jena/src/main/java/org/fcrepo/apix/jena/Util.java
// public static String triple(final String s, final String p, final String o) {
//     return String.format("<%s> <%s> <%s> .\n", s, p, o);
// }
// 
// Path: fcrepo-api-x-model/src/main/java/org/fcrepo/apix/model/Service.java
// public interface Service extends WebResource {
// 
//     /**
//      * The canonical URI for a service.
//      *
//      * @return Canonical URI. If none defined, then this is implicitly the URI of the service resource itself.
//      */
//     URI canonicalURI();
// }


// Path: fcrepo-api-x-jena/src/test/java/org/fcrepo/apix/jena/impl/JenaServiceRegistryTest.java
import static org.fcrepo.apix.jena.Util.rdfResource;
import static org.fcrepo.apix.jena.Util.triple;
import static org.fcrepo.apix.model.Ontologies.RDF_TYPE;
import static org.fcrepo.apix.model.Ontologies.Service.CLASS_LDP_SERVICE_INSTANCE_REGISTRY;
import static org.fcrepo.apix.model.Ontologies.Service.CLASS_SERVICE;
import static org.fcrepo.apix.model.Ontologies.Service.PROP_CANONICAL;
import static org.fcrepo.apix.model.Ontologies.Service.PROP_HAS_ENDPOINT;
import static org.fcrepo.apix.model.Ontologies.Service.PROP_HAS_SERVICE_INSTANCE;
import static org.fcrepo.apix.model.Ontologies.Service.PROP_HAS_SERVICE_INSTANCE_REGISTRY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.fcrepo.apix.model.Service;
import org.fcrepo.apix.model.ServiceInstance;
import org.fcrepo.apix.model.WebResource;
import org.fcrepo.apix.model.components.Registry;
import org.fcrepo.apix.model.components.ServiceInstanceRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

    @SuppressWarnings("resource")
    @Test
    public void instancesOfTest() {
        final String SERVICE_INSTANCE_1 = "http://example.org/service#instance1";
        final String SERVICE_ENDPOINT_1 = "http://example.org/endpoints/1";
        final String SERVICE_INSTANCE_2 = "http://example.org/service#instance2";
        final String SERVICE_ENDPOINT_2 = "http://example.org/endpoints/2";
        final String SERVICE = "http://example.org/service#uri";
        final URI SERVICE_URI = URI.create(SERVICE);

        final WebResource resource = rdfResource(SERVICE,
                triple(SERVICE, RDF_TYPE, CLASS_SERVICE) +
                        triple(SERVICE, RDF_TYPE, CLASS_LDP_SERVICE_INSTANCE_REGISTRY) +
                        triple(SERVICE, PROP_HAS_SERVICE_INSTANCE_REGISTRY, SERVICE) +
                        triple(SERVICE, PROP_HAS_SERVICE_INSTANCE, SERVICE_INSTANCE_1) +
                        triple(SERVICE, PROP_HAS_SERVICE_INSTANCE, SERVICE_INSTANCE_2) +
                        triple(SERVICE_INSTANCE_1, PROP_HAS_ENDPOINT, SERVICE_ENDPOINT_1) +
                        triple(SERVICE_INSTANCE_2, PROP_HAS_ENDPOINT, SERVICE_ENDPOINT_2));

        // Emulate what happens if we do GETS with fragments
        when(delegate.get(SERVICE_URI)).thenReturn(resource);
        when(delegate.get(URI.create(SERVICE_INSTANCE_1))).thenReturn(resource);
        when(delegate.get(URI.create(SERVICE_INSTANCE_2))).thenReturn(resource);

        final ServiceInstanceRegistry instanceRegistry = toTest.instancesOf(toTest.getService(SERVICE_URI));
        assertEquals(2, instanceRegistry.instances().size());
        instanceRegistry.instances().stream().forEach(si -> assertEquals(1, si.endpoints().size()));
        final List<URI> instanceURIs = instanceRegistry.instances().stream().map(ServiceInstance::endpoints).flatMap(
                Collection::stream).collect(
                        Collectors.toList());
        assertTrue(instanceURIs.containsAll(Arrays.asList(URI.create(SERVICE_ENDPOINT_1), URI.create(