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.MkStorage;
9   import com.jcabi.http.Request;
10  import com.jcabi.http.mock.MkAnswer;
11  import com.jcabi.http.mock.MkContainer;
12  import com.jcabi.http.mock.MkGrizzlyContainer;
13  import com.jcabi.http.mock.MkQuery;
14  import com.jcabi.http.request.JdkRequest;
15  import jakarta.json.Json;
16  import jakarta.json.JsonValue;
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.Test;
22  import org.junit.jupiter.api.extension.ExtendWith;
23  import org.mockito.Mockito;
24  
25  /**
26   * Tests for {@link RtCollaborators}.
27   * @since 0.8
28   * @checkstyle MultipleStringLiteralsCheck (200 lines)
29   * @checkstyle ClassDataAbstractionCouplingCheck (200 lines)
30   */
31  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
32  @ExtendWith(RandomPort.class)
33  final class RtCollaboratorsTest {
34  
35      /**
36       * RtCollaborators can iterate over a list of collaborators.
37       * @throws Exception if any error occurs.
38       */
39      @Test
40      void canIterate() throws Exception {
41          try (MkContainer container = new MkGrizzlyContainer().next(
42              new MkAnswer.Simple(
43                  HttpURLConnection.HTTP_OK,
44                  Json.createArrayBuilder()
45                      .add(RtCollaboratorsTest.json("octocat"))
46                      .add(RtCollaboratorsTest.json("dummy"))
47                      .build().toString()
48              )
49          ).start(RandomPort.port())) {
50              final Collaborators users = new RtCollaborators(
51                  new JdkRequest(container.home()),
52                  RtCollaboratorsTest.repo()
53              );
54              MatcherAssert.assertThat(
55                  "Collection size is incorrect",
56                  users.iterate(),
57                  Matchers.iterableWithSize(2)
58              );
59          }
60      }
61  
62      /**
63       * User can be added to a repo as a collaborator.
64       * @throws Exception if any error occurs.
65       */
66      @Test
67      void userCanBeAddedAsCollaborator() throws Exception {
68          try (MkContainer container = new MkGrizzlyContainer().next(
69              new MkAnswer.Simple(
70                  HttpURLConnection.HTTP_NO_CONTENT,
71                  Json.createArrayBuilder()
72                      .add(RtCollaboratorsTest.json("octocat2"))
73                      .add(RtCollaboratorsTest.json("dummy"))
74                      .build().toString()
75              )
76          ).start(RandomPort.port())) {
77              final Collaborators users = new RtCollaborators(
78                  new JdkRequest(container.home()),
79                  RtCollaboratorsTest.repo()
80              );
81              users.add("dummy1");
82              final MkQuery query = container.take();
83              MatcherAssert.assertThat(
84                  "Values are not equal",
85                  query.method(),
86                  Matchers.equalTo(Request.PUT)
87              );
88              container.stop();
89          }
90      }
91  
92      /**
93       * User can be checked for being a collaborator.
94       * @throws Exception if any error occurs.
95       */
96      @Test
97      void userCanBeTestForBeingCollaborator() throws Exception {
98          try (
99              MkContainer container = new MkGrizzlyContainer().next(
100                 new MkAnswer.Simple(
101                     HttpURLConnection.HTTP_NO_CONTENT,
102                     Json.createArrayBuilder()
103                         .add(RtCollaboratorsTest.json("octocat2"))
104                         .add(RtCollaboratorsTest.json("dummy"))
105                         .build().toString()
106                 )
107             ).start(RandomPort.port())
108         ) {
109             final Collaborators users = new RtCollaborators(
110                 new JdkRequest(container.home()),
111                 RtCollaboratorsTest.repo()
112             );
113             MatcherAssert.assertThat(
114                 "Values are not equal",
115                 users.isCollaborator("octocat2"),
116                 Matchers.equalTo(true)
117             );
118             container.stop();
119         }
120     }
121 
122     /**
123      * User can be removed from a list of collaborators.
124      * @throws Exception if any error occurs.
125      */
126     @Test
127     void userCanBeRemoved() throws Exception {
128         try (
129             MkContainer container = new MkGrizzlyContainer().next(
130                 new MkAnswer.Simple(
131                     HttpURLConnection.HTTP_NO_CONTENT,
132                     Json.createArrayBuilder()
133                         .add(RtCollaboratorsTest.json("octocat2"))
134                         .add(RtCollaboratorsTest.json("dummy"))
135                         .build().toString()
136                 )
137             ).start(RandomPort.port())
138         ) {
139             final Collaborators users = new RtCollaborators(
140                 new JdkRequest(container.home()),
141                 RtCollaboratorsTest.repo()
142             );
143             users.remove("dummy");
144             final MkQuery query = container.take();
145             MatcherAssert.assertThat(
146                 "Values are not equal",
147                 query.method(),
148                 Matchers.equalTo(Request.DELETE)
149             );
150             container.stop();
151         }
152     }
153 
154     /**
155      * Create and return JsonObject to test.
156      * @param login Username to login
157      * @return JsonObject
158      */
159     private static JsonValue json(final String login) {
160         return Json.createObjectBuilder()
161             .add("login", login)
162             .build();
163     }
164 
165     /**
166      * Create and return repo for testing.
167      * @return Repo
168      */
169     private static Repo repo() throws IOException {
170         final Repo repo = Mockito.mock(Repo.class);
171         Mockito.doReturn(new Coordinates.Simple("test", "collaboratorrepo"))
172             .when(repo).coordinates();
173         Mockito.doReturn(new MkGitHub(new MkStorage.InFile(), "userLogin"))
174             .when(repo).github();
175         return repo;
176     }
177 }