View Javadoc

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