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