Skip to main content
summaryrefslogtreecommitdiffstats
blob: c6abb018f40e792700288d9356701ae3351238b2 (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
/*******************************************************************************
 /*******************************************************************************
 * Copyright (c) 2011, 2015 Tasktop Technologies and others.
 * 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:
 *     Tasktop Technologies - initial API and implementation
 *     Sascha Scholz (SAP) - improvements
 *     Francois Chouinard (Ericsson)  - Bug 414219 Add new Test
 *     Jacques Bouthillier (Ericsson) - Fix comments for Bug 414219
 *     Jacques Bouthillier (Ericsson) - Bug 414253 Adjust some Test
 *******************************************************************************/

package org.eclipse.mylyn.gerrit.tests.core.client;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.EnumSet;
import java.util.List;

import org.apache.commons.httpclient.Cookie;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.mylyn.commons.net.AbstractWebLocation;
import org.eclipse.mylyn.commons.net.WebLocation;
import org.eclipse.mylyn.commons.net.WebUtil;
import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil;
import org.eclipse.mylyn.gerrit.tests.support.GerritFixture;
import org.eclipse.mylyn.gerrit.tests.support.GerritHarness;
import org.eclipse.mylyn.internal.gerrit.core.GerritUtil;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritAuthenticationState;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritClient;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritClient212;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritConfiguration;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritException;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritSystemInfo;
import org.eclipse.mylyn.internal.gerrit.core.client.compat.PatchScriptX;
import org.eclipse.mylyn.internal.gerrit.core.client.data.GerritQueryResult;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.verification.VerificationMode;

import com.google.common.collect.ImmutableList;
import com.google.gerrit.reviewdb.Account;
import com.google.gerrit.reviewdb.Patch.ChangeType;
import com.google.gerrit.reviewdb.Patch.Key;
import com.google.gerrit.reviewdb.PatchSet.Id;

import junit.framework.TestCase;

/**
 * @author Steffen Pingel
 * @author Sascha Scholz
 * @author Francois Chouinard
 * @author Jacques Bouthillier
 */
public class GerritClientTest extends TestCase {
	public class TestGerritClient extends GerritClient212 {

		public TestGerritClient(TaskRepository repository, AbstractWebLocation location) {
			super(repository, GerritFixture.current().getGerritVersion());
			initialize(location, null, null, null, null);
		}

		@Override
		protected byte[] fetchBinaryContent(String url, IProgressMonitor monitor) throws GerritException {
			return super.fetchBinaryContent(url, monitor);
		}

		@Override
		protected void fetchRightBinaryContent(PatchScriptX patchScript, Key key, Id rightId, IProgressMonitor monitor)
				throws GerritException {
			super.fetchRightBinaryContent(patchScript, key, rightId, monitor);
		}

		@Override
		protected void fetchLeftBinaryContent(PatchScriptX patchScript, Key key, Id leftId, IProgressMonitor monitor)
				throws GerritException {
			super.fetchLeftBinaryContent(patchScript, key, leftId, monitor);
		}

		@Override
		protected String getUrlForPatchSet(Key key, Id id) throws GerritException {
			return super.getUrlForPatchSet(key, id);
		}

		@Override
		protected String getUrlForPatchSetOrBase(Key key, Id id) throws GerritException {
			return super.getUrlForPatchSetOrBase(key, id);
		}
	}

	private static final String GET_LABELS_OPTION = "LABELS"; //$NON-NLS-1$

	private static final String GET_DETAILED_ACCOUNTS_OPTION = "DETAILED_ACCOUNTS"; //$NON-NLS-1$

	private GerritHarness harness;

	private GerritClient client;

	@Override
	@Before
	public void setUp() throws Exception {
		harness = GerritFixture.current().harness();
		client = harness.client();
	}

	@Override
	@After
	public void tearDown() throws Exception {
		harness.dispose();
	}

	@Test
	public void testRefreshConfig() throws Exception {
		GerritConfiguration config = client.refreshConfig(new NullProgressMonitor());
		assertNotNull(config);
		assertNotNull(config.getGerritConfig());
		assertNotNull(config.getProjects());
		assertNotNull(config.getGerritConfig().getWildProject());
		assertEquals("All-Projects", config.getGerritConfig().getWildProject().toString());
		if (client.getVersion().getMinor() >= 12) {
			assertNotNull(config.getGerritConfig().getSchemes());
		}
	}

	@Test
	public void testGetAccount() throws Exception {
		if (!GerritFixture.current().canAuthenticate()) {
			return; // skip
		}
		Account account = client.getAccount(new NullProgressMonitor());
		assertEquals(CommonTestUtil.getShortUserName(harness.readCredentials()), account.getUserName());
	}

	@Test
	public void testGetAccountAnonymous() throws Exception {
		client = harness.clientAnonymous();
		try {
			client.getAccount(new NullProgressMonitor());
			fail("Expected GerritException");
		} catch (GerritException e) {
			assertEquals("Not Signed In", e.getMessage());
		}
	}

	@Test
	public void testGetInfo() throws Exception {
		GerritSystemInfo info = client.getInfo(new NullProgressMonitor());
		if (GerritFixture.current().canAuthenticate()) {
			assertEquals(CommonTestUtil.getShortUserName(harness.readCredentials()), info.getFullName());
		} else {
			assertEquals("Anonymous", info.getFullName());
		}
	}

	@Test
	public void testInvalidXrsfKey() throws Exception {
		if (!GerritFixture.current().canAuthenticate()) {
			return;
		}

		WebLocation location = harness.location();
		GerritAuthenticationState authState = new GerritAuthenticationState();
		authState.setCookie(new Cookie(WebUtil.getHost(location.getUrl()), "xrsfKey", "invalid"));
		client = GerritClient.create(null, location, null, authState, "invalid", null);
		client.getAccount(null);
	}

	@Test
	public void testGetVersion() throws Exception {
		assertEquals(GerritFixture.current().getGerritVersion(), client.getVersion(new NullProgressMonitor()));
	}

	private List<GerritQueryResult> executeQuery(String query) throws GerritException {
		return client.getRestClient().executeQuery(new NullProgressMonitor(), query);
	}

	private List<GerritQueryResult> executeQuery(String query, List<String> options) throws GerritException {
		return client.getRestClient().executeQuery(new NullProgressMonitor(), query, options);
	}

	@Test
	public void testExecuteQueryWithoutOption() throws Exception {
		String query = "status:open";
		List<GerritQueryResult> results = executeQuery(query);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryWithNullOption() throws Exception {
		String query = "status:open";
		List<String> options = null;
		List<GerritQueryResult> results = executeQuery(query, options);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryWithEmptyList() throws Exception {
		String query = "status:open";
		List<String> options = ImmutableList.of();
		List<GerritQueryResult> results = executeQuery(query, options);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryWithEmptyOption() throws Exception {
		String query = "status:open";
		List<String> options = ImmutableList.of("");
		List<GerritQueryResult> results = executeQuery(query, options);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryWithOption() throws Exception {
		String query = "status:open";
		List<String> options = ImmutableList.of(GET_LABELS_OPTION);
		List<GerritQueryResult> results = executeQuery(query, options);
		assertNotNull(results);
		for (int index = 0; index < results.size(); index++) {
			assertNotNull(results.get(index));
			assertNotNull(results.get(index).getReviewLabel());
		}
	}

	@Test
	public void testExecuteQueryWithMultipleOptions() throws Exception {
		String query = "status:open";
		List<String> options = ImmutableList.of(GET_LABELS_OPTION, GET_DETAILED_ACCOUNTS_OPTION);
		List<GerritQueryResult> results = executeQuery(query, options);
		assertNotNull(results);
		for (int index = 0; index < results.size(); index++) {
			assertNotNull(results.get(index));
			assertNotNull(results.get(index).getReviewLabel());
		}
	}

	@Test
	public void testExecuteQueryAllMerged() throws GerritException {
		String query = "status:merged";
		List<String> options = ImmutableList.of(GET_LABELS_OPTION);
		List<GerritQueryResult> results = executeQuery(query, options);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryAllAbandoned() throws GerritException {
		String query = "status:abandoned";
		List<String> options = ImmutableList.of(GET_LABELS_OPTION);
		List<GerritQueryResult> results = executeQuery(query, options);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryisStarred() throws GerritException {
		String query = "is:starred status:open";
		List<String> options = ImmutableList.of(GET_LABELS_OPTION);
		List<GerritQueryResult> results = executeQuery(query, options);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryHasComments() throws GerritException {
		String query = "has:draft";
		List<GerritQueryResult> results = executeQuery(query);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryDraftsCommentsReviews() throws GerritException {
		String query = "has:draft";
		List<GerritQueryResult> results = executeQuery(query);
		assertNotNull(results);
	}

	@Test
	public void testExecuteQueryDraftsReviews() throws GerritException {
		String query = "is:draft";
		List<GerritQueryResult> results = executeQuery(query);
		assertNotNull(results);
	}

	@Test
	public void testToReviewId() throws GerritException {
		assertEquals("123", client.toReviewId("123", null));
		assertEquals("1", client.toReviewId("1", null));
	}

	@Test
	public void testToReviewIdWithInvalidId() {
		try {
			client.toReviewId("invalidid", null);
			fail("Expected GerritException");
		} catch (GerritException e) {
			assertEquals("invalidid is not a valid review ID", e.getMessage());

		}
	}

	@Test
	public void testToReviewIdWithChangeId() throws Exception {
		harness.ensureOneReviewExists();
		List<GerritQueryResult> results = executeQuery("status:open");
		GerritQueryResult result = results.get(0);
		String reviewId = Integer.toString(result.getNumber());
		String changeId = GerritUtil.toChangeId(result.getId());
		assertEquals(reviewId, client.toReviewId(changeId, null));
	}

	@Test
	public void testIsZippedContent() throws Exception {
		assertTrue(GerritClient.isZippedContent("PK\u0003\u0004somezippedcontent".getBytes()));
		assertFalse(GerritClient.isZippedContent("PK\u0003notzippedcontent".getBytes()));
		assertFalse(GerritClient.isZippedContent("PKnotzippedcontent".getBytes()));
		assertFalse(GerritClient.isZippedContent("notzippedcontent".getBytes()));
		assertFalse(GerritClient.isZippedContent("PK".getBytes()));
		assertFalse(GerritClient.isZippedContent("".getBytes()));
		assertFalse(GerritClient.isZippedContent(null));
	}

	@Test
	public void testFetchLeftBinaryContent() throws Exception {
		fetchBinaryContentForSide(false);
	}

	@Test
	public void testFetchRightBinaryContent() throws Exception {
		fetchBinaryContentForSide(true);
	}

	private void fetchBinaryContentForSide(boolean rightSide) throws Exception, GerritException {
		Id ps2 = Id.fromRef("refs/changes/34/1234/2");
		Id ps4 = Id.fromRef("refs/changes/34/1234/4");
		Key key = new Key(ps4, "/mylyn/gerrit/File.jpg");
		PatchScriptX script = mock(PatchScriptX.class);

		for (ChangeType type : ChangeType.values()) {
			TestGerritClient client = createSpy();
			doReturn(null).when(client).fetchBinaryContent(any(String.class), any(IProgressMonitor.class));
			when(script.getChangeType()).thenReturn(type);
			VerificationMode fetchBinaryContentExpected = times(1);
			if (rightSide) {
				client.fetchRightBinaryContent(script, key, ps2, new NullProgressMonitor());
				if (type == ChangeType.DELETED) {
					fetchBinaryContentExpected = never();
				}
			} else {
				client.fetchLeftBinaryContent(script, key, ps2, new NullProgressMonitor());
				if (!EnumSet.of(ChangeType.DELETED, ChangeType.MODIFIED).contains(type)) {
					fetchBinaryContentExpected = never();
				}
			}
			verify(client, fetchBinaryContentExpected).fetchBinaryContent(any(String.class),
					any(IProgressMonitor.class));
		}
	}

	@Test
	public void testGetUrlForPatchSet() throws Exception {
		TestGerritClient client = createSpy();
		Id ps2 = Id.fromRef("refs/changes/34/1234/2");
		Id ps4 = Id.fromRef("refs/changes/34/1234/4");
		Key key = new Key(ps4, "/mylyn/gerrit/File.jpg");
		assertEquals(encode("1234,4,/mylyn/gerrit/File.jpg^1"), client.getUrlForPatchSetOrBase(key, null));
		assertEquals(encode("1234,2,/mylyn/gerrit/File.jpg^0"), client.getUrlForPatchSetOrBase(key, ps2));
		assertEquals(encode("1234,4,/mylyn/gerrit/File.jpg^0"), client.getUrlForPatchSetOrBase(key, ps4));
		assertEquals(encode("1234,2,/mylyn/gerrit/File.jpg^0"), client.getUrlForPatchSet(key, ps2));
		assertEquals(encode("1234,4,/mylyn/gerrit/File.jpg^0"), client.getUrlForPatchSet(key, ps4));
	}

	private TestGerritClient createSpy() throws Exception {
		return spy(new TestGerritClient(GerritFixture.current().repository(), GerritFixture.current().location()));
	}

	private String encode(String s) throws UnsupportedEncodingException {
		return URLEncoder.encode(s, "UTF-8");
	}
}

Back to the top