Improve coverage of cadi-client
[aaf/authz.git] / cadi / client / src / test / java / org / onap / aaf / cadi / http / test / JU_HClient.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.http.test;
23
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
26
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;
34 import java.net.URI;
35 import java.net.URISyntaxException;
36 import java.net.URL;
37
38 import javax.servlet.ServletOutputStream;
39 import javax.servlet.http.HttpServletResponse;
40
41 import static org.hamcrest.CoreMatchers.*;
42 import org.junit.*;
43 import org.mockito.*;
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;
54
55 public class JU_HClient {
56
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;
63
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";
69
70         private static final String errorString = "error string";
71         private static final String successString = "success string";
72
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";
77
78         private URI uri;
79
80         @Before
81         public void setup() throws URISyntaxException {
82                 MockitoAnnotations.initMocks(this);
83
84                 uri = new URI(uriString);
85         }
86
87         @Test
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()));
98         }
99
100         @Test
101         public void sendTest() throws LocatorException, APIException, URISyntaxException {
102                 HClientStub client;
103                 client = new HClientStub(ssMock, uri, 0, null);
104                 client.send();
105                 
106                 client.setPathInfo("/pathinfo");
107                 client.send();
108
109                 client.setPathInfo("pathinfo");
110                 client.send();
111
112                 client = new HClientStub(null, uri, 0, null);
113                 client.send();
114
115                 client.addHeader(tag1, value1);
116                 client.addHeader(tag2, value2);
117                 client.send();
118
119                 client.setPayload(transferMock);
120                 client.send();
121         }
122         
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);
126                 client.send();
127         }
128
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);
134                 client.send();
135         }
136
137         @Test
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);
141
142                 // Test a bad response code (default 0) without output
143                 assertThat(future.get(0), is(false));
144                 assertThat(future.body().length(), is(0));
145
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));
151
152                 // Test a good response code
153                 when(hucMock.getResponseCode()).thenReturn(201);
154                 assertThat(future.get(0), is(true));
155         }
156
157         @Test
158         public void futureReadStringTest() throws LocatorException, CadiException, IOException {
159                 HClient client = new HClientStub(ssMock, uri, 0, hucMock);
160                 Future<String> future = client.futureReadString();
161
162                 // Test a bad response code (default 0) without output
163                 assertThat(future.get(0), is(false));
164                 assertThat(future.body().length(), is(0));
165
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));
170
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));
176         }
177
178         @Test
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);
182
183                 // Test a bad response code (default 0) without output
184                 assertThat(future.get(0), is(false));
185                 assertThat(future.body().length(), is(0));
186
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));
191
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));
202         }
203
204         @Test
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);
208
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"));
214
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));
220         }
221
222         @Test
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);
226
227                 ServletOutputStream sos = new ServletOutputStream() {
228                         @Override public void write(int arg0) throws IOException { }
229                 };
230                 when(respMock.getOutputStream()).thenReturn(sos);
231
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()));
237
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(""));
243         }
244
245         @Test
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));
250
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));
254
255                 assertThat(future.get(0), is(false));
256
257                 when(hucMock.getResponseCode()).thenReturn(200);
258                 assertThat(future.get(0), is(true));
259
260                 StringBuilder sb = future.inputStreamToString(new ByteArrayInputStream(errorString.getBytes()));
261                 assertThat(sb.toString(), is(errorString));
262
263                 assertThat(future.code(), is(200));
264                 assertThat(future.huc(), is(hucMock));
265
266                 assertThat(future.exception(), is(nullValue()));
267                 assertThat(future.header("string"), is(nullValue()));
268
269                 // coverage...
270                 future.setHuc(null);
271                 future.close();
272         }
273
274         @Test
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);
283         }
284
285         @Test(expected = LocatorException.class)
286         public void throws1Test() throws LocatorException {
287                 @SuppressWarnings("unused")
288                 HClient client = new HClient(ssMock, null, 0);
289         }
290
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);
294                         setHuc(huc);
295                 }
296                 public void setHuc(HttpURLConnection huc) {
297                         Field field;
298                         try {
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) {
304                                 e.printStackTrace();
305                                 fail("Caught an exception: " + e.getMessage());
306                         }
307                 }
308                 @Override
309                 public HttpURLConnection getConnection(URI uri, StringBuilder pi) throws IOException {
310                         return hucMock;
311                 }
312         }
313
314         private class HFutureStub extends HFuture<HttpURLConnection> {
315                 public HFutureStub(HClient hClient, HttpURLConnection huc) {
316                         hClient.super(huc);
317                 }
318
319                 @Override public String body() { return null; }
320                 public void setHuc(HttpURLConnection huc) { this.huc = huc; }
321         }
322
323 }