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.github.mock.MkOrganization;
9   import com.jcabi.github.mock.MkStorage;
10  import com.jcabi.http.Request;
11  import com.jcabi.http.mock.MkAnswer;
12  import com.jcabi.http.mock.MkContainer;
13  import com.jcabi.http.mock.MkGrizzlyContainer;
14  import com.jcabi.http.mock.MkQuery;
15  import com.jcabi.http.request.ApacheRequest;
16  import com.jcabi.http.request.FakeRequest;
17  import java.io.IOException;
18  import java.net.HttpURLConnection;
19  import org.hamcrest.MatcherAssert;
20  import org.hamcrest.Matchers;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.api.extension.ExtendWith;
24  
25  /**
26   * Test case for {@link RtPublicMembers}.
27   * @since 0.1
28   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
29   */
30  @ExtendWith(RandomPort.class)
31  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
32  final class RtPublicMembersTest {
33      /**
34       * Test organization.
35       */
36      private static final String ORG = "starfleet";
37  
38      /**
39       * Test username.
40       */
41      private static final String USERNAME = "wesley";
42  
43      /**
44       * Public members URL for test org.
45       */
46      private static final String MEMBERS_URL = String.format(
47          "/orgs/%s/public_members",
48          RtPublicMembersTest.ORG
49      );
50  
51      /**
52       * Public member URL for test user in test org.
53       */
54      private static final String MEMBER_URL = String.format(
55          "%s/%s",
56          RtPublicMembersTest.MEMBERS_URL,
57          RtPublicMembersTest.USERNAME
58      );
59  
60      /**
61       * RtPublicMembers can fetch its organization.
62       * @throws IOException If there is an I/O problem
63       */
64      @Test
65      void fetchesOrg() throws IOException {
66          final Organization org = RtPublicMembersTest.organization();
67          MatcherAssert.assertThat(
68              "Values are not equal",
69              new RtPublicMembers(new FakeRequest(), org).org(),
70              Matchers.equalTo(org)
71          );
72      }
73  
74      /**
75       * RtPublicMembers can conceal a user's membership in the organization.
76       * @throws IOException If there is an I/O problem
77       */
78      @Test
79      void concealsMembers() throws IOException {
80          try (
81              MkContainer container = new MkGrizzlyContainer()
82                  .next(new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT))
83                  .next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR))
84                  .start(RandomPort.port())
85          ) {
86              final RtPublicMembers members = new RtPublicMembers(
87                  new ApacheRequest(container.home()),
88                  RtPublicMembersTest.organization()
89              );
90              members.conceal(RtPublicMembersTest.user());
91              final MkQuery req = container.take();
92              MatcherAssert.assertThat(
93                  "Values are not equal",
94                  req.method(),
95                  Matchers.equalTo(Request.DELETE)
96              );
97              MatcherAssert.assertThat(
98                  "Values are not equal",
99                  req.body(),
100                 Matchers.is(Matchers.emptyOrNullString())
101             );
102             MatcherAssert.assertThat(
103                 "String does not end with expected value",
104                 req.uri().toString(),
105                 Matchers.endsWith(RtPublicMembersTest.MEMBER_URL)
106             );
107             Assertions.assertThrows(
108                 AssertionError.class,
109                 () -> members.conceal(RtPublicMembersTest.user())
110             );
111             container.stop();
112         }
113     }
114 
115     /**
116      * RtPublicMembers can publicize the membership of
117      * a user in the organization.
118      * @throws IOException If there is an I/O problem
119      */
120     @Test
121     void publicizesMembers() throws IOException {
122         try (MkContainer container = new MkGrizzlyContainer()
123             .next(new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT))
124             .next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR))
125             .start(RandomPort.port())
126         ) {
127             final RtPublicMembers members = new RtPublicMembers(
128                 new ApacheRequest(container.home()),
129                 RtPublicMembersTest.organization()
130             );
131             members.publicize(RtPublicMembersTest.user());
132             final MkQuery req = container.take();
133             MatcherAssert.assertThat(
134                 "Values are not equal",
135                 req.method(),
136                 Matchers.equalTo(Request.PUT)
137             );
138             MatcherAssert.assertThat(
139                 "String does not end with expected value",
140                 req.uri().toString(),
141                 Matchers.endsWith(RtPublicMembersTest.MEMBER_URL)
142             );
143             Assertions.assertThrows(
144                 AssertionError.class,
145                 () -> members.publicize(RtPublicMembersTest.user())
146             );
147             container.stop();
148         }
149     }
150 
151     /**
152      * RtPublicMembers can check whether a user
153      * is a public member of the organization.
154      * @throws IOException If there is an I/O problem
155      */
156     @Test
157     void checkPublicMembership() throws IOException {
158         try (
159             MkContainer container = new MkGrizzlyContainer()
160                 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_NOT_FOUND))
161                 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_NOT_FOUND))
162                 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_NO_CONTENT))
163                 .next(
164                     new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR)
165                 )
166                 .start(RandomPort.port())
167         ) {
168             final RtPublicMembers members = new RtPublicMembers(
169                 new ApacheRequest(container.home()),
170                 RtPublicMembersTest.organization()
171             );
172             members.contains(RtPublicMembersTest.user());
173             final MkQuery req = container.take();
174             MatcherAssert.assertThat(
175                 "Values are not equal",
176                 req.method(),
177                 Matchers.equalTo(Request.GET)
178             );
179             MatcherAssert.assertThat(
180                 "String does not end with expected value",
181                 req.uri().toString(),
182                 Matchers.endsWith(RtPublicMembersTest.MEMBER_URL)
183             );
184             MatcherAssert.assertThat(
185                 "404 is interpreted as the user not being a public member",
186                 !members.contains(RtPublicMembersTest.user()),
187                 Matchers.is(true)
188             );
189             MatcherAssert.assertThat(
190                 "204 is interpreted as the user being a public member",
191                 members.contains(RtPublicMembersTest.user()),
192                 Matchers.is(true)
193             );
194             Assertions.assertThrows(
195                 AssertionError.class,
196                 () -> members.contains(RtPublicMembersTest.user())
197             );
198             container.stop();
199         }
200     }
201 
202     /**
203      * RtPublicMembers can list the public members of the organization.
204      * @throws IOException If there is an I/O problem
205      */
206     @Test
207     void iteratesPublicMembers() throws IOException {
208         try (
209             MkContainer container = new MkGrizzlyContainer()
210                 .next(
211                     new MkAnswer.Simple(
212                         HttpURLConnection.HTTP_OK,
213                         "[{\"login\":\"octobat\"}]"
214                     )
215                 )
216                 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR))
217                 .start(RandomPort.port())
218         ) {
219             final RtPublicMembers members = new RtPublicMembers(
220                 new ApacheRequest(container.home()),
221                 RtPublicMembersTest.organization()
222             );
223             members.iterate().iterator().next();
224             final MkQuery req = container.take();
225             MatcherAssert.assertThat(
226                 "Values are not equal",
227                 req.method(),
228                 Matchers.equalTo(Request.GET)
229             );
230             MatcherAssert.assertThat(
231                 "String does not end with expected value",
232                 req.uri().toString(),
233                 Matchers.endsWith(RtPublicMembersTest.MEMBERS_URL)
234             );
235             Assertions.assertThrows(
236                 AssertionError.class,
237                 () -> members.iterate().iterator().next()
238             );
239             container.stop();
240         }
241     }
242 
243     /**
244      * Get test organization.
245      * @return Organization
246      * @throws IOException If there is an I/O problem
247      */
248     private static Organization organization() throws IOException {
249         return new MkOrganization(new MkStorage.InFile(), RtPublicMembersTest.ORG);
250     }
251 
252     /**
253      * Get test user.
254      * @return User
255      * @throws IOException If there is an I/O problem
256      */
257     private static User user() throws IOException {
258         return new MkGitHub().users().get(RtPublicMembersTest.USERNAME);
259     }
260 }