You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by wt...@apache.org on 2009/09/06 19:16:23 UTC

svn commit: r811839 - in /camel/trunk/components/camel-cxf/src/test: java/org/apache/camel/component/cxf/soap/headers/ resources/ resources/org/apache/camel/component/cxf/soap/headers/

Author: wtam
Date: Sun Sep  6 17:16:23 2009
New Revision: 811839

URL: http://svn.apache.org/viewvc?rev=811839&view=rev
Log:
[CAMEL-1983] port unit test from 1.x

Added:
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterWithInsertionImpl.java   (with props)
Modified:
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest.java
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterImpl.java
    camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest-context.xml
    camel/trunk/components/camel-cxf/src/test/resources/soap_header.wsdl

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest.java?rev=811839&r1=811838&r2=811839&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest.java (original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest.java Sun Sep  6 17:16:23 2009
@@ -17,6 +17,7 @@
 
 package org.apache.camel.component.cxf.soap.headers;
 
+import java.io.StringReader;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Proxy;
 import java.util.ArrayList;
@@ -37,6 +38,7 @@
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
+import org.apache.camel.Processor;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.component.cxf.CxfConstants;
 import org.apache.camel.component.cxf.CxfEndpoint;
@@ -46,7 +48,9 @@
 import org.apache.cxf.binding.soap.SoapHeader;
 import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.headers.Header;
+import org.apache.cxf.headers.Header.Direction;
 import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.jaxb.JAXBDataBinding;
 import org.apache.cxf.message.MessageContentsList;
 import org.apache.cxf.outofband.header.OutofBandHeader;
@@ -78,13 +82,17 @@
 
     private Endpoint relayEndpoint;
     private Endpoint noRelayEndpoint;
-    
+    private Endpoint relayEndpointWithInsertion;
+
     @Before
     public void setUp() throws Exception {        
         template = context.createProducerTemplate();
 
         relayEndpoint = Endpoint.publish("http://localhost:9091/HeaderService/", new HeaderTesterImpl());
         noRelayEndpoint = Endpoint.publish("http://localhost:7070/HeaderService/", new HeaderTesterImpl(false));
+        relayEndpointWithInsertion = Endpoint.publish("http://localhost:5091/HeaderService/", 
+                                                      new HeaderTesterWithInsertionImpl());
+
     }
 
     @After
@@ -99,6 +107,11 @@
             noRelayEndpoint.stop();
             noRelayEndpoint = null;
         }
+        
+        if (relayEndpointWithInsertion != null) {
+            relayEndpointWithInsertion.stop();
+            relayEndpointWithInsertion = null;
+        }
     }
     
     protected static void addOutOfBoundHeader(HeaderTester proxy, boolean invalid) throws JAXBException {
@@ -190,6 +203,32 @@
                    response.getFirstName().equals("pass"));
         validateReturnedOutOfBandHeader(proxy);
     }
+    
+    
+    @Test
+    public void testInoutOutOfBandHeaderCXFClientRelayWithHeaderInsertion() throws Exception {
+        HeaderService s = new HeaderService(getClass().getClassLoader().getResource("soap_header.wsdl"),
+                                            HeaderService.SERVICE);
+        HeaderTester proxy = s.getSoapPortRelayWithInsertion();
+        addOutOfBoundHeader(proxy, false);
+        Me me = new Me();
+        me.setFirstName("john");
+        me.setLastName("Doh");
+        Me response = proxy.inoutOutOfBandHeader(me);
+        assertTrue("Expected the out of band header to propagate but it didn't", 
+                   response.getFirstName().equals("pass"));
+        
+        InvocationHandler handler  = Proxy.getInvocationHandler(proxy);
+        BindingProvider  bp = null;
+        if (!(handler instanceof BindingProvider)) {
+            fail("Unable to cast dynamic proxy InocationHandler to BindingProvider type");
+        }
+
+        bp = (BindingProvider)handler;
+        Map<String, Object> responseContext = bp.getResponseContext();
+        validateReturnedOutOfBandHeaderWithInsertion(responseContext, true);
+    }
+    
 
     @Test
     public void testOutOutOfBandHeaderCXFClientRelay() throws Exception {
@@ -517,4 +556,102 @@
         headers.add(hdr);
         return headers;
     }
