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.junit.Assert.assertThat;
25 import static org.junit.Assert.fail;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.CoreMatchers.nullValue;
28 import static org.mockito.Mockito.when;
29 import static org.mockito.Mockito.doReturn;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.net.HttpURLConnection;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 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.oauth.TimedToken;
50 import org.onap.aaf.cadi.oauth.TzHClient;
51 import org.onap.aaf.misc.env.APIException;
52 import org.onap.aaf.cadi.config.Config;
53 import org.onap.aaf.cadi.config.SecurityInfoC;
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 lon }
77 errStream = new ByteArrayOutputStream();
78 System.setErr(new PrintStream(errStream));
82 public void tearDown() {
83 System.setErr(System.err);
87 public void test() throws CadiException, LocatorException, APIException, IOException {
88 TzHClient client = new TzHClient(access, "tag");
90 client.best(retryableMock);
91 fail("Should've thrown an exception");
92 } catch (CadiException e) {
93 assertThat(e.getMessage(), is("OAuth2 Token has not been set"));
95 client.setToken(client_id, tokenMock);
96 when(tokenMock.expired()).thenReturn(true);
98 client.best(retryableMock);
99 fail("Should've thrown an exception");
100 } catch (CadiException e) {
101 assertThat(e.getMessage(), is("Expired Token"));
104 client = new TzHClient(access, siMock, locMock);
105 when(tokenMock.expired()).thenReturn(false);
106 doReturn(clientMock).when(retryableMock).lastClient();
108 when(retryableMock.item()).thenReturn(itemMock);
109 client.setToken(client_id, tokenMock);
110 assertThat(client.best(retryableMock), is(nullValue()));