Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ea5c6044ec2c8e66c181d29d7c05b1b8696fc37e (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/******************************************************************************
 *  Copyright (c) 2011, 2018 GitHub Inc. and others
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *    Michael Mathews (Arizona Board of Regents) - (Bug: 447419)
 *    			 Team Membership API implementation
 *    Singaram Subramanian (Capital One) - (Bug: 529850)
 *    			 User teams across GitHub organizations implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.tests;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.util.Collections;

import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.Team;
import org.eclipse.egit.github.core.TeamMembership;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.GitHubRequest;
import org.eclipse.egit.github.core.client.GitHubResponse;
import org.eclipse.egit.github.core.service.TeamService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

/**
 * Unit tests of {@link TeamService}
 */
@RunWith(MockitoJUnitRunner.class)
public class TeamServiceTest {

	@Mock
	private GitHubClient client;

	@Mock
	private GitHubResponse response;

	private TeamService service;

	private RepositoryId repo;

	/**
	 * Test case set up
	 *
	 * @throws IOException
	 */
	@Before
	public void before() throws IOException {
		doReturn(response).when(client).get(any(GitHubRequest.class));
		service = new TeamService(client);
		repo = new RepositoryId("o", "n");
	}

	/**
	 * Create service using default constructor
	 */
	@Test
	public void constructor() {
		assertNotNull(new TeamService().getClient());
	}

	/**
	 * Get team
	 *
	 * @throws IOException
	 */
	@Test
	public void getTeam() throws IOException {
		service.getTeam(3);
		GitHubRequest request = new GitHubRequest();
		request.setUri("/teams/3");
		verify(client).get(request);
	}

	/**
	 * Get teams with null name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void getTeamsNullName() throws IOException {
		service.getTeams((String) null);
	}

	/**
	 * Get teams with empty name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void getTeamsEmptyName() throws IOException {
		service.getTeams("");
	}

	/**
	 * Get teams
	 *
	 * @throws IOException
	 */
	@Test
	public void getTeams() throws IOException {
		service.getTeams("group");
		GitHubRequest request = new GitHubRequest();
		request.setUri(Utils.page("/orgs/group/teams"));
		verify(client).get(request);
	}

	/**
	 * Create team with null organization
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void createTeamNullOrg() throws IOException {
		service.createTeam(null, new Team().setName("pullers"));
	}

	/**
	 * Create team with empty organization
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void createTeamEmptyOrg() throws IOException {
		service.createTeam("", new Team().setName("pullers"));
	}

	/**
	 * Create team with null team
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void createTeamNullTeam() throws IOException {
		service.createTeam("group", null);
	}

	/**
	 * Create team
	 *
	 * @throws IOException
	 */
	@Test
	public void createTeam() throws IOException {
		service.createTeam("group", new Team().setName("pullers"));
		verify(client).post(eq("/orgs/group/teams"), any(), eq(Team.class));
	}

	/**
	 * Create team with repositories
	 *
	 * @throws IOException
	 */
	@Test
	public void createTeamWithRepos() throws IOException {
		service.createTeam("group", new Team().setName("pullers"),
				Collections.singletonList("repo1"));
		verify(client).post(eq("/orgs/group/teams"), any(), eq(Team.class));
	}

	/**
	 * Edit team null team
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void editTeamNullTeam() throws IOException {
		service.editTeam(null);
	}

	/**
	 * Edit team
	 *
	 * @throws IOException
	 */
	@Test
	public void editTeam() throws IOException {
		Team team = new Team().setId(20);
		service.editTeam(team);
		verify(client).post("/teams/20", team, Team.class);
	}

	/**
	 * Delete team
	 *
	 * @throws IOException
	 */
	@Test
	public void deleteTeam() throws IOException {
		service.deleteTeam(50);
		verify(client).delete("/teams/50");
	}

	/**
	 * Get members
	 *
	 * @throws IOException
	 */
	@Test
	public void getMembers() throws IOException {
		service.getMembers(15);
		GitHubRequest request = new GitHubRequest();
		request.setUri(Utils.page("/teams/15/members"));
		verify(client).get(request);
	}

	/**
	 * Is member with null name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void isMemberNullName() throws IOException {
		service.isMember(2, null);
	}

	/**
	 * Is member with empty name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void isMemberEmptyName() throws IOException {
		service.isMember(2, "");
	}

	/**
	 * Is member
	 *
	 * @throws IOException
	 */
	@Test
	public void isMember() throws IOException {
		service.isMember(5, "abc");
		GitHubRequest request = new GitHubRequest();
		request.setUri("/teams/5/members/abc");
		verify(client).get(request);
	}

	/**
	 * Add member with null name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void addMemberNullName() throws IOException {
		service.addMember(2, null);
	}

	/**
	 * Add member with empty name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void addMemberEmptyName() throws IOException {
		service.addMember(2, "");
	}

	/**
	 * Add member
	 *
	 * @throws IOException
	 */
	@Test
	public void addMember() throws IOException {
		service.addMember(6, "tt");
		verify(client).put("/teams/6/members/tt");
	}

	/**
	 * Remove member with null name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void removeMemberNullName() throws IOException {
		service.removeMember(3, null);
	}

	/**
	 * Remove member with empty name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void removeMemberEmptyName() throws IOException {
		service.removeMember(3, "");
	}

	@Test(expected = IllegalArgumentException.class)
	public void getMembershipNullName() throws IOException {
		service.getMembership(6, null);
	}

	@Test(expected = IllegalArgumentException.class)
	public void getMembershipEmptyName() throws IOException {
		service.getMembership(6, "");
	}

	@Test
	public void getMembership() throws IOException {
		service.getMembership(6, "tt");
		GitHubRequest request = new GitHubRequest();
		request.setUri("/teams/6/memberships/tt");
		verify(client).get(request);
	}

	@Test(expected = IllegalArgumentException.class)
	public void addMembershipNullName() throws IOException {
		service.addMembership(6, null);
	}

	@Test(expected = IllegalArgumentException.class)
	public void addMembershipEmptyName() throws IOException {
		service.addMembership(6, "");
	}

	@Test
	public void addMembership() throws IOException {
		service.addMembership(6, "tt");
		verify(client).put("/teams/6/memberships/tt", null, TeamMembership.class);
	}

	@Test(expected = IllegalArgumentException.class)
	public void removeMembershipNullName() throws IOException {
		service.removeMembership(6, null);
	}

	@Test(expected = IllegalArgumentException.class)
	public void removeMembershipEmptyName() throws IOException {
		service.removeMembership(6, "");
	}

	@Test
	public void removeMembership() throws IOException {
		service.removeMembership(6, "tt");
		verify(client).delete("/teams/6/memberships/tt");
	}

	/**
	 * Remove member
	 *
	 * @throws IOException
	 */
	@Test
	public void removeMember() throws IOException {
		service.removeMember(6, "aa");
		verify(client).delete("/teams/6/members/aa");
	}

	/**
	 * Get repositories
	 *
	 * @throws IOException
	 */
	@Test
	public void getRepositories() throws IOException {
		service.getRepositories(7);
		GitHubRequest request = new GitHubRequest();
		request.setUri(Utils.page("/teams/7/repos"));
		verify(client).get(request);
	}

	/**
	 * Is repository
	 *
	 * @throws IOException
	 */
	@Test
	public void isRepository() throws IOException {
		service.isTeamRepository(8, repo);
		GitHubRequest request = new GitHubRequest();
		request.setUri("/teams/8/repos/o/n");
		verify(client).get(request);
	}

	/**
	 * Add repository
	 *
	 * @throws IOException
	 */
	@Test
	public void addRepository() throws IOException {
		service.addRepository(8, repo);
		verify(client).put("/teams/8/repos/o/n");
	}

	/**
	 * Remove repository
	 *
	 * @throws IOException
	 */
	@Test
	public void removeRepository() throws IOException {
		service.removeRepository(8, repo);
		verify(client).delete("/teams/8/repos/o/n");
	}

	/**
	 * Get teams for repository
	 *
	 * @throws IOException
	 */
	@Test
	public void getRepositoryTeams() throws IOException {
		service.getTeams(repo);
		GitHubRequest request = new GitHubRequest();
		request.setUri(Utils.page("/repos/o/n/teams"));
		verify(client).get(request);
	}

	/**
	 * Get current user's teams across all GitHub organizations
	 *
	 * @throws IOException
	 */
	@Test
	public void getAllTeams() throws IOException {
		service.getTeams();
		GitHubRequest request = new GitHubRequest();
		request.setUri(Utils.page("/user/teams"));
		verify(client).get(request);
	}
}

Back to the top