You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by contactreji <co...@gmail.com> on 2015/11/30 18:02:33 UTC

Camel - Spock test cases

Hi friends

I am trying to write few test cases using spock. My Aim here is to start the
routes in a class which extends RouteBuilder class and it has few camel
routes in the configure() method.

I use groovy dsl and gradle as build tool.

My spock test cases look like follows :- 
*
package unittest.groovy

import com.groovy.routes.VMMBrandIntegrationRoute
import org.apache.camel.impl.DefaultCamelContext
import spock.lang.Specification
import java.net.*;

/**
 * Created by Z001NLH on 11/30/2015.
 */
class SpockTestCases extends Specification{

   // URL url =
VMMBrandIntegrationRoute.class.getClassLoader().getResource("appConfigs.groovy")
    //Available blocks in SPOCK - setup(given), when, then, expect, cleanup,
and where

           def "Firewall Rules"()
        {
            def portAvailable
            Socket socket = new Socket()
            when:
            socket.connect new InetSocketAddress("stgibp.target.com", 443);
            portAvailable = socket.isConnected();
            then:
            portAvailable

            cleanup:
            socket.close()

    }

    def "Dummy Test Case"()
    {
        setup:
        def stack = new Stack()
        def elem = "push me"
        when:
        stack.push(elem)
        then:
        !stack.empty
        stack.size() == 1
        stack.peek() == elem

    }

    def "Test Function"()
    {
        setup:
        def camelContext = new DefaultCamelContext()
        when:
        camelContext.addRoutes(VMMBrandIntegrationRoute)
        camelContext.start()
        then:
        camelContext.isStarted()

    }

}
*


However, I get following exception
---------------------------------
*No signature of method:
org.apache.camel.impl.DefaultCamelContext.addRoutes() is applicable for
argument types: (java.lang.Class) values: [class
com.groovy.routes.VMMBrandIntegrationRoute]
Possible solutions: addRoutes(org.apache.camel.RoutesBuilder), getRoutes(),
setRoutes(java.util.List), getRoute(java.lang.String)
groovy.lang.MissingMethodException: No signature of method:
org.apache.camel.impl.DefaultCamelContext.addRoutes() is applicable for
argument types: (java.lang.Class) values: [class
com.groovy.routes.VMMBrandIntegrationRoute]
Possible solutions: addRoutes(org.apache.camel.RoutesBuilder), getRoutes(),
setRoutes(java.util.List), getRoute(java.lang.String)
	at unittest.groovy.SpockTestCases.Test Function(SpockTestCases.groovy:50)*

I understand that camelContext.addRoutes() takes RoutesBuilder as argument.
Is there a way I can load routes from the groovy class below which extends
RouteBuilder class. Following class contains my camel routes.

*
public class VMMBrandIntegrationRoute extends RouteBuilder{
//Use Case taken from VMM Brand Integration
    URL url =
VMMBrandIntegrationRoute.class.getClassLoader().getResource("appConfigs.groovy")
    def config = new ConfigSlurper().parse(url)

