7 Replies Latest reply on May 1, 2013 2:00 AM by Claus Ibsen

    CamelBlueprintTestSupport injecting wrong route from time to time

    Milan Milas Newbie

      I have spent whole day on Camel Testing and still I dont have clue what am I doing wrong. If I have only one blueprint xml file with route it always works fine.

      If there are 2-3 other blueprint xml files it will pick some other route?????

      XML files are not the same, Camelcontext has different name, file names are different,

      route names are different.

      This test sometimes pick route from other file instead from Route-RestletRequest.xml

       

      import static org.junit.Assert.*;

       

      import org.apache.camel.EndpointInject;

      import org.apache.camel.Exchange;

      import org.apache.camel.Produce;

      import org.apache.camel.ProducerTemplate;

      import org.apache.camel.builder.AdviceWithRouteBuilder;

      import org.apache.camel.builder.RouteBuilder;

      import org.apache.camel.component.mock.MockEndpoint;

      import org.apache.camel.model.RouteDefinition;

      import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;

      import org.junit.Ignore;

      import org.junit.Test;

      import org.slf4j.Logger;

      import org.slf4j.LoggerFactory;

       

      import java.io.Console;

      import java.util.List;

      import java.util.Properties;

       

      public class RequestXmlTest extends CamelBlueprintTestSupport {

            

          @Override

          protected String getBlueprintDescriptor() {

                

              return "/OSGI-INF/blueprint/Route-RestletRequest.xml";

          }

       

       

           // Expected message bodies

           protected Object[] expectedBodies = {

                     "expectedBody1",

                     "expectedBody2" };

           // Templates to send to input endpoints

           @Produce(uri = "restlet:http://localhost:8887/Interface/CreateRequest?restletMethod=POST")

           protected ProducerTemplate inputEndpoint;

       

           @Test

           //@Ignore

           public void testCamelRoute() throws Exception {

                //Mock  Endpoints

                 

                getMockEndpoint("mock:queue").expectedMessageCount(2);

                 

                Logger LOG = LoggerFactory.getLogger(String.class);

                 

                LOG.info( "ROOOUTEEEEEE:" +context.getRouteDefinitions().get(0));

                 

                context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {

                   @Override

                   public void configure() throws Exception {

                         

                       interceptSendToEndpoint("activemq:queue:Inbound*")

                                          .skipSendToOriginalEndpoint().to("mock:queue");

                   }

               });

                 

                context.start();

                 

                for (Object expectedBody : expectedBodies) {

                     inputEndpoint.sendBody(expectedBody);

                }

       

                // Validate our expectations

                assertMockEndpointsSatisfied();

           }

          }

      -


      Route-RestletRequest.xml

       

       

              <log message="The message contains $"/>         <inOnly uri="activemq:queue:Inbound?jmsMessageType=Text"/>         <process ref="SendRequestReponse"/>         <log message="Response body $"/>

              <log message="Response headers $"/>

          </route>

      </camelContext>

       

      -


      Route-Transform.xml

       

       

       

      Edited by: milanmilas on Apr 18, 2013 5:09 PM

        • 1. Re: CamelBlueprintTestSupport injecting wrong route from time to time
          Milan Milas Newbie

          context.getRouteDefinitions().get(0).adviceWith......

           

          context.start();

           

          then set expectations:     

          getMockEndpoint("mock:queue").expectedMessageCount(2);

           

          as well as :

              @Override

               public boolean isUseAdviceWith() {

                    // TODO Auto-generated method stub

                    return true;

               }

           

          JUnit is not workint - Run

          JUnit is working - Debug

          Maven test is working

          for now....... 6-7 runs

          • 2. Re: CamelBlueprintTestSupport injecting wrong route from time to time
            Claus Ibsen Master

            Do you have 2+ .xml files in the OSGI-INF/blueprint directory?

             

            You should not do that for unit testing, as the blueprint container will load all *.xml files in that directory. Not sure if we can tell it to filter out the unwanted files.

             

            If you have the XML files in src/test/resources, then you have control of which xml file to load for testing.

             

            Edited by: davsclaus on Apr 19, 2013 6:03 AM

            • 3. Re: CamelBlueprintTestSupport injecting wrong route from time to time
              Milan Milas Newbie

              Hi,

               

              You are correct (have multiple routes) as it does not matter what do I specify in:

              @Override

                   protected String getBlueprintDescriptor() {

               

              it loads different route from time to time.

               

              Is there a work around this?

               

              Milan

              • 5. Re: CamelBlueprintTestSupport injecting wrong route from time to time
                Milan Milas Newbie

                Thanks,

                 

                When I find time I will try to add additional code in SetUp() to

                remove all caml routes from blueprint folder except the one that is tested and in

                TearDown() to return all routes. I think this should work.

                 

                Milan

                • 6. Re: CamelBlueprintTestSupport injecting wrong route from time to time
                  Milan Milas Newbie

                  Base test for all tests, it leaves only root needed for testing and move all other files, when test is finished it returns back all other files:

                   

                  How to use it:

                  public class RouteTransformCreateRequestTest extends BlueprintBaseTest {

                        

                       @Override

                       protected String GetCamelRoute() {     

                            return "OSGI-INF/blueprint/CreateRequest.xml";

                       }

                   

                   

                  -


                  package com.al;

                   

                  import java.io.File;

                  import java.io.InputStream;

                  import java.io.OutputStream;

                   

                  import org.apache.camel.Exchange;

                  import org.apache.camel.Message;

                  import org.apache.camel.Produce;

                  import org.apache.camel.ProducerTemplate;

                  import org.apache.camel.ServiceStatus;

                  import org.apache.camel.builder.AdviceWithRouteBuilder;

                  import org.apache.camel.test.blueprint.CamelBlueprintTestSupport;

                  import org.junit.Test;

                   

                   

                  public class BlueprintBaseTest extends CamelBlueprintTestSupport {

                   

                       private java.net.URL URL;

                        

                       private String TempFolderPath;

                       private String OriginalFolderPath;

                        

                       protected  String GetCamelRoute(){

                            return "";

                       }

                        

                       @Override

                       protected String getBlueprintDescriptor() {     

                             

                     ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

                          if (contextClassLoader != null) {

                               URL = contextClassLoader.getResource(GetCamelRoute());

                          }

                   

                         File file = new File(URL.getPath());

                            String absolutePath = file.getAbsolutePath();

                            String filePath = absolutePath.

                                substring(0,absolutePath.lastIndexOf(File.separator));

                            

                           File folder = new File(filePath.replace("%20", " "));

                           OriginalFolderPath = folder.getAbsolutePath();

                            

                           TempFolderPath = OriginalFolderPath + "
                  test";

                            

                           File tempfolder = new File(folder.getAbsolutePath()+"
                  test");

                           if (!tempfolder.exists())

                           {

                             tempfolder.mkdir(); 

                           }

                           

                           for (File fileEntry : folder.listFiles()) {

                                if(fileEntry.isFile() && !fileEntry.getName().equals(file.getName()) )){

                                     fileEntry.renameTo(new File(TempFolderPath,fileEntry.getName()));

                                }

                           }

                            

                            return GetCamelRoute();

                       }

                        

                       @Override

                       public void tearDown() throws Exception {

                            // TODO Auto-generated method stub

                            super.tearDown();

                             

                             File folder = new File(TempFolderPath);

                   

                                for (File fileEntry : folder.listFiles()) {

                                          fileEntry.renameTo(new File(OriginalFolderPath,fileEntry.getName()));

                                }

                                 

                       }

                        

                      @Override

                       public boolean isUseAdviceWith() {

                            // TODO Auto-generated method stub

                            return true;

                       }

                   

                  }

                  • 7. Re: CamelBlueprintTestSupport injecting wrong route from time to time
                    Claus Ibsen Master

                    Hi

                     

                    Thanks for sharing your solution. I logged a ticket to not forget and look into a future solution for this

                    http://fusesource.com/issues/browse/MR-717