Skip to content

Instantly share code, notes, and snippets.

@KomanRudden
Last active March 9, 2017 06:15
Show Gist options
  • Save KomanRudden/654eb7fdaeb216924eb9 to your computer and use it in GitHub Desktop.
Save KomanRudden/654eb7fdaeb216924eb9 to your computer and use it in GitHub Desktop.
Mocking Web Service
package za.co.fnb.cbs.plexus.business.service.ejb.adeptra;
import javax.ejb.Stateless;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.Adeptra;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.Adeptra_Service;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseData;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseResponseType;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseResponseType1;
/**
*
* @author Koman Rudden [f3240479]
*/
@Stateless
public class AdeptraClientService implements AdeptraClientServiceLocal {
@Override
public String callAdeptra(CaseData caseData) {
final Adeptra adeptraClient = createAdeptraClient();
final CaseResponseType caseResponseType = adeptraClient.setCaseData(caseData);
final CaseResponseType1 caseDataResponse = caseResponseType.getCaseDataResponse();
return caseDataResponse.getStatus().getMessage();
}
protected Adeptra createAdeptraClient() {
Adeptra_Service service = new Adeptra_Service();
return service.getAdeptraSOAP();
}
}
package za.co.fnb.cbs.plexus.business.service.ejb.adeptra;
import javax.ejb.Local;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseData;
/**
*
* @author Koman Rudden [f3240479]
*/
@Local
public interface AdeptraClientServiceLocal {
public String callAdeptra(CaseData caseData);
}
package za.co.fnb.cbs.plexus.business.service.ejb.adeptra;
import static org.junit.Assert.*;
import static org.powermock.api.mockito.PowerMockito.*;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Endpoint;
import javax.xml.namespace.QName;
import java.net.URL;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.MockAdeptraImpl;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.Adeptra;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.Adeptra_Service;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseData;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseInfoType;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseResponseType;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseType;
/**
*
* @author Koman Rudden [f3240479]
*/
@RunWith(PowerMockRunner.class)
public class AdeptraClientServiceTest {
protected static Endpoint ep;
protected static String address;
protected AdeptraClientService clientService;
@BeforeClass
public static void setUp() {
address = "http://localhost:9000/services/MockAdeptraService";
ep = Endpoint.publish(address, new MockAdeptraImpl());
}
@Before
public void beforeTest() throws Exception {
clientService = spy(new AdeptraClientService());
when(clientService, "createAdeptraClient").thenReturn(createPort());
}
@AfterClass
public static void tearDown() {
try {
ep.stop();
} catch (Throwable t) {
System.out.println("Error thrown: " + t.getMessage());
}
}
@Test
public void should_return_correct_case_id_successfully() throws Exception {
CaseData caseData = new CaseData();
CaseType caseType = new CaseType();
CaseInfoType caseInfoType = new CaseInfoType();
caseInfoType.setCaseId("123");
caseType.setCaseInfo(caseInfoType);
caseData.setCase(caseType);
final String response = clientService.callAdeptra(caseData);
verifyPrivate(clientService).invoke("createAdeptraClient");
assertNotNull("Response is null", response);
assertEquals("OK", response);
}
private Adeptra createPort() {
URL wsdlLocation = this.getClass().getResource("/wsdl/adeptra.wsdl");
QName service = new QName("http://www.adeptra.net/Adeptra/", "Adeptra");
final Adeptra port = new Adeptra_Service(wsdlLocation, service).getAdeptraSOAP();
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);
assertNotNull("Port is null", port);
return port;
}
}
package za.co.fnb.cbs.plexus.business.service.ws.adeptra;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.*;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by Apache CXF 2.7.5
* 2015-06-24T14:58:04.193+02:00
* Generated source version: 2.7.5
*
*/
@WebService(targetNamespace = "http://www.adeptra.net/Adeptra/", name = "Adeptra")
@XmlSeeAlso({ObjectFactory.class})
public interface MockAdeptra {
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebResult(name = "setCaseDeleteResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", partName = "parameters")
@WebMethod(action = "http://www.adeptra.net/Adeptra/setCaseDelete")
public SetCaseDeleteResponse setCaseDelete(
@WebParam(partName = "parameters", name = "setCaseDeleteRequest", targetNamespace = "http://www.adeptra.net/Adeptra/")
SetCaseDeleteRequest parameters
);
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebResult(name = "getCaseDataResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", partName = "parameters")
@WebMethod(action = "http://www.adeptra.net/Adeptra/getCaseData")
public CaseData getCaseData(
@WebParam(partName = "parameters", name = "getCaseDataRequest", targetNamespace = "http://www.adeptra.net/Adeptra/")
GetCaseDataRequest parameters
);
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebResult(name = "setCaseResultResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", partName = "parameters")
@WebMethod(action = "http://www.adeptra.net/Adeptra/setCaseResult")
public SetCaseResultResponse setCaseResult(
@WebParam(partName = "parameters", name = "setCaseResultRequest", targetNamespace = "http://www.adeptra.net/Adeptra/")
SetCaseResultRequest parameters
);
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebResult(name = "getCaseStatusResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", partName = "parameters")
@WebMethod(action = "http://www.adeptra.net/Adeptra/getCaseStatus")
public GetCaseStatusResponse getCaseStatus(
@WebParam(partName = "parameters", name = "getCaseStatusRequest", targetNamespace = "http://www.adeptra.net/Adeptra/")
GetCaseStatusRequest parameters
);
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebResult(name = "getNextCaseIdListResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", partName = "parameters")
@WebMethod(action = "http://www.adeptra.net/Adeptra/getNextCaseIdList")
public GetNextCaseIdListResponse getNextCaseIdList(
@WebParam(partName = "parameters", name = "getNextCaseIdListRequest", targetNamespace = "http://www.adeptra.net/Adeptra/")
GetNextCaseIdListRequest parameters
);
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebResult(name = "getVersionResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", partName = "parameters")
@WebMethod(action = "http://www.adeptra.net/Adeptra/getVersion")
public GetVersionResponse getVersion(
@WebParam(partName = "parameters", name = "getVersionRequest", targetNamespace = "http://www.adeptra.net/Adeptra/")
GetVersionRequest parameters
);
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebResult(name = "setCaseOpenResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", partName = "parameters")
@WebMethod(action = "http://www.adeptra.net/Adeptra/setCaseOpen")
public SetCaseOpenResponse setCaseOpen(
@WebParam(partName = "parameters", name = "setCaseOpenRequest", targetNamespace = "http://www.adeptra.net/Adeptra/")
SetCaseOpenRequest parameters
);
@WebResult(name = "status", targetNamespace = "")
@RequestWrapper(localName = "setCaseTerminate", targetNamespace = "http://www.adeptra.net/Adeptra/", className = "za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseTerminate")
@WebMethod(action = "http://www.adeptra.net/Adeptra/setCaseTerminate")
@ResponseWrapper(localName = "setCaseTerminateResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", className = "za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseTerminateResponse")
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.StatusType setCaseTerminate(
@WebParam(name = "caseId", targetNamespace = "")
java.lang.String caseId,
@WebParam(name = "variableList", targetNamespace = "")
java.util.List<za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.VariableListType> variableList
);
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebResult(name = "setCaseDataResponse", targetNamespace = "http://www.adeptra.net/Adeptra/", partName = "parameters")
@WebMethod(action = "http://www.adeptra.net/Adeptra/setCaseData")
public CaseResponseType setCaseData(
@WebParam(partName = "parameters", name = "setCaseDataRequest", targetNamespace = "http://www.adeptra.net/Adeptra/")
CaseData parameters
);
}
/**
* Please modify this class to meet your needs This class is not complete
*/
package za.co.fnb.cbs.plexus.business.service.ws.adeptra;
import za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.*;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by Apache CXF 2.7.5 2015-06-24T14:58:04.158+02:00
* Generated source version: 2.7.5
*
*/
@javax.jws.WebService(
serviceName = "Adeptra",
portName = "AdeptraSOAP",
targetNamespace = "http://www.adeptra.net/Adeptra/",
endpointInterface = "za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.Adeptra")
public class MockAdeptraImpl implements MockAdeptra {
private static final Logger LOG = Logger.getLogger(MockAdeptraImpl.class.getName());
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#setCaseData(za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseData parameters )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseResponseType setCaseData(CaseData parameters) {
LOG.info("Executing operation setCaseData()");
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseResponseType _return = new CaseResponseType();
CaseResponseType1 caseResponseType1 = new CaseResponseType1();
caseResponseType1.setCaseId(parameters.getCase().getCaseInfo().getCaseId());
StatusType statusType = new StatusType();
statusType.setCode("200");
statusType.setMessage("OK");
caseResponseType1.setStatus(statusType);
VariableListType variableListType = new VariableListType();
VariableType variableType = new VariableType();
variableType.setName("TEST");
variableType.setValue("TEST VALUE");
variableListType.setVariable(variableType);
caseResponseType1.setVariableList(variableListType);
_return.setCaseDataResponse(caseResponseType1);
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#setCaseDelete(za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseDeleteRequest parameters )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseDeleteResponse setCaseDelete(SetCaseDeleteRequest parameters) {
LOG.info("Executing operation setCaseDelete");
System.out.println(parameters);
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseDeleteResponse _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#getCaseData(za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetCaseDataRequest parameters )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseData getCaseData(GetCaseDataRequest parameters) {
LOG.info("Executing operation getCaseData");
System.out.println(parameters);
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.CaseData _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#setCaseResult(za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseResultRequest parameters )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseResultResponse setCaseResult(SetCaseResultRequest parameters) {
LOG.info("Executing operation setCaseResult");
System.out.println(parameters);
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseResultResponse _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#getCaseStatus(za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetCaseStatusRequest parameters )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetCaseStatusResponse getCaseStatus(GetCaseStatusRequest parameters) {
LOG.info("Executing operation getCaseStatus");
System.out.println(parameters);
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetCaseStatusResponse _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#getNextCaseIdList(za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetNextCaseIdListRequest parameters )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetNextCaseIdListResponse getNextCaseIdList(GetNextCaseIdListRequest parameters) {
LOG.info("Executing operation getNextCaseIdList");
System.out.println(parameters);
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetNextCaseIdListResponse _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#getVersion(za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetVersionRequest parameters )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetVersionResponse getVersion(GetVersionRequest parameters) {
LOG.info("Executing operation getVersion");
System.out.println(parameters);
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.GetVersionResponse _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#setCaseOpen(za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseOpenRequest parameters )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseOpenResponse setCaseOpen(SetCaseOpenRequest parameters) {
LOG.info("Executing operation setCaseOpen");
System.out.println(parameters);
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.SetCaseOpenResponse _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/* (non-Javadoc)
* @see za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.MockAdeptra#setCaseTerminate(java.lang.String caseId ,)java.util.List<za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.VariableListType> variableList )*
*/
public za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.StatusType setCaseTerminate(java.lang.String caseId, java.util.List<za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.VariableListType> variableList) {
LOG.info("Executing operation setCaseTerminate");
System.out.println(caseId);
System.out.println(variableList);
try {
za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts.StatusType _return = null;
return _return;
} catch (java.lang.Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
}
Use the cxf-codegen-plugin with the -impl argument to create mock implementations of the web service from the wsdl. Once a build is done remove the -impl argument and copy the generated "impl" classes to your test package
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.7.5</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>
${basedir}/src/main/resources/wsdl/adeptra.wsdl
</wsdl>
<extraargs>
<extraarg>-impl</extraarg>
<extraarg>-p</extraarg>
<extraarg>za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.7.5</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>
${basedir}/src/main/resources/wsdl/adeptra.wsdl
</wsdl>
<extraargs>
<extraarg>-p</extraarg>
<extraarg>za.co.fnb.cbs.plexus.business.service.ws.adeptra.artefacts</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
@pro-wiz
Copy link

pro-wiz commented Mar 9, 2017

great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment