1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jdigg.requests;
17  
18  import net.sf.jdigg.arguments.ArgumentValues;
19  import net.sf.jdigg.arguments.ListStoriesArguments;
20  import net.sf.jdigg.beans.Stories;
21  import net.sf.jdigg.beans.Story;
22  import net.sf.jdigg.exception.DiggException;
23  import net.sf.jdigg.exception.RequestException;
24  import net.sf.jdigg.exception.ResponseParserException;
25  
26  /**
27   * JUnit Test.
28   * @author Philip May
29   * @since 1.0 M1
30   */
31  public class ListStoriesRequestTest extends ListRequestTestCase {
32  
33      public ListStoriesRequestTest() {
34      }
35  
36      public ListStoriesRequestTest(String name) {
37          super(name);
38      }
39  
40      private void assertStory(Story story) {
41          assertTrue(story.getId() > 0);
42          assertNotNull(story.getLink());
43          assertTrue(story.getSubmitDate() > 0);
44          assertTrue(story.getDiggs() >= 0);
45          assertTrue(story.getComments() >= 0);
46          assertNotNull(story.getUser());
47          assertNotNull(story.getTopic());
48          assertNotNull(story.getTopic().getName());
49          assertNotNull(story.getTopic().getShortName());
50          assertNotNull(story.getContainer());
51          assertNotNull(story.getContainer().getName());
52          assertNotNull(story.getContainer().getShortName());
53      }
54  
55      public void testAllStories() throws RequestException, ResponseParserException, DiggException {
56          ListStoriesArguments listStoriesArguments = new ListStoriesArguments("http://sf.net");
57          listStoriesArguments.setCount(5);
58  
59          ListStoriesRequest listStoriesRequest = new ListStoriesRequest(getRequestConfig());
60          Stories result = listStoriesRequest.allStories(listStoriesArguments);
61  
62          assertNotNull(result);
63          assertNotNull(result.getStoryList());
64          assertTrue(result.getStoryList().size() > 0);
65          assertEquals(5, result.getStoryList().size());
66          assertStory((Story) result.getStoryList().iterator().next());
67      }
68  
69      public void testPopularStories() throws RequestException, ResponseParserException, DiggException {
70          ListStoriesArguments listStoriesArguments = new ListStoriesArguments("http://sf.net");
71          listStoriesArguments.setCount(5);
72  
73          ListStoriesRequest listStories = new ListStoriesRequest(getRequestConfig());
74          Stories result = listStories.popularStories(listStoriesArguments);
75  
76          assertNotNull(result);
77          assertNotNull(result.getStoryList());
78          assertTrue(result.getStoryList().size() > 0);
79          assertEquals(5, result.getStoryList().size());
80          assertStory((Story) result.getStoryList().iterator().next());
81      }
82  
83      public void testUpcomingStories() throws RequestException, ResponseParserException, DiggException {
84          ListStoriesArguments listStoriesArguments = new ListStoriesArguments("http://sf.net");
85          listStoriesArguments.setCount(5);
86  
87          ListStoriesRequest listStories = new ListStoriesRequest(getRequestConfig());
88          Stories result = listStories.upcomingStories(listStoriesArguments);
89  
90          assertNotNull(result);
91          assertNotNull(result.getStoryList());
92          assertTrue(result.getStoryList().size() > 0);
93          assertEquals(5, result.getStoryList().size());
94          assertStory((Story) result.getStoryList().iterator().next());
95      }
96  
97      public void testTopStories() throws RequestException, ResponseParserException, DiggException {
98          ListStoriesArguments listStoriesArguments = new ListStoriesArguments("http://sf.net");
99          listStoriesArguments.setCount(5);
100 
101         ListStoriesRequest listStories = new ListStoriesRequest(getRequestConfig());
102         Stories result = listStories.topStories(listStoriesArguments);
103 
104         assertNotNull(result);
105         assertNotNull(result.getStoryList());
106         assertTrue(result.getStoryList().size() > 0);
107         assertEquals(5, result.getStoryList().size());
108         assertStory((Story) result.getStoryList().iterator().next());
109     }
110 
111     public void testHotStories() throws RequestException, ResponseParserException, DiggException {
112         ListStoriesArguments listStoriesArguments = new ListStoriesArguments("http://sf.net");
113         listStoriesArguments.setCount(2);
114 
115         ListStoriesRequest listStories = new ListStoriesRequest(getRequestConfig());
116         Stories result = listStories.hotStories(listStoriesArguments);
117 
118         assertNotNull(result);
119         assertNotNull(result.getStoryList());
120         assertTrue(result.getStoryList().size() > 0);
121         assertEquals(2, result.getStoryList().size());
122         assertStory((Story) result.getStoryList().iterator().next());
123     }
124 
125     public void testAllStoriesFromGivenContainer_01() throws RequestException, ResponseParserException, DiggException {
126         ListStoriesArguments listStoriesArguments = new ListStoriesArguments("http://sf.net");
127         listStoriesArguments.setCount(5);
128 
129         ListStoriesRequest listStories = new ListStoriesRequest(getRequestConfig());
130         Stories result = listStories.allStoriesFromGivenContainer(listStoriesArguments, "technology");
131 
132         assertNotNull(result);
133         assertNotNull(result.getStoryList());
134         assertTrue(result.getStoryList().size() > 0);
135         assertEquals(5, result.getStoryList().size());
136         assertStory((Story) result.getStoryList().iterator().next());
137     }
138 
139     public void testAllStoriesFromGivenContainer_02() throws RequestException, ResponseParserException, DiggException {
140         ListStoriesArguments listStoriesArguments = new ListStoriesArguments("http://sf.net");
141         listStoriesArguments.setCount(5);
142 
143         ListStoriesRequest listStories = new ListStoriesRequest(getRequestConfig());
144 
145         try {
146             listStories.allStoriesFromGivenContainer(listStoriesArguments, "xyz");
147             fail();
148         } catch (DiggException e) {
149             assertEquals(1014, e.getDiggErrorCode());
150             assertNotNull(e.getDiggErrorMessage());
151             assertTrue(e.getDiggErrorMessage().length() > 0);
152         }
153     }
154 
155     public void testAllStoriesFromGivenTopic() throws RequestException, ResponseParserException, DiggException {
156         ListStoriesArguments listStoriesArguments = new ListStoriesArguments("http://sf.net");
157         listStoriesArguments.setCount(5);
158         listStoriesArguments.setSort(ArgumentValues.SORT_DIGG_COUNT_DESC);
159 
160         ListStoriesRequest listStories = new ListStoriesRequest(getRequestConfig());
161         Stories result = listStories.allStoriesFromGivenTopic(listStoriesArguments, "apple");
162 
163         assertNotNull(result);
164         assertNotNull(result.getStoryList());
165         assertTrue(result.getStoryList().size() > 0);
166         assertEquals(5, result.getStoryList().size());
167         assertStory((Story) result.getStoryList().iterator().next());
168     }
169 
170     public void testRequestConfigNotNull() {
171         try {
172             new ListStoriesRequest(null);
173             fail();
174         } catch (IllegalArgumentException e) {
175             // everything is all right!
176         }
177     }
178 
179 }