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 java.util.Iterator;
19  import java.util.List;
20  
21  import net.sf.jdigg.arguments.ListCommentsArguments;
22  import net.sf.jdigg.beans.Comment;
23  import net.sf.jdigg.beans.Comments;
24  import net.sf.jdigg.exception.DiggException;
25  import net.sf.jdigg.exception.RequestException;
26  import net.sf.jdigg.exception.ResponseParserException;
27  
28  /**
29   * JUnit Test.
30   * @author Philip May
31   * @since 1.0 M1
32   */
33  public class ListCommentsRequestTest extends ListRequestTestCase {
34  
35      public ListCommentsRequestTest() {
36      }
37  
38      public ListCommentsRequestTest(String name) {
39          super(name);
40      }
41  
42      private void assertComment(Comment comment) {
43          assertTrue(comment.getId() > 0);
44          assertNotNull(comment.getComment());
45      }
46  
47      private void assertCommentList(List commentList) {
48          for (Iterator commentListIterator = commentList.iterator(); commentListIterator.hasNext(); ) {
49              Comment comment = (Comment) commentListIterator.next();
50              assertComment(comment);
51          }
52      }
53  
54      public void testAllComments() throws RequestException, ResponseParserException, DiggException {
55          final int COUNT = 23;
56  
57          ListCommentsArguments listCommentsArguments = new ListCommentsArguments("http://sf.net");
58          listCommentsArguments.setCount(COUNT);
59  
60          ListCommentsRequest listCommentsRequest = new ListCommentsRequest(getRequestConfig());
61          Comments result = listCommentsRequest.allComments(listCommentsArguments);
62  
63          assertNotNull(result);
64          assertEquals(COUNT, result.getCount());
65          List commentList = result.getCommentList();
66          assertNotNull(commentList);
67          assertTrue(commentList.size() > 0);
68          assertEquals(COUNT, commentList.size());
69          assertCommentList(commentList);
70      }
71  
72      public void testPopularComments() throws RequestException, ResponseParserException, DiggException {
73          final int COUNT = 23;
74  
75          ListCommentsArguments listCommentsArguments = new ListCommentsArguments("http://sf.net");
76          listCommentsArguments.setCount(COUNT);
77  
78          ListCommentsRequest listCommentsRequest = new ListCommentsRequest(getRequestConfig());
79          Comments result = listCommentsRequest.popularComments(listCommentsArguments);
80  
81          assertNotNull(result);
82          assertEquals(COUNT, result.getCount());
83          List commentList = result.getCommentList();
84          assertNotNull(commentList);
85          assertTrue(commentList.size() > 0);
86          assertEquals(COUNT, commentList.size());
87          assertCommentList(commentList);
88      }
89  
90      public void testUpcomingComments() throws RequestException, ResponseParserException, DiggException {
91          final int COUNT = 23;
92  
93          ListCommentsArguments listCommentsArguments = new ListCommentsArguments("http://sf.net");
94          listCommentsArguments.setCount(COUNT);
95  
96          ListCommentsRequest listCommentsRequest = new ListCommentsRequest(getRequestConfig());
97          Comments result = listCommentsRequest.upcomingComments(listCommentsArguments);
98  
99          assertNotNull(result);
100         assertEquals(COUNT, result.getCount());
101         List commentList = result.getCommentList();
102         assertNotNull(commentList);
103         assertTrue(commentList.size() > 0);
104         assertEquals(COUNT, commentList.size());
105         assertCommentList(commentList);
106     }
107 
108     public void testTopLevelCommentsForGivenStory() throws RequestException, ResponseParserException, DiggException {
109         final int COUNT = 11;
110 
111         ListCommentsArguments listCommentsArguments = new ListCommentsArguments("http://sf.net");
112         listCommentsArguments.setCount(COUNT);
113 
114         ListCommentsRequest listCommentsRequest = new ListCommentsRequest(getRequestConfig());
115         Comments result = listCommentsRequest.topLevelCommentsForGivenStory(listCommentsArguments, 857895);
116 
117         assertNotNull(result);
118         List commentList = result.getCommentList();
119         assertNotNull(commentList);
120         assertTrue(commentList.size() > 0);
121         assertEquals(COUNT, commentList.size());
122         assertCommentList(commentList);
123     }
124 
125     public void testDiggError() throws RequestException, ResponseParserException, DiggException {
126         ListCommentsArguments listCommentsArguments = new ListCommentsArguments("http://sf.net");
127         listCommentsArguments.setCount(200);
128 
129         ListCommentsRequest listCommentsRequest = new ListCommentsRequest(getRequestConfig());
130 
131         try {
132             listCommentsRequest.topLevelCommentsForGivenStory(listCommentsArguments, 857895);
133             fail();
134         } catch (DiggException e) {
135             assertEquals(1007, e.getDiggErrorCode());
136             assertNotNull(e.getDiggErrorMessage());
137             assertTrue(e.getDiggErrorMessage().length() > 0);
138         }
139     }
140 
141     public void testRequestConfigNotNull() {
142         try {
143             new ListCommentsRequest(null);
144             fail();
145         } catch (IllegalArgumentException e) {
146             // everything is all right!
147         }
148     }
149 
150 }