1 /**
2 * Copyright (c) 2013-2023, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 package com.jcabi.github;
32
33 import com.jcabi.github.mock.MkGithub;
34 import com.jcabi.github.mock.MkStorage;
35 import com.jcabi.http.Request;
36 import com.jcabi.http.mock.MkAnswer;
37 import com.jcabi.http.mock.MkContainer;
38 import com.jcabi.http.mock.MkGrizzlyContainer;
39 import com.jcabi.http.mock.MkQuery;
40 import com.jcabi.http.request.JdkRequest;
41 import java.net.HttpURLConnection;
42 import javax.json.Json;
43 import javax.json.JsonValue;
44 import org.hamcrest.MatcherAssert;
45 import org.hamcrest.Matchers;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.mockito.Mockito;
49
50 /**
51 * Tests for {@link RtCollaborators}.
52 * @author Aleksey Popov (alopen@yandex.ru)
53 * @version $Id: 93f6316755a713d6678d7c0a59bdfedf521eb15d $
54 * @since 0.8
55 * @checkstyle MultipleStringLiteralsCheck (200 lines)
56 * @checkstyle ClassDataAbstractionCouplingCheck (200 lines)
57 */
58 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
59 public final class RtCollaboratorsTest {
60
61 /**
62 * The rule for skipping test if there's BindException.
63 * @checkstyle VisibilityModifierCheck (3 lines)
64 */
65 @Rule
66 public final transient RandomPort resource = new RandomPort();
67
68 /**
69 * RtCollaborators can iterate over a list of collaborators.
70 * @throws Exception if any error occurs.
71 */
72 @Test
73 public void canIterate() throws Exception {
74 try (final MkContainer container = new MkGrizzlyContainer().next(
75 new MkAnswer.Simple(
76 HttpURLConnection.HTTP_OK,
77 Json.createArrayBuilder()
78 .add(RtCollaboratorsTest.json("octocat"))
79 .add(RtCollaboratorsTest.json("dummy"))
80 .build().toString()
81 )
82 ).start(this.resource.port())) {
83 final Collaborators users = new RtCollaborators(
84 new JdkRequest(container.home()),
85 this.repo()
86 );
87 MatcherAssert.assertThat(
88 users.iterate(),
89 Matchers.<User>iterableWithSize(2)
90 );
91 }
92 }
93
94 /**
95 * User can be added to a repo as a collaborator.
96 * @throws Exception if any error occurs.
97 */
98 @Test
99 public void userCanBeAddedAsCollaborator() throws Exception {
100 try (final MkContainer container = new MkGrizzlyContainer().next(
101 new MkAnswer.Simple(
102 HttpURLConnection.HTTP_NO_CONTENT,
103 Json.createArrayBuilder()
104 .add(RtCollaboratorsTest.json("octocat2"))
105 .add(RtCollaboratorsTest.json("dummy"))
106 .build().toString()
107 )
108 ).start(this.resource.port())) {
109 final Collaborators users = new RtCollaborators(
110 new JdkRequest(container.home()),
111 this.repo()
112 );
113 users.add("dummy1");
114 final MkQuery query = container.take();
115 MatcherAssert.assertThat(
116 query.method(),
117 Matchers.equalTo(Request.PUT)
118 );
119 container.stop();
120 }
121 }
122
123 /**
124 * User can be checked for being a collaborator.
125 * @throws Exception if any error occurs.
126 */
127 @Test
128 public void userCanBeTestForBeingCollaborator() throws Exception {
129 try (
130 final MkContainer container = new MkGrizzlyContainer().next(
131 new MkAnswer.Simple(
132 HttpURLConnection.HTTP_NO_CONTENT,
133 Json.createArrayBuilder()
134 .add(RtCollaboratorsTest.json("octocat2"))
135 .add(RtCollaboratorsTest.json("dummy"))
136 .build().toString()
137 )
138 ).start(this.resource.port())
139 ) {
140 final Collaborators users = new RtCollaborators(
141 new JdkRequest(container.home()),
142 this.repo()
143 );
144 MatcherAssert.assertThat(
145 users.isCollaborator("octocat2"),
146 Matchers.equalTo(true)
147 );
148 container.stop();
149 }
150 }
151
152 /**
153 * User can be removed from a list of collaborators.
154 * @throws Exception if any error occurs.
155 */
156 @Test
157 public void userCanBeRemoved() throws Exception {
158 try (
159 final MkContainer container = new MkGrizzlyContainer().next(
160 new MkAnswer.Simple(
161 HttpURLConnection.HTTP_NO_CONTENT,
162 Json.createArrayBuilder()
163 .add(RtCollaboratorsTest.json("octocat2"))
164 .add(RtCollaboratorsTest.json("dummy"))
165 .build().toString()
166 )
167 ).start(this.resource.port())
168 ) {
169 final Collaborators users = new RtCollaborators(
170 new JdkRequest(container.home()),
171 this.repo()
172 );
173 users.remove("dummy");
174 final MkQuery query = container.take();
175 MatcherAssert.assertThat(
176 query.method(),
177 Matchers.equalTo(Request.DELETE)
178 );
179 container.stop();
180 }
181 }
182
183 /**
184 * Create and return JsonObject to test.
185 * @param login Username to login
186 * @return JsonObject
187 */
188 private static JsonValue json(final String login) {
189 return Json.createObjectBuilder()
190 .add("login", login)
191 .build();
192 }
193
194 /**
195 * Create and return repo for testing.
196 * @return Repo
197 * @throws Exception If some problem inside
198 */
199 private Repo repo() throws Exception {
200 final Repo repo = Mockito.mock(Repo.class);
201 Mockito.doReturn(new Coordinates.Simple("test", "collaboratorrepo"))
202 .when(repo).coordinates();
203 Mockito.doReturn(new MkGithub(new MkStorage.InFile(), "userLogin"))
204 .when(repo).github();
205 return repo;
206 }
207 }