1 /**
2 * Copyright (c) 2013-2023, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package com.jcabi.github;
31
32 import com.jcabi.github.mock.MkGithub;
33 import java.io.IOException;
34 import java.time.ZonedDateTime;
35 import java.util.Map;
36 import javax.json.Json;
37 import org.hamcrest.MatcherAssert;
38 import org.hamcrest.Matchers;
39 import org.junit.Test;
40
41 /**
42 * Test case for {@link RepositoryStatistics}.
43 *
44 * @version $Id: dfb923ba1575c8134afe7721c6045f5ce21a11e1 $
45 * @author Volodya Lombrozo (volodya.lombrozo@gmail.com)
46 * @since 1.8.0
47 * @todo #1660:90min MkRepo returns only string values from json.
48 * MkRepo returns only strings values, it is why in all tests
49 * below we expect string values, which is wrong, of course.
50 * MkRepo should return different types like integer, double, long,
51 * etc. When it is implemented, we can replace strings with concrete
52 * types and remove that puzzle.
53 * @todo #1663:90min Refactor RepositoryStatisticsTest.
54 * RepositoryStatisticsTest has too many boilerplate code. Also it
55 * has repeated variables and constants. Refactor it to make it more
56 * readable and maintainable. Moreover all this variables don't follow
57 * PMD and Checkstyle rules. When it is done, remove this puzzle and
58 * all the checkstyle and PMD suppressions.
59 * @checkstyle StaticVariableNameCheck (1000 lines)
60 * @checkstyle MagicNumberCheck (1000 lines)
61 * @checkstyle LineLengthCheck (2 lines)
62 */
63 @SuppressWarnings({"PMD.SuspiciousConstantFieldName", "PMD.VariableNamingConventions"})
64 public final class RepositoryStatisticsTest {
65
66 /**
67 * Forks key in JSON.
68 */
69 private static final String FORKS_KEY = "forks_count";
70
71 /**
72 * Forks JSON value.
73 */
74 private static final int FORKS_VALUE = 1;
75
76 /**
77 * Language key in JSON.
78 */
79 private static final String LANGUAGE_KEY = "language";
80
81 /**
82 * Language value in JSON.
83 */
84 private static String LANGUAGE_VALUE = "java";
85
86 /**
87 * Stargazers key in JSON.
88 */
89 private static String STARGAZERS_KEY = "stargazers_count";
90
91 /**
92 * Stargazers value in JSON.
93 */
94 private static int STARGAZERS_VALUE = 2;
95
96 /**
97 * Watchers key in JSON.
98 */
99 private static String WATCHERS_KEY = "watchers_count";
100
101 /**
102 * Watchers value in JSON.
103 */
104 private static int WATCHERS_VALUE = 3;
105
106 /**
107 * Size key in JSON.
108 */
109 private static String SIZE_KEY = "size";
110
111 /**
112 * Size value in JSON.
113 */
114 private static int SIZE_VALUE = 4;
115
116 /**
117 * Issues key in JSON.
118 */
119 private static String ISSUES_KEY = "open_issues_count";
120
121 /**
122 * Issues value in JSON.
123 */
124 private static int ISSUES_VALUE = 5;
125
126 /**
127 * Created key in JSON.
128 */
129 private static String CREATED_KEY = "created_at";
130
131 /**
132 * Created value in JSON.
133 */
134 private static String CREATED_VALUE = "2011-01-26T19:14:43Z";
135
136 /**
137 * Updated key in JSON.
138 */
139 private static String UPDATED_KEY = "updated_at";
140
141 /**
142 * Updated value in JSON.
143 */
144 private static String UPDATED_VALUE = "2012-01-26T19:14:43Z";
145
146 /**
147 * Checks that RepositryStatistics can convert all values to a map.
148 * @throws IOException If some problem with I/O happened.
149 */
150 @Test
151 public void retrievesBasicStatisticsFromRepo()
152 throws IOException {
153 MatcherAssert.assertThat(
154 "We expect to have basic statistics from repo",
155 new RepositoryStatistics(this.repo()).toMap(),
156 Matchers.<Map<String, ?>>allOf(
157 Matchers.hasEntry(
158 RepositoryStatisticsTest.LANGUAGE_KEY,
159 RepositoryStatisticsTest.LANGUAGE_VALUE
160 ),
161 Matchers.hasEntry(
162 RepositoryStatisticsTest.FORKS_KEY,
163 String.valueOf(
164 RepositoryStatisticsTest.FORKS_VALUE
165 )
166 ),
167 Matchers.hasEntry(
168 RepositoryStatisticsTest.STARGAZERS_KEY,
169 String.valueOf(
170 RepositoryStatisticsTest.STARGAZERS_VALUE
171 )
172 ),
173 Matchers.hasEntry(
174 RepositoryStatisticsTest.WATCHERS_KEY,
175 String.valueOf(
176 RepositoryStatisticsTest.WATCHERS_VALUE
177 )
178 ),
179 Matchers.hasEntry(
180 RepositoryStatisticsTest.SIZE_KEY,
181 String.valueOf(
182 RepositoryStatisticsTest.SIZE_VALUE
183 )
184 ),
185 Matchers.hasEntry(
186 RepositoryStatisticsTest.ISSUES_KEY,
187 String.valueOf(
188 RepositoryStatisticsTest.ISSUES_VALUE
189 )
190 ),
191 Matchers.hasEntry(
192 RepositoryStatisticsTest.CREATED_KEY,
193 RepositoryStatisticsTest.CREATED_VALUE
194 ),
195 Matchers.hasEntry(
196 RepositoryStatisticsTest.UPDATED_KEY,
197 RepositoryStatisticsTest.UPDATED_VALUE
198 )
199 )
200 );
201 }
202
203 /**
204 * Checks that RepositryStatistics.Smart can retrieve all values.
205 * @throws IOException If some problem with I/O happened.
206 */
207 @Test
208 public void retrievesSmartStatistics() throws IOException {
209 final RepositoryStatistics.Smart smart =
210 new RepositoryStatistics.Smart(this.repo());
211 MatcherAssert.assertThat(
212 "Forks should be equal to 1",
213 smart.forks(),
214 Matchers.equalTo(
215 RepositoryStatisticsTest.FORKS_VALUE
216 )
217 );
218 MatcherAssert.assertThat(
219 "Stargazers should be equal to 2",
220 smart.stargazers(),
221 Matchers.equalTo(
222 RepositoryStatisticsTest.STARGAZERS_VALUE
223 )
224 );
225 MatcherAssert.assertThat(
226 "Watchers should be equal to 3",
227 smart.watchers(),
228 Matchers.equalTo(
229 RepositoryStatisticsTest.WATCHERS_VALUE
230 )
231 );
232 MatcherAssert.assertThat(
233 "Size should be equal to 4",
234 smart.size(),
235 Matchers.equalTo(
236 RepositoryStatisticsTest.SIZE_VALUE
237 )
238 );
239 MatcherAssert.assertThat(
240 "Issues should be equal to 5",
241 smart.openIssues(),
242 Matchers.equalTo(
243 RepositoryStatisticsTest.ISSUES_VALUE
244 )
245 );
246 MatcherAssert.assertThat(
247 "Created date should be equal to 2011-01-26T19:14:43Z",
248 smart.created(),
249 Matchers.equalTo(
250 ZonedDateTime.parse(
251 RepositoryStatisticsTest.CREATED_VALUE
252 )
253 )
254 );
255 }
256
257 /**
258 * Creates mock repo.
259 * @return Repo
260 * @throws IOException If some problem with I/O happened.
261 */
262 private Repo repo() throws IOException {
263 return new MkGithub()
264 .repos()
265 .create(
266 new Repos.RepoCreate("volodya-lombrozo", false)
267 .with(
268 RepositoryStatisticsTest.LANGUAGE_KEY,
269 Json.createValue(
270 RepositoryStatisticsTest.LANGUAGE_VALUE
271 )
272 )
273 .with(
274 RepositoryStatisticsTest.FORKS_KEY,
275 Json.createValue(
276 RepositoryStatisticsTest.FORKS_VALUE
277 )
278 )
279 .with(
280 RepositoryStatisticsTest.STARGAZERS_KEY,
281 Json.createValue(
282 RepositoryStatisticsTest.STARGAZERS_VALUE
283 )
284 )
285 .with(
286 RepositoryStatisticsTest.WATCHERS_KEY,
287 Json.createValue(
288 RepositoryStatisticsTest.WATCHERS_VALUE
289 )
290 )
291 .with(
292 RepositoryStatisticsTest.SIZE_KEY,
293 Json.createValue(
294 RepositoryStatisticsTest.SIZE_VALUE
295 )
296 )
297 .with(
298 RepositoryStatisticsTest.ISSUES_KEY,
299 Json.createValue(
300 RepositoryStatisticsTest.ISSUES_VALUE
301 )
302 )
303 .with(
304 RepositoryStatisticsTest.CREATED_KEY,
305 Json.createValue(
306 RepositoryStatisticsTest.CREATED_VALUE
307 )
308 )
309 .with(
310 RepositoryStatisticsTest.UPDATED_KEY,
311 Json.createValue(
312 RepositoryStatisticsTest.UPDATED_VALUE
313 )
314 )
315 );
316 }
317 }