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.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.junit4.rule.DynamicPort;
14  
15  import java.util.HashMap;
16  import java.util.Map;
17  
18  import javax.jcr.Node;
19  import javax.jcr.NodeIterator;
20  import javax.jcr.PathNotFoundException;
21  import javax.jcr.Repository;
22  import javax.jcr.Session;
23  import javax.jcr.SimpleCredentials;
24  
25  import org.apache.abdera.model.Document;
26  import org.apache.abdera.model.Entry;
27  import org.apache.abdera.model.Feed;
28  import org.apache.abdera.protocol.client.AbderaClient;
29  import org.apache.abdera.protocol.client.ClientResponse;
30  import org.junit.Rule;
31  import org.junit.Test;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.assertNotNull;
35  import static org.junit.Assert.assertTrue;
36  
37  public class AtomEventTestCase extends FunctionalTestCase
38  {
39  
40      @Rule
41      public DynamicPort dynamicPort = new DynamicPort("port1");
42      
43      @Override
44      protected String getConfigResources()
45      {
46          return "atom-builder-conf.xml";
47      }
48  
49      @Override
50      protected void doTearDown() throws Exception
51      {
52          clearJcrRepository();
53          super.doTearDown();
54      }
55  
56      @Test
57      public void testCustomProvider() throws Exception
58      {
59          MuleClient client = muleContext.getClient();
60          client.send("vm://in", createOutboundMessage());
61  
62          Thread.sleep(1000);
63  
64          AbderaClient aClient = new AbderaClient();
65          String url = "http://localhost:" + dynamicPort.getNumber() + "/events";
66          ClientResponse res = aClient.get(url);
67  
68          Document<Feed> doc = res.getDocument();
69          Feed feed = doc.getRoot();
70  
71          assertEquals(1, feed.getEntries().size());
72          Entry e = feed.getEntries().get(0);
73  
74          assertEquals("Mmm feeding", e.getContent());
75          assertEquals("Foo Bar", e.getTitle());
76      }
77  
78      @Test
79      public void testMessageTransformation() throws Exception
80      {
81          MuleClient client = muleContext.getClient();
82          client.dispatch("vm://fromTest", createOutboundMessage());
83  
84          MuleMessage response = client.request("vm://toTest", RECEIVE_TIMEOUT);
85          assertNotNull(response);
86  
87          String payload = response.getPayloadAsString();
88          assertTrue(payload.contains("<author><name>Ross Mason</name></author>"));
89      }
90  
91      private MuleMessage createOutboundMessage()
92      {
93          Map<String, Object> props = new HashMap<String, Object>();
94          props.put("title", "Foo Bar");
95  
96          return new DefaultMuleMessage("Mmm feeding", props, muleContext);
97      }
98  
99      private void clearJcrRepository()
100     {
101         Repository repository = (Repository) muleContext.getRegistry().lookupObject("jcrRepository");
102         if (repository == null)
103         {
104             return;
105         }
106 
107         try
108         {
109             Session session = repository.login(new SimpleCredentials("username", "password".toCharArray()));
110 
111             Node node = session.getRootNode();
112             for (NodeIterator itr = node.getNodes(); itr.hasNext();)
113             {
114                 Node child = itr.nextNode();
115                 if (!child.getName().equals("jcr:system"))
116                 {
117                     child.remove();
118                 }
119             }
120             session.save();
121             session.logout();
122         }
123         catch (PathNotFoundException t)
124         {
125             // ignore, we're shutting down anyway
126         }
127         catch (Throwable t)
128         {
129             t.printStackTrace();
130         }
131     }
132 }