Search in sources :

Example 41 with IMocksControl

use of org.easymock.IMocksControl in project cxf by apache.

the class SOAPHandlerInterceptorTest method testGetSOAPMessageInBound.

@Test
public void testGetSOAPMessageInBound() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            try {
                smc.getMessage();
            } catch (Exception e) {
                throw new Fault(e);
            }
            return true;
        }

        public boolean handleFault(SOAPMessageContext smc) {
            return true;
        }

        public Set<QName> getHeaders() {
            return null;
        }

        public void close(MessageContext messageContext) {
        }
    });
    HandlerChainInvoker invoker = new HandlerChainInvoker(list);
    IMocksControl control = createNiceControl();
    Binding binding = control.createMock(Binding.class);
    Exchange exchange = control.createMock(Exchange.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
    // This is to set direction to inbound
    expect(exchange.getOutMessage()).andReturn(null);
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    XMLStreamReader reader = preparemXMLStreamReader("resources/greetMeRpcLitReq.xml");
    message.setContent(XMLStreamReader.class, reader);
    control.replay();
    SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
    li.handleMessage(message);
    control.verify();
    // Verify SOAPMessage
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    SOAPBody bodyNew = soapMessageNew.getSOAPBody();
    Iterator<?> itNew = bodyNew.getChildElements();
    SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
    assertEquals("sendReceiveData", bodyElementNew.getLocalName());
    // Verify the XMLStreamReader
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    QName qn = xmlReader.getName();
    assertEquals("sendReceiveData", qn.getLocalPart());
}
Also used : Binding(javax.xml.ws.Binding) Set(java.util.Set) HashSet(java.util.HashSet) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) IOException(java.io.IOException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) Exchange(org.apache.cxf.message.Exchange) SOAPBody(javax.xml.soap.SOAPBody) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) Test(org.junit.Test)

Example 42 with IMocksControl

use of org.easymock.IMocksControl in project cxf by apache.

the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderOutBound.

