Cleanup JUnits
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / oauth / test / JU_OAuthTest.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 java.io.ByteArrayOutputStream;
25 import java.io.PrintStream;
26 import java.net.ConnectException;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.aaf.cadi.CadiException;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.client.Future;
38 import org.onap.aaf.cadi.client.Rcli;
39 import org.onap.aaf.cadi.client.Result;
40 import org.onap.aaf.cadi.client.Retryable;
41 import org.onap.aaf.cadi.config.Config;
42 import org.onap.aaf.cadi.oauth.TimedToken;
43 import org.onap.aaf.cadi.oauth.TokenClient;
44 import org.onap.aaf.cadi.oauth.TokenClientFactory;
45 import org.onap.aaf.cadi.oauth.TzClient;
46 import org.onap.aaf.cadi.principal.Kind;
47 import org.onap.aaf.misc.env.APIException;
48 import org.onap.aaf.misc.env.util.Chrono;
49
50 import aafoauth.v2_0.Introspect;
51 import aafoauth.v2_0.Token;
52 import junit.framework.Assert;
53
54 public class JU_OAuthTest {
55
56         private ByteArrayOutputStream outStream;
57
58         private static PropAccess access;
59         private static TokenClientFactory tcf;
60
61         @BeforeClass
62         public static void setUpBeforeClass()  {
63                 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
64                 System.setOut(new PrintStream(outStream));
65
66                 access = new PropAccess();
67                 try {
68                         tcf = TokenClientFactory.instance(access);
69                 } catch (Exception e) {
70                         e.printStackTrace();
71                         Assert.fail();
72                 }
73         }
74
75         @AfterClass
76         public static void tearDownAfterClass() throws Exception {
77         }
78
79         @Before
80         public void setUp() throws Exception {
81                 outStream = new ByteArrayOutputStream();
82                 System.setOut(new PrintStream(outStream));
83         }
84
85         @After
86         public void tearDown() throws Exception {
87                 System.setOut(System.out);
88         }
89
90         @Test
91         public void testROPCFlowHappy() {
92                 try {
93                         // AAF OAuth
94                         String client_id = access.getProperty(Config.AAF_APPID);
95                         String client_secret = access.getProperty(Config.AAF_APPPASS);
96                         String tokenServiceURL = access.getProperty(Config.AAF_OAUTH2_TOKEN_URL);
97 //                      Assert.assertNotNull(tokenServiceURL);
98                         String tokenIntrospectURL = access.getProperty(Config.AAF_OAUTH2_INTROSPECT_URL);
99                         String tokenAltIntrospectURL = access.getProperty(Config.AAF_ALT_OAUTH2_INTROSPECT_URL);
100 //                      Assert.assertNotNull(tokenIntrospectURL);
101                         final String endServicesURL = access.getProperty(Config.AAF_OAUTH2_HELLO_URL);
102                         String username = access.getProperty("cadi_username");
103
104                         TokenClient tc;
105                         Result<TimedToken> rtt;
106                         if(true) {
107                                 tc = tcf.newClient(tokenServiceURL, 3000);
108                                 tc.client_creds(client_id,client_secret);
109                                 tc.password(access.getProperty("cadi_username"),access.getProperty("cadi_password"));
110                                 rtt = tc.getToken(Kind.BASIC_AUTH,"org.osaaf.aaf","org.osaaf.test");
111                                 if(rtt.isOK()) {
112                                         print(rtt.value);
113                                         rtt = tc.refreshToken(rtt.value);
114                                         if(rtt.isOK()) {
115                                                 print(rtt.value);
116                                                 TokenClient ic = tcf.newClient(tokenIntrospectURL,3000);
117                                                 ic.client_creds(client_id,client_secret);
118
119                                                 Result<Introspect> ri = ic.introspect(rtt.value.getAccessToken());
120                                                 if(ri.isOK()) {
121                                                         print(ri.value);
122                                                 } else {
123                                                         System.out.println(ri.code + ' ' + ri.error);
124                                                         Assert.fail(ri.code + ' ' + ri.error);
125                                                 }
126                                                 TzClient helloClient = tcf.newTzClient(endServicesURL);
127                                                 helloClient.setToken(client_id, rtt.value);
128 //                                              String rv = serviceCall(helloClient);
129 //                                              System.out.println(rv);
130                 //                              Assert.assertEquals("Hello AAF OAuth2\n",rv);
131                                         } else {
132                                                 System.out.println(rtt.code + ' ' + rtt.error);
133                                                 Assert.fail(rtt.code + ' ' + rtt.error);
134                                         }
135                                 } else {
136                                         System.out.println(rtt.code + ' ' + rtt.error);
137                                         Assert.fail(rtt.code + ' ' + rtt.error);
138                                 }
139                         }
140                 
141                         // ISAM Test
142                         if(true) {
143                                 System.out.println("**** ISAM TEST ****");
144                                 tokenServiceURL=access.getProperty(Config.AAF_ALT_OAUTH2_TOKEN_URL);
145                                 client_id=access.getProperty(Config.AAF_ALT_CLIENT_ID);
146                                 client_secret=access.getProperty(Config.AAF_ALT_CLIENT_SECRET);
147                                 if(tokenServiceURL!=null) {
148                                         tc = tcf.newClient(tokenServiceURL, 3000);
149                                         tc.client_creds(client_id, client_secret);
150                                         int at = username.indexOf('@');
151                                         
152                                         tc.password(at>=0?username.substring(0, at):username,access.getProperty("cadi_password"));
153                                         rtt = tc.getToken("org.osaaf.aaf","org.osaaf.test");
154                                         if(rtt.isOK()) {
155                                                 print(rtt.value);
156                                                 rtt = tc.refreshToken(rtt.value);
157                                                 if(rtt.isOK()) {
158                                                         print(rtt.value);
159                                                         
160                                                         tc = tcf.newClient(tokenAltIntrospectURL, 3000);
161                                                         tc.client_creds(client_id, client_secret);
162                                                         Result<Introspect> rti = tc.introspect(rtt.value.getAccessToken());
163                                                         if(rti.isOK()) {
164                                                                 System.out.print("Normal ISAM ");
165                                                                 print(rti.value);
166                                                         } else {
167                                                                 System.out.println(rti.code + ' ' + rti.error);
168                                                                 Assert.fail(rtt.code + ' ' + rtt.error);
169                                                         }
170
171                                                         tc = tcf.newClient(tokenIntrospectURL, 3000);
172                                                         tc.client_creds(client_id, client_secret);
173                                                         rti = tc.introspect(rtt.value.getAccessToken());
174                                                         if(rti.isOK()) {
175                                                                 System.out.print("AAF with ISAM Token ");
176                                                                 print(rti.value);
177                                                         } else {
178                                                                 System.out.println(rti.code + ' ' + rti.error);
179                                                                 if(rti.code!=404) {
180                                                                         Assert.fail(rti.code + ' ' + rti.error);
181                                                                 }
182                                                         }
183
184                                                         TzClient tzClient = tcf.newTzClient(endServicesURL);
185                                                         tzClient.setToken(client_id, rtt.value);
186                                                         // Note: this is AAF's "Hello" server
187                                                         String rv = serviceCall(tzClient);
188                                                         System.out.println(rv);
189                         //                              Assert.assertEquals("Hello AAF OAuth2\n",rv);
190                                                 } else {
191                                                         System.out.println(rtt.code + ' ' + rtt.error);
192                                                         Assert.fail(rtt.code + ' ' + rtt.error);
193                                                 }
194                                         } else {
195                                                 System.out.println(rtt.code + ' ' + rtt.error);
196                                                 Assert.fail(rtt.code + ' ' + rtt.error);
197                                         }
198                                 } else {
199                                         Assert.fail(Config.AAF_ALT_OAUTH2_TOKEN_URL + " is required");
200                                 }
201                         }
202                 } catch (Exception e) {
203 //                      Assert.fail();
204                 }
205         }
206         
207         
208 //      private TokenClient testROPCFlow(final String url, final String client_id, final String client_secret, String user, String password, final String ... scope) throws Exception {
209 //              TokenClient tclient = tcf.newClient(url,3000);
210 //              tclient.client_creds(client_id, client_secret);
211 //              if(user!=null && password!=null) {
212 //                      tclient.password(user,password);
213 //              }
214 //              Result<TimedToken> rt = tclient.getToken(scope);
215 //              if(rt.isOK()) {
216 //                      print(rt.value);
217 //                      Result<Introspect> rti = tclient.introspect(rt.value.getAccessToken());
218 //                      if(rti.isOK()) {
219 //                              print(rti.value);
220 //                      } else {
221 //                              printAndFail(rti);
222 //                      }
223 //              } else {
224 //                      printAndFail(rt);
225 //              }
226 //              return tclient;
227 //      }
228         
229         private String serviceCall(TzClient tzClient) throws Exception {
230                 return tzClient.best(new Retryable<String>() {
231                         @Override
232                         public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
233                                 Future<String> future = client.read(null,"text/plain");
234                                 if(future.get(3000)) {
235                                         return future.value;
236                                 } else {
237                                         throw new APIException(future.code()  + future.body());
238                                 }
239                         }
240                 });
241         }
242 //      private void printAndFail(Result<?> rt) {
243 //              System.out.printf("HTTP Code %d: %s\n", rt.code, rt.error);
244 //              Assert.fail(rt.toString());
245 //      }
246
247         private void print(Token t) {
248                 GregorianCalendar exp_date = new GregorianCalendar();
249                 exp_date.add(GregorianCalendar.SECOND, t.getExpiresIn());
250                 System.out.printf("Access Token\n\tToken:\t\t%s\n\tToken Type:\t%s\n\tExpires In:\t%d (%s)\n\tScope:\t\t%s\n\tRefresh Token:\t%s\n",
251                 t.getAccessToken(),
252                 t.getTokenType(),
253                 t.getExpiresIn(),
254                 Chrono.timeStamp(new Date(System.currentTimeMillis()+(t.getExpiresIn()*1000))),
255                 t.getScope(),
256                 t.getRefreshToken());
257         }
258         
259         private void print(Introspect ti) {
260                 if(ti==null || ti.getClientId()==null) {
261                         System.out.println("Empty Introspect");
262                         return;
263                 }
264                 Date exp = new Date(ti.getExp()*1000); // seconds
265                 System.out.printf("Introspect\n"
266                                 + "\tAccessToken:\t%s\n"
267                                 + "\tClient-id:\t%s\n"
268                                 + "\tClient Type:\t%s\n"
269                                 + "\tActive:  \t%s\n"
270                                 + "\tUserName:\t%s\n"
271                                 + "\tExpires: \t%d (%s)\n"
272                                 + "\tScope:\t\t%s\n"
273                                 + "\tContent:\t\t%s\n",
274                 ti.getAccessToken(),
275                 ti.getClientId(),
276                 ti.getClientType(),
277                 ti.isActive()?Boolean.TRUE.toString():Boolean.FALSE.toString(),
278                 ti.getUsername(),
279                 ti.getExp(),
280                 Chrono.timeStamp(exp),
281                 ti.getScope(),
282                 ti.getContent()==null?"":ti.getContent());
283                 
284                 System.out.println();
285         }
286 }