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.oauth.test;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.nullValue;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.fail;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.when;
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.net.HttpURLConnection;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.aaf.cadi.CadiException;
43 import org.onap.aaf.cadi.Locator;
44 import org.onap.aaf.cadi.Locator.Item;
45 import org.onap.aaf.cadi.LocatorException;
46 import org.onap.aaf.cadi.PropAccess;
47 import org.onap.aaf.cadi.client.Rcli;
48 import org.onap.aaf.cadi.client.Retryable;
49 import org.onap.aaf.cadi.config.Config;
50 import org.onap.aaf.cadi.config.SecurityInfoC;
51 import org.onap.aaf.cadi.oauth.TimedToken;
52 import org.onap.aaf.cadi.oauth.TzHClient;
53 import org.onap.aaf.misc.env.APIException;
55 public class JU_TzHClient {
57 @Mock private Retryable<Integer> retryableMock;
58 @Mock private TimedToken tokenMock;
59 @Mock private SecurityInfoC<HttpURLConnection> siMock;
60 @Mock private Locator<URI> locMock;
61 @Mock private Item itemMock;
62 @Mock private Rcli<HttpURLConnection> clientMock;
64 private PropAccess access;
66 private ByteArrayOutputStream errStream;
68 private final static String client_id = "id";
72 MockitoAnnotations.initMocks(this);
73 access = new PropAccess(new PrintStream(new ByteArrayOutputStream()), new String[0]);
74 access.setProperty(Config.CADI_LATITUDE, "38.62"); // St Louis approx lat
75 access.setProperty(Config.CADI_LONGITUDE, "90.19"); // St Louis approx long
76 //access.setProperty("tag", "http://aaf.something.com");
78 errStream = new ByteArrayOutputStream();
79 System.setErr(new PrintStream(errStream));
83 public void tearDown() {
84 System.setErr(System.err);
88 public void test() throws CadiException, LocatorException, APIException, IOException {
91 client = new TzHClient(access, "tag");
92 } catch (Exception e) {
96 client.best(retryableMock);
97 fail("Should've thrown an exception");
98 } catch (CadiException e) {
99 assertThat(e.getMessage(), is("OAuth2 Token has not been set"));
101 client.setToken(client_id, tokenMock);
102 when(tokenMock.expired()).thenReturn(true);
104 client.best(retryableMock);
105 fail("Should've thrown an exception");
106 } catch (CadiException e) {
107 assertThat(e.getMessage(), is("Expired Token"));
110 client = new TzHClient(access, siMock, locMock);
111 when(tokenMock.expired()).thenReturn(false);
112 doReturn(clientMock).when(retryableMock).lastClient();
114 when(retryableMock.item()).thenReturn(itemMock);
115 client.setToken(client_id, tokenMock);
116 assertThat(client.best(retryableMock), is(nullValue()));