alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Axis 2 example source code file (AddressingValidationHandlerTest.java)

This example Axis 2 source code file (AddressingValidationHandlerTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Axis 2 tags/keywords

addressingvalidationhandler, axisfault, axisoperation, axisservice, axisservice, exception, exception, inoutaxisoperation, messagecontext, messagecontext, string, string, testutil, the

The Axis 2 AddressingValidationHandlerTest.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.axis2.handlers.addressing;

import junit.framework.TestCase;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.AddressingConstants;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.InOnlyAxisOperation;
import org.apache.axis2.description.InOutAxisOperation;
import org.apache.axis2.handlers.util.TestUtil;

import javax.xml.namespace.QName;

public class AddressingValidationHandlerTest extends TestCase implements AddressingConstants {
    AddressingInHandler inHandler = new AddressingFinalInHandler();
    AddressingValidationHandler validationHandler = new AddressingValidationHandler();
    String addressingNamespace = AddressingConstants.Final.WSA_NAMESPACE;
    String versionDirectory = "final";
    TestUtil testUtil = new TestUtil();

    protected MessageContext testMessageWithOmittedHeaders(String testName) throws Exception {
        return testAddressingMessage("omitted-header-messages", testName + "Message.xml");
    }

    protected MessageContext testAddressingMessage(String directory, String testName)
            throws Exception {
        String testfile = directory + "/" + versionDirectory + "/" + testName;

        MessageContext mc = new MessageContext();

        StAXSOAPModelBuilder omBuilder = testUtil.getOMBuilder(testfile);
        mc.setEnvelope(omBuilder.getSOAPEnvelope());

        inHandler.invoke(mc);

        return mc;
    }

    public void testMessageWithOmittedMessageIDInOutMEP() throws Exception {
        MessageContext messageContext = testMessageWithOmittedHeaders("noMessageID");
        String messageID = messageContext.getOptions().getMessageId();

        assertNull("The message id is not null.", messageID);

        AxisOperation axisOperation = new InOutAxisOperation();
        messageContext.setAxisOperation(axisOperation);
        AxisService axisService = new AxisService();
        messageContext.setAxisService(axisService);

        try {
            validationHandler.invoke(messageContext);
            fail("An AxisFault should have been thrown due to the absence of a message id.");
        }
        catch (AxisFault af) {
            //Test passed.
        }
    }

    public void testValidateActionFlag() throws Exception {
        MessageContext mc = testAddressingMessage("valid-messages", "soapmessage.xml");

        // Tell validation handler NOT to check action dispatch
        mc.setProperty(AddressingConstants.ADDR_VALIDATE_ACTION, Boolean.FALSE);

        // Even though this message has an action that will not dispatch to an
        // AxisOperation, this shouldn't throw a fault.
        validationHandler.invoke(mc);
    }

    public void testMessageWithOmittedMessageIDInOnlyMEP() throws Exception {
        MessageContext messageContext = testMessageWithOmittedHeaders("noMessageID");
        String messageID = messageContext.getOptions().getMessageId();

        assertNull("The message id is not null.", messageID);

        AxisOperation axisOperation = new InOnlyAxisOperation();
        messageContext.setAxisOperation(axisOperation);
        AxisService axisService = new AxisService();
        messageContext.setAxisService(axisService);
        validationHandler.invoke(messageContext);
    }

    public void testMessageWithMessageIDInOutMEP() throws Exception {
        MessageContext messageContext = testMessageWithOmittedHeaders("noFrom");
        String messageID = messageContext.getOptions().getMessageId();

        assertNotNull("The message id is null.", messageID);

        AxisOperation axisOperation = new InOutAxisOperation();
        messageContext.setAxisOperation(axisOperation);
        AxisService axisService = new AxisService();
        messageContext.setAxisService(axisService);
        validationHandler.invoke(messageContext);
    }

    public void testInOutMessageWithOmittedMessageID() throws Exception {
        MessageContext messageContext = testMessageWithOmittedHeaders("noMessageID");
        String messageID = messageContext.getOptions().getMessageId();

        assertNull("The message id is not null.", messageID);

        AxisOperation axisOperation = new InOutAxisOperation();
        messageContext.setAxisOperation(axisOperation);
        AxisService axisService = new AxisService();
        messageContext.setAxisService(axisService);

        try {
            validationHandler.invoke(messageContext);
        } catch (AxisFault axisFault) {
            // Confirm this is the correct fault
            assertEquals("Wrong fault code",
                         new QName(Final.WSA_NAMESPACE,
                                   Final.FAULT_ADDRESSING_HEADER_REQUIRED),
                         axisFault.getFaultCode());
            return;
        }
        fail("Validated message with missing message ID!");
    }
}

Other Axis 2 examples (source code examples)

Here is a short list of links related to this Axis 2 AddressingValidationHandlerTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.