title: rollback transaction in test with apache camel
body: <p>I am struggling to make a working junit test that rolls back actions that the occurred during the camel routing.</p>
  
  <p>I have a camel route setup that listens on a directory.  It is expecting a csv file.  When the csv file appears it then creates new SearchAnalytics data.  It adds a new row into a table per each line in the csv file.  </p>
  
  <p>The default spring transaction methods that I have put do not seem to apply to actions that occur on the camel routing. </p>
  
  <p>The code below works.  However it saves the data permanently and does not rollback the insert.  This means that the test will only pass once unless I manually delete the data.</p>
  
  <blockquote>
    <p>Given my example code how do I make it roll back the transaction?</p>
  </blockquote>
  
  <p><strong>my route looks like this</strong></p>
  
  <pre><code>from("ftp://some__remote__ftp_dir_path")
    .routeId("searchAnalyticsImport")             
    .choice()
      .when(simple("${in.header.CamelFileName} contains '.csv'"))
      .split().method("csvSplitter", "iterator").streaming() // reads the csv file returns data objects
      .processRef("searchAnalyticsProcesser")  // this some dao saves
      .to(Queues.SOME_REQUEST)
  .end();
  </code></pre>
  
  <p><strong>Junit test</strong></p>
  
  <pre><code>@TransactionConfiguration(defaultRollback = true, transactionManager = "transactionManager")
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { TestAppConfig.class})
  public class searchAnalyticsImportTest  {
  
      @EndpointInject(uri = "mock:sippmatcher.requestqueue?preserveMessageQos=true")
      private MockEndpoint mockEndpointRequest;
  
      @Before
      public void setup() throws Exception {
  
          camelContext.getRouteDefinition("searchAnalyticsImport").adviceWith(camelContext, new AdviceWithRouteBuilder() {
              @Override
              public void configure() throws Exception {
  
                  replaceFromWith("file://"+this.getClass().getResource("path to folder etc...")+"?noop=true");
  
                  interceptSendToEndpoint(Queues.SOME_REQUEST)
                          .skipSendToOriginalEndpoint()
                          .to(mockEndpointRequest);
              }
          });
      }
  
      @Test
      public void simpleTest() throws Exception{
  
          // there are 2 results in the test csv file.. need to poll the results till it completes
          PollWithTimeout.run("keep polling until route has been statisfied", 15000, new PollWithTimeout.Attempt() {
              @Override
              public boolean complete() {
                  Date dateTime1MinuteAgo = new DateTime().minusMinutes(1).toDate();
  
                  Integer newSearchCount = searchAnalysiticDao.findBySearchStartedAfter(dateTime1MinuteAgo).size();
  
                  System.out.println("Recently added count: " + newSearchCount);
                  return (newSearchCount == 2);
              }
          });
  
          mockEndpointRequest.expectedMessageCount(2);
          mockEndpointRequest.assertIsSatisfied();
      }
  }
  </code></pre>