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.github.mock.MkGitHub;
8   import com.jcabi.http.mock.MkAnswer;
9   import com.jcabi.http.mock.MkContainer;
10  import com.jcabi.http.mock.MkGrizzlyContainer;
11  import com.jcabi.http.request.ApacheRequest;
12  import jakarta.json.Json;
13  import jakarta.json.JsonObject;
14  import java.io.IOException;
15  import java.net.HttpURLConnection;
16  import org.hamcrest.MatcherAssert;
17  import org.hamcrest.Matchers;
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.extension.ExtendWith;
20  
21  /**
22   * Test case for {@link RtOrganizations}.
23   * @since 0.1
24   */
25  @ExtendWith(RandomPort.class)
26  final class RtOrganizationsTest {
27  
28      /**
29       * The rule for skipping test if there's BindException.
30       * @checkstyle VisibilityModifierCheck (3 lines)
31       */
32      @Test
33      void fetchesSingleOrganization() throws IOException {
34          try (
35              MkContainer container = new MkGrizzlyContainer().next(
36                  new MkAnswer.Simple(HttpURLConnection.HTTP_OK, "")
37              ).start(RandomPort.port())
38          ) {
39              final Organizations orgs = new RtOrganizations(
40                  new MkGitHub(),
41                  new ApacheRequest(container.home())
42              );
43              MatcherAssert.assertThat(
44                  "Value is null",
45                  orgs.get("org"),
46                  Matchers.notNullValue()
47              );
48              container.stop();
49          }
50      }
51  
52      /**
53       * RtOrganizations should be able to iterate
54       * the logged-in user's organizations.
55       * @checkstyle MagicNumberCheck (25 lines)
56       */
57      @Test
58      void retrievesOrganizations() throws IOException {
59          final GitHub github = new MkGitHub();
60          try (
61              MkContainer container = new MkGrizzlyContainer().next(
62                  new MkAnswer.Simple(
63                      HttpURLConnection.HTTP_OK,
64                      Json.createArrayBuilder()
65                          .add(RtOrganizationsTest.org(1, "org1"))
66                          .add(RtOrganizationsTest.org(2, "org2"))
67                          .add(RtOrganizationsTest.org(3, "org3"))
68                          .build().toString()
69                  )
70              ).start(RandomPort.port())
71          ) {
72              final Organizations orgs = new RtOrganizations(
73                  github,
74                  new ApacheRequest(container.home())
75              );
76              MatcherAssert.assertThat(
77                  "Collection size is incorrect",
78                  orgs.iterate(),
79                  Matchers.iterableWithSize(3)
80              );
81              MatcherAssert.assertThat(
82                  "String does not end with expected value",
83                  container.take().uri().toString(),
84                  Matchers.endsWith("/user/orgs")
85              );
86              container.stop();
87          }
88      }
89  
90      /**
91       * Create and return organization to test.
92       * @param number Organization ID
93       * @param login Organization login name.
94       * @return JsonObject
95       */
96      private static JsonObject org(final int number, final String login) {
97          return Json.createObjectBuilder()
98              .add("id", number)
99              .add("login", login)
100             .build();
101     }
102 }