1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.dao.aaf.test;
\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
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
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
49 import junit.framework.Assert;
\r
52 * Do Setup of Cassandra for Cassandra JUnit Testing
\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
68 public static void startup() throws APIException, IOException {
\r
69 synchronized(AUTHZ) {
\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
77 is = new FileInputStream(f);
\r
79 URL rsrc = ClassLoader.getSystemResource(resource);
\r
80 is = rsrc.openStream();
\r
85 env= new AuthzEnv();
\r
86 Assert.fail(resource + " must exist in etc dir, or in Classpath");
\r
90 env = new AuthzEnv(props);
\r
93 cluster = CassAccess.cluster(env,"LOCAL");
\r
95 env.info().log("Connecting to Cluster");
\r
97 cluster.connect(AUTHZ);
\r
98 } catch(Exception e) {
\r
100 env.error().log(e);
\r
101 Assert.fail("Not able to connect to DB: " + e.getLocalizedMessage());
\r
103 env.info().log("Connected");
\r
105 // Load special data here
\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
116 public static void shutdown() {
\r
117 if(cluster!=null) {
\r
124 public void newTrans() {
\r
125 trans = env.newTrans();
\r
127 trans.setProperty(CassDAOImpl.USER_NAME, System.getProperty("user.name"));
\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
139 totals += metric.total,
\r
146 totals += metric.total;
\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
160 public static void print() {
\r
162 if(iterations==0) {
\r
165 transTime=totals/iterations;
\r
176 "Transaction time:",
\r
182 * Take a User/Pass and turn into an MD5 Hashed BasicAuth
\r
187 * @throws IOException
\r
188 * @throws NoSuchAlgorithmException
\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