Skip to content
Permalink
Browse files

How to mock SOAP web service

  • Loading branch information...
slem1 committed Oct 30, 2017
1 parent c2e78f2 commit aa10cea4744b120d2eef1debb4e66522975e6b2c
@@ -29,6 +29,17 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@@ -0,0 +1,17 @@
package fr.sle.testhowto;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.client.core.WebServiceTemplate;

/**
* @author slemoine
*/
@Configuration
public class MyConfig {

@Bean
public WebServiceTemplate getWebServiceTemplate() {
return new WebServiceTemplate();
}
}
@@ -4,7 +4,16 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.WebServiceMessageExtractor;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapMessage;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@@ -21,6 +30,9 @@
@Autowired
private RestTemplate restTemplate;

@Autowired
private WebServiceTemplate webServiceTemplate;

public void createnewMyEntity() {
myEntityJpaRepository.save(new MyEntity());
}
@@ -32,4 +44,23 @@ public void createnewMyEntity() {
public Map<String,String> getExternalData(){
return restTemplate.getForObject("http://mythirdpartyhost/rest/api/data", Map.class);
}


public void callASoapWebService(){

WebServiceMessageCallback requestCallback = message -> {

SoapMessage soapMessage = (SoapMessage) message;

SoapHeader soapHeader = soapMessage.getSoapHeader();
soapHeader.addHeaderElement(new QName("namespace", "username")).setText("user1");
soapHeader.addHeaderElement(new QName("namespace", "password")).setText("password1");

};

WebServiceMessageCallback responseCallback = message -> {
};

webServiceTemplate.sendAndReceive("/CosignWS/CosignWS", requestCallback, responseCallback);
}
}
@@ -18,7 +18,19 @@
import org.springframework.test.web.client.response.MockRestResponseCreators;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.test.client.MockWebServiceServer;
import org.springframework.ws.test.client.RequestMatchers;
import org.springframework.ws.test.client.ResponseCreators;
import org.springframework.xml.transform.StringSource;

import javax.xml.transform.Source;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

@@ -40,6 +52,9 @@
@Autowired
private RestTemplate restTemplate;

@Autowired
private WebServiceTemplate webServiceTemplate;

@Test
public void saveMyEntityShouldCreateNewEntity() {
testEntityManager.persistAndGetId(new MyEntity());
@@ -64,4 +79,29 @@ public void getExternalData(){

Assert.assertEquals("value",externalData.get("key"));
}

@Test
public void callASoapWebServiceTest() throws URISyntaxException, IOException {

URL resourceRequestXml = getClass().getResource("/soap/soap-request.xml");
byte[] resourceRequestBytes = Files.readAllBytes(Paths.get(resourceRequestXml.toURI()));
String xmlSourceRequest = new String(resourceRequestBytes, StandardCharsets.UTF_8);

URL resourceResponseXml = getClass().getResource("/soap/soap-response.xml");
byte[] resourceResponseBytes = Files.readAllBytes(Paths.get(resourceResponseXml.toURI()));
String xmlSourceResponse = new String(resourceResponseBytes, StandardCharsets.UTF_8);

Source requestPayload = new StringSource(xmlSourceRequest);

Source responsePayload = new StringSource(xmlSourceResponse);

MockWebServiceServer server = MockWebServiceServer.createServer(webServiceTemplate);

server.expect(RequestMatchers.soapEnvelope(requestPayload))
.andRespond(ResponseCreators.withSoapEnvelope(responsePayload));

myEntityService.callASoapWebService();

server.verify();
}
}
@@ -5,6 +5,7 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.ws.client.core.WebServiceTemplate;

/**
* @author slemoine
@@ -0,0 +1,8 @@
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<username xmlns="namespace">user1</username>
<password xmlns="namespace">password1</password>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
</S:Body>
</S:Envelope>

0 comments on commit aa10cea

Please sign in to comment.
You can’t perform that action at this time.