c32abe436a2b4398669dff545715e296bc1bd69f
[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.IOException;
29 import java.io.InputStream;
30 import java.lang.reflect.Field;
31 import java.net.HttpURLConnection;
32 import java.net.URI;
33 import java.net.URISyntaxException;
34
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletResponse;
37
38 import static org.hamcrest.CoreMatchers.*;
39 import org.junit.*;
40 import org.mockito.*;
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;
51
52 public class JU_HClient {
53
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;
60
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";
66
67     private static final String errorString = "error string";
68     private static final String successString = "success string";
69
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";
74
75     private URI uri;
76
77     @Before
78     public void setup() throws URISyntaxException {
79         MockitoAnnotations.initMocks(this);
80
81         uri = new URI(uriString);
82     }
83
84     @Test
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()));
95     }
96
97     @Test
98     public void sendTest() throws LocatorException, APIException, URISyntaxException {
99         HClientStub client;
100         client = new HClientStub(ssMock, uri, 0, null);
101         client.send();
102         
103         client.setPathInfo("/pathinfo");
104         client.send();
105
106         client.setPathInfo("pathinfo");
107         client.send();
108
109         client = new HClientStub(null, uri, 0, null);
110         client.send();
111
112         client.addHeader(tag1, value1);
113         client.addHeader(tag2, value2);
114         client.send();
115
116         client.setPayload(transferMock);
117         client.send();
118     }
119     
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);
123         client.send();
124     }
125
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);
131         client.send();
132     }
133
134     @Test
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);
138
139         // Test a bad response code (default 0) without output
140         assertThat(future.get(0), is(false));
141         assertThat(future.body().length(), is(0));
142
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));
148
149         // Test a good response code
150         when(hucMock.getResponseCode()).thenReturn(201);
151         assertThat(future.get(0), is(true));
152     }
153
154     @Test
155     public void futureReadStringTest() throws LocatorException, CadiException, IOException {
156         HClient client = new HClientStub(ssMock, uri, 0, hucMock);
157         Future<String> future = client.futureReadString();
158
159         // Test a bad response code (default 0) without output
160         assertThat(future.get(0), is(false));
161         assertThat(future.body().length(), is(0));
162
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));
167
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));
173     }
174
175     @Test
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);
179
180         // Test a bad response code (default 0) without output
181         assertThat(future.get(0), is(false));
182         assertThat(future.body().length(), is(0));
183
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));
188
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));
199     }
200
201     @Test
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);
205
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"));
211
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));
217     }
218
219     @Test
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);
223
224         ServletOutputStream sos = new ServletOutputStream() {
225             @Override public void write(int arg0) throws IOException { }
226         };
227         when(respMock.getOutputStream()).thenReturn(sos);
228
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()));
234
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(""));
240     }
241
242     @Test
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));
247
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));
251
252         assertThat(future.get(0), is(false));
253
254         when(hucMock.getResponseCode()).thenReturn(200);
255         assertThat(future.get(0), is(true));
256
257         StringBuilder sb = future.inputStreamToString(new ByteArrayInputStream(errorString.getBytes()));
258         assertThat(sb.toString(), is(errorString));
259
260         assertThat(future.code(), is(200));
261         assertThat(future.huc(), is(hucMock));
262
263         assertThat(future.exception(), is(nullValue()));
264         assertThat(future.header("string"), is(nullValue()));
265
266         // coverage...
267         future.setHuc(null);
268         future.close();
269     }
270
271     @Test
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);
280     }
281
282     @Test(expected = LocatorException.class)
283     public void throws1Test() throws LocatorException {
284         @SuppressWarnings("unused")
285         HClient client = new HClient(ssMock, null, 0);
286     }
287
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);
291             setHuc(huc);
292         }
293         public void setHuc(HttpURLConnection huc) {
294             Field field;
295             try {
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) {
301                 e.printStackTrace();
302                 fail("Caught an exception: " + e.getMessage());
303             }
304         }
305         @Override
306         public HttpURLConnection getConnection(URI uri, StringBuilder pi) throws IOException {
307             return hucMock;
308         }
309     }
310
311     private class HFutureStub extends HFuture<HttpURLConnection> {
312         public HFutureStub(HClient hClient, HttpURLConnection huc) {
313             hClient.super(huc);
314         }
315
316         @Override public String body() { return null; }
317         public void setHuc(HttpURLConnection huc) { this.huc = huc; }
318     }
319
320 }