Skip to main content
summaryrefslogtreecommitdiffstats
blob: c30a373d487c8a03b9aa69d22824f61b3328019f (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
/*******************************************************************************
 * Copyright (c) 2011, 2015 Christian Trutz and others.
 * 
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * https://www.eclipse.org/legal/epl-2.0
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 *     Christian Trutz - initial API and implementation
 *     Tasktop Technologies - ongoing maintenance
 *******************************************************************************/

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

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.eclipse.core.runtime.AssertionFailedException;
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.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.JsonEntity;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.Request;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.Request.HttpMethod;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.google.gson.reflect.TypeToken;

/**
 * Unit tests for {@link GerritHttpClient}.
 *
 * @author Christian Trutz
 */
@RunWith(MockitoJUnitRunner.class)
public class GerritHttpClientTest {
	public class TestGerritHttpClient extends GerritHttpClient {
		private final int code;

		private TestGerritHttpClient(AbstractWebLocation location, int code) {
			super(location, GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
			this.code = code;
		}

		@Override
		public int execute(org.apache.commons.httpclient.HttpMethod method, IProgressMonitor monitor)
				throws IOException {
			return code;
		}

		@Override
		void requestCredentials(IProgressMonitor monitor, AuthenticationType authenticationType)
				throws GerritLoginException {
		}

		@Override
		String getUrl() {
			return "http://mock";
		}
	}

	@Mock
	AbstractWebLocation abstractWebLocation;

	@Mock
	IProgressMonitor progressMonitor;

	/**
	 * Test {@link GerritHttpClient} constructor with {@code null} argument.
	 */
	@Test(expected = AssertionFailedException.class)
	public void constructorNull() {
		new GerritHttpClient((AbstractWebLocation) null, GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
	}

	/**
	 * Test {@link GerritHttpClient#postJsonRequest(String, JsonEntity, IProgressMonitor)} with {@code null} service URI
	 * argument.
	 */
	@Test(expected = AssertionFailedException.class)
	public void postJsonRequestNullServiceUri() throws IOException, GerritException {
		GerritHttpClient gerritHttpClient = new GerritHttpClient(abstractWebLocation,
				GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
		gerritHttpClient.postJsonRequest(null, new JsonEntity() {
			@Override
			public String getContent() {
				return "[]"; //$NON-NLS-1$
			}
		}, progressMonitor);
	}

	/**
	 * Test {@link GerritHttpClient#postJsonRequest(String, JsonEntity, IProgressMonitor)} with {@code null}
	 * {@link JsonEntity} argument.
	 */
	@Test(expected = AssertionFailedException.class)
	public void postJsonRequestNullJsonEntity() throws IOException, GerritException {
		GerritHttpClient gerritHttpClient = new GerritHttpClient(abstractWebLocation,
				GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
		gerritHttpClient.postJsonRequest("not null", null, progressMonitor); //$NON-NLS-1$
	}

	@Test
	public void restRequestCanReturnBinaryContent() throws IOException {
		// given
		final TypeToken<Byte[]> byteArrayType = new TypeToken<Byte[]>() {
		};
		Request<byte[]> request = new GerritHttpClient(abstractWebLocation,
				GerritCapabilities.MAXIMUM_SUPPORTED_VERSION).new RestRequest<byte[]>(HttpMethod.GET, "serviceUri", //$NON-NLS-1$
						null /*input*/, byteArrayType.getType(), null /*error handler*/);
		HttpMethodBase httpMethodBase = mock(HttpMethodBase.class);
		byte[] binary = "binary".getBytes(); //$NON-NLS-1$
		when(httpMethodBase.getResponseBody()).thenReturn(binary);

		// when
		byte[] result = request.process(httpMethodBase);

		// then
		assertArrayEquals(binary, result);
	}

	@Test
	public void authenticateForm() throws IOException, GerritException {
		GerritHttpClient client = spy(new TestGerritHttpClient(abstractWebLocation, HttpStatus.SC_BAD_REQUEST));
		int result = client.authenticateForm(new AuthenticationCredentials("", ""), new NullProgressMonitor());
		assertEquals(HttpStatus.SC_NOT_FOUND, result);
		InOrder inOrder = inOrder(client);
		inOrder.verify(client).execute(isA(PostMethod.class), any(IProgressMonitor.class));
		inOrder.verify(client).execute(isA(GetMethod.class), any(IProgressMonitor.class));

		client = spy(new TestGerritHttpClient(abstractWebLocation, HttpStatus.SC_METHOD_NOT_ALLOWED));
		result = client.authenticateForm(new AuthenticationCredentials("", ""), new NullProgressMonitor());
		assertEquals(HttpStatus.SC_NOT_FOUND, result);
		inOrder = inOrder(client);
		inOrder.verify(client).execute(isA(PostMethod.class), any(IProgressMonitor.class));
		inOrder.verify(client).execute(isA(GetMethod.class), any(IProgressMonitor.class));

		client = spy(new TestGerritHttpClient(abstractWebLocation, HttpStatus.SC_UNAUTHORIZED));
		result = client.authenticateForm(new AuthenticationCredentials("", ""), new NullProgressMonitor());
		assertEquals(-1, result);
		verify(client).execute(isA(PostMethod.class), any(IProgressMonitor.class));
		verify(client, never()).execute(isA(GetMethod.class), any(IProgressMonitor.class));
	}
}

Back to the top