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