IdentifierGenerator { public URI generateIdentifier(URI classUri) { boolean unique = false; URI id = null; int counter = 0; while (!unique && counter++ < GENERATION_THRESHOLD) { id = IdentifierUtils.generateIdentifier(classUri); unique = isIdentifierUnique(id); } if (!unique) { throw new IdentifierGenerationException("Unable to generate a unique identifier."); } return id; } IdentifierGenerator(OWLOntology ontology); URI generateIdentifier(URI classUri);  }
@Test void generatesUniqueIdentifier() { final URI baseUri = URI.create("http: when(ontologyMock.containsIndividualInSignature(any(IRI.class))).thenReturn(false); final Set<URI> results = new HashSet<>(); int tries = 100; for (int i = 0; i < tries; i++) { results.add(generator.generateIdentifier(baseUri)); } results.forEach(uri -> assertTrue(uri.toString().contains(baseUri.toString()))); assertEquals(tries, results.size()); }
@Test void throwsExceptionWhenUnableToGenerateUriWithinThreshold() throws Exception { final URI baseUri = URI.create("http: final int threshold = extractThreshold(); when(ontologyMock.containsIndividualInSignature(any(IRI.class))).thenReturn(true); try { assertThrows(IdentifierGenerationException.class, () -> generator.generateIdentifier(baseUri)); } finally { verify(ontologyMock, times(threshold)).containsIndividualInSignature(any(IRI.class)); } }