+    
+    protected static void validateReturnedOutOfBandHeaderWithInsertion(Map<String, Object> responseContext, boolean expect) {
+        List<OutofBandHeader> hdrToTest = new ArrayList<OutofBandHeader>();
+        List oobHdr = (List)responseContext.get(Header.HEADER_LIST);
+        if (!expect) {
+            if (oobHdr == null || (oobHdr != null && oobHdr.size() == 0)) {
+                return;
+            }
+            fail("Should have got *no* out-of-band headers, but some were found");
+        }
+        if (oobHdr == null) {
+            fail("Should have got List of out-of-band headers");
+        }
+
+        assertTrue("HeaderHolder list expected to conain 2 object received " + oobHdr.size(),
+                   oobHdr.size() == 2);
+        
+        int i = 0;
+        if (oobHdr != null & oobHdr instanceof List) {
+            Iterator iter = oobHdr.iterator();
+            while (iter.hasNext()) {
+                Object hdr = iter.next();
+                if (hdr instanceof Header) {
+                    Header hdr1 = (Header)hdr;
+                    if (hdr1.getObject() instanceof Node) {
+                        try {
+                            JAXBElement job = (JAXBElement)JAXBContext
+                                .newInstance(org.apache.cxf.outofband.header.ObjectFactory.class)
+                                .createUnmarshaller().unmarshal((Node)hdr1.getObject());
+                            hdrToTest.add((OutofBandHeader)job.getValue());
+                        } catch (JAXBException ex) {
+                            ex.printStackTrace();
+                        }
+                    }
+                }
+            }
+        }
+
+        assertTrue("out-of-band header should not be null", hdrToTest.size() > 0);
+        assertTrue("Expected out-of-band Header name testOobReturnHeaderName recevied :"
+                   + hdrToTest.get(0).getName(), "testOobReturnHeaderName".equals(hdrToTest.get(0).getName()));
+        assertTrue("Expected out-of-band Header value testOobReturnHeaderValue recevied :"
+                   + hdrToTest.get(0).getValue(), "testOobReturnHeaderValue".equals(hdrToTest.get(0).getValue()));
+        assertTrue("Expected out-of-band Header attribute testReturnHdrAttribute recevied :"
+                   + hdrToTest.get(0).getHdrAttribute(), "testReturnHdrAttribute"
+            .equals(hdrToTest.get(0).getHdrAttribute()));
+        
+        assertTrue("Expected out-of-band Header name New_testOobHeader recevied :"
+                   + hdrToTest.get(1).getName(), "New_testOobHeader".equals(hdrToTest.get(1).getName()));
+        assertTrue("Expected out-of-band Header value New_testOobHeaderValue recevied :"
+                   + hdrToTest.get(1).getValue(), "New_testOobHeaderValue".equals(hdrToTest.get(1).getValue()));
+        assertTrue("Expected out-of-band Header attribute testHdrAttribute recevied :"
+                   + hdrToTest.get(1).getHdrAttribute(), "testHdrAttribute"
+            .equals(hdrToTest.get(1).getHdrAttribute()));
+    }
+
+    public static class InsertRequestOutHeaderProcessor implements Processor {
+
+        public void process(Exchange exchange) throws Exception {
+            List<SoapHeader> soapHeaders = (List)exchange.getIn().getHeader(Header.HEADER_LIST);
+   
+            // Insert a new header
+            String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><outofbandHeader "
+                + "xmlns=\"http://cxf.apache.org/outofband/Header\" hdrAttribute=\"testHdrAttribute\" "
+                + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:mustUnderstand=\"1\">"
+                + "<name>New_testOobHeader</name><value>New_testOobHeaderValue</value></outofbandHeader>";
+            
+            SoapHeader newHeader = new SoapHeader(soapHeaders.get(0).getName(),
+                                                  DOMUtils.readXml(new StringReader(xml)).getDocumentElement());
+            // make sure direction is IN since it is a request message.
+            newHeader.setDirection(Direction.DIRECTION_IN);
+            //newHeader.setMustUnderstand(false);
+            soapHeaders.add(newHeader);
+            
+        }
+        
+    }
+    
+    public static class InsertResponseOutHeaderProcessor implements Processor {
+
+        public void process(Exchange exchange) throws Exception {
+            List<SoapHeader> soapHeaders = (List)exchange.getIn().getHeader(Header.HEADER_LIST);
+
+            // Insert a new header
+            String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><outofbandHeader "
+                + "xmlns=\"http://cxf.apache.org/outofband/Header\" hdrAttribute=\"testHdrAttribute\" "
+                + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:mustUnderstand=\"1\">"
+                + "<name>New_testOobHeader</name><value>New_testOobHeaderValue</value></outofbandHeader>";
+            SoapHeader newHeader = new SoapHeader(soapHeaders.get(0).getName(),
+                           DOMUtils.readXml(new StringReader(xml)).getDocumentElement());
+            // make sure direction is OUT since it is a response message.
+            newHeader.setDirection(Direction.DIRECTION_OUT);
+            //newHeader.setMustUnderstand(false);
+            soapHeaders.add(newHeader);
+            
+        }
+    }
+
 }

Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterImpl.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterImpl.java?rev=811839&r1=811838&r2=811839&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterImpl.java (original)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterImpl.java Sun Sep  6 17:16:23 2009
@@ -50,8 +50,8 @@
 public class HeaderTesterImpl implements HeaderTester {
     
     @Resource
-    private WebServiceContext context;
-    private boolean relayHeaders = true;
+    protected WebServiceContext context;
+    protected boolean relayHeaders = true;
     
     public HeaderTesterImpl() {
     }
@@ -183,7 +183,7 @@
         }
     }
     
