Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c5e97420c397c31512a66f36a4efc974c6ae9925 (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
/******************************************************************************
 *  Copyright (c) 2015 Jon Ander Peñalba
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Jon Ander Peñalba - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.tests;

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

import java.io.IOException;

import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.User;
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.client.PageIterator;
import org.eclipse.egit.github.core.service.StargazerService;
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 StargazerService}
 */
@RunWith(MockitoJUnitRunner.class)
public class StargazerServiceTest {

	@Mock
	private GitHubClient client;

	@Mock
	private GitHubResponse response;

	private StargazerService 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 StargazerService(client);
		repo = new RepositoryId("o", "n");
	}

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

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

	/**
	 * Get starred
	 *
	 * @throws IOException
	 */
	@Test
	public void getCurrentStarred() throws IOException {
		service.getStarred();
		GitHubRequest request = new GitHubRequest();
		request.setUri(Utils.page("/user/starred"));
		verify(client).get(request);
	}

	/**
	 * Get starred with null name
	 *
	 * @throws IOException
	 */
	@Test(expected = IllegalArgumentException.class)
	public void getStarredNullName() throws IOException {
		service.getStarred(null);
	}

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

	/**
	 * Get starred
	 *
	 * @throws IOException
	 */
	@Test
	public void getStarred() throws IOException {
		service.getStarred("auser");
		GitHubRequest request = new GitHubRequest();
		request.setUri(Utils.page("/users/auser/starred"));
		verify(client).get(request);
	}

	/**
	 * Is starring
	 *
	 * @throws IOException
	 */
	@Test
	public void isStarring() throws IOException {
		service.isStarring(repo);
		GitHubRequest request = new GitHubRequest();
		request.setUri("/user/starred/o/n");
		verify(client).get(request);
	}

	/**
	 * Star repository
	 *
	 * @throws IOException
	 */
	@Test
	public void star() throws IOException {
		service.star(repo);
		verify(client).put("/user/starred/o/n");
	}

	/**
	 * Unstar repository
	 *
	 * @throws IOException
	 */
	@Test
	public void unstar() throws IOException {
		service.unstar(repo);
		verify(client).delete("/user/starred/o/n");
	}

	/**
	 * Page stargazes
	 *
	 * @throws IOException
	 */
	@Test
	public void pageStargazers() throws IOException {
		PageIterator<User> iter = service.pageStargazers(repo);
		assertNotNull(iter);
		assertTrue(iter.hasNext());
		assertEquals(Utils.page("/repos/o/n/stargazers"), iter.getRequest()
				.generateUri());
	}

	/**
	 * Page starred
	 *
	 * @throws IOException
	 */
	@Test
	public void pageCurrentStarred() throws IOException {
		PageIterator<Repository> iter = service.pageStarred();
		assertNotNull(iter);
		assertTrue(iter.hasNext());
		assertEquals(Utils.page("/user/starred"), iter.getRequest()
				.generateUri());
	}

	/**
	 * Page starred
	 *
	 * @throws IOException
	 */
	@Test
	public void pageStarred() throws IOException {
		PageIterator<Repository> iter = service.pageStarred("auser");
		assertNotNull(iter);
		assertTrue(iter.hasNext());
		assertEquals(Utils.page("/users/auser/starred"), iter.getRequest()
				.generateUri());
	}
}

Back to the top