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 RtUserOrganizations}.
23   * @since 0.1
24   */
25  @ExtendWith(RandomPort.class)
26  final class RtUserOrganizationsTest {
27      /**
28       * The rule for skipping test if there's BindException.
29       * @checkstyle VisibilityModifierCheck (3 lines)
30       */
31      @Test
32      void canIterateOrganizationsForUnauthUser() throws IOException {
33          final String username = "octopus";
34          final GitHub github = new MkGitHub();
35          final User user = github.users().get(username);
36          final MkContainer container = new MkGrizzlyContainer().next(
37              new MkAnswer.Simple(
38                  HttpURLConnection.HTTP_OK,
39                  Json.createArrayBuilder()
40                      .add(RtUserOrganizationsTest.org(3, "org11"))
41                      .add(RtUserOrganizationsTest.org(4, "org12"))
42                      .add(RtUserOrganizationsTest.org(5, "org13"))
43                      .build().toString()
44              )
45          ).start(RandomPort.port());
46          try {
47              final UserOrganizations orgs = new RtUserOrganizations(
48                  github,
49                  new ApacheRequest(container.home()),
50                  user
51              );
52              MatcherAssert.assertThat(
53                  "Collection size is incorrect",
54                  orgs.iterate(),
55                  Matchers.iterableWithSize(3)
56              );
57              MatcherAssert.assertThat(
58                  "String does not end with expected value",
59                  container.take().uri().toString(),
60                  Matchers.endsWith(String.format("/users/%s/orgs", username))
61              );
62          } finally {
63              container.stop();
64          }
65      }
66  
67      /**
68       * Create and return organization to test.
69       * @param number Organization ID
70       * @param login Organization login name.
71       * @return JsonObject
72       */
73      private static JsonObject org(final int number, final String login) {
74          return Json.createObjectBuilder()
75              .add("id", number)
76              .add("login", login)
77              .build();
78      }
79  }