Search in sources :

Example 26 with Metadata

use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.

the class RestEnvelopeBuilder method payload.

private JsonObject payload() {
    final JsonObjectBuilder payloadBuilder = initialPayload.map(jsonObject -> createObjectBuilderWithFilter(jsonObject, key -> !key.equals(METADATA))).orElse(Json.createObjectBuilder());
    params.ifPresent(parameters -> parameters.forEach(param -> {
        switch(param.getType()) {
            case NUMERIC:
                payloadBuilder.add(param.getName(), param.getNumericValue());
                break;
            case BOOLEAN:
                payloadBuilder.add(param.getName(), param.getBooleanValue());
                break;
            default:
                payloadBuilder.add(param.getName(), param.getStringValue());
        }
    }));
    return payloadBuilder.build();
}
Also used : CLIENT_CORRELATION_ID(uk.gov.justice.services.common.http.HeaderConstants.CLIENT_CORRELATION_ID) MetadataBuilder(uk.gov.justice.services.messaging.MetadataBuilder) JsonEnvelope.envelopeFrom(uk.gov.justice.services.messaging.JsonEnvelope.envelopeFrom) SESSION_ID(uk.gov.justice.services.common.http.HeaderConstants.SESSION_ID) METADATA(uk.gov.justice.services.messaging.JsonEnvelope.METADATA) Json(javax.json.Json) JsonEnvelope.metadataFrom(uk.gov.justice.services.messaging.JsonEnvelope.metadataFrom) JsonObject(javax.json.JsonObject) Parameter(uk.gov.justice.services.adapter.rest.parameter.Parameter) Collections.emptyList(java.util.Collections.emptyList) CAUSATION(uk.gov.justice.services.common.http.HeaderConstants.CAUSATION) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) USER_ID(uk.gov.justice.services.common.http.HeaderConstants.USER_ID) Consumer(java.util.function.Consumer) List(java.util.List) JsonObjects.getJsonObject(uk.gov.justice.services.messaging.JsonObjects.getJsonObject) HttpHeaders(javax.ws.rs.core.HttpHeaders) Optional(java.util.Optional) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Metadata(uk.gov.justice.services.messaging.Metadata) Arrays.stream(java.util.Arrays.stream) JsonObjects.createObjectBuilderWithFilter(uk.gov.justice.services.messaging.JsonObjects.createObjectBuilderWithFilter) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 27 with Metadata

use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.

the class RestEnvelopeBuilderTest method shouldNotFailIfSessionIdIsSetInBothHeaderAndPayloadAndAreEqual.

