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

JsonEnvelope Cookbook

Allan Mckenzie edited this page Jul 9, 2019 · 11 revisions

To create a full JsonEnvelope

    import static java.util.UUID.randomUUID;
    import static uk.gov.justice.services.test.utils.core.messaging.JsonEnvelopeBuilder.envelope;
    import static uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID;

    import uk.gov.justice.services.messaging.JsonEnvelope;


    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

import static uk.gov.justice.services.messaging.JsonEnvelope.envelopeFrom;
import static uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID;

import uk.gov.justice.services.messaging.JsonEnvelope;
import javax.json.Json;

    final JsonEnvelope envelope = envelopeFrom(
            metadataWithRandomUUID("some-command-name")
                    .withUserId(USER_ID.toString()),
            Json.createObjectBuilder()
                    .add("myArray", Json.createArrayBuilder()
                            .add(Json.createObjectBuilder()
                                    .add("firstName", "Fred")
                                    .add("lastName", "Bloggs")
                                    .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.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;
  import static uk.gov.justice.services.test.utils.core.messaging.JsonEnvelopeBuilder.envelope;
  import static uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataOf;

  import uk.gov.justice.services.messaging.JsonEnvelope;
  import java.util.UUID;

    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"))
            ))
    )));