@Test
public void testChangeSOAPHeaderOutBound() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            try {
                Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                if (outboundProperty.booleanValue()) {
                    // change mustUnderstand to false
                    SOAPMessage message = smc.getMessage();
                    SOAPHeader soapHeader = message.getSOAPHeader();
                    Iterator<?> it = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
                    SOAPHeaderElement headerElementNew = (SOAPHeaderElement) it.next();
                    SoapVersion soapVersion = Soap11.getInstance();
                    Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
                    attr.setValue("false");
                    headerElementNew.setAttributeNodeNS(attr);
                }
            } catch (Exception e) {
                throw new Fault(e);
            }
            return true;
        }

        public boolean handleFault(SOAPMessageContext smc) {
            return true;
        }

        public Set<QName> getHeaders() {
            return null;
        }

        public void close(MessageContext messageContext) {
        }
    });
    HandlerChainInvoker invoker = new HandlerChainInvoker(list);
    IMocksControl control = createNiceControl();
    Binding binding = control.createMock(Binding.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    Exchange exchange = control.createMock(Exchange.class);
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    // This is to set direction to outbound
    expect(exchange.getOutMessage()).andReturn(message).anyTimes();
    CachedStream originalEmptyOs = new CachedStream();
    message.setContent(OutputStream.class, originalEmptyOs);
    InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
    // Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
    chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {

        public void handleMessage(SoapMessage message) throws Fault {
            try {
                XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
                SoapVersion soapVersion = Soap11.getInstance();
                writer.setPrefix("soap", soapVersion.getNamespace());
                writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
                writer.writeNamespace("soap", soapVersion.getNamespace());
                Object[] headerInfo = prepareSOAPHeader();
                StaxUtils.writeElement((Element) headerInfo[1], writer, true, false);
                writer.writeEndElement();
                writer.flush();
            } catch (Exception e) {
            // do nothing
            }
        }
    });
    chain.add(new SOAPHandlerInterceptor(binding));
    message.setInterceptorChain(chain);
    control.replay();
    chain.doIntercept(message);
    control.verify();
    // Verify SOAPMessage header
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    SOAPHeader soapHeader = soapMessageNew.getSOAPHeader();
    Iterator<?> itNew = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
    SOAPHeaderElement headerElementNew = (SOAPHeaderElement) itNew.next();
    SoapVersion soapVersion = Soap11.getInstance();
    assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
    originalEmptyOs.close();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) Attr(org.w3c.dom.Attr) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Iterator(java.util.Iterator) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPHeader(javax.xml.soap.SOAPHeader) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) Binding(javax.xml.ws.Binding) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) QName(javax.xml.namespace.QName) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) IOException(java.io.IOException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Test(org.junit.Test)

Example 43 with IMocksControl

use of org.easymock.IMocksControl in project cxf by apache.

the class SOAPHandlerInterceptorTest method testChangeSOAPBodyOutBound.

// SAAJ tree is created from DOMXMLStreamWriter. Any changes to SOAPMessage should be streamed back to
// outputStream
@Test
public void testChangeSOAPBodyOutBound() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outboundProperty.booleanValue()) {
                try {
                    smc.setMessage(prepareSOAPMessage("resources/greetMeRpcLitRespChanged.xml"));
                } catch (Exception e) {
                    throw new Fault(e);
                }
            }
            return true;
        }

        public boolean handleFault(SOAPMessageContext smc) {
            return true;
        }

        public Set<QName> getHeaders() {
            return null;
        }

        public void close(MessageContext messageContext) {
        }
    });
    HandlerChainInvoker invoker = new HandlerChainInvoker(list);
    IMocksControl control = createNiceControl();
    Binding binding = control.createMock(Binding.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    Exchange exchange = control.createMock(Exchange.class);
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    // This is to set direction to outbound
    expect(exchange.getOutMessage()).andReturn(message).anyTimes();
    CachedStream originalEmptyOs = new CachedStream();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(originalEmptyOs);
    message.setContent(XMLStreamWriter.class, writer);
    InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
    // Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
    chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {

        public void handleMessage(SoapMessage message) throws Fault {
            try {
                XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
                SoapVersion soapVersion = Soap11.getInstance();
                writer.setPrefix("soap", soapVersion.getNamespace());
                writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
                writer.writeNamespace("soap", soapVersion.getNamespace());
                writer.writeEndElement();
                writer.flush();
            } catch (Exception e) {
            // do nothing
            }
        }
    });
    chain.add(new SOAPHandlerInterceptor(binding));
    message.setInterceptorChain(chain);
    control.replay();
    chain.doIntercept(message);
    control.verify();
    writer.flush();
    // Verify SOAPMessage
    SOAPMessage resultedMessage = message.getContent(SOAPMessage.class);
    assertNotNull(resultedMessage);
    SOAPBody bodyNew = resultedMessage.getSOAPBody();
    Iterator<?> itNew = bodyNew.getChildElements(new QName("http://apache.org/hello_world_rpclit", "sendReceiveDataResponse"));
    SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
    Iterator<?> outIt = bodyElementNew.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "out"));
    Element outElement = (SOAPElement) outIt.next();
    assertNotNull(outElement);
    Element elem3Element = DOMUtils.findAllElementsByTagNameNS(outElement, "http://apache.org/hello_world_rpclit/types", "elem3").get(0);
    assertEquals("100", elem3Element.getTextContent());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPElement(javax.xml.soap.SOAPElement) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) Binding(javax.xml.ws.Binding) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) QName(javax.xml.namespace.QName) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) IOException(java.io.IOException) Exchange(org.apache.cxf.message.Exchange) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) SOAPBody(javax.xml.soap.SOAPBody) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Test(org.junit.Test)

Example 44 with IMocksControl

use of org.easymock.IMocksControl in project cxf by apache.

the class JaxWsServiceConfigurationTest method getMockedServiceModel.

