LoaderService { public synchronized URI load(final WebResource resource) { final byte[] body = toByteArray(resource); final Model model = parse(WebResource.of(new ByteArrayInputStream(body), resource.contentType(), resource .uri(), null), resource.uri().toString()); final int extensionCount = subjectsOf(RDF_TYPE, CLASS_EXTENSION, model).size(); final int definedServiceCount = allDefinedServices(model).size(); if (extensionCount > 1) { throw new RuntimeException("Too many extension URIs in " + resource.uri()); } URI depositedResource = null; if (extensionCount == 1) { LOG.info("Loading extension definition"); depositedResource = extensionRegistry.put( WebResource.of( new ByteArrayInputStream(body), resource.contentType(), findMatchingExtension(model), extensionName(model)), useBinary(model)); } else if (definedServiceCount > 0) { LOG.info("Adding new service to registry"); depositedResource = serviceRegistry.put( WebResource.of(new ByteArrayInputStream(body), resource.contentType())); } else { throw new RuntimeException(String.format( "Cannot load resource <%s>. Does not describe exactly one extension (%d), " + "or define any services:\n===\n%s\n===", resource.uri(), extensionCount, new String(body))); } final Set<URI> allServices = loadServices(depositedResource); if (allServices.size() == 1) { try (Service service = serviceRegistry.getService(allServices.iterator().next())) { LOG.info("Adding instance <> of service <>", resource.uri(), service.canonicalURI()); addInstance(resource.uri(), service); } catch (final Exception e) { throw new RuntimeException(String.format("Error getting service <%s> from registry", resource.uri()), e); } } else { LOG.info("Extension does not define a service, so not adding any"); return depositedResource; } return depositedResource; }  void setServiceRegistry(final ServiceRegistry registry); void setExtensionRegistry(final ExtensionRegistry registry); void setGeneralRegistry(final Registry registry); synchronized URI load(final WebResource resource);  }
@Test public void referencedServiceNotExistsTest() { when(generalRegistry.get(eq(PERSISTED_EXTENSION_URI))).thenReturn( rdfResource(PERSISTED_EXTENSION, triple(PERSISTED_EXTENSION, RDF_TYPE, CLASS_EXTENSION) + triple(PERSISTED_EXTENSION, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL))); when(serviceRegistry.contains(any(URI.class))).thenReturn(false); when(serviceRegistry.instancesOf(any(Service.class))).thenAnswer(i -> { throw new ResourceNotFoundException("not found"); }); final URI loadResult = toTest.load(rdfResource(SERVICE_ENDPOINT, triple(SERVICE_ENDPOINT, RDF_TYPE, CLASS_EXTENSION) + triple(SERVICE_ENDPOINT, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL))); verify(serviceInstanceRegistry).addEndpoint(SERVICE_ENDPOINT_URI); verify(serviceRegistry).createInstanceRegistry(eq(service)); verify(serviceRegistry).put(any(WebResource.class)); verify(extensionRegistry).put(any(WebResource.class), eq(false)); assertEquals(PERSISTED_EXTENSION_URI, loadResult); }
@Test public void referencedServiceExistsTest() { when(generalRegistry.get(eq(PERSISTED_EXTENSION_URI))).thenReturn( rdfResource(PERSISTED_EXTENSION, triple(PERSISTED_EXTENSION, RDF_TYPE, CLASS_EXTENSION) + triple(PERSISTED_EXTENSION, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL))); when(serviceRegistry.contains(any(URI.class))).thenReturn(true); when(serviceRegistry.getService(eq(SERVICE_CANONICAL_URI))).thenReturn(service); when(serviceRegistry.instancesOf(any(Service.class))).thenAnswer(i -> { throw new ResourceNotFoundException("not found"); }); final URI loadResult = toTest.load(rdfResource(SERVICE_ENDPOINT, triple(SERVICE_ENDPOINT, RDF_TYPE, CLASS_EXTENSION) + triple(SERVICE_ENDPOINT, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL))); verify(serviceRegistry, times(0)).put(any(WebResource.class)); verify(serviceInstanceRegistry).addEndpoint(SERVICE_ENDPOINT_URI); verify(serviceRegistry).createInstanceRegistry(eq(service)); verify(extensionRegistry).put(any(WebResource.class), eq(false)); assertEquals(PERSISTED_EXTENSION_URI, loadResult); }
@Test public void referencedServiceAndInstanceRegistryExistsTest() { when(generalRegistry.get(eq(PERSISTED_EXTENSION_URI))).thenReturn( rdfResource(PERSISTED_EXTENSION, triple(PERSISTED_EXTENSION, RDF_TYPE, CLASS_EXTENSION) + triple(PERSISTED_EXTENSION, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL))); when(serviceRegistry.contains(any(URI.class))).thenReturn(true); when(serviceRegistry.getService(eq(SERVICE_CANONICAL_URI))).thenReturn(service); when(serviceRegistry.instancesOf(eq(service))).thenAnswer(i -> serviceInstanceRegistry); final URI loadResult = toTest.load(rdfResource(SERVICE_ENDPOINT, triple(SERVICE_ENDPOINT, RDF_TYPE, CLASS_EXTENSION) + triple(SERVICE_ENDPOINT, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL))); verify(serviceRegistry, times(0)).put(any(WebResource.class)); verify(serviceRegistry, times(0)).createInstanceRegistry(any(Service.class)); verify(serviceRegistry).instancesOf(eq(service)); verify(serviceInstanceRegistry).addEndpoint(SERVICE_ENDPOINT_URI); verify(extensionRegistry).put(any(WebResource.class), eq(false)); assertEquals(PERSISTED_EXTENSION_URI, loadResult); }
@Test public void newDefinedServiceTest() { final String LOCALLY_DEFINED_SERVICE = "http: final URI LOCALLY_DEFINED_SERVICE_URI = URI.create(LOCALLY_DEFINED_SERVICE); when(generalRegistry.get(eq(PERSISTED_EXTENSION_URI))).thenReturn( rdfResource(PERSISTED_EXTENSION, triple(PERSISTED_EXTENSION, RDF_TYPE, CLASS_EXTENSION) + triple(PERSISTED_EXTENSION, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL) + triple(LOCALLY_DEFINED_SERVICE, RDF_TYPE, CLASS_SERVICE) + triple(LOCALLY_DEFINED_SERVICE, PROP_CANONICAL, SERVICE_CANONICAL))); when(serviceRegistry.contains(any(URI.class))).thenReturn(false); when(serviceRegistry.instancesOf(any(Service.class))).thenAnswer(i -> { throw new ResourceNotFoundException("not found"); }); when(serviceRegistry.getService(LOCALLY_DEFINED_SERVICE_URI)).thenReturn(service); final URI loadResult = toTest.load(rdfResource(SERVICE_ENDPOINT, triple(SERVICE_ENDPOINT, RDF_TYPE, CLASS_EXTENSION) + triple(SERVICE_ENDPOINT, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL))); verify(serviceRegistry, times(1)).put(any(WebResource.class)); verify(serviceRegistry).createInstanceRegistry(eq(service)); verify(serviceInstanceRegistry).addEndpoint(SERVICE_ENDPOINT_URI); verify(extensionRegistry).put(any(WebResource.class), eq(false)); assertEquals(PERSISTED_EXTENSION_URI, loadResult); }
@Test public void existingDefinedServiceTest() { final String LOCALLY_DEFINED_SERVICE = "http: final URI LOCALLY_DEFINED_SERVICE_URI = URI.create(LOCALLY_DEFINED_SERVICE); when(generalRegistry.get(eq(PERSISTED_EXTENSION_URI))).thenReturn( rdfResource(PERSISTED_EXTENSION, triple(PERSISTED_EXTENSION, RDF_TYPE, CLASS_EXTENSION) + triple(PERSISTED_EXTENSION, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL) + triple(LOCALLY_DEFINED_SERVICE, RDF_TYPE, CLASS_SERVICE) + triple(LOCALLY_DEFINED_SERVICE, PROP_CANONICAL, SERVICE_CANONICAL))); when(serviceRegistry.contains(eq(SERVICE_CANONICAL_URI))).thenReturn(true); when(serviceRegistry.getService(eq(SERVICE_CANONICAL_URI))).thenReturn(service); when(serviceRegistry.instancesOf(any(Service.class))).thenAnswer(i -> { throw new ResourceNotFoundException("not found"); }); when(serviceRegistry.getService(LOCALLY_DEFINED_SERVICE_URI)).thenReturn(service); final URI loadResult = toTest.load(rdfResource(SERVICE_ENDPOINT, triple(SERVICE_ENDPOINT, RDF_TYPE, CLASS_EXTENSION) + triple(SERVICE_ENDPOINT, PROP_CONSUMES_SERVICE, SERVICE_CANONICAL))); verify(serviceRegistry, times(0)).put(any(WebResource.class)); verify(serviceRegistry, times(0)).register(any(URI.class)); verify(serviceRegistry).createInstanceRegistry(eq(service)); verify(serviceInstanceRegistry).addEndpoint(SERVICE_ENDPOINT_URI); verify(extensionRegistry).put(any(WebResource.class), eq(false)); assertEquals(PERSISTED_EXTENSION_URI, loadResult); }
@Test public void duplicateExtensionTest() { final URI EXPOSED_AT = URI.create("exposed()"); final ArgumentCaptor<WebResource> arg = ArgumentCaptor.forClass(WebResource.class); when(generalRegistry.get(eq(PERSISTED_EXTENSION_URI))).thenReturn( rdfResource(PERSISTED_EXTENSION, triple(PERSISTED_EXTENSION, RDF_TYPE, CLASS_EXTENSION) + ltriple(SERVICE_ENDPOINT, PROP_EXPOSES_SERVICE_AT, EXPOSED_AT.toString()) + triple(PERSISTED_EXTENSION, PROP_EXPOSES_SERVICE, SERVICE_CANONICAL))); when(serviceRegistry.contains(any(URI.class))).thenReturn(false); when(serviceRegistry.instancesOf(any(Service.class))).thenAnswer(i -> { throw new ResourceNotFoundException("not found"); }); final Extension existing = mock(Extension.class); final ServiceExposureSpec spec = mock(ServiceExposureSpec.class); when(spec.exposedAt()).thenReturn(EXPOSED_AT); when(existing.uri()).thenReturn(PERSISTED_EXTENSION_URI); when(existing.isExposing()).thenReturn(true); when(existing.exposed()).thenReturn(spec); when(extensionRegistry.getExtensions()).thenReturn(Arrays.asList(existing)); final URI loadResult = toTest.load(rdfResource(SERVICE_ENDPOINT, triple(SERVICE_ENDPOINT, RDF_TYPE, CLASS_EXTENSION) + ltriple(SERVICE_ENDPOINT, PROP_EXPOSES_SERVICE_AT, EXPOSED_AT.toString()) + triple(SERVICE_ENDPOINT, PROP_EXPOSES_SERVICE, SERVICE_CANONICAL))); verify(serviceInstanceRegistry).addEndpoint(SERVICE_ENDPOINT_URI); verify(serviceRegistry).createInstanceRegistry(eq(service)); verify(serviceRegistry).put(any(WebResource.class)); assertEquals(PERSISTED_EXTENSION_URI, loadResult); verify(extensionRegistry).put(arg.capture(), eq(false)); assertEquals(PERSISTED_EXTENSION_URI, arg.getValue().uri()); }