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

Commit

Permalink
Refactor RestProcessorProducer
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeshanGhalib committed Apr 22, 2016
1 parent 105bc69 commit 3e8f36a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
5 changes: 0 additions & 5 deletions adapters/rest-adapter-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,5 @@
<artifactId>json-path-assert</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import uk.gov.justice.services.adapter.rest.envelope.RestEnvelopeBuilderFactory;
import uk.gov.justice.services.common.converter.JsonObjectToStringConverter;
import uk.gov.justice.services.messaging.JsonEnvelope;
import uk.gov.justice.services.messaging.JsonObjectEnvelopeConverter;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
import javax.json.JsonObject;

import static javax.json.JsonValue.ValueType.ARRAY;
import static uk.gov.justice.services.core.annotation.Component.QUERY_API;
import static uk.gov.justice.services.core.annotation.Component.componentFrom;
import static uk.gov.justice.services.messaging.JsonObjectEnvelopeConverter.RESULTS;

/**
* Produces the correct implementation of RestProcessor depending on the Adapter's Component type.
Expand Down Expand Up @@ -50,30 +46,16 @@ public RestProcessor produceRestProcessor(final InjectionPoint injectionPoint) {

private RestProcessor defaultRestProcessor() {
if (defaultRestProcessor == null) {
defaultRestProcessor = new RestProcessor(envelopeBuilderFactory,
envelope -> jsonObjectToStringConverter.convert(jsonObjectEnvelopeConverter.fromEnvelope(envelope)));
defaultRestProcessor = new RestProcessor(envelopeBuilderFactory, envelope -> jsonObjectToStringConverter.convert(jsonObjectEnvelopeConverter.fromEnvelope(envelope)));
}
return defaultRestProcessor;
}

private RestProcessor payloadOnlyRestProcessor() {
if (payloadOnlyRestProcessor == null) {
payloadOnlyRestProcessor = new RestProcessor(envelopeBuilderFactory, this::payloadOnlyEnvelopeAsString);
payloadOnlyRestProcessor = new RestProcessor(envelopeBuilderFactory, envelope -> envelope.payloadAsJsonObject().toString());
}
return payloadOnlyRestProcessor;
}

private String payloadOnlyEnvelopeAsString(final JsonEnvelope envelope) {
final JsonObject payload = envelope.payloadAsJsonObject();

final boolean isArrayOnlyPayload = payload.size() == 1 && payload.containsKey(RESULTS) && payload.get(RESULTS).getValueType() == ARRAY;

if (isArrayOnlyPayload) {
return payload.getJsonArray(RESULTS).toString();
} else {
return payload.toString();
}

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package uk.gov.justice.services.adapter.rest;

import com.jayway.jsonassert.JsonAssert;
import org.hamcrest.collection.IsCollectionWithSize;
import org.hamcrest.core.IsCollectionContaining;
import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
import org.jboss.resteasy.specimpl.ResteasyHttpHeaders;
import org.junit.Before;
Expand Down Expand Up @@ -34,7 +36,6 @@
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static uk.gov.justice.services.core.annotation.Component.QUERY_API;
import static uk.gov.justice.services.core.annotation.Component.QUERY_CONTROLLER;
import static uk.gov.justice.services.messaging.JsonObjectEnvelopeConverter.METADATA;
Expand Down Expand Up @@ -97,7 +98,9 @@ public void shouldReturnPayloadOnlyRestProcessorForJsonArray() {
.processSynchronously(function, headersWith("Accept", "application/vnd.somecontext.query.somequery+json"), NOT_USED_PATH_PARAMS);

assertThat(response, notNullValue());
assertEquals("[" + ARRAY_ITEM_1 + "," + ARRAY_ITEM_2 + "]", response.getEntity().toString(), false);
JsonAssert.with(response.getEntity().toString())
.assertThat("$." + RESULTS, IsCollectionWithSize.hasSize(2))
.assertThat("$." + RESULTS, IsCollectionContaining.hasItems(ARRAY_ITEM_1, ARRAY_ITEM_2));
}

@Test
Expand All @@ -108,7 +111,8 @@ public void shouldReturnPayloadOnlyRestProcessorForJsonObject() {
.processSynchronously(function, headersWith("Accept", "application/vnd.somecontext.query.somequery+json"), NOT_USED_PATH_PARAMS);

assertThat(response, notNullValue());
assertEquals("{" + FIELD_NAME + ":" + FIELD_VALUE + "}", response.getEntity().toString(), false);
JsonAssert.with(response.getEntity().toString())
.assertThat("$." + FIELD_NAME, equalTo(FIELD_VALUE));
}

@Test
Expand Down

0 comments on commit 3e8f36a

Please sign in to comment.