TwoStepInstanceLoader extends EntityInstanceLoader { @Override <T> T loadEntity(LoadingParameters<T> loadingParameters) { final EntityTypeImpl<T> rootEt = metamodel.entity(loadingParameters.getEntityType()); try { final EntityType<? extends T> et = resolveEntityType(loadingParameters, rootEt); if (et == null) { return null; } return loadInstance(loadingParameters, et); } catch (OntoDriverException e) { throw new StorageAccessException(e); } } private  TwoStepInstanceLoader(TwoStepInstanceLoaderBuilder builder);   }
@Test void loadEntityLoadsTypesAndDeterminesEntityTypeBeforeLoadingInstance() throws Exception { final Set<Axiom<URI>> types = Collections.singleton( new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false), new Value<>(URI.create(OWLClassR.getClassIri())))); when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(types); instanceLoader.loadEntity(loadingParameters); verify(typesMock).getTypes(INDIVIDUAL, null, false); verify(descriptorFactoryMock).createForEntityLoading(loadingParameters, metamodelMock.entity(OWLClassR.class)); }
@Test void loadEntityLoadsEntityFromStorageWhenEntityTypeIsDetermined() throws Exception { final Set<Axiom<URI>> types = Collections.singleton( new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false), new Value<>(URI.create(OWLClassR.getClassIri())))); when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(types); final OWLClassR entityR = new OWLClassR(); final Collection<Axiom<?>> axioms = new HashSet<>(types); when(connectionMock.find(axiomDescriptor)).thenReturn(axioms); when(entityConstructorMock .reconstructEntity(IDENTIFIER, metamodelMock.entity(OWLClassR.class), descriptor, axioms)) .thenReturn(entityR); final OWLClassS result = instanceLoader.loadEntity(loadingParameters); assertNotNull(result); assertSame(entityR, result); }
@Test void loadEntityReturnsNullWhenNoTypesForIndividualAreFound() throws Exception { when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(Collections.emptySet()); assertNull(instanceLoader.loadEntity(loadingParameters)); }
@Test void loadEntityReturnsNullWhenNoMatchingEntityTypeIsFound() throws Exception { final Set<Axiom<URI>> types = Collections.singleton( new AxiomImpl<>(INDIVIDUAL, Assertion.createClassAssertion(false), new Value<>(URI.create(OWLClassA.getClassIri())))); when(typesMock.getTypes(INDIVIDUAL, null, false)).thenReturn(types); assertNull(instanceLoader.loadEntity(loadingParameters)); }
@Test void loadEntityThrowsStorageAccessExceptionWhenOntoDriverThrowsException() throws Exception { final String msg = "Exception message."; when(typesMock.getTypes(INDIVIDUAL, null, false)).thenThrow(new OntoDriverException(msg)); final StorageAccessException ex = assertThrows(StorageAccessException.class, () -> instanceLoader.loadEntity(loadingParameters)); assertThat(ex.getMessage(), containsString(msg)); }