Search code examples
unit-testingsoapcxfinterceptor

Apache CXF transform xml to SoapMessage for Unit-Tests


I want to create a small Unit-Test for an AbstractSoapInterceptor like this:

public class SimpleInterceptorTest {

    private SimpleInterceptor simpleInterceptor;

    @BeforeMethod
    public void setUp() {
        simpleInterceptor = new SimpleInterceptor();
    }

    @Test
    public void testHandleMessage() throws Exception {
        SoapMessage soapMessage = createSoapMessage("requests/sampleRequest.xml");
        simpleInterceptor.handleMessage(soapMessage);

        // Assertions
    }
}

requests/sampleRequest.xml is a Soap-Request like:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <soap:Header>
        <wsse:Security>
            <wsc:SecurityContextToken xmlns:wsc="http://schemas.xmlsoap.org/ws/2005/02/sc" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="sct">
                <wsc:Identifier>abcd</wsc:Identifier>
            </wsc:SecurityContextToken>
        </wsse:Security>
    </soap:Header>
    <soap:Body>
        <wst:RequestSecurityToken xmlns:wst="http://schemas.xmlsoap.org/ws/2005/02/trust" >
            <wst:TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</wst:TokenType>
            <wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Validate</wst:RequestType>
            <wst:ValidateTarget>
                <wsse:SecurityTokenReference>
                    <wsse:Reference URI="#sct"/>
                </wsse:SecurityTokenReference>
            </wst:ValidateTarget>
        </wst:RequestSecurityToken>
    </soap:Body>
</soap:Envelope>

Now I have the following questions:

  1. Is this the right way to test Interceptors?

  2. If yes, how to implement the method createSoapMessage?


Solution

  • Here is an example how Unit-Tests for Apaches interceptors look like:

    https://github.com/apache/cxf/blob/master/rt/wsdl/src/test/java/org/apache/cxf/wsdl/interceptors/DocLiteralInInterceptorTest.java

    Therefore it is possible to implement the createSoapMessage method, but not as simple as wished. I would be very grateful for easier solutions.

    For now, I test the interceptors with SoapUI.