3535import com .jcabi .http .request .JdkRequest ;
3636import java .io .IOException ;
3737import java .net .HttpURLConnection ;
38+ import java .util .Arrays ;
39+ import java .util .Collection ;
3840import java .util .Random ;
3941import javax .json .Json ;
42+ import javax .json .JsonArrayBuilder ;
43+ import javax .json .JsonObjectBuilder ;
4044import org .hamcrest .MatcherAssert ;
4145import org .hamcrest .Matchers ;
4246import org .junit .Assert ;
5357 */
5458public final class RtChecksTest {
5559
60+ /**
61+ * Conclusion key in json check.
62+ */
63+ private static final String CONCLUSION_KEY = "conclusion" ;
64+
5665 /**
5766 * The rule for skipping test if there's BindException.
5867 * @checkstyle VisibilityModifierCheck (3 lines)
@@ -138,67 +147,70 @@ public void assertsOkResponse() throws IOException {
138147 }
139148 }
140149
150+ /**
151+ * Checks that library can handle unfinished checks.
152+ * @throws IOException If some I/O problem happens.
153+ */
141154 @ Test
142- public void retrievesUnfinishedChecksWithoutConclusion () throws IOException {
155+ public void retrievesUnfinishedChecksWithoutConclusion ()
156+ throws IOException {
143157 try (final MkContainer container = new MkGrizzlyContainer ()
144158 .next (
145159 new MkAnswer .Simple (
146160 HttpURLConnection .HTTP_OK ,
147- Json .createObjectBuilder ()
148- .add ("total_count" , Json .createValue (1 ))
149- .add ("check_runs" ,
150- Json .createArrayBuilder ()
151- .add (
152- Json .createObjectBuilder ()
153- .add ("id" , Json .createValue (new Random ().nextInt ()))
154- .add ("status" , "completed" )
155- .build ()
156- )
157- )
158- .build ()
159- .toString ()
161+ RtChecksTest .jsonChecks (
162+ RtChecksTest .jsonCheck ()
163+ .add (
164+ RtChecksTest .CONCLUSION_KEY ,
165+ Check .Conclusion .SUCCESS .value ()
166+ )
167+ )
160168 )
161169 ).start (this .resource .port ())
162170 ) {
163171 final Checks checks = new RtChecks (
164172 new JdkRequest (container .home ()),
165173 this .repo ().pulls ().get (0 )
166174 );
167- MatcherAssert .assertThat (checks .all (), Matchers .hasSize (1 ));
168- for (final Check check : checks .all ()) {
169- MatcherAssert .assertThat (check .successful (), Matchers .is (false ));
175+ final Collection <? extends Check > all = checks .all ();
176+ MatcherAssert .assertThat (all , Matchers .hasSize (1 ));
177+ for (final Check check : all ) {
178+ MatcherAssert .assertThat (
179+ check .successful (),
180+ Matchers .is (false )
181+ );
170182 }
171183 }
172184 }
173185
186+ /**
187+ * Checks that library can handle unfinished checks.
188+ * @throws IOException If some I/O problem happens.
189+ */
174190 @ Test
175- public void retrievesUnfinishedChecksWithoutStatusAndConclusion () throws IOException {
191+ public void retrievesUnfinishedChecksWithoutStatusAndConclusion ()
192+ throws IOException {
176193 try (final MkContainer container = new MkGrizzlyContainer ()
177194 .next (
178195 new MkAnswer .Simple (
179196 HttpURLConnection .HTTP_OK ,
180- Json .createObjectBuilder ()
181- .add ("total_count" , Json .createValue (1 ))
182- .add ("check_runs" ,
183- Json .createArrayBuilder ()
184- .add (
185- Json .createObjectBuilder ()
186- .add ("id" , Json .createValue (new Random ().nextInt ()))
187- .build ()
188- )
189- )
190- .build ()
191- .toString ()
197+ RtChecksTest .jsonChecks (
198+ RtChecksTest .jsonCheck ()
199+ )
192200 )
193201 ).start (this .resource .port ())
194202 ) {
195203 final Checks checks = new RtChecks (
196204 new JdkRequest (container .home ()),
197205 this .repo ().pulls ().get (0 )
198206 );
199- MatcherAssert .assertThat (checks .all (), Matchers .hasSize (1 ));
200- for (final Check check : checks .all ()) {
201- MatcherAssert .assertThat (check .successful (), Matchers .is (false ));
207+ final Collection <? extends Check > all = checks .all ();
208+ MatcherAssert .assertThat (all , Matchers .hasSize (1 ));
209+ for (final Check check : all ) {
210+ MatcherAssert .assertThat (
211+ check .successful (),
212+ Matchers .is (false )
213+ );
202214 }
203215 }
204216 }
@@ -209,19 +221,36 @@ public void retrievesUnfinishedChecksWithoutStatusAndConclusion() throws IOExcep
209221 * @return Json response body.
210222 */
211223 private static String jsonWithCheckRuns () {
224+ return RtChecksTest .jsonChecks (
225+ RtChecksTest .jsonCheck ()
226+ .add ("status" , Check .Status .COMPLETED .value ())
227+ .add (
228+ RtChecksTest .CONCLUSION_KEY ,
229+ Check .Conclusion .SUCCESS .value ()
230+ )
231+ );
232+ }
233+
234+ /**
235+ * Creates Json Check Builder.
236+ * @return JsonObjectBuilder.
237+ */
238+ private static JsonObjectBuilder jsonCheck () {
239+ return Json .createObjectBuilder ()
240+ .add ("id" , Json .createValue (new Random ().nextInt ()));
241+ }
242+
243+ /**
244+ * Creates json checks.
245+ * @param checks All checks that have to be included.
246+ * @return Json.
247+ */
248+ private static String jsonChecks (final JsonObjectBuilder ... checks ) {
249+ final JsonArrayBuilder all = Json .createArrayBuilder ();
250+ Arrays .stream (checks ).map (JsonObjectBuilder ::build ).forEach (all ::add );
212251 return Json .createObjectBuilder ()
213252 .add ("total_count" , Json .createValue (1 ))
214- .add (
215- "check_runs" ,
216- Json .createArrayBuilder ()
217- .add (
218- Json .createObjectBuilder ()
219- .add ("id" , Json .createValue (new Random ().nextInt ()))
220- .add ("status" , "completed" )
221- .add ("conclusion" , "success" )
222- .build ()
223- )
224- )
253+ .add ("check_runs" , all .build ())
225254 .build ()
226255 .toString ();
227256 }
0 commit comments