TwoStepInstanceLoader extends EntityInstanceLoader { @Override <T> T loadReference(LoadingParameters<T> loadingParameters) { final EntityTypeImpl<T> rootEt = metamodel.entity(loadingParameters.getEntityType()); try { final EntityType<? extends T> et = resolveEntityType(loadingParameters, rootEt); return et != null ? entityBuilder.createEntityInstance(loadingParameters.getIdentifier(), et) : null; } catch (OntoDriverException e) { throw new StorageAccessException(e); } catch (IllegalAccessException | InstantiationException e) { throw new EntityReconstructionException(e); } } private  TwoStepInstanceLoader(TwoStepInstanceLoaderBuilder builder);   }
@Test void loadReferenceLoadsReferenceFromStorageWhenEntityTypeIsDetermined() throws Exception { final Axiom<URI> type = new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false), new Value<>(URI.create(OWLClassA.getClassIri()))); when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(Collections.singleton(type)); when(entityConstructorMock.createEntityInstance(IDENTIFIER, metamodelMock.entity(OWLClassA.class))) .thenReturn(new OWLClassA(IDENTIFIER)); final OWLClassA result = instanceLoader.loadReference(new LoadingParameters<>(OWLClassA.class, IDENTIFIER, descriptor)); assertNotNull(result); verify(typesMock).getTypes(INDIVIDUAL, null, false); }
@Test void loadReferenceReturnsNullWhenTypeCannotBeResolved() throws Exception { when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(Collections.emptySet()); when(entityConstructorMock.createEntityInstance(IDENTIFIER, metamodelMock.entity(OWLClassA.class))) .thenReturn(new OWLClassA(IDENTIFIER)); final OWLClassA result = instanceLoader.loadReference(new LoadingParameters<>(OWLClassA.class, IDENTIFIER, descriptor)); assertNull(result); verify(entityConstructorMock, never()).createEntityInstance(any(), any()); }
@Test void loadReferenceThrowsStorageAccessExceptionWhenOntoDriverExceptionIsThrown() throws Exception { final String msg = "Exception message."; when(typesMock.getTypes(INDIVIDUAL, null, false)).thenThrow(new OntoDriverException(msg)); final StorageAccessException ex = assertThrows(StorageAccessException.class, () -> instanceLoader.loadReference(loadingParameters)); assertThat(ex.getMessage(), containsString(msg)); verify(entityConstructorMock, never()).createEntityInstance(any(), any()); }