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.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;
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;
39 import java.net.URISyntaxException;
41 import javax.net.ssl.SSLHandshakeException;
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;
58 import junit.framework.Assert;
60 public class JU_HMangr {
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;
69 private PropAccess access;
71 private final static String uriString = "http://example.com";
74 public void setup() throws URISyntaxException {
75 MockitoAnnotations.initMocks(this);
77 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
78 uri = new URI(uriString);
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()));
89 when(retryableMock.lastClient()).thenReturn(clientMock);
90 assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
92 CadiException cadiException;
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()));
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()));
106 doThrow(connectException).when(retryableMock).code(clientMock);
107 assertThat(hman.same(ssMock, retryableMock), is(nullValue()));
111 @Test(expected = LocatorException.class)
112 public void throwsLocatorException1Test() throws LocatorException {
113 @SuppressWarnings("unused")
114 HMangr hman = new HMangr(access, null);
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);
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);
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);
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);
149 ConnectException connectException = new ConnectException();
150 CadiException cadiException = new CadiException(connectException);
151 doThrow(cadiException).when(retryableMock).code(clientMock);
152 hman.same(ssMock, retryableMock);
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);
162 SocketException socketException = new SocketException();
163 CadiException cadiException = new CadiException(socketException);
164 doThrow(cadiException).when(retryableMock).code(clientMock);
165 hman.same(ssMock, retryableMock);
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);
175 Exception e = new Exception();
176 CadiException cadiException = new CadiException(e);
177 doThrow(cadiException).when(retryableMock).code(clientMock);
178 hman.same(ssMock, retryableMock);
182 public void allTest() throws LocatorException, CadiException, APIException {
183 HManagerStub hman = new HManagerStub(access, locMock);
185 assertThat(hman.best(ssMock, retryableMock), is(nullValue()));
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");
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()));
200 hman.oneOf(ssMock, retryableMock, true, "host");
201 fail("Should've thrown an exception");
202 } catch (LocatorException e) {
205 when(locMock.first()).thenReturn(itemMock);
206 when(locMock.get(itemMock)).thenReturn(uri);
208 // Branching coverage...
209 assertThat(hman.oneOf(ssMock, retryableMock, false, null), is(nullValue()));
210 assertThat(hman.oneOf(ssMock, retryableMock, false, "host"), is(nullValue()));
212 assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue()));
214 CadiException cadiException;
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()));
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()));
224 cadiException = new CadiException(new SocketException());
225 doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
227 hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
228 fail("Should've thrown an exception");
229 } catch (CadiException e) {
232 cadiException = new CadiException(new SocketException("java.net.SocketException: Connection reset"));
233 doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
235 hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
236 fail("Should've thrown an exception");
237 } catch (CadiException e) {
240 cadiException = new CadiException();
241 doThrow(cadiException).when(retryableMock).code((Rcli<?>) any());
243 hman.oneOf(ssMock, retryableMock, false, uriString.substring(7));
244 fail("Should've thrown an exception");
245 } catch (CadiException e) {
248 doThrow(new ConnectException()).when(retryableMock).code((Rcli<?>) any());
249 assertThat(hman.oneOf(ssMock, retryableMock, false, uriString.substring(7)), is(nullValue()));
251 when(goodRetry.code((Rcli<?>) any())).thenReturn(5);
252 assertThat(hman.oneOf(ssMock, goodRetry, false, uriString.substring(7)), is(5));
256 public void coverageTest() throws LocatorException {
257 HMangr hman = new HMangr(access, locMock);
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"));
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) {
273 @Override public<RET> RET oneOf(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable, boolean notify, String host) {