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.msg.xsd.DeliveryRequestType; import at.gv.zustellung.msg.xsd.ObjectFactory; 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 java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.math.BigInteger; 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(); // this test requires that a zusemsg service runs under httpServiceUri! // tmp disabled. todo: set up integration tests // @Test public void sendValidMessage() throws IOException { var request = loadFromFile("validDeliveryRequest.xml"); var httpServiceUri = "http://localhost:8081/services/DeliveryRequest"; var clientParams = generateClientParams(httpServiceUri); 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()); } } //@Test public void sendValidMessageSSL() throws IOException { var request = loadFromFile("validDeliveryRequest.xml"); var sslServiceUri = "https://localhost/zusemsg/services/DeliveryRequest"; var clientParams = generateSSLClientParams(sslServiceUri); var client = factory.create(clientParams); var status = client.delivery(request); log.info("status: " + msgMarshaller.marshallXml(OF.createDeliveryRequestStatus(status))); } private ClientType generateSSLClientParams(String sslServiceUri) { var keystore = keyStoreTypeBuilder() .withFileName("ssl/client.cert.key.p12") .withFileType("PKCS12") .withPassword("123456") .build(); var truststore = keyStoreTypeBuilder() .withFileName("ssl/truststore.jks") .withPassword("123456") .withFileType("JKS") .build(); var sslParams = SSLTypeBuilder() .withLaxHostNameVerification(false) .withTrustAll(false) .withKeyStore(keystore) .withTrustStore(truststore) .build(); return ClientType.clientTypeBuilder() .withURL(sslServiceUri) .withSSL(sslParams) .withReceiveTimeout(BigInteger.ZERO) .withConnectionTimeout(BigInteger.ZERO) .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(); } } private ClientType generateClientParams(String url) { return ClientType.clientTypeBuilder().withURL(url).build(); } }