IdentifierGenerator { public URI generateIdentifier(URI classUri) { int i = 0; boolean exists; final Property property = ResourceFactory.createProperty(Vocabulary.RDF_TYPE); final RDFNode type = ResourceFactory.createResource(classUri.toString()); URI result; do { result = IdentifierUtils.generateIdentifier(classUri); exists = storageConnector.contains(ResourceFactory.createResource(result.toString()), property, type, null); i++; } while (exists && i < GENERATOR_THRESHOLD); if (i >= GENERATOR_THRESHOLD) { throw new IdentifierGenerationException( "Failed to generate a unique identifier in " + GENERATOR_THRESHOLD + " attempts."); } return result; } IdentifierGenerator(StorageConnector storageConnector); URI generateIdentifier(URI classUri);  }
@Test public void generateIdentifierCreatesIdentifierBasedOnClassUri() { final URI result = generator.generateIdentifier(TYPE_URI); assertNotNull(result); assertTrue(result.toString().contains(TYPE_URI.toString())); }
@Test public void generateIdentifierChecksForExistenceOfNewlyGeneratedIdentifier() { when(connectorMock.contains(any(), any(), any(), anyString())).thenReturn(false); final URI result = generator.generateIdentifier(TYPE_URI); assertNotNull(result); verify(connectorMock) .contains(ResourceFactory.createResource(result.toString()), ResourceFactory.createProperty( Vocabulary.RDF_TYPE), ResourceFactory.createResource(TYPE_URI.toString()), null); }
@Test public void throwsIdentifierGenerationExceptionWhenMaximumAttemptsToGenerateAreExceeded() { when(connectorMock.contains(any(), any(), any(), any())).thenReturn(true); thrown.expect(IdentifierGenerationException.class); generator.generateIdentifier(TYPE_URI); }