Merge "Cleanup JUnits"
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / lur / aaf / test / JU_JMeter.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 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.lur.aaf.test;
23
24 import org.junit.*;
25
26 import java.io.BufferedReader;
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.PrintStream;
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
33 import java.net.HttpURLConnection;
34 import java.security.Principal;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Properties;
38
39 import org.onap.aaf.cadi.Permission;
40 import org.onap.aaf.cadi.PropAccess;
41 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
42 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
43 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
44 import org.onap.aaf.cadi.aaf.v2_0.AAFTaf;
45 import org.onap.aaf.cadi.config.Config;
46 import org.onap.aaf.cadi.locator.DNSLocator;
47 import org.onap.aaf.cadi.principal.CachedBasicPrincipal;
48
49 import junit.framework.Assert;
50
51 public class JU_JMeter {
52         private static AAFConHttp aaf;
53         private static AAFAuthn<HttpURLConnection> aafAuthn;
54         private static AAFLurPerm aafLur;
55         private static ArrayList<Principal> perfIDs;
56         
57         private static AAFTaf<HttpURLConnection> aafTaf;
58         private static PropAccess access;
59
60         private static ByteArrayOutputStream outStream;
61         private static ByteArrayOutputStream errStream;
62
63         @BeforeClass
64         public static void before() throws Exception {
65                 outStream = new ByteArrayOutputStream();
66                 errStream = new ByteArrayOutputStream();
67
68                 System.setOut(new PrintStream(outStream));
69                 System.setErr(new PrintStream(errStream));
70                 
71                 if(aafLur==null) {
72                         Properties props = System.getProperties();
73                         props.setProperty("AFT_LATITUDE", "32.780140");
74                         props.setProperty("AFT_LONGITUDE", "-96.800451");
75                         props.setProperty("DME2_EP_REGISTRY_CLASS","DME2FS");
76                         props.setProperty("AFT_DME2_EP_REGISTRY_FS_DIR","/Volumes/Data/src/authz/dme2reg");
77                         props.setProperty("AFT_ENVIRONMENT", "AFTUAT");
78                         props.setProperty("SCLD_PLATFORM", "NON-PROD");
79                         props.setProperty(Config.AAF_URL,"https://DME2RESOLVE/service=com.att.authz.AuthorizationService/version=2.0/envContext=DEV/routeOffer=BAU_SE");
80                         props.setProperty(Config.AAF_CALL_TIMEOUT, "2000");
81                         int timeToLive = 3000;
82                         props.setProperty(Config.AAF_CLEAN_INTERVAL, Integer.toString(timeToLive));
83                         props.setProperty(Config.AAF_HIGH_COUNT, "4");
84
85                         String aafPerfIDs = props.getProperty("AAF_PERF_IDS");
86                         perfIDs = new ArrayList<Principal>();
87                         File perfFile = null;
88                         if(aafPerfIDs!=null) {
89                                 perfFile = new File(aafPerfIDs);
90                         }
91
92                         access = new PropAccess();
93                         aaf = new AAFConHttp(access, new DNSLocator(access,"https","localhost","8100"));
94                         aafTaf = new AAFTaf<HttpURLConnection>(aaf,false);
95                         aafLur = aaf.newLur(aafTaf);
96                         aafAuthn = aaf.newAuthn(aafTaf);
97                         aaf.basicAuth("testid@aaf.att.com", "whatever");
98
99                         if(perfFile==null||!perfFile.exists()) {
100                                 perfIDs.add(new CachedBasicPrincipal(aafTaf, 
101                                                 "Basic dGVzdGlkOndoYXRldmVy", 
102                                                 "aaf.att.com",timeToLive));
103                                 perfIDs.add(new Princ("ab1234@aaf.att.com")); // Example of Local ID, which isn't looked up
104                         } else {
105                                 BufferedReader ir = new BufferedReader(new FileReader(perfFile));
106                                 try {
107                                         String line;
108                                         while((line = ir.readLine())!=null) {
109                                                 if((line=line.trim()).length()>0)
110                                                         perfIDs.add(new Princ(line));
111                                         }
112                                 } finally {
113                                         ir.close();
114                                 }
115                         }
116                         Assert.assertNotNull(aafLur);
117                 }
118         }
119
120         @Before
121         public void setup() {
122                 outStream = new ByteArrayOutputStream();
123                 errStream = new ByteArrayOutputStream();
124
125                 System.setOut(new PrintStream(outStream));
126                 System.setErr(new PrintStream(errStream));
127         }
128
129         @After
130         public void tearDown() {
131                 System.setOut(System.out);
132                 System.setErr(System.err);
133         }
134
135         private static class Princ implements Principal {
136                 private String name;
137                 public Princ(String name) {
138                         this.name = name;
139                 }
140                 public String getName() {
141                         return name;
142                 }
143                 
144         };
145         
146         private static int index = -1;
147         
148         private synchronized Principal getIndex() {
149                 if(perfIDs.size()<=++index)index=0;
150                 return perfIDs.get(index);
151         }
152         @Test
153         public void test() {
154                 try {
155                                 aafAuthn.validate("testid@aaf.att.com", "whatever");
156                                 List<Permission> perms = new ArrayList<Permission>();
157                                 aafLur.fishAll(getIndex(), perms);
158 //                              Assert.assertFalse(perms.isEmpty());
159 //                              for(Permission p : perms) {
160 //                                      //access.log(Access.Level.AUDIT, p.permType());
161 //                              }
162                 } catch (Exception e) {
163                         StringWriter sw = new StringWriter();
164                         e.printStackTrace(new PrintWriter(sw));
165                         Assert.fail(sw.toString());
166                 }
167         }
168
169 }