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.ByteArrayOutputStream;
28 import java.io.PrintStream;
29 import java.net.ConnectException;
30 import java.net.HttpURLConnection;
31 import java.net.SocketException;
33 import java.net.URISyntaxException;
35 import javax.net.ssl.SSLHandshakeException;
37 import static org.hamcrest.CoreMatchers.*;
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;
52 public class JU_HMangr {
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;
61 private PropAccess access;
63 private final static String uriString = "http://example.com";
66 public void setup() throws URISyntaxException {
67 MockitoAnnotations.initMocks(this);
69 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
70 uri = new URI(uriString);
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()));
81 when(retryableMock.lastClient()).thenReturn(clientMock);
82 assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
84 CadiException cadiException;
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()));
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()));
98 doThrow(connectException).when(retryableMock).code(clientMock);
99 assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
103 @Test(expected = LocatorException.class)
104 public void throwsLocatorException1Test() throws LocatorException {
105 @SuppressWarnings("unused")
106 HMangr hman = new HMangr(access, null);
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);
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);
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);
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);
141 ConnectException connectException = new ConnectException();
142 CadiException cadiException = new CadiException(connectException);
143 doThrow(cadiException).when(retryableMock).code(clientMock);
144 hman.same(ssMock, retryableMock);
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);
154 SocketException socketException = new SocketException();
155 CadiException cadiException = new CadiException(socketException);
156 doThrow(cadiException).when(retryableMock).code(clientMock);
157 hman.same(ssMock, retryableMock);
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);
167 Exception e = new Exception();
168 CadiException cadiException = new CadiException(e);
169 doThrow(cadiException).when(retryableMock).code(clientMock);
170 hman.same(ssMock, retryableMock);
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()));
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()));
187 hman.oneOf(ssMock, retryableMock, true, "host");
188 fail("Should've thrown an exception");
189 } catch (LocatorException e) {
192 when(locMock.first()).thenReturn(itemMock);
193 when(locMock.get(itemMock)).thenReturn(uri);
195 // Branching coverage...
196 assertThat(hman.oneOf(ssMock, retryableMock, false, null), is(nullValue()));
197 assertThat(hman.oneOf(ssMock, retryableMock, false, "host"), is(nullValue()));
199 assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue()));
201 CadiException cadiException;
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()));
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()));
211 cadiException = new CadiException(new SocketException());
212 doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
214 hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
215 fail("Should've thrown an exception");
216 } catch (CadiException e) {
219 cadiException = new CadiException(new SocketException("java.net.SocketException: Connection reset"));
220 doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
222 hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
223 fail("Should've thrown an exception");
224 } catch (CadiException e) {
227 cadiException = new CadiException();
228 doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
230 hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
231 fail("Should've thrown an exception");
232 } catch (CadiException e) {
235 doThrow(new ConnectException()).when(retryableMock).code((Rcli<?>) any());
236 assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue()));
238 when(goodRetry.code((Rcli<?>) any())).thenReturn(5);
239 assertThat(hman.oneOf(ssMock, goodRetry, false, uriString.substring(7)), is(5));
243 public void coverageTest() throws LocatorException {
244 HMangr hman = new HMangr(access, locMock);
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"));
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) {
260 @Override public<RET> RET oneOf(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable, boolean notify, String host) {