1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
23 package org.onap.aaf.auth.dao.aaf.test;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
30 import java.security.NoSuchAlgorithmException;
31 import java.util.Properties;
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;
47 import com.datastax.driver.core.Cluster;
49 import junit.framework.Assert;
52 * Do Setup of Cassandra for Cassandra JUnit Testing
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;
68 public static void startup() throws APIException, IOException {
71 final String resource = "cadi.properties";
72 File f = new File("etc" + resource);
74 Properties props = new Properties();
77 is = new FileInputStream(f);
79 URL rsrc = ClassLoader.getSystemResource(resource);
80 is = rsrc.openStream();
86 Assert.fail(resource + " must exist in etc dir, or in Classpath");
90 env = new AuthzEnv(props);
93 cluster = CassAccess.cluster(env,"LOCAL");
95 env.info().log("Connecting to Cluster");
97 cluster.connect(AUTHZ);
98 } catch (Exception e) {
101 Assert.fail("Not able to connect to DB: " + e.getLocalizedMessage());
103 env.info().log("Connected");
105 // Load special data here
108 env.setProperty("java.naming.provider.url","ldap://ldap.webphone.att.com:389");
109 env.setProperty("com.sun.jndi.ldap.connect.pool","true");
116 public static void shutdown() {
124 public void newTrans() {
125 trans = env.newTrans();
127 trans.setProperty(CassDAOImpl.USER_NAME, System.getProperty("user.name"));
131 public void auditTrail() {
132 if (totals==0) { // "updateTotals()" was not called... just do one Trans
133 StringBuilder sb = new StringBuilder();
134 Metric metric = trans.auditTrail(4, sb, Env.JSON, Env.REMOTE);
139 totals += metric.total,
146 totals += metric.total;
151 protected void updateTotals() {
152 Metric metric = trans.auditTrail(0, null, Env.JSON, Env.REMOTE);
153 totals+=metric.total;
154 json +=metric.buckets[0];
155 remote+=metric.buckets[1];
160 public static void print() {
165 transTime=totals/iterations;
182 * Take a User/Pass and turn into an MD5 Hashed BasicAuth
187 * @throws IOException
188 * @throws NoSuchAlgorithmException
190 //TODO: Gabe [JUnit] Issue
191 public static byte[] userPassToBytes(String user, String pass)
192 throws IOException, NoSuchAlgorithmException {
193 // Take the form of BasicAuth, so as to allow any character in Password
194 // (this is an issue in 1.0)
195 // Also, it makes it quicker to evaluate Basic Auth direct questions
196 String ba = Symm.base64url.encode(user + ':' + pass);
197 // Take MD5 Hash, so that data in DB can't be reversed out.
198 return Hash.hashMD5(ba.getBytes());