Ravi
Ravi

Reputation: 1287

Camel endpoint mocking nullpointer exception

Iam trying to mock switchyard endpoint switchyard://TohpGetAccountReports using the mock endpoint in camel. the mockAccountReportEndpoint defined in junit test is returning null and the test id failing with nullpointer exception.can you please tell me what is wrong with my code?

Camel route :

    <route id="accountReportRoute" streamCache="true">
        <from uri="direct:accountReport" />
        <doTry>
            <setProperty propertyName="hpResponse">
                <constant>A</constant>
            </setProperty>
            <to
                uri="xslt:xslt/account_reports_manager/create_get_accountreport_request.xslt" />

            <log message="GetAccountReports Request : ${body}" logName="esb.hp.InModelProvider.AccountReport"
                loggingLevel="TRACE" />
            <to
                id="AccountReportEndpoint" uri="switchyard://TohpGetAccountReports?operationName=GetAccountReports"  />

            <log message="GetAccountReports Response : ${body}" logName="esb.hp.InModelProvider.AccountReport"
                loggingLevel="TRACE" />
            <to
                uri="xslt:xslt/account_reports_manager/create_get_accountreport_response.xslt" />

            <doCatch>
                <exception>java.lang.Exception</exception>
                <handled>
                    <constant>false</constant>
                </handled>
                <log
                    message="Exception in hp Model provider accountReportRoute ${exception.stacktrace} "
                    logName="esb.hp.InModelProvider.AccountReport" loggingLevel="ERROR" />
            </doCatch>
        </doTry>
        <log message="Workaround for handling account report soap fault"
            logName="esb.hp.InModelProvider.AccountReport" loggingLevel="DEBUG" />
    </route>

junit test class

package com.company.esb.hp.in.modelprovider.test;        
import java.util.Map;        
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.model.ModelCamelContext;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.switchyard.ServiceDomain;
import org.switchyard.component.test.mixins.cdi.CDIMixIn;
import org.switchyard.component.test.mixins.http.HTTPMixIn;
import org.switchyard.test.BeforeDeploy;
import org.switchyard.test.Invoker;
import org.switchyard.test.ServiceOperation;
import org.switchyard.test.SwitchYardRunner;
import org.switchyard.test.SwitchYardTestCaseConfig;
import org.switchyard.test.SwitchYardTestKit;
import org.switchyard.transform.config.model.TransfohpwitchYardScanner;

import com.company.esb.common.test.esbSwitchyardTest;
import com.company.esb.hp.in.modelprovider.test.util.RiskInProviderUtil;

@SwitchYardTestCaseConfig(config = SwitchYardTestCaseConfig.SWITCHYARD_XML, mixins = {
        CDIMixIn.class, HTTPMixIn.class }, scanners = { TransfohpwitchYardScanner.class })
@RunWith(SwitchYardRunner.class)
public class hpInModelProviderTest extends esbSwitchyardTest {
    private static final Logger LOG = Logger
            .getLogger(hpInModelProviderTest.class);
    private SwitchYardTestKit testKit;

    public static final String getAcctResult = "TohpGetAccountReports";

    @EndpointInject(uri = "mock://switchyard:TohpGetAccountReports*")
    protected MockEndpoint mockAccountReportEndpoint;



    @BeforeDeploy
    public void setProperties() {
        System.setProperty("esb.hp.inmodelprovider.url", "http://pusslasrws1451.company.biz");

    }

    @Before
    public void installServices() {
        simulateesbServices(testKit);
    }

    @Test
    public void testAccountReportDirectRouteSuccess() throws Exception {
        String request = readFile("xml/endtoend/success/GetJobsResults/get_job_status-response.xml");
        ServiceDomain domain = testKit.getServiceDomain();
        //testKit.removeService(getAcctResult);
        //testKit.registerInOutService(getAcctResult,
            //  new GetAccountReportsServiceMockExchangeHandler());
        CamelContext ctx = (CamelContext) domain
                .getProperty("CamelContextProperty");
        Exchange ex = new DefaultExchange(ctx);
        ModelCamelContext modelContext = (ModelCamelContext) ctx;
        modelContext.getRouteDefinition("accountReportRoute").adviceWith(modelContext, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {

                weaveById("AccountReportEndpoint").replace().to(mockAccountReportEndpoint);
            }
        });
        mockAccountReportEndpoint.expectedMessageCount(1);
        mockAccountReportEndpoint.returnReplyBody(new Expression() {
            @Override
            public <T> T evaluate(Exchange exchange, Class<T> type) {
                return exchange.getContext().getTypeConverter().convertTo(type, RiskInProviderUtil.readFile("xml/endtoend/success/GetJobsResults/get_account_report_service-response.xml"));
            }
        });

        // MockEndpoint endpoint = getMockEndpoint("mock:switchyard:service"); 
        ex.setProperty("sToken", "f3669d3a-5a2b-4b99-a8df-3aabfd4aae58");
        ex.setProperty("sAccId", "196497");
        ex.getIn().setBody(request);
        ProducerTemplate producer = ctx.createProducerTemplate();
        producer.send("direct:accountReport", ex);
        System.out.println(ex.getIn().getBody(String.class));
    }

    @Override
    protected void addSetupProperties(Map<String, String> map) {

    }

}

Upvotes: 0

Views: 2592

Answers (1)

mgyongyosi
mgyongyosi

Reputation: 2677

..after you have done the adviceWith you must start CamelContext by invoke the start method on the context instance.

http://camel.apache.org/advicewith.html#AdviceWith-UsingisUseAdviceWith()

Try to override the isUseAdviceWith() method (should return true) and start the context after you have done the adviceWith. If it has been already started then try to stop() it and start() it again (cold restart).

Upvotes: 1

Related Questions