Skip to content
This repository was archived by the owner on Aug 13, 2020. It is now read-only.

JsonEnvelope Cookbook

allanmckenzie edited this page Dec 8, 2016 · 11 revisions

To create a full JsonEnvelope

import static uk.gov.justice.services.messaging.DefaultJsonEnvelope.envelope;
import static uk.gov.justice.services.messaging.JsonObjectMetadata.metadataWithRandomUUID;

    final UUID correlationId = randomUUID();
    final UUID sessionId = randomUUID();
    final UUID userId = randomUUID();
    final UUID streamId = randomUUID();
    final String commandName = "notification-added";

    final JsonEnvelope command = envelope()
             .with(metadataWithRandomUUID(commandName)
                    .withClientCorrelationId(correlationId.toString())
                    .withSessionId(sessionId.toString())
                    .withUserId(userId.toString())
                    .withStreamId(streamId))
             .withPayloadOf("Example Value", "exampleField")
             .build();

To create an envelope with an array

final JsonEnvelope envelope = DefaultJsonEnvelope.envelopeFrom(
            JsonObjectMetadata.metadataWithRandomUUID("some-command-name")
                    .withUserId(USER_ID.toString()),
            Json.createObjectBuilder()
                    .add("myArray", Json.createArrayBuilder()
                            .add(Json.createObjectBuilder()
                                    .add("firstName", "Fred")
                                    .add("lastName", "Boggs")
                                    .build())
                            .add(Json.createObjectBuilder()
                                    .add("firstName", "Mavis")
                                    .add("lastName", "Davies")
                                    .build())
                            .build())
                    .build());

To assert a JsonEnvelope

import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static uk.gov.justice.services.messaging.DefaultJsonEnvelope.envelope;
import static uk.gov.justice.services.messaging.JsonObjectMetadata.metadataOf;
import static uk.gov.justice.services.test.utils.core.matchers.JsonEnvelopeMatcher.jsonEnvelope;
import static uk.gov.justice.services.test.utils.core.matchers.JsonEnvelopeMetadataMatcher.metadata;
import static uk.gov.justice.services.test.utils.core.matchers.JsonEnvelopePayloadMatcher.payLoad;

    final UUID commandId = UUID.randomUUID();
    final JsonEnvelope command = envelope()
            .with(metadataOf(commandId, "command.name"))
            .withPayloadOf("Fred", "firstname")
            .withPayloadOf("Bloggs", "lastname")
            .build();

    assertThat(command, is(jsonEnvelope(
            metadata()
                    .withId(commandId)
                    .withName("command.name"),
            payLoad().isJson(allOf(
                    withJsonPath("$.firstname", equalTo("Fred")),
                    withJsonPath("$.lastname", equalTo("Bloggs"))
            ))
    )));
Clone this wiki locally