diff options
author | Sai Gandham <sg481n@att.com> | 2019-05-20 21:05:42 -0500 |
---|---|---|
committer | Sai Gandham <sg481n@att.com> | 2019-05-20 21:06:13 -0500 |
commit | 1876581ffb0b9c83021bd422826c76fa8b125bf0 (patch) | |
tree | 47481798664d057a469154fe0af83f6f200acb74 /auth | |
parent | 685a3eea830c15d2b26a1f2851251203fa4f6156 (diff) |
Add junits to auth cass
Issue-ID: AAF-111
Change-Id: I65684b72d3b9e9bb9314345b2dd6efd1c5efa4f2
Signed-off-by: Sai Gandham <sg481n@att.com>
Diffstat (limited to 'auth')
9 files changed, 3190 insertions, 11 deletions
diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedRoleDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedRoleDAO.java new file mode 100644 index 00000000..f2bdad2d --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedRoleDAO.java @@ -0,0 +1,175 @@ +/** + * ============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 org.onap.aaf.auth.dao.cached; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.auth.dao.CIDAO; +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.env.AuthzTrans; +import org.onap.aaf.auth.env.AuthzTransImpl; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.LogTarget; + + +@RunWith(MockitoJUnitRunner.class) +public class JU_CachedRoleDAO { + + @Mock + RoleDAO dao; + + @Mock + CIDAO<AuthzTrans> info; + + @Mock + AuthzTransImpl trans; + + @Mock + RoleDAO.Data data; + + @Mock + PermDAO.Data permData; + + @Before + public void setUp() throws Exception { + initMocks(this); + when(trans.debug()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + } + + @Test + public void testReadNameSuccess() { + CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test4",new String[0]); + roleDaoObj.invalidate(""); + Mockito.doReturn(retVal1).when(dao).readName(trans, "test4"); +// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); + Result<List<Data>> retVal = roleDaoObj.readName(trans, "test4"); +// System.out.println(retVal.status); + //retVal.status = 0; + assertEquals("1", Integer.toString(retVal.status)); + } + + @Test + public void testReadNameFailure() { + CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test3123",new String[0]); + Mockito.doReturn(retVal1).when(dao).readName(trans, "test3"); +// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); + Result<List<Data>> retVal = roleDaoObj.readName(trans, "test3"); +// System.out.println(retVal.status); + assertEquals("22", Integer.toString(retVal.status)); + } + @Test + public void testReadNSSuccess() { + CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal1).when(dao).readNS(trans, ""); +// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); + Result<List<Data>> retVal = roleDaoObj.readNS(trans, ""); +// System.out.println(retVal.status); + assertEquals("1", Integer.toString(retVal.status)); + } + @Test + public void testReadNSFailure() { + CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]); + Mockito.doReturn(retVal1).when(dao).readNS(trans, ""); +// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); + Result<List<Data>> retVal = roleDaoObj.readNS(trans, ""); +// System.out.println(retVal.status); + assertEquals("22", Integer.toString(retVal.status)); + } + + @Test + public void testReadChildren() { + CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<List<Data>> retVal1 = new Result<List<Data>>(null,0,"test1",new String[0]); + Mockito.doReturn(retVal1).when(dao).readChildren(trans, "",""); + Result<List<Data>> retVal = roleDaoObj.readChildren(trans, "", ""); + //System.out.println(retVal.status); + assertEquals("0", Integer.toString(retVal.status)); + } + + @Test + public void testAddPerm() { + CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10); + Result<Void> retVal1 = new Result<Void>(null,0,"testAddPerm",new String[0]); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).addPerm(trans, data,permData); + Result<Void> retVal = roleDaoObj.addPerm(trans, data, permData); + assertEquals("testAddPerm", retVal.toString()); + } + + @Test + public void testDelPerm() { + CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10); + Result<Void> retVal1 = new Result<Void>(null,0,"testAddPerm",new String[0]); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).delPerm(trans, data,permData); + Result<Void> retVal = roleDaoObj.delPerm(trans, data, permData); + System.out.println(retVal); + assertEquals("testAddPerm", retVal.toString()); + } + + @Test + public void testAddDescription() { + CachedRoleDAO roleDaoObj =new CachedRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result<Void> retVal1 = new Result<Void>(null,0,"test1",new String[0]); + Mockito.doReturn(retVal1).when(dao).addDescription(trans, "","",""); + Result<Void> retVal = roleDaoObj.addDescription(trans, "", "",""); + //System.out.println(retVal.status); + assertEquals("0", Integer.toString(retVal.status)); + } +}
\ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_CertDAOTest.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_CertDAOTest.java index a4b24c37..3b2352c0 100644 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_CertDAOTest.java +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_CertDAOTest.java @@ -226,6 +226,7 @@ public class JU_CertDAOTest { Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on CertDAO", Env.SUB); Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("CertDAOImpl READ",Env.REMOTE); Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); @@ -242,9 +243,9 @@ public class JU_CertDAOTest { CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); Mockito.doReturn(rs1).when(cacheInfoDAO).touch(trans, CertDAO.TABLE, new int[1]); - CertDAO daoObj = null; + CertDAOImpl daoObj = null; try { - daoObj = new CertDAO(trans, historyDAO, cacheInfoDAO); + daoObj = new CertDAOImpl(trans, historyDAO, cacheInfoDAO, createPS); } catch (APIException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -328,8 +329,8 @@ class CertDAOImpl extends CertDAO{ // setSession(this, Mockito.mock(Session.class)); } - public CertDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,PSInfo readPS ) throws APIException, IOException { - super(trans, cluster, keyspace); + public CertDAOImpl(AuthzTrans trans, HistoryDAO historyDAO, CacheInfoDAO cacheInfoDAO,PSInfo readPS ) throws APIException, IOException { + super(trans, historyDAO, cacheInfoDAO); this.readPS = readPS; } diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_DelegateDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_DelegateDAO.java index 451cc720..88d71a99 100644 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_DelegateDAO.java +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_DelegateDAO.java @@ -23,11 +23,6 @@ package org.onap.aaf.auth.dao.cass; import static org.mockito.MockitoAnnotations.initMocks; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -36,7 +31,6 @@ import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.util.Date; import java.util.List; -import java.util.TreeSet; import org.junit.Before; import org.junit.Test; @@ -115,8 +109,15 @@ public class JU_DelegateDAO { @Test public void testDelegateLoader(){ + Class<?> innerClass = null; + Class<?>[] innerClassArr = DelegateDAO.class.getDeclaredClasses(); + for(Class indCls:innerClassArr) { + if(indCls.getName().contains("DelegateLoader")) { + innerClass = indCls; + break; + } + } - Class<?> innerClass = DelegateDAO.class.getDeclaredClasses()[1]; Constructor<?> constructor = innerClass.getDeclaredConstructors()[0]; constructor.setAccessible(true); try { diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_Namespace.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_Namespace.java new file mode 100644 index 00000000..8de750c6 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_Namespace.java @@ -0,0 +1,105 @@ +/** + * ============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 org.onap.aaf.auth.dao.cass; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.onap.aaf.misc.env.APIException; + +public class JU_Namespace { + + Namespace namespace; + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + } + + @Test + public void testInit() { + new Namespace(); + NsDAO.Data data = new NsDAO.Data(); + data.name = "name"; + namespace = new Namespace(data); + assertTrue(namespace.name.equals("name")); + data.attrib = new HashMap<>(); + namespace = new Namespace(data); + data.attrib.put("test", "test"); + namespace = new Namespace(data); + } + + + + @Test + public void testSecondConstructor() { + + NsDAO.Data data = new NsDAO.Data(); + data.name = "name"; + List<String> owner = new ArrayList<>(); + List<String> admin = new ArrayList<>();; + namespace = new Namespace(data,owner, admin); + assertTrue(namespace.name.equals("name")); + data.attrib = new HashMap<>(); + namespace = new Namespace(data,owner, admin); + data.attrib.put("test", "test"); + namespace = new Namespace(data ,owner, admin); + + NsDAO.Data retData = namespace.data(); + assertTrue(retData.name.equals("name")); + + } + @Test + public void testBytify() { + testSecondConstructor(); + try { + ByteBuffer retVal = namespace.bytify(); + namespace.reconstitute(retVal); + namespace.hashCode(); + namespace.toString(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + @Test + public void testEquals() { + testSecondConstructor(); + NsDAO.Data data = new NsDAO.Data(); + data.name = "name"; + Namespace nameObj = null; + assertFalse(namespace.equals(nameObj)); + assertFalse(namespace.equals(data)); + nameObj = new Namespace(data); + assertTrue(namespace.equals(nameObj)); + } + +}
\ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_NsDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_NsDAO.java new file mode 100644 index 00000000..2b0e101b --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_NsDAO.java @@ -0,0 +1,886 @@ +/** + * ============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 org.onap.aaf.auth.dao.cass; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.AbsCassDAO; +import org.onap.aaf.auth.dao.AbsCassDAO.CRUD; +import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.CassDAOImpl; +import org.onap.aaf.auth.dao.cass.NsDAO.Data; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.TimeTaken; + +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 JU_NsDAO { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + Session session; + @Mock + ResultSet rs; + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(session).when(cluster).connect("test"); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LATITUDE); + Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LONGITUDE); + + Iterator<Row> ite = Mockito.mock(Iterator.class); + Mockito.doReturn(ite).when(rs).iterator(); + Mockito.doReturn(rs).when(session).execute(Mockito.anyString()); + } + + @Test + public void testInit() { + try { + Session session = Mockito.mock(Session.class); + new NsDAOImpl(trans, cluster, "test", session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setPsByStartAndTarget(NsDAO NsDAOObj, PSInfo psInfoObj, String fieldName) { + Field nsDaoField; + try { + nsDaoField = NsDAO.class.getDeclaredField(fieldName); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(NsDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testWasMOdified() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("NsDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on NsDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doNothing().when(tt).done(); + NsDAO.Data data = new NsDAO.Data(); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + + NsDAO daoObj = null; + try { + daoObj = new NsDAO(trans, historyDAO, cacheInfoDAO); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"}); + + daoObj.wasModified(trans, CRUD.create, data, new String[] {}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {null}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test",null}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test","test"}); + + rs1 = new Result<ResultSet>(null,1,"test",new Object[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test","test"}); + } + + @Test + public void testSecondConstructor() { + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + + try { + new NsDAO(trans, historyDAO, cacheInfoDAO); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testNSLoader(){ + Class<?> innerClass = null; + Class<?>[] innerClassArr = NsDAO.class.getDeclaredClasses(); + for(Class<?> indCls:innerClassArr) { + if(indCls.getName().contains("NSLoader")) { + innerClass = indCls; + break; + } + } + + Constructor<?> constructor = innerClass.getDeclaredConstructors()[0]; + constructor.setAccessible(true); + + try { + + Object obj = constructor.newInstance(1); + Method innnerClassMtd; + + NsDAO.Data data = new NsDAO.Data(); + Row row = Mockito.mock(Row.class); + ByteBuffer bbObj = ByteBuffer.allocateDirect(10); + bbObj.limit(7); + bbObj.put(0, new Byte("0")); + bbObj.put(1, new Byte("1")); + bbObj.put(2, new Byte("2")); + Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1); + + innnerClassMtd = innerClass.getMethod("load", new Class[] {NsDAO.Data.class, Row.class}); + innnerClassMtd.invoke(obj, new Object[] {data, row}); + + innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {NsDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} }); +// + innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {NsDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} }); + + DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_NsDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {NsDAO.Data.class, DataOutputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dos }); + + DataInputStream dis = new DataInputStream(new FileInputStream("JU_NsDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {NsDAO.Data.class, DataInputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dis }); + + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + @Test + public void testCreate() { + PSInfo psObj = Mockito.mock(PSInfo.class); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + + Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Mockito.doReturn("test user").when(trans).user(); + + Result<NsDAO.Data> retVal = daoObj.create(trans,data); + assertTrue(retVal.status == 4); + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + data.parent = "parent"; + data.attrib = new HashMap<>(); + data.attrib.put("test", "test"); + + Field cbField; + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + try { + cbField = CassAccess.class.getDeclaredField("cb"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + retVal = daoObj.create(trans,data); + assertTrue(retVal.status == 9); + + Field owningField; + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + try { + owningField = AbsCassDAO.class.getDeclaredField("owningDAO"); + owningField.setAccessible(true); + owningField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.create(trans,data); + assertTrue(retVal.status == 0); + + } + + @Test + public void testUpdate() { + PSInfo psObj = Mockito.mock(PSInfo.class); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + + Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Mockito.doReturn("test user").when(trans).user(); + + Result<Void> retVal = daoObj.update(trans,data); + assertTrue(retVal.status == 4); + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + data.parent = "parent"; + data.attrib = new HashMap<>(); + data.attrib.put("test", "test"); + Field cbField; + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + try { + cbField = AbsCassDAO.class.getDeclaredField("owningDAO"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + retVal = daoObj.update(trans,data); + assertTrue(retVal.status == 0); + + ResultSet rsMock = Mockito.mock(ResultSet.class); + Iterator<Row> iteMock = Mockito.mock(Iterator.class); + Mockito.doReturn(iteMock).when(rsMock).iterator(); + Row rowMock = Mockito.mock(Row.class); + Mockito.doReturn(rowMock).when(iteMock).next(); + Mockito.when(iteMock.hasNext()).thenReturn(true, false); + Mockito.doReturn("test").when(rowMock).getString(Mockito.anyInt()); + Mockito.doReturn(rsMock).when(session).execute(Mockito.anyString()); + retVal = daoObj.update(trans,data); + assertTrue(retVal.status == 0); + } + + @Test + public void testRead() { + PSInfo psObj = Mockito.mock(PSInfo.class); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Mockito.doReturn("test user").when(trans).user(); + + Result<List<Data>> retVal = daoObj.read(trans,data); + assertTrue(retVal.status == 0); + + List<Data> dataAL= new ArrayList<>(); + dataAL.add(data); + rs1 = new Result<List<Data>>(dataAL,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", data); + Field cbField; + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + try { + cbField = AbsCassDAO.class.getDeclaredField("owningDAO"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.read(trans,data); + assertTrue(retVal.status == 0); + + } + + @Test + public void testReadByObject() { + PSInfo psObj = Mockito.mock(PSInfo.class); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", new Object[] {}); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Mockito.doReturn("test user").when(trans).user(); + + Result<List<Data>> retVal = daoObj.read(trans,new Object[] {}); + assertTrue(retVal.status == 0); + + List<Data> dataAL= new ArrayList<>(); + dataAL.add(data); + rs1 = new Result<List<Data>>(dataAL,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", new Object[] {}); + Field cbField; + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + try { + cbField = AbsCassDAO.class.getDeclaredField("owningDAO"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.read(trans,new Object[] {}); + assertTrue(retVal.status == 0); + + } + + @Test + public void testDelete() { + PSInfo psObj = Mockito.mock(PSInfo.class); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Mockito.doReturn("test user").when(trans).user(); + Field cbField; + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + try { + cbField = AbsCassDAO.class.getDeclaredField("owningDAO"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<Void> retVal = daoObj.delete(trans,data, false); + assertTrue(retVal.status == 0); + + List<Data> dataAL= new ArrayList<>(); + dataAL.add(data); + rs1 = new Result<List<Data>>(dataAL,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", data); + + retVal = daoObj.delete(trans,data, false); + assertTrue(retVal.status == 0); + + } + + @Test + public void testReadNsByAttrib() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Mockito.doReturn("test user").when(trans).user(); + Field cbField; + try { + cbField = AbsCassDAO.class.getDeclaredField("owningDAO"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<Set<String>> retVal = daoObj.readNsByAttrib(trans,"test"); + assertTrue(retVal.status == 0); + + ResultSet rsMock = Mockito.mock(ResultSet.class); + Iterator<Row> iteMock = Mockito.mock(Iterator.class); + Mockito.doReturn(iteMock).when(rsMock).iterator(); + Row rowMock = Mockito.mock(Row.class); + Mockito.doReturn(rowMock).when(iteMock).next(); + Mockito.when(iteMock.hasNext()).thenReturn(true, false); + Mockito.doReturn("test").when(rowMock).getString(Mockito.anyInt()); + Mockito.doReturn(rsMock).when(session).execute(Mockito.anyString()); + + retVal = daoObj.readNsByAttrib(trans,"test"); + assertTrue(retVal.status == 0); + + } + @Test + public void testAttribAdd() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Mockito.doReturn("test user").when(trans).user(); + Field cbField; + try { + cbField = AbsCassDAO.class.getDeclaredField("owningDAO"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<Void> retVal = daoObj.attribAdd(trans, "test", "test", "test"); + assertTrue(retVal.status == 0); + } + + @Test + public void testAttribRemove() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Mockito.doReturn("test user").when(trans).user(); + Field cbField; + try { + cbField = AbsCassDAO.class.getDeclaredField("owningDAO"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<Void> retVal = daoObj.attribRemove(trans, "test", "test"); + assertTrue(retVal.status == 0); + } + + @Test + public void testAddDescription() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + NsDAO.Data data = new NsDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "NsDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "NsDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + rs1 = new Result<List<Data>>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn("test user").when(trans).user(); + Field cbField; + try { + cbField = AbsCassDAO.class.getDeclaredField("owningDAO"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<Void> retVal = daoObj.addDescription(trans, "test", "test"); + assertTrue(retVal.status == 0); + } + + @Test + public void testGetChildren() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + NsDAOImpl daoObj=null; + try { + daoObj = new NsDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, session); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<List<Data>> retVal = daoObj.getChildren(trans, "test"); + assertNull(retVal); + } + + @Test + public void testData() { + NsDAO.Data data = new NsDAO.Data(); + data.attrib = null; + data.attrib(true); + + data.attrib = new HashMap<>(); + data.attrib(true); + + data.attrib(false); + data.attrib = new ConcurrentHashMap<>(); + data.attrib(true); + + data.name="123"; + data.split("test"); + + data.toString(); + } + +} + +class NsDAOImpl extends NsDAO{ + + +// public NsDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS ) throws APIException, IOException { +// super(trans, historyDAO); +// setSession(this, Mockito.mock(Session.class)); +// } + + + public NsDAOImpl(AuthzTrans trans, Cluster cluster, String keySpace, Session session)throws APIException, IOException { + super(trans, cluster, keySpace); + setSession(this, session); + } + + + public NsDAOImpl(AuthzTrans trans, HistoryDAO historyDAO, CacheInfoDAO cacheInfoDAO, + org.onap.aaf.auth.dao.AbsCassDAO.PSInfo psObj, Session session) throws APIException, IOException { + super(trans, historyDAO, cacheInfoDAO); + setPs(this, psObj, "createPS"); + setPs(this, psObj, "updatePS"); + setPs(this, psObj, "readPS"); + setPs(this, psObj, "deletePS"); + setPsNs(this, psObj, "psNS"); + setSession(this, session); + } + public void setPsNs(NsDAOImpl NsDAOObj, PSInfo psInfoObj, String methodName) { + Field nsDaoField; + try { + nsDaoField = NsDAO.class.getDeclaredField(methodName); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(NsDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setPs(NsDAOImpl NsDAOObj, PSInfo psInfoObj, String methodName) { + Field nsDaoField; + try { + nsDaoField = CassDAOImpl.class.getDeclaredField(methodName); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(NsDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public void setSession(NsDAOImpl approvalDaoObj, Session session) { + Field nsDaoField; + try { + nsDaoField = AbsCassDAO.class.getDeclaredField("session"); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(approvalDaoObj, session); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_OAuthTokenDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_OAuthTokenDAO.java new file mode 100644 index 00000000..c9f08a83 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_OAuthTokenDAO.java @@ -0,0 +1,273 @@ +/** + * ============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 org.onap.aaf.auth.dao.cass; + +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; +import java.util.Date; +import java.util.HashSet; +import java.util.List; + +import org.eclipse.jetty.util.ConcurrentHashSet; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.AbsCassDAO; +import org.onap.aaf.auth.dao.AbsCassDAO.CRUD; +import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo; +import org.onap.aaf.auth.dao.CassDAOImpl; +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.Env; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.TimeTaken; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Row; + +public class JU_OAuthTokenDAO { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + } + + @Test + public void testInit() { + OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, cluster, "test"); +// daoObj. + } + @Test + public void testReadByUser() { + OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, cluster, "test"); + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psByUser"); + + Result<List<OAuthTokenDAO.Data>> rs1 = new Result<List<OAuthTokenDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "OAuthTokenDAO READ", new Object[]{"test"}); + + daoObj.readByUser(trans, "test"); + } + + public void setPsByStartAndTarget(OAuthTokenDAO OAuthTokenDAOObj, PSInfo psInfoObj, String fieldName) { + Field nsDaoField; + try { + nsDaoField = OAuthTokenDAO.class.getDeclaredField(fieldName); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(OAuthTokenDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testWasMOdified() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("OAuthTokenDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on OAuthTokenDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doNothing().when(tt).done(); + OAuthTokenDAO.Data data = new OAuthTokenDAO.Data(); + + OAuthTokenDAO daoObj = null; + daoObj = new OAuthTokenDAO(trans, cluster, "test" ); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"}); + + } + + @Test + public void testSecondConstructor() { + AbsCassDAO absCassDAO = Mockito.mock(AbsCassDAO.class); + + OAuthTokenDAO daoObj = new OAuthTokenDAO(trans, absCassDAO); + + } + + @Test + public void testData(){ + OAuthTokenDAO.Data data = new OAuthTokenDAO.Data(); + data.scopes = null; + data.scopes(true); + + data.scopes = new HashSet<>(); + data.scopes(true); + + data.scopes(false); + data.scopes = new ConcurrentHashSet<>(); + data.scopes(true); + + data.expires = new Date(); + data.user="test"; + data.id="id"; + data.toString(); + + data.active=true; + data.toString(); + + try { + ByteBuffer bb = data.bytify(); + data.reconstitute(bb); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + @Test + public void testOAuthLoader(){ + Class<?> innerClass = null; + Class<?>[] innerClassArr = OAuthTokenDAO.class.getDeclaredClasses(); + for(Class indCls:innerClassArr) { + if(indCls.getName().contains("OAuthLoader")) { + innerClass = indCls; + break; + } + } + + Constructor<?> constructor = innerClass.getDeclaredConstructors()[0]; + constructor.setAccessible(true); + + try { + + Object obj = constructor.newInstance(1); + Method innnerClassMtd; + + OAuthTokenDAO.Data data = new OAuthTokenDAO.Data(); + Row row = Mockito.mock(Row.class); + ByteBuffer bbObj = ByteBuffer.allocateDirect(10); + bbObj.limit(7); + bbObj.put(0, new Byte("0")); + bbObj.put(1, new Byte("1")); + bbObj.put(2, new Byte("2")); + Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1); + + innnerClassMtd = innerClass.getMethod("load", new Class[] {OAuthTokenDAO.Data.class, Row.class}); + innnerClassMtd.invoke(obj, new Object[] {data, row}); + + innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {OAuthTokenDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} }); +// + innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {OAuthTokenDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test","test"} }); + + DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_OAuthTokenDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {OAuthTokenDAO.Data.class, DataOutputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dos }); + + DataInputStream dis = new DataInputStream(new FileInputStream("JU_OAuthTokenDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {OAuthTokenDAO.Data.class, DataInputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dis }); + + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} + +class OAuthTokenDAOImpl extends OAuthTokenDAO{ + + + public OAuthTokenDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,PSInfo readPS ) throws APIException, IOException { + super(trans, historyDAO); + setPs(this, readPS, "createPS"); + } + + + public void setPs(OAuthTokenDAOImpl OAuthTokenDAOObj, PSInfo psInfoObj, String methodName) { + Field nsDaoField; + try { + nsDaoField = CassDAOImpl.class.getDeclaredField(methodName); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(OAuthTokenDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_PermDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_PermDAO.java new file mode 100644 index 00000000..a26e179b --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_PermDAO.java @@ -0,0 +1,657 @@ +/** + * ============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 org.onap.aaf.auth.dao.cass; + +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.AbsCassDAO.CRUD; +import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo; +import org.onap.aaf.auth.dao.cass.PermDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.dao.AbsCassDAO; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.CassDAOImpl; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.TimeTaken; + +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 JU_PermDAO { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + Session session; + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LATITUDE); + Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LONGITUDE); + Mockito.doReturn(session).when(cluster).connect("test"); + } + + @Test + public void testInit() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("PermDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on PermDAO", Env.SUB); + try { + Session session = Mockito.mock(Session.class); + PermDAO daoObj = new PermDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// daoObj. + } + @Test + public void testReadByStartAndTarget() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("PermDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on PermDAO", Env.SUB); + PermDAO daoObj = null; + try { + Session session = Mockito.mock(Session.class); + daoObj = new PermDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psByType"); + + Result<List<PermDAO.Data>> rs1 = new Result<List<PermDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "PermDAO READ", new Object[]{"test"}); + + daoObj.readByType(trans, "test", "test"); + } + @Test + public void testReadChildren() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("PermDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on PermDAO", Env.SUB); + PermDAO daoObj = null; + try { + Session session = Mockito.mock(Session.class); + daoObj = new PermDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psChildren"); + + Result<List<PermDAO.Data>> rs1 = new Result<List<PermDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "PermDAO READ", new Object[]{"test"}); + + daoObj.readChildren(trans, "test", "test"); + } + @Test + public void testReadNs() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("PermDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on PermDAO", Env.SUB); + PermDAO daoObj = null; + try { + Session session = Mockito.mock(Session.class); + daoObj = new PermDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psNS"); + + Result<List<PermDAO.Data>> rs1 = new Result<List<PermDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "PermDAO READ", new Object[]{"test"}); + + daoObj.readNS(trans, "test"); + } + @Test + public void testAddRole() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + PermDAO.Data data = new PermDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + PermDAOImpl daoObj=null; + try { + daoObj = new PermDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, Mockito.mock(Session.class)); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("PermDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on PermDAO", Env.SUB); + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "PermDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Result<Void> rs2 = new Result<Void>(null,0,"test",new String[0]); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + rs1 = new Result<List<Data>>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn("test user").when(trans).user(); + Field cbField; + try { + cbField = CassAccess.class.getDeclaredField("cb"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PermDAO.Data perm = new PermDAO.Data(); + Result<Void> retVal = daoObj.addRole(trans, perm, "test"); + assertTrue(retVal.status == 9); + + Field owningField; + try { + owningField = AbsCassDAO.class.getDeclaredField("owningDAO"); + owningField.setAccessible(true); + owningField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.addRole(trans, perm, "test"); + assertTrue(retVal.status == 0); + } + + @Test + public void testDelRole() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + PermDAO.Data data = new PermDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + PermDAOImpl daoObj=null; + try { + daoObj = new PermDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, Mockito.mock(Session.class)); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("PermDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on PermDAO", Env.SUB); + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "PermDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Result<Void> rs2 = new Result<Void>(null,0,"test",new String[0]); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + rs1 = new Result<List<Data>>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn("test user").when(trans).user(); + + PermDAO.Data perm = new PermDAO.Data(); + Field cbField; + try { + cbField = CassAccess.class.getDeclaredField("cb"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<Void> retVal = daoObj.delRole(trans, perm, "test"); + assertTrue(retVal.status == 9); + + Field owningDaoField; + try { + owningDaoField = AbsCassDAO.class.getDeclaredField("owningDAO"); + owningDaoField.setAccessible(true); + owningDaoField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.delRole(trans, perm, "test"); + assertTrue(retVal.status == 0); + } + + @Test + public void testAddDescription() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + PermDAO.Data data = new PermDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + PermDAOImpl daoObj=null; + try { + daoObj = new PermDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, Mockito.mock(Session.class)); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("PermDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on PermDAO", Env.SUB); + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "PermDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "PermDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Result<Void> rs2 = new Result<Void>(null,1,"test",new String[0]); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + rs1 = new Result<List<Data>>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn("test user").when(trans).user(); + + PermDAO.Data perm = new PermDAO.Data(); + Field cbField; + try { + cbField = CassAccess.class.getDeclaredField("cb"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<Void> retVal = daoObj.addDescription(trans, "test", "test", "test", "test", "test"); + assertTrue(retVal.status == 9); + + Field owningDaoField; + try { + owningDaoField = AbsCassDAO.class.getDeclaredField("owningDAO"); + owningDaoField.setAccessible(true); + owningDaoField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.addDescription(trans, "test", "test", "test", "test", "test"); + assertTrue(retVal.status == 0); + } + + public void setPsByStartAndTarget(PermDAO PermDAOObj, PSInfo psInfoObj, String fieldName) { + Field PermDAOField; + try { + PermDAOField = PermDAO.class.getDeclaredField(fieldName); + + PermDAOField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(PermDAOField, PermDAOField.getModifiers() & ~Modifier.FINAL); + + PermDAOField.set(PermDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testWasMOdified() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("PermDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on PermDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doNothing().when(tt).done(); + PermDAO.Data data = new PermDAO.Data(); + PSInfo createPS = Mockito.mock(PSInfo.class); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]); + Result<Void> rs2 = new Result<Void>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + PermDAOImpl daoObj = null; + try { + daoObj = new PermDAOImpl(trans, historyDAO, cacheInfoDAO, createPS ); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"}); + + daoObj.wasModified(trans, CRUD.create, data, new String[] {}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {null}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test",null}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test","test"}); + + rs1 = new Result<ResultSet>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"}); + } + + @Test + public void testSecondConstructor() { + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + + PermDAO daoObj = new PermDAO(trans, historyDAO, cacheInfoDAO); + + } + + @Test + public void testFutureLoader(){ + Class<?> innerClass = null; + Class<?>[] innerClassArr = PermDAO.class.getDeclaredClasses(); + for(Class indCls:innerClassArr) { + if(indCls.getName().contains("PermLoader")) { + innerClass = indCls; + break; + } + } + + Constructor<?> constructor = innerClass.getDeclaredConstructors()[0]; + constructor.setAccessible(true); + + try { + + Object obj = constructor.newInstance(1); + Method innnerClassMtd; + + PermDAO.Data data = new PermDAO.Data(); + Row row = Mockito.mock(Row.class); + ByteBuffer bbObj = ByteBuffer.allocateDirect(10); + bbObj.limit(7); + bbObj.put(0, new Byte("0")); + bbObj.put(1, new Byte("1")); + bbObj.put(2, new Byte("2")); + Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1); + + innnerClassMtd = innerClass.getMethod("load", new Class[] {PermDAO.Data.class, Row.class}); + innnerClassMtd.invoke(obj, new Object[] {data, row}); + + innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {PermDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test"} }); +// + innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {PermDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} }); + + DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_PermDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {PermDAO.Data.class, DataOutputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dos }); + + DataInputStream dis = new DataInputStream(new FileInputStream("JU_PermDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {PermDAO.Data.class, DataInputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dis }); + + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testData() { + PermDAO.Data data = new PermDAO.Data(); + NsSplit nss = new NsSplit("test", "test"); + data = new PermDAO.Data(nss, "test", "test"); + + data.toString(); + + assertTrue("test.test|test|test".equalsIgnoreCase(data.fullPerm())); + + Question q = Mockito.mock( Question.class); + + Result<NsSplit> rs = new Result<NsSplit>(nss,0,"test",new Object[0]); + Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test"); + Result<Data> retVal= PermDAO.Data.decode(trans, q, "test|||"); + assertTrue(retVal.status==0); + Result<String[]> retVal1= PermDAO.Data.decodeToArray(trans, q, "test|||"); + assertTrue(retVal.status==0); + retVal= PermDAO.Data.decode(trans, q, "test||"); + retVal1= PermDAO.Data.decodeToArray(trans, q, "test||"); + assertTrue(retVal.status==0); + + rs = new Result<NsSplit>(nss,1,"test",new Object[0]); + Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test"); + retVal= PermDAO.Data.decode(trans, q, "test||"); + retVal1= PermDAO.Data.decodeToArray(trans, q, "test||"); + assertTrue(retVal.status==1); + + retVal= PermDAO.Data.decode(trans, q, "test|"); + retVal1= PermDAO.Data.decodeToArray(trans, q, "test|"); + assertTrue(retVal.status==4); + + NsDAO.Data ns = new NsDAO.Data(); + ns.name="test"; + PermDAO.Data.create(ns, "test"); + + PermDAO.Data.create(trans,q, "test"); + rs = new Result<NsSplit>(nss,0,"test",new Object[0]); + Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test"); + PermDAO.Data.create(trans,q, "test|test|test|test"); + } + +} + +class PermDAOImpl extends PermDAO{ + public PermDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS) throws APIException, IOException { + super(trans, historyDAO, cacheDao); + setPs(this, readPS, "createPS"); + } + + public PermDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS, Session session ) throws APIException, IOException { + super(trans, historyDAO, cacheDao); + setPs(this, readPS, "createPS"); + setSession(this, session); + } + + + public void setPs(PermDAOImpl PermDAOObj, PSInfo psInfoObj, String methodName) { + Field PermDAOField; + try { + PermDAOField = CassDAOImpl.class.getDeclaredField(methodName); + + PermDAOField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(PermDAOField, PermDAOField.getModifiers() & ~Modifier.FINAL); + + PermDAOField.set(PermDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setSession(PermDAOImpl approvalDaoObj, Session session) { + Field nsDaoField; + try { + nsDaoField = AbsCassDAO.class.getDeclaredField("session"); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(approvalDaoObj, session); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_RoleDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_RoleDAO.java new file mode 100644 index 00000000..1f383469 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_RoleDAO.java @@ -0,0 +1,667 @@ +/** + * ============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 org.onap.aaf.auth.dao.cass; + +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; +import java.util.HashSet; +import java.util.List; +import java.util.TreeSet; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.AbsCassDAO; +import org.onap.aaf.auth.dao.AbsCassDAO.CRUD; +import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.CassDAOImpl; +import org.onap.aaf.auth.dao.cass.RoleDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.TimeTaken; + +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 JU_RoleDAO { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + Session session; + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LATITUDE); + Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LONGITUDE); + Mockito.doReturn(session).when(cluster).connect("test"); + } + + @Test + public void testInit() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("RoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on RoleDAO", Env.SUB); + try { + new RoleDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// daoObj. + } + @Test + public void testReadByStartAndTarget() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("RoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on RoleDAO", Env.SUB); + RoleDAO daoObj = null; + try { + daoObj = new RoleDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psName"); + + Result<List<RoleDAO.Data>> rs1 = new Result<List<RoleDAO.Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "RoleDAO READ", new Object[]{"test"}); + + daoObj.readName(trans, "test"); + } + @Test + public void testReadChildren() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("RoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on RoleDAO", Env.SUB); + RoleDAO daoObj = null; + try { + daoObj = new RoleDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psChildren"); + + Result<List<RoleDAO.Data>> rs1 = new Result<List<RoleDAO.Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "RoleDAO READ", new Object[]{"test"}); + + daoObj.readChildren(trans, "test", "test"); + + daoObj.readChildren(trans, "test", "*"); + daoObj.readChildren(trans, "test", ""); + } + @Test + public void testReadNs() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("RoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on RoleDAO", Env.SUB); + RoleDAO daoObj = null; + try { + daoObj = new RoleDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psNS"); + + Result<List<RoleDAO.Data>> rs1 = new Result<List<RoleDAO.Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "RoleDAO READ", new Object[]{"test"}); + + daoObj.readNS(trans, "test"); + } + @Test + public void testAddRole() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + RoleDAO.Data data = new RoleDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + RoleDAOImpl daoObj=null; + try { + daoObj = new RoleDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, Mockito.mock(Session.class)); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("RoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on RoleDAO", Env.SUB); + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "RoleDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Result<Void> rs2 = new Result<Void>(null,0,"test",new Object[0]); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + rs1 = new Result<List<Data>>(null,1,"test",new Object[0]); + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn("test user").when(trans).user(); + Field cbField; + try { + cbField = CassAccess.class.getDeclaredField("cb"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PermDAO.Data perm = new PermDAO.Data(); + RoleDAO.Data role = new RoleDAO.Data(); + Result<Void> retVal = daoObj.addPerm(trans, role, perm); + assertTrue(retVal.status == 9); + + Field owningField; + try { + owningField = AbsCassDAO.class.getDeclaredField("owningDAO"); + owningField.setAccessible(true); + owningField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.addPerm(trans, role, perm); + assertTrue(retVal.status == 0); + } + + @Test + public void testDelRole() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + RoleDAO.Data data = new RoleDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + RoleDAOImpl daoObj=null; + try { + daoObj = new RoleDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, Mockito.mock(Session.class)); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("RoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on RoleDAO", Env.SUB); + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "RoleDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Result<Void> rs2 = new Result<Void>(null,0,"test",new String[0]); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + rs1 = new Result<List<Data>>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn("test user").when(trans).user(); + + PermDAO.Data perm = new PermDAO.Data(); + Field cbField; + try { + cbField = CassAccess.class.getDeclaredField("cb"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + RoleDAO.Data role = new RoleDAO.Data(); + Result<Void> retVal = daoObj.delPerm(trans, role, perm); + assertTrue(retVal.status == 9); + + Field owningDaoField; + try { + owningDaoField = AbsCassDAO.class.getDeclaredField("owningDAO"); + owningDaoField.setAccessible(true); + owningDaoField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.delPerm(trans,role, perm); + assertTrue(retVal.status == 0); + } + + @Test + public void testAddDescription() { + PSInfo psObj = Mockito.mock(PSInfo.class); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + RoleDAO.Data data = new RoleDAO.Data(); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + RoleDAOImpl daoObj=null; + try { + daoObj = new RoleDAOImpl(trans, historyDAO, cacheInfoDAO, psObj, Mockito.mock(Session.class)); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("RoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on RoleDAO", Env.SUB); + + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_PORT,"9042"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,"100"); + Mockito.doReturn("100").when(trans).getProperty(CassAccess.CASSANDRA_CLUSTERS_USER_NAME,null); + + Result<List<Data>> rs1 = new Result<List<Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl CREATE", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl UPDATE", data); + Mockito.doReturn(rs1).when(psObj).read(trans, "RoleDAOImpl READ", data); + Mockito.doReturn(rs1).when(psObj).exec(trans, "RoleDAOImpl DELETE", data); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + Result<Void> rs2 = new Result<Void>(null,1,"test",new String[0]); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + rs1 = new Result<List<Data>>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(cacheInfoDAO).touch(Mockito.any(), Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn("test user").when(trans).user(); + + RoleDAO.Data perm = new RoleDAO.Data(); + Field cbField; + try { + cbField = CassAccess.class.getDeclaredField("cb"); + cbField.setAccessible(true); + cbField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Result<Void> retVal = daoObj.addDescription(trans, "test", "test", "test"); + assertTrue(retVal.status == 9); + + Field owningDaoField; + try { + owningDaoField = AbsCassDAO.class.getDeclaredField("owningDAO"); + owningDaoField.setAccessible(true); + owningDaoField.set(daoObj, null); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + retVal = daoObj.addDescription(trans, "test", "test", "test"); + assertTrue(retVal.status == 0); + } + + public void setPsByStartAndTarget(RoleDAO RoleDAOObj, PSInfo psInfoObj, String fieldName) { + Field RoleDAOField; + try { + RoleDAOField = RoleDAO.class.getDeclaredField(fieldName); + + RoleDAOField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(RoleDAOField, RoleDAOField.getModifiers() & ~Modifier.FINAL); + + RoleDAOField.set(RoleDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testWasMOdified() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("RoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on RoleDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doNothing().when(tt).done(); + RoleDAO.Data data = new RoleDAO.Data(); + PSInfo createPS = Mockito.mock(PSInfo.class); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]); + Result<Void> rs2 = new Result<Void>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + RoleDAOImpl daoObj = null; + try { + daoObj = new RoleDAOImpl(trans, historyDAO, cacheInfoDAO, createPS ); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"}); + + daoObj.wasModified(trans, CRUD.create, data, new String[] {}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {null}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test",null}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test","test"}); + + rs1 = new Result<ResultSet>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"}); + } + + @Test + public void testSecondConstructor() { + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + + RoleDAO daoObj = new RoleDAO(trans, historyDAO, cacheInfoDAO); + + } + + @Test + public void testFutureLoader(){ + Class<?> innerClass = null; + Class<?>[] innerClassArr = RoleDAO.class.getDeclaredClasses(); + for(Class indCls:innerClassArr) { + if(indCls.getName().contains("RoleLoader")) { + innerClass = indCls; + break; + } + } + + Constructor<?> constructor = innerClass.getDeclaredConstructors()[0]; + constructor.setAccessible(true); + + try { + + Object obj = constructor.newInstance(1); + Method innnerClassMtd; + + RoleDAO.Data data = new RoleDAO.Data(); + Row row = Mockito.mock(Row.class); + ByteBuffer bbObj = ByteBuffer.allocateDirect(10); + bbObj.limit(7); + bbObj.put(0, new Byte("0")); + bbObj.put(1, new Byte("1")); + bbObj.put(2, new Byte("2")); + Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1); + + innnerClassMtd = innerClass.getMethod("load", new Class[] {RoleDAO.Data.class, Row.class}); + innnerClassMtd.invoke(obj, new Object[] {data, row}); + + innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {RoleDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test"} }); +// + innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {RoleDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} }); + + DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_RoleDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {RoleDAO.Data.class, DataOutputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dos }); + + DataInputStream dis = new DataInputStream(new FileInputStream("JU_RoleDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {RoleDAO.Data.class, DataInputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dis }); + + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testData() { + RoleDAO.Data data = new RoleDAO.Data(); + NsSplit nss = new NsSplit("test", "test"); + data = new RoleDAO.Data(); + + data.toString(); + + + Question q = Mockito.mock( Question.class); + + Result<NsSplit> rs = new Result<NsSplit>(nss,0,"test",new Object[0]); + Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test"); + Result<Data> retVal= RoleDAO.Data.decode(trans, q, "test|||"); + assertTrue(retVal.status==0); + Result<String[]> retVal1= RoleDAO.Data.decodeToArray(trans, q, "test|||"); + assertTrue(retVal.status==0); + retVal= RoleDAO.Data.decode(trans, q, "test"); + retVal1= RoleDAO.Data.decodeToArray(trans, q, "test"); + assertTrue(retVal.status==0); + + rs = new Result<NsSplit>(nss,1,"test",new Object[0]); + Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test"); + retVal= RoleDAO.Data.decode(trans, q, "test"); + retVal1= RoleDAO.Data.decodeToArray(trans, q, "test"); + assertTrue(retVal.status==1); + + retVal= RoleDAO.Data.decode(trans, q, "test"); + retVal1= RoleDAO.Data.decodeToArray(trans, q, "test"); + assertTrue(retVal.status==1); + + NsDAO.Data ns = new NsDAO.Data(); + ns.name="test"; + RoleDAO.Data.create(ns, "test"); + + UserRoleDAO.Data urdd = new UserRoleDAO.Data(); + urdd.ns="test"; + RoleDAO.Data dd=RoleDAO.Data.decode(urdd); + assertTrue("test".equals(dd.ns)); + + assertTrue(data.encode().contains("null")); + + data.perms = null; + data.perms(true); + + data.perms = new HashSet<>(); + data.perms(true); + + data.perms(false); + data.perms = new TreeSet<>(); + data.perms(true); + } + +} + +class RoleDAOImpl extends RoleDAO{ + public RoleDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS) throws APIException, IOException { + super(trans, historyDAO, cacheDao); + setPs(this, readPS, "createPS"); + } + + public RoleDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS, Session session ) throws APIException, IOException { + super(trans, historyDAO, cacheDao); + setPs(this, readPS, "createPS"); + setSession(this, session); + } + + + public void setPs(RoleDAOImpl RoleDAOObj, PSInfo psInfoObj, String methodName) { + Field RoleDAOField; + try { + RoleDAOField = CassDAOImpl.class.getDeclaredField(methodName); + + RoleDAOField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(RoleDAOField, RoleDAOField.getModifiers() & ~Modifier.FINAL); + + RoleDAOField.set(RoleDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setSession(RoleDAOImpl approvalDaoObj, Session session) { + Field nsDaoField; + try { + nsDaoField = AbsCassDAO.class.getDeclaredField("session"); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(approvalDaoObj, session); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_UserRoleDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_UserRoleDAO.java new file mode 100644 index 00000000..e01cd42e --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_UserRoleDAO.java @@ -0,0 +1,414 @@ +/** + * ============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 org.onap.aaf.auth.dao.cass; + +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; +import java.util.HashSet; +import java.util.List; +import java.util.TreeSet; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.AbsCassDAO; +import org.onap.aaf.auth.dao.AbsCassDAO.CRUD; +import org.onap.aaf.auth.dao.AbsCassDAO.PSInfo; +import org.onap.aaf.auth.dao.CassAccess; +import org.onap.aaf.auth.dao.CassDAOImpl; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.TimeTaken; + +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 JU_UserRoleDAO { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + Session session; + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LATITUDE); + Mockito.doReturn("100").when(trans).getProperty(Config.CADI_LONGITUDE); + Mockito.doReturn(session).when(cluster).connect("test"); + } + + @Test + public void testInit() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB); + try { + new UserRoleDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +// daoObj. + } + @Test + public void testReadByUser() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB); + UserRoleDAO daoObj = null; + try { + daoObj = new UserRoleDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psByUser"); + + Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"}); + + daoObj.readByUser(trans, "test"); + } + @Test + public void testReadByRole() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB); + UserRoleDAO daoObj = null; + try { + daoObj = new UserRoleDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psByRole"); + + Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"}); + + daoObj.readByRole(trans, "test"); + } + @Test + public void testReadByUserRole() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB); + UserRoleDAO daoObj = null; + try { + daoObj = new UserRoleDAO(trans, cluster, "test"); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + PSInfo psObj = Mockito.mock(PSInfo.class); + setPsByStartAndTarget(daoObj, psObj, "psUserInRole"); + + Result<List<UserRoleDAO.Data>> rs1 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(psObj).read(trans, "UserRoleDAO READ", new Object[]{"test"}); + + daoObj.readByUserRole(trans, "test","test"); + } + + + public void setPsByStartAndTarget(UserRoleDAO UserRoleDAOObj, PSInfo psInfoObj, String fieldName) { + Field UserRoleDAOField; + try { + UserRoleDAOField = UserRoleDAO.class.getDeclaredField(fieldName); + + UserRoleDAOField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(UserRoleDAOField, UserRoleDAOField.getModifiers() & ~Modifier.FINAL); + + UserRoleDAOField.set(UserRoleDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testWasMOdified() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("UserRoleDAO CREATE", Env.REMOTE); + Mockito.doReturn(tt).when(trans).start("Clear Reset Deque", Env.SUB); + Mockito.doReturn(tt).when(trans).start("New Cassandra Session", Env.SUB); + Mockito.doReturn(tt).when(trans).start("Preparing PSInfo CREATE on UserRoleDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE Future",Env.REMOTE); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doNothing().when(tt).done(); + UserRoleDAO.Data data = new UserRoleDAO.Data(); + PSInfo createPS = Mockito.mock(PSInfo.class); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new Object[0]); + Result<Void> rs2 = new Result<Void>(null,0,"test",new Object[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + + UserRoleDAOImpl daoObj = null; + try { + daoObj = new UserRoleDAOImpl(trans, historyDAO, cacheInfoDAO, createPS ); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test"}); + + daoObj.wasModified(trans, CRUD.update, data, new String[] {}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {null}); + daoObj.wasModified(trans, CRUD.create, data, new String[] {"test",null}); + daoObj.wasModified(trans, CRUD.update, data, new String[] {"test","test"}); + + daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"}); + daoObj.wasModified(trans, CRUD.delete, data, new String[] {}); + + rs2 = new Result<Void>(null,1,"test",new Object[0]); + Mockito.doReturn(rs2).when(cacheInfoDAO).touch(Mockito.any(AuthzTrans.class),Mockito.anyString(), Mockito.anyVararg()); + daoObj.wasModified(trans, CRUD.read, data, new String[] {"test","test"}); + daoObj.wasModified(trans, CRUD.read, data, new String[] {}); + + rs1 = new Result<ResultSet>(null,1,"test",new String[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"}); + } + + @Test + public void testSecondConstructor() { + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + CacheInfoDAO cacheInfoDAO = Mockito.mock(CacheInfoDAO.class); + + UserRoleDAO daoObj = new UserRoleDAO(trans, historyDAO, cacheInfoDAO); + + } + + @Test + public void testFutureLoader(){ + Class<?> innerClass = null; + Class<?>[] innerClassArr = UserRoleDAO.class.getDeclaredClasses(); + for(Class indCls:innerClassArr) { + if(indCls.getName().contains("URLoader")) { + innerClass = indCls; + break; + } + } + + Constructor<?> constructor = innerClass.getDeclaredConstructors()[0]; + constructor.setAccessible(true); + + try { + + Object obj = constructor.newInstance(1); + Method innnerClassMtd; + + UserRoleDAO.Data data = new UserRoleDAO.Data(); + Row row = Mockito.mock(Row.class); + ByteBuffer bbObj = ByteBuffer.allocateDirect(10); + bbObj.limit(7); + bbObj.put(0, new Byte("0")); + bbObj.put(1, new Byte("1")); + bbObj.put(2, new Byte("2")); + Mockito.doReturn(bbObj).when(row).getBytesUnsafe(1); + + innnerClassMtd = innerClass.getMethod("load", new Class[] {UserRoleDAO.Data.class, Row.class}); + innnerClassMtd.invoke(obj, new Object[] {data, row}); + + innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {UserRoleDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test"} }); +// + innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {UserRoleDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test","test","test","test","test","test","test","test","test"} }); + + DataOutputStream dos = new DataOutputStream(new FileOutputStream("JU_UserRoleDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("marshal", new Class[] {UserRoleDAO.Data.class, DataOutputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dos }); + + DataInputStream dis = new DataInputStream(new FileInputStream("JU_UserRoleDAOTest.java")); + innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {UserRoleDAO.Data.class, DataInputStream.class }); + innnerClassMtd.invoke(obj, new Object[] {data, dis }); + + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testData() { + UserRoleDAO.Data data = new UserRoleDAO.Data(); + NsSplit nss = new NsSplit("test", "test"); + data = new UserRoleDAO.Data(); + + data.toString(); + data.role("test", "test"); + assertTrue("test".equals(data.ns)); + + RoleDAO.Data rdd = new RoleDAO.Data(); + rdd.ns="test"; + data.role(rdd); + assertTrue("test".equals(data.ns)); + + Question q = Mockito.mock( Question.class); + Result<NsSplit> rs = new Result<NsSplit>(nss,0,"test",new Object[0]); + Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test"); + + data.role(trans, q, "test"); + + rs = new Result<NsSplit>(nss,1,"test",new Object[0]); + Mockito.doReturn(rs).when(q).deriveNsSplit(trans, "test"); + + data.role(trans, q, "test"); + } + +} + +class UserRoleDAOImpl extends UserRoleDAO{ + public UserRoleDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS) throws APIException, IOException { + super(trans, historyDAO, cacheDao); + setPs(this, readPS, "createPS"); + } + + public UserRoleDAOImpl(AuthzTrans trans, HistoryDAO historyDAO,CacheInfoDAO cacheDao, PSInfo readPS, Session session ) throws APIException, IOException { + super(trans, historyDAO, cacheDao); + setPs(this, readPS, "createPS"); + setSession(this, session); + } + + + public void setPs(UserRoleDAOImpl UserRoleDAOObj, PSInfo psInfoObj, String methodName) { + Field UserRoleDAOField; + try { + UserRoleDAOField = CassDAOImpl.class.getDeclaredField(methodName); + + UserRoleDAOField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(UserRoleDAOField, UserRoleDAOField.getModifiers() & ~Modifier.FINAL); + + UserRoleDAOField.set(UserRoleDAOObj, psInfoObj); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setSession(UserRoleDAOImpl approvalDaoObj, Session session) { + Field nsDaoField; + try { + nsDaoField = AbsCassDAO.class.getDeclaredField("session"); + + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); +// modifiersField.setInt(nsDaoField, nsDaoField.getModifiers() & ~Modifier.FINAL); + + nsDaoField.set(approvalDaoObj, session); + } catch (NoSuchFieldException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} |