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