Update project structure to org.onap.aaf
[aaf/authz.git] / authz-cass / src / test / java / org / onap / aaf / dao / aaf / test / AbsJUCass.java
diff --git a/authz-cass/src/test/java/org/onap/aaf/dao/aaf/test/AbsJUCass.java b/authz-cass/src/test/java/org/onap/aaf/dao/aaf/test/AbsJUCass.java
new file mode 100644 (file)
index 0000000..887f88b
--- /dev/null
@@ -0,0 +1,200 @@
+/*******************************************************************************\r
+ * ============LICENSE_START====================================================\r
+ * * org.onap.aaf\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ *  *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ *  * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+package org.onap.aaf.dao.aaf.test;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.URL;\r
+import java.security.NoSuchAlgorithmException;\r
+import java.util.Properties;\r
+\r
+import org.junit.After;\r
+import org.junit.AfterClass;\r
+import org.junit.Before;\r
+import org.junit.BeforeClass;\r
+import org.onap.aaf.authz.env.AuthzEnv;\r
+import org.onap.aaf.authz.env.AuthzTrans;\r
+import org.onap.aaf.dao.CassAccess;\r
+import org.onap.aaf.dao.CassDAOImpl;\r
+\r
+import org.onap.aaf.cadi.Hash;\r
+import org.onap.aaf.cadi.Symm;\r
+import org.onap.aaf.inno.env.APIException;\r
+import org.onap.aaf.inno.env.Env;\r
+import org.onap.aaf.inno.env.Trans.Metric;\r
+import com.datastax.driver.core.Cluster;\r
+\r
+import junit.framework.Assert;\r
+\r
+/**\r
+ * Do Setup of Cassandra for Cassandra JUnit Testing\r
+ * \r
+ *\r
+ */\r
+public class AbsJUCass {\r
+       protected static final String AUTHZ = "authz";\r
+       protected static Cluster cluster;\r
+       protected static AuthzEnv env;\r
+       protected static int iterations = 0;\r
+       protected static float totals=0.0f;\r
+       protected static float remote = 0.0f;\r
+       protected static float json = 0.0f;\r
+       protected static AuthzTrans trans;\r
+       protected static boolean details = true;\r
+       \r
+       @BeforeClass \r
+       public static void startup() throws APIException, IOException {\r
+               synchronized(AUTHZ) {\r
+                       if(env==null) {\r
+                               final String resource = "cadi.properties";\r
+                   File f = new File("etc" + resource);\r
+                   InputStream is=null;\r
+                   Properties props = new Properties();\r
+                   try {\r
+                       if(f.exists()) {\r
+                           is = new FileInputStream(f);\r
+                       } else {\r
+                           URL rsrc = ClassLoader.getSystemResource(resource);\r
+                           is = rsrc.openStream();\r
+                       }\r
+                       props.load(is);\r
+                   } finally {\r
+                       if(is==null) {\r
+                               env= new AuthzEnv();\r
+                           Assert.fail(resource + " must exist in etc dir, or in Classpath");\r
+                       }\r
+                       is.close();\r
+                   }\r
+                               env = new AuthzEnv(props);\r
+                       }\r
+               }\r
+               cluster = CassAccess.cluster(env,"LOCAL");\r
+\r
+               env.info().log("Connecting to Cluster");\r
+               try {\r
+                       cluster.connect(AUTHZ);\r
+               } catch(Exception e) {\r
+                       cluster=null;\r
+                       env.error().log(e);\r
+                       Assert.fail("Not able to connect to DB: " + e.getLocalizedMessage());\r
+               }\r
+               env.info().log("Connected");\r
+               \r
+               // Load special data here\r
+               \r
+               // WebPhone\r
+               env.setProperty("java.naming.provider.url","ldap://ldap.webphone.att.com:389");\r
+               env.setProperty("com.sun.jndi.ldap.connect.pool","true");\r
+               \r
+               iterations = 0;\r
+               \r
+       }\r
+       \r
+       @AfterClass\r
+       public static void shutdown() {\r
+               if(cluster!=null) {\r
+                       cluster.close();\r
+                       cluster = null;\r
+               }\r
+       }\r
+\r
+       @Before\r
+       public void newTrans() {\r
+               trans = env.newTrans();\r
+               \r
+               trans.setProperty(CassDAOImpl.USER_NAME, System.getProperty("user.name"));\r
+       }\r
+       \r
+       @After\r
+       public void auditTrail() {\r
+               if(totals==0) { // "updateTotals()" was not called... just do one Trans\r
+                       StringBuilder sb = new StringBuilder();\r
+                       Metric metric = trans.auditTrail(4, sb, Env.JSON, Env.REMOTE);\r
+                       if(details) {\r
+                               env.info().log(\r
+                               sb,\r
+                               "Total time:",\r
+                               totals += metric.total,\r
+                               "JSON time: ",\r
+                               metric.buckets[0],\r
+                               "REMOTE time: ",\r
+                               metric.buckets[1]\r
+                               );\r
+                       } else {\r
+                               totals += metric.total;\r
+                       }\r
+               }\r
+       }\r
+       \r
+       protected void updateTotals() {\r
+               Metric metric = trans.auditTrail(0, null, Env.JSON, Env.REMOTE);\r
+               totals+=metric.total;\r
+               json  +=metric.buckets[0];\r
+               remote+=metric.buckets[1];\r
+       }\r
+\r
+\r
+       @AfterClass\r
+       public static void print() {\r
+               float transTime;\r
+               if(iterations==0) {\r
+                       transTime=totals;\r
+               } else {\r
+                       transTime=totals/iterations;\r
+               }\r
+               env.info().log(\r
+               "Total time:",\r
+               totals,   \r
+               "JSON time:",\r
+               json,\r
+               "REMOTE time:",\r
+               remote,\r
+               "Iterations:",\r
+               iterations,\r
+               "Transaction time:",\r
+               transTime\r
+               );\r
+       }\r
+       \r
+       /**\r
+        * Take a User/Pass and turn into an MD5 Hashed BasicAuth\r
+        * \r
+        * @param user\r
+        * @param pass\r
+        * @return\r
+        * @throws IOException\r
+        * @throws NoSuchAlgorithmException\r
+        */\r
+       public static byte[] userPassToBytes(String user, String pass)\r
+                       throws IOException, NoSuchAlgorithmException {\r
+               // Take the form of BasicAuth, so as to allow any character in Password\r
+               // (this is an issue in 1.0)\r
+               // Also, it makes it quicker to evaluate Basic Auth direct questions\r
+               String ba = Symm.base64url.encode(user + ':' + pass);\r
+               // Take MD5 Hash, so that data in DB can't be reversed out.\r
+               return Hash.encryptMD5(ba.getBytes());\r
+       }\r
+\r
+}\r