'How to add SOAPHeader to SaajSoapMessage in JUNIT

Trying to pass junit test for SOAP Webservicemessage with headers

public void doWithMessage(final WebServiceMessage message) throws IOException, TransformerException {
    final SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
    final OktaTokenRequest request = createOktaRequest();
    final String token = service.getAccessToken(request);
    addSecurityHeader(saajSoapMessage, token);
}


public void addSecurityHeader(final SaajSoapMessage ssm, final String tokenValue) {
    try {
        final SOAPHeader header = ssm.getSaajMessage().getSOAPHeader();
        final SOAPElement security = header.addChildElement("Security", "wsse",
                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        final SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
        usernameToken.addAttribute(new QName("http://www.w3.org/2000/xmlns/", "wsu", "xmlns"),
                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        final SOAPElement username = usernameToken.addChildElement("Username", "wsse");
        username.addTextNode(tokenValue);
    } catch (SOAPException exp) {
        throw new SoapHeaderException("Failed to add user token profile security" + exp);
    }
}

Here is the test case method

@Test
void test() throws IOException, TransformerException, SOAPException {
    OktaTokenRequest oktaRequest = createOktaRequest();
    final HttpEntity<MultiValueMap<String, String>> httpEntity = prepareHttpEntity(oktaRequest);

    TokenData tokenData = new TokenData();
    tokenData.setAccessToken("token");
    ResponseEntity<TokenData> response = new ResponseEntity<TokenData>(tokenData, HttpStatus.OK);

    Mockito.when(restTemplate.exchange(oktaRequest.getIssuerUri(), HttpMethod.POST, httpEntity, TokenData.class))
            .thenReturn(response);

    Mockito.when(service.getAccessToken(Mockito.any(OktaTokenRequest.class)))
            .thenReturn(response.getBody().getAccessToken());

    MimeHeaders mimeHeaders = new MimeHeaders();
    String testSoapAction = "fooAction";
    mimeHeaders.setHeader("SOAPHeader", testSoapAction);
    String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"></soapenv:Envelope>";
    SOAPMessage message = MessageFactory.newInstance().createMessage(mimeHeaders,
            new ByteArrayInputStream(soap.getBytes()));
    SaajSoapMessage soapMessage = new SaajSoapMessage(message);
    
    soapMessage.getSaajMessage().getMimeHeaders().getHeader(TransportConstants.HEADER_SOAP_ACTION);
    
    webSerivceMessageCallback.doWithMessage(soapMessage);
    verify(oktaTokenService, times(1)).getAccessToken(Mockito.any(OktaTokenRequest.class));
}

SOAPHeder getting null via test case. It is failing at line final SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); because of heder getting null.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source