Remove spaces from dockerbuild script
[aaf/authz.git] / auth / auth-cass / src / test / java / com / att / dao / aaf / test / AbsJUCass.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 com.att.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  * @author Jonathan
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                 try {
70                         synchronized(AUTHZ) {
71                                 if(env==null) {
72                                         final String resource = "cadi.properties";
73                             File f = new File("etc" + resource);
74                             InputStream is=null;
75                             Properties props = new Properties();
76                             try {
77                                 if(f.exists()) {
78                                     is = new FileInputStream(f);
79                                 } else {
80                                     URL rsrc = ClassLoader.getSystemResource(resource);
81                                     is = rsrc.openStream();
82                                 }
83                                 props.load(is);
84                             } finally {
85                                 if(is==null) {
86                                         env= new AuthzEnv();
87                                     Assert.fail(resource + " must exist in etc dir, or in Classpath");
88                                 }
89                                 is.close();
90                             }
91                                         env = new AuthzEnv(props);
92                                 }
93                         }
94                         cluster = CassAccess.cluster(env,"LOCAL");
95         
96                         env.info().log("Connecting to Cluster");
97                         try {
98                                 cluster.connect(AUTHZ);
99                         } catch(Exception e) {
100                                 cluster=null;
101                                 env.error().log(e);
102                                 Assert.fail("Not able to connect to DB: " + e.getLocalizedMessage());
103                         }
104                         env.info().log("Connected");
105                         
106                         // Load special data here
107                         
108                         iterations = 0;
109                 } catch (Throwable t) {
110                         t.printStackTrace();
111                         throw t;
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         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 }