Remove Tabs, per Jococo
[aaf/authz.git] / cadi / client / src / test / java / org / onap / aaf / cadi / http / test / JU_HMangr.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.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.nullValue;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.fail;
29 import static org.mockito.Matchers.any;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.when;
32
33 import java.io.ByteArrayOutputStream;
34 import java.io.PrintStream;
35 import java.net.ConnectException;
36 import java.net.HttpURLConnection;
37 import java.net.SocketException;
38 import java.net.URI;
39 import java.net.URISyntaxException;
40
41 import javax.net.ssl.SSLHandshakeException;
42
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.onap.aaf.cadi.Access;
48 import org.onap.aaf.cadi.CadiException;
49 import org.onap.aaf.cadi.Locator;
50 import org.onap.aaf.cadi.LocatorException;
51 import org.onap.aaf.cadi.PropAccess;
52 import org.onap.aaf.cadi.SecuritySetter;
53 import org.onap.aaf.cadi.client.Rcli;
54 import org.onap.aaf.cadi.client.Retryable;
55 import org.onap.aaf.cadi.http.HMangr;
56 import org.onap.aaf.misc.env.APIException;
57
58 import junit.framework.Assert;
59
60 public class JU_HMangr {
61     
62     @Mock Locator<URI> locMock;
63     @Mock SecuritySetter<HttpURLConnection> ssMock;
64     @Mock Retryable<Void> retryableMock;
65     @Mock Retryable<Integer> goodRetry;
66     @Mock Locator.Item itemMock;
67     @Mock Rcli<Object> clientMock;
68     
69     private PropAccess access;
70     private URI uri;
71     private final static String uriString = "http://example.com";
72
73     @Before
74     public void setup() throws URISyntaxException {
75         MockitoAnnotations.initMocks(this);
76
77         access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
78         uri = new URI(uriString);
79     }
80
81     @Test
82     public void sameTest() throws LocatorException, APIException, CadiException, ConnectException {
83         HMangr hman = new HMangr(access, locMock);
84         when(retryableMock.item()).thenReturn(itemMock);
85         when(locMock.get(itemMock)).thenReturn(uri);
86         assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
87         
88         //coverage...
89         when(retryableMock.lastClient()).thenReturn(clientMock);
90         assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
91         
92         CadiException cadiException;
93
94         ConnectException connectException = new ConnectException();
95         cadiException = new CadiException(connectException);
96         doThrow(cadiException).when(retryableMock).code(clientMock);
97         when(locMock.hasItems()).thenReturn(true).thenReturn(false);
98         assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
99
100         SocketException socketException = new SocketException();
101         cadiException = new CadiException(socketException);
102         doThrow(cadiException).when(retryableMock).code(clientMock);
103         when(locMock.hasItems()).thenReturn(true).thenReturn(false);
104         assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
105
106         doThrow(connectException).when(retryableMock).code(clientMock);
107         assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
108
109     }
110
111     @Test(expected = LocatorException.class)
112     public void throwsLocatorException1Test() throws LocatorException {
113         @SuppressWarnings("unused")
114         HMangr hman = new HMangr(access, null);
115     }
116
117     @Test(expected = LocatorException.class)
118     public void throwsLocatorException2Test() throws LocatorException, APIException, CadiException {
119         HMangr hman = new HMangr(access, locMock);
120         hman.same(ssMock, retryableMock);
121     }
122
123     @Test(expected = LocatorException.class)
124     public void throwsLocatorException3Test() throws LocatorException, APIException, CadiException {
125         HMangr hman = new HMangr(access, locMock);
126         when(locMock.best()).thenReturn(itemMock);
127         when(locMock.hasItems()).thenReturn(true).thenReturn(false);
128         hman.same(ssMock, retryableMock);
129     }
130
131     @SuppressWarnings("unchecked")
132     @Test(expected = CadiException.class)
133     public void throwsCadiException1Test() throws LocatorException, APIException, CadiException, ConnectException {
134         HMangr hman = new HMangr(access, locMock);
135         when(retryableMock.item()).thenReturn(itemMock);
136         when(locMock.get(itemMock)).thenReturn(uri);
137         when(retryableMock.lastClient()).thenReturn(clientMock);
138         when(retryableMock.code(clientMock)).thenThrow(CadiException.class);
139         hman.same(ssMock, retryableMock);
140     }
141
142     @Test(expected = CadiException.class)
143     public void throwsCadiException2Test() throws LocatorException, APIException, CadiException, ConnectException {
144         HMangr hman = new HMangr(access, locMock);
145         when(retryableMock.item()).thenReturn(itemMock);
146         when(locMock.get(itemMock)).thenReturn(uri);
147         when(retryableMock.lastClient()).thenReturn(clientMock);
148
149         ConnectException connectException = new ConnectException();
150         CadiException cadiException = new CadiException(connectException);
151         doThrow(cadiException).when(retryableMock).code(clientMock);
152         hman.same(ssMock, retryableMock);
153     }
154
155     @Test(expected = CadiException.class)
156     public void throwsCadiException3Test() throws LocatorException, APIException, CadiException, ConnectException {
157         HMangr hman = new HMangr(access, locMock);
158         when(retryableMock.item()).thenReturn(itemMock);
159         when(locMock.get(itemMock)).thenReturn(uri);
160         when(retryableMock.lastClient()).thenReturn(clientMock);
161
162         SocketException socketException = new SocketException();
163         CadiException cadiException = new CadiException(socketException);
164         doThrow(cadiException).when(retryableMock).code(clientMock);
165         hman.same(ssMock, retryableMock);
166     }
167
168     @Test(expected = CadiException.class)
169     public void throwsCadiException4Test() throws LocatorException, APIException, CadiException, ConnectException {
170         HMangr hman = new HMangr(access, locMock);
171         when(retryableMock.item()).thenReturn(itemMock);
172         when(locMock.get(itemMock)).thenReturn(uri);
173         when(retryableMock.lastClient()).thenReturn(clientMock);
174
175         Exception e = new Exception();
176         CadiException cadiException = new CadiException(e);
177         doThrow(cadiException).when(retryableMock).code(clientMock);
178         hman.same(ssMock, retryableMock);
179     }
180
181     @Test
182     public void allTest() throws LocatorException, CadiException, APIException {
183         HManagerStub hman = new HManagerStub(access, locMock);
184
185         assertThat(hman.best(ssMock, retryableMock), is(nullValue()));
186         try {
187             hman.all(ssMock, retryableMock, true);
188             Assert.fail("Should have thrown LocatorException");
189         } catch (LocatorException e) {
190             assertEquals(e.getLocalizedMessage(),"No available clients to call");
191         }
192     }
193
194     @Test
195     public void oneOfTest() throws LocatorException, CadiException, APIException, ConnectException {
196         HMangr hman = new HMangr(access, locMock);
197         assertThat(hman.oneOf(ssMock, retryableMock, false, "host"), is(nullValue()));
198
199         try {
200             hman.oneOf(ssMock, retryableMock, true, "host");
201             fail("Should've thrown an exception");
202         } catch (LocatorException e) {
203         }
204
205         when(locMock.first()).thenReturn(itemMock);
206         when(locMock.get(itemMock)).thenReturn(uri);
207
208         // Branching coverage...
209         assertThat(hman.oneOf(ssMock, retryableMock, false, null), is(nullValue()));
210         assertThat(hman.oneOf(ssMock, retryableMock, false, "host"), is(nullValue()));
211
212         assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue()));
213         
214         CadiException cadiException;
215
216         cadiException = new CadiException(new ConnectException());
217         doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
218         assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue()));
219
220         cadiException = new CadiException(new SSLHandshakeException(null));
221         doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
222         assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue()));
223
224         cadiException = new CadiException(new SocketException());
225         doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
226         try {
227             hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
228             fail("Should've thrown an exception");
229         } catch (CadiException e) {
230         }
231
232         cadiException = new CadiException(new SocketException("java.net.SocketException: Connection reset"));
233         doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
234         try {
235             hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
236             fail("Should've thrown an exception");
237         } catch (CadiException e) {
238         }
239
240         cadiException = new CadiException();
241         doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
242         try {
243             hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
244             fail("Should've thrown an exception");
245         } catch (CadiException e) {
246         }
247         
248         doThrow(new ConnectException()).when(retryableMock).code((Rcli<?>) any());
249         assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue()));
250
251         when(goodRetry.code((Rcli<?>) any())).thenReturn(5);
252         assertThat(hman.oneOf(ssMock, goodRetry, false, uriString.substring(7)), is(5));
253     }
254
255     @Test
256     public void coverageTest() throws LocatorException {
257         HMangr hman = new HMangr(access, locMock);
258         hman.readTimeout(5);
259         assertThat(hman.readTimeout(), is(5));
260         hman.connectionTimeout(5);
261         assertThat(hman.connectionTimeout(), is(5));
262         hman.apiVersion("v1.0");
263         assertThat(hman.apiVersion(), is("v1.0"));
264         hman.close();
265
266     }
267
268     private class HManagerStub extends HMangr {
269         public HManagerStub(Access access, Locator<URI> loc) throws LocatorException { super(access, loc); }
270         @Override public<RET> RET same(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) {
271             return null;
272         }
273         @Override public<RET> RET oneOf(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable, boolean notify, String host) {
274             return null;
275         }
276     }
277         
278 }