From 1489f3926ef8ff9a022b2949f68d7b24d70ca07e Mon Sep 17 00:00:00 2001 From: Holger Knoche Date: Sun, 24 Mar 2024 20:56:13 +0100 Subject: [PATCH] Preparations for mono-to-poly mapping --- .../json/AbstractOperationProxy.java | 1 + .../apievolution/json/JsonMappingTest.java | 53 +++++++++++++++++++ .../json/consumer/ConsumerMonoToPolyType.java | 15 ++++++ .../provider/ProviderMonoToPolySubTypeA.java | 15 ++++++ .../provider/ProviderMonoToPolySubTypeB.java | 15 ++++++ .../json/provider/ProviderMonoToPolyType.java | 15 ++++++ .../resources/apis/provider-revision-2.api | 2 +- 7 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 gutta-apievolution-json/src/test/java/gutta/apievolution/json/consumer/ConsumerMonoToPolyType.java create mode 100644 gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolySubTypeA.java create mode 100644 gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolySubTypeB.java create mode 100644 gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolyType.java diff --git a/gutta-apievolution-json/src/main/java/gutta/apievolution/json/AbstractOperationProxy.java b/gutta-apievolution-json/src/main/java/gutta/apievolution/json/AbstractOperationProxy.java index b064835..6b50056 100644 --- a/gutta-apievolution-json/src/main/java/gutta/apievolution/json/AbstractOperationProxy.java +++ b/gutta-apievolution-json/src/main/java/gutta/apievolution/json/AbstractOperationProxy.java @@ -117,6 +117,7 @@ public JsonNode handleRecordType(RecordType recordType) { ObjectNode objectNode = (ObjectNode) this.representation; + // TODO mono-to-poly and poly-to-mono mappings Optional specificTypeId = determineSpecificTypeId(objectNode); if (specificTypeId.isPresent()) { // If a specific type ID is present, the actual type may be a subtype of diff --git a/gutta-apievolution-json/src/test/java/gutta/apievolution/json/JsonMappingTest.java b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/JsonMappingTest.java index 2be00b1..c515559 100644 --- a/gutta-apievolution-json/src/test/java/gutta/apievolution/json/JsonMappingTest.java +++ b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/JsonMappingTest.java @@ -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; @@ -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; @@ -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 extends ConsumerOperationProxy { @@ -245,5 +266,37 @@ protected ProviderResult invokeOperation(ProviderParameter parameter) { } } + + private static class MonoToPolyMappingConsumerProxy extends ConsumerOperationProxyTemplate { + + public MonoToPolyMappingConsumerProxy(RequestRouter router) { + super("monoToPolyMapping", "ConsumerMonoToPolyType", "ConsumerMonoToPolyType", ConsumerMonoToPolyType.class, router); + } + + } + + private static class MonoToPolyMappingProviderProxy extends ProviderOperationProxyTemplate { + + 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; + } + } + + } } diff --git a/gutta-apievolution-json/src/test/java/gutta/apievolution/json/consumer/ConsumerMonoToPolyType.java b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/consumer/ConsumerMonoToPolyType.java new file mode 100644 index 0000000..ea7aa24 --- /dev/null +++ b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/consumer/ConsumerMonoToPolyType.java @@ -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; + } + +} diff --git a/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolySubTypeA.java b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolySubTypeA.java new file mode 100644 index 0000000..9f80eb1 --- /dev/null +++ b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolySubTypeA.java @@ -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; + } + +} diff --git a/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolySubTypeB.java b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolySubTypeB.java new file mode 100644 index 0000000..7a6735e --- /dev/null +++ b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolySubTypeB.java @@ -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; + } + +} diff --git a/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolyType.java b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolyType.java new file mode 100644 index 0000000..5bab11d --- /dev/null +++ b/gutta-apievolution-json/src/test/java/gutta/apievolution/json/provider/ProviderMonoToPolyType.java @@ -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; + } + +} diff --git a/gutta-apievolution-json/src/test/resources/apis/provider-revision-2.api b/gutta-apievolution-json/src/test/resources/apis/provider-revision-2.api index ba59d90..1315ff7 100644 --- a/gutta-apievolution-json/src/test/resources/apis/provider-revision-2.api +++ b/gutta-apievolution-json/src/test/resources/apis/provider-revision-2.api @@ -30,7 +30,7 @@ api test.provider { } record MonoToPolySubTypeB extends MonoToPolyType as ProviderMonoToPolySubTypeB { - string(10) field3 + string field3 } operation testOperation(TestParameter): TestResult