/** * Copyright (c) 2014-2024 Linagora * * This program/library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or (at your * option) any later version. * * This program/library 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 program/library; If not, see http://www.gnu.org/licenses/ * for the GNU Lesser General Public License version 2.1. */ package org.ow2.petals.binding.soap; import static org.junit.Assert.assertNotNull; import java.io.File; import java.net.URISyntaxException; import java.net.URL; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.namespace.QName; import org.apache.mina.util.AvailablePortFinder; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.rules.RuleChain; import org.junit.rules.TestRule; import org.ow2.petals.binding.soap.junit.SimpleServiceImpl; import org.ow2.petals.component.framework.junit.Component; import org.ow2.petals.component.framework.junit.helpers.SimpleComponent; import org.ow2.petals.component.framework.junit.impl.ConsumesServiceConfiguration; import org.ow2.petals.component.framework.junit.impl.ProvidesServiceConfiguration; import org.ow2.petals.component.framework.junit.impl.ServiceConfiguration; import org.ow2.petals.component.framework.junit.rule.ComponentUnderTest; import org.ow2.petals.component.framework.junit.rule.ServiceConfigurationFactory; import org.ow2.petals.junit.rules.cxfserver.CxfServer; import org.ow2.petals.junit.rules.log.handler.InMemoryLogHandler; import com.ebmwebsourcing.easycommons.lang.UncheckedException; /** * Abstract class for unit tests about request processing * * @author Christophe DENEUX - Linagora * */ public abstract class AbstractComponentTest { protected static final int STANDARD_HTTP_PORT = CxfServer.DEFAULT_HTTP_PORT; protected static final int HTTP_LISTENING_PORT = AvailablePortFinder.getNextAvailable(STANDARD_HTTP_PORT); public static final int BC_SOAP_LISTENING_PORT = AvailablePortFinder .getNextAvailable(SoapConstants.HttpServer.DEFAULT_HTTP_PORT); /* All stuff about service 'Attachment' */ protected static final String SERVICE_NAMESPACE_ATTACHMENT = "http://petals.ow2.org/bc/soap/attachments"; protected static final QName SVC_ITF_NAME_ATTACHMENT = new QName(SERVICE_NAMESPACE_ATTACHMENT, "Attachment"); protected static final String SVC_LOCAL_NAME_ATTACHMENT = "AttachmentService"; protected static final QName SVC_NAME_ATTACHMENT = new QName(SERVICE_NAMESPACE_ATTACHMENT, SVC_LOCAL_NAME_ATTACHMENT); protected static final String SVC_ENDPOINT_ATTACHMENT = "Attachment"; protected static final QName TEST_OPERATION_ATTACHMENT_ECHO = new QName(SERVICE_NAMESPACE_ATTACHMENT, "echo"); /* All stuff about service 'Simple' */ protected static final String SERVICE_NAMESPACE_SIMPLE = "http://petals.ow2.org/bc/soap/simple"; public static final QName SVC_ITF_NAME_SIMPLE = new QName(SERVICE_NAMESPACE_SIMPLE, "Simple"); public static final String SVC_LOCAL_NAME_SIMPLE = "SimpleService"; public static final QName SVC_NAME_SIMPLE = new QName(SERVICE_NAMESPACE_SIMPLE, SVC_LOCAL_NAME_SIMPLE); public static final String SVC_ENDPOINT_SIMPLE = "Simple"; protected static final long SVC_SIMPLE_TIMEOUT = 10000; public static final QName TEST_OPERATION_SIMPLE_ECHO = new QName(SERVICE_NAMESPACE_SIMPLE, "echo"); public static final QName TEST_OPERATION_SIMPLE_PRINT = new QName(SERVICE_NAMESPACE_SIMPLE, "print"); public static CxfServer CXFSERVER = new CxfServer(HTTP_LISTENING_PORT).withService(new SimpleServiceImpl(), SVC_LOCAL_NAME_SIMPLE); protected static final String VALID_SU_CONSUME_POP3 = "valid-su-consume-pop3"; protected static final String ATTACHMENT_SU_CONSUME = "attachment-su-consume"; protected static final String ATTACHMENT_SU_PROVIDE = "attachment-su-provide"; public static final String SIMPLE_SU_CONSUME = "simple-su-consume"; protected static final String WSS_SIMPLE_SU_CONSUME = "wss-simple-su-consume"; public static final String SIMPLE_SU_PROVIDE = "simple-su-provide"; protected static final String INTEGRATION_SVC_CFG = "native"; protected static final QName CONSUMED_OPERATION = new QName("http://testProvideService", "consumedOperation"); protected static final InMemoryLogHandler IN_MEMORY_LOG_HANDLER = new InMemoryLogHandler(); protected static final Component COMPONENT_UNDER_TEST = new ComponentUnderTest() .addLogHandler(IN_MEMORY_LOG_HANDLER.getHandler()) .setParameter(new QName(SoapConstants.Component.NS_URI, SoapConstants.HttpServer.HTTP_HOSTNAME), "localhost") .setParameter(new QName(SoapConstants.Component.NS_URI, SoapConstants.HttpServer.HTTP_PORT), String.valueOf(BC_SOAP_LISTENING_PORT)) .addFile(AbstractComponentTest.addresingMarFileName(), "install") .addFile(AbstractComponentTest.rampartMarFileName(), "install") .registerServiceToDeploy(ATTACHMENT_SU_PROVIDE, new ServiceConfigurationFactory() { @Override public ServiceConfiguration create() { final URL wsdlUrl = Thread.currentThread().getContextClassLoader() .getResource("wsdl/attachmentService.wsdl"); assertNotNull(wsdlUrl); final ProvidesServiceConfiguration serviceConfiguration = new ProvidesServiceConfiguration( SVC_ITF_NAME_ATTACHMENT, SVC_NAME_ATTACHMENT, SVC_ENDPOINT_ATTACHMENT, wsdlUrl); serviceConfiguration.setParameter( new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.ADDRESS), AbstractComponentTest.CXFSERVER.getHttpBaseUrl() + "/" + SVC_LOCAL_NAME_ATTACHMENT); serviceConfiguration.setParameter( new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.SOAP_VERSION), SoapConstants.SOAP.SOAP_VERSION_11); return serviceConfiguration; } }).registerServiceToDeploy(ATTACHMENT_SU_CONSUME, new ServiceConfigurationFactory() { @Override public ServiceConfiguration create() { final ConsumesServiceConfiguration serviceConfiguration = new ConsumesServiceConfiguration( SVC_ITF_NAME_ATTACHMENT, SVC_NAME_ATTACHMENT, SVC_ENDPOINT_ATTACHMENT); serviceConfiguration.setParameter( new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.ADDRESS), SVC_LOCAL_NAME_ATTACHMENT); return serviceConfiguration; } }).registerServiceToDeploy(SIMPLE_SU_PROVIDE, new ServiceConfigurationFactory() { @Override public ServiceConfiguration create() { return createSimpleServiceProviderConfiguration(); } }).registerServiceToDeploy(SIMPLE_SU_CONSUME, new ServiceConfigurationFactory() { @Override public ServiceConfiguration create() { return createSimpleServiceConsumerConfiguration(); } }).registerServiceToDeploy(WSS_SIMPLE_SU_CONSUME, new ServiceConfigurationFactory() { @Override public ServiceConfiguration create() { final ConsumesServiceConfiguration serviceConfiguration = new ConsumesServiceConfiguration( SVC_ITF_NAME_SIMPLE, SVC_NAME_SIMPLE, SVC_ENDPOINT_SIMPLE); serviceConfiguration.setTimeout(SVC_SIMPLE_TIMEOUT); serviceConfiguration.setParameter( new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.ADDRESS), "Wss" + SVC_LOCAL_NAME_SIMPLE); serviceConfiguration.setParameter( new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.MODULES), "rampart"); serviceConfiguration.setParameter( new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.WSS_POLICY_FILE), "wss-policy-ut-soap11.xml"); final URL wssPolicyURI = Thread.currentThread().getContextClassLoader() .getResource("wss-policy-ut-soap11.xml"); assertNotNull("Policy file not found: 'wss-policy-ut-soap11.xml'"); serviceConfiguration.addResource(wssPolicyURI); return serviceConfiguration; } }); public static ProvidesServiceConfiguration createSimpleServiceProviderConfiguration() { final URL wsdlUrl = Thread.currentThread().getContextClassLoader().getResource("wsdl/simpleService.wsdl"); assertNotNull(wsdlUrl); final ProvidesServiceConfiguration serviceConfiguration = new ProvidesServiceConfiguration(SVC_ITF_NAME_SIMPLE, SVC_NAME_SIMPLE, SVC_ENDPOINT_SIMPLE, wsdlUrl); serviceConfiguration.setParameter(new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.ADDRESS), AbstractComponentTest.CXFSERVER.getHttpBaseUrl() + "/" + SVC_LOCAL_NAME_SIMPLE); serviceConfiguration.setParameter( new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.SOAP_VERSION), SoapConstants.SOAP.SOAP_VERSION_11); return serviceConfiguration; } public static ConsumesServiceConfiguration createSimpleServiceConsumerConfiguration() { final ConsumesServiceConfiguration serviceConfiguration = new ConsumesServiceConfiguration(SVC_ITF_NAME_SIMPLE, SVC_NAME_SIMPLE, SVC_ENDPOINT_SIMPLE); serviceConfiguration.setTimeout(SVC_SIMPLE_TIMEOUT); serviceConfiguration.setParameter(new QName(SoapConstants.Component.NS_URI, SoapConstants.ServiceUnit.ADDRESS), SVC_LOCAL_NAME_SIMPLE); return serviceConfiguration; } public static final String addresingMarFileName() { return getMarFileName("addressing-1.8.2.mar"); } private static final String rampartMarFileName() { return getMarFileName("rampart-1.7.1.mar"); } private static final String getMarFileName(final String marFileName) { final URL marUrl = Thread.currentThread().getContextClassLoader().getResource(marFileName); assertNotNull(String.format("MAR file '%s' not found in resources", marFileName), marUrl); try { return new File(marUrl.toURI()).getAbsolutePath(); } catch (final URISyntaxException e) { throw new RuntimeException(e); } } public static Marshaller MARSHALLER; public static Unmarshaller UNMARSHALLER; static { try { final JAXBContext context = JAXBContext.newInstance( org.ow2.petals.bc.soap.attachments.ObjectFactory.class, org.ow2.petals.bc.soap.simple.ObjectFactory.class); UNMARSHALLER = context.createUnmarshaller(); MARSHALLER = context.createMarshaller(); MARSHALLER.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); } catch (final JAXBException e) { throw new UncheckedException(e); } } /** * Avoid the error 'java.lang.ClassCastException: * org.ow2.petals.bc.soap.attachments.EchoResponse$JaxbAccessorF_attachment cannot be cast to * com.sun.xml.internal.bind.v2.runtime.reflect.Accessor' when creating the WS client stub. I don't know why. TODO: * To remove when the cause will be fixed. Remove also {@link #restoreHackAvoidingClassCastExceptionOnWSCltStub()} */ @BeforeClass public static void avoidClassCastExceptionOnWSCltStub() { System.setProperty("com.sun.xml.internal.bind.v2.bytecode.ClassTailor.noOptimize", "true"); } @AfterClass public static void restoreHackAvoidingClassCastExceptionOnWSCltStub() { System.getProperties().remove("com.sun.xml.internal.bind.v2.bytecode.ClassTailor.noOptimize"); } @ClassRule public static final TestRule chain = RuleChain.outerRule(IN_MEMORY_LOG_HANDLER).around(CXFSERVER) .around(COMPONENT_UNDER_TEST); protected static final SimpleComponent COMPONENT = new SimpleComponent(COMPONENT_UNDER_TEST); /** * All log traces must be cleared before starting a unit test */ @Before public void clearLogTraces() { IN_MEMORY_LOG_HANDLER.clear(); } }