All Downloads are FREE. Search and download functionalities are using the official Maven repository.

testutil.W3CWsaUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2004, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package testutil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.Iterator;

import javax.xml.namespace.QName;
import jakarta.xml.soap.MessageFactory;
import jakarta.xml.soap.SOAPConstants;
import jakarta.xml.soap.SOAPMessage;
import jakarta.xml.soap.SOAPFault;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import jakarta.xml.ws.Dispatch;
import jakarta.xml.ws.Response;

import com.sun.xml.ws.addressing.W3CAddressingConstants;
import com.sun.xml.ws.addressing.v200408.MemberSubmissionAddressingConstants;
import com.sun.xml.ws.api.addressing.AddressingVersion;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

public class W3CWsaUtils {
    public static final String UUID = "uuid:" + java.util.UUID.randomUUID();
    public static final String W3C_WSA_NS = W3CAddressingConstants.WSA_NAMESPACE_NAME;
    public static final String MS_WSA_NS = MemberSubmissionAddressingConstants.WSA_NAMESPACE_NAME;
    public static final String S11_NS = SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE;
    public static final String S12_NS = SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE;

    private static final Source makeStreamSource(String msg) {
        byte[] bytes = msg.getBytes();
        ByteArrayInputStream sinputStream = new ByteArrayInputStream(bytes);
        return new StreamSource(sinputStream);
    }

    private static final SOAPMessage getSOAPMessage(Source msg) throws Exception {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();
        message.getSOAPPart().setContent(msg);
        message.saveChanges();
        return message;
    }

    private static final SOAPMessage getSOAP12Message(Source msg) throws Exception {
        MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        SOAPMessage message = factory.createMessage();
        message.getSOAPPart().setContent(msg);
        message.saveChanges();
        return message;
    }

    public static String fileToXMLString(String filename) {
        return sourceToXMLString(new StreamSource(new File(filename)));
    }

    public static String sourceToXMLString(Source result) {
        String xmlResult = null;
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            OutputStream out = new ByteArrayOutputStream();
            StreamResult streamResult = new StreamResult();
            streamResult.setOutputStream(out);
            transformer.transform(result, streamResult);
            xmlResult = streamResult.getOutputStream().toString();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return xmlResult;
    }

    public static final SOAPMessage invoke(Dispatch dispatch, String request, String ... args) throws Exception {
        String fRequest = String.format(request, args);

        return dispatch.invoke(getSOAPMessage(makeStreamSource(fRequest)));
    }

    public static final Response invokeAsync(Dispatch dispatch, String request, String ... args) throws Exception {
        String fRequest = String.format(request, args);

        return dispatch.invokeAsync(getSOAPMessage(makeStreamSource(fRequest)));
    }

    public static final void invokeOneWay(Dispatch dispatch, String request, String ... args) throws Exception {
        String fRequest = String.format(request, args);

        dispatch.invokeOneWay(getSOAPMessage(makeStreamSource(fRequest)));
    }

    public static final SOAPMessage invoke12(Dispatch dispatch, String request, String ... args) throws Exception {
        String fRequest = String.format(request, args);

        return dispatch.invoke(getSOAP12Message(makeStreamSource(fRequest)));
    }

