View Javadoc

1   /*
2    * $Id: EventTest.java 22450 2011-07-19 08:20:41Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.atom.event;
11  
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.junit4.rule.DynamicPort;
14  
15  import javax.jcr.Node;
16  import javax.jcr.NodeIterator;
17  import javax.jcr.PathNotFoundException;
18  import javax.jcr.Repository;
19  import javax.jcr.Session;
20  import javax.jcr.SimpleCredentials;
21  
22  import org.apache.abdera.model.Document;
23  import org.apache.abdera.model.Feed;
24  import org.apache.abdera.protocol.client.AbderaClient;
25  import org.apache.abdera.protocol.client.ClientResponse;
26  import org.junit.Rule;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertNotNull;
30  import static org.junit.Assert.assertTrue;
31  
32  public class EventTest extends FunctionalTestCase
33  {
34      private Repository repository;
35  
36      @Rule
37      public DynamicPort dynamicPort = new DynamicPort("port1");
38  
39      @Override
40      protected String getConfigResources()
41      {
42          return "eventqueue-conf.xml";
43      }
44  
45      @Override
46      protected void doSetUp() throws Exception
47      {
48          super.doSetUp();
49          EntryReceiver component = (EntryReceiver)getComponent("eventConsumer");
50          component.getReceivedEntries().set(0);
51      }
52  
53      @Override
54      protected void doTearDown() throws Exception
55      {
56          clearJcrRepository();
57          super.doTearDown();
58      }
59  
60      private void clearJcrRepository()
61      {
62          try
63          {
64              if (repository == null)
65              {
66                  return;
67              }
68              Session session = repository.login(new SimpleCredentials("username", "password".toCharArray()));
69      
70              Node node = session.getRootNode();
71      
72              for (NodeIterator itr = node.getNodes(); itr.hasNext();)
73              {
74                  Node child = itr.nextNode();
75                  if (!child.getName().equals("jcr:system"))
76                  {
77                      child.remove();
78                  }
79              }
80              session.save();
81              session.logout();
82          }
83          catch (PathNotFoundException t)
84          {
85              // do nothing
86          }
87          catch (Throwable t)
88          {
89              // do nothing
90          }
91      }
92  
93      @Test
94      public void testCustomerProvider() throws Exception
95      {
96          repository = (Repository) muleContext.getRegistry().lookupObject("jcrRepository");
97  
98          Thread.sleep(5000);
99  
100         AbderaClient client = new AbderaClient();
101         ClientResponse res = client.get("http://localhost:" + dynamicPort.getNumber() + "/events");
102 
103         Document<Feed> doc = res.getDocument();
104         // see if this helps with intermittent failures
105         doc.complete();
106         Feed feed = doc.getRoot();
107         // see if this helps with intermittent failures
108         feed.complete();
109         assertNotNull("feed is null", feed);
110         assertTrue( feed.getEntries().size() >= 1);
111         EntryReceiver component = (EntryReceiver)getComponent("eventConsumer");
112 
113         assertTrue(component.getCount() > 0);
114     }
115 }