AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / dao / aaf / test / AbsJUCass.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.auth.dao.aaf.test;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
29 import java.security.NoSuchAlgorithmException;
30 import java.util.Properties;
31
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.onap.aaf.auth.dao.CassAccess;
37 import org.onap.aaf.auth.dao.CassDAOImpl;
38 import org.onap.aaf.auth.env.AuthzEnv;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.cadi.Hash;
41 import org.onap.aaf.cadi.Symm;
42 import org.onap.aaf.misc.env.APIException;
43 import org.onap.aaf.misc.env.Env;
44 import org.onap.aaf.misc.env.Trans.Metric;
45
46 import com.datastax.driver.core.Cluster;
47
48 import junit.framework.Assert;
49
50 /**
51  * Do Setup of Cassandra for Cassandra JUnit Testing
52  * 
53  *
54  */
55 public class AbsJUCass {
56         protected static final String AUTHZ = "authz";
57         protected static Cluster cluster;
58         protected static AuthzEnv env;
59         protected static int iterations = 0;
60         protected static float totals=0.0f;
61         protected static float remote = 0.0f;
62         protected static float json = 0.0f;
63         protected static AuthzTrans trans;
64         protected static boolean details = true;
65         
66         @BeforeClass 
67         public static void startup() throws APIException, IOException {
68                 synchronized(AUTHZ) {
69                         if(env==null) {
70                                 final String resource = "cadi.properties";
71                     File f = new File("etc" + resource);
72                     InputStream is=null;
73                     Properties props = new Properties();
74                     try {
75                         if(f.exists()) {
76                             is = new FileInputStream(f);
77                         } else {
78                             URL rsrc = ClassLoader.getSystemResource(resource);
79                             is = rsrc.openStream();
80                         }
81                         props.load(is);
82                     } finally {
83                         if(is==null) {
84                                 env= new AuthzEnv();
85                             Assert.fail(resource + " must exist in etc dir, or in Classpath");
86                         }
87                         is.close();
88                     }
89                                 env = new AuthzEnv(props);
90                         }
91                 }
92                 cluster = CassAccess.cluster(env,"LOCAL");
93
94                 env.info().log("Connecting to Cluster");
95                 try {
96                         cluster.connect(AUTHZ);
97                 } catch(Exception e) {
98                         cluster=null;
99                         env.error().log(e);
100                         Assert.fail("Not able to connect to DB: " + e.getLocalizedMessage());
101                 }
102                 env.info().log("Connected");
103                 
104                 // Load special data here
105                 
106                 // WebPhone
107                 env.setProperty("java.naming.provider.url","ldap://ldap.webphone.att.com:389");
108                 env.setProperty("com.sun.jndi.ldap.connect.pool","true");
109                 
110                 iterations = 0;
111                 
112         }
113         
114         @AfterClass
115         public static void shutdown() {
116                 if(cluster!=null) {
117                         cluster.close();
118                         cluster = null;
119                 }
120         }
121
122         @Before
123         public void newTrans() {
124                 trans = env.newTrans();
125                 
126                 trans.setProperty(CassDAOImpl.USER_NAME, System.getProperty("user.name"));
127         }
128         
129         @After
130         public void auditTrail() {
131                 if(totals==0) { // "updateTotals()" was not called... just do one Trans
132                         StringBuilder sb = new StringBuilder();
133                         Metric metric = trans.auditTrail(4, sb, Env.JSON, Env.REMOTE);
134                         if(details) {
135                                 env.info().log(
136                                 sb,
137                                 "Total time:",
138                                 totals += metric.total,
139                                 "JSON time: ",
140                                 metric.buckets[0],
141                                 "REMOTE time: ",
142                                 metric.buckets[1]
143                                 );
144                         } else {
145                                 totals += metric.total;
146                         }
147                 }
148         }
149         
150         protected void updateTotals() {
151                 Metric metric = trans.auditTrail(0, null, Env.JSON, Env.REMOTE);
152                 totals+=metric.total;
153                 json  +=metric.buckets[0];
154                 remote+=metric.buckets[1];
155         }
156
157
158         @AfterClass
159         public static void print() {
160                 float transTime;
161                 if(iterations==0) {
162                         transTime=totals;
163                 } else {
164                         transTime=totals/iterations;
165                 }
166                 env.info().log(
167                 "Total time:",
168                 totals,   
169                 "JSON time:",
170                 json,
171                 "REMOTE time:",
172                 remote,
173                 "Iterations:",
174                 iterations,
175                 "Transaction time:",
176                 transTime
177                 );
178         }
179         
180         /**
181          * Take a User/Pass and turn into an MD5 Hashed BasicAuth
182          * 
183          * @param user
184          * @param pass
185          * @return
186          * @throws IOException
187          * @throws NoSuchAlgorithmException
188          */
189         //TODO: Gabe [JUnit] Issue
190         public static byte[] userPassToBytes(String user, String pass)
191                         throws IOException, NoSuchAlgorithmException {
192                 // Take the form of BasicAuth, so as to allow any character in Password
193                 // (this is an issue in 1.0)
194                 // Also, it makes it quicker to evaluate Basic Auth direct questions
195                 String ba = Symm.base64url.encode(user + ':' + pass);
196                 // Take MD5 Hash, so that data in DB can't be reversed out.
197                 return Hash.hashMD5(ba.getBytes());
198         }
199
200 }