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