Skip to content

Commit

Permalink
deserialize message payload
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Nov 8, 2023
1 parent 712998b commit 461edd5
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 deletions.
13 changes: 13 additions & 0 deletions api-java-mixin.raml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ annotationTypes:
java-implements:
type: string
allowedTargets: TypeDeclaration
java-impl-mixin:
type: string
allowedTargets: TypeDeclaration

types:
BaseAddress:
Expand Down Expand Up @@ -1393,7 +1396,17 @@ types:
(java-extends): 'com.commercetools.api.models.Identifiable<AssociateRole>'
AssociateRoleUpdateAction:
(java-extends): 'com.commercetools.api.models.ResourceUpdateAction<AssociateRoleUpdateAction>'
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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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 {

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

package com.commercetools.api.models.subscription;

import com.commercetools.api.models.message.MessagePayload;

public interface MessagePayloadMixin {
@SuppressWarnings("unchecked")
default <M extends MessagePayload> M as(Class<M> clazz) {
if (clazz.isInstance(this)) {
return (M) this;
}
throw new IllegalArgumentException("message type mismatch");
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"notificationType": "Message",
"projectKey": "<project_key>",
"id": "<message_id>",
"version": 1,
"sequenceNumber": 2,
"resource": {
"typeId": "customer",
"id": "<customer_id>"
},
"resourceVersion": 2,
"resourceUserProvidedIdentifiers": {},
"type": "CustomerLastNameSet",
"lastName": "Doe",
"createdAt": "2022-10-25T13:30:09.760Z",
"lastModifiedAt": "2022-10-25T13:30:09.760Z"
}

0 comments on commit 461edd5

Please sign in to comment.