Skip to content

Commit

Permalink
Add new JNDI property to enable/disable rest json response validation…
Browse files Browse the repository at this point in the history
… in the query pillar.
  • Loading branch information
amckenzie committed Jul 24, 2020
1 parent 0e0e169 commit 4172ddd
Show file tree
Hide file tree
Showing 23 changed files with 268 additions and 66 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

## [7.0.8] - 2020-07-24
### Added
- New JNDI property `rest.dispatcher.response.json.validation.enabled` to
enable/disable rest json response validation in the query pillar. Disabled
by default

## [7.0.7] - 2020-07-07
### Changed
- Update to framework-libraries 7.0.9
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.gov.justice.services.core.dispatcher;

import static java.lang.Boolean.parseBoolean;

import uk.gov.justice.services.common.configuration.GlobalValue;

import javax.inject.Inject;

public class DispatcherConfiguration {

@Inject
@GlobalValue(key = "rest.dispatcher.response.json.validation.enabled", defaultValue = "false")
private String validateRestResponseJson;

public boolean shouldValidateRestResponseJson() {
return parseBoolean(validateRestResponseJson);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ public class DispatcherDelegate implements Requester, Sender {
private final RequestResponseEnvelopeValidator requestResponseEnvelopeValidator;
private final EnvelopePayloadTypeConverter envelopePayloadTypeConverter;
private final JsonEnvelopeRepacker jsonEnvelopeRepacker;
private final DispatcherConfiguration dispatcherConfiguration;

public DispatcherDelegate(final Dispatcher dispatcher,
final SystemUserUtil systemUserUtil,
final RequestResponseEnvelopeValidator requestResponseEnvelopeValidator,
final EnvelopePayloadTypeConverter envelopePayloadTypeConverter,
final JsonEnvelopeRepacker jsonEnvelopeRepacker) {
final JsonEnvelopeRepacker jsonEnvelopeRepacker,
final DispatcherConfiguration dispatcherConfiguration) {
this.dispatcher = dispatcher;
this.systemUserUtil = systemUserUtil;
this.requestResponseEnvelopeValidator = requestResponseEnvelopeValidator;
this.envelopePayloadTypeConverter = envelopePayloadTypeConverter;
this.jsonEnvelopeRepacker = jsonEnvelopeRepacker;
this.dispatcherConfiguration = dispatcherConfiguration;
}

@Override
Expand All @@ -46,15 +49,21 @@ public <T> Envelope<T> request(final Envelope<?> envelope, final Class<T> clazz)
@Override
public JsonEnvelope requestAsAdmin(final JsonEnvelope envelope) {
final JsonEnvelope response = dispatchAsAdmin().apply(envelope);
requestResponseEnvelopeValidator.validateResponse(response);

if (dispatcherConfiguration.shouldValidateRestResponseJson()) {
requestResponseEnvelopeValidator.validateResponse(response);
}

return response;
}

@Override
public <T> Envelope<T> requestAsAdmin(final Envelope<?> envelope, final Class<T> clazz) {
final JsonEnvelope response = dispatchAsAdmin().compose(convertAndRepackEnvelope()).apply(envelope);

requestResponseEnvelopeValidator.validateResponse(response);
if (dispatcherConfiguration.shouldValidateRestResponseJson()) {
requestResponseEnvelopeValidator.validateResponse(response);
}

return envelopePayloadTypeConverter.convert(response, clazz);
}
Expand All @@ -64,7 +73,6 @@ public void send(final Envelope<?> envelope) {
final JsonEnvelope jsonEnvelope = convertAndRepackEnvelope().apply(envelope);

requestResponseEnvelopeValidator.validateRequest(jsonEnvelope);

dispatch().apply(jsonEnvelope);
}

Expand All @@ -86,7 +94,10 @@ public void sendAsAdmin(final Envelope<?> envelope) {
private JsonEnvelope dispatchAndValidateResponse(final Envelope<?> envelope) {
final JsonEnvelope response = dispatch().compose(convertAndRepackEnvelope()).apply(envelope);

requestResponseEnvelopeValidator.validateResponse(response);
if (dispatcherConfiguration.shouldValidateRestResponseJson()) {
requestResponseEnvelopeValidator.validateResponse(response);
}

return response;
}

Expand All @@ -99,7 +110,7 @@ private Function<JsonEnvelope, JsonEnvelope> dispatch() {
return (jsonEnvelope) -> dispatcher.dispatch(jsonEnvelope);
}

private Function<JsonEnvelope, JsonEnvelope> dispatchAsAdmin(){
private Function<JsonEnvelope, JsonEnvelope> dispatchAsAdmin() {
return (jsonEnvelope) -> dispatcher.dispatch(systemUserUtil.asEnvelopeWithSystemUserId(jsonEnvelope));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import uk.gov.justice.services.core.annotation.ServiceComponent;
import uk.gov.justice.services.core.dispatcher.DispatcherCache;
import uk.gov.justice.services.core.dispatcher.DispatcherConfiguration;
import uk.gov.justice.services.core.dispatcher.DispatcherDelegate;
import uk.gov.justice.services.core.dispatcher.EnvelopePayloadTypeConverter;
import uk.gov.justice.services.core.dispatcher.JsonEnvelopeRepacker;
Expand All @@ -26,34 +27,37 @@
public class RequesterProducer {

@Inject
JsonSchemaValidator jsonSchemaValidator;
private JsonSchemaValidator jsonSchemaValidator;

@Inject
ObjectMapper objectMapper;
private ObjectMapper objectMapper;

@Inject
EnvelopeValidationExceptionHandler envelopeValidationExceptionHandler;
private EnvelopeValidationExceptionHandler envelopeValidationExceptionHandler;

@Inject
NameToMediaTypeConverter nameToMediaTypeConverter;
private NameToMediaTypeConverter nameToMediaTypeConverter;

@Inject
MediaTypeProvider mediaTypeProvider;
private MediaTypeProvider mediaTypeProvider;

@Inject
EnvelopeInspector envelopeInspector;
private EnvelopeInspector envelopeInspector;

@Inject
DispatcherCache dispatcherCache;
private DispatcherCache dispatcherCache;

@Inject
SystemUserUtil systemUserUtil;
private SystemUserUtil systemUserUtil;

@Inject
EnvelopePayloadTypeConverter envelopePayloadTypeConverter;
private EnvelopePayloadTypeConverter envelopePayloadTypeConverter;

@Inject
JsonEnvelopeRepacker jsonEnvelopeRepacker;
private JsonEnvelopeRepacker jsonEnvelopeRepacker;

@Inject
private DispatcherConfiguration dispatcherConfiguration;

/**
* Produces the correct implementation of a requester depending on the {@link ServiceComponent}
Expand Down Expand Up @@ -84,6 +88,7 @@ public Requester produceRequester(final InjectionPoint injectionPoint) {
systemUserUtil,
requestResponseEnvelopeValidator,
envelopePayloadTypeConverter,
jsonEnvelopeRepacker);
jsonEnvelopeRepacker,
dispatcherConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import uk.gov.justice.services.core.annotation.ServiceComponent;
import uk.gov.justice.services.core.dispatcher.DispatcherCache;
import uk.gov.justice.services.core.dispatcher.DispatcherConfiguration;
import uk.gov.justice.services.core.dispatcher.DispatcherDelegate;
import uk.gov.justice.services.core.dispatcher.EnvelopePayloadTypeConverter;
import uk.gov.justice.services.core.dispatcher.JsonEnvelopeRepacker;
Expand All @@ -28,34 +29,37 @@
public class SenderProducer {

@Inject
JsonSchemaValidator jsonSchemaValidator;
private JsonSchemaValidator jsonSchemaValidator;

@Inject
ObjectMapper objectMapper;
private ObjectMapper objectMapper;

@Inject
EnvelopeValidationExceptionHandler envelopeValidationExceptionHandler;
private EnvelopeValidationExceptionHandler envelopeValidationExceptionHandler;

@Inject
NameToMediaTypeConverter nameToMediaTypeConverter;
private NameToMediaTypeConverter nameToMediaTypeConverter;

@Inject
MediaTypeProvider mediaTypeProvider;
private MediaTypeProvider mediaTypeProvider;

@Inject
EnvelopeInspector envelopeInspector;
private EnvelopeInspector envelopeInspector;

@Inject
DispatcherCache dispatcherCache;
private DispatcherCache dispatcherCache;

@Inject
SystemUserUtil systemUserUtil;
private SystemUserUtil systemUserUtil;

@Inject
EnvelopePayloadTypeConverter envelopePayloadTypeConverter;
private EnvelopePayloadTypeConverter envelopePayloadTypeConverter;

@Inject
JsonEnvelopeRepacker jsonEnvelopeRepacker;
private JsonEnvelopeRepacker jsonEnvelopeRepacker;

@Inject
private DispatcherConfiguration dispatcherConfiguration;

/**
* Produces the correct implementation of a requester depending on the {@link ServiceComponent}
Expand Down Expand Up @@ -85,6 +89,7 @@ public Sender produceSender(final InjectionPoint injectionPoint) {
systemUserUtil,
requestResponseEnvelopeValidator,
envelopePayloadTypeConverter,
jsonEnvelopeRepacker);
jsonEnvelopeRepacker,
dispatcherConfiguration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package uk.gov.justice.services.core.dispatcher;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil.setField;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class DispatcherConfigurationTest {

@InjectMocks
private DispatcherConfiguration dispatcherConfiguration;

@Test
public void shouldGetTheStartWaitTime() throws Exception {

setField(dispatcherConfiguration, "validateRestResponseJson", "true");
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(true));

setField(dispatcherConfiguration, "validateRestResponseJson", "TRUE");
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(true));

setField(dispatcherConfiguration, "validateRestResponseJson", "True");
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(true));

setField(dispatcherConfiguration, "validateRestResponseJson", "false");
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(false));

setField(dispatcherConfiguration, "validateRestResponseJson", "FALSE");
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(false));

setField(dispatcherConfiguration, "validateRestResponseJson", "False");
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(false));

setField(dispatcherConfiguration, "validateRestResponseJson", null);
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(false));

setField(dispatcherConfiguration, "validateRestResponseJson", "");
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(false));

setField(dispatcherConfiguration, "validateRestResponseJson", "something silly");
assertThat(dispatcherConfiguration.shouldValidateRestResponseJson(), is(false));
}
}
Loading

0 comments on commit 4172ddd

Please sign in to comment.