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.google.common.base.Optional;
33 import com.jcabi.github.mock.MkGithub;
34 import javax.json.Json;
35 import org.hamcrest.MatcherAssert;
36 import org.hamcrest.Matchers;
37 import org.junit.Test;
38
39 /**
40 * Test case for {@link Status}.
41 * @author Chris Rebert (github@chrisrebert.com)
42 * @version $Id: 8b3dd3bbf0227ec819516e03e6e7578d51d2ecda $
43 * @since 0.24
44 */
45 @SuppressWarnings("PMD.TooManyMethods")
46 public final class StatusTest {
47 /**
48 * Name of state property in Status JSON object.
49 */
50 private static final String STATE_PROP = "state";
51 /**
52 * Name of description property in Status JSON object.
53 */
54 private static final String DESCRIPTION_PROP = "description";
55 /**
56 * Name of description property in Status JSON object.
57 */
58 private static final String TARGET_PROP = "target_url";
59
60 /**
61 * Status.Smart can fetch its commit.
62 * @throws Exception If some problem inside
63 */
64 @Test
65 public void fetchesCommit() throws Exception {
66 final Commit cmmt = StatusTest.commit();
67 MatcherAssert.assertThat(
68 new Status.Smart(
69 new RtStatus(cmmt, Json.createObjectBuilder().build())
70 ).commit(),
71 Matchers.equalTo(cmmt)
72 );
73 }
74
75 /**
76 * Status.Smart can fetch its ID number.
77 * @throws Exception If some problem inside
78 */
79 @Test
80 public void fetchesId() throws Exception {
81 final int ident = 777;
82 MatcherAssert.assertThat(
83 new Status.Smart(
84 new RtStatus(
85 StatusTest.commit(),
86 Json.createObjectBuilder().add("id", ident).build()
87 )
88 ).identifier(),
89 Matchers.equalTo(ident)
90 );
91 }
92
93 /**
94 * Status.Smart can fetch its URL.
95 * @throws Exception If some problem inside
96 */
97 @Test
98 public void fetchesUrl() throws Exception {
99 final String url = "http://api.jcabi-github.invalid/wherever";
100 MatcherAssert.assertThat(
101 new Status.Smart(
102 new RtStatus(
103 StatusTest.commit(),
104 Json.createObjectBuilder().add("url", url).build()
105 )
106 ).url(),
107 Matchers.equalTo(url)
108 );
109 }
110
111 /**
112 * Status.Smart can fetch its state when it's error.
113 * @throws Exception If some problem inside
114 */
115 @Test
116 public void fetchesErrorState() throws Exception {
117 MatcherAssert.assertThat(
118 new Status.Smart(
119 new RtStatus(
120 StatusTest.commit(),
121 Json.createObjectBuilder()
122 .add(StatusTest.STATE_PROP, "error").build()
123 )
124 ).state(),
125 Matchers.equalTo(Status.State.ERROR)
126 );
127 }
128
129 /**
130 * Status.Smart can fetch its state when it's failure.
131 * @throws Exception If some problem inside
132 */
133 @Test
134 public void fetchesFailureState() throws Exception {
135 MatcherAssert.assertThat(
136 new Status.Smart(
137 new RtStatus(
138 StatusTest.commit(),
139 Json.createObjectBuilder()
140 .add(StatusTest.STATE_PROP, "failure").build()
141 )
142 ).state(),
143 Matchers.equalTo(Status.State.FAILURE)
144 );
145 }
146
147 /**
148 * Status.Smart can fetch its state when it's pending.
149 * @throws Exception If some problem inside
150 */
151 @Test
152 public void fetchesPendingState() throws Exception {
153 MatcherAssert.assertThat(
154 new Status.Smart(
155 new RtStatus(
156 StatusTest.commit(),
157 Json.createObjectBuilder()
158 .add(StatusTest.STATE_PROP, "pending").build()
159 )
160 ).state(),
161 Matchers.equalTo(Status.State.PENDING)
162 );
163 }
164
165 /**
166 * Status.Smart can fetch its state when it's success.
167 * @throws Exception If some problem inside
168 */
169 @Test
170 public void fetchesSuccessState() throws Exception {
171 MatcherAssert.assertThat(
172 new Status.Smart(
173 new RtStatus(
174 StatusTest.commit(),
175 Json.createObjectBuilder()
176 .add(StatusTest.STATE_PROP, "success").build()
177 )
178 ).state(),
179 Matchers.equalTo(Status.State.SUCCESS)
180 );
181 }
182
183 /**
184 * Status.Smart can fetch its target URL when it's present.
185 * @throws Exception If some problem inside
186 */
187 @Test
188 public void fetchesPresentTargetUrl() throws Exception {
189 final String url = "http://api.jcabi-github.invalid/good-luck";
190 MatcherAssert.assertThat(
191 new Status.Smart(
192 new RtStatus(
193 StatusTest.commit(),
194 Json.createObjectBuilder()
195 .add(StatusTest.TARGET_PROP, url).build()
196 )
197 ).targetUrl(),
198 Matchers.equalTo(Optional.of(url))
199 );
200 }
201
202 /**
203 * Status.Smart can fetch its target URL when it's absent.
204 * @throws Exception If some problem inside
205 */
206 @Test
207 public void fetchesAbsentTargetUrl() throws Exception {
208 MatcherAssert.assertThat(
209 new Status.Smart(
210 new RtStatus(
211 StatusTest.commit(),
212 Json.createObjectBuilder().build()
213 )
214 ).targetUrl(),
215 Matchers.equalTo(Optional.<String>absent())
216 );
217 }
218
219 /**
220 * Status.Smart can fetch its target URL when it's null.
221 * @throws Exception If some problem inside
222 */
223 @Test
224 public void fetchesNullTargetUrl() throws Exception {
225 MatcherAssert.assertThat(
226 new Status.Smart(
227 new RtStatus(
228 StatusTest.commit(),
229 Json.createObjectBuilder()
230 .addNull(StatusTest.TARGET_PROP).build()
231 )
232 ).targetUrl(),
233 Matchers.equalTo(Optional.<String>absent())
234 );
235 }
236
237 /**
238 * Status.Smart can fetch its description when it's present.
239 * @throws Exception If some problem inside
240 */
241 @Test
242 public void fetchesPresentDescription() throws Exception {
243 final String description = "Mostly harmless";
244 MatcherAssert.assertThat(
245 new Status.Smart(
246 new RtStatus(
247 StatusTest.commit(),
248 Json.createObjectBuilder()
249 .add(StatusTest.DESCRIPTION_PROP, description).build()
250 )
251 ).description(),
252 Matchers.equalTo(description)
253 );
254 }
255
256 /**
257 * Status.Smart can fetch its description when it's absent.
258 * @throws Exception If some problem inside
259 */
260 @Test
261 public void fetchesAbsentDescription() throws Exception {
262 MatcherAssert.assertThat(
263 new Status.Smart(
264 new RtStatus(
265 StatusTest.commit(),
266 Json.createObjectBuilder().build()
267 )
268 ).description(),
269 Matchers.equalTo("")
270 );
271 }
272
273 /**
274 * Status.Smart can fetch its description when it's null.
275 * @throws Exception If some problem inside
276 */
277 @Test
278 public void fetchesNullDescription() throws Exception {
279 MatcherAssert.assertThat(
280 new Status.Smart(
281 new RtStatus(
282 StatusTest.commit(),
283 Json.createObjectBuilder()
284 .addNull(StatusTest.DESCRIPTION_PROP).build()
285 )
286 ).description(),
287 Matchers.equalTo("")
288 );
289 }
290
291 /**
292 * Status.Smart can fetch its context.
293 * @throws Exception If some problem inside
294 */
295 @Test
296 public void fetchesContext() throws Exception {
297 final String context = "jcabi/github/tester";
298 MatcherAssert.assertThat(
299 new Status.Smart(
300 new RtStatus(
301 StatusTest.commit(),
302 Json.createObjectBuilder().add("context", context).build()
303 )
304 ).context(),
305 Matchers.equalTo(context)
306 );
307 }
308
309 /**
310 * Status.Smart can fetch its created-at timestamp.
311 * @throws Exception If some problem inside
312 */
313 @Test
314 public void fetchesCreatedAt() throws Exception {
315 final String when = "2015-02-27T19:35:32Z";
316 MatcherAssert.assertThat(
317 new Status.Smart(
318 new RtStatus(
319 StatusTest.commit(),
320 Json.createObjectBuilder().add("created_at", when).build()
321 )
322 ).createdAt(),
323 Matchers.equalTo(new Github.Time(when).date())
324 );
325 }
326
327 /**
328 * Status.Smart can fetch its last-updated-at timestamp.
329 * @throws Exception If some problem inside
330 */
331 @Test
332 public void fetchesUpdatedAt() throws Exception {
333 final String when = "2013-02-27T19:35:32Z";
334 MatcherAssert.assertThat(
335 new Status.Smart(
336 new RtStatus(
337 StatusTest.commit(),
338 Json.createObjectBuilder().add("updated_at", when).build()
339 )
340 ).updatedAt(),
341 Matchers.equalTo(new Github.Time(when).date())
342 );
343 }
344
345 /**
346 * Status.Smart can fetch its creator.
347 * @throws Exception If some problem inside
348 */
349 @Test
350 public void fetchesCreator() throws Exception {
351 final String login = "bob";
352 MatcherAssert.assertThat(
353 new Status.Smart(
354 new RtStatus(
355 StatusTest.commit(),
356 Json.createObjectBuilder()
357 .add(
358 "creator",
359 Json.createObjectBuilder()
360 .add("login", login).build()
361 ).build()
362 )
363 ).creator().login(),
364 Matchers.equalTo(login)
365 );
366 }
367
368 /**
369 * Returns a test commit to work with.
370 * @return Commit
371 * @throws Exception If some problem inside
372 */
373 private static Commit commit() throws Exception {
374 return new MkGithub().randomRepo().git().commits()
375 .get("d288364af5028c72e2a2c91c29343bae11fffcbe");
376 }
377 }