Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Add dependencies on Camel-Spring, and the Spring test JAR (which will automatically bring in JUnit 3.8.x) to your POM:
    Code Block
    xml
    xml
    <dependency>
        <artifactId>camel-spring</artifactId>
        <groupId>org.apache.camel</groupId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <artifactId>spring-test</artifactId>
        <groupId>org.springframework</groupId>
        <version>2.5.5</version>
        <scope>test</scope>
    </dependency>
    
  2. Create a new unit test class in src/test/java/your-package-here, perhaps called XMLInputTest.java
  3. Make the test extend Spring's AbstractJUnit38SpringContextTests class, so it can load a Spring context for the test
  4. Create a Spring context configuration file in src/test/resources, perhaps called XMLInputTest-context.xml
  5. In the unit test class, use the class-level @ContextConfiguration annotation to indicate that a Spring context should be loaded
    • By default, this looks for a Context configuration file called TestClassName-context.xml in a subdirectory corresponding to the package of the test class. For instance, if your test class was org.apache.camel.tutorial.XMLInputTest, it would look for org/apache/camel/tutorial/XMLInputTest-context.xml
    • To override this default, use the locations attribute on the @ContextConfiguration annotation to provide specific context file locations (starting each path with a / if you don't want it to be relative to the package directory). My solution does this so I can put the context file directly in src/test/resources instead of in a package directory under there.
  6. Add a CamelContext instance variable to the test class, with the @Autowired annotation. That way Spring will automatically pull the CamelContext out of the Spring context and inject it into our test class.
  7. Add a ProducerTemplate instance variable and a setUp method that instantiates it from the CamelContext. We'll use the ProducerTemplate later to send messages to the route.
    Code Block
    java
    java
    protected ProducerTemplate<Exchange> template;
    
    protected void setUp() throws Exception {
        super.setUp();
        template = camelContext.createProducerTemplate();
    }
    
  8. Put in an empty test method just for the moment (so when we run this we can see that "1 test succeeded")
  9. Add the Spring <beans> element (including the Camel Namespace) with an empty <camelContext> element to the Spring context, like this:
    Code Block
    xml
    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                                   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                               http://activemq.apache.org/camel/schema/spring
                                   http://activemq.apache.org/camel/schema/spring/camel-spring-1.4.0.xsd">
    
        <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
        </camelContext>
    </beans>
    

...

  1. Save the input-customer1.xml file to src/test/resources
  2. Save your XSLT file (created in the previous step) to src/main/resources
  3. Write a Camel Route, either right in the Spring XML, or using the Java DSL (in another class under src/test/java somewhere). This route should use the Pipes and Filters integration pattern to:
    1. Start from the endpoint direct:start (which lets the test conveniently pass messages into the route)
    2. Call the endpoint xslt:YourXSLTFile.xsl (to transform the message with the specified XSLT template)
    3. Send the result to the endpoint mock:finish (which lets the test verify the route output)
  4. Add a test method to the unit test class that:
    1. Get a reference to the Mock endpoint mock:finish using code like this:
      Code Block
      java
      java
      MockEndpoint finish = MockEndpoint.resolve(camelContext, "mock:finish");
      
    2. Set the expectedMessageCount on that endpoint to 1
    3. Get a reference to the Customer 1 input file, using code like this:
      Code Block
      java
      java
      InputStream in = XMLInputTest.class.getResourceAsStream("/input-partner1.xml");
      assertNotNull(in);
      
    4. Send that InputStream as a message to the direct:start endpoint, using code like this:
      Code Block
      java
      java
          template.sendBody("direct:start", in);
      
      Note that we can send the sample file body in several formats (File, InputStream, String, etc.) but in this case an InputStream is pretty convenient.
    5. Ensure that the message made it through the route to the final endpoint, by testing all configured Mock endpoints like this:
      Code Block
      java
      java
      MockEndpoint.assertIsSatisfied(camelContext);
      
    6. If you like, inspect the final message body using some code like finish.getExchanges().get(0).getIn().getBody().
      • If you do this, you'll need to know what format that body is – String, byte array, InputStream, etc.
  5. Run your test with mvn install and make sure the build completes successfully.

...

  1. Create a new class under src/main/java, perhaps called CSVConverterBean.
  2. Add a method, with one argument of type List<List<String>> and the return type Invoice
    • You may annotate the argument with @Body to specifically designate it as the body of the incoming message
  3. In the method, the logic should look roughly like this:
    1. Create a new Invoice, using the method on the generated ObjectFactory class
    2. Loop through all the rows in the incoming CSV (the outer List)
    3. Skip the first row, which contains headers (column names)
    4. For the other rows:
      1. Create a new LineItemType (using the ObjectFactory again)
      2. Pick out all the cell values (the Strings in the inner List) and put them into the correct fields of the LineItemType
        • Not all of the values will actually go into the line item in this example
        • You may hardcode the column ordering based on the sample data file, or else try to read it dynamically from the headers in the first line
        • Note that you'll need to use a JAXB DatatypeFactory to create the XMLGregorianCalendar values that JAXB uses for the date fields in the XML – which probably means using a SimpleDateFormat to parse the date and setting that date on a GregorianCalendar
      3. Add the line item to the invoice
    5. Populate the partner ID, date of receipt, and order total on the Invoice
    6. Throw any exceptions out of the method, so Camel knows something went wrong
    7. Return the finished Invoice

...

Camel does not have a data format handler for Excel by default. We have two options – create an Excel DataFormat (so Camel can convert Excel spreadsheets to something like the CSV List<List<String>> automatically), or create a POJO that can translate Excel data manually. For now, the second approach is easier (if we go the DataFormat route, we need code to both read and write Excel files, whereas otherwise read-only will do).

...

  1. Update the Maven POM to include POI support:
    Code Block
    xml
    xml
    <dependency>
        <artifactId>poi</artifactId>
        <groupId>org.apache.poi</groupId>
        <version>3.1-FINAL</version>
    </dependency>
    
  2. Create a new class under src/main/java, perhaps called ExcelConverterBean.
  3. Add a method, with one argument of type InputStream and the return type Invoice
    • You may annotate the argument with @Body to specifically designate it as the body of the incoming message
  4. In the method, the logic should look roughly like this:
    1. Create a new Invoice, using the method on the generated ObjectFactory class
    2. Create a new HSSFWorkbook from the InputStream, and get the first sheet from it
    3. Loop through all the rows in the sheet
    4. Skip the first row, which contains headers (column names)
    5. For the other rows:
      1. Create a new LineItemType (using the ObjectFactory again)
      2. Pick out all the cell values and put them into the correct fields of the LineItemType (you'll need some data type conversion logic)
        • Not all of the values will actually go into the line item in this example
        • You may hardcode the column ordering based on the sample data file, or else try to read it dynamically from the headers in the first line
        • Note that you'll need to use a JAXB DatatypeFactory to create the XMLGregorianCalendar values that JAXB uses for the date fields in the XML – which probably means setting the date from a date cell on a GregorianCalendar
      3. Add the line item to the invoice
    6. Populate the partner ID, date of receipt, and order total on the Invoice
    7. Throw any exceptions out of the method, so Camel knows something went wrong
    8. Return the finished Invoice

...