diff --git a/api-java-mixin.raml b/api-java-mixin.raml index 4d6baf5d9a4..35c4a7765b5 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-extends): MessageDeliveryPayloadMixin + (java-impl-mixin): | + private MessagePayload messagePayload; + @Override + public MessagePayload getMessagePayload() { + return messagePayload; + } + MessagePayload: + (java-extends): 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..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 @@ -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 messagePayload; + + @Override + public MessagePayload getMessagePayload() { + return messagePayload; + } + /** * 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..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 @@ -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,17 @@ 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.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); + } } 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" +}