    public static final void invokeOneWay12(Dispatch dispatch, String request, String ... args) throws Exception {
        String fRequest = String.format(request, args);

        dispatch.invokeOneWay(getSOAP12Message(makeStreamSource(fRequest)));
    }
    public static final String BAD_ACTION_MESSAGE = "\n" +
            "\n" +
            "%s\n" +
            "" + UUID + "\n" +
            "\n" +
            "  %s\n" +
            "\n" +
            "badSOAPAction\n" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String MISSING_ACTION_MESSAGE = "\n" +
            "\n" +
            "%s\n" +
            "" + UUID + "\n" +
            "\n" +
            "  %s\n" +
            "\n" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String REPLY_TO_REFPS_MESSAGE = "\n" +
            "\n" +
            "%s\n" +
            "" + UUID + "\n" +
            "\n" +
            "  %s\n" +
            "  \n" +
            "    Key#123456789\n" +
            "  " +
            "\n" +
            "%s\n" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String FAULT_TO_REFPS_MESSAGE = "\n" +
            "\n" +
            "%s\n" +
            "" + UUID + "\n" +
            "\n" +
            "  %s\n" +
            "  \n" +
            "    Key#123456789\n" +
            "  " +
            "\n" +
            "\n" +
            "  %s\n" +
            "  \n" +
            "    Fault#123456789\n" +
            "  " +
            "\n" +
            "%s\n" +
            "\n" +
            "\n" +
            "\n" +
            "  -10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String DUPLICATE_TO_MESSAGE = "\n" +
            "\n" +
            "%s\n" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String DUPLICATE_REPLY_TO_MESSAGE = "\n" +
            "\n" +
            "%s" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String DUPLICATE_FAULT_TO_MESSAGE = "\n" +
            "\n" +
            "%s" +
            "%s" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String DUPLICATE_ACTION_MESSAGE = "\n" +
            "\n" +
            "%s" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String DUPLICATE_MESSAGE_ID_MESSAGE = "\n" +
            "\n" +
            "" + UUID + "" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String DUPLICATE_MESSAGE_ID_MESSAGE_ONEWAY = "\n" +
            "\n" +
            "" + UUID + "" +
            "" + UUID + "" +
            "\n" +
            "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    private static final String ADD_NUMBERS_HEADER = "\n" +
            "\n" +
            "%s\n" +
            "" + UUID + "\n" +
            "\n" +
            "  %s\n" +
            "\n" +
            "%s\n" +
            "\n";

    private static final String ADD_NUMBERS_PAYLOAD = "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    private static final String ADD_NUMBERS2_PAYLOAD = "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    private static final String ADD_NUMBERS3_PAYLOAD = "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    private static final String ADD_NUMBERS4_PAYLOAD = "\n" +
            "\n" +
            "  10\n" +
            "  10\n" +
            "\n" +
            "";

    public static final String ACTION_DISPATCH_MESSAGE1 = ADD_NUMBERS_HEADER + ADD_NUMBERS_PAYLOAD;
    public static final String ACTION_DISPATCH_MESSAGE2 = ADD_NUMBERS_HEADER + ADD_NUMBERS2_PAYLOAD;
    public static final String ACTION_DISPATCH_MESSAGE3 = ADD_NUMBERS_HEADER + ADD_NUMBERS3_PAYLOAD;
    public static final String ACTION_DISPATCH_MESSAGE4 = ADD_NUMBERS_HEADER + ADD_NUMBERS4_PAYLOAD;

    public static final void assertInvalidHeaderFaultCode(QName got, AddressingVersion av) {
        try {
            TestCase.assertEquals(av.invalidCardinalityTag, got);
        } catch (AssertionFailedError e) {
            TestCase.assertEquals(av.invalidMapTag, got);
        }
    }

    public static final void assertHeaderRequiredFaultCode(QName got) {
        TestCase.assertEquals(W3CAddressingConstants.MAP_REQUIRED_QNAME, got);
    }

    public static final void assertHeaderRequiredFaultCode12(SOAPFault f) {
        TestCase.assertNotNull("Fault element is null", f);
        QName faultcode = f.getFaultCodeAsQName();
        TestCase.assertEquals(SOAPConstants.SOAP_SENDER_FAULT, faultcode);
        Iterator iter = f.getFaultSubcodes();
        TestCase.assertNotNull("Subcode iterator is null", iter);
        TestCase.assertTrue("Subcode iterator has no elements", iter.hasNext());
        TestCase.assertEquals(iter.next(), W3CAddressingConstants.MAP_REQUIRED_QNAME);
//        TestCase.assertTrue("No subsubcode on the fault", iter.hasNext());
//        TestCase.assertEquals(iter.next(), W3CAddressingConstants.INVALID_CARDINALITY);
    }

    public static final void assertInvalidCardinalityCode12(SOAPFault f, AddressingVersion av) {
        TestCase.assertNotNull("Fault element is null", f);
        QName faultcode = f.getFaultCodeAsQName();
        TestCase.assertEquals(SOAPConstants.SOAP_SENDER_FAULT, faultcode);
        Iterator iter = f.getFaultSubcodes();
        TestCase.assertNotNull("Subcode iterator is null", iter);
        TestCase.assertTrue("Subcode iterator has no elements", iter.hasNext());
        TestCase.assertEquals(iter.next(), av.invalidMapTag);
        TestCase.assertTrue("No subsubcode on the fault", iter.hasNext());
        TestCase.assertEquals(iter.next(), av.invalidCardinalityTag);
    }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy