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
|
/*******************************************************************************
* Copyright (c) 2011, 2014 Christian Trutz 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:
* 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.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.eclipse.core.runtime.AssertionFailedException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.commons.net.AbstractWebLocation;
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.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 {
@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);
}
/**
* 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);
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);
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).new RestRequest<byte[]>(HttpMethod.GET,
"serviceUri", null /*input*/, byteArrayType.getType(), null /*error handler*/); //$NON-NLS-1$
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);
}
}
|