diff options
Diffstat (limited to 'auth/auth-cass/src/test/java/com/att/dao')
19 files changed, 2531 insertions, 0 deletions
diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/.gitignore b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/.gitignore new file mode 100644 index 00000000..488b914c --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/.gitignore @@ -0,0 +1 @@ +/JU_OAuthAppDAO.java diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/AbsJUCass.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/AbsJUCass.java new file mode 100644 index 00000000..5b6a08c5 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/AbsJUCass.java @@ -0,0 +1,200 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.security.NoSuchAlgorithmException; +import java.util.Properties; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.CassDAOImpl; +import org.onap.aaf.auth.env.AuthzEnv; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.cadi.Hash; +import org.onap.aaf.cadi.Symm; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.Trans.Metric; + +import com.datastax.driver.core.Cluster; + +import junit.framework.Assert; + +/** + * Do Setup of Cassandra for Cassandra JUnit Testing + * + * @author Jonathan + * + */ +public class AbsJUCass { + protected static final String AUTHZ = "authz"; + protected static Cluster cluster; + protected static AuthzEnv env; + protected static int iterations = 0; + protected static float totals=0.0f; + protected static float remote = 0.0f; + protected static float json = 0.0f; + protected static AuthzTrans trans; + protected static boolean details = true; + + @BeforeClass + public static void startup() throws APIException, IOException { + try { + synchronized(AUTHZ) { + if(env==null) { + final String resource = "cadi.properties"; + File f = new File("etc" + resource); + InputStream is=null; + Properties props = new Properties(); + try { + if(f.exists()) { + is = new FileInputStream(f); + } else { + URL rsrc = ClassLoader.getSystemResource(resource); + is = rsrc.openStream(); + } + props.load(is); + } finally { + if(is==null) { + env= new AuthzEnv(); + Assert.fail(resource + " must exist in etc dir, or in Classpath"); + } + is.close(); + } + env = new AuthzEnv(props); + } + } + cluster = CassAccess.cluster(env,"LOCAL"); + + env.info().log("Connecting to Cluster"); + try { + cluster.connect(AUTHZ); + } catch(Exception e) { + cluster=null; + env.error().log(e); + Assert.fail("Not able to connect to DB: " + e.getLocalizedMessage()); + } + env.info().log("Connected"); + + // Load special data here + + iterations = 0; + } catch (Throwable t) { + t.printStackTrace(); + throw t; + } + } + + @AfterClass + public static void shutdown() { + if(cluster!=null) { + cluster.close(); + cluster = null; + } + } + + @Before + public void newTrans() { + trans = env.newTrans(); + + trans.setProperty(CassDAOImpl.USER_NAME, System.getProperty("user.name")); + } + + @After + public void auditTrail() { + if(totals==0) { // "updateTotals()" was not called... just do one Trans + StringBuilder sb = new StringBuilder(); + Metric metric = trans.auditTrail(4, sb, Env.JSON, Env.REMOTE); + if(details) { + env.info().log( + sb, + "Total time:", + totals += metric.total, + "JSON time: ", + metric.buckets[0], + "REMOTE time: ", + metric.buckets[1] + ); + } else { + totals += metric.total; + } + } + } + + protected void updateTotals() { + Metric metric = trans.auditTrail(0, null, Env.JSON, Env.REMOTE); + totals+=metric.total; + json +=metric.buckets[0]; + remote+=metric.buckets[1]; + } + + + @AfterClass + public static void print() { + float transTime; + if(iterations==0) { + transTime=totals; + } else { + transTime=totals/iterations; + } + env.info().log( + "Total time:", + totals, + "JSON time:", + json, + "REMOTE time:", + remote, + "Iterations:", + iterations, + "Transaction time:", + transTime + ); + } + + /** + * Take a User/Pass and turn into an MD5 Hashed BasicAuth + * + * @param user + * @param pass + * @return + * @throws IOException + * @throws NoSuchAlgorithmException + */ + public static byte[] userPassToBytes(String user, String pass) + throws IOException, NoSuchAlgorithmException { + // Take the form of BasicAuth, so as to allow any character in Password + // (this is an issue in 1.0) + // Also, it makes it quicker to evaluate Basic Auth direct questions + String ba = Symm.base64url.encode(user + ':' + pass); + // Take MD5 Hash, so that data in DB can't be reversed out. + return Hash.hashMD5(ba.getBytes()); + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_ApprovalDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_ApprovalDAO.java new file mode 100644 index 00000000..bc860d0a --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_ApprovalDAO.java @@ -0,0 +1,153 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.ApprovalDAO; +import org.onap.aaf.auth.dao.cass.ApprovalDAO.Data; +import org.onap.aaf.auth.layer.Result; + +public class JU_ApprovalDAO extends AbsJUCass { + @Test + public void testCRUD() throws Exception { + ApprovalDAO rrDAO = new ApprovalDAO(trans, cluster, AUTHZ); + ApprovalDAO.Data data = new ApprovalDAO.Data(); + + // Note, Create creates the time id + data.ticket = UUID.randomUUID(); // normally, read from Future object + data.user = "testid@test.com"; + data.approver = "mySuper@att.com"; + data.type = "supervisor"; + data.status = "pending"; + data.operation = "C"; + data.updated = new Date(); + data.memo = "Sing Hey for the break of day"; + data.last_notified = null; + + try { + // Test create + Result<Data> rc = rrDAO.create(trans, data); + if(rc.isOKhasData()) { // Create creates the TIMEID. + data = rc.value; + } + + // Test Read by Ticket + Result<List<ApprovalDAO.Data>> rlad; + rlad = rrDAO.readByTicket(trans, data.ticket); + assertTrue(rlad.isOK()); + assertEquals(1,rlad.value.size()); + compare(data,rlad.value.get(0)); + + // Hold onto original ID for deletion, and read tests + UUID id = rlad.value.get(0).id; + + try { + // Test Read by User + rlad = rrDAO.readByUser(trans, data.user); + assertTrue(rlad.isOKhasData()); + boolean ok = false; + for(ApprovalDAO.Data a : rlad.value) { + if(a.id.equals(id)) { + ok = true; + compare(data,a); + } + } + assertTrue(ok); + + // Test Read by Approver + rlad = rrDAO.readByApprover(trans, data.approver); + assertTrue(rlad.isOKhasData()); + ok = false; + for(ApprovalDAO.Data a : rlad.value) { + if(a.id.equals(id)) { + ok = true; + compare(data,a); + } + } + assertTrue(ok); + + // Test Read by ID + rlad = rrDAO.read(trans, id); + assertTrue(rlad.isOKhasData()); + ok = false; + for(ApprovalDAO.Data a : rlad.value) { + if(a.id.equals(id)) { + ok = true; + compare(data,a); + } + } + assertTrue(ok); + + // Test Update + data.status = "approved"; + data.id = id; + assertTrue(rrDAO.update(trans, data).isOK()); + + rlad = rrDAO.read(trans, id); + assertTrue(rlad.isOKhasData()); + ok = false; + for(ApprovalDAO.Data a : rlad.value) { + if(a.id.equals(id)) { + ok = true; + compare(data,a); + } + } + assertTrue(ok); + + } finally { + // Delete + data.id = id; + rrDAO.delete(trans, data, true); + rlad = rrDAO.read(trans, id); + assertTrue(rlad.isOK()); + assertTrue(rlad.isEmpty()); + } + + } finally { + rrDAO.close(trans); + } + } + + private void compare(Data d1, Data d2) { + assertEquals(d1.id.toString(),d2.id.toString()); + assertEquals(d1.ticket.toString(),d2.ticket.toString()); + assertEquals(d1.user,d2.user); + assertEquals(d1.approver,d2.approver); + assertEquals(d1.type,d2.type); + assertEquals(d1.status,d2.status); + assertEquals(d1.operation,d2.operation); + //assertEquals(d1.updated,d2.updated); + assertEquals(d1.memo,d2.memo); + assertEquals(d1.last_notified,d2.last_notified); + } + + + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_ArtiDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_ArtiDAO.java new file mode 100644 index 00000000..e104cbe5 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_ArtiDAO.java @@ -0,0 +1,137 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.NoSuchAlgorithmException; +import java.util.Date; +import java.util.List; + +import org.junit.Test; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.cass.ArtiDAO; +import org.onap.aaf.auth.dao.cass.ArtiDAO.Data; +import org.onap.aaf.auth.layer.Result; + +/** + * UserDAO unit test. + * Date: 7/19/13 + */ +public class JU_ArtiDAO extends AbsJUCass { + @Test + public void test() throws IOException, NoSuchAlgorithmException { + ArtiDAO adao = new ArtiDAO(trans,cluster,CassAccess.KEYSPACE); + try { + // TODO: Clean out AT&T specific data + // Create + ArtiDAO.Data data = new ArtiDAO.Data(); + data.mechid="m553j5@perturbed.onap.org"; + data.machine="perturbed1232.onap.org"; + data.type(false).add("file"); + data.type(false).add("jks"); + data.sponsor="Fred Flintstone"; + data.ca="devl"; + data.dir="/opt/app/aft/keys"; + data.ns="kumquat"; + data.os_user="aft"; + data.notify="email:fred@bogus.com"; + data.expires=new Date(); + +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new ArtiDAO.Data(); + bdata.reconstitute(bb); + checkData1(data, bdata); + + +// DB work + adao.create(trans,data); + try { + // Validate Read with key fields in Data + Result<List<ArtiDAO.Data>> rlcd = adao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(ArtiDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // Validate Read with key fields in Data + rlcd = adao.read(trans,data.mechid, data.machine); + assertTrue(rlcd.isOKhasData()); + for(ArtiDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // By Machine + rlcd = adao.readByMachine(trans,data.machine); + assertTrue(rlcd.isOKhasData()); + for(ArtiDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // By MechID + rlcd = adao.readByMechID(trans,data.mechid); + assertTrue(rlcd.isOKhasData()); + for(ArtiDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // Update + data.sponsor = "Wilma Flintstone"; + adao.update(trans,data); + rlcd = adao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(ArtiDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + } finally { + // Always delete data, even if failure. + adao.delete(trans,data, true); + } + } finally { + adao.close(trans); + } + + + } + + private void checkData1(Data data, Data d) { + assertEquals(data.mechid,d.mechid); + assertEquals(data.machine,d.machine); + assertEquals(data.type(false).size(),d.type(false).size()); + for(String s: data.type(false)) { + assertTrue(d.type(false).contains(s)); + } + assertEquals(data.sponsor,d.sponsor); + assertEquals(data.ca,d.ca); + assertEquals(data.dir,d.dir); + assertEquals(data.ns,d.ns); + assertEquals(data.os_user,d.os_user); + assertEquals(data.notify,d.notify); + assertEquals(data.expires,d.expires); + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_Bytification.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_Bytification.java new file mode 100644 index 00000000..206c52a7 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_Bytification.java @@ -0,0 +1,266 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Date; + +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.CredDAO; +import org.onap.aaf.auth.dao.cass.NsDAO; +import org.onap.aaf.auth.dao.cass.NsType; +import org.onap.aaf.auth.dao.cass.PermDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO; + +public class JU_Bytification { + + @Test + public void testNS() throws IOException { + + // Normal + NsDAO.Data ns = new NsDAO.Data(); + ns.name = "org.osaaf.whatever"; + ns.type = NsType.APP.type; + + ByteBuffer bb = ns.bytify(); + + NsDAO.Data nsr = new NsDAO.Data(); + nsr.reconstitute(bb); + check(ns,nsr); + + // Empty admin +// ns.admin(true).clear(); + bb = ns.bytify(); + nsr = new NsDAO.Data(); + nsr.reconstitute(bb); + check(ns,nsr); + + // Empty responsible +// ns.responsible(true).clear(); + bb = ns.bytify(); + nsr = new NsDAO.Data(); + nsr.reconstitute(bb); + check(ns,nsr); + + bb = ns.bytify(); + nsr = new NsDAO.Data(); + nsr.reconstitute(bb); + check(ns,nsr); + } + + private void check(NsDAO.Data a, NsDAO.Data b) { + assertEquals(a.name,b.name); + assertEquals(a.type,b.type); +// assertEquals(a.admin.size(),b.admin.size()); + +// for(String s: a.admin) { +// assertTrue(b.admin.contains(s)); +// } +// +// assertEquals(a.responsible.size(),b.responsible.size()); +// for(String s: a.responsible) { +// assertTrue(b.responsible.contains(s)); +// } + } + + @Test + public void testRole() throws IOException { + RoleDAO.Data rd1 = new RoleDAO.Data(); + rd1.ns = "org.osaaf.whatever"; + rd1.name = "my.role"; + rd1.perms(true).add("org.osaaf.whatever.my.Perm|myInstance|myAction"); + rd1.perms(true).add("org.osaaf.whatever.my.Perm|myInstance|myAction2"); + + // Normal + ByteBuffer bb = rd1.bytify(); + RoleDAO.Data rd2 = new RoleDAO.Data(); + rd2.reconstitute(bb); + check(rd1,rd2); + + // Overshoot Buffer + StringBuilder sb = new StringBuilder(300); + sb.append("role|instance|veryLongAction..."); + for(int i=0;i<280;++i) { + sb.append('a'); + } + rd1.perms(true).add(sb.toString()); + bb = rd1.bytify(); + rd2 = new RoleDAO.Data(); + rd2.reconstitute(bb); + check(rd1,rd2); + + // No Perms + rd1.perms.clear(); + + bb = rd1.bytify(); + rd2 = new RoleDAO.Data(); + rd2.reconstitute(bb); + check(rd1,rd2); + + // 1000 Perms + for(int i=0;i<1000;++i) { + rd1.perms(true).add("com|inst|action"+ i); + } + + bb = rd1.bytify(); + rd2 = new RoleDAO.Data(); + rd2.reconstitute(bb); + check(rd1,rd2); + + } + + private void check(RoleDAO.Data a, RoleDAO.Data b) { + assertEquals(a.ns,b.ns); + assertEquals(a.name,b.name); + + assertEquals(a.perms.size(),b.perms.size()); + for(String s: a.perms) { + assertTrue(b.perms.contains(s)); + } + } + + @Test + public void testPerm() throws IOException { + PermDAO.Data pd1 = new PermDAO.Data(); + pd1.ns = "org.osaaf.whatever"; + pd1.type = "my.perm"; + pd1.instance = "instance"; + pd1.action = "read"; + pd1.roles(true).add("org.osaaf.whatever.my.Role"); + pd1.roles(true).add("org.osaaf.whatever.my.Role2"); + + // Normal + ByteBuffer bb = pd1.bytify(); + PermDAO.Data rd2 = new PermDAO.Data(); + rd2.reconstitute(bb); + check(pd1,rd2); + + // No Perms + pd1.roles.clear(); + + bb = pd1.bytify(); + rd2 = new PermDAO.Data(); + rd2.reconstitute(bb); + check(pd1,rd2); + + // 1000 Perms + for(int i=0;i<1000;++i) { + pd1.roles(true).add("org.osaaf.whatever.my.Role"+ i); + } + + bb = pd1.bytify(); + rd2 = new PermDAO.Data(); + rd2.reconstitute(bb); + check(pd1,rd2); + + } + + private void check(PermDAO.Data a, PermDAO.Data b) { + assertEquals(a.ns,b.ns); + assertEquals(a.type,b.type); + assertEquals(a.instance,b.instance); + assertEquals(a.action,b.action); + + assertEquals(a.roles.size(),b.roles.size()); + for(String s: a.roles) { + assertTrue(b.roles.contains(s)); + } + } + + @Test + public void testUserRole() throws IOException { + UserRoleDAO.Data urd1 = new UserRoleDAO.Data(); +// TODO: Clean out AT&T specific data + urd1.user = "jg1555@abc.att.com"; + urd1.role("org.osaaf.whatever","my.role"); + urd1.expires = new Date(); + + // Normal + ByteBuffer bb = urd1.bytify(); + UserRoleDAO.Data urd2 = new UserRoleDAO.Data(); + urd2.reconstitute(bb); + check(urd1,urd2); + + // A null + urd1.expires = null; + urd1.role = null; + + bb = urd1.bytify(); + urd2 = new UserRoleDAO.Data(); + urd2.reconstitute(bb); + check(urd1,urd2); + } + + private void check(UserRoleDAO.Data a, UserRoleDAO.Data b) { + assertEquals(a.user,b.user); + assertEquals(a.role,b.role); + assertEquals(a.expires,b.expires); + } + + + @Test + public void testCred() throws IOException { + CredDAO.Data cd = new CredDAO.Data(); + cd.id = "m55555@abc.att.com"; + cd.ns = "org.osaaf.abc"; + cd.type = 2; + cd.cred = ByteBuffer.wrap(new byte[]{1,34,5,3,25,0,2,5,3,4}); + cd.expires = new Date(); + + // Normal + ByteBuffer bb = cd.bytify(); + CredDAO.Data cd2 = new CredDAO.Data(); + cd2.reconstitute(bb); + check(cd,cd2); + + // nulls + cd.expires = null; + cd.cred = null; + + bb = cd.bytify(); + cd2 = new CredDAO.Data(); + cd2.reconstitute(bb); + check(cd,cd2); + + } + + private void check(CredDAO.Data a, CredDAO.Data b) { + assertEquals(a.id,b.id); + assertEquals(a.ns,b.ns); + assertEquals(a.type,b.type); + if(a.cred==null) { + assertEquals(a.cred,b.cred); + } else { + int l = a.cred.limit(); + assertEquals(l,b.cred.limit()); + for (int i=0;i<l;++i) { + assertEquals(a.cred.get(),b.cred.get()); + } + } + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CacheInfoDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CacheInfoDAO.java new file mode 100644 index 00000000..87b88482 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CacheInfoDAO.java @@ -0,0 +1,64 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + + +package com.att.dao.aaf.test; + +import java.io.IOException; +import java.util.Date; + +import org.junit.Test; +import org.onap.aaf.auth.dao.CIDAO; +import org.onap.aaf.auth.dao.DAOException; +import org.onap.aaf.auth.dao.cass.CacheInfoDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.Status; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.util.Chrono; + +import junit.framework.Assert; + + +public class JU_CacheInfoDAO extends AbsJUCass { + + @Test + public void test() throws DAOException, APIException, IOException { + CIDAO<AuthzTrans> id = new CacheInfoDAO(trans, cluster, AUTHZ); + Date date = new Date(); + + id.touch(trans, RoleDAO.TABLE,1); + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + } + Result<Void> rid = id.check(trans); + Assert.assertEquals(rid.status,Status.OK); + Date[] dates = CacheInfoDAO.info.get(RoleDAO.TABLE); + if(dates.length>0 && dates[1]!=null) { + System.out.println(Chrono.dateStamp(dates[1])); + System.out.println(Chrono.dateStamp(date)); + Assert.assertTrue(Math.abs(dates[1].getTime() - date.getTime())<20000); // allow for 4 seconds, given Remote DB + } + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CertDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CertDAO.java new file mode 100644 index 00000000..cd3fb8d0 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CertDAO.java @@ -0,0 +1,103 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.security.NoSuchAlgorithmException; +import java.util.List; + +import org.junit.Test; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.cass.CertDAO; +import org.onap.aaf.auth.dao.cass.CertDAO.Data; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.APIException; + +/** + * UserDAO unit test. + * Date: 7/19/13 + */ +public class JU_CertDAO extends AbsJUCass { + @Test + public void test() throws IOException, NoSuchAlgorithmException, APIException { + CertDAO cdao = new CertDAO(trans,cluster,CassAccess.KEYSPACE); + try { + // Create + CertDAO.Data data = new CertDAO.Data(); + data.serial=new BigInteger("11839383"); + data.id = "m55555@tguard.att.com"; + data.x500="CN=ju_cert.dao.att.com, OU=AAF, O=\"ATT Services, Inc.\", L=Southfield, ST=Michigan, C=US"; + data.x509="I'm a cert"; + data.ca = "aaf"; + cdao.create(trans,data); + +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new CertDAO.Data(); + bdata.reconstitute(bb); + checkData1(data, bdata); + + // Validate Read with key fields in Data + Result<List<CertDAO.Data>> rlcd = cdao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(CertDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // Validate Read with key fields in Data + rlcd = cdao.read(trans,data.ca,data.serial); + assertTrue(rlcd.isOKhasData()); + for(CertDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // Update + data.id = "m66666.tguard.att.com"; + cdao.update(trans,data); + rlcd = cdao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(CertDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + cdao.delete(trans,data, true); + } finally { + cdao.close(trans); + } + + + } + + private void checkData1(Data data, Data d) { + assertEquals(data.ca,d.ca); + assertEquals(data.serial,d.serial); + assertEquals(data.id,d.id); + assertEquals(data.x500,d.x500); + assertEquals(data.x509,d.x509); + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CredDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CredDAO.java new file mode 100644 index 00000000..3ccc4320 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_CredDAO.java @@ -0,0 +1,250 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.NoSuchAlgorithmException; +import java.util.Date; +import java.util.List; + +import org.junit.Test; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.cass.CredDAO; +import org.onap.aaf.auth.dao.cass.CredDAO.Data; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.APIException; + +/** + * UserDAO unit test. + * Date: 7/19/13 + */ +public class JU_CredDAO extends AbsJUCass { + @Test + public void test() throws IOException, NoSuchAlgorithmException, APIException { + CredDAO udao = new CredDAO(trans,cluster,CassAccess.KEYSPACE); + try { + // Create + CredDAO.Data data = new CredDAO.Data(); + data.id = "m55555@aaf.att.com"; + data.type = CredDAO.BASIC_AUTH; + data.notes = "temp pass"; + data.cred = ByteBuffer.wrap(userPassToBytes("m55555","mypass")); + data.other = 12; + data.expires = new Date(System.currentTimeMillis() + 60000*60*24*90); + udao.create(trans,data); + +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new CredDAO.Data(); + bdata.reconstitute(bb); + checkData1(data, bdata); + + // Validate Read with key fields in Data + Result<List<CredDAO.Data>> rlcd = udao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(CredDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // Update + data.cred = ByteBuffer.wrap(userPassToBytes("m55555","mynewpass")); + udao.update(trans,data); + rlcd = udao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(CredDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + udao.delete(trans,data, true); + } finally { + udao.close(trans); + } + + + } + + private void checkData1(Data data, Data d) { + assertEquals(data.id,d.id); + assertEquals(data.type,d.type); + assertEquals(data.ns,d.ns); + assertEquals(data.notes,d.notes); + assertEquals(data.cred,d.cred); + assertEquals(data.other,d.other); + assertEquals(data.expires,d.expires); + } + +// private String CONST_myName = "MyName"; +// public static final java.nio.ByteBuffer CONST_MY_CRED = get_CONST_MY_CRED(); +// public static final int CONST_CRED_TYPE = 11; +// +// public static final Date CONST_UPDATE_DATE = new Date(System.currentTimeMillis()+60000*24); +// @Test +// public void test() { +// UserDAO ud = new UserDAO(trans, cluster,CassAccess.KEYSPACE); +// try { +// UserDAO.Data data = createPrototypeUserData(); +// ud.create(trans, data); +// +// // Validate Read with key fields in Data +// for(UserDAO.Data d : ud.read(trans, data)) { +// checkData1(data,d); +// } +// +// // Validate readByName +// for(UserDAO.Data d : ud.read(trans, CONST_myName)) { +// checkData1(data,d); +// } +// +// ud.delete(trans, data); +// List<UserDAO.Data> d_2 = ud.read(trans, CONST_myName); +// +// // Validate that data was deleted +// assertEquals("User should not be found after deleted", 0, d_2.size() ); +// +// data = new UserDAO.Data(); +// data.name = CONST_myName; +// data.cred = CONST_MY_CRED; +// data.cred_type= CONST_CRED_TYPE; +// data.expires = new Date(System.currentTimeMillis()+60000*24); +// final Result<UserDAO.Data> user = ud.r_create(trans, data); +// assertEquals("ud.createUser should work", Result.Status.OK, user.status); +// +// checkDataIgnoreDateDiff(data, user.value); +// +// // finally leave system in consistent state by deleting user again +// ud.delete(trans,data); +// +// } catch (DAOException e) { +// e.printStackTrace(); +// fail("Fail due to Exception"); +// } finally { +// ud.close(trans); +// } +// } +// +// private UserDAO.Data createPrototypeUserData() { +// UserDAO.Data data = new UserDAO.Data(); +// data.name = CONST_myName; +// +// data.cred_type = CONST_CRED_TYPE; +// data.cred = CONST_MY_CRED; +// data.expires = CONST_UPDATE_DATE; +// return data; +// } +// +// // @Test +// // public void testReadByUser() throws Exception { +// // // this test was done above in our super test, since it uses the same setup +// // } +// +// @Test +// public void testFunctionCreateUser() throws Exception { +// String name = "roger_rabbit"; +// Integer credType = CONST_CRED_TYPE; +// java.nio.ByteBuffer cred = CONST_MY_CRED; +// final UserDAO ud = new UserDAO(trans, cluster,CassAccess.KEYSPACE); +// final UserDAO.Data data = createPrototypeUserData(); +// Result<UserDAO.Data> ret = ud.r_create(trans, data); +// Result<List<Data>> byUserNameLookup = ud.r_read(trans, name); +// +// assertEquals("sanity test w/ different username (different than other test cases) failed", name, byUserNameLookup.value.get(0).name); +// assertEquals("delete roger_rabbit failed", true, ud.delete(trans, byUserNameLookup.value.get(0))); +// } +// +// @Test +// public void testLowLevelCassandraCreateData_Given_UserAlreadyPresent_ShouldPass() throws Exception { +// UserDAO ud = new UserDAO(trans, cluster,CassAccess.KEYSPACE); +// +// final UserDAO.Data data = createPrototypeUserData(); +// final UserDAO.Data data1 = ud.create(trans, data); +// final UserDAO.Data data2 = ud.create(trans, data); +// +// assertNotNull(data1); +// assertNotNull(data2); +// +// assertEquals(CONST_myName, data1.name); +// assertEquals(CONST_myName, data2.name); +// } +// +// @Test +// public void testCreateUser_Given_UserAlreadyPresent_ShouldFail() throws Exception { +// UserDAO ud = new UserDAO(trans, cluster,CassAccess.KEYSPACE); +// +// final UserDAO.Data data = createPrototypeUserData(); +// +// // make sure that some prev test did not leave the user in the DB +// ud.delete(trans, data); +// +// // attempt to create same user twice !!! +// +// final Result<UserDAO.Data> data1 = ud.r_create(trans, data); +// final Result<UserDAO.Data> data2 = ud.r_create(trans, data); +// +// assertNotNull(data1); +// assertNotNull(data2); +// +// assertEquals(true, Result.Status.OK == data1.status); +// assertEquals(false, Result.Status.OK == data2.status); +// } +// +// private void checkData1(UserDAO.Data data, UserDAO.Data d) { +// data.name = CONST_myName; +// +// data.cred_type = CONST_CRED_TYPE; +// data.cred = CONST_MY_CRED; +// data.expires = CONST_UPDATE_DATE; +// +// assertEquals(data.name, d.name); +// assertEquals(data.cred_type, d.cred_type); +// assertEquals(data.cred, d.cred); +// assertEquals(data.expires, d.expires); +// +// } +// +// private void checkDataIgnoreDateDiff(UserDAO.Data data, UserDAO.Data d) { +// data.name = CONST_myName; +// +// data.cred_type = CONST_CRED_TYPE; +// data.cred = CONST_MY_CRED; +// data.expires = CONST_UPDATE_DATE; +// +// assertEquals(data.name, d.name); +// assertEquals(data.cred_type, d.cred_type); +// assertEquals(data.cred, d.cred); +// // we allow dates to be different, e.g. high level calls e.g. createUser sets the date itself. +// //assertEquals(data.updated, d.updated); +// +// } +// +// /** +// * Get a CONST_MY_CRED ByteBuffer, which is the java type for a cass blob. +// * @return +// */ +// private static java.nio.ByteBuffer get_CONST_MY_CRED() { +// return ByteBuffer.wrap("Hello".getBytes()); +// } +// +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_DelegateDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_DelegateDAO.java new file mode 100644 index 00000000..1a4d21c4 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_DelegateDAO.java @@ -0,0 +1,108 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.nio.ByteBuffer; +import java.util.Date; +import java.util.List; + +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.DelegateDAO; +import org.onap.aaf.auth.dao.cass.DelegateDAO.Data; +import org.onap.aaf.auth.layer.Result; + + +public class JU_DelegateDAO extends AbsJUCass { + @Test + public void testCRUD() throws Exception { + DelegateDAO dao = new DelegateDAO(trans, cluster, AUTHZ); + DelegateDAO.Data data = new DelegateDAO.Data(); +// TODO: Clean out AT&T specific data + data.user = "jg1555"; + data.delegate = "rd8227"; + data.expires = new Date(); + +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new DelegateDAO.Data(); + bdata.reconstitute(bb); + compare(data, bdata); + + try { + // Test create + Result<Data> ddcr = dao.create(trans,data); + assertTrue(ddcr.isOK()); + + + // Read by User + Result<List<DelegateDAO.Data>> records = dao.read(trans,data.user); + assertTrue(records.isOKhasData()); + for(DelegateDAO.Data rdata : records.value) + compare(data,rdata); + + // Read by Delegate + records = dao.readByDelegate(trans,data.delegate); + assertTrue(records.isOKhasData()); + for(DelegateDAO.Data rdata : records.value) + compare(data,rdata); + + // Update + // TODO: Clean out AT&T specific data + data.delegate = "pf2819"; + data.expires = new Date(); + assertTrue(dao.update(trans, data).isOK()); + + // Read by User + records = dao.read(trans,data.user); + assertTrue(records.isOKhasData()); + for(DelegateDAO.Data rdata : records.value) + compare(data,rdata); + + // Read by Delegate + records = dao.readByDelegate(trans,data.delegate); + assertTrue(records.isOKhasData()); + for(DelegateDAO.Data rdata : records.value) + compare(data,rdata); + + // Test delete + dao.delete(trans,data, true); + records = dao.read(trans,data.user); + assertTrue(records.isEmpty()); + + + } finally { + dao.close(trans); + } + } + + private void compare(Data d1, Data d2) { + assertEquals(d1.user, d2.user); + assertEquals(d1.delegate, d2.delegate); + assertEquals(d1.expires,d2.expires); + } + + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_FastCalling.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_FastCalling.java new file mode 100644 index 00000000..9d3ff5db --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_FastCalling.java @@ -0,0 +1,90 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.NoSuchAlgorithmException; +import java.util.Date; +import java.util.List; + +import org.junit.Test; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.cass.CredDAO; +import org.onap.aaf.auth.dao.cass.CredDAO.Data; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.APIException; + +public class JU_FastCalling extends AbsJUCass { + + @Test + public void test() throws IOException, NoSuchAlgorithmException, APIException { + trans.setProperty("cassandra.writeConsistency.cred","ONE"); + + CredDAO udao = new CredDAO(env.newTransNoAvg(),cluster,CassAccess.KEYSPACE); + System.out.println("Starting calls"); + for(iterations=0;iterations<8;++iterations) { + try { + // Create + CredDAO.Data data = new CredDAO.Data(); + data.id = "m55555@aaf.att.com"; + data.type = CredDAO.BASIC_AUTH; + data.cred = ByteBuffer.wrap(userPassToBytes("m55555","mypass")); + data.expires = new Date(System.currentTimeMillis() + 60000*60*24*90); + udao.create(trans,data); + + // Validate Read with key fields in Data + Result<List<CredDAO.Data>> rlcd = udao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(CredDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // Update + data.cred = ByteBuffer.wrap(userPassToBytes("m55555","mynewpass")); + udao.update(trans,data); + rlcd = udao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(CredDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + udao.delete(trans,data, true); + } finally { + updateTotals(); + newTrans(); + } + } + + } + + private void checkData1(Data data, Data d) { + assertEquals(data.id,d.id); + assertEquals(data.type,d.type); + assertEquals(data.cred,d.cred); + assertEquals(data.expires,d.expires); + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_HistoryDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_HistoryDAO.java new file mode 100644 index 00000000..11d88358 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_HistoryDAO.java @@ -0,0 +1,153 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.nio.ByteBuffer; +import java.security.SecureRandom; +import java.util.List; + +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.HistoryDAO; +import org.onap.aaf.auth.layer.Result; + +public class JU_HistoryDAO extends AbsJUCass { + + @Test + public void testCreate() throws Exception { + HistoryDAO historyDAO = new HistoryDAO(trans, cluster, AUTHZ); + HistoryDAO.Data data = createHistoryData(); + + try { + historyDAO.create(trans,data); + Thread.sleep(200);// History Create is Async + Result<List<HistoryDAO.Data>> records = historyDAO.readByUser(trans,data.user,data.yr_mon); + assertTrue(records.isOKhasData()); + for(HistoryDAO.Data d : records.value) { + assertHistory(data, d); + } + } finally { + historyDAO.close(trans); + } + } + + @Test + public void tesReadByUser() throws Exception { + HistoryDAO historyDAO = new HistoryDAO(trans,cluster, AUTHZ); + HistoryDAO.Data data = createHistoryData(); + + try { + historyDAO.create(trans,data); + Thread.sleep(200);// History Create is Async + Result<List<HistoryDAO.Data>> records = historyDAO.readByUser(trans, data.user,data.yr_mon); + assertTrue(records.isOKhasData()); + for(HistoryDAO.Data d : records.value) { + assertHistory(data, d); + } + } finally { + historyDAO.close(trans); + } + } + +/* + @Test + public void readByUserAndMonth() throws Exception { + HistoryDAO historyDAO = new HistoryDAO(trans,cluster, AUTHZ); + HistoryDAO.Data data = createHistoryData(); + + try { + historyDAO.create(trans,data); + Thread.sleep(200);// History Create is Async + Result<List<HistoryDAO.Data>> records = historyDAO.readByUserAndMonth(trans, + data.user, Integer.valueOf(String.valueOf(data.yr_mon).substring(0, 4)), + Integer.valueOf(String.valueOf(data.yr_mon).substring(4, 6))); + assertTrue(records.isOKhasData()); + for(HistoryDAO.Data d : records.value) { + assertHistory(data, d); + } + } finally { + historyDAO.close(trans); + } + } +*/ + //TODO readadd this +// @Test +// public void readByUserAndDay() throws Exception { +// HistoryDAO historyDAO = new HistoryDAO(trans, cluster, AUTHZ); +// HistoryDAO.Data data = createHistoryData(); +// +// try { +// historyDAO.create(trans, data); +// Thread.sleep(200);// History Create is Async +// +// String dayTime = String.valueOf(data.day_time); +// String day = null; +// if (dayTime.length() < 8) +// day = dayTime.substring(0, 1); +// else +// day = dayTime.substring(0, 2); +// +// List<HistoryDAO.Data> records = historyDAO.readByUserBetweenDates(trans, +// data.user, Integer.valueOf(String.valueOf(data.yr_mon).substring(0, 4)), +// Integer.valueOf(String.valueOf(data.yr_mon).substring(4, 6)), +// Integer.valueOf(day), 0); +// assertEquals(1,records.size()); +// for(HistoryDAO.Data d : records) { +// assertHistory(data, d); +// } +// } finally { +// historyDAO.close(trans); +// } +// } + private HistoryDAO.Data createHistoryData() { + HistoryDAO.Data data = HistoryDAO.newInitedData(); + SecureRandom random = new SecureRandom(); + data.user = "test" + random.nextInt(); + data.action = "add"; + data.target = "history"; + data.memo = "adding a row into history table"; +// data.detail().put("id", "test"); +// data.detail().put("name", "test"); + //String temp = "Test Blob Message"; + data.reconstruct = ByteBuffer.wrap("Temp Blob Message".getBytes()); + return data; + } + + private void assertHistory(HistoryDAO.Data ip, HistoryDAO.Data op) { + assertEquals(ip.yr_mon, op.yr_mon); +// assertEquals(ip.day_time, op.day_time); + assertEquals(ip.user, op.user); + assertEquals(ip.action, op.action); + assertEquals(ip.target, op.target); + assertEquals(ip.memo, op.memo); + //TODO : have to see if third party assert utility can be used +// assertTrue(CollectionUtils.isEqualCollection(ip.detail, op.detail)); +// for (String key : ip.detail().keySet()) { +// assertNotNull(op.detail().get(key)); +// } + assertNotNull(op.reconstruct); + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_LocateDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_LocateDAO.java new file mode 100644 index 00000000..283a3563 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_LocateDAO.java @@ -0,0 +1,146 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Set; + +import org.junit.Test; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.cass.LocateDAO; +import org.onap.aaf.auth.dao.cass.LocateDAO.Data; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.APIException; + +/** + * Test the LocateDAO + * + * Utilize AbsJUCass to initialize and pre-load Cass + * + * @author Jonathan + * + */ +public class JU_LocateDAO extends AbsJUCass{ + + @Test + public void test() throws APIException, IOException { + LocateDAO pd = new LocateDAO(trans,cluster,CassAccess.KEYSPACE); + try { + LocateDAO.Data data = new LocateDAO.Data(); + data.name="org.osaaf.aaf.locateTester"; + data.hostname="mithrilcsp.sbc.com"; + data.port=19999; + data.latitude=32.780140f; + data.longitude=-96.800451f; + data.major=2; + data.minor=0; + data.patch=19; + data.pkg=23; + data.protocol="https"; + Set<String> sp = data.subprotocol(true); + sp.add("TLS1.1"); + sp.add("TLS1.2"); + + + + // CREATE + Result<Data> rpdc = pd.create(trans,data); + assertTrue(rpdc.isOK()); + + Result<List<LocateDAO.Data>> rlpd; + try { +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new LocateDAO.Data(); + bdata.reconstitute(bb); + compare(data, bdata); + + // Validate Read with key fields in Data + rlpd = pd.read(trans,data); + assertTrue(rlpd.isOK()); + if(rlpd.isOK()) { + for(LocateDAO.Data d : rlpd.value) { + compare(data,d); + } + } + + // Validate Read by Name + rlpd = pd.readByName(trans,data.name); + assertTrue(rlpd.isOK()); + if(rlpd.isOK()) { + for(LocateDAO.Data d : rlpd.value) { + compare(data,d); + } + } + + // Modify + data.latitude = -31.0000f; + + Result<Void> rupd = pd.update(trans, data); + assertTrue(rupd.isOK()); + rlpd = pd.read(trans,data); + assertTrue(rlpd.isOK()); + if(rlpd.isOK()) { + for(LocateDAO.Data d : rlpd.value) { + compare(data,d); + } + } + + } catch (IOException e) { + e.printStackTrace(); + } finally { + // DELETE + Result<Void> rpdd = pd.delete(trans,data,true); + assertTrue(rpdd.isOK()); + rlpd = pd.read(trans, data); + assertTrue(rlpd.isOK() && rlpd.isEmpty()); + assertEquals(rlpd.value.size(),0); + } + } finally { + pd.close(trans); + } + } + + private void compare(Data a, Data b) { + assertEquals(a.name,b.name); + assertEquals(a.hostname,b.hostname); + assertEquals(a.port,b.port); + assertEquals(a.major,b.major); + assertEquals(a.minor,b.minor); + assertEquals(a.patch,b.patch); + assertEquals(a.pkg,b.pkg); + assertEquals(a.latitude,b.latitude); + assertEquals(a.longitude,b.longitude); + assertEquals(a.protocol,b.protocol); + Set<String> spa = a.subprotocol(false); + Set<String> spb = b.subprotocol(false); + assertEquals(spa.size(),spb.size()); + for(String s : spa) { + assertTrue(spb.contains(s)); + } + } +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_LocationContent.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_LocationContent.java new file mode 100644 index 00000000..39f096c1 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_LocationContent.java @@ -0,0 +1,93 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Data; +import org.onap.aaf.misc.env.Data.TYPE; +import org.onap.aaf.misc.rosetta.env.RosettaDF; +import org.onap.aaf.misc.rosetta.env.RosettaEnv; + +import locate.v1_0.MgmtEndpoint; +import locate.v1_0.MgmtEndpoint.SpecialPorts; +import locate.v1_0.MgmtEndpoints; + +public class JU_LocationContent { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + PropAccess access = new PropAccess(); + RosettaEnv env = new RosettaEnv(access.getProperties()); + try { + RosettaDF<MgmtEndpoints> medf = env.newDataFactory(MgmtEndpoints.class); + medf.out(TYPE.JSON); + medf.option(Data.PRETTY); + MgmtEndpoint me = new MgmtEndpoint(); + me.setHostname("mithrilcsp.sbc.com"); + me.setLatitude(32); + me.setLongitude(-90); + me.setMajor(2); + me.setMinor(0); + me.setPatch(19); + me.setPort(3312); + me.setProtocol("http"); + me.getSubprotocol().add("TLS1.1"); + + SpecialPorts sp = new SpecialPorts(); + sp.setName("debug"); + sp.setPort(9000); + sp.setProtocol("java"); + me.getSpecialPorts().add(sp); + + MgmtEndpoints mes = new MgmtEndpoints(); + mes.getMgmtEndpoint().add(me); + System.out.println(medf.newData().load(mes).asString()); + + } catch (APIException e) { + e.printStackTrace(); + } + + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_NsDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_NsDAO.java new file mode 100644 index 00000000..5b313419 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_NsDAO.java @@ -0,0 +1,187 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.NsDAO; +import org.onap.aaf.auth.dao.cass.NsType; +import org.onap.aaf.auth.dao.cass.NsDAO.Data; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.APIException; + + +public class JU_NsDAO extends AbsJUCass { + private static final String CRM = "ju_crm"; + private static final String SWM = "ju_swm"; + + @Test + public void test() throws APIException, IOException { + NsDAO nsd = new NsDAO(trans, cluster, AUTHZ); + try { + final String nsparent = "com.test"; + final String ns1 = nsparent +".ju_ns"; + final String ns2 = nsparent + ".ju_ns2"; + + Map<String,String> oAttribs = new HashMap<String,String>(); + oAttribs.put(SWM, "swm_data"); + oAttribs.put(CRM, "crm_data"); + Data data = new NsDAO.Data(); + data.name = ns1; + data.type = NsType.APP.type; + data.parent = nsparent; + data.attrib(true).putAll(oAttribs); + + + Result<List<Data>> rdrr; + + // CREATE + Result<Data> rdc = nsd.create(trans, data); + assertTrue(rdc.isOK()); + + try { +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new NsDAO.Data(); + bdata.reconstitute(bb); + compare(data, bdata); + + // Test READ by Object + rdrr = nsd.read(trans, data); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),1); + Data d = rdrr.value.get(0); + assertEquals(d.name,data.name); + assertEquals(d.type,data.type); + attribsEqual(d.attrib(false),data.attrib(false)); + attribsEqual(oAttribs,data.attrib(false)); + + // Test Read by Key + rdrr = nsd.read(trans, data.name); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),1); + d = rdrr.value.get(0); + assertEquals(d.name,data.name); + assertEquals(d.type,data.type); + attribsEqual(d.attrib(false),data.attrib(false)); + attribsEqual(oAttribs,data.attrib(false)); + + // Read NS by Type + Result<Set<String>> rtypes = nsd.readNsByAttrib(trans, SWM); + Set<String> types; + if(rtypes.notOK()) { + throw new IOException(rtypes.errorString()); + } else { + types = rtypes.value; + } + assertEquals(1,types.size()); + assertEquals(true,types.contains(ns1)); + + // Add second NS to test list of data returned + Data data2 = new NsDAO.Data(); + data2.name = ns2; + data2.type = 3; // app + data2.parent = nsparent; + Result<Data> rdc2 = nsd.create(trans, data2); + assertTrue(rdc2.isOK()); + + // Interrupt - test PARENT + Result<List<Data>> rdchildren = nsd.getChildren(trans, "com.test"); + assertTrue(rdchildren.isOKhasData()); + boolean child1 = false; + boolean child2 = false; + for(Data dchild : rdchildren.value) { + if(ns1.equals(dchild.name))child1=true; + if(ns2.equals(dchild.name))child2=true; + } + assertTrue(child1); + assertTrue(child2); + + // FINISH DATA 2 by deleting + Result<Void> rddr = nsd.delete(trans, data2, true); + assertTrue(rddr.isOK()); + + // ADD DESCRIPTION + String description = "This is my test Namespace"; + assertFalse(description.equalsIgnoreCase(data.description)); + + Result<Void> addDesc = nsd.addDescription(trans, data.name, description); + assertTrue(addDesc.isOK()); + rdrr = nsd.read(trans, data); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),1); + assertEquals(rdrr.value.get(0).description,description); + + // UPDATE + String newDescription = "zz1234 Owns This Namespace Now"; + oAttribs.put("mso", "mso_data"); + data.attrib(true).put("mso", "mso_data"); + data.description = newDescription; + Result<Void> update = nsd.update(trans, data); + assertTrue(update.isOK()); + rdrr = nsd.read(trans, data); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),1); + assertEquals(rdrr.value.get(0).description,newDescription); + attribsEqual(oAttribs, rdrr.value.get(0).attrib); + + + } catch (IOException e) { + e.printStackTrace(); + } finally { + // DELETE + Result<Void> rddr = nsd.delete(trans, data, true); + assertTrue(rddr.isOK()); + rdrr = nsd.read(trans, data); + assertTrue(rdrr.isOK() && rdrr.isEmpty()); + assertEquals(rdrr.value.size(),0); + } + } finally { + nsd.close(trans); + } + } + + private void compare(NsDAO.Data d, NsDAO.Data data) { + assertEquals(d.name,data.name); + assertEquals(d.type,data.type); + attribsEqual(d.attrib(false),data.attrib(false)); + attribsEqual(d.attrib(false),data.attrib(false)); + } + + private void attribsEqual(Map<String,String> aa, Map<String,String> ba) { + assertEquals(aa.size(),ba.size()); + for(Entry<String, String> es : aa.entrySet()) { + assertEquals(es.getValue(),ba.get(es.getKey())); + } + } +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_NsType.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_NsType.java new file mode 100644 index 00000000..2644fedd --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_NsType.java @@ -0,0 +1,58 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.AfterClass; +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.NsType; + +public class JU_NsType { + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Test + public void test() { + NsType nt,nt2; + String[] tests = new String[] {"DOT","ROOT","COMPANY","APP","STACKED_APP","STACK"}; + for(String s : tests) { + nt = NsType.valueOf(s); + assertEquals(s,nt.name()); + + nt2 = NsType.fromString(s); + assertEquals(nt,nt2); + + int t = nt.type; + nt2 = NsType.fromType(t); + assertEquals(nt,nt2); + } + + nt = NsType.fromType(Integer.MIN_VALUE); + assertEquals(nt,NsType.UNKNOWN); + nt = NsType.fromString("Garbage"); + assertEquals(nt,NsType.UNKNOWN); + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_OAuthTokenDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_OAuthTokenDAO.java new file mode 100644 index 00000000..f3f91d00 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_OAuthTokenDAO.java @@ -0,0 +1,134 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.NoSuchAlgorithmException; +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import org.junit.Test; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.cass.OAuthTokenDAO; +import org.onap.aaf.auth.dao.cass.OAuthTokenDAO.Data; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.oauth.AAFToken; + +/** + * UserDAO unit test. + * Date: 7/19/13 + */ +public class JU_OAuthTokenDAO extends AbsJUCass { + @Test + public void test() throws IOException, NoSuchAlgorithmException { + OAuthTokenDAO adao = new OAuthTokenDAO(trans,cluster,CassAccess.KEYSPACE); + UUID uuid = UUID.randomUUID(); + try { + // Create + Data data = new OAuthTokenDAO.Data(); + data.id=AAFToken.toToken(uuid); + data.client_id="zClient"; + data.user = "xy1255@csp.att.com"; + data.active = true; + data.type=1; + data.refresh = AAFToken.toToken(UUID.randomUUID()); + data.expires=new Date(); + data.scopes(false).add("org.osaaf.aaf"); + data.scopes(false).add("org.osaaf.grid"); + data.content="{darth:\"I am your content\"}"; + data.req_ip="::1"; + +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new OAuthTokenDAO.Data(); +// System.out.println(new String(Symm.base64noSplit.encode(bb.array()))); + bdata.reconstitute(bb); + checkData1(data, bdata); + +// DB work + adao.create(trans,data); + try { + // Validate Read with Data Object + Result<List<OAuthTokenDAO.Data>> rlcd = adao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(OAuthTokenDAO.Data d : rlcd.value) { + checkData1(data,d); + } + // Validate Read with key fields in Data + rlcd = adao.read(trans,data.id); + assertTrue(rlcd.isOKhasData()); + for(OAuthTokenDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // Validate Read by User + rlcd = adao.readByUser(trans,data.user); + assertTrue(rlcd.isOKhasData()); + for(OAuthTokenDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + // Update + data.content = "{darth:\"I am your content\", luke:\"Noooooooo!\"}"; + data.active = false; + adao.update(trans,data); + rlcd = adao.read(trans,data); + assertTrue(rlcd.isOKhasData()); + for(OAuthTokenDAO.Data d : rlcd.value) { + checkData1(data,d); + } + + } finally { + // Always delete data, even if failure. + adao.delete(trans,data, true); + } + } finally { + adao.close(trans); + } + + } + + private void checkData1(Data data, Data d) { + assertEquals(data.id,d.id); + assertEquals(data.client_id,d.client_id); + assertEquals(data.user,d.user); + assertEquals(data.active,d.active); + assertEquals(data.type,d.type); + assertEquals(data.refresh,d.refresh); + assertEquals(data.expires,d.expires); + for(String s: data.scopes(false)) { + assertTrue(d.scopes(false).contains(s)); + } + for(String s: d.scopes(false)) { + assertTrue(data.scopes(false).contains(s)); + } + assertEquals(data.content,d.content); + assertEquals(data.state,d.state); + assertEquals(data.req_ip,d.req_ip); + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_PermDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_PermDAO.java new file mode 100644 index 00000000..0a506db1 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_PermDAO.java @@ -0,0 +1,176 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Set; + +import org.junit.Test; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.cass.PermDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.PermDAO.Data; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.APIException; + +/** + * Test the PermissionDAO + * + * Utilize AbsJUCass to initialize and pre-load Cass + * + * @author Jonathan + * + */ +public class JU_PermDAO extends AbsJUCass{ + + @Test + public void test() throws APIException, IOException { + PermDAO pd = new PermDAO(trans,cluster,CassAccess.KEYSPACE); + try { + PermDAO.Data data = new PermDAO.Data(); + data.ns = "com.test.ju_perm"; + data.type = "MyType"; + data.instance = "MyInstance"; + data.action = "MyAction"; + data.roles(true).add(data.ns + ".dev"); + + + + // CREATE + Result<Data> rpdc = pd.create(trans,data); + assertTrue(rpdc.isOK()); + + Result<List<PermDAO.Data>> rlpd; + try { +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new PermDAO.Data(); + bdata.reconstitute(bb); + compare(data, bdata); + + // Validate Read with key fields in Data + if((rlpd = pd.read(trans,data)).isOK()) + for(PermDAO.Data d : rlpd.value) { + checkData1(data,d); + } + + // Validate readByName + if((rlpd = pd.readByType(trans,data.ns, data.type)).isOK()) + for(PermDAO.Data d : rlpd.value) { + checkData1(data,d); + } + + // Add Role + RoleDAO.Data role = new RoleDAO.Data(); + role.ns = data.ns; + role.name = "test"; + + Result<Void> rvpd = pd.addRole(trans, data, role.fullName()); + assertTrue(rvpd.isOK()); + // Validate Read with key fields in Data + if((rlpd = pd.read(trans,data)).isOK()) + for(PermDAO.Data d : rlpd.value) { + checkData2(data,d); + } + + // Remove Role + rvpd = pd.delRole(trans, data, role.fullName()); + assertTrue(rvpd.isOK()); + if((rlpd = pd.read(trans,data)).isOK()) + for(PermDAO.Data d : rlpd.value) { + checkData1(data,d); + } + + // Add Child + Data data2 = new Data(); + data2.ns = data.ns; + data2.type = data.type + ".2"; + data2.instance = data.instance; + data2.action = data.action; + + rpdc = pd.create(trans, data2); + assertTrue(rpdc.isOK()); + try { + rlpd = pd.readChildren(trans, data.ns,data.type); + assertTrue(rlpd.isOKhasData()); + assertEquals(rlpd.value.size(),1); + assertEquals(rlpd.value.get(0).fullType(),data2.fullType()); + } finally { + // Delete Child + pd.delete(trans, data2,true); + + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + // DELETE + Result<Void> rpdd = pd.delete(trans,data,true); + assertTrue(rpdd.isOK()); + rlpd = pd.read(trans, data); + assertTrue(rlpd.isOK() && rlpd.isEmpty()); + assertEquals(rlpd.value.size(),0); + } + } finally { + pd.close(trans); + } + } + + private void compare(Data a, Data b) { + assertEquals(a.ns,b.ns); + assertEquals(a.type,b.type); + assertEquals(a.instance,b.instance); + assertEquals(a.action,b.action); + assertEquals(a.roles(false).size(),b.roles(false).size()); + for(String s: a.roles(false)) { + assertTrue(b.roles(false).contains(s)); + } + } + private void checkData1(Data data, Data d) { + assertEquals(data.ns,d.ns); + assertEquals(data.type,d.type); + assertEquals(data.instance,d.instance); + assertEquals(data.action,d.action); + + Set<String> ss = d.roles(true); + assertEquals(1,ss.size()); + assertTrue(ss.contains(data.ns+".dev")); + } + + private void checkData2(Data data, Data d) { + assertEquals(data.ns,d.ns); + assertEquals(data.type,d.type); + assertEquals(data.instance,d.instance); + assertEquals(data.action,d.action); + + Set<String> ss = d.roles(true); + assertEquals(2,ss.size()); + assertTrue(ss.contains(data.ns+".dev")); + assertTrue(ss.contains(data.ns+".test")); + } + + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_RoleDAO.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_RoleDAO.java new file mode 100644 index 00000000..56875bdd --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/JU_RoleDAO.java @@ -0,0 +1,138 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + + +package com.att.dao.aaf.test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; + +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.PermDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO.Data; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.APIException; + + +public class JU_RoleDAO extends AbsJUCass { + + @Test + public void test() throws IOException, APIException { + RoleDAO rd = new RoleDAO(trans, cluster, AUTHZ); + try { + Data data = new RoleDAO.Data(); + data.ns = "com.test.ju_role"; + data.name = "role1"; + +// Bytification + ByteBuffer bb = data.bytify(); + Data bdata = new RoleDAO.Data(); + bdata.reconstitute(bb); + compare(data, bdata); + + // CREATE + Result<Data> rdc = rd.create(trans, data); + assertTrue(rdc.isOK()); + Result<List<Data>> rdrr; + try { + // READ + rdrr = rd.read(trans, data); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),1); + Data d = rdrr.value.get(0); + assertEquals(d.perms.size(),0); + assertEquals(d.name,data.name); + assertEquals(d.ns,data.ns); + + PermDAO.Data perm = new PermDAO.Data(); + perm.ns = data.ns; + perm.type = "Perm"; + perm.instance = "perm1"; + perm.action = "write"; + + // ADD Perm + Result<Void> rdar = rd.addPerm(trans, data, perm); + assertTrue(rdar.isOK()); + rdrr = rd.read(trans, data); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),1); + assertEquals(rdrr.value.get(0).perms.size(),1); + assertTrue(rdrr.value.get(0).perms.contains(perm.encode())); + + // DEL Perm + rdar = rd.delPerm(trans, data,perm); + assertTrue(rdar.isOK()); + rdrr = rd.read(trans, data); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),1); + assertEquals(rdrr.value.get(0).perms.size(),0); + + // Add Child + Data data2 = new Data(); + data2.ns = data.ns; + data2.name = data.name + ".2"; + + rdc = rd.create(trans, data2); + assertTrue(rdc.isOK()); + try { + rdrr = rd.readChildren(trans, data.ns,data.name); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),1); + assertEquals(rdrr.value.get(0).name,data.name + ".2"); + + rdrr = rd.readChildren(trans, data.ns,"*"); + assertTrue(rdrr.isOKhasData()); + assertEquals(rdrr.value.size(),2); + + } finally { + // Delete Child + rd.delete(trans, data2, true); + } + + } finally { + // DELETE + Result<Void> rddr = rd.delete(trans, data, true); + assertTrue(rddr.isOK()); + rdrr = rd.read(trans, data); + assertTrue(rdrr.isOK() && rdrr.isEmpty()); + assertEquals(rdrr.value.size(),0); + } + } finally { + rd.close(trans); + } + } + + private void compare(Data a, Data b) { + assertEquals(a.name,b.name); + assertEquals(a.description, b.description); + assertEquals(a.ns,b.ns); + assertEquals(a.perms(false).size(),b.perms(false).size()); + for(String p : a.perms(false)) { + assertTrue(b.perms(false).contains(p)); + } + } + +} diff --git a/auth/auth-cass/src/test/java/com/att/dao/aaf/test/NS_ChildUpdate.java b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/NS_ChildUpdate.java new file mode 100644 index 00000000..8e2f78b6 --- /dev/null +++ b/auth/auth-cass/src/test/java/com/att/dao/aaf/test/NS_ChildUpdate.java @@ -0,0 +1,74 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package com.att.dao.aaf.test; + +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.env.AuthzEnv; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.Session; + +public class NS_ChildUpdate { + + public static void main(String[] args) { + if(args.length < 3 ) { + System.out.println("usage: NS_ChildUpdate machine mechid (encrypted)passwd"); + } else { + try { + AuthzEnv env = new AuthzEnv(); + env.setLog4JNames("log.properties","authz","authz","audit","init","trace"); + + Cluster cluster = Cluster.builder() + .addContactPoint(args[0]) + .withCredentials(args[1],env.decrypt(args[2], false)) + .build(); + + Session session = cluster.connect(CassAccess.KEYSPACE); + try { + ResultSet result = session.execute("SELECT name,parent FROM ns"); + int count = 0; + for(Row r : result.all()) { + ++count; + String name = r.getString(0); + String parent = r.getString(1); + if(parent==null) { + int idx = name.lastIndexOf('.'); + + parent = idx>0?name.substring(0, idx):"."; + System.out.println("UPDATE " + name + " to " + parent); + session.execute("UPDATE ns SET parent='" + parent + "' WHERE name='" + name + "';"); + } + } + System.out.println("Processed " + count + " records"); + } finally { + session.close(); + cluster.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + +} |