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