    void configure() throws Exception {

        final Namespaces ns = new Namespaces("ns",
"http://target.com/2012/03/01/canonical/BusinessPartner")

       
from("timer://BRANDJOB_Scheduler?fixedRate=true&period="+config.brandJobScheduleInterval+"%amp;delay=0")
                .routeId("BRAND_APIConnectRoute")
               
.setHeader("jobID_BRND",simple('VMMBRANDJOB${date:now:yyyyMMddHHmmss}'))
                .inOnly("seda:setJobStartTimeMetrics_BRND")
                .inOnly("seda:StoreJOBIdInCache_BRND")
                .setProperty("limit",constant(config.brandAPILimit))
               
.setProperty("updatedOffset",constant(config.brandAPIInitialOffset))
                .setHeader("Content-Type",constant("application/xml;
charset=UTF-8"))
                .setHeader("CamelHttpMethod",constant("GET"))
               
.setHeader("CamelHttpQuery",simple(config.brandAPIURLOptions+'&offset=${property.updatedOffset}&limit=${property.limit}'))
                .log('Initial HttpQuery = ${in.header.CamelHttpQuery}')
               
.to("https4://"+config.brandAPIUrl+"?authUsername="+config.brandAPIUserName+"&authPassword="+config.brandAPIPassword+"&disableStreamCache=true")
                .convertBodyTo(java.lang.String.class)
               
.setHeader("CamelFileName",simple('Download_${date:now:yyyyMMddHHmmss}_${date:now:HHmmss}'))
                .inOnly("seda:ProcessVMMRecords_BRND")
               
.setProperty("TotalRecordCount",ns.xpath('ns:brand_list/ns:record_count/text()',Integer.class))
                .log('The total count is ${property.TotalRecordCount}')
                .processRef("CalculateCycles")
                .loop(simple('${property.RemainingCycles}'))
                .processRef("UpdateOffsetValue")
                .log('updated offset = ${property.updatedOffset}')
               
.setHeader("CamelHttpQuery",simple(config.brandAPIURLOptions+'&offset=${property.updatedOffset}&limit=${property.limit}'))
                .log('Updated HttpQuery = ${in.header.CamelHttpQuery}')
               
.to("https4://"+config.brandAPIUrl+"?authUsername="+config.brandAPIUserName+"&authPassword="+config.brandAPIPassword+"&disableStreamCache=true")
                .convertBodyTo(java.lang.String.class)
                .inOnly("seda:ProcessVMMRecords_BRND")
                .log("Data download completed....")
                .end()



        from("seda:ProcessVMMRecords_BRND")
                .routeId("BRAND_APIDataSplitter")
                .split()
                .tokenizeXML("brand")
                .streaming()
                .to("seda:FilterRejects")
                .end()


        from("seda:FilterRejects")
                .routeId("BRAND_DataQualifierRoute")
                .choice()

               
.when().xquery("fn:not(fn:exists(//associated_business_partner/business_partner/role/vendor/gmsvendor_number))")
                .setHeader("rejectionCode",constant("GMS Vendor number is
missing"))
                .to("seda:ProcessRejects")

                .when().xquery("fn:not(fn:exists(//brand_name))")
                .setHeader("rejectionCode",constant("Brand Name is
missing"))
                .to("seda:ProcessRejects")

                .when().xquery("fn:not(fn:exists(//brand_id))")
                .setHeader("rejectionCode",constant("Brand ID is missing"))
                .to("seda:ProcessRejects")

                .when().xquery("fn:not(fn:exists(//brand_status_name))")
                .setHeader("rejectionCode",constant("Brand Status is
missing"))
                .to("seda:ProcessRejects")

                .otherwise().
                to("seda:ProcessRecords")
                .end()

        from("seda:ProcessRecords")
                .routeId("BRAND_DataProcessorRoute")
                .to("metrics:counter:BRAND_RECORDSProcessed")
                .to("xquery:xquery/Brand_File1.xq")
                .aggregate(new
AggregateIndividualRecords()).constant(true).completionInterval(config.aggregationStrategyTimeout)
                .beanRef("addOuterXMLElementBrand")
               
.multicast().to("seda:BRAND_FILEFormat1","seda:BRAND_FILEFormat2")
                .end()

        from("seda:ProcessRejects")
                .routeId("BRAND_ProcessRejects")
                .to("metrics:counter:BRAND_RECORDSRejected")
                .to("xquery:xquery/Brand_File1.xq").aggregate(new
AggregateIndividualRecords()).constant(true)
                .completionInterval(config.aggregationStrategyTimeout)
                .beanRef("addOuterXMLElementBrand")
                .to("xslt:xslt/rejectCSV.xsl")
               
.setHeader("FileNamePattern",simple('brand_mandatory_reject_${date:now:yyyyMMddHHmmss}_${date:now:HHmmss}'))
                .to("seda:WriteDLMFile")
                .end()

        from("seda:BRAND_FILEFormat1")
                .routeId("BRAND_ready_attributeList_brand")
                .to("xslt:xslt/BRAND_Format1.xsl")
               
.setHeader("FileNamePattern",simple('ready_attributeList_brand_${date:now:yyyyMMddHHmmss}_${date:now:HHmmss}'))
                .multicast().to("seda:WriteDLMFile","seda:WriteCTLFile")
                .end()

        from("seda:BRAND_FILEFormat2")
                .routeId("BRAND_ready_attributeValidity_brand")
                .to("xslt:xslt/BRAND_Format2.xsl")
                .processRef("CalculateRecordsCount")
               
.setHeader("FileNamePattern",simple('ready_attributeValidity_brand_${date:now:yyyyMMddHHmmss}_${date:now:HHmmss}'))
                .multicast().to("seda:WriteDLMFile","seda:WriteCTLFile")
                .end()

        from("seda:WriteDLMFile")
                .routeId("BRAND_DLMWriterRoute")
               
.setHeader("CamelFileName",simple('${in.header.FileNamePattern}.gz'))
                .marshal().gzip()
                .to("file:"+config.Brand_outputPath)
                .to("direct:retrieveJobID_BRND")
               
.setHeader("recepientListStringEndTime",simple('metrics:histogram:${in.header.jobID_BRND}.ENDTIME?value=${date:now:yyyyMMddHHmmss}'))
                .recipientList(header("recepientListStringEndTime")
                .tokenize(","))
                .end()

        from("seda:WriteCTLFile")
                .routeId("BRAND_CTLWriterRoute")
                .to("velocity:velocitytemplates/ControlFile.vm")
               
.setHeader("CamelFileName",simple('${in.header.FileNamePattern}.ctl'))
                .to("file:"+config.Brand_outputPath)
                .to("direct:retrieveJobID_BRND")
               
.setHeader("recepientListStringEndTime",simple('metrics:histogram:${in.header.jobID_BRND}.ENDTIME?value=${date:now:yyyyMMddHHmmss}'))
               
.recipientList(header("recepientListStringEndTime").tokenize(","))
                .end()


         from("seda:StoreJOBIdInCache_BRND")
                 .routeId("Brand_CacheJobIDPersistRoute")
                 .setBody(simple("in.header.jobID_BRND"))
                 .setHeader("CamelCacheKey",constant("jobID_BRND"))
                
.setHeader("CamelCacheOperation",constant("CamelCacheUpdate"))
                 .to("cache://JobIDStore")
                 .setHeader("CamelCacheOperation",constant("CamelCacheGet"))
                 .setHeader("CamelCacheKey",constant("jobID_BRND"))
                 .to("cache://JobIDStore")
                 .log('The job ID assigned is ${body}')
                 .end()

         from("direct:retrieveJobID_BRND")
                 .routeId("Brand_CacheJobIDRetrieveRoute")
                 .setHeader("OriginalBody",simple('${body}'))
                 .setHeader("CamelCacheKey",constant("jobID_BRND"))
                 .setHeader("CamelCacheOperation",constant("CamelCacheGet"))
                 .to("cache://JobIDStore")
                 .setHeader("jobID_BRND",simple("body"))
                 .setBody(simple('${in.header.OriginalBody}'))
                 .end()

         from("seda:setJobStartTimeMetrics_BRND")
                 .routeId("Brand_StartTimeMetricsSetterRoute")
                
.setHeader("recepientListStringStartTime",simple('metrics:histogram:${in.header.jobID_BRND}.STARTTIME?value=${date:now:yyyyMMddHHmmss}'))
                 .recipientList(header("recepientListStringStartTime")
                 .tokenize(","))
                 .end()

         from("seda:TrackJobLevelVolumeMetrics_BRND")
                 .routeId("Brand_TrackJobLevelVolumeMetrics")
                
.setHeader("recepientListStringJobLevelVolume",simple('metrics:counter:${in.header.jobID_BRND}.VOLUME'))
                
.recipientList(header("recepientListStringJobLevelVolume").tokenize(","))
                 .end()



    }
}
*






-----
Reji Mathews
Sr. Developer - Middleware Integration / SOA ( Open Source - Apache Camel & Jboss Fuse ESB | Mule ESB )
LinkedIn - http://in.linkedin.com/pub/reji-mathews/31/9a2/40a
Twitter - reji_mathews
--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Spock-test-cases-tp5774570.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel - Spock test cases

Posted by yihtserns <si...@yahoo.com>.
Change `camelContext.addRoutes(*VMMBrandIntegrationRoute*)` to
`camelContext.addRoutes(*new VMMBrandIntegrationRoute()*)`.



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Spock-test-cases-tp5774570p5775391.html
Sent from the Camel - Users mailing list archive at Nabble.com.