View Javadoc

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