From c43f9764b2132ce54a491c1daeeafe6aa4851ef5 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Fri, 28 Jun 2019 10:29:50 +0200 Subject: Separate Test Suite into Unit- And Integration Tests Per default, integration tests are excluded from build. They can be included with the `-P integration-test` argument. - Pom: Add maven profile for integration tests. - Rename integration tests (add `IT` prefix). - Move Resources into IT* folders. - Add MsgClientTest to test suite and add assertions to each test case. --- .../egiz/moazs/ITMoaSPSSSignatureVerifierTest.java | 56 +++++++ .../java/at/gv/egiz/moazs/ITMsgClientTest.java | 185 +++++++++++++++++++++ .../java/at/gv/egiz/moazs/ITMzsServiceTest.java | 131 +++++++++++++++ .../egiz/moazs/MoaSPSSSignatureVerifierTest.java | 56 ------- src/test/java/at/gv/egiz/moazs/MsgClientTest.java | 182 -------------------- src/test/java/at/gv/egiz/moazs/MzsServiceTest.java | 131 --------------- 6 files changed, 372 insertions(+), 369 deletions(-) create mode 100644 src/test/java/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest.java create mode 100644 src/test/java/at/gv/egiz/moazs/ITMsgClientTest.java create mode 100644 src/test/java/at/gv/egiz/moazs/ITMzsServiceTest.java delete mode 100644 src/test/java/at/gv/egiz/moazs/MoaSPSSSignatureVerifierTest.java delete mode 100644 src/test/java/at/gv/egiz/moazs/MsgClientTest.java delete mode 100644 src/test/java/at/gv/egiz/moazs/MzsServiceTest.java (limited to 'src/test/java') diff --git a/src/test/java/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest.java b/src/test/java/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest.java new file mode 100644 index 0000000..2c6b978 --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest.java @@ -0,0 +1,56 @@ +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 org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +//Note: Certificate that signed these delivery responses expires in 2023-09-27. +@RunWith(SpringRunner.class) +@SpringBootTest + public class ITMoaSPSSSignatureVerifierTest { + + private final String resourcesPath = "src/test/resources/at/gv/egiz/moazs/ITMoaSPSSSignatureVerifierTest/"; + + @TestConfiguration + public class Config{ + + @Bean + public SignatureVerifier verifier(@Autowired ISignatureVerificationService service){ + return new MoaSPSSSignatureVerifier(service, "test-trust-profile", true); + } + + } + + @Autowired + private SignatureVerifier verifier; + + @Test + public void acceptValidSignedDeliveryResponse() throws IOException { + + var path = resourcesPath + "valid-signed-delivery-response.xml"; + var signature = Files.readAllBytes(new File(path).toPath()); + + verifier.verify(signature); + } + + @Test(expected = MoaZSException.class) + public void rejectInvalidSignedDeliveryResponse() throws IOException { + + var path = resourcesPath + "invalid-signed-delivery-response.xml"; + var signature = Files.readAllBytes(new File(path).toPath()); + + verifier.verify(signature); + } + +} diff --git a/src/test/java/at/gv/egiz/moazs/ITMsgClientTest.java b/src/test/java/at/gv/egiz/moazs/ITMsgClientTest.java new file mode 100644 index 0000000..10c2859 --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/ITMsgClientTest.java @@ -0,0 +1,185 @@ +package at.gv.egiz.moazs; + +import at.gv.egiz.moazs.msg.MsgClientFactory; +import at.gv.egiz.moazs.msg.StoreSOAPBodyBinaryInRepositoryInterceptor; +import at.gv.egiz.moazs.scheme.Marshaller; +import at.gv.zustellung.app2mzs.xsd.ClientType; +import at.gv.zustellung.app2mzs.xsd.KeyStoreType; +import at.gv.zustellung.msg.xsd.DeliveryRequestType; +import at.gv.zustellung.msg.xsd.ObjectFactory; +import org.assertj.core.api.Assertions; +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.test.context.junit4.SpringRunner; + +import javax.xml.bind.JAXBElement; +import javax.xml.ws.soap.SOAPFaultException; +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.math.BigInteger; + +import static at.gv.zustellung.app2mzs.xsd.ClientType.clientTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.KeyStoreType.keyStoreTypeBuilder; +import static at.gv.zustellung.app2mzs.xsd.SSLType.SSLTypeBuilder; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ITMsgClientTest { + + private final static Logger log = LoggerFactory.getLogger(ITMsgClientTest.class); + private static final Object VALID_MZS_REQUEST_ID = "valid-delivery-request-id" ; + private final String basePath = "src/test/resources/at/gv/egiz/moazs/ITMsgClientTest/"; + + @Autowired + private Marshaller msgMarshaller; + + @Autowired + private MsgClientFactory factory; + + @Autowired + private StoreSOAPBodyBinaryInRepositoryInterceptor interceptor; + + private static final ObjectFactory OF = new ObjectFactory(); + + + // Requirements: + // - run zusemsg service under httpServiceURL + @Test + public void sendValidMessage() throws IOException { + + var request = loadFromFile("validDeliveryRequest.xml"); + var httpServiceURL = "http://localhost:8081/services/DeliveryRequest"; + var clientParams = clientTypeBuilder() + .withURL(httpServiceURL) + .withConnectionTimeout(BigInteger.ZERO) + .withReceiveTimeout(BigInteger.ZERO) + .build(); + + var client = factory.create(clientParams); + + var status = client.delivery(request); + assertThat(status.getPartialSuccess().getAppDeliveryID()).isEqualTo(VALID_MZS_REQUEST_ID); + + } + + // Requirements: + // - run zusemsg service under httpsServiceURL + // - server trusts client cert (by trusting CA bundle in ssl/trusted-cas-bundle.pem) + // - server uses the server certificate in ssl/server/server.localhost.*.pem + // - server sends certificate chain ssl/server/ca-chain.cert.pem + @Test + public void sendOverSSLWithClientAuthentication() throws IOException { + + var request = loadFromFile("validDeliveryRequest.xml"); + var httpsServiceURL = "https://localhost/zusemsg/services/DeliveryRequest"; + + var clientParams = generateSSLClientParams(httpsServiceURL, false, false); + var client = factory.create(clientParams); + + var status = client.delivery(request); + assertThat(status.getPartialSuccess().getAppDeliveryID()).isEqualTo(VALID_MZS_REQUEST_ID); + } + + // Requirements: + // - run zusemsg service under httpsServiceURL + // - server trusts client cert (by trusting CA bundle in ssl/trusted-cas-bundle.pem) + // - server uses the server certificate in ssl/server/server.localhost.*.pem + // - server sends certificate chain ssl/server/ca-chain.cert.pem + @Test + public void sendOverSSLWithClientAuthTrustAll() throws IOException { + + var request = loadFromFile("validDeliveryRequest.xml"); + var httpsServiceURL = "https://localhost/zusemsg/services/DeliveryRequest"; + + var clientParams = generateSSLClientParams(httpsServiceURL, true, false); + var client = factory.create(clientParams); + + var status = client.delivery(request); + assertThat(status.getPartialSuccess().getAppDeliveryID()).isEqualTo(VALID_MZS_REQUEST_ID); + } + + // Requirements: + // - run zusemsg service under httpsServiceURL (e.g. by adding notlocalhost to /etc/hosts) + // - server trusts client cert (by trusting CA bundle in ssl/trusted-cas-bundle.pem) + // - server uses the server certificate in ssl/server/server.localhost.*.pem + // - server sends certificate chain ssl/server/ca-chain.cert.pem + @Test + public void sendOverSSLWithClientAuthLaxHostnameVerification() throws IOException { + + var request = loadFromFile("validDeliveryRequest.xml"); + var httpsServiceURL = "https://notlocalhost/zusemsg/services/DeliveryRequest"; + + var clientParams = generateSSLClientParams(httpsServiceURL, false, true); + var client = factory.create(clientParams); + + var status = client.delivery(request); + assertThat(status.getPartialSuccess().getAppDeliveryID()).isEqualTo(VALID_MZS_REQUEST_ID); + } + + //Requirements: + // - run zusemsg service under httpsServiceURL (e.g. by adding notlocalhost to /etc/hosts) + // - server trusts client cert (by trusting CA bundle in ssl/trusted-cas-bundle.pem) + // - server uses the server certificate in ssl/server/server.localhost.*.pem + // - server sends certificate chain ssl/server/ca-chain.cert.pem + @Test(expected=SOAPFaultException.class) + public void rejectBecauseHostNameVerificationFails() throws IOException { + + var request = loadFromFile("validDeliveryRequest.xml"); + var httpsServiceURL = "https://notlocalhost/zusemsg/services/DeliveryRequest"; + + var clientParams = generateSSLClientParams(httpsServiceURL, false, false); + var client = factory.create(clientParams); + + client.delivery(request); + } + + private ClientType generateSSLClientParams(String httpsServiceURL, boolean trustAll, boolean laxHostNameVerification) { + return generateSSLClientParams(httpsServiceURL, trustAll, laxHostNameVerification, generateTrustLocalhostStore()); + } + + private ClientType generateSSLClientParams(String httpsServiceURL, boolean trustAll, boolean laxHostNameVerification, KeyStoreType truststore) { + + var keystore = keyStoreTypeBuilder() + .withFileName("ssl/client.cert.key.p12") + .withFileType("PKCS12") + .withPassword("123456") + .build(); + + var sslParams = SSLTypeBuilder() + .withLaxHostNameVerification(laxHostNameVerification) + .withTrustAll(trustAll) + .withKeyStore(keystore) + .withTrustStore(trustAll ? null : truststore) + .build(); + + return clientTypeBuilder() + .withURL(httpsServiceURL) + .withSSL(sslParams) + .withReceiveTimeout(BigInteger.ZERO) + .withConnectionTimeout(BigInteger.ZERO) + .build(); + + } + + private KeyStoreType generateTrustLocalhostStore() { + return keyStoreTypeBuilder() + .withFileName("ssl/truststore.jks") + .withPassword("123456") + .withFileType("JKS") + .build(); + } + + private DeliveryRequestType loadFromFile(String fileName) throws IOException { + try (var inputStream = new BufferedInputStream(new FileInputStream(basePath + fileName))) { + var request = (JAXBElement) msgMarshaller.unmarshallXml(inputStream); + return request.getValue(); + } + } + +} diff --git a/src/test/java/at/gv/egiz/moazs/ITMzsServiceTest.java b/src/test/java/at/gv/egiz/moazs/ITMzsServiceTest.java new file mode 100644 index 0000000..4f24518 --- /dev/null +++ b/src/test/java/at/gv/egiz/moazs/ITMzsServiceTest.java @@ -0,0 +1,131 @@ +package at.gv.egiz.moazs; + +import at.gv.egiz.moazs.pipeline.DeliveryPipeline; +import at.gv.egiz.moazs.repository.DeliveryRepository; +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; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Paths; +import java.util.GregorianCalendar; + +import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Success.successBuilder; +import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; +import static java.net.http.HttpClient.Version; +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) + +public class ITMzsServiceTest { + + private final String serviceUri = "http://localhost:8080/services/DeliveryRequest"; + + 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 { + + @Autowired + private DeliveryRepository repository; + + @Bean + @Primary + public DeliveryPipeline deliveryPipelineThatAlwaysSucceeds() { + return appDeliveryId -> { + + var success = successBuilder() + .withDeliverySystem("Test Delivery System") + .withZSDeliveryID("ZD-Delivery-ID") + .withAppDeliveryID(appDeliveryId) + .withDeliveryTimestamp(genTimeStamp()) + .build(); + + var status = deliveryRequestStatusTypeBuilder() + .withSuccess(success) + .build(); + + repository.add(status); + }; + } + + private XMLGregorianCalendar genTimeStamp() { + try { + return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()); + } catch (DatatypeConfigurationException e) { + throw new RuntimeException("ups"); + } + + } + } + + @Test + public void acceptValidDeliveryRequest() throws IOException, InterruptedException { + var response = sendDeliveryRequestFile("validDeliveryRequest.soap"); + assertEquals(200, response.statusCode()); + } + + @Test + public void rejectRequestWithoutSender() throws IOException, InterruptedException { + var response = sendDeliveryRequestFile("missingSender.soap"); + assertEquals(500, response.statusCode()); + } + + @Test + public void rejectBothProfileAndCorporateBody() throws IOException, InterruptedException { + var response = sendDeliveryRequestFile("profileAndCorporateBody.soap"); + assertEquals(500, response.statusCode()); + } + + @Test + public void rejectFormallyIncorrectDeliveryRequest() throws IOException, InterruptedException { + var response = sendDeliveryRequestFile("formallyIncorrectDeliveryRequest.soap"); + assertEquals(500, response.statusCode()); + } + + @Test + public void rejectRequestWithoutAppDeliveryID() throws IOException, InterruptedException { + var response = sendDeliveryRequestFile("missingAppDeliveryId.soap"); + assertEquals(500, response.statusCode()); + } + + @Test + public void rejectRequestWithoutMetaData() throws IOException, InterruptedException { + var response = sendDeliveryRequestFile("missingMetaData.soap"); + assertEquals(500, response.statusCode()); + } + + private HttpResponse sendDeliveryRequestFile(String fileName) throws IOException, InterruptedException { + + var path = basePath + fileName; + var client = HttpClient.newBuilder().version(Version.HTTP_1_1).build(); + var request = HttpRequest.newBuilder() + .uri(URI.create(serviceUri)) + .header("Content-Type", "text/xml;charset=UTF-8") + .header("SOAPAction", "\"\"") + .POST(HttpRequest.BodyPublishers.ofFile(Paths.get(path))) + .build(); + + return client.send(request, HttpResponse.BodyHandlers.ofString()); + + } + +} diff --git a/src/test/java/at/gv/egiz/moazs/MoaSPSSSignatureVerifierTest.java b/src/test/java/at/gv/egiz/moazs/MoaSPSSSignatureVerifierTest.java deleted file mode 100644 index 7d3e9d1..0000000 --- a/src/test/java/at/gv/egiz/moazs/MoaSPSSSignatureVerifierTest.java +++ /dev/null @@ -1,56 +0,0 @@ -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 org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.test.context.junit4.SpringRunner; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; - -//Note: Certificate that signed these delivery responses expires in 2023-09-27. -@RunWith(SpringRunner.class) -@SpringBootTest - public class MoaSPSSSignatureVerifierTest { - - private final String resourcesPath = "src/test/resources/at/gv/egiz/moazs/MoaSPSSSignatureVerifierTest/"; - - @TestConfiguration - public class Config{ - - @Bean - public SignatureVerifier verifier(@Autowired ISignatureVerificationService service){ - return new MoaSPSSSignatureVerifier(service, "test-trust-profile", true); - } - - } - - @Autowired - private SignatureVerifier verifier; - - @Test - public void acceptValidSignedDeliveryResponse() throws IOException { - - var path = resourcesPath + "valid-signed-delivery-response.xml"; - var signature = Files.readAllBytes(new File(path).toPath()); - - verifier.verify(signature); - } - - @Test(expected = MoaZSException.class) - public void rejectInvalidSignedDeliveryResponse() throws IOException { - - var path = resourcesPath + "invalid-signed-delivery-response.xml"; - var signature = Files.readAllBytes(new File(path).toPath()); - - verifier.verify(signature); - } - -} diff --git a/src/test/java/at/gv/egiz/moazs/MsgClientTest.java b/src/test/java/at/gv/egiz/moazs/MsgClientTest.java deleted file mode 100644 index 485d01c..0000000 --- a/src/test/java/at/gv/egiz/moazs/MsgClientTest.java +++ /dev/null @@ -1,182 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.egiz.moazs.msg.MsgClientFactory; -import at.gv.egiz.moazs.msg.StoreSOAPBodyBinaryInRepositoryInterceptor; -import at.gv.egiz.moazs.scheme.Marshaller; -import at.gv.zustellung.app2mzs.xsd.ClientType; -import at.gv.zustellung.app2mzs.xsd.KeyStoreType; -import at.gv.zustellung.msg.xsd.DeliveryRequestType; -import at.gv.zustellung.msg.xsd.ObjectFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -import javax.xml.bind.JAXBElement; -import java.io.BufferedInputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.math.BigInteger; - -import static at.gv.zustellung.app2mzs.xsd.ClientType.clientTypeBuilder; -import static at.gv.zustellung.app2mzs.xsd.KeyStoreType.keyStoreTypeBuilder; -import static at.gv.zustellung.app2mzs.xsd.SSLType.SSLTypeBuilder; - -// @RunWith(SpringRunner.class) -// @SpringBootTest - -public class MsgClientTest { - - private final static Logger log = LoggerFactory.getLogger(MsgClientTest.class); - private final String basePath = "src/test/resources/at/gv/egiz/moazs/MsgClientTest/"; - - @Autowired - private Marshaller msgMarshaller; - - @Autowired - private MsgClientFactory factory; - - @Autowired - private StoreSOAPBodyBinaryInRepositoryInterceptor interceptor; - - private static final ObjectFactory OF = new ObjectFactory(); - - - // tmp disabled. todo: set up integration tests - - // Requirements: - // - run zusemsg service under httpServiceURL - // @Test - public void sendValidMessage() throws IOException { - - var request = loadFromFile("validDeliveryRequest.xml"); - var httpServiceURL = "http://localhost:8081/services/DeliveryRequest"; - var clientParams = clientTypeBuilder() - .withURL(httpServiceURL) - .build(); - - var client = factory.create(clientParams); - - try{ - var status = client.delivery(request); - log.info("status: " + msgMarshaller.marshallXml(OF.createDeliveryResponse(status))); - } catch (Exception ex) { - System.out.println(ex.getMessage()); - } - } - - // Requirements: - // - run zusemsg service under httpsServiceURL - // - server trusts client cert (by trusting CA bundle in ssl/trusted-cas-bundle.pem) - // - server uses the server certificate in ssl/server/server.localhost.*.pem - // - server sends certificate chain ssl/server/ca-chain.cert.pem - //@Test - public void sendOverSSLWithClientAuthentication() throws IOException { - - var request = loadFromFile("validDeliveryRequest.xml"); - var httpsServiceURL = "https://localhost/zusemsg/services/DeliveryRequest"; - - var clientParams = generateSSLClientParams(httpsServiceURL, false, false); - var client = factory.create(clientParams); - - var status = client.delivery(request); - log.info("status: " + msgMarshaller.marshallXml(OF.createDeliveryRequestStatus(status))); - } - - // Requirements: - // - run zusemsg service under httpsServiceURL - // - server trusts client cert (by trusting CA bundle in ssl/trusted-cas-bundle.pem) - // - server uses the server certificate in ssl/server/server.localhost.*.pem - // - server sends certificate chain ssl/server/ca-chain.cert.pem - //@Test - public void sendOverSSLWithClientAuthTrustAll() throws IOException { - - var request = loadFromFile("validDeliveryRequest.xml"); - var httpsServiceURL = "https://localhost/zusemsg/services/DeliveryRequest"; - - var clientParams = generateSSLClientParams(httpsServiceURL, true, false); - var client = factory.create(clientParams); - - var status = client.delivery(request); - log.info("status: " + msgMarshaller.marshallXml(OF.createDeliveryRequestStatus(status))); - } - - // Requirements: - // - run zusemsg service under httpsServiceURL (e.g. by adding notlocalhost to /etc/hosts) - // - server trusts client cert (by trusting CA bundle in ssl/trusted-cas-bundle.pem) - // - server uses the server certificate in ssl/server/server.localhost.*.pem - // - server sends certificate chain ssl/server/ca-chain.cert.pem - //@Test - public void sendOverSSLWithClientAuthLaxHostnameVerification() throws IOException { - - var request = loadFromFile("validDeliveryRequest.xml"); - var httpsServiceURL = "https://notlocalhost/zusemsg/services/DeliveryRequest"; - - var clientParams = generateSSLClientParams(httpsServiceURL, false, true); - var client = factory.create(clientParams); - - var status = client.delivery(request); - log.info("status: " + msgMarshaller.marshallXml(OF.createDeliveryRequestStatus(status))); - } - - //Requirements: - // - run zusemsg service under httpsServiceURL (e.g. by adding notlocalhost to /etc/hosts) - // - server trusts client cert (by trusting CA bundle in ssl/trusted-cas-bundle.pem) - // - server uses the server certificate in ssl/server/server.localhost.*.pem - // - server sends certificate chain ssl/server/ca-chain.cert.pem - //@Test(expected=SOAPFaultException.class) - public void rejectBecauseHostNameVerificationFails() throws IOException { - - var request = loadFromFile("validDeliveryRequest.xml"); - var httpsServiceURL = "https://notlocalhost/zusemsg/services/DeliveryRequest"; - - var clientParams = generateSSLClientParams(httpsServiceURL, false, false); - var client = factory.create(clientParams); - - var status = client.delivery(request); - log.info("status: " + msgMarshaller.marshallXml(OF.createDeliveryRequestStatus(status))); - } - - private ClientType generateSSLClientParams(String httpsServiceURL, boolean trustAll, boolean laxHostNameVerification) { - return generateSSLClientParams(httpsServiceURL, trustAll, laxHostNameVerification, generateTrustLocalhostStore()); - } - - private ClientType generateSSLClientParams(String httpsServiceURL, boolean trustAll, boolean laxHostNameVerification, KeyStoreType truststore) { - - var keystore = keyStoreTypeBuilder() - .withFileName("ssl/client.cert.key.p12") - .withFileType("PKCS12") - .withPassword("123456") - .build(); - - var sslParams = SSLTypeBuilder() - .withLaxHostNameVerification(laxHostNameVerification) - .withTrustAll(trustAll) - .withKeyStore(keystore) - .withTrustStore(trustAll ? null : truststore) - .build(); - - return clientTypeBuilder() - .withURL(httpsServiceURL) - .withSSL(sslParams) - .withReceiveTimeout(BigInteger.ZERO) - .withConnectionTimeout(BigInteger.ZERO) - .build(); - - } - - private KeyStoreType generateTrustLocalhostStore() { - return keyStoreTypeBuilder() - .withFileName("ssl/truststore.jks") - .withPassword("123456") - .withFileType("JKS") - .build(); - } - - private DeliveryRequestType loadFromFile(String fileName) throws IOException { - try (var inputStream = new BufferedInputStream(new FileInputStream(basePath + fileName))) { - var request = (JAXBElement) msgMarshaller.unmarshallXml(inputStream); - return request.getValue(); - } - } - -} diff --git a/src/test/java/at/gv/egiz/moazs/MzsServiceTest.java b/src/test/java/at/gv/egiz/moazs/MzsServiceTest.java deleted file mode 100644 index 579051d..0000000 --- a/src/test/java/at/gv/egiz/moazs/MzsServiceTest.java +++ /dev/null @@ -1,131 +0,0 @@ -package at.gv.egiz.moazs; - -import at.gv.egiz.moazs.pipeline.DeliveryPipeline; -import at.gv.egiz.moazs.repository.DeliveryRepository; -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; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.test.context.junit4.SpringRunner; - -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.nio.file.Paths; -import java.util.GregorianCalendar; - -import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Success.successBuilder; -import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; -import static java.net.http.HttpClient.Version; -import static org.junit.Assert.assertEquals; - -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) - -public class MzsServiceTest { - - private final String serviceUri = "http://localhost:8080/services/DeliveryRequest"; - - private final String basePath = "src/test/resources/at/gv/egiz/moazs/MzsServiceTest/"; - - private final static Logger logger = LoggerFactory.getLogger(MzsServiceTest.class); - - @TestConfiguration - public static class TestConfig { - - @Autowired - private DeliveryRepository repository; - - @Bean - @Primary - public DeliveryPipeline deliveryPipelineThatAlwaysSucceeds() { - return appDeliveryId -> { - - var success = successBuilder() - .withDeliverySystem("Test Delivery System") - .withZSDeliveryID("ZD-Delivery-ID") - .withAppDeliveryID(appDeliveryId) - .withDeliveryTimestamp(genTimeStamp()) - .build(); - - var status = deliveryRequestStatusTypeBuilder() - .withSuccess(success) - .build(); - - repository.add(status); - }; - } - - private XMLGregorianCalendar genTimeStamp() { - try { - return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()); - } catch (DatatypeConfigurationException e) { - throw new RuntimeException("ups"); - } - - } - } - - @Test - public void acceptValidDeliveryRequest() throws IOException, InterruptedException { - var response = sendDeliveryRequestFile("validDeliveryRequest.soap"); - assertEquals(200, response.statusCode()); - } - - @Test - public void rejectRequestWithoutSender() throws IOException, InterruptedException { - var response = sendDeliveryRequestFile("missingSender.soap"); - assertEquals(500, response.statusCode()); - } - - @Test - public void rejectBothProfileAndCorporateBody() throws IOException, InterruptedException { - var response = sendDeliveryRequestFile("profileAndCorporateBody.soap"); - assertEquals(500, response.statusCode()); - } - - @Test - public void rejectFormallyIncorrectDeliveryRequest() throws IOException, InterruptedException { - var response = sendDeliveryRequestFile("formallyIncorrectDeliveryRequest.soap"); - assertEquals(500, response.statusCode()); - } - - @Test - public void rejectRequestWithoutAppDeliveryID() throws IOException, InterruptedException { - var response = sendDeliveryRequestFile("missingAppDeliveryId.soap"); - assertEquals(500, response.statusCode()); - } - - @Test - public void rejectRequestWithoutMetaData() throws IOException, InterruptedException { - var response = sendDeliveryRequestFile("missingMetaData.soap"); - assertEquals(500, response.statusCode()); - } - - private HttpResponse sendDeliveryRequestFile(String fileName) throws IOException, InterruptedException { - - var path = basePath + fileName; - var client = HttpClient.newBuilder().version(Version.HTTP_1_1).build(); - var request = HttpRequest.newBuilder() - .uri(URI.create(serviceUri)) - .header("Content-Type", "text/xml;charset=UTF-8") - .header("SOAPAction", "\"\"") - .POST(HttpRequest.BodyPublishers.ofFile(Paths.get(path))) - .build(); - - return client.send(request, HttpResponse.BodyHandlers.ofString()); - - } - -} -- cgit v1.2.3