bd2393e4e490a8e22ba1ec24cb2f142a5373d52f
[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.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;
34 import java.net.URI;
35
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;
41
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;
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 lon     }
76                 
77                 errStream = new ByteArrayOutputStream();
78                 System.setErr(new PrintStream(errStream));
79         }
80         
81         @After
82         public void tearDown() {
83                 System.setErr(System.err);
84         }
85
86         @Test
87         public void test() throws CadiException, LocatorException, APIException, IOException {
88                 TzHClient client;
89                 try {
90                         client = new TzHClient(access, "tag");
91                 } catch (Exception e) {
92                         throw e;
93                 }
94                 try {
95                         client.best(retryableMock);
96                         fail("Should've thrown an exception");
97                 } catch (CadiException e) {
98                         assertThat(e.getMessage(), is("OAuth2 Token has not been set"));
99                 }
100                 client.setToken(client_id, tokenMock);
101                 when(tokenMock.expired()).thenReturn(true);
102                 try {
103                         client.best(retryableMock);
104                         fail("Should've thrown an exception");
105                 } catch (CadiException e) {
106                         assertThat(e.getMessage(), is("Expired Token"));
107                 }
108
109                 client = new TzHClient(access, siMock, locMock);
110                 when(tokenMock.expired()).thenReturn(false);
111                 doReturn(clientMock).when(retryableMock).lastClient();
112
113                 when(retryableMock.item()).thenReturn(itemMock);
114                 client.setToken(client_id, tokenMock);
115                 assertThat(client.best(retryableMock), is(nullValue()));
116         }
117
118 }