1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
52
53
54
55
56
57
58 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
59 public final class RtCollaboratorsTest {
60
61
62
63
64
65 @Rule
66 public final transient RandomPort resource = new RandomPort();
67
68
69
70
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
96
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
125
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
154
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
185
186
187
188 private static JsonValue json(final String login) {
189 return Json.createObjectBuilder()
190 .add("login", login)
191 .build();
192 }
193
194
195
196
197
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 }