...
- Update the Maven POM to include CSV Data Format support:
Code Block xml xml <dependency> <artifactId>camel-csv</artifactId> <groupId>org.apache.camel</groupId> <version>1.4.0</version> </dependency>
- Write the routes (right in the Spring XML context, or using the Java DSL) for the CSV conversion process, again using the Pipes and Filters pattern:
- Start from the endpoint direct:CSVstart (which lets the test conveniently pass messages into the route). We'll name this differently than the starting point for the previous test, in case you use the Java DSL and put all your routes in the same package (which would mean that each test would load the DSL routes for several tests.)
- This time, there's a little preparation to be done. Camel doesn't know that the initial input is a CSV, so it won't be able to convert it to the expected
List<List<String>>
without a little hint. For that, we need an unmarshal transformation in the route. Theunmarshal
method (in the DSL) or element (in the XML) takes a child indicating the format to unmarshal; in this case that should becsv
. - Next invoke the POJO to transform the message with a bean:CSVConverter endpoint
- As before, send the result to the endpoint mock:finish (which lets the test verify the route output)
- Finally, we need a Spring
<bean>
element in the Spring context XML file (but outside the<camelContext>
element) to define the Spring bean that our route invokes. This Spring bean should have aname
attribute that matches the name used in thebean
endpoint (CSVConverter
in the example above), and aclass
attribute that points to the CSV-to-JAXB POJO class you wrote above (such as,org.apache.camel.tutorial.CSVConverterBean
). When Spring is in the picture, anybean
endpoints look up Spring beans with the specified name.
- Write a test method in the test class, which should look very similar to the previous test class:
- Get the MockEndpoint for the final endpoint, and tell it to expect one message
- Load the Partner 2 sample CSV file from the ClassPath, and send it as the body of a message to the starting endpoint
- Verify that the final MockEndpoint is satisfied (that is, it received one message) and examine the message body if you like
- Note that we didn't marshal the JAXB POJOs to XML in this test, so the final message should contain an
Invoice
as the body. You could write a simple line of code to get the Exchange (and Message) from the MockEndpoint to confirm that.
- Note that we didn't marshal the JAXB POJOs to XML in this test, so the final message should contain an
- Run this new test with mvn install and make sure it passes and the build completes successfully.
...
Solution: Here's an example of what the ExcelConverterBean might look like.
Create a unit test
The unit tests should be pretty familiar now. The test class and context for the Excel bean should be quite similar to the CSV bean.
- Create the basic test class and corresponding Spring Context XML configuration file
- The XML config should look a lot like the CSV test, except:
- Remember to use a different start endpoint name if you're using the Java DSL and not use separate packages per test
- You don't need the
unmarshal
step since the Excel POJO takes the rawInputStream
from the source endpoint - You'll declare a
<bean>
and endpoint for the Excel bean prepared above instead of the CSV bean
- The test class should look a lot like the CSV test, except use the right input file name and start endpoint name.
Tip | ||
---|---|---|
| ||
You may notice that your tests emit a lot less output all of a sudden. The dependency on POI brought in Log4J and configured commons-logging to use it, so now we need a log4j.properties file to configure log output. You can use the attached one (snarfed from ActiveMQ) or write your own; either way save it to |
Solution: Your finished test might look something like this:
- src/test/java/org/apache/camel/tutorial/ExcelInputTest.java
- For XML Configuration:
- src/test/resources/ExcelInputTest-context.xml
- Or, for Java DSL Configuration:
- src/test/resources/ExcelInputTest-dsl-context.xml
- src/test/java/org/apache/camel/tutorial/routes/ExcelInputTestRoute.java
Step 7: Put this all together into Camel routes for the Customer Input
...