From 461edd5d35804b97d0fc8400dbde28c6279466ec Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 8 Nov 2023 10:26:57 +0100 Subject: [PATCH 1/5] deserialize message payload --- api-java-mixin.raml | 13 +++++++++++ .../api/models/message/MessagePayload.java | 3 ++- .../subscription/MessageDeliveryPayload.java | 2 +- .../MessageDeliveryPayloadImpl.java | 8 +++++++ .../MessageDeliveryPayloadMixin.java | 22 +++++++++++++++++++ .../subscription/MessagePayloadMixin.java | 14 ++++++++++++ .../com/commercetools/MessagePayloadTest.java | 17 ++++++++++++++ .../resources/messagedeliverypayload.json | 17 ++++++++++++++ 8 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/subscription/MessageDeliveryPayloadMixin.java create mode 100644 commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/subscription/MessagePayloadMixin.java create mode 100644 commercetools/commercetools-sdk-java-api/src/test/resources/messagedeliverypayload.json diff --git a/api-java-mixin.raml b/api-java-mixin.raml index 4d6baf5d9a4..0d0bccbeede 100644 --- a/api-java-mixin.raml +++ b/api-java-mixin.raml @@ -20,6 +20,9 @@ annotationTypes: java-implements: type: string allowedTargets: TypeDeclaration + java-impl-mixin: + type: string + allowedTargets: TypeDeclaration types: BaseAddress: @@ -1393,7 +1396,17 @@ types: (java-extends): 'com.commercetools.api.models.Identifiable' AssociateRoleUpdateAction: (java-extends): 'com.commercetools.api.models.ResourceUpdateAction' + MessageDeliveryPayload: + (java-implements): MessageDeliveryPayloadMixin + (java-impl-mixin): | + private MessagePayload message; + @Override + public MessagePayload getMessagePayload() { + return message; + } + MessagePayload: + (java-implements): MessagePayloadMixin /{projectKey}: /categories: (java-implements): 'ByProjectKeyCategoriesRequestBuilderMixin' diff --git a/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/message/MessagePayload.java b/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/message/MessagePayload.java index e1f6efe3cd3..c1cd61248eb 100644 --- a/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/message/MessagePayload.java +++ b/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/message/MessagePayload.java @@ -8,6 +8,7 @@ import javax.annotation.Nullable; import javax.validation.constraints.NotNull; +import com.commercetools.api.models.subscription.MessagePayloadMixin; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.annotation.*; @@ -245,7 +246,7 @@ @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", defaultImpl = MessagePayloadImpl.class, visible = true) @JsonDeserialize(as = MessagePayloadImpl.class) @Generated(value = "io.vrap.rmf.codegen.rendering.CoreCodeGenerator", comments = "https://github.com/commercetools/rmf-codegen") -public interface MessagePayload { +public interface MessagePayload extends MessagePayloadMixin { /** * diff --git a/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayload.java b/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayload.java index eadc6d5f9fc..2d50361fe45 100644 --- a/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayload.java +++ b/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayload.java @@ -37,7 +37,7 @@ */ @Generated(value = "io.vrap.rmf.codegen.rendering.CoreCodeGenerator", comments = "https://github.com/commercetools/rmf-codegen") @JsonDeserialize(as = MessageDeliveryPayloadImpl.class) -public interface MessageDeliveryPayload extends DeliveryPayload { +public interface MessageDeliveryPayload extends DeliveryPayload, MessageDeliveryPayloadMixin { /** * discriminator value for MessageDeliveryPayload diff --git a/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayloadImpl.java b/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayloadImpl.java index 795f2594199..56d838d42b1 100644 --- a/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayloadImpl.java +++ b/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayloadImpl.java @@ -4,6 +4,7 @@ import java.time.*; import java.util.*; +import com.commercetools.api.models.message.MessagePayload; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.*; @@ -42,6 +43,13 @@ public class MessageDeliveryPayloadImpl implements MessageDeliveryPayload, Model private com.commercetools.api.models.subscription.PayloadNotIncluded payloadNotIncluded; + private MessagePayload message; + + @Override + public MessagePayload getMessage() { + return message; + } + /** * create instance with all properties */ diff --git a/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/subscription/MessageDeliveryPayloadMixin.java b/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/subscription/MessageDeliveryPayloadMixin.java new file mode 100644 index 00000000000..d74ec126023 --- /dev/null +++ b/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/subscription/MessageDeliveryPayloadMixin.java @@ -0,0 +1,22 @@ + +package com.commercetools.api.models.subscription; + +import com.commercetools.api.models.message.MessagePayload; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonUnwrapped; + +public interface MessageDeliveryPayloadMixin { + @JsonUnwrapped + MessagePayload getMessagePayload(); + + /** + * Returns true if this payload contains a complete message. + * + * @return true if this payload contains a complete message + * @see #getMessagePayload() + */ + @JsonIgnore + default boolean hasCompleteMessage() { + return getMessagePayload().getType() != null; + } +} diff --git a/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/subscription/MessagePayloadMixin.java b/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/subscription/MessagePayloadMixin.java new file mode 100644 index 00000000000..b23c09a4150 --- /dev/null +++ b/commercetools/commercetools-sdk-java-api/src/main/java/com/commercetools/api/models/subscription/MessagePayloadMixin.java @@ -0,0 +1,14 @@ + +package com.commercetools.api.models.subscription; + +import com.commercetools.api.models.message.MessagePayload; + +public interface MessagePayloadMixin { + @SuppressWarnings("unchecked") + default M as(Class clazz) { + if (clazz.isInstance(this)) { + return (M) this; + } + throw new IllegalArgumentException("message type mismatch"); + } +} diff --git a/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java b/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java index cfceb3bea8c..6f815f3b2bf 100644 --- a/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java +++ b/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java @@ -1,10 +1,15 @@ package com.commercetools; +import static com.commercetools.TestUtils.stringFromResource; + import java.io.IOException; +import com.commercetools.api.models.message.CustomerFirstNameSetMessagePayload; +import com.commercetools.api.models.message.CustomerLastNameSetMessagePayload; import com.commercetools.api.models.message.MessagePayload; import com.commercetools.api.models.message.OrderCreatedMessagePayload; +import com.commercetools.api.models.subscription.MessageDeliveryPayload; import io.vrap.rmf.base.client.utils.json.JsonUtils; @@ -20,4 +25,16 @@ public void deserializeOrderCreatedMessageFromString() throws IOException { Assertions.assertThat(deserializedObject).isInstanceOf(OrderCreatedMessagePayload.class); } + + @Test + public void deserializeMessageDeliveryPayload() { + MessageDeliveryPayload delivery = JsonUtils.fromJsonString(stringFromResource("messagedeliverypayload.json"), + MessageDeliveryPayload.class); + + Assertions.assertThat(delivery.getMessage()).isInstanceOf(CustomerLastNameSetMessagePayload.class); + Assertions.assertThat(delivery.getMessage().as(CustomerLastNameSetMessagePayload.class)) + .isInstanceOf(CustomerLastNameSetMessagePayload.class); + Assertions.assertThatThrownBy(() -> delivery.getMessage().as(CustomerFirstNameSetMessagePayload.class)) + .isInstanceOf(IllegalArgumentException.class); + } } diff --git a/commercetools/commercetools-sdk-java-api/src/test/resources/messagedeliverypayload.json b/commercetools/commercetools-sdk-java-api/src/test/resources/messagedeliverypayload.json new file mode 100644 index 00000000000..53e96ffedd4 --- /dev/null +++ b/commercetools/commercetools-sdk-java-api/src/test/resources/messagedeliverypayload.json @@ -0,0 +1,17 @@ +{ + "notificationType": "Message", + "projectKey": "", + "id": "", + "version": 1, + "sequenceNumber": 2, + "resource": { + "typeId": "customer", + "id": "" + }, + "resourceVersion": 2, + "resourceUserProvidedIdentifiers": {}, + "type": "CustomerLastNameSet", + "lastName": "Doe", + "createdAt": "2022-10-25T13:30:09.760Z", + "lastModifiedAt": "2022-10-25T13:30:09.760Z" +} From 04802d1eff48673970381514c668a9bee67a663f Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 8 Nov 2023 10:30:17 +0100 Subject: [PATCH 2/5] typo in mixin.raml --- api-java-mixin.raml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api-java-mixin.raml b/api-java-mixin.raml index 0d0bccbeede..de28d53509c 100644 --- a/api-java-mixin.raml +++ b/api-java-mixin.raml @@ -1397,7 +1397,7 @@ types: AssociateRoleUpdateAction: (java-extends): 'com.commercetools.api.models.ResourceUpdateAction' MessageDeliveryPayload: - (java-implements): MessageDeliveryPayloadMixin + (java-extends): MessageDeliveryPayloadMixin (java-impl-mixin): | private MessagePayload message; @@ -1406,7 +1406,7 @@ types: return message; } MessagePayload: - (java-implements): MessagePayloadMixin + (java-extends): MessagePayloadMixin /{projectKey}: /categories: (java-implements): 'ByProjectKeyCategoriesRequestBuilderMixin' From 5296f5764294ab5db3883b6af10bf3ee66c53bfa Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 8 Nov 2023 10:40:08 +0100 Subject: [PATCH 3/5] typo --- api-java-mixin.raml | 4 ++-- .../api/models/subscription/MessageDeliveryPayloadImpl.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/api-java-mixin.raml b/api-java-mixin.raml index de28d53509c..35c4a7765b5 100644 --- a/api-java-mixin.raml +++ b/api-java-mixin.raml @@ -1399,11 +1399,11 @@ types: MessageDeliveryPayload: (java-extends): MessageDeliveryPayloadMixin (java-impl-mixin): | - private MessagePayload message; + private MessagePayload messagePayload; @Override public MessagePayload getMessagePayload() { - return message; + return messagePayload; } MessagePayload: (java-extends): MessagePayloadMixin diff --git a/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayloadImpl.java b/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayloadImpl.java index 56d838d42b1..520628b296f 100644 --- a/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayloadImpl.java +++ b/commercetools/commercetools-sdk-java-api/src/main/java-generated/com/commercetools/api/models/subscription/MessageDeliveryPayloadImpl.java @@ -43,11 +43,11 @@ public class MessageDeliveryPayloadImpl implements MessageDeliveryPayload, Model private com.commercetools.api.models.subscription.PayloadNotIncluded payloadNotIncluded; - private MessagePayload message; + private MessagePayload messagePayload; @Override - public MessagePayload getMessage() { - return message; + public MessagePayload getMessagePayload() { + return messagePayload; } /** From 8236b399d19c5b48bac7609906a195533c17cb8e Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 8 Nov 2023 10:59:49 +0100 Subject: [PATCH 4/5] typo in test --- .../src/test/java/com/commercetools/MessagePayloadTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java b/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java index 6f815f3b2bf..3c565a50a9d 100644 --- a/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java +++ b/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java @@ -31,10 +31,10 @@ public void deserializeMessageDeliveryPayload() { MessageDeliveryPayload delivery = JsonUtils.fromJsonString(stringFromResource("messagedeliverypayload.json"), MessageDeliveryPayload.class); - Assertions.assertThat(delivery.getMessage()).isInstanceOf(CustomerLastNameSetMessagePayload.class); - Assertions.assertThat(delivery.getMessage().as(CustomerLastNameSetMessagePayload.class)) + Assertions.assertThat(delivery.getMessagePayload()).isInstanceOf(CustomerLastNameSetMessagePayload.class); + Assertions.assertThat(delivery.getMessagePayload().as(CustomerLastNameSetMessagePayload.class)) .isInstanceOf(CustomerLastNameSetMessagePayload.class); - Assertions.assertThatThrownBy(() -> delivery.getMessage().as(CustomerFirstNameSetMessagePayload.class)) + Assertions.assertThatThrownBy(() -> delivery.getMessagePayload().as(CustomerFirstNameSetMessagePayload.class)) .isInstanceOf(IllegalArgumentException.class); } } From 11db4b92b5055489c182229baf1bc10b4c2c7163 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 8 Nov 2023 11:25:17 +0100 Subject: [PATCH 5/5] add complete message assertion to test --- .../src/test/java/com/commercetools/MessagePayloadTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java b/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java index 3c565a50a9d..ec6f4179ca8 100644 --- a/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java +++ b/commercetools/commercetools-sdk-java-api/src/test/java/com/commercetools/MessagePayloadTest.java @@ -34,6 +34,7 @@ public void deserializeMessageDeliveryPayload() { Assertions.assertThat(delivery.getMessagePayload()).isInstanceOf(CustomerLastNameSetMessagePayload.class); Assertions.assertThat(delivery.getMessagePayload().as(CustomerLastNameSetMessagePayload.class)) .isInstanceOf(CustomerLastNameSetMessagePayload.class); + Assertions.assertThat(delivery.hasCompleteMessage()).isTrue(); Assertions.assertThatThrownBy(() -> delivery.getMessagePayload().as(CustomerFirstNameSetMessagePayload.class)) .isInstanceOf(IllegalArgumentException.class); }