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.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.Reader;
32 import java.lang.reflect.Field;
33 import java.net.HttpURLConnection;
35 import java.net.URISyntaxException;
38 import javax.servlet.ServletOutputStream;
39 import javax.servlet.http.HttpServletResponse;
41 import static org.hamcrest.CoreMatchers.*;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.LocatorException;
46 import org.onap.aaf.cadi.SecuritySetter;
47 import org.onap.aaf.cadi.client.EClient.Transfer;
48 import org.onap.aaf.cadi.client.Future;
49 import org.onap.aaf.cadi.http.HClient;
50 import org.onap.aaf.cadi.http.HClient.HFuture;
51 import org.onap.aaf.misc.env.APIException;
52 import org.onap.aaf.misc.rosetta.env.RosettaDF;
53 import org.onap.aaf.misc.rosetta.env.RosettaData;
55 public class JU_HClient {
57 @Mock private SecuritySetter<HttpURLConnection> ssMock;
58 @Mock private Transfer transferMock;
59 @Mock private HttpURLConnection hucMock;
60 @Mock private HttpServletResponse respMock;
61 @Mock private RosettaDF<HttpURLConnection> dfMock;
62 @Mock private RosettaData<HttpURLConnection> dataMock;
64 private static final String uriString = "http://example.com:8080/path/to/a/file.txt";
65 private static final String fragment = "fragment";
66 private static final String method = "method";
67 private static final String pathinfo = "pathinfo";
68 private static final String queryParams = "queryParams";
70 private static final String errorString = "error string";
71 private static final String successString = "success string";
73 private static final String tag1 = "tag1";
74 private static final String tag2 = "tag2";
75 private static final String value1 = "value1";
76 private static final String value2 = "value2";
81 public void setup() throws URISyntaxException {
82 MockitoAnnotations.initMocks(this);
84 uri = new URI(uriString);
88 public void accessorsMutatorsTest() throws LocatorException {
89 HClient client = new HClient(ssMock, uri, 0);
90 client.setFragment(fragment);
91 client.setMethod(method);
92 client.setPathInfo(pathinfo);
93 client.setPayload(transferMock);
94 client.setQueryParams(queryParams);
95 assertThat(client.getURI(), is(uri));
96 assertThat(client.timeout(), is(0));
97 assertThat(client.toString(), is("HttpURLConnection Client configured to " + uri.toString()));
101 public void sendTest() throws LocatorException, APIException, URISyntaxException {
103 client = new HClientStub(ssMock, uri, 0, null);
106 client.setPathInfo("/pathinfo");
109 client.setPathInfo("pathinfo");
112 client = new HClientStub(null, uri, 0, null);
115 client.addHeader(tag1, value1);
116 client.addHeader(tag2, value2);
119 client.setPayload(transferMock);
123 @Test(expected = APIException.class)
124 public void sendThrows1Test() throws APIException, LocatorException, URISyntaxException {
125 HClientStub client = new HClientStub(ssMock, new URI("mailto:me@domain.com"), 0, null);
129 @Test(expected = APIException.class)
130 public void sendThrows2Test() throws APIException, LocatorException, URISyntaxException {
131 HClientStub client = new HClientStub(ssMock, new URI("mailto:me@domain.com"), 0, null);
132 client.addHeader(tag1, value1);
133 client.addHeader(tag2, value2);
138 public void futureCreateTest() throws LocatorException, CadiException, IOException {
139 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
140 HFuture<HttpURLConnection> future = (HFuture<HttpURLConnection>) client.futureCreate(HttpURLConnection.class);
142 // Test a bad response code (default 0) without output
143 assertThat(future.get(0), is(false));
144 assertThat(future.body().length(), is(0));
146 // Test a bad response code (default 0) with output
147 ByteArrayInputStream bais = new ByteArrayInputStream(errorString.getBytes());
148 when(hucMock.getInputStream()).thenReturn(bais);
149 assertThat(future.get(0), is(false));
150 assertThat(future.body(), is(errorString));
152 // Test a good response code
153 when(hucMock.getResponseCode()).thenReturn(201);
154 assertThat(future.get(0), is(true));
158 public void futureReadStringTest() throws LocatorException, CadiException, IOException {
159 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
160 Future<String> future = client.futureReadString();
162 // Test a bad response code (default 0) without output
163 assertThat(future.get(0), is(false));
164 assertThat(future.body().length(), is(0));
166 // Test a bad response code (default 0) with output
167 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
168 assertThat(future.get(0), is(false));
169 assertThat(future.body(), is(errorString));
171 // Test a good response code
172 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(successString.getBytes()));
173 when(hucMock.getResponseCode()).thenReturn(200);
174 assertThat(future.get(0), is(true));
175 assertThat(future.body(), is(successString));
179 public void futureReadTest() throws LocatorException, CadiException, IOException, APIException {
180 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
181 Future<HttpURLConnection> future = client.futureRead(dfMock, null);
183 // Test a bad response code (default 0) without output
184 assertThat(future.get(0), is(false));
185 assertThat(future.body().length(), is(0));
187 // Test a bad response code (default 0) with output
188 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
189 assertThat(future.get(0), is(false));
190 assertThat(future.body(), is(errorString));
192 // Test a good response code
193 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(successString.getBytes()));
194 when(dfMock.newData()).thenReturn(dataMock);
195 when(dataMock.in(null)).thenReturn(dataMock);
196 when(dataMock.load((InputStream)any())).thenReturn(dataMock);
197 when(dataMock.asObject()).thenReturn(hucMock);
198 when(dataMock.asString()).thenReturn(successString);
199 when(hucMock.getResponseCode()).thenReturn(200);
200 assertThat(future.get(0), is(true));
201 assertThat(future.body(), is(successString));
205 public void future1Test() throws LocatorException, CadiException, IOException, APIException {
206 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
207 Future<HttpURLConnection> future = client.future(hucMock);
209 // Test a good response code
210 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(successString.getBytes()));
211 when(hucMock.getResponseCode()).thenReturn(200);
212 assertThat(future.get(0), is(true));
213 assertThat(future.body(), is("200"));
215 // Test a bad response code
216 when(hucMock.getResponseCode()).thenReturn(0);
217 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
218 assertThat(future.get(0), is(false));
219 assertThat(future.body(), is(errorString));
223 public void future2Test() throws LocatorException, CadiException, IOException, APIException {
224 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
225 Future<Void> future = client.future(respMock, 200);
227 ServletOutputStream sos = new ServletOutputStream() {
228 @Override public void write(int arg0) throws IOException { }
230 when(respMock.getOutputStream()).thenReturn(sos);
232 // Test a good response code
233 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(successString.getBytes()));
234 when(hucMock.getResponseCode()).thenReturn(200);
235 assertThat(future.get(0), is(true));
236 assertThat(future.body(), is(nullValue()));
238 // Test a bad response code
239 when(hucMock.getResponseCode()).thenReturn(0);
240 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
241 assertThat(future.get(0), is(false));
242 assertThat(future.body(), is(""));
246 public void hfutureTest() throws CadiException, IOException, LocatorException {
247 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
248 HFutureStub future = new HFutureStub(client, hucMock);
249 assertThat(future.get(0), is(false));
251 // Test a bad response code (default 0) with output
252 when(hucMock.getInputStream()).thenReturn(new ByteArrayInputStream(errorString.getBytes()));
253 assertThat(future.get(0), is(false));
255 assertThat(future.get(0), is(false));
257 when(hucMock.getResponseCode()).thenReturn(200);
258 assertThat(future.get(0), is(true));
260 StringBuilder sb = future.inputStreamToString(new ByteArrayInputStream(errorString.getBytes()));
261 assertThat(sb.toString(), is(errorString));
263 assertThat(future.code(), is(200));
264 assertThat(future.huc(), is(hucMock));
266 assertThat(future.exception(), is(nullValue()));
267 assertThat(future.header("string"), is(nullValue()));
275 public void headerTest() throws LocatorException {
276 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
277 String tag1 = "tag1";
278 String tag2 = "tag2";
279 String value1 = "value1";
280 String value2 = "value2";
281 client.addHeader(tag1, value1);
282 client.addHeader(tag2, value2);
285 @Test(expected = LocatorException.class)
286 public void throws1Test() throws LocatorException {
287 @SuppressWarnings("unused")
288 HClient client = new HClient(ssMock, null, 0);
291 private class HClientStub extends HClient {
292 public HClientStub(SecuritySetter<HttpURLConnection> ss, URI uri, int connectTimeout, HttpURLConnection huc) throws LocatorException {
293 super(ss, uri, connectTimeout);
296 public void setHuc(HttpURLConnection huc) {
299 field = HClient.class.getDeclaredField("huc");
300 field.setAccessible(true);
301 field.set(this, huc);
302 field.setAccessible(false);
303 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
305 fail("Caught an exception: " + e.getMessage());
309 public HttpURLConnection getConnection(URI uri, StringBuilder pi) throws IOException {
314 private class HFutureStub extends HFuture<HttpURLConnection> {
315 public HFutureStub(HClient hClient, HttpURLConnection huc) {
319 @Override public String body() { return null; }
320 public void setHuc(HttpURLConnection huc) { this.huc = huc; }