Sure! Here is the class file as well as the txt version again.

Thanks,
Mary Cochran

On Fri, Mar 18, 2016 at 3:30 PM, Andrew Block <[email protected]> wrote:

> Mary,
>
> Would you be able to validate the attachment of the test class file?
>
> Thanks,
> Andy
>
> --
> Andrew Block
>
> On March 18, 2016 at 1:23:19 PM, Mary Cochran ([email protected]) wrote:
>
> Can someone point me to why I am getting a NPE on context.start() ?  Its
> line 63 of the attached. I have verified before that line that context is
> not null with my print statements.  The test is for the route here:
>
> https://github.com/rhtconsulting/fuse-quickstarts/blob/jboss-fuse-6.2.1/karaf/rest_dsl_basic_auth/src/main/java/com/redhat/consulting/fusequickstarts/karaf/rest/auth/basic/dsl/route/RestDslRoute.java
>
> thanks,
> Mary Cochran
>
package com.redhat.consulting.fusequickstarts.karaf.rest.auth.basic.dsl.route;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.redhat.consulting.fusequickstarts.karaf.rest.auth.basic.dsl.model.Note;

public class RestDslRouteTest extends CamelTestSupport {

    /**
     * Set Routes for Testing
     * @throws Exception 
     */
    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
    	return new RestDslRoute();
    }
    
    @Override
    protected JndiRegistry createRegistry() throws Exception{
    	JndiRegistry jr = new JndiRegistry(createJndiContext());
    	ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
    	BasicAuthenticator basicAuthenticator=new BasicAuthenticator();
    	securityHandler.setAuthenticator(basicAuthenticator);
    	securityHandler.addConstraintMapping(new ConstraintMapping());
    	securityHandler.setStrict(false);
    	jr.bind("securityHandler", securityHandler);
    	return jr;
    }
    
    @Override
    public boolean isUseAdviceWith() {
        return true;
    }
    
    @Before
    public void mockEndpoints() throws Exception {
        //do nothing
     }

    /**
     * Test GET Direct Route
     * @throws Exception 
     */
    @Test
    public void testDirectGet() throws Exception {
    	System.out.println("Context is: " + context);
    	System.out.println("Route definitions: " + context.getRouteDefinitions());
    	System.out.println("Is setup routes: "+context.isSetupRoutes());
    	System.out.println("Context is: " + context);
    	System.out.println("Context name: " + context.getName() + ", context endpoints: " + context.getEndpoints() + ", context routes: " + context.getRoutes()
    			+ "context registry: " + context.getRegistry());
    	context.start();
        // Send Test Message to
        Exchange responseExchange = template.send("direct:get", new Processor() {
            public void process(Exchange exchange) {
            	System.out.println("Setting headers");
                Message in = exchange.getIn();
                in.setHeader("authUsername", "admin");
                in.setHeader("authPassword", "admin");
                in.setBody("");
            }
        });
        System.out.println("GET response exchange: " + responseExchange);
        // Test Response
        Assert.assertNotNull("Response was Null", responseExchange);

        Message responseMessage = responseExchange.getIn();
        System.out.println("GET response message: " + responseMessage);
        Assert.assertNotNull("Response Message was Null", responseMessage);

        Note responseNote = responseMessage.getBody(Note.class);
        Assert.assertNotNull("Note was Null", responseNote);

        Assert.assertEquals("To was Incorrect", "User", responseNote.getTo());
        Assert.assertEquals("From was Incorrect", "Developer", responseNote.getFrom());
        Assert.assertEquals("Message was Incorrect", "REST is Awesome", responseNote.getMessage());
        context.stop();
    }

    /**
     * Test Post Direct Route
     * @throws Exception 
     */
    @Test
    public void testDirectPost() throws Exception {

    	createCamelContext().start();
        final Note sampleMessage = new Note();
        sampleMessage.setFrom("Developer");
        sampleMessage.setTo("User");
        sampleMessage.setMessage("REST is Awesome");

        // Send Test Message to
        Exchange responseExchange = template.send("direct:post", new Processor() {
            public void process(Exchange exchange) {
                Message in = exchange.getIn();
                in.setHeader("authUsername", "admin");
                in.setHeader("authPassword", "admin");
                in.setBody(sampleMessage);
            }
        });

        // Test Response
        Assert.assertNotNull("Response was Null", responseExchange);

        Message responseMessage = responseExchange.getIn();
        Assert.assertNotNull("Response Message was Null", responseMessage);
        
        Assert.assertEquals("Incoorect Response Code", "201", responseMessage.getHeader(Exchange.HTTP_RESPONSE_CODE));
    }

}

Reply via email to