From 8aba1b4f18f5fbfebdf239b4b4945b628e439905 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Tue, 9 Jul 2019 14:11:47 +0200 Subject: Refactor Needles Interfaces and Rename "process" to "backend" Reason: - Interfaces with a single method can be replaced with interfaces from java.util.function. - Less interfaces = less code = less maintenance! - Spring can inject beans by name so we name dependencies correctly to prevent ambiguity. Others: - Rename process to backend since backend gives a better description of its components. --- .../gv/egiz/moazs/DeliveryRequestBackendTest.java | 203 ++++++++++++++++++++ .../egiz/moazs/ITMoaSPSSSignatureVerifierTest.java | 9 +- .../java/at/gv/egiz/moazs/ITMzsServiceTest.java | 8 +- .../gv/egiz/moazs/SaveResponseToFileSinkTest.java | 4 +- .../moazs/SingleThreadedDeliveryPipelineTest.java | 204 --------------------- 5 files changed, 211 insertions(+), 217 deletions(-) create mode 100644 src/test/java/at/gv/egiz/moazs/DeliveryRequestBackendTest.java delete mode 100644 src/test/java/at/gv/egiz/moazs/SingleThreadedDeliveryPipelineTest.java (limited to 'src/test/java/at/gv/egiz/moazs') diff --git a/src/test/java/at/gv/egiz/moazs/DeliveryRequestBackendTest.java b/src/test/java/at/gv/egiz/moazs/DeliveryRequestBackendTest.java new file mode 100644 index 0000000..ed63489 --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/DeliveryRequestBackendTest.java @@ -0,0 +1,203 @@ +package at.gv.egiz.moazs; + +import at.gv.egiz.moazs.client.ClientFactory; +import at.gv.egiz.moazs.client.TnvzHelper; +import at.gv.egiz.moazs.backend.DeliveryRequestBackend; +import at.gv.egiz.moazs.backend.SignatureVerifier; +import at.gv.egiz.moazs.repository.DeliveryRepository; +import at.gv.egiz.moazs.repository.InMemoryDeliveryRepository; +import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; +import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; +import at.gv.zustellung.msg.xsd.App2ZusePort; +import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; +import at.gv.zustellung.msg.xsd.MetaData; +import at.gv.zustellung.msg.xsd.persondata.IdentificationType; +import at.gv.zustellung.tnvz.xsd.TNVZServicePort; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static at.gv.egiz.moazs.MoaZSException.moaZSException; +import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; +import static at.gv.egiz.moazs.scheme.RequestStatusResponse.getResponseID; +import static at.gv.zustellung.app2mzs.xsd.ClientType.clientTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; +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.MetaData.metaDataBuilder; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.same; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class DeliveryRequestBackendTest { + + private DeliveryRepository repository = new InMemoryDeliveryRepository(100); + + @Mock + private TnvzHelper tnvzHelper; + + @Mock + private ClientFactory clientFactory; + + @Mock + private App2ZusePort msgClient; + + @Mock + private TNVZServicePort tnvzClient; + + @Mock + private Mzs2MsgConverter converter; + + @Mock + private SignatureVerifier verifier; + + + private DeliveryRequestBackend pipeline; + + @Before + public void setup() { + pipeline = new DeliveryRequestBackend( + repository, + tnvzHelper, + converter, + clientFactory, + verifier); + } + + @Test + public void executePipelineWithoutTnvzRequest() { + var appDeliveryID = "no-tnvz-request"; + var expectedStatus = setupMocks(appDeliveryID, false); + + pipeline.accept(appDeliveryID); + + verifyZeroInteractions(tnvzHelper); + var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); + var actualStatus = (DeliveryRequestStatusType) response.getResponse(); + + assertThat(actualStatus).isEqualTo(expectedStatus); + } + + @Test + public void rejectDeliveryWhenReceiverIsNotAddressable() { + var appDeliveryID = "not-addressable"; + setupMocks(appDeliveryID, true); + when(tnvzHelper.performQueryPersonRequest(any(), any(), any())) + .thenThrow(buildMzsException("400", appDeliveryID)); + + pipeline.accept(appDeliveryID); + + var responseID = getResponseID(appDeliveryID); + var response = repository.retrieveResponse(responseID).get(); + var status = (DeliveryRequestStatusType) response.getResponse(); + var actualCode = status.getError().getErrorInfo().getCode(); + + assertThat(actualCode).isEqualTo("400"); + verifyZeroInteractions(converter); + verify(tnvzHelper).performQueryPersonRequest(any(), any(), any()); + } + + @Test + public void executePipelineWithTnvzRequest() { + var appDeliveryID = "tnvz-request"; + var expectedStatus = setupMocks(appDeliveryID, true); + + pipeline.accept(appDeliveryID); + var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); + var actualStatus = (DeliveryRequestStatusType) response.getResponse(); + + assertThat(actualStatus).isEqualTo(expectedStatus); + } + + @Test + public void rejectInvalidSignature() { + var appDeliveryID = "invalid-signature"; + setupMocks(appDeliveryID, true); + doThrow(moaZSException("Signature Invalid!")).when(verifier).accept(any()); + + pipeline.accept(appDeliveryID); + var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); + var status = (DeliveryRequestStatusType) response.getResponse(); + var actualCode = status.getError().getErrorInfo().getCode(); + + assertThat(actualCode).isEqualTo(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID); + + } + + private DeliveryRequestStatusType setupMocks(String appDeliveryId, boolean tnvzRequest) { + + var mzsRequest = setupMzsRequest(appDeliveryId, tnvzRequest); + var msgRequest = setupMsgRequest(appDeliveryId); + var status = setupStatus(appDeliveryId); + var signedStatus = new byte[0]; + var identification = IdentificationType.identificationTypeBuilder().build(); + repository.store(mzsRequest); + repository.store(getResponseID(appDeliveryId), signedStatus); + + when(converter.convert(eq(mzsRequest) )).thenReturn(msgRequest); + when(converter.convert(eq(mzsRequest), any())).thenReturn(msgRequest); + when(clientFactory.create(any(), same(App2ZusePort.class))).thenReturn(msgClient); + when(msgClient.delivery(msgRequest)).thenReturn(status); + + if (tnvzRequest) { + when(clientFactory.create(any(), same(TNVZServicePort.class))).thenReturn(tnvzClient); + when(tnvzHelper.performQueryPersonRequest(any(), any(), any())).thenReturn(identification); + } + return status; + + } + + private MoaZSException buildMzsException(String code, String appDeliveryID) { + return moaZSExceptionBuilder("") + .withErrorCode(code) + .withAppDeliveryID(appDeliveryID) + .build(); + } + + private DeliveryRequestStatusType setupStatus(String appDeliveryId) { + + var success = successBuilder() + .withAppDeliveryID(appDeliveryId) + .build(); + + return deliveryRequestStatusTypeBuilder() + .withSuccess(success) + .build(); + + } + + private DeliveryRequestType setupMzsRequest(String appDeliveryId, boolean tnvzRequest) { + + var msgClient = clientTypeBuilder() + .withURL("http://zuse") + .build(); + + var config = configTypeBuilder() + .withPerformQueryPersonRequest(tnvzRequest) + .withMSGClient(msgClient) + .build(); + + return deliveryRequestTypeBuilder() + .withMetaData(setupMetaData(appDeliveryId)) + .withConfig(config) + .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(); + } + +} diff --git a/src/test/java/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest.java b/src/test/java/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest.java index 2c6b978..34f4e1b 100644 --- a/src/test/java/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest.java +++ b/src/test/java/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest.java @@ -1,8 +1,7 @@ package at.gv.egiz.moazs; import at.gv.egiz.eid.authhandler.modules.sigverify.moasig.api.ISignatureVerificationService; -import at.gv.egiz.moazs.verify.MoaSPSSSignatureVerifier; -import at.gv.egiz.moazs.verify.SignatureVerifier; +import at.gv.egiz.moazs.backend.SignatureVerifier; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -27,7 +26,7 @@ import java.nio.file.Files; @Bean public SignatureVerifier verifier(@Autowired ISignatureVerificationService service){ - return new MoaSPSSSignatureVerifier(service, "test-trust-profile", true); + return new SignatureVerifier(service, "test-trust-profile", true); } } @@ -41,7 +40,7 @@ import java.nio.file.Files; var path = resourcesPath + "valid-signed-delivery-response.xml"; var signature = Files.readAllBytes(new File(path).toPath()); - verifier.verify(signature); + verifier.accept(signature); } @Test(expected = MoaZSException.class) @@ -50,7 +49,7 @@ import java.nio.file.Files; var path = resourcesPath + "invalid-signed-delivery-response.xml"; var signature = Files.readAllBytes(new File(path).toPath()); - verifier.verify(signature); + verifier.accept(signature); } } diff --git a/src/test/java/at/gv/egiz/moazs/ITMzsServiceTest.java b/src/test/java/at/gv/egiz/moazs/ITMzsServiceTest.java index a93c720..e9b4f70 100644 --- a/src/test/java/at/gv/egiz/moazs/ITMzsServiceTest.java +++ b/src/test/java/at/gv/egiz/moazs/ITMzsServiceTest.java @@ -1,12 +1,9 @@ package at.gv.egiz.moazs; -import at.gv.egiz.moazs.process.DeliveryPipeline; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.RequestStatusResponse; import org.junit.Test; import org.junit.runner.RunWith; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -25,6 +22,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.file.Paths; import java.util.GregorianCalendar; +import java.util.function.Consumer; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Success.successBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; @@ -40,8 +38,6 @@ public class ITMzsServiceTest { private final String basePath = "src/test/resources/at/gv/egiz/moazs/ITMzsServiceTest/"; - private final static Logger logger = LoggerFactory.getLogger(ITMzsServiceTest.class); - @TestConfiguration public static class TestConfig { @@ -50,7 +46,7 @@ public class ITMzsServiceTest { @Bean @Primary - public DeliveryPipeline deliveryPipelineThatAlwaysSucceeds() { + public Consumer deliveryPipelineThatAlwaysSucceeds() { return appDeliveryId -> { var success = successBuilder() diff --git a/src/test/java/at/gv/egiz/moazs/SaveResponseToFileSinkTest.java b/src/test/java/at/gv/egiz/moazs/SaveResponseToFileSinkTest.java index ca501c8..9ddd37e 100644 --- a/src/test/java/at/gv/egiz/moazs/SaveResponseToFileSinkTest.java +++ b/src/test/java/at/gv/egiz/moazs/SaveResponseToFileSinkTest.java @@ -1,6 +1,6 @@ package at.gv.egiz.moazs; -import at.gv.egiz.moazs.process.SaveResponseToFileSink; +import at.gv.egiz.moazs.backend.SaveResponseToFileSink; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.egiz.moazs.scheme.RequestStatusResponse; @@ -63,7 +63,7 @@ public class SaveResponseToFileSinkTest { var fileContent = "some content"; var status = setupMocks(fileContent); - sink.handle(status) + sink.apply(status) .thenRun(() -> assertFilesCreatedAndContentMatches(fileContent)); } diff --git a/src/test/java/at/gv/egiz/moazs/SingleThreadedDeliveryPipelineTest.java b/src/test/java/at/gv/egiz/moazs/SingleThreadedDeliveryPipelineTest.java deleted file mode 100644 index e245eb1..0000000 --- a/src/test/java/at/gv/egiz/moazs/SingleThreadedDeliveryPipelineTest.java +++ /dev/null @@ -1,204 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.egiz.moazs.process.DeliveryPipeline; -import at.gv.egiz.moazs.process.SingleThreadedDeliveryPipeline; -import at.gv.egiz.moazs.client.TnvzHelper; -import at.gv.egiz.moazs.scheme.*; -import at.gv.egiz.moazs.client.ClientFactory; -import at.gv.egiz.moazs.repository.DeliveryRepository; -import at.gv.egiz.moazs.repository.InMemoryDeliveryRepository; -import at.gv.egiz.moazs.verify.MoaSPSSSignatureVerifier; -import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; -import at.gv.zustellung.msg.xsd.App2ZusePort; -import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; -import at.gv.zustellung.msg.xsd.MetaData; -import at.gv.zustellung.msg.xsd.persondata.IdentificationType; -import at.gv.zustellung.tnvz.xsd.TNVZServicePort; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import static at.gv.egiz.moazs.MoaZSException.moaZSException; -import static at.gv.egiz.moazs.MoaZSException.moaZSExceptionBuilder; -import static at.gv.egiz.moazs.scheme.RequestStatusResponse.getResponseID; -import static at.gv.zustellung.app2mzs.xsd.ClientType.clientTypeBuilder; -import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; -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.MetaData.metaDataBuilder; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.same; -import static org.mockito.Mockito.*; - -@RunWith(MockitoJUnitRunner.class) -public class SingleThreadedDeliveryPipelineTest { - - private DeliveryRepository repository = new InMemoryDeliveryRepository(100); - - @Mock - private TnvzHelper tnvzHelper; - - @Mock - private ClientFactory clientFactory; - - @Mock - private App2ZusePort msgClient; - - @Mock - private TNVZServicePort tnvzClient; - - @Mock - private Mzs2MsgConverter converter; - - @Mock - private MoaSPSSSignatureVerifier verifier; - - - private DeliveryPipeline pipeline; - - @Before - public void setup() { - pipeline = new SingleThreadedDeliveryPipeline( - repository, - tnvzHelper, - converter, - clientFactory, - verifier); - } - - @Test - public void executePipelineWithoutTnvzRequest() { - var appDeliveryID = "no-tnvz-request"; - var expectedStatus = setupMocks(appDeliveryID, false); - - pipeline.processRequest(appDeliveryID); - - verifyZeroInteractions(tnvzHelper); - var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); - var actualStatus = (DeliveryRequestStatusType) response.getResponse(); - - assertThat(actualStatus).isEqualTo(expectedStatus); - } - - @Test - public void rejectDeliveryWhenReceiverIsNotAddressable() { - var appDeliveryID = "not-addressable"; - setupMocks(appDeliveryID, true); - when(tnvzHelper.performQueryPersonRequest(any(), any(), any())) - .thenThrow(buildMzsException("400", appDeliveryID)); - - pipeline.processRequest(appDeliveryID); - - var responseID = getResponseID(appDeliveryID); - var response = repository.retrieveResponse(responseID).get(); - var status = (DeliveryRequestStatusType) response.getResponse(); - var actualCode = status.getError().getErrorInfo().getCode(); - - assertThat(actualCode).isEqualTo("400"); - verifyZeroInteractions(converter); - verify(tnvzHelper).performQueryPersonRequest(any(), any(), any()); - } - - @Test - public void executePipelineWithTnvzRequest() { - var appDeliveryID = "tnvz-request"; - var expectedStatus = setupMocks(appDeliveryID, true); - - pipeline.processRequest(appDeliveryID); - var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); - var actualStatus = (DeliveryRequestStatusType) response.getResponse(); - - assertThat(actualStatus).isEqualTo(expectedStatus); - } - - @Test - public void rejectInvalidSignature() { - var appDeliveryID = "invalid-signature"; - setupMocks(appDeliveryID, true); - doThrow(moaZSException("Signature Invalid!")).when(verifier).verify(any()); - - pipeline.processRequest(appDeliveryID); - var response = repository.retrieveResponse(getResponseID(appDeliveryID)).get(); - var status = (DeliveryRequestStatusType) response.getResponse(); - var actualCode = status.getError().getErrorInfo().getCode(); - - assertThat(actualCode).isEqualTo(MoaZSException.ERROR_MOASP_SIGNATURE_INVALID); - - } - - private DeliveryRequestStatusType setupMocks(String appDeliveryId, boolean tnvzRequest) { - - var mzsRequest = setupMzsRequest(appDeliveryId, tnvzRequest); - var msgRequest = setupMsgRequest(appDeliveryId); - var status = setupStatus(appDeliveryId); - var signedStatus = new byte[0]; - var identification = IdentificationType.identificationTypeBuilder().build(); - repository.store(mzsRequest); - repository.store(getResponseID(appDeliveryId), signedStatus); - - when(converter.convert(eq(mzsRequest) )).thenReturn(msgRequest); - when(converter.convert(eq(mzsRequest), any())).thenReturn(msgRequest); - when(clientFactory.create(any(), same(App2ZusePort.class))).thenReturn(msgClient); - when(msgClient.delivery(msgRequest)).thenReturn(status); - - if (tnvzRequest) { - when(clientFactory.create(any(), same(TNVZServicePort.class))).thenReturn(tnvzClient); - when(tnvzHelper.performQueryPersonRequest(any(), any(), any())).thenReturn(identification); - } - return status; - - } - - private MoaZSException buildMzsException(String code, String appDeliveryID) { - return moaZSExceptionBuilder("") - .withErrorCode(code) - .withAppDeliveryID(appDeliveryID) - .build(); - } - - private DeliveryRequestStatusType setupStatus(String appDeliveryId) { - - var success = successBuilder() - .withAppDeliveryID(appDeliveryId) - .build(); - - return deliveryRequestStatusTypeBuilder() - .withSuccess(success) - .build(); - - } - - private DeliveryRequestType setupMzsRequest(String appDeliveryId, boolean tnvzRequest) { - - var msgClient = clientTypeBuilder() - .withURL("http://zuse") - .build(); - - var config = configTypeBuilder() - .withPerformQueryPersonRequest(tnvzRequest) - .withMSGClient(msgClient) - .build(); - - return deliveryRequestTypeBuilder() - .withMetaData(setupMetaData(appDeliveryId)) - .withConfig(config) - .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