test.integ.be.fedict.hsm.ws.WSS4JTest.java Source code

Java tutorial

Introduction

Here is the source code for test.integ.be.fedict.hsm.ws.WSS4JTest.java

Source

/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package test.integ.be.fedict.hsm.ws;

import java.net.URL;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * WSS4J integration tests.
 * 
 * @author Frank Cornelis
 * 
 */
@RunWith(Arquillian.class)
@RunAsClient
public class WSS4JTest {

    private static final Log LOG = LogFactory.getLog(WSS4JTest.class);

    @ArquillianResource
    private URL baseURL;

    @Deployment
    public static WebArchive createTestArchive() {
        WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
                .addAsLibraries(Maven.resolver().resolve("commons-io:commons-io:2.4").withTransitivity()
                        .as(JavaArchive.class))
                .addAsLibraries(Maven.resolver().resolve("org.apache.ws.security:wss4j:1.6.9").withTransitivity()
                        .as(JavaArchive.class))
                .addClasses(WSS4JTestServlet.class);
        return war;
    }

    @Test
    public void testInvocation() throws Exception {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(this.baseURL + "test");
        HttpResponse httpResponse = httpClient.execute(httpGet);
        String result = EntityUtils.toString(httpResponse.getEntity());
        LOG.debug("result: " + result);
    }

    @Test
    public void testSecurity() throws Exception {
        QName serviceName = new QName("http://www.example.com/test", "TestService");
        Service service = Service.create(serviceName);
        QName portName = new QName("http://www.example.com/test", "TestPort");
        String endpointAddress = this.baseURL + "test";
        LOG.debug("endpoint address: " + endpointAddress);
        service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
        Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
        BindingProvider bindingProvider = (BindingProvider) dispatch;
        SOAPBinding soapBinding = (SOAPBinding) bindingProvider.getBinding();
        List handlerChain = soapBinding.getHandlerChain();
        handlerChain.add(new WSS4JTestSOAPHandler());
        soapBinding.setHandlerChain(handlerChain);
        MessageFactory messageFactory = soapBinding.getMessageFactory();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPBody soapBody = soapMessage.getSOAPBody();
        QName payloadName = new QName("http://www.example.com/test", "Payload", "prefix");
        SOAPBodyElement soapBodyElement = soapBody.addBodyElement(payloadName);
        soapBodyElement.addTextNode("hello world");

        SOAPMessage replyMessage = dispatch.invoke(soapMessage);
    }
}