Update Fixes from testing
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / oauth / test / JU_TzHClient.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.oauth.test;
23
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;
30
31 import java.io.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import java.net.HttpURLConnection;
35 import java.net.URI;
36
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;
54
55 public class JU_TzHClient {
56     
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;
63     
64     private PropAccess access;
65     
66     private ByteArrayOutputStream errStream;
67     
68     private final static String client_id = "id";
69     
70     @Before
71     public void setup() {
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");
77         
78         errStream = new ByteArrayOutputStream();
79         System.setErr(new PrintStream(errStream));
80     }
81     
82     @After
83     public void tearDown() {
84         System.setErr(System.err);
85     }
86
87     @Test
88     public void test() throws CadiException, LocatorException, APIException, IOException {
89         TzHClient client;
90         try {
91             client = new TzHClient(access, "tag");
92         } catch (Exception e) {
93             throw e;
94         }
95         try {
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"));
100         }
101         client.setToken(client_id, tokenMock);
102         when(tokenMock.expired()).thenReturn(true);
103         try {
104             client.best(retryableMock);
105             fail("Should've thrown an exception");
106         } catch (CadiException e) {
107             assertThat(e.getMessage(), is("Expired Token"));
108         }
109
110         client = new TzHClient(access, siMock, locMock);
111         when(tokenMock.expired()).thenReturn(false);
112         doReturn(clientMock).when(retryableMock).lastClient();
113
114         when(retryableMock.item()).thenReturn(itemMock);
115         client.setToken(client_id, tokenMock);
116         assertThat(client.best(retryableMock), is(nullValue()));
117     }
118
119 }