private ServiceInfo getMockedServiceModel(String wsdlUrl) throws Exception {
    WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader();
    wsdlReader.setFeature("javax.wsdl.verbose", false);
    Definition def = wsdlReader.readWSDL(new CatalogWSDLLocator(wsdlUrl));
    IMocksControl control = EasyMock.createNiceControl();
    Bus bus = control.createMock(Bus.class);
    BindingFactoryManager bindingFactoryManager = control.createMock(BindingFactoryManager.class);
    DestinationFactoryManager dfm = control.createMock(DestinationFactoryManager.class);
    WSDLServiceBuilder wsdlServiceBuilder = new WSDLServiceBuilder(bus);
    Service service = null;
    for (Iterator<?> it = def.getServices().values().iterator(); it.hasNext(); ) {
        Object obj = it.next();
        if (obj instanceof Service) {
            service = (Service) obj;
            break;
        }
    }
    EasyMock.expect(bus.getExtension(BindingFactoryManager.class)).andReturn(bindingFactoryManager);
    EasyMock.expect(bus.getExtension(DestinationFactoryManager.class)).andStubReturn(dfm);
    control.replay();
    ServiceInfo serviceInfo = wsdlServiceBuilder.buildServices(def, service).get(0);
    serviceInfo.setProperty(WSDLServiceBuilder.WSDL_DEFINITION, null);
    serviceInfo.setProperty(WSDLServiceBuilder.WSDL_SERVICE, null);
    return serviceInfo;
}
Also used : IMocksControl(org.easymock.IMocksControl) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) Bus(org.apache.cxf.Bus) DestinationFactoryManager(org.apache.cxf.transport.DestinationFactoryManager) Definition(javax.wsdl.Definition) WSDLServiceBuilder(org.apache.cxf.wsdl11.WSDLServiceBuilder) WebService(javax.jws.WebService) Service(javax.wsdl.Service) BindingFactoryManager(org.apache.cxf.binding.BindingFactoryManager) CatalogWSDLLocator(org.apache.cxf.wsdl11.CatalogWSDLLocator) WSDLReader(javax.wsdl.xml.WSDLReader)

Example 45 with IMocksControl

use of org.easymock.IMocksControl in project cxf by apache.

the class JaxbAssertionTest method testEqual.

@Test
public void testEqual() {
    JaxbAssertion<FooType> assertion = new JaxbAssertion<FooType>();
    FooType data = new FooType();
    data.setName("CXF");
    data.setNumber(2);
    QName qn = new QName("http://cxf.apache.org/test/assertions/foo", "FooType");
    assertion.setName(qn);
    assertion.setData(data);
    PolicyComponent pc = new Policy();
    assertTrue(!assertion.equal(pc));
    pc = new All();
    assertTrue(!assertion.equal(pc));
    pc = new ExactlyOne();
    assertTrue(!assertion.equal(pc));
    IMocksControl ctrl = EasyMock.createNiceControl();
    PrimitiveAssertion xpa = ctrl.createMock(PrimitiveAssertion.class);
    QName oqn = new QName("http://cxf.apache.org/test/assertions/blah", "OtherType");
    EasyMock.expect(xpa.getName()).andReturn(oqn);
    EasyMock.expect(xpa.getType()).andReturn(Constants.TYPE_ASSERTION);
    ctrl.replay();
    assertTrue(!assertion.equal(xpa));
    ctrl.verify();
    FooType odata = new FooType();
    odata.setName(data.getName());
    odata.setNumber(data.getNumber());
    JaxbAssertion<FooType> oassertion = new JaxbAssertion<FooType>();
    oassertion.setData(odata);
    oassertion.setName(qn);
    assertTrue(!assertion.equal(oassertion));
    oassertion.setData(data);
    assertTrue(assertion.equal(oassertion));
    assertTrue(assertion.equal(assertion));
}
Also used : Policy(org.apache.neethi.Policy) All(org.apache.neethi.All) IMocksControl(org.easymock.IMocksControl) FooType(org.apache.cxf.test.assertions.foo.FooType) PolicyComponent(org.apache.neethi.PolicyComponent) QName(javax.xml.namespace.QName) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) ExactlyOne(org.apache.neethi.ExactlyOne) Test(org.junit.Test)

Aggregations

IMocksControl (org.easymock.IMocksControl)57 Test (org.junit.Test)34 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Message (org.apache.cxf.message.Message)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 MessageImpl (org.apache.cxf.message.MessageImpl)9 QName (javax.xml.namespace.QName)7 BindingFactoryManager (org.apache.cxf.binding.BindingFactoryManager)7 Before (org.junit.Before)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Exchange (org.apache.cxf.message.Exchange)6 FakeHttpServletRequest (com.google.gerrit.util.http.testutil.FakeHttpServletRequest)5 FakeHttpServletResponse (com.google.gerrit.util.http.testutil.FakeHttpServletResponse)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Binding (javax.xml.ws.Binding)5 Handler (javax.xml.ws.handler.Handler)5 MessageContext (javax.xml.ws.handler.MessageContext)5