e85431d31caf68c2274ec4d33fae642260dafc7c
[aaf/authz.git] / auth / auth-hello / src / test / java / org / onap / aaf / auth / hello / test / HelloTester.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.auth.hello.test;
23
24 import java.net.ConnectException;
25 import java.net.HttpURLConnection;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28
29 import org.onap.aaf.auth.common.Define;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.LocatorException;
32 import org.onap.aaf.cadi.PropAccess;
33 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
34 import org.onap.aaf.cadi.aaf.v2_0.AAFLocator;
35 import org.onap.aaf.cadi.client.Future;
36 import org.onap.aaf.cadi.client.Rcli;
37 import org.onap.aaf.cadi.client.Retryable;
38 import org.onap.aaf.cadi.config.SecurityInfoC;
39 import org.onap.aaf.misc.env.APIException;
40
41 public class HelloTester {
42
43     public static void main(String[] args) {
44         // Do Once and ONLY once
45         PropAccess access =  new PropAccess(args);
46         try {
47             Define.set(access);
48             String uriPrefix = access.getProperty("locatorURI",null);
49             if (uriPrefix==null) {
50                 System.out.println("You must add \"locatorURI=<uri>\" to the command line or VM_Args");
51             } else {
52                 SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
53                 AAFLocator loc = new AAFLocator(si,new URI(uriPrefix+"/locate/"+Define.ROOT_NS()+".hello:1.0"));
54                 AAFConHttp aafcon = new AAFConHttp(access,loc,si);
55                 
56                 //
57                 String pathinfo = "/hello";
58                 final int iterations = Integer.parseInt(access.getProperty("iterations","5"));
59                 System.out.println("Calling " + loc + " with Path " + pathinfo + ' ' + iterations + " time" + (iterations==1?"":"s"));
60                 for (int i=0;i<iterations;++i) {
61                     aafcon.best(new Retryable<Void> () {
62                         @Override
63                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
64                             Future<String> fs = client.read("/hello","text/plain");
65                             if (fs.get(5000)) {
66                                 System.out.print(fs.body());
67                             } else {
68                                 System.err.println("Ooops, missed one: " + fs.code() + ": " + fs.body());
69                             }
70                             return null;
71     
72                         }
73                     });
74                     Thread.sleep(500L);
75                 }
76             }
77         } catch (CadiException | LocatorException | URISyntaxException | APIException | InterruptedException e) {
78             e.printStackTrace();
79         }
80     }
81 }