2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.http.test;
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.lang.reflect.Field;
31 import java.net.HttpURLConnection;
33 import java.net.URISyntaxException;
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletResponse;
38 import static org.hamcrest.CoreMatchers.*;
41 import org.onap.aaf.cadi.CadiException;
42 import org.onap.aaf.cadi.LocatorException;
43 import org.onap.aaf.cadi.SecuritySetter;
44 import org.onap.aaf.cadi.client.EClient.Transfer;
45 import org.onap.aaf.cadi.client.Future;
46 import org.onap.aaf.cadi.http.HClient;
47 import org.onap.aaf.cadi.http.HClient.HFuture;
48 import org.onap.aaf.misc.env.APIException;
49 import org.onap.aaf.misc.rosetta.env.RosettaDF;
50 import org.onap.aaf.misc.rosetta.env.RosettaData;
52 public class JU_HClient {
54 @Mock private SecuritySetter<HttpURLConnection> ssMock;
55 @Mock private Transfer transferMock;
56 @Mock private HttpURLConnection hucMock;
57 @Mock private HttpServletResponse respMock;
58 @Mock private RosettaDF<HttpURLConnection> dfMock;
59 @Mock private RosettaData<HttpURLConnection> dataMock;
61 private static final String uriString = "http://example.com:8080/path/to/a/file.txt";
62 private static final String fragment = "fragment";
63 private static final String method = "method";
64 private static final String pathinfo = "pathinfo";
65 private static final String queryParams = "queryParams";
67 private static final String errorString = "error string";
68 private static final String successString = "success string";
70 private static final String tag1 = "tag1";
71 private static final String tag2 = "tag2";
72 private static final String value1 = "value1";
73 private static final String value2 = "value2";
78 public void setup() throws URISyntaxException {
79 MockitoAnnotations.initMocks(this);
81 uri = new URI(uriString);
85 public void accessorsMutatorsTest() throws LocatorException {
86 HClient client = new HClient(ssMock, uri, 0);
87 client.setFragment(fragment);
88 client.setMethod(method);
89 client.setPathInfo(pathinfo);
90 client.setPayload(transferMock);
91 client.setQueryParams(queryParams);
92 assertThat(client.getURI(), is(uri));
93 assertThat(client.timeout(), is(0));
94 assertThat(client.toString(), is("HttpURLConnection Client configured to " + uri.toString()));
98 public void sendTest() throws LocatorException, APIException, URISyntaxException {
100 client = new HClientStub(ssMock, uri, 0, null);
103 client.setPathInfo("/pathinfo");
106 client.setPathInfo("pathinfo");
109 client = new HClientStub(null, uri, 0, null);
112 client.addHeader(tag1, value1);
113 client.addHeader(tag2, value2);
116 client.setPayload(transferMock);
120 @Test(expected = APIException.class)
121 public void sendThrows1Test() throws APIException, LocatorException, URISyntaxException {
122 HClientStub client = new HClientStub(ssMock, new URI("mailto:me@domain.com"), 0, null);
126 @Test(expected = APIException.class)
127 public void sendThrows2Test() throws APIException, LocatorException, URISyntaxException {
128 HClientStub client = new HClientStub(ssMock, new URI("mailto:me@domain.com"), 0, null);
129 client.addHeader(tag1, value1);
130 client.addHeader(tag2, value2);
135 public void futureCreateTest() throws LocatorException, CadiException, IOException {
136 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
137 HFuture<HttpURLConnection> future = (HFuture<HttpURLConnection>) client.futureCreate(HttpURLConnection.class);
139 // Test a bad response code (default 0) without output
140 assertThat(future.get(0), is(false));
141 assertThat(future.body().length(), is(0));
143 // Test a bad response code (default 0) with output
144 ByteArrayInputStream bais = new ByteArrayInputStream(errorString.getBytes());
145 when(hucMock.getInputStream()).thenReturn(bais);
146 assertThat(future.get(0), is(false));
147 assertThat(future.body(), is(errorString));
149 // Test a good response code
150 when(hucMock.getResponseCode()).thenReturn(201);
151 assertThat(future.get(0), is(true));
155 public void futureReadStringTest() throws LocatorException, CadiException, IOException {
156 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
157 Future<String> future = client.futureReadString();
159 // Test a bad response code (default 0) without output
160 assertThat(future.get(0), is(false));
161 assertThat(future.body().length(), is(0));
163 // Test a bad response code (default 0) with output
164 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
165 assertThat(future.get(0), is(false));
166 assertThat(future.body(), is(errorString));
168 // Test a good response code
169 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(successString.getBytes()));
170 when(hucMock.getResponseCode()).thenReturn(200);
171 assertThat(future.get(0), is(true));
172 assertThat(future.body(), is(successString));
176 public void futureReadTest() throws LocatorException, CadiException, IOException, APIException {
177 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
178 Future<HttpURLConnection> future = client.futureRead(dfMock, null);
180 // Test a bad response code (default 0) without output
181 assertThat(future.get(0), is(false));
182 assertThat(future.body().length(), is(0));
184 // Test a bad response code (default 0) with output
185 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
186 assertThat(future.get(0), is(false));
187 assertThat(future.body(), is(errorString));
189 // Test a good response code
190 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(successString.getBytes()));
191 when(dfMock.newData()).thenReturn(dataMock);
192 when(dataMock.in(null)).thenReturn(dataMock);
193 when(dataMock.load((InputStream)any())).thenReturn(dataMock);
194 when(dataMock.asObject()).thenReturn(hucMock);
195 when(dataMock.asString()).thenReturn(successString);
196 when(hucMock.getResponseCode()).thenReturn(200);
197 assertThat(future.get(0), is(true));
198 assertThat(future.body(), is(successString));
202 public void future1Test() throws LocatorException, CadiException, IOException, APIException {
203 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
204 Future<HttpURLConnection> future = client.future(hucMock);
206 // Test a good response code
207 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(successString.getBytes()));
208 when(hucMock.getResponseCode()).thenReturn(200);
209 assertThat(future.get(0), is(true));
210 assertThat(future.body(), is("200"));
212 // Test a bad response code
213 when(hucMock.getResponseCode()).thenReturn(0);
214 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
215 assertThat(future.get(0), is(false));
216 assertThat(future.body(), is(errorString));
220 public void future2Test() throws LocatorException, CadiException, IOException, APIException {
221 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
222 Future<Void> future = client.future(respMock, 200);
224 ServletOutputStream sos = new ServletOutputStream() {
225 @Override public void write(int arg0) throws IOException { }
227 when(respMock.getOutputStream()).thenReturn(sos);
229 // Test a good response code
230 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(successString.getBytes()));
231 when(hucMock.getResponseCode()).thenReturn(200);
232 assertThat(future.get(0), is(true));
233 assertThat(future.body(), is(nullValue()));
235 // Test a bad response code
236 when(hucMock.getResponseCode()).thenReturn(0);
237 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
238 assertThat(future.get(0), is(false));
239 assertThat(future.body(), is(""));
243 public void hfutureTest() throws CadiException, IOException, LocatorException {
244 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
245 HFutureStub future = new HFutureStub(client, hucMock);
246 assertThat(future.get(0), is(false));
248 // Test a bad response code (default 0) with output
249 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
250 assertThat(future.get(0), is(false));
252 assertThat(future.get(0), is(false));
254 when(hucMock.getResponseCode()).thenReturn(200);
255 assertThat(future.get(0), is(true));
257 StringBuilder sb = future.inputStreamToString(new ByteArrayInputStream(errorString.getBytes()));
258 assertThat(sb.toString(), is(errorString));
260 assertThat(future.code(), is(200));
261 assertThat(future.huc(), is(hucMock));
263 assertThat(future.exception(), is(nullValue()));
264 assertThat(future.header("string"), is(nullValue()));
272 public void headerTest() throws LocatorException {
273 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
274 String tag1 = "tag1";
275 String tag2 = "tag2";
276 String value1 = "value1";
277 String value2 = "value2";
278 client.addHeader(tag1, value1);
279 client.addHeader(tag2, value2);
282 @Test(expected = LocatorException.class)
283 public void throws1Test() throws LocatorException {
284 @SuppressWarnings("unused")
285 HClient client = new HClient(ssMock, null, 0);
288 private class HClientStub extends HClient {
289 public HClientStub(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout, HttpURLConnection huc) throws LocatorException {
290 super(ss, uri, connectTimeout);
293 public void setHuc(HttpURLConnection huc) {
296 field = HClient.class.getDeclaredField("huc");
297 field.setAccessible(true);
298 field.set(this, huc);
299 field.setAccessible(false);
300 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
302 fail("Caught an exception: " + e.getMessage());
306 public HttpURLConnection getConnection(URI uri, StringBuilder pi) throws IOException {
311 private class HFutureStub extends HFuture<HttpURLConnection> {
312 public HFutureStub(HClient hClient, HttpURLConnection huc) {
316 @Override public String body() { return null; }
317 public void setHuc(HttpURLConnection huc) { this.huc = huc; }