Search code examples
springjunitapache-camelcxfrs

Camel CXF Junit Testing failing with 404


I am trying to Unit test my camel route and i am getting a 404 from test code after a successful invocation of route, meaning am not able to read response from test, always throws 404 not found

Here is my test code

    final Exchange send = template.send("cxfrs://http://localhost:9001/v1/core/handshake", new Processor() {
        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.OutOnly);
            Message inMessage = exchange.getIn();
            //  setupDestinationURL(inMessage);

            final String uuid = UUID.randomUUID().toString().replace("-", "");
            System.out.println("uuid = " + uuid);

            final GenerateTestHeaders headerGenerator = new GenerateTestHeaders();
            final Map<String, Object> outboundHeaderMap = headerGenerator.getOutboundHeaderMap(API_KEY, ACCESS_ID, PRIVATE_ACCESS_KEY, "utf-8", "POST", "2016-08-31T10:40:55.979-0400", uuid);

            // set a customer header
            inMessage.setHeaders(outboundHeaderMap);

            // using the http central client API
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE);

            inMessage.setHeader("HOST", "localhost:9001");

            // set the Http method
            inMessage.setHeader(Exchange.HTTP_METHOD, "POST");

            // set the operation name
            inMessage.setHeader(CxfConstants.OPERATION_NAME, "handshake");

            inMessage.setHeader(Exchange.ACCEPT_CONTENT_TYPE, "application/json");

            // set the relative path
            // inMessage.setHeader(Exchange.HTTP_PATH, "/IMP/v1/core/handshake");

            // Specify the response class , cxfrs will use InputStream as the response object type
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, HandshakeResponse.class);

            // since we use the Get method, so we don't need to set the message body
            inMessage.setBody(null);


        }
    });

My Route is defines as below

<cxf:rsServer id="coreEndPoint" address="http://localhost:9001/v1/core" staticSubresourceResolution="true"
              serviceClass="com.incomm.imp.neo.core.incoming.Framework"
              loggingFeatureEnabled="true" loggingSizeLimit="20">
    <cxf:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
        </bean>
    </cxf:providers>
    <cxf:inInterceptors>
        <ref bean="loggingInInterceptor"></ref>
    </cxf:inInterceptors>
    <cxf:outInterceptors>

        <ref bean="loggingOutInterceptor"></ref>
    </cxf:outInterceptors>
     <cxf:features >
        <ref bean="swagger2Feature"></ref>
    </cxf:features>
</cxf:rsServer>

So my route gets invoked, The logging inteceptor logs a 200 Success with payload however when producer template is returned it has 404 Exception.

Any idea what i am doing wrong?.


Solution

  • On further debuggin realised that it has to be something with the way the Jetty Server is handled internally.So did a cross comparison with the Camel Apache Samples. Modified it and played around a little bit, long story short main difference is in POM

    The reason for failure was the Dependencies in POM i suppose.

     <!-- http client tests -->
     <dependency>
         <groupId>org.apache.camel</groupId>
         <artifactId>camel-http</artifactId>
         <scope>test</scope>
         <version>${camel-version}</version>
     </dependency>
    
     <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jetty</artifactId>
        <version>${camel-version}</version>
     <!--   use the same version as your Camel core version-->
    </dependency>
    
     <!--  Test Dependencies -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core-xml</artifactId>
        <scope>test</scope>
        <version>${camel-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-test</artifactId>
        <version>${camel-version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-test-spring</artifactId>
        <version>${camel-version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-ws-rm</artifactId>
        <version>${cxf-version}</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <scope>test</scope>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymockclassextension</artifactId>
        <scope>test</scope>
        <version>3.2</version>
    </dependency>
    

    I added these and it started working. I have to see which one does that magic, For now am good.