ServiceDocumentGenerator implements ServiceDiscovery { @Override public WebResource getServiceDocumentFor(final URI resource, final Routing routing, final String... contentType) { try { final ServiceDocumentImpl doc = new ServiceDocumentImpl(resource, pickMediaType(contentType), routing); extensionBinding.getExtensionsFor(resource).stream() .filter(Extension::isExposing) .map(Extension::exposed).forEach(doc::expose); return doc; } catch (final Exception e) { throw new RuntimeException(e); } }  @Reference void setExtensionBinding(final ExtensionBinding binding); void setRelativeURIs(final boolean relative); void setInterceptURIs(final boolean use); @Override WebResource getServiceDocumentFor(final URI resource, final Routing routing, final String... contentType);  }
@Test public void functionOfTest() throws Exception { when(binding.getExtensionsFor(RESOURCE_URI)).thenReturn( Arrays.asList(EXPOSING_EXTENSION_RESOURCE_SCOPED, EXPOSING_EXTENSION_UNREGISTERED, EXPOSING_EXTENSION_REPOSITORY_SCOPED)); final Model doc = parse(toTest.getServiceDocumentFor(RESOURCE_URI, routing, "text/turtle")); final String sparql = "CONSTRUCT { ?service <test:/rel> ?serviceInstance . } WHERE { " + String.format("?serviceInstance <%s> <%s> . ", PROP_IS_FUNCTION_OF, RESOURCE_URI) + String.format("?serviceInstance <%s> ?service . ", PROP_IS_SERVICE_INSTANCE_OF) + "}"; final Set<URI> matchingServices = subjectsOf(query(sparql, doc)); assertEquals(2, matchingServices.size()); assertTrue(matchingServices.containsAll(Arrays.asList(RESOURCE_SCOPE_SERVICE_URI, UNREGISTERED_SERVICE_URI))); }
@Test public void serviceDocumentForTest() throws Exception { final String SERVICE_DOC_URI = "test:/doc"; when(routing.serviceDocFor(RESOURCE_URI)).thenReturn(URI.create(SERVICE_DOC_URI)); final Model doc = parse(toTest.getServiceDocumentFor(RESOURCE_URI, routing, "text/turtle")); assertTrue(doc.contains( doc.getResource(SERVICE_DOC_URI), doc.getProperty(PROP_IS_SERVICE_DOCUMENT_FOR), doc.getResource(RESOURCE_URI.toString()))); }
@Test public void serviceInstanceOfTest() { when(binding.getExtensionsFor(RESOURCE_URI)).thenReturn( Arrays.asList(EXPOSING_EXTENSION_RESOURCE_SCOPED, EXPOSING_EXTENSION_UNREGISTERED, EXPOSING_EXTENSION_REPOSITORY_SCOPED, INTERCEPTING_EXTENSION)); final Model doc = parse(toTest.getServiceDocumentFor(RESOURCE_URI, routing, "text/turtle")); final String sparql = "CONSTRUCT { ?service <test:/isPresentFromInstance> ?serviceInstance . } WHERE { " + String.format("?serviceInstance <%s> <%s> . ", RDF_TYPE, CLASS_SERVICE_INSTANCE) + String.format("?serviceInstance <%s> ?service . ", PROP_IS_SERVICE_INSTANCE_OF) + "}"; final Set<URI> servicesFromInstances = subjectsOf(query(sparql, doc)); assertEquals(3, servicesFromInstances.size()); assertTrue(servicesFromInstances.containsAll(Arrays.asList(REPOSITORY_SCOPE_SERVICE_URI, RESOURCE_SCOPE_SERVICE_URI, UNREGISTERED_SERVICE_URI))); }
@Test public void endpointURITest() { when(binding.getExtensionsFor(RESOURCE_URI)).thenReturn( Arrays.asList(EXPOSING_EXTENSION_RESOURCE_SCOPED, EXPOSING_EXTENSION_UNREGISTERED, EXPOSING_EXTENSION_REPOSITORY_SCOPED, INTERCEPTING_EXTENSION)); final Model doc = parse(toTest.getServiceDocumentFor(RESOURCE_URI, routing, "text/turtle")); final String sparql = "CONSTRUCT { ?endpoint <test:/endpointFor> ?serviceInstance . } WHERE { " + String.format("?serviceInstance <%s> <%s> . ", RDF_TYPE, CLASS_SERVICE_INSTANCE) + String.format("?serviceInstance <%s> ?endpoint . ", PROP_HAS_ENDPOINT) + "}"; final Set<URI> canonicalEndpoints = subjectsOf(query(sparql, doc)); assertEquals(3, canonicalEndpoints.size()); assertTrue(canonicalEndpoints.containsAll(Arrays.asList(REPOSITORY_SCOPE_ENDPOINT_URI, RESOURCE_SCOPE_ENDPOINT_URI, UNREGISTERED_ENDPOINT_URI))); }
@Test public void contentTypeTest() throws Exception { try (WebResource serviceDoc = toTest.getServiceDocumentFor(RESOURCE_URI, routing, "application/rdf+xml")) { final Model doc = ModelFactory.createDefaultModel().read(serviceDoc.representation(), Lang.RDFXML .getName()); assertTrue(doc.contains( null, doc.getProperty(PROP_IS_SERVICE_DOCUMENT_FOR), doc.getResource(RESOURCE_URI.toString()))); } }
@Test public void defaultContentTypeTest() throws Exception { try (final WebResource serviceDoc = toTest.getServiceDocumentFor(RESOURCE_URI, routing, "not/supported")) { assertEquals("text/turtle", serviceDoc.contentType()); } }
@Test public void emptyRegistriesTest() { when(binding.getExtensionsFor(RESOURCE_URI)).thenReturn(new ArrayList<>()); toTest.getServiceDocumentFor(RESOURCE_URI, routing, "text/turtle"); }