Sonar Fixes, Formatting
[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
108         iterations = 0;
109
110     }
111
112     @AfterClass
113     public static void shutdown() {
114         if (cluster!=null) {
115             cluster.close();
116             cluster = null;
117         }
118     }
119
120     @Before
121     public void newTrans() {
122         trans = env.newTrans();
123
124         trans.setProperty(CassDAOImpl.USER_NAME, System.getProperty("user.name"));
125     }
126
127     @After
128     public void auditTrail() {
129         if (totals==0) { // "updateTotals()" was not called... just do one Trans
130             StringBuilder sb = new StringBuilder();
131             Metric metric = trans.auditTrail(4, sb, Env.JSON, Env.REMOTE);
132             if (details) {
133                 env.info().log(
134                 sb,
135                 "Total time:",
136                 totals += metric.total,
137                 "JSON time: ",
138                 metric.buckets[0],
139                 "REMOTE time: ",
140                 metric.buckets[1]
141                 );
142             } else {
143                 totals += metric.total;
144             }
145         }
146     }
147
148     protected void updateTotals() {
149         Metric metric = trans.auditTrail(0, null, Env.JSON, Env.REMOTE);
150         totals+=metric.total;
151         json  +=metric.buckets[0];
152         remote+=metric.buckets[1];
153     }
154
155
156     @AfterClass
157     public static void print() {
158         float transTime;
159         if (iterations==0) {
160             transTime=totals;
161         } else {
162             transTime=totals/iterations;
163         }
164         env.info().log(
165         "Total time:",
166         totals,
167         "JSON time:",
168         json,
169         "REMOTE time:",
170         remote,
171         "Iterations:",
172         iterations,
173         "Transaction time:",
174         transTime
175         );
176     }
177
178     /**
179      * Take a User/Pass and turn into an MD5 Hashed BasicAuth
180      *
181      * @param user
182      * @param pass
183      * @return
184      * @throws IOException
185      * @throws NoSuchAlgorithmException
186      */
187     //TODO: Gabe [JUnit] Issue
188     public static byte[] userPassToBytes(String user, String pass)
189             throws IOException, NoSuchAlgorithmException {
190         // Take the form of BasicAuth, so as to allow any character in Password
191         // (this is an issue in 1.0)
192         // Also, it makes it quicker to evaluate Basic Auth direct questions
193         String ba = Symm.base64url.encode(user + ':' + pass);
194         // Take MD5 Hash, so that data in DB can't be reversed out.
195         return Hash.hashMD5(ba.getBytes());
196     }
197
198 }