Skip to content

Commit

Permalink
Preparations for mono-to-poly mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerknoche committed Mar 24, 2024
1 parent 235520b commit 1489f39
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public JsonNode handleRecordType(RecordType<?, ?, ?> recordType) {

ObjectNode objectNode = (ObjectNode) this.representation;

// TODO mono-to-poly and poly-to-mono mappings
Optional<String> specificTypeId = determineSpecificTypeId(objectNode);
if (specificTypeId.isPresent()) {
// If a specific type ID is present, the actual type may be a subtype of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gutta.apievolution.dsl.ConsumerApiLoader;
import gutta.apievolution.dsl.ProviderApiLoader;
import gutta.apievolution.json.consumer.ConsumerEnum;
import gutta.apievolution.json.consumer.ConsumerMonoToPolyType;
import gutta.apievolution.json.consumer.ConsumerOperationProxy;
import gutta.apievolution.json.consumer.ConsumerParameter;
import gutta.apievolution.json.consumer.ConsumerResult;
Expand All @@ -16,6 +17,8 @@
import gutta.apievolution.json.consumer.ConsumerTestExceptionData;
import gutta.apievolution.json.provider.MappableProviderTestException;
import gutta.apievolution.json.provider.ProviderEnum;
import gutta.apievolution.json.provider.ProviderMonoToPolySubTypeA;
import gutta.apievolution.json.provider.ProviderMonoToPolyType;
import gutta.apievolution.json.provider.ProviderOperationProxy;
import gutta.apievolution.json.provider.ProviderParameter;
import gutta.apievolution.json.provider.ProviderResult;
Expand Down Expand Up @@ -113,6 +116,24 @@ void exceptionMapping() {

assertEquals(1234, exception.getExceptionField());
}

/**
* Test case: Mono-to-poly mapping (parameter) and vice versa (result).
*/
@Test
void monoToPolyTypeMapping() {
MonoToPolyMappingProviderProxy providerProxy = new MonoToPolyMappingProviderProxy();
RequestRouter requestRouter = new SimpleJsonRequestRouter(providerProxy);

ConsumerMonoToPolyType parameter = new ConsumerMonoToPolyType();
parameter.setField1(1234);

MonoToPolyMappingConsumerProxy consumerProxy = new MonoToPolyMappingConsumerProxy(requestRouter);
ConsumerMonoToPolyType result = consumerProxy.invokeOperation(parameter);

assertNotSame(parameter, result);
assertEquals(parameter, result);
}

private abstract static class ConsumerOperationProxyTemplate<P, R> extends ConsumerOperationProxy<P, R> {

Expand Down Expand Up @@ -245,5 +266,37 @@ protected ProviderResult invokeOperation(ProviderParameter parameter) {
}

}

private static class MonoToPolyMappingConsumerProxy extends ConsumerOperationProxyTemplate<ConsumerMonoToPolyType, ConsumerMonoToPolyType> {

public MonoToPolyMappingConsumerProxy(RequestRouter router) {
super("monoToPolyMapping", "ConsumerMonoToPolyType", "ConsumerMonoToPolyType", ConsumerMonoToPolyType.class, router);
}

}

private static class MonoToPolyMappingProviderProxy extends ProviderOperationProxyTemplate<ProviderMonoToPolyType, ProviderMonoToPolyType> {

public MonoToPolyMappingProviderProxy() {
super("monoToPolyMapping", "ProviderMonoToPolyType", "ProviderMonoToPolyType", ProviderMonoToPolyType.class);
}

@Override
protected ProviderMonoToPolyType invokeOperation(ProviderMonoToPolyType parameter) {
if (parameter.getField1() == 1) {
ProviderMonoToPolySubTypeA result = new ProviderMonoToPolySubTypeA();
result.setField1(5678);
result.setField2(4321);

return result;
} else {
ProviderMonoToPolyType result = new ProviderMonoToPolyType();
result.setField1(1234);

return result;
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gutta.apievolution.json.consumer;

public class ConsumerMonoToPolyType {

private Integer field1;

public Integer getField1() {
return this.field1;
}

public void setField1(Integer field1) {
this.field1 = field1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gutta.apievolution.json.provider;

public class ProviderMonoToPolySubTypeA extends ProviderMonoToPolyType {

private Integer field2;

public Integer getField2() {
return this.field2;
}

public void setField2(Integer field2) {
this.field2 = field2;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gutta.apievolution.json.provider;

public class ProviderMonoToPolySubTypeB extends ProviderMonoToPolyType {

private String field3;

public String getField3() {
return this.field3;
}

public void setField3(String field3) {
this.field3 = field3;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gutta.apievolution.json.provider;

public class ProviderMonoToPolyType {

private Integer field1;

public Integer getField1() {
return this.field1;
}

public void setField1(Integer field1) {
this.field1 = field1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ api test.provider {
}

record MonoToPolySubTypeB extends MonoToPolyType as ProviderMonoToPolySubTypeB {
string(10) field3
string field3
}

operation testOperation(TestParameter): TestResult
Expand Down

0 comments on commit 1489f39

Please sign in to comment.