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 --- .../egiz/moazs/SameThreadDeliveryPipelineTest.java | 211 +++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 src/test/java/at/gv/egiz/moazs/SameThreadDeliveryPipelineTest.java (limited to 'src/test/java/at/gv') 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