Skip to content

Commit

Permalink
Fix DispatcherDelegateTest
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Jan 11, 2023
1 parent c1800c9 commit 5593eee
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,56 @@ 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 DispatcherConfiguration dispatcherConfiguration) {
final JsonEnvelopeRepacker jsonEnvelopeRepacker) {
this.dispatcher = dispatcher;
this.systemUserUtil = systemUserUtil;
this.requestResponseEnvelopeValidator = requestResponseEnvelopeValidator;
this.envelopePayloadTypeConverter = envelopePayloadTypeConverter;
this.jsonEnvelopeRepacker = jsonEnvelopeRepacker;
this.dispatcherConfiguration = dispatcherConfiguration;
}

@Override
public JsonEnvelope request(final Envelope<?> envelope) {
return dispatchAndValidateResponse(envelope);

final Envelope<JsonValue> convertedEnvelope = envelopePayloadTypeConverter.convert(
envelope,
JsonValue.class);
final JsonEnvelope repackedEnvelope = jsonEnvelopeRepacker.repack(convertedEnvelope);
final JsonEnvelope response = dispatcher.dispatch(repackedEnvelope);

requestResponseEnvelopeValidator.validateResponse(response);

return response;
}

@Override
public <T> Envelope<T> request(final Envelope<?> envelope, final Class<T> clazz) {
final JsonEnvelope response = dispatchAndValidateResponse(envelope);
public <T> Envelope<T> request(final Envelope<?> requestEnvelope, final Class<T> clazz) {

final Envelope<JsonValue> convertedEnvelope = envelopePayloadTypeConverter.convert(
requestEnvelope,
JsonValue.class);
final JsonEnvelope repackedEnvelope = jsonEnvelopeRepacker.repack(convertedEnvelope);
final JsonEnvelope responseEnvelope = dispatcher.dispatch(repackedEnvelope);

requestResponseEnvelopeValidator.validateResponse(responseEnvelope);

return envelopePayloadTypeConverter.convert(response, clazz);
return envelopePayloadTypeConverter.convert(responseEnvelope, clazz);
}

@Override
public JsonEnvelope requestAsAdmin(final JsonEnvelope envelope) {

final JsonEnvelope adminEnvelope = systemUserUtil.asEnvelopeWithSystemUserId(envelope);
final JsonEnvelope response = dispatcher.dispatch(adminEnvelope);
final JsonEnvelope responseEnvelope = dispatcher.dispatch(adminEnvelope);

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

return response;
return responseEnvelope;
}

@Override
Expand All @@ -67,9 +78,7 @@ public <T> Envelope<T> requestAsAdmin(final Envelope<?> envelope, final Class<T>
final JsonEnvelope adminEnvelope = systemUserUtil.asEnvelopeWithSystemUserId(repackedEnvelope);
final JsonEnvelope responseEnvelope = dispatcher.dispatch(adminEnvelope);

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

return envelopePayloadTypeConverter.convert(responseEnvelope, clazz);
}
Expand Down Expand Up @@ -109,20 +118,4 @@ public void sendAsAdmin(final Envelope<?> envelope) {
dispatcher.dispatch(adminEnvelope);
}

private JsonEnvelope dispatchAndValidateResponse(final Envelope<?> envelope) {

final Envelope<JsonValue> convertedEnvelope = envelopePayloadTypeConverter.convert(
envelope,
JsonValue.class);

final JsonEnvelope repackedEnvelope = jsonEnvelopeRepacker.repack(convertedEnvelope);

final JsonEnvelope response = dispatcher.dispatch(repackedEnvelope);

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

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Optional.of;

import uk.gov.justice.services.core.dispatcher.DispatcherConfiguration;
import uk.gov.justice.services.core.mapping.MediaType;
import uk.gov.justice.services.core.mapping.NameToMediaTypeConverter;
import uk.gov.justice.services.messaging.JsonEnvelope;
Expand All @@ -14,16 +15,19 @@ public class RequestResponseEnvelopeValidator {
private final NameToMediaTypeConverter nameToMediaTypeConverter;
private final MediaTypeProvider mediaTypeProvider;
private final EnvelopeInspector envelopeInspector;
private final DispatcherConfiguration dispatcherConfiguration;

public RequestResponseEnvelopeValidator(
final EnvelopeValidator envelopeValidator,
final NameToMediaTypeConverter nameToMediaTypeConverter,
final MediaTypeProvider mediaTypeProvider,
final EnvelopeInspector envelopeInspector) {
final EnvelopeInspector envelopeInspector,
final DispatcherConfiguration dispatcherConfiguration) {
this.envelopeValidator = envelopeValidator;
this.nameToMediaTypeConverter = nameToMediaTypeConverter;
this.mediaTypeProvider = mediaTypeProvider;
this.envelopeInspector = envelopeInspector;
this.dispatcherConfiguration = dispatcherConfiguration;
}

public void validateRequest(final JsonEnvelope jsonEnvelope) {
Expand All @@ -36,9 +40,11 @@ public void validateRequest(final JsonEnvelope jsonEnvelope) {

public void validateResponse(final JsonEnvelope jsonEnvelope) {

final String actionName = envelopeInspector.getActionNameFor(jsonEnvelope);
final Optional<MediaType> mediaType = mediaTypeProvider.getResponseMediaType(actionName);
if(dispatcherConfiguration.shouldValidateRestResponseJson()) {
final String actionName = envelopeInspector.getActionNameFor(jsonEnvelope);
final Optional<MediaType> mediaType = mediaTypeProvider.getResponseMediaType(actionName);

envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.justice.services.core.producers;

import uk.gov.justice.services.core.dispatcher.DispatcherConfiguration;
import uk.gov.justice.services.core.envelope.EnvelopeInspector;
import uk.gov.justice.services.core.envelope.EnvelopeValidator;
import uk.gov.justice.services.core.envelope.MediaTypeProvider;
Expand All @@ -22,6 +23,9 @@ public class RequestResponseEnvelopeValidatorFactory {
@Inject
private EnvelopeValidatorFactory envelopeValidatorFactory;

@Inject
private DispatcherConfiguration dispatcherConfiguration;

public RequestResponseEnvelopeValidator createNew() {

final EnvelopeValidator envelopeValidator = envelopeValidatorFactory.createNew();
Expand All @@ -30,6 +34,7 @@ public RequestResponseEnvelopeValidator createNew() {
envelopeValidator,
nameToMediaTypeConverter,
mediaTypeProvider,
envelopeInspector);
envelopeInspector,
dispatcherConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class RequesterProducer {
@Inject
private JsonEnvelopeRepacker jsonEnvelopeRepacker;

@Inject
private DispatcherConfiguration dispatcherConfiguration;

@Inject
private RequestResponseEnvelopeValidatorFactory requestResponseEnvelopeValidatorFactory;

Expand All @@ -57,7 +54,6 @@ public Requester produceRequester(final InjectionPoint injectionPoint) {
systemUserUtil,
requestResponseEnvelopeValidator,
envelopePayloadTypeConverter,
jsonEnvelopeRepacker,
dispatcherConfiguration);
jsonEnvelopeRepacker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public Sender produceSender(final InjectionPoint injectionPoint) {
systemUserUtil,
requestResponseEnvelopeValidator,
envelopePayloadTypeConverter,
jsonEnvelopeRepacker,
dispatcherConfiguration);
jsonEnvelopeRepacker);
}
}
Loading

0 comments on commit 5593eee

Please sign in to comment.