From f4af4f437ff79cb6e2a35fdfe87e8d952a70b685 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Mon, 13 May 2019 14:02:42 +0200 Subject: Add Unit Tests for DeliveryPipeline - Refactor: Move TnvzClient in sub package - Autowire all parameters instead of each parameter individually --- src/main/java/at/gv/egiz/moazs/TnvzClient.java | 2 +- .../moazs/pipeline/SameThreadDeliveryPipeline.java | 13 +- .../egiz/moazs/SameThreadDeliveryPipelineTest.java | 211 +++++++++++++++++++++ 3 files changed, 219 insertions(+), 7 deletions(-) create mode 100644 src/test/java/at/gv/egiz/moazs/SameThreadDeliveryPipelineTest.java diff --git a/src/main/java/at/gv/egiz/moazs/TnvzClient.java b/src/main/java/at/gv/egiz/moazs/TnvzClient.java index 7aa6344..4a69aea 100644 --- a/src/main/java/at/gv/egiz/moazs/TnvzClient.java +++ b/src/main/java/at/gv/egiz/moazs/TnvzClient.java @@ -1,4 +1,4 @@ -package at.gv.egiz.moazs; +package at.gv.egiz.moazs.tnvz; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Sender; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Receiver; diff --git a/src/main/java/at/gv/egiz/moazs/pipeline/SameThreadDeliveryPipeline.java b/src/main/java/at/gv/egiz/moazs/pipeline/SameThreadDeliveryPipeline.java index 7943754..bc80188 100644 --- a/src/main/java/at/gv/egiz/moazs/pipeline/SameThreadDeliveryPipeline.java +++ b/src/main/java/at/gv/egiz/moazs/pipeline/SameThreadDeliveryPipeline.java @@ -2,7 +2,7 @@ package at.gv.egiz.moazs.pipeline; import at.gv.egiz.moazs.MsgClient; -import at.gv.egiz.moazs.TnvzClient; +import at.gv.egiz.moazs.tnvz.TnvzClient; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; @@ -29,10 +29,11 @@ public class SameThreadDeliveryPipeline implements DeliveryPipeline { private final Mzs2MsgConverter converter; private final MsgClient msgClient; - public SameThreadDeliveryPipeline(@Autowired DeliveryRepository repository, - @Autowired TnvzClient tnvzClient, - @Autowired Mzs2MsgConverter converter, - @Autowired MsgClient msgClient) { + @Autowired + public SameThreadDeliveryPipeline(DeliveryRepository repository, + TnvzClient tnvzClient, + Mzs2MsgConverter converter, + MsgClient msgClient) { this.repository = repository; this.tnvzClient = tnvzClient; this.converter = converter; @@ -65,7 +66,7 @@ public class SameThreadDeliveryPipeline implements DeliveryPipeline { var mismatchedTypes = findMimeTypeMismatches(result, request); if (!mismatchedTypes.isEmpty()) { - var template = "Request contains attachment of type(s) %s, but receiver only accepts attachments of type(s) %s"; + var template = "Request contains attachment of type(s) %s, but receiver only accepts attachments of type(s) %s."; var acceptedTypesString = join(",", getAcceptedTypes(result)); var mismatchedTypesString = join(",", mismatchedTypes); throw moaZSException(template, mismatchedTypesString, acceptedTypesString); diff --git a/src/test/java/at/gv/egiz/moazs/SameThreadDeliveryPipelineTest.java b/src/test/java/at/gv/egiz/moazs/SameThreadDeliveryPipelineTest.java new file mode 100644 index 0000000..f394a63 --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/SameThreadDeliveryPipelineTest.java @@ -0,0 +1,211 @@ +package at.gv.egiz.moazs; + +import at.gv.egiz.moazs.pipeline.DeliveryPipeline; +import at.gv.egiz.moazs.pipeline.SameThreadDeliveryPipeline; +import at.gv.egiz.moazs.repository.DeliveryRepository; +import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; +import at.gv.egiz.moazs.tnvz.TnvzClient; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; +import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; +import at.gv.zustellung.msg.xsd.MetaData; +import at.gv.zustellung.tnvz.xsd.MimeTypeList; +import at.gv.zustellung.tnvz.xsd.PersonResultType; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.List; + +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Payload; +import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.Payload.payloadBuilder; +import static at.gv.zustellung.app2mzs.xsd.DeliveryRequestType.deliveryRequestTypeBuilder; +import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Success.successBuilder; +import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; +import static at.gv.zustellung.msg.xsd.ErrorInfoType.errorInfoTypeBuilder; +import static at.gv.zustellung.msg.xsd.MetaData.metaDataBuilder; +import static at.gv.zustellung.tnvz.xsd.MimeTypeList.mimeTypeListBuilder; +import static at.gv.zustellung.tnvz.xsd.PersonResultSuccessType.personResultSuccessTypeBuilder; +import static at.gv.zustellung.tnvz.xsd.PersonResultType.Error.errorBuilder; +import static at.gv.zustellung.tnvz.xsd.PersonResultType.personResultTypeBuilder; +import static java.util.Optional.of; +import static java.util.stream.Collectors.toList; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class SameThreadDeliveryPipelineTest { + + @Mock + private DeliveryRepository repository; + + @Mock + private TnvzClient tnvzClient; + + @Mock + private MsgClient msgClient; + + @Mock + private Mzs2MsgConverter converter; + + + private DeliveryPipeline pipeline; + + @Before + public void setup() { + pipeline = new SameThreadDeliveryPipeline(repository, tnvzClient, converter, msgClient); + } + + @Test + public void executePipelineWithoutTnvzRequest() { + var appDeliveryId = "no-tnvz-request"; + var status = setupMocks(appDeliveryId, false, List.of(), List.of("*/*")); + + pipeline.processRequest(appDeliveryId); + + verifyZeroInteractions(tnvzClient); + verify(repository).add(status); + } + + @Test(expected = MoaZSException.class) + public void rejectDeliveryWhenReceiverIsNotAddressable() { + var appDeliveryId = "not-addressable"; + setupMocks(appDeliveryId, true, List.of(), List.of("*/*")); + when(tnvzClient.queryPerson(any(), any())).thenReturn(setupTnvzError()); + + pipeline.processRequest(appDeliveryId); + + verify(tnvzClient).queryPerson(any(), any()); + } + + @Test + public void acceptWildcardMimeType() { + var appDeliveryId = "wild-card-mimetype"; + List acceptedTypes = List.of("*/*"); + List attachedTypes = List.of("pdf", "xml", "html", "random/attachedtype"); + var status = setupMocks(appDeliveryId, true, attachedTypes, acceptedTypes); + + pipeline.processRequest(appDeliveryId); + + verify(tnvzClient).queryPerson(any(), any()); + verify(repository).add(status); + } + + @Test(expected = MoaZSException.class) + public void rejectMismatchedMimeTypes() { + var appDeliveryId = "mismatched-mimetype"; + List acceptedTypes = List.of("xml"); + List attachedTypes = List.of("pdf", "xml", "html", "random/attachedtype"); + setupMocks(appDeliveryId, true, attachedTypes, acceptedTypes); + + pipeline.processRequest(appDeliveryId); + + verify(tnvzClient).queryPerson(any(), any()); + } + + @Test + public void acceptSpecificMimeType() { + var appDeliveryId = "specific-mimetype"; + List acceptedTypes = List.of("pdf", "xml", "html"); + List attachedTypes = List.of("pdf", "xml"); + var status = setupMocks(appDeliveryId, true, attachedTypes, acceptedTypes); + + pipeline.processRequest(appDeliveryId); + + verify(tnvzClient).queryPerson(any(), any()); + verify(repository).add(status); + } + + private DeliveryRequestStatusType setupMocks(String appDeliveryId, boolean tnvzRequest, + List attachedTypes, List acceptedTypes) { + + var mzsRequest = setupMzsRequest(appDeliveryId, tnvzRequest, attachedTypes); + var msgRequest = setupMsgRequest(appDeliveryId); + var status = setupStatus(appDeliveryId); + when(repository.getDeliveryRequest(appDeliveryId)).thenReturn(of(mzsRequest)); + when(tnvzClient.queryPerson(any(), any())).thenReturn(setupTnvzSuccess(acceptedTypes)); + when(converter.convert(eq(mzsRequest) )).thenReturn(msgRequest); + when(converter.convert(eq(mzsRequest), any())).thenReturn(msgRequest); + when(msgClient.send(msgRequest)).thenReturn(status); + + return status; + + } + + private PersonResultType setupTnvzError() { + + var info = errorInfoTypeBuilder() + .withCode("400") + .withText("Person Not Found") + .build(); + + var error = errorBuilder() + .withErrorInfo(info) + .build(); + + return personResultTypeBuilder() + .withError(error) + .build(); + } + + private PersonResultType setupTnvzSuccess(List mimeTypes) { + + var success = personResultSuccessTypeBuilder() + .withMimeTypeList(setupMimeTypeList(mimeTypes)) + .build(); + + return personResultTypeBuilder() + .withSuccess(success) + .build(); + } + + private DeliveryRequestStatusType setupStatus(String appDeliveryId) { + + var success = successBuilder() + .withAppDeliveryID(appDeliveryId) + .build(); + + return deliveryRequestStatusTypeBuilder() + .withSuccess(success) + .build(); + + } + + private DeliveryRequestType setupMzsRequest(String appDeliveryId, boolean tnvzRequest, List mimeTypes) { + + var config = configTypeBuilder() + .withPerformQueryPersonRequest(tnvzRequest) + .build(); + + return deliveryRequestTypeBuilder() + .withMetaData(setupMetaData(appDeliveryId)) + .withConfig(config) + .withPayload(setupPayload(mimeTypes)) + .build(); + } + + private List setupPayload(List mimeTypes) { + return mimeTypes.stream() + .map((mtype) -> payloadBuilder().withMIMEType(mtype).build()) + .collect(toList()); + } + + private MimeTypeList setupMimeTypeList(List mimeTypes) { + return mimeTypeListBuilder().withMimeType(mimeTypes).build(); + } + + private at.gv.zustellung.msg.xsd.DeliveryRequestType setupMsgRequest(String appDeliveryId) { + return at.gv.zustellung.msg.xsd.DeliveryRequestType.deliveryRequestTypeBuilder() + .withMetaData(setupMetaData(appDeliveryId)) + .build(); + } + + private MetaData setupMetaData(String appDeliveryId) { + return metaDataBuilder() + .withAppDeliveryID(appDeliveryId) + .build(); + } + +} -- cgit v1.2.3