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.rss;
8   
9   import org.mule.tck.functional.CounterCallback;
10  import org.mule.tck.functional.FunctionalTestComponent;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  import org.mule.tck.junit4.rule.DynamicPort;
13  import org.mule.tck.probe.PollingProber;
14  import org.mule.tck.probe.Probe;
15  import org.mule.tck.probe.Prober;
16  import org.mule.util.IOUtils;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.util.concurrent.atomic.AtomicInteger;
22  
23  import org.junit.After;
24  import org.junit.Rule;
25  import org.junit.Test;
26  import org.simpleframework.http.Request;
27  import org.simpleframework.http.Response;
28  import org.simpleframework.http.core.Container;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.fail;
32  
33  public class FeedConsumeAndSplitTestCase extends FunctionalTestCase
34  {
35      private static final int ENTRIES_IN_RSS_FEED = 25;
36  
37      private final CounterCallback counter = new CounterCallback();
38      private SimpleHttpServer httpServer;
39      private AtomicInteger pollCount = new AtomicInteger(0);
40  
41      @Rule
42      public DynamicPort dynamicPort = new DynamicPort("port1");
43  
44      @Override
45      protected String getConfigResources()
46      {
47          return "rss-consume-and-split.xml";
48     }
49  
50      @Override
51      protected void doSetUp() throws Exception
52      {
53          // start the HTTP server before Mule is started
54          startHttpServer();
55  
56          super.doSetUp();
57          addCounterToFeedConsumerComponent();
58      }
59  
60      private void startHttpServer() throws IOException
61      {
62          httpServer = new SimpleHttpServer(dynamicPort.getNumber(), new RssFeeder());
63          httpServer.start();
64      }
65  
66      private void addCounterToFeedConsumerComponent() throws Exception
67      {
68          FunctionalTestComponent comp = (FunctionalTestComponent) getComponent("feedConsumer");
69          comp.setEventCallback(counter);
70      }
71  
72      @After
73      public void stopHttpServer()
74      {
75          if (httpServer != null)
76          {
77              httpServer.stop();
78          }
79      }
80  
81      @Test
82      public void testConsume() throws Exception
83      {
84          waitForAllEntriesFromSampleFeedToArrive();
85          waitForTheNextPoll();
86  
87          // We should only receive entries once
88          assertEquals(ENTRIES_IN_RSS_FEED, counter.getCallbackCount());
89      }
90  
91      private void waitForAllEntriesFromSampleFeedToArrive()
92      {
93          Prober prober = new PollingProber(10000, 100);
94          prober.check(new Probe()
95          {
96              public boolean isSatisfied()
97              {
98                  return counter.getCallbackCount() == ENTRIES_IN_RSS_FEED;
99              }
100 
101             public String describeFailure()
102             {
103                 return String.format("Did not receive %d feed entries (only got %d)",
104                     ENTRIES_IN_RSS_FEED, counter.getCallbackCount());
105             }
106         });
107     }
108 
109     private void waitForTheNextPoll()
110     {
111         final int currentPollCount = pollCount.get();
112         Prober prober = new PollingProber(2000, 100);
113         prober.check(new Probe()
114         {
115             public boolean isSatisfied()
116             {
117                 return pollCount.get() > currentPollCount;
118             }
119 
120             public String describeFailure()
121             {
122                 return "Poll count did not increment in time";
123             }
124         });
125     }
126 
127     private class RssFeeder implements Container
128     {
129         public void handle(Request request, Response response)
130         {
131             try
132             {
133                 InputStream rssFeed = SampleFeed.feedAsStream();
134 
135                 OutputStream responseStream = response.getOutputStream();
136                 IOUtils.copy(rssFeed, responseStream);
137                 responseStream.close();
138             }
139             catch (IOException e)
140             {
141                 fail();
142             }
143 
144             pollCount.incrementAndGet();
145         }
146     }
147 }