-    private boolean validateOutOfBandHander() {
+    protected boolean validateOutOfBandHander() {
         MessageContext ctx = context == null ? null : context.getMessageContext();
         if (!relayHeaders) {
             if (ctx != null 

Added: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterWithInsertionImpl.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterWithInsertionImpl.java?rev=811839&view=auto
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterWithInsertionImpl.java (added)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterWithInsertionImpl.java Sun Sep  6 17:16:23 2009
@@ -0,0 +1,93 @@
+/**
+ * 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.camel.component.cxf.soap.headers;
+
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.ws.handler.MessageContext;
+
+import org.w3c.dom.Node;
+
+import org.apache.cxf.headers.Header;
+import org.apache.cxf.outofband.header.OutofBandHeader;
+
+public class HeaderTesterWithInsertionImpl extends HeaderTesterImpl {
+    
+    @Override
+    protected boolean validateOutOfBandHander() {
+        MessageContext ctx = context == null ? null : context.getMessageContext();
+        if (!relayHeaders) {
+            if (ctx != null 
+                && !ctx.containsKey(Header.HEADER_LIST)
+                || (ctx.containsKey(Header.HEADER_LIST) 
+                    && ((List)ctx.get(Header.HEADER_LIST)).size() == 0)) {
+                return true;
+            }
+            return false;
+        }
+        
+        boolean success = false;
+        if (ctx != null && ctx.containsKey(Header.HEADER_LIST)) {
+            List oobHdr = (List) ctx.get(Header.HEADER_LIST);
+            if (oobHdr.size() != 2) {
+                throw new RuntimeException("test failed expected 2 soap headers but found " + oobHdr.size());
+            }
+            verifyHeader(oobHdr.get(0), "testOobHeader", "testOobHeaderValue");
+            verifyHeader(oobHdr.get(1), "New_testOobHeader", "New_testOobHeaderValue");
+            oobHdr.clear();
+            success = true;
+        } else {
+            throw new RuntimeException("MessageContext is null or doesnot contain OOBHeaders");
+        }
+        
+        return success;
+    }
+
+    private void verifyHeader(Object hdr, String headerName, String headerValue) {
+        if (hdr instanceof Header && ((Header) hdr).getObject() instanceof Node) {
+            Header hdr1 = (Header) hdr;
+            try {
+                JAXBElement job = 
+                    (JAXBElement)JAXBContext.newInstance(org.apache.cxf.outofband.header.ObjectFactory.class)
+                        .createUnmarshaller()
+                        .unmarshal((Node) hdr1.getObject());
+                OutofBandHeader ob = (OutofBandHeader) job.getValue();
+                if (!headerName.equals(ob.getName())) {
+                    throw new RuntimeException("test failed expected name ' + headerName + ' but found '"
+                                               + ob.getName() + "'");
+                }
+                
+                if (!headerValue.equals(ob.getValue())) {
+                    throw new RuntimeException("test failed expected name ' + headerValue + ' but found '"
+                                               + ob.getValue() + "'");
+                }
+            } catch (JAXBException ex) {
+                throw new RuntimeException("test failed", ex);
+            }
+        } else {
+            throw new RuntimeException("test failed. Unexpected type " + hdr.getClass());
+        }
+        
+    }        
+    
+    
+
+}

Propchange: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterWithInsertionImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/soap/headers/HeaderTesterWithInsertionImpl.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest-context.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest-context.xml?rev=811839&r1=811838&r2=811839&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest-context.xml (original)
+++ camel/trunk/components/camel-cxf/src/test/resources/org/apache/camel/component/cxf/soap/headers/CxfMessageHeadersRelayTest-context.xml Sun Sep  6 17:16:23 2009
@@ -35,6 +35,21 @@
                    xmlns:tns="http://apache.org/camel/component/cxf/soap/headers">                
   </cxf:cxfEndpoint> 
 
+  <cxf:cxfEndpoint id="routerRelayEndpointWithInsertion" 
+                   address="http://localhost:5000/HeaderService/"
+                   serviceClass="org.apache.camel.component.cxf.soap.headers.HeaderTester"
+                   endpointName="tns:SoapPortRelayWithInsertion"
+                   serviceName="tns:HeaderService"
+                   wsdlURL="soap_header.wsdl"
+                   xmlns:tns="http://apache.org/camel/component/cxf/soap/headers">
+        <cxf:inInterceptors>
+		    <ref bean="logInbound"/>
+		</cxf:inInterceptors>	
+		<cxf:outInterceptors>
+		    <ref bean="loggingOutInterceptor"/>
+		</cxf:outInterceptors>	
+  </cxf:cxfEndpoint> 
+  
   <cxf:cxfEndpoint id="routerNoRelayEndpoint" 
                    address="http://localhost:7000/HeaderService"
                    serviceClass="org.apache.camel.component.cxf.soap.headers.HeaderTester"
@@ -56,6 +71,21 @@
                    wsdlURL="soap_header.wsdl"
                    xmlns:tns="http://apache.org/camel/component/cxf/soap/headers">
   </cxf:cxfEndpoint>
+  
+  <cxf:cxfEndpoint id="serviceRelayEndpointWithInsertion" 
+                   address="http://localhost:5091/HeaderService/"
+                   serviceClass="org.apache.camel.component.cxf.soap.headers.HeaderTester"
+                   endpointName="tns:SoapPortRelayWithInsertion"
+                   serviceName="tns:HeaderService"
+                   wsdlURL="soap_header.wsdl"
+                   xmlns:tns="http://apache.org/camel/component/cxf/soap/headers">
+        <cxf:inInterceptors>
+		    <ref bean="logInbound"/>
+		</cxf:inInterceptors>	
+        <cxf:outInterceptors>
+		    <ref bean="loggingOutInterceptor"/>
+		</cxf:outInterceptors>	
+  </cxf:cxfEndpoint>  
 
   <cxf:cxfEndpoint id="serviceNoRelayEndpoint" 
                    address="http://localhost:7070/HeaderService/"
@@ -109,6 +139,12 @@
           <to uri="cxf:bean:serviceRelayEndpoint"/>
       </route>
       <route>
+          <from uri="cxf:bean:routerRelayEndpointWithInsertion"/>
+          <process ref="InsertRequestOutHeaderProcessor" />
+          <to uri="cxf:bean:serviceRelayEndpointWithInsertion"/>
+          <process ref="InsertResponseOutHeaderProcessor" />
+      </route>          
+      <route>
          <from uri="direct:directProducer" />
          <to uri="cxf:bean:serviceRelayEndpoint" />
       </route>
@@ -129,4 +165,9 @@
           <to uri="mock:result"/>
       </route>
     </camelContext>
+    
+    <bean id="InsertRequestOutHeaderProcessor" class="org.apache.camel.component.cxf.soap.headers.CxfMessageHeadersRelayTest$InsertRequestOutHeaderProcessor" />
+    <bean id="InsertResponseOutHeaderProcessor" class="org.apache.camel.component.cxf.soap.headers.CxfMessageHeadersRelayTest$InsertResponseOutHeaderProcessor" />
+    <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />         
+    <bean id="logInbound" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
 </beans>

Modified: camel/trunk/components/camel-cxf/src/test/resources/soap_header.wsdl
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/resources/soap_header.wsdl?rev=811839&r1=811838&r2=811839&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/resources/soap_header.wsdl (original)
+++ camel/trunk/components/camel-cxf/src/test/resources/soap_header.wsdl Sun Sep  6 17:16:23 2009
@@ -278,6 +278,10 @@
         </port>        
         <port binding="tns:headerTesterSOAPBinding" name="SoapPortCustomRelay">
             <soap:address location="http://localhost:6000/HeaderService/"/>
-        </port>        
+        </port>      
+        <port binding="tns:headerTesterSOAPBinding" name="SoapPortRelayWithInsertion">
+            <soap:address location="http://localhost:5000/HeaderService/"/>
+        </port>  
     </service>
 </definitions>
+