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