View Javadoc

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