@Test
public void shouldNotFailIfSessionIdIsSetInBothHeaderAndPayloadAndAreEqual() throws Exception {
    final Optional<JsonObject> initialPayload = Optional.of(createObjectBuilder().add(METADATA, createObjectBuilder().add(ID, randomUUID().toString()).add(NAME, PAYLOAD_NAME).add(CONTEXT, createObjectBuilder().add(SESSION_ID, UUID_SESSION_ID.toString()))).build());
    final JsonEnvelope envelope = builderWithDefaultAction().withInitialPayload(initialPayload).withHeaders(httpHeadersOf(ImmutableMap.of(HeaderConstants.SESSION_ID, UUID_SESSION_ID.toString()))).build();
    final Metadata metadata = envelope.metadata();
    assertThat(metadata.sessionId(), is(Optional.of(UUID_SESSION_ID.toString())));
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonObject(javax.json.JsonObject) JsonObjects.getJsonObject(uk.gov.justice.services.messaging.JsonObjects.getJsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 28 with Metadata

use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.

the class RestEnvelopeBuilderTest method shouldNotFailIfUserIdIsSetInBothHeaderAndPayloadAndAreEqual.

@Test
public void shouldNotFailIfUserIdIsSetInBothHeaderAndPayloadAndAreEqual() throws Exception {
    final Optional<JsonObject> initialPayload = Optional.of(createObjectBuilder().add(METADATA, createObjectBuilder().add(ID, randomUUID().toString()).add(NAME, PAYLOAD_NAME).add(CONTEXT, createObjectBuilder().add(USER_ID, UUID_USER_ID.toString()))).build());
    final JsonEnvelope envelope = builderWithDefaultAction().withInitialPayload(initialPayload).withHeaders(httpHeadersOf(ImmutableMap.of(HeaderConstants.USER_ID, UUID_USER_ID.toString()))).build();
    final Metadata metadata = envelope.metadata();
    assertThat(metadata.userId(), is(Optional.of(UUID_USER_ID.toString())));
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonObject(javax.json.JsonObject) JsonObjects.getJsonObject(uk.gov.justice.services.messaging.JsonObjects.getJsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 29 with Metadata

use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.

the class RestEnvelopeBuilderTest method shouldRemoveMetadataFromPayloadAndAddToMetadataOfEnvelope.

@Test
public void shouldRemoveMetadataFromPayloadAndAddToMetadataOfEnvelope() throws Exception {
    final String payloadCausationId = randomUUID().toString();
    final Optional<JsonObject> initialPayload = Optional.of(createObjectBuilder().add(METADATA, createObjectBuilder().add(ID, PAYLOAD_METADATA_ID.toString()).add(NAME, PAYLOAD_NAME).add(CONTEXT, createObjectBuilder().add(SESSION_ID, UUID_SESSION_ID.toString()).add(USER_ID, UUID_USER_ID.toString())).add(CAUSATION, createArrayBuilder().add(payloadCausationId)).add(CORRELATION, createObjectBuilder().add(CLIENT_ID, UUID_CLIENT_CORRELATION_ID.toString())).add(STREAM, createObjectBuilder().add(STREAM_ID, UUID_STREAM_ID.toString()).add(VERSION, VERSION_VALUE))).add("test", "value").build());
    final JsonEnvelope envelope = builderWithDefaultAction().withInitialPayload(initialPayload).build();
    final JsonObject payload = envelope.payloadAsJsonObject();
    final Metadata metadata = envelope.metadata();
    assertThat(metadata.id(), is(PAYLOAD_METADATA_ID));
    assertThat(metadata.name(), is(PAYLOAD_NAME));
    assertThat(metadata.sessionId(), is(Optional.of(UUID_SESSION_ID.toString())));
    assertThat(metadata.userId(), is(Optional.of(UUID_USER_ID.toString())));
    assertThat(metadata.clientCorrelationId(), is(Optional.of(UUID_CLIENT_CORRELATION_ID.toString())));
    assertThat(metadata.streamId(), is(Optional.of(UUID_STREAM_ID)));
    assertThat(metadata.version(), is(Optional.of(VERSION_VALUE)));
    assertThat(metadata.causation().get(0).toString(), is(payloadCausationId));
    assertThat(getString(payload, METADATA, ID), is(Optional.empty()));
    assertThat(getString(payload, METADATA, NAME), is(Optional.empty()));
    assertThat(getJsonObject(payload, METADATA, CONTEXT), is(Optional.empty()));
    assertThat(getJsonObject(payload, METADATA, CORRELATION), is(Optional.empty()));
    assertThat(getJsonObject(payload, METADATA, STREAM), is(Optional.empty()));
    assertThat(getString(payload, "test"), is(Optional.of("value")));
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonObject(javax.json.JsonObject) JsonObjects.getJsonObject(uk.gov.justice.services.messaging.JsonObjects.getJsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) JsonObjects.getString(uk.gov.justice.services.messaging.JsonObjects.getString) Test(org.junit.Test)

Example 30 with Metadata

use of uk.gov.justice.services.messaging.Metadata in project microservice_framework by CJSCommonPlatform.

the class EnvelopeInspectorTest method shouldGetTheMetadataOfAnEnvelope.

@Test
public void shouldGetTheMetadataOfAnEnvelope() throws Exception {
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    final Metadata metadata = mock(Metadata.class);
    when(jsonEnvelope.metadata()).thenReturn(metadata);
    assertThat(envelopeInspector.getMetadataFor(jsonEnvelope), is(metadata));
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Aggregations

Metadata (uk.gov.justice.services.messaging.Metadata)67 Test (org.junit.Test)58 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)19 UUID (java.util.UUID)17 UUID.randomUUID (java.util.UUID.randomUUID)11 JsonObject (javax.json.JsonObject)10 JsonObjects.getJsonObject (uk.gov.justice.services.messaging.JsonObjects.getJsonObject)8 MetadataBuilder (uk.gov.justice.services.messaging.MetadataBuilder)6 JsonObjectBuilder (javax.json.JsonObjectBuilder)5 JsonValue (javax.json.JsonValue)3 JsonObjects.getString (uk.gov.justice.services.messaging.JsonObjects.getString)3 Json.createObjectBuilder (javax.json.Json.createObjectBuilder)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 UtcClock (uk.gov.justice.services.common.util.UtcClock)2 Interceptor (uk.gov.justice.services.core.interceptor.Interceptor)2 InterceptorChain (uk.gov.justice.services.core.interceptor.InterceptorChain)2 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)2 JsonEnvelope.metadataBuilder (uk.gov.justice.services.messaging.JsonEnvelope.metadataBuilder)2 EqualsTester (com.google.common.testing.EqualsTester)1 String.format (java.lang.String.format)1