Skip to content

Commit

Permalink
Fix ITs
Browse files Browse the repository at this point in the history
  • Loading branch information
joostfarla committed Oct 4, 2023
1 parent ea203fb commit e98f9bc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ private Object mapRelation(Object value, DataRequest request) {

if (value instanceof List<?> nestedResultList) {
return nestedResultList.stream()
.map(nestedResult -> {
.flatMap(nestedResult -> {
if (nestedResult instanceof ObjectResult nestedObjectResult) {
return map(nestedObjectResult, request);
return Stream.of(map(nestedObjectResult, request));
}

if (nestedResult instanceof Map<?, ?> nestedMapResult) {
return cast(nestedMapResult);
if (nestedResult instanceof CollectionResult nestedCollectionResult) {
return nestedCollectionResult.getObjectResults()
.stream()
.map(nestedObjectResult -> map(nestedObjectResult, request));
}

throw new OrchestrateException("Could not map nested result");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.list;
import static org.assertj.core.api.InstanceOfAssertFactories.map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import graphql.ExecutionResult;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import nl.geostandaarden.imx.orchestrate.engine.exchange.BatchRequest;
import nl.geostandaarden.imx.orchestrate.engine.exchange.CollectionRequest;
import nl.geostandaarden.imx.orchestrate.engine.exchange.ObjectRequest;
import nl.geostandaarden.imx.orchestrate.engine.exchange.ObjectResult;
import nl.geostandaarden.imx.orchestrate.engine.source.DataRepository;
import nl.geostandaarden.imx.orchestrate.model.ComponentRegistry;
import nl.geostandaarden.imx.orchestrate.model.ModelMapping;
import nl.geostandaarden.imx.orchestrate.model.loader.ModelLoaderRegistry;
import nl.geostandaarden.imx.orchestrate.model.types.ValueTypeRegistry;
import nl.geostandaarden.imx.orchestrate.parser.yaml.YamlModelLoader;
Expand All @@ -26,6 +29,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.test.StepVerifier;

@ExtendWith(MockitoExtension.class)
class EngineIT {
Expand All @@ -35,60 +39,54 @@ class EngineIT {
@Mock
private DataRepository bldRepositoryStub;

// private GraphQL graphQL;
private ModelMapping modelMapping;

private OrchestrateEngine engine;

@BeforeEach
void setUp() throws FileNotFoundException {
var modelLoaderRegistry = new ModelLoaderRegistry()
.register(new YamlModelLoader());
var mappingParser = new YamlModelMappingParser(new ComponentRegistry(), modelLoaderRegistry,
new ValueTypeRegistry());
var mapping = mappingParser.parse(new FileInputStream("../data/geo/mapping.yaml"));

bldRepository = new FileSource(mapping.getSourceModel("bld"), Paths.get("../data/bld")).getDataRepository();
modelMapping = mappingParser.parse(new FileInputStream("../data/geo/mapping.yaml"));
bldRepository = new FileSource(modelMapping.getSourceModel("bld"), Paths.get("../data/bld")).getDataRepository();

var engine = OrchestrateEngine.builder()
.modelMapping(mapping)
engine = OrchestrateEngine.builder()
.modelMapping(modelMapping)
.source("bld", () -> bldRepositoryStub)
.build();

// graphQL = GraphQL.newGraphQL(SchemaFactory.create(orchestration))
// .build();
}

@Test
void queryObject_withoutBatchLoading() {
when(bldRepositoryStub.findOne(any(ObjectRequest.class)))
.thenAnswer(invocation -> bldRepository.findOne(invocation.getArgument(0)));

// var result = graphQL.execute("""
// query {
// building(id: "B0003") {
// id
// surface
// hasAddress {
// id
// houseNumber
// postalCode
// }
// hasLineage {
// orchestratedProperties {
// property
// isDerivedFrom {
// property
// subject {
// objectType
// objectKey
// }
// }
// }
// }
// }
// }
// """);
//
// verify(bldRepositoryStub, times(4)).findOne(any(ObjectRequest.class));
// assertResult(result);
when(bldRepositoryStub.find(any(CollectionRequest.class)))
.thenAnswer(invocation -> bldRepository.find(invocation.getArgument(0)));

var request = ObjectRequest.builder(modelMapping.getTargetModel())
.objectType("Construction")
.objectKey(Map.of("id", "B0002"))
.selectProperty("id")
.selectProperty("surface")
.selectCollectionProperty("hasAddress", b1 -> b1
.selectProperty("id")
.selectProperty("houseNumber")
.selectProperty("postalCode")
.build())
.build();

var result = engine.fetch(request);

StepVerifier.create(result)
.assertNext(this::assertResult)
.verifyComplete();

verify(bldRepositoryStub, times(5)).findOne(any(ObjectRequest.class));
verify(bldRepositoryStub, times(1)).find(any(CollectionRequest.class));
}

@Test
Expand All @@ -98,64 +96,43 @@ void queryObject_withBatchLoading() {
when(bldRepositoryStub.findOne(any(ObjectRequest.class)))
.thenAnswer(invocation -> bldRepository.findOne(invocation.getArgument(0)));

when(bldRepositoryStub.find(any(CollectionRequest.class)))
.thenAnswer(invocation -> bldRepository.find(invocation.getArgument(0)));

when(bldRepositoryStub.findBatch(any(BatchRequest.class)))
.thenAnswer(invocation -> bldRepository.findBatch(invocation.getArgument(0)));

// var result = graphQL.execute("""
// query {
// building(id: "B0003") {
// id
// surface
// hasAddress {
// id
// houseNumber
// postalCode
// }
// hasLineage {
// orchestratedProperties {
// property
// isDerivedFrom {
// property
// subject {
// objectType
// objectKey
// }
// }
// }
// }
// }
// }
// """);
//
// verify(bldRepositoryStub, times(2)).findOne(any(ObjectRequest.class));
// verify(bldRepositoryStub, times(1)).findBatch(any(BatchRequest.class));
// assertResult(result);
var request = ObjectRequest.builder(modelMapping.getTargetModel())
.objectType("Construction")
.objectKey(Map.of("id", "B0002"))
.selectProperty("id")
.selectProperty("surface")
.selectCollectionProperty("hasAddress", b1 -> b1
.selectProperty("id")
.selectProperty("houseNumber")
.selectProperty("postalCode")
.build())
.build();

var result = engine.fetch(request);

StepVerifier.create(result)
.assertNext(this::assertResult)
.verifyComplete();

verify(bldRepositoryStub, times(1)).findOne(any(ObjectRequest.class));
verify(bldRepositoryStub, times(1)).find(any(CollectionRequest.class));
verify(bldRepositoryStub, times(2)).findBatch(any(BatchRequest.class));
}

private void assertResult(ExecutionResult result) {
Map<String, Object> data = result.getData();

assertThat(result.getErrors()).isEmpty();
assertThat(data).isNotNull()
.hasEntrySatisfying("building", building ->
assertThat(building).isNotNull()
.isInstanceOf(Map.class)
.asInstanceOf(map(String.class, Object.class))
.containsEntry("id", "B0003")
.containsEntry("surface", 254)
.hasEntrySatisfying("hasAddress", hasAddress ->
assertThat(hasAddress).isNotNull()
.isInstanceOf(List.class)
.asInstanceOf(list(Map.class))
.hasSize(3))
.hasEntrySatisfying("hasLineage", hasLineage ->
assertThat(hasLineage).isNotNull()
.isInstanceOf(Map.class)
.asInstanceOf(map(String.class, Object.class))
.hasEntrySatisfying("orchestratedProperties", orchestratedProperties ->
assertThat(orchestratedProperties).isNotNull()
.isInstanceOf(List.class)
.asInstanceOf(list(Map.class))
.hasSize(3))));
private void assertResult(ObjectResult result) {
assertThat(result.getProperties()).isNotNull()
.containsEntry("id", "B0002")
.containsEntry("surface", 195)
.hasEntrySatisfying("hasAddress", hasAddress ->
assertThat(hasAddress).isNotNull()
.isInstanceOf(List.class)
.asInstanceOf(list(Map.class))
.hasSize(4));
}
}

0 comments on commit e98f9bc

Please sign in to comment.