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.jcabi.http.mock.MkAnswer;
8   import com.jcabi.http.mock.MkContainer;
9   import com.jcabi.http.mock.MkGrizzlyContainer;
10  import com.jcabi.http.request.ApacheRequest;
11  import jakarta.json.Json;
12  import jakarta.json.JsonObject;
13  import java.io.IOException;
14  import java.net.HttpURLConnection;
15  import org.hamcrest.MatcherAssert;
16  import org.hamcrest.Matchers;
17  import org.junit.jupiter.api.Test;
18  import org.junit.jupiter.api.extension.ExtendWith;
19  import org.mockito.Mockito;
20  
21  /**
22   * Test case for {@link RtUsers}.
23   * @since 0.4
24   */
25  @ExtendWith(RandomPort.class)
26  final class RtUsersTest {
27  
28      /**
29       * The rule for skipping test if there's BindException.
30       * @checkstyle VisibilityModifierCheck (3 lines)
31       */
32      @Test
33      void iterateUsers() throws IOException {
34          final String identifier = "1";
35          final MkContainer container = new MkGrizzlyContainer().next(
36              new MkAnswer.Simple(
37                  HttpURLConnection.HTTP_OK,
38                  Json.createArrayBuilder()
39                      .add(RtUsersTest.json("octocat", identifier))
40                      .add(RtUsersTest.json("dummy", "2"))
41                      .build().toString()
42              )
43          ).start(RandomPort.port());
44          final Users users = new RtUsers(
45              Mockito.mock(GitHub.class),
46              new ApacheRequest(container.home())
47          );
48          MatcherAssert.assertThat(
49              "Collection size is incorrect",
50              users.iterate(identifier),
51              Matchers.iterableWithSize(2)
52          );
53          container.stop();
54      }
55  
56      @Test
57      void getSingleUser() throws IOException {
58          final String login = "mark";
59          final MkContainer container = new MkGrizzlyContainer().next(
60              new MkAnswer.Simple(
61                  HttpURLConnection.HTTP_OK,
62                  RtUsersTest.json(login, "3").toString()
63              )
64          ).start(RandomPort.port());
65          final Users users = new RtUsers(
66              Mockito.mock(GitHub.class),
67              new ApacheRequest(container.home())
68          );
69          MatcherAssert.assertThat(
70              "Values are not equal",
71              users.get(login).login(),
72              Matchers.equalTo(login)
73          );
74          container.stop();
75      }
76  
77      @Test
78      void getCurrentUser() throws IOException {
79          final String login = "kendy";
80          final MkContainer container = new MkGrizzlyContainer().next(
81              new MkAnswer.Simple(
82                  HttpURLConnection.HTTP_OK,
83                  RtUsersTest.json(login, "4").toString()
84              )
85          ).start(RandomPort.port());
86          final Users users = new RtUsers(
87              Mockito.mock(GitHub.class),
88              new ApacheRequest(container.home())
89          );
90          MatcherAssert.assertThat(
91              "Values are not equal",
92              users.self().login(),
93              Matchers.equalTo(login)
94          );
95          container.stop();
96      }
97  
98      /**
99       * Create and return JsonObject to test.
100      * @param login Username to login
101      * @param identifier User Id
102      * @return JsonObject
103      */
104     private static JsonObject json(
105         final String login, final String identifier
106     ) {
107         return Json.createObjectBuilder()
108             .add("id", Integer.valueOf(identifier))
109             .add("login", login)
110             .build();
111     }
112 }