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