diff options
author | Sai Gandham <sg481n@att.com> | 2019-04-02 08:45:17 -0500 |
---|---|---|
committer | Sai Gandham <sg481n@att.com> | 2019-04-02 08:45:51 -0500 |
commit | 4fb5572210ac353ab9cf6edd55cc45a5d0381cd4 (patch) | |
tree | f827e792c0e2c8429efc3c0acc465d716e2dc74a /auth/auth-cass/src | |
parent | 2fc9c79ee3d7311e0902424712d1c90093d5557c (diff) |
Add more junits to auth-cass
Issue-ID: AAF-111
Change-Id: I27481a930a8b9b4421ab26164c1d08d617d75cb4
Signed-off-by: Sai Gandham <sg481n@att.com>
Diffstat (limited to 'auth/auth-cass/src')
3 files changed, 2167 insertions, 81 deletions
diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_ApprovalDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_ApprovalDAO.java new file mode 100644 index 00000000..79b4aeb5 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_ApprovalDAO.java @@ -0,0 +1,370 @@ +/** + * ============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.IOException; +import java.lang.reflect.Field; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; + +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.cass.ApprovalDAO.Data; +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.ColumnDefinitions; +import com.datastax.driver.core.ExecutionInfo; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.Session; +import com.google.common.util.concurrent.ListenableFuture; + +public class JU_ApprovalDAO { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + + @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(); + } + + @Test + public void testInit() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("ApprovalDAO 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 ApprovalDAO", Env.SUB); + Mockito.doNothing().when(tt).done(); + ApprovalDAO.Data data = new ApprovalDAO.Data(); + PSInfo createPS = Mockito.mock(PSInfo.class); + Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]); + Mockito.doReturn(rs).when(createPS).exec(trans, "ApprovalDAOImpl CREATE", data); + + ApprovalDAOImpl daoObj = new ApprovalDAOImpl(trans, cluster, "test",data, createPS); +// data.id + Result<Data> retVal = daoObj.create(trans, data); + assertTrue(retVal.status == 0); + + rs = new Result<ResultSet>(null,1,"test",new String[0]); + Mockito.doReturn(rs).when(createPS).exec(trans, "ApprovalDAOImpl CREATE", data); + retVal = daoObj.create(trans, data); + assertTrue(retVal.status == 1); + + Result<List<ApprovalDAO.Data>> rs1 = new Result<List<ApprovalDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(createPS).read(trans, "ApprovalDAOImpl CREATE", new Object[]{"testUser"}); + Result<List<ApprovalDAO.Data>> retVal1 = daoObj.readByUser(trans, "testUser"); + assertNull(retVal1); + + Mockito.doReturn(rs1).when(createPS).read(trans, "ApprovalDAOImpl CREATE", new Object[]{"testApprover"}); + retVal1 = daoObj.readByApprover(trans, "testApprover"); + assertNull(retVal1); + + Mockito.doReturn(rs1).when(createPS).read(trans, "ApprovalDAOImpl CREATE", new Object[]{new UUID(0, 0)}); + retVal1 = daoObj.readByTicket(trans, new UUID(0, 0)); + assertNull(retVal1); + + Mockito.doReturn(rs1).when(createPS).read(trans, "ApprovalDAOImpl CREATE", new Object[]{"testStatus"}); + retVal1 = daoObj.readByStatus(trans, "testStatus"); + assertNull(retVal1); + } + + @Test + public void testDelete() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("ApprovalDAO 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 ApprovalDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE); + Mockito.doNothing().when(tt).done(); + ApprovalDAO.Data data = new ApprovalDAO.Data(); + + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + Result<ResultSet> rs1 = new Result<ResultSet>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + PSInfo createPS = Mockito.mock(PSInfo.class); + ResultSet rsObj = new ResultSetImpl(); + Result<ResultSet> rs = new Result<ResultSet>(rsObj,0,"test",new String[0]); + Mockito.doReturn(rs).when(createPS).exec(trans, "ApprovalDAOImpl READ", data); + Mockito.doReturn(rs).when(createPS).exec(trans, "ApprovalDAOImpl DELETE", data); + + ApprovalDAOImpl daoObj = new ApprovalDAOImpl(trans, cluster, "test", createPS, historyDAO); +// data.id + Result<Void> retVal = daoObj.delete(trans, data, true); + assertTrue(retVal.status == 0); + + rs = new Result<ResultSet>(rsObj,1,"test",new String[0]); + Mockito.doReturn(rs).when(createPS).exec(trans, "ApprovalDAOImpl READ", data); + retVal = daoObj.delete(trans, data, true); + assertTrue(retVal.status == 1); + + data.status="approved"; + data.memo="test"; + retVal = daoObj.delete(trans, data, false); + assertTrue(retVal.status == 0); + + daoObj.async(true); + data.status="denied"; + retVal = daoObj.delete(trans, data, false); + assertTrue(retVal.status == 0); + + data.status=null; + retVal = daoObj.delete(trans, data, false); + } + + @Test + public void testWasMOdified() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("ApprovalDAO 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 ApprovalDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE); + Mockito.doNothing().when(tt).done(); + ApprovalDAO.Data data = new ApprovalDAO.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]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + ApprovalDAOImpl daoObj = new ApprovalDAOImpl(trans, cluster, "test", createPS, historyDAO); + 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.create, data, new String[] {"test","test"}); + } + + @Test + public void testSecondConstructor() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("ApprovalDAO 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 ApprovalDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE); + Mockito.doNothing().when(tt).done(); + ApprovalDAO.Data data = new ApprovalDAO.Data(); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + + ApprovalDAO daoObj = new ApprovalDAO(trans, historyDAO); + } +} + +class ResultSetImpl implements ResultSet{ + + @Override + public boolean isExhausted() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isFullyFetched() { + // TODO Auto-generated method stub + return false; + } + + @Override + public int getAvailableWithoutFetching() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public ListenableFuture<ResultSet> fetchMoreResults() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List<Row> all() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Iterator<Row> iterator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ExecutionInfo getExecutionInfo() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List<ExecutionInfo> getAllExecutionInfo() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Row one() { + // TODO Auto-generated method stub + Row rowObj = Mockito.mock(Row.class); + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(rowObj).getColumnDefinitions(); + return rowObj; + } + + @Override + public ColumnDefinitions getColumnDefinitions() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean wasApplied() { + // TODO Auto-generated method stub + return false; + } + +} + +class ApprovalDAOImpl extends ApprovalDAO{ + + public ApprovalDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,ApprovalDAO.Data data,PSInfo createPS ) { + super(trans, cluster, keyspace); + this.createPS = createPS; + setPs(this, createPS, "psByUser"); + setPs(this, createPS, "psByApprover"); + setPs(this, createPS, "psByTicket"); + setPs(this, createPS, "psByStatus"); + } + + public ApprovalDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,PSInfo readPS ) { + super(trans, cluster, keyspace); + this.readPS = readPS; + } + + public ApprovalDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,PSInfo readPS, HistoryDAO historyDAO ) { + super(trans, cluster, keyspace); + this.deletePS = readPS; + this.readPS = readPS; + setHistoryDao(this, historyDAO); + setSession(this, Mockito.mock(Session.class)); + } + + public void setPs(ApprovalDAOImpl approvalDaoObj, PSInfo psInfoObj, String methodName) { + Field nsDaoField; + try { + nsDaoField = ApprovalDAO.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(approvalDaoObj, 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 setHistoryDao(ApprovalDAOImpl approvalDaoObj, HistoryDAO historyDAO) { + Field nsDaoField; + try { + nsDaoField = ApprovalDAO.class.getDeclaredField("historyDAO"); + + 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, historyDAO); + } 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(ApprovalDAOImpl 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_ArtiDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_ArtiDAO.java new file mode 100644 index 00000000..e6318b99 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cass/JU_ArtiDAO.java @@ -0,0 +1,301 @@ +/** + * ============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.FileInputStream; +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.util.Date; +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.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.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.Session; + +public class JU_ArtiDAO { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + + @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(); + } + + @Test + public void testReadByMechID() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("ArtiDAO 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 ArtiDAO", Env.SUB); + Mockito.doNothing().when(tt).done(); + + PSInfo psByMechIdObj = Mockito.mock(PSInfo.class); + Result<ResultSet> rs = new Result<ResultSet>(null,0,"test",new String[0]); +// Mockito.doReturn(rs).when(createPS).exec(trans, "ArtiDAOImpl CREATE", data); + + ArtiDAOImpl daoObj = new ArtiDAOImpl(trans, cluster, "test", psByMechIdObj); + + Result<List<ArtiDAO.Data>> rs1 = new Result<List<ArtiDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psByMechIdObj).read(trans, "ArtiDAOImpl READ", new Object[]{"testMechId"}); + daoObj.readByMechID(trans, "testMechId"); + + rs1 = new Result<List<ArtiDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psByMechIdObj).read(trans, "ArtiDAOImpl READ", new Object[]{"testMachine"}); + daoObj.readByMachine(trans, "testMachine"); + + rs1 = new Result<List<ArtiDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(rs1).when(psByMechIdObj).read(trans, "ArtiDAOImpl READ", new Object[]{"testNs"}); + daoObj.readByNs(trans, "testNs"); + } + + @Test + public void testWasMOdified() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("ArtiDAO 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 ArtiDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE); + Mockito.doNothing().when(tt).done(); + ArtiDAO.Data data = new ArtiDAO.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]); + Mockito.doReturn(rs1).when(historyDAO).create(Mockito.any(), Mockito.any()); + + ArtiDAOImpl daoObj = new ArtiDAOImpl(trans, cluster, "test", createPS, historyDAO); + 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.create, data, new String[] {"test","test"}); + + data.type(true); + daoObj.wasModified(trans, CRUD.delete, data, new String[] {"test","test"}); + + } + @Test + public void testData(){ + ArtiDAO.Data data = new ArtiDAO.Data(); + data.type(true); + data.type(false); + + data.sans(true); + data.sans(false); + data.sans = new TreeSet(); + data.sans(false); + data.sans(true); + + data.expires = new Date(); + data.toString(); + } + + @Test + public void testArtifactLoader(){ + ArtiDAO daoObj = new ArtiDAO(trans, cluster, "test"); + Class<?> innerClass = ArtiDAO.class.getDeclaredClasses()[0]; + Constructor<?> constructor = innerClass.getDeclaredConstructors()[0]; + constructor.setAccessible(true); + try { + Object obj = constructor.newInstance(10); + Method innnerClassMtd; + + ArtiDAO.Data data = new ArtiDAO.Data(); + Row row = Mockito.mock(Row.class); + innnerClassMtd = innerClass.getMethod("load", new Class[] {ArtiDAO.Data.class, Row.class}); + innnerClassMtd.invoke(obj, new Object[] {data, row}); + + innnerClassMtd = innerClass.getDeclaredMethod("key", new Class[] {ArtiDAO.Data.class, Integer.TYPE, Object[].class }); + innnerClassMtd.invoke(obj, new Object[] {data, 1, new Object[] {"test","test","test"} }); + + innnerClassMtd = innerClass.getDeclaredMethod("body", new Class[] {ArtiDAO.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"} }); + +// DataInputStream in = Mockito.mock(DataInputStream.class); +//// Mockito.doReturn(100).when(in).read(); +//// Mockito.doReturn(100).when(in).readInt(); +// innnerClassMtd = innerClass.getDeclaredMethod("unmarshal", new Class[] {ArtiDAO.Data.class, DataInputStream.class }); +// innnerClassMtd.invoke(obj, new Object[] {data, in}); + } 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(); + } + } + + @Test + public void testSecondConstructor() { + TimeTaken tt = Mockito.mock(TimeTaken.class); + Mockito.doReturn(tt).when(trans).start("ArtiDAO 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 ArtiDAO", Env.SUB); + Mockito.doReturn(tt).when(trans).start("DELETE APPROVAL",Env.REMOTE); + Mockito.doNothing().when(tt).done(); + ArtiDAO.Data data = new ArtiDAO.Data(); + HistoryDAO historyDAO = Mockito.mock(HistoryDAO.class); + + ArtiDAO daoObj = new ArtiDAO(trans, historyDAO, Mockito.mock(CacheInfoDAO.class)); + } + +} + + +class ArtiDAOImpl extends ArtiDAO{ + + public ArtiDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace, PSInfo createPS ) { + super(trans, cluster, keyspace); + this.createPS = createPS; + setPs(this, createPS, "psByMechID"); + setPs(this, createPS, "psByMachine"); + setPs(this, createPS, "psByNs"); + } + + public ArtiDAOImpl(AuthzTrans trans, Cluster cluster, String keyspace,PSInfo readPS, HistoryDAO historyDAO ) { + super(trans, cluster, keyspace); + this.deletePS = readPS; + this.readPS = readPS; + setHistoryDao(this, historyDAO); + setSession(this, Mockito.mock(Session.class)); + } + + public void setPs(ArtiDAOImpl ArtiDAOObj, PSInfo psInfoObj, String methodName) { + Field nsDaoField; + try { + nsDaoField = ArtiDAO.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(ArtiDAOObj, 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 setHistoryDao(ArtiDAOImpl ArtiDAOObj, HistoryDAO historyDAO) { + Field nsDaoField; + try { + nsDaoField = ArtiDAO.class.getDeclaredField("historyDAO"); + + 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(ArtiDAOObj, historyDAO); + } 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(ArtiDAOImpl ArtiDAOObj, 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(ArtiDAOObj, 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/hl/JU_Function.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_Function.java index df370b7e..1f2727ce 100644 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_Function.java +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_Function.java @@ -22,21 +22,27 @@ package org.onap.aaf.auth.dao.hl; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.MockitoAnnotations.initMocks; import java.io.IOException; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.nio.ByteBuffer; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.GregorianCalendar; import java.util.HashSet; import java.util.List; import java.util.Properties; import java.util.Set; +import java.util.UUID; import org.junit.Before; import org.junit.Test; @@ -48,13 +54,19 @@ import org.onap.aaf.auth.dao.cached.CachedNSDAO; import org.onap.aaf.auth.dao.cached.CachedPermDAO; import org.onap.aaf.auth.dao.cached.CachedRoleDAO; import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO; +import org.onap.aaf.auth.dao.cass.ApprovalDAO; import org.onap.aaf.auth.dao.cass.CredDAO; +import org.onap.aaf.auth.dao.cass.FutureDAO; import org.onap.aaf.auth.dao.cass.Namespace; import org.onap.aaf.auth.dao.cass.NsDAO; +import org.onap.aaf.auth.dao.cass.NsSplit; +import org.onap.aaf.auth.dao.cass.NsType; import org.onap.aaf.auth.dao.cass.PermDAO; import org.onap.aaf.auth.dao.cass.RoleDAO; import org.onap.aaf.auth.dao.cass.Status; import org.onap.aaf.auth.dao.cass.UserRoleDAO; +import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP; +import org.onap.aaf.auth.dao.hl.Function.Lookup; import org.onap.aaf.auth.dao.hl.Question.Access; import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.env.AuthzTrans.REQD_TYPE; @@ -69,8 +81,6 @@ import org.onap.aaf.cadi.config.Config; import org.onap.aaf.misc.env.APIException; import org.onap.aaf.misc.env.LogTarget; -import io.netty.util.internal.SystemPropertyUtil; - public class JU_Function { @Mock @@ -426,6 +436,52 @@ public class JU_Function { e.printStackTrace(); } } + public void setQuestionFutureDao(Question ques, FutureDAO futureDaoObj) { + Field nsDaoField; + try { + nsDaoField = Question.class.getDeclaredField("futureDAO"); + + 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(ques, futureDaoObj); + } 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 setQuestionApprovalDao(Question ques, ApprovalDAO approvalDaoObj) { + Field nsDaoField; + try { + nsDaoField = Question.class.getDeclaredField("approvalDAO"); + + 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(ques, approvalDaoObj); + } 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 testCreateNsAdminLoop() { Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); @@ -783,104 +839,1463 @@ public class JU_Function { assertTrue(result.status == 1); } -// @Test -// public void test4DeleteNsMayUserSuc() { -// Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); -// Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); -// Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); -// Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); -// try { -// Define.set(access); -// } catch (CadiException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class); + @Test + public void test4DeleteNsMayUserSuc() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class); + List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); + NsDAO.Data dataObj = new NsDAO.Data(); + dataObj.type=1; + dataAl.add(dataObj); + Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]); + Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString()); + setQuestion(ques, nsDaoObj); + + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.deleteNS(trans, "test"); + assertTrue(result.status == 1); + + Mockito.doReturn(true).when(ques).isGranted(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); + + CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class); + Mockito.doReturn(retVal2).when(credDAO).readNS(Mockito.any(), Mockito.anyString()); + setQuestionCredDao(ques, credDAO); + + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); + Result<List<PermDAO.Data>> retVal5 = new Result<List<PermDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(retVal5).when(cachedPermDAO).readNS(trans, "test"); + setQuestionCachedPermDao(ques, cachedPermDAO); + + CachedUserRoleDAO cachedUserRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + List<UserRoleDAO.Data> dataObj4 = new ArrayList<>(); + UserRoleDAO.Data indData4 = new UserRoleDAO.Data(); + indData4.ns = "test"; + indData4.rname = "test"; + dataObj4.add(indData4); + Result<List<UserRoleDAO.Data>> retVal4 = new Result<List<UserRoleDAO.Data>>(dataObj4,0,"test",new String[0]); + Mockito.doReturn(retVal4).when(cachedUserRoleDAO).readByRole(trans, "test"); + setQuestionUserRoleDao(ques, cachedUserRoleDAO); + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + List<RoleDAO.Data> dataObj1 = new ArrayList<>(); + RoleDAO.Data indData1 = new RoleDAO.Data(); + indData1.ns = "test"; + indData1.name = "test"; + Set<String> permsSet = new HashSet<>(); + permsSet.add("test|test"); + indData1.perms = permsSet; + dataObj1.add(indData1); + Result<List<RoleDAO.Data>> retVal3 = new Result<List<RoleDAO.Data>>(dataObj1,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(cachedRoleDAO).readNS(trans, "test"); + Mockito.doReturn(retVal3).when(cachedRoleDAO).read(trans, indData1); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + + funcObj = new Function(trans, ques); + result = funcObj.deleteNS(trans, "test"); + assertTrue(result.status == Status.ERR_DependencyExists); + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + setQuestionUserRoleDao(ques, userRoleDAO); + Mockito.doReturn(retVal4).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + + Mockito.doReturn(true).when(trans).requested(REQD_TYPE.force); + funcObj = new Function(trans, ques); + result = funcObj.deleteNS(trans, "test"); + assertNull(result); + } + @Test + public void test4DeleteNsDrivensFailure() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class); + List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); + NsDAO.Data dataObj = new NsDAO.Data(); + dataObj.type=1; + dataAl.add(dataObj); + Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]); + Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString()); + setQuestion(ques, nsDaoObj); + + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.deleteNS(trans, "test"); + assertTrue(result.status == 1); + + Mockito.doReturn(true).when(ques).isGranted(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); + + CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class); + Mockito.doReturn(retVal2).when(credDAO).readNS(Mockito.any(), Mockito.anyString()); + setQuestionCredDao(ques, credDAO); + + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); + List<PermDAO.Data> dataObj5 = new ArrayList<>(); + PermDAO.Data indData5 = new PermDAO.Data(); + indData5.ns = "test"; + indData5.type = "test"; + dataObj5.add(indData5); + Result<List<PermDAO.Data>> retVal5 = new Result<List<PermDAO.Data>>(dataObj5,0,"test",new String[0]); + Mockito.doReturn(retVal5).when(cachedPermDAO).readNS(trans, "test"); + Mockito.doReturn(retVal5).when(cachedPermDAO).readNS(trans, "test.test"); + Mockito.doReturn(retVal5).when(cachedPermDAO).read(trans, indData5); + setQuestionCachedPermDao(ques, cachedPermDAO); + + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + List<RoleDAO.Data> dataObj1 = new ArrayList<>(); + RoleDAO.Data indData1 = new RoleDAO.Data(); + indData1.ns = "test"; + indData1.name = "test"; + Set<String> permsSet = new HashSet<>(); + permsSet.add("test|test"); + indData1.perms = permsSet; + dataObj1.add(indData1); + Result<List<RoleDAO.Data>> retVal3 = new Result<List<RoleDAO.Data>>(dataObj1,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(cachedRoleDAO).readNS(trans, "test"); + Mockito.doReturn(retVal3).when(cachedRoleDAO).readNS(trans, "test.test"); + Mockito.doReturn(retVal3).when(cachedRoleDAO).read(trans, indData1); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + + funcObj = new Function(trans, ques); + result = funcObj.deleteNS(trans, "test"); + assertTrue(result.status == Status.ERR_DependencyExists); + + NsDAO.Data data = new NsDAO.Data(); + data.name="test"; + Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,1,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + + Mockito.doReturn(true).when(trans).requested(REQD_TYPE.force); + funcObj = new Function(trans, ques); + result = funcObj.deleteNS(trans, "test.test"); + assertTrue(result.status == 1); + } + @Test + public void test4DeleteNsWithDot() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class); + List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); + NsDAO.Data dataObj = new NsDAO.Data(); + dataObj.type=1; + dataAl.add(dataObj); + Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]); + Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString()); + setQuestion(ques, nsDaoObj); + + List<CredDAO.Data> nsDataList = new ArrayList<CredDAO.Data>(); + CredDAO.Data nsData = new CredDAO.Data(); + nsData.id="test"; + nsDataList.add(nsData); + Result<List<CredDAO.Data>> retVal21 = new Result<List<CredDAO.Data>>(nsDataList,0,"test",new String[0]); + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.deleteNS(trans, "test"); + assertTrue(result.status == 1); + + Mockito.doReturn(true).when(ques).isGranted(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); + + CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class); + Mockito.doReturn(retVal21).when(credDAO).readNS(Mockito.any(), Mockito.anyString()); + Mockito.doReturn(retVal21).when(credDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); + setQuestionCredDao(ques, credDAO); + + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); + List<PermDAO.Data> dataObj5 = new ArrayList<>(); + PermDAO.Data indData5 = new PermDAO.Data(); + indData5.ns = "test"; + indData5.type = "test"; + dataObj5.add(indData5); + Result<List<PermDAO.Data>> retVal5 = new Result<List<PermDAO.Data>>(dataObj5,0,"test",new String[0]); + Mockito.doReturn(retVal5).when(cachedPermDAO).readNS(trans, "test"); + Mockito.doReturn(retVal5).when(cachedPermDAO).readNS(trans, "test.test"); + Mockito.doReturn(retVal5).when(cachedPermDAO).read(trans, indData5); + setQuestionCachedPermDao(ques, cachedPermDAO); + + CachedUserRoleDAO cachedUserRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + List<UserRoleDAO.Data> dataObj4 = new ArrayList<>(); + UserRoleDAO.Data indData4 = new UserRoleDAO.Data(); + indData4.ns = "test"; + indData4.rname = "test"; + dataObj4.add(indData4); + Result<List<UserRoleDAO.Data>> retVal4 = new Result<List<UserRoleDAO.Data>>(dataObj4,0,"test",new String[0]); + Mockito.doReturn(retVal4).when(cachedUserRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + Mockito.doReturn(retVal4).when(cachedUserRoleDAO).readByUser(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, cachedUserRoleDAO); + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + List<RoleDAO.Data> dataObj1 = new ArrayList<>(); + RoleDAO.Data indData1 = new RoleDAO.Data(); + indData1.ns = "test"; + indData1.name = "admin"; + Set<String> permsSet = new HashSet<>(); + permsSet.add("test|test"); + indData1.perms = permsSet; + dataObj1.add(indData1); + Result<List<RoleDAO.Data>> retVal3 = new Result<List<RoleDAO.Data>>(dataObj1,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(cachedRoleDAO).readNS(trans, "test"); + Mockito.doReturn(retVal3).when(cachedRoleDAO).readNS(trans, "test.test"); + Mockito.doReturn(retVal3).when(cachedRoleDAO).read(trans, indData1); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + + funcObj = new Function(trans, ques); + result = funcObj.deleteNS(trans, "test"); + assertTrue(result.status == Status.ERR_DependencyExists); + + NsDAO.Data data = new NsDAO.Data(); + data.name="test"; + Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + + Mockito.doReturn(true).when(trans).requested(REQD_TYPE.force); + funcObj = new Function(trans, ques); + result = funcObj.deleteNS(trans, "test.test"); + assertNull(result); + } + @Test + public void testGetOwners() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); +// List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); +// NsDAO.Data dataObj = new NsDAO.Data(); +// dataObj.type=1; +// dataAl.add(dataObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); +// +// Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); +// Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write); +// + Function funcObj = new Function(trans, ques); + Result<List<String>> result = funcObj.getOwners(trans, "test", false); + assertTrue(result.status == 1); +// + } + + @Test + public void testDelOwner() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + Mockito.doReturn(retVal).when(userRoleDAO).read(Mockito.any(), Mockito.any( UserRoleDAO.Data.class)); + setQuestionUserRoleDao(ques, userRoleDAO); + + NsDAO.Data data = new NsDAO.Data(); + data.name="test"; + Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(data,1,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal1.value, Access.write); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.delOwner(trans, "test", "test"); + assertTrue(result.status == 1); + + retVal1 = new Result<NsDAO.Data>(data,1,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + result = funcObj.delOwner(trans, "test", "test"); + assertTrue(result.status == 1); + + retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + result = funcObj.delOwner(trans, "test", "test"); + retVal2 = new Result<NsDAO.Data>(data,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal1.value, Access.write); + result = funcObj.delOwner(trans, "test", "test"); +// + } + + @Test + public void testGetAdmins() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); // List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); // NsDAO.Data dataObj = new NsDAO.Data(); // dataObj.type=1; // dataAl.add(dataObj); -// Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]); -// Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString()); -// setQuestion(ques, nsDaoObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); // // Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); // Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write); // -// Function funcObj = new Function(trans, ques); -// Result<Void> result = funcObj.deleteNS(trans, "test"); + Function funcObj = new Function(trans, ques); + Result<List<String>> result = funcObj.getAdmins(trans, "test", false); + assertTrue(result.status == 1); +// + } + + @Test + public void testDelAdmin() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readUserInRole(Mockito.any(), Mockito.anyString(), Mockito.anyString()); + Mockito.doReturn(retVal).when(userRoleDAO).read(Mockito.any(), Mockito.any( UserRoleDAO.Data.class)); + setQuestionUserRoleDao(ques, userRoleDAO); + + NsDAO.Data data = new NsDAO.Data(); + data.name="test"; + Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(data,1,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal1.value, Access.write); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.delAdmin(trans, "test", "test"); + assertTrue(result.status == 1); + + retVal1 = new Result<NsDAO.Data>(data,1,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + result = funcObj.delAdmin(trans, "test", "test"); + assertTrue(result.status == 1); + + retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + result = funcObj.delOwner(trans, "test", "test"); + retVal2 = new Result<NsDAO.Data>(data,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal1.value, Access.write); + result = funcObj.delAdmin(trans, "test", "test"); +// + } + + @Test + public void testMovePerms() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); +// List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); +// NsDAO.Data dataObj = new NsDAO.Data(); +// dataObj.type=1; +// dataAl.add(dataObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + Mockito.doReturn(retVal).when(cachedRoleDAO).delPerm(Mockito.any(), Mockito.any(), Mockito.any()); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); + Mockito.doReturn(retVal).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + setQuestionCachedPermDao(ques, cachedPermDAO); + + NsDAO.Data nsDataObj = new NsDAO.Data(); + nsDataObj.name="test"; + StringBuilder sb = new StringBuilder(); + Result<List<PermDAO.Data>> retVal1 = new Result<List<PermDAO.Data>>(null,1,"test",new String[0]); + + invokeMovePerms(nsDataObj, sb, retVal1); + + List<PermDAO.Data> dataObj5 = new ArrayList<>(); + PermDAO.Data indData5 = new PermDAO.Data(); + indData5.ns = "test"; + indData5.type = "test"; + Set<String> rolesSet = new HashSet<>(); + rolesSet.add("testRole"); + indData5.roles = rolesSet; + dataObj5.add(indData5); + indData5 = new PermDAO.Data(); + indData5.ns = "test"; + indData5.type = "access"; + dataObj5.add(indData5); + retVal1 = new Result<List<PermDAO.Data>>(dataObj5,0,"test",new String[0]); + + Result<List<UserRoleDAO.Data>> retVal3 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + Mockito.doReturn(retVal3).when(cachedPermDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); + + NsSplit splitObj = new NsSplit("test", "test"); + Result<NsSplit> retVal2 = new Result<NsSplit>(splitObj,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).deriveNsSplit(Mockito.any(), Mockito.anyString()); + + invokeMovePerms(nsDataObj, sb, retVal1); + + Result<List<UserRoleDAO.Data>> retVal4 = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal4).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + invokeMovePerms(nsDataObj, sb, retVal1); + + Mockito.doReturn(retVal3).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + Mockito.doReturn(retVal4).when(cachedPermDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); + invokeMovePerms(nsDataObj, sb, retVal1); + + } + + private void invokeMovePerms(NsDAO.Data nsDataObj, StringBuilder sb,Result<List<PermDAO.Data>> retVal1) { + Function funcObj = new Function(trans, ques); + Method met; + try { + met = Function.class.getDeclaredMethod("movePerms", AuthzTrans.class, NsDAO.Data.class, StringBuilder.class, Result.class); + met.setAccessible(true); + met.invoke(funcObj, trans, nsDataObj, sb, retVal1); + } catch (NoSuchMethodException | SecurityException 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(); + } + } + + @Test + public void testMoveRoles() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); +// List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); +// NsDAO.Data dataObj = new NsDAO.Data(); +// dataObj.type=1; +// dataAl.add(dataObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + Mockito.doReturn(retVal).when(cachedRoleDAO).delPerm(Mockito.any(), Mockito.any(), Mockito.any()); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + + Mockito.doReturn(retVal).when(cachedRoleDAO).create(Mockito.any(), Mockito.any()); + + NsDAO.Data nsDataObj = new NsDAO.Data(); + nsDataObj.name="test"; + StringBuilder sb = new StringBuilder(); + Result<List<RoleDAO.Data>> retVal1 = new Result<List<RoleDAO.Data>>(null,1,"test",new String[0]); + + invokeMoveRoles(nsDataObj, sb, retVal1); + + List<RoleDAO.Data> dataObj5 = new ArrayList<>(); + RoleDAO.Data indData5 = new RoleDAO.Data(); + indData5.ns = "test"; + indData5.name = "test"; + Set<String> rolesSet = new HashSet<>(); + rolesSet.add("testRole"); + indData5.perms = rolesSet; + dataObj5.add(indData5); + indData5 = new RoleDAO.Data(); + indData5.ns = "test"; + indData5.name = "admin"; + dataObj5.add(indData5); + retVal1 = new Result<List<RoleDAO.Data>>(dataObj5,0,"test",new String[0]); + + Result<List<UserRoleDAO.Data>> retVal3 = new Result<List<UserRoleDAO.Data>>(null,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(cachedRoleDAO).create(Mockito.any(), Mockito.any()); + Mockito.doReturn(retVal3).when(cachedRoleDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); + + NsSplit splitObj = new NsSplit("test", "test"); + Result<NsSplit> retVal2 = new Result<NsSplit>(splitObj,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).deriveNsSplit(Mockito.any(), Mockito.anyString()); + + invokeMoveRoles(nsDataObj, sb, retVal1); + + Result<List<UserRoleDAO.Data>> retVal4 = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal4).when(cachedRoleDAO).create(Mockito.any(), Mockito.any()); + invokeMoveRoles(nsDataObj, sb, retVal1); + + Mockito.doReturn(retVal3).when(cachedRoleDAO).create(Mockito.any(), Mockito.any()); + Mockito.doReturn(retVal4).when(cachedRoleDAO).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); + invokeMoveRoles(nsDataObj, sb, retVal1); + + } + + private void invokeMoveRoles(NsDAO.Data nsDataObj, StringBuilder sb,Result<List<RoleDAO.Data>> retVal1) { + Function funcObj = new Function(trans, ques); + Method met; + try { + met = Function.class.getDeclaredMethod("moveRoles", AuthzTrans.class, NsDAO.Data.class, StringBuilder.class, Result.class); + met.setAccessible(true); + met.invoke(funcObj, trans, nsDataObj, sb, retVal1); + } catch (NoSuchMethodException | SecurityException 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(); + } + } + + @Test + public void testCreatePerm() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(true).when(trans).requested(REQD_TYPE.force); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List<PermDAO.Data> dataAl = new ArrayList<PermDAO.Data>(); + PermDAO.Data perm = new PermDAO.Data(); + Set<String> rolesSet = new HashSet<>(); + rolesSet.add("testRole"); + perm.roles = rolesSet; +// perm.type=1 + dataAl.add(perm); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(ques).deriveNsSplit(Mockito.any(), Mockito.anyString()); + + CachedRoleDAO userRoleDAO = Mockito.mock(CachedRoleDAO.class); + Mockito.doReturn(retVal).when(userRoleDAO).read(Mockito.any(), Mockito.any(RoleDAO.Data.class)); + Mockito.doReturn(retVal).when(userRoleDAO).create(Mockito.any(), Mockito.any(RoleDAO.Data.class)); + setQuestionCachedRoleDao(ques, userRoleDAO); + + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); + Mockito.doReturn(retVal).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + Mockito.doReturn(retVal).when(cachedPermDAO).read(trans, perm); + setQuestionCachedPermDao(ques, cachedPermDAO); + + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,1,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,perm, Access.write); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.createPerm(trans, perm, false); + assertTrue(result.status == 1); + + retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,perm, Access.write); + result = funcObj.createPerm(trans, perm, false); + assertTrue(result.status == 1); + + NsSplit nsObj = new NsSplit("test","test"); + Result<NsSplit> retValNs = new Result<NsSplit>(nsObj,0,"test",new String[0]); + Mockito.doReturn(retValNs).when(ques).deriveNsSplit(Mockito.any(), Mockito.anyString()); + Mockito.doReturn(retVal2).when(ques).mayUser(Mockito.any(), Mockito.anyString(),Mockito.any(RoleDAO.Data.class), Mockito.any()); + result = funcObj.createPerm(trans, perm, false); + + Mockito.doReturn(retVal).when(cachedPermDAO).read(trans, perm); + result = funcObj.createPerm(trans, perm, true); + assertTrue(result.status == 1); + + Mockito.doReturn(retVal2).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + result = funcObj.createPerm(trans, perm, true); + assertTrue(result.status == 0); + + Mockito.doReturn(false).when(trans).requested(REQD_TYPE.force); + Result<List<PermDAO.Data>> retVal1 = new Result<List<PermDAO.Data>>(dataAl,0,"test",new String[0]); + Mockito.doReturn(retVal1).when(cachedPermDAO).read(trans, perm); + result = funcObj.createPerm(trans, perm, true); + assertTrue(result.status == Status.ERR_ConflictAlreadyExists); + + } + @Test + public void testDeletePerm() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List<PermDAO.Data> dataAl = new ArrayList<PermDAO.Data>(); + PermDAO.Data perm = new PermDAO.Data(); + Set<String> rolesSet = new HashSet<>(); + rolesSet.add("testRole"); + perm.roles = rolesSet; +// perm.type=1 + dataAl.add(perm); + + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,1,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,perm, Access.write); + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); +// List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); +// NsDAO.Data dataObj = new NsDAO.Data(); +// dataObj.type=1; +// dataAl.add(dataObj); + Result<List<PermDAO.Data>> retVal = new Result<List<PermDAO.Data>>(dataAl,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.deletePerm(trans, perm, true,false); + assertTrue(result.status == 1); + + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); +// Mockito.doReturn(retVal).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + Mockito.doReturn(retVal).when(cachedPermDAO).read(trans, perm); + setQuestionCachedPermDao(ques, cachedPermDAO); + + result = funcObj.deletePerm(trans, perm, true,true); + assertTrue(result.status == Status.ERR_PermissionNotFound); + + retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,perm, Access.write); + Result<List<PermDAO.Data>> retVal3 = new Result<List<PermDAO.Data>>(dataAl,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(cachedPermDAO).read(trans, perm); + + NsSplit nsObj = new NsSplit("test","test"); + Result<NsSplit> retValNs = new Result<NsSplit>(nsObj,0,"test",new String[0]); + Mockito.doReturn(retValNs).when(ques).deriveNsSplit(Mockito.any(), Mockito.anyString()); + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + Mockito.doReturn(retVal).when(cachedRoleDAO).delPerm(Mockito.any(), Mockito.any(), Mockito.any()); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + + result = funcObj.deletePerm(trans, perm, true,false); + assertNull(result); + + Mockito.doReturn(retVal2).when(cachedRoleDAO).delPerm(Mockito.any(), Mockito.any(), Mockito.any()); + result = funcObj.deletePerm(trans, perm, true,false); + assertNull(result); + + result = funcObj.deletePerm(trans, perm, false,false); // assertTrue(result.status == 1); -// -// Mockito.doReturn(true).when(ques).isGranted(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); -// -// CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class); -// Mockito.doReturn(retVal2).when(credDAO).readNS(Mockito.any(), Mockito.anyString()); -// setQuestionCredDao(ques, credDAO); -// -// CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); -// Result<List<PermDAO.Data>> retVal5 = new Result<List<PermDAO.Data>>(null,0,"test",new String[0]); -// Mockito.doReturn(retVal5).when(cachedPermDAO).readNS(trans, "test"); -// setQuestionCachedPermDao(ques, cachedPermDAO); + } + + @Test + public void testDeleteRole() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + List<UserRoleDAO.Data> dataAlUser = new ArrayList<UserRoleDAO.Data>(); + UserRoleDAO.Data roleUser = new UserRoleDAO.Data(); + Set<String> rolesSetUser = new HashSet<>(); + rolesSetUser.add("testRole|test|test"); +// perm.roles = rolesSet; +// perm.type=1 + dataAlUser.add(roleUser); + + List<RoleDAO.Data> dataAl = new ArrayList<RoleDAO.Data>(); + RoleDAO.Data role = new RoleDAO.Data(); + Set<String> rolesSet = new HashSet<>(); + rolesSet.add("testRole|test|test"); + role.perms = rolesSet; +// perm.roles = rolesSet; +// perm.type=1 + dataAl.add(role); + + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,1,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,role, Access.write); + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); +// List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); +// NsDAO.Data dataObj = new NsDAO.Data(); +// dataObj.type=1; +// dataAl.add(dataObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); // -// CachedUserRoleDAO cachedUserRoleDAO = Mockito.mock(CachedUserRoleDAO.class); -// List<UserRoleDAO.Data> dataObj4 = new ArrayList<>(); -// UserRoleDAO.Data indData4 = new UserRoleDAO.Data(); -// indData4.ns = "test"; -// indData4.rname = "test"; -// dataObj4.add(indData4); -// Result<List<UserRoleDAO.Data>> retVal4 = new Result<List<UserRoleDAO.Data>>(dataObj4,0,"test",new String[0]); -// Mockito.doReturn(retVal4).when(cachedUserRoleDAO).readByRole(trans, "test"); -// setQuestionUserRoleDao(ques, cachedUserRoleDAO); +// Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); +// Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write); +// + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.deleteRole(trans, role, true, false); + assertTrue(result.status == 1); + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + Result<List<RoleDAO.Data>> retVal1 = new Result<List<RoleDAO.Data>>(dataAl,0,"test",new String[0]); + Mockito.doReturn(retVal1).when(cachedRoleDAO).read(Mockito.any(), Mockito.any(RoleDAO.Data.class)); + NsSplit splitObj = new NsSplit("test", "test"); + Result<NsSplit> retVal3 = new Result<NsSplit>(splitObj,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(ques).deriveNsSplit(Mockito.any(), Mockito.anyString()); + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); +// Mockito.doReturn(retVal).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + Mockito.doReturn(retVal).when(cachedPermDAO).delRole(Mockito.any(), Mockito.any(),Mockito.any()); + setQuestionCachedPermDao(ques, cachedPermDAO); + result = funcObj.deleteRole(trans, role, true, true); + assertNull(result); + + Mockito.doReturn(retVal1).when(cachedPermDAO).delRole(Mockito.any(), Mockito.any(),Mockito.any()); + result = funcObj.deleteRole(trans, role, true, true); + assertNull(result); + + Mockito.doReturn(retVal).when(cachedRoleDAO).read(Mockito.any(), Mockito.any(RoleDAO.Data.class)); + result = funcObj.deleteRole(trans, role, true, true); + assertTrue(result.status == Status.ERR_RoleNotFound); + + retVal = new Result<List<UserRoleDAO.Data>>(dataAlUser,0,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + result = funcObj.deleteRole(trans, role, false, true); + assertTrue(result.status == Status.ERR_DependencyExists); + } + + @Test + public void testAddPermToRole() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List<PermDAO.Data> dataAlPerm = new ArrayList<PermDAO.Data>(); + PermDAO.Data rolePerm = new PermDAO.Data(); + Set<String> rolesSetUser = new HashSet<>(); + rolesSetUser.add("testRole|test|test"); +// perm.roles = rolesSet; +// perm.type=1 + dataAlPerm.add(rolePerm); + + List<RoleDAO.Data> dataAl = new ArrayList<RoleDAO.Data>(); + RoleDAO.Data role = new RoleDAO.Data(); + Set<String> rolesSet = new HashSet<>(); + rolesSet.add("17623"); + role.perms = rolesSet; +// perm.roles = rolesSet; +// perm.type=1 + dataAl.add(role); + + NsDAO.Data nsObj = new NsDAO.Data(); + nsObj.name="test"; + NsDAO.Data nsObj1 = new NsDAO.Data(); + nsObj1.name="test12"; + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); +// List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); +// NsDAO.Data dataObj = new NsDAO.Data(); +// dataObj.type=1; +// dataAl.add(dataObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); + + Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(nsObj,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).deriveFirstNsForType(trans, role.ns, NsType.COMPANY); + Mockito.doReturn(retVal2).when(ques).deriveFirstNsForType(trans, rolePerm.ns, NsType.COMPANY); + + Result<NsDAO.Data> retVal3 = new Result<NsDAO.Data>(null,1,"test",new String[0]); + Mockito.doReturn(retVal3).when(ques).mayUser(trans, null,rolePerm, Access.write); + Mockito.doReturn(retVal3).when(ques).mayUser(trans, null,role, Access.write); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.addPermToRole(trans, role, rolePerm, false); + assertTrue(result.status == 1); + + retVal2 = new Result<NsDAO.Data>(nsObj,1,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).deriveFirstNsForType(trans, role.ns, NsType.COMPANY); + Mockito.doReturn(retVal2).when(ques).deriveFirstNsForType(trans, rolePerm.ns, NsType.COMPANY); + result = funcObj.addPermToRole(trans, role, rolePerm, false); + assertTrue(result.status == 1); + + role.ns="test2"; + retVal2 = new Result<NsDAO.Data>(nsObj,0,"test",new String[0]); + Mockito.doReturn(retVal2).when(ques).deriveFirstNsForType(trans, role.ns, NsType.COMPANY); + result = funcObj.addPermToRole(trans, role, rolePerm, false); + assertTrue(result.status == 1); + + retVal2 = new Result<NsDAO.Data>(nsObj,0,"test1",new String[0]); + Mockito.doReturn(retVal2).when(ques).deriveFirstNsForType(trans, role.ns, NsType.COMPANY); + Result<NsDAO.Data> retVal21 = new Result<NsDAO.Data>(nsObj1,0,"test1",new String[0]); + Mockito.doReturn(retVal21).when(ques).deriveFirstNsForType(trans, rolePerm.ns, NsType.COMPANY); + result = funcObj.addPermToRole(trans, role, rolePerm, false); + assertTrue(result.status == 1); + + retVal3 = new Result<NsDAO.Data>(null,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(ques).mayUser(trans, null,rolePerm, Access.write); + retVal2 = new Result<NsDAO.Data>(nsObj,0,"test1",new String[0]); + Mockito.doReturn(retVal2).when(ques).deriveFirstNsForType(trans, role.ns, NsType.COMPANY); + Mockito.doReturn(retVal2).when(ques).deriveFirstNsForType(trans, rolePerm.ns, NsType.COMPANY); + + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); +// Mockito.doReturn(retVal).when(cachedPermDAO).create(Mockito.any(), Mockito.any()); + Mockito.doReturn(retVal).when(cachedPermDAO).read(Mockito.any(), Mockito.any(PermDAO.Data.class)); + setQuestionCachedPermDao(ques, cachedPermDAO); + + result = funcObj.addPermToRole(trans, role, rolePerm, false); + assertTrue(result.status == Status.ERR_PermissionNotFound); + + Result<List<PermDAO.Data>> retValPerm= new Result<List<PermDAO.Data>>(dataAlPerm,0,"test1",new String[0]); + Mockito.doReturn(retValPerm).when(cachedPermDAO).read(Mockito.any(), Mockito.any(PermDAO.Data.class)); + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + Mockito.doReturn(retVal3).when(cachedRoleDAO).read(trans, role); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + + result = funcObj.addPermToRole(trans, role, rolePerm, true); + assertTrue(result.status == 22); + + Mockito.doReturn(true).when(trans).requested(REQD_TYPE.force); + result = funcObj.addPermToRole(trans, role, rolePerm, true); + assertTrue(result.status == 2); + + retVal3 = new Result<NsDAO.Data>(null,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(ques).mayUser(trans, null,role, Access.write); + Mockito.doReturn(retVal3).when(cachedRoleDAO).create(trans, role); + result = funcObj.addPermToRole(trans, role, rolePerm, true); +// System.out.println(result.status); + assertNull(result); + + retVal3 = new Result<NsDAO.Data>(null,1,"test",new String[0]); + Mockito.doReturn(retVal3).when(cachedRoleDAO).create(trans, role); + result = funcObj.addPermToRole(trans, role, rolePerm, true); + assertTrue(result.status == 1); + + Result<List<RoleDAO.Data>> retVal31 = new Result<List<RoleDAO.Data>>(dataAl,0,"test",new String[0]); + Mockito.doReturn(retVal31).when(cachedRoleDAO).read(trans, role); + result = funcObj.addPermToRole(trans, role, rolePerm, true); + assertTrue(result.status == 7); + } + + @Test + public void testDelPermFromRole() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List<PermDAO.Data> dataAlPerm = new ArrayList<PermDAO.Data>(); + PermDAO.Data rolePerm = new PermDAO.Data(); + Set<String> rolesSetUser = new HashSet<>(); + rolesSetUser.add("testRole|test|test"); +// perm.roles = rolesSet; +// perm.type=1 + dataAlPerm.add(rolePerm); + + List<RoleDAO.Data> dataAl = new ArrayList<RoleDAO.Data>(); + RoleDAO.Data role = new RoleDAO.Data(); + Set<String> rolesSet = new HashSet<>(); + rolesSet.add("17623"); + role.perms = rolesSet; + dataAl.add(role); + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); +// List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); +// NsDAO.Data dataObj = new NsDAO.Data(); +// dataObj.type=1; +// dataAl.add(dataObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); + + Result<NsDAO.Data> retValFail = new Result<NsDAO.Data>(null,1,"test",new String[0]); + Result<NsDAO.Data> retValSuc = new Result<NsDAO.Data>(null,0,"test",new String[0]); + Mockito.doReturn(retValFail).when(ques).mayUser(trans, null,rolePerm, Access.write); + Mockito.doReturn(retValFail).when(ques).mayUser(trans, null,role, Access.write); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.delPermFromRole(trans, role, rolePerm, false); + assertTrue(result.status == Status.ERR_Denied); + + Mockito.doReturn(retValFail).when(ques).mayUser(trans, null,rolePerm, Access.write); + Mockito.doReturn(retValSuc).when(ques).mayUser(trans, null,role, Access.write); + + CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); + Mockito.doReturn(retValFail).when(cachedRoleDAO).read(trans, role); + setQuestionCachedRoleDao(ques, cachedRoleDAO); + + CachedPermDAO cachedPermDAO = Mockito.mock(CachedPermDAO.class); + Mockito.doReturn(retVal).when(cachedPermDAO).read(Mockito.any(), Mockito.any(PermDAO.Data.class)); + setQuestionCachedPermDao(ques, cachedPermDAO); + + result = funcObj.delPermFromRole(trans, role, rolePerm, false); + assertTrue(result.status == 1); + + Result<List<PermDAO.Data>> retValPermSuc = new Result<List<PermDAO.Data>>(dataAlPerm,0,"test",new String[0]); + Mockito.doReturn(retValPermSuc).when(cachedPermDAO).read(Mockito.any(), Mockito.any(PermDAO.Data.class)); + result = funcObj.delPermFromRole(trans, role, rolePerm, false); + assertTrue(result.status == 1); + + Result<List<RoleDAO.Data>> retValRoleSuc = new Result<List<RoleDAO.Data>>(dataAl,0,"test",new String[0]); + Mockito.doReturn(retValRoleSuc).when(cachedRoleDAO).read(Mockito.any(), Mockito.any(RoleDAO.Data.class)); + result = funcObj.delPermFromRole(trans, role, rolePerm, true); + assertTrue(result.status == Status.ERR_PermissionNotFound); + + role.perms = null; + dataAl.add(role); + rolesSet.add("null|null|null|null"); + role.perms = rolesSet; + dataAl.add(role); + Mockito.doReturn(retValRoleSuc).when(cachedRoleDAO).read(Mockito.any(), Mockito.any(RoleDAO.Data.class)); + Mockito.doReturn(retVal).when(cachedPermDAO).delRole(Mockito.any(), Mockito.any(),Mockito.any(RoleDAO.Data.class)); + result = funcObj.delPermFromRole(trans, role, rolePerm, true); + assertTrue(result.status == 1); + + Mockito.doReturn(true).when(trans).requested(REQD_TYPE.force); + result = funcObj.delPermFromRole(trans, role, rolePerm, true); + assertTrue(result.status == 1); + + Mockito.doReturn(retValRoleSuc).when(cachedPermDAO).delRole(Mockito.any(), Mockito.any(),Mockito.any(RoleDAO.Data.class)); + Mockito.doReturn(retVal).when(cachedRoleDAO).delPerm(Mockito.any(), Mockito.any(),Mockito.any(PermDAO.Data.class)); + result = funcObj.delPermFromRole(trans, role, rolePerm, true); + assertTrue(result.status == 1); + + Mockito.doReturn(retValPermSuc).when(cachedRoleDAO).delPerm(Mockito.any(), Mockito.any(),Mockito.any(PermDAO.Data.class)); + result = funcObj.delPermFromRole(trans, role, rolePerm, true); + assertTrue(result.status == 0); + + Mockito.doReturn(retVal).when(cachedPermDAO).read(Mockito.any(), Mockito.any(PermDAO.Data.class)); + result = funcObj.delPermFromRole(trans, role, rolePerm, true); + assertTrue(result.status == 0); + + Mockito.doReturn(retVal).when(cachedRoleDAO).delPerm(Mockito.any(), Mockito.any(),Mockito.any(PermDAO.Data.class)); + result = funcObj.delPermFromRole(trans, role, rolePerm, true); + assertTrue(result.status == 1); + + NsSplit splitObj = new NsSplit("test", "test"); + Result<NsSplit> retVal3 = new Result<NsSplit>(splitObj,0,"test",new String[0]); + Mockito.doReturn(retVal3).when(ques).deriveNsSplit(Mockito.any(), Mockito.anyString()); + Mockito.doReturn(retValFail).when(ques).mayUser(Mockito.any(), Mockito.anyString(),Mockito.any(RoleDAO.Data.class), Mockito.any()); + Mockito.doReturn(retValFail).when(ques).mayUser(Mockito.any(), Mockito.anyString(),Mockito.any(PermDAO.Data.class), Mockito.any()); + result = funcObj.delPermFromRole(trans, "test", rolePerm); + assertTrue(result.status == 2); + + retVal3 = new Result<NsSplit>(null,1,"test",new String[0]); + Mockito.doReturn(retVal3).when(ques).deriveNsSplit(Mockito.any(), Mockito.anyString()); + result = funcObj.delPermFromRole(trans, "test", rolePerm); + assertTrue(result.status == 1); + } + @Test + public void testAddUserRole() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List<UserRoleDAO.Data> urDataAl = new ArrayList<>(); + UserRoleDAO.Data urData = new UserRoleDAO.Data(); + urData.ns="test"; + urData.rname="test"; + urData.user="test"; + urDataAl.add(urData); + + Organization org = Mockito.mock(Organization.class); + Mockito.doReturn(org).when(trans).org(); + Mockito.doReturn(Mockito.mock(GregorianCalendar.class)).when(org).expiration(Mockito.any(), Mockito.any(), Mockito.anyString()); + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + CachedRoleDAO roleDAO = Mockito.mock(CachedRoleDAO.class); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Result<List<UserRoleDAO.Data>> retValSuc = new Result<List<UserRoleDAO.Data>>(urDataAl,0,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + Mockito.doReturn(retVal).when(userRoleDAO).read(Mockito.any(), Mockito.any(UserRoleDAO.Data.class)); + Mockito.doReturn(retVal).when(userRoleDAO).create(Mockito.any(), Mockito.any(UserRoleDAO.Data.class)); + Mockito.doReturn(retValSuc).when(roleDAO).read(Mockito.any(), Mockito.anyString(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); + setQuestionCachedRoleDao(ques, roleDAO); + + CachedCredDAO credDAO = Mockito.mock(CachedCredDAO.class); + Result<List<CredDAO.Data>> retVal2 = new Result<List<CredDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal2).when(credDAO).readID(Mockito.any(), Mockito.anyString()); + setQuestionCredDao(ques, credDAO); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.addUserRole(trans, urData); + assertTrue(result.status == 1); + + urData.rname=Question.ADMIN; + result = funcObj.addUserRole(trans, urData); + assertTrue(result.status == 1); + + NsDAO.Data data = new NsDAO.Data(); + data.name="test"; + Result<NsDAO.Data> retVal1 = new Result<NsDAO.Data>(data,0,"test",new String[0]); + Mockito.doReturn(retVal1).when(ques).mayUser(trans, null,retVal1.value, Access.write); + Mockito.doReturn(retVal1).when(ques).deriveNs(trans, "test"); + try { + Mockito.doReturn(Mockito.mock(Identity.class)).when(org).getIdentity(trans, "test"); + } catch (OrganizationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + urData.rname=Question.OWNER; + result = funcObj.addUserRole(trans, urData); + assertTrue(result.status == 1); + + Mockito.doReturn(retValSuc).when(userRoleDAO).create(Mockito.any(), Mockito.any(UserRoleDAO.Data.class)); + result = funcObj.addUserRole(trans, urData); + assertTrue(result.status == 0); + + Mockito.doReturn(retVal).when(roleDAO).read(Mockito.any(), Mockito.anyString(), Mockito.anyString()); + result = funcObj.addUserRole(trans, urData); + assertTrue(result.status == Status.ERR_RoleNotFound); + + Mockito.doReturn(retValSuc).when(userRoleDAO).read(Mockito.any(), Mockito.any(UserRoleDAO.Data.class)); + result = funcObj.addUserRole(trans, urData); + assertTrue(result.status == Status.ERR_ConflictAlreadyExists); + + result = funcObj.addUserRole(trans, "test", "test", "test"); + assertTrue(result.status == 1); + + try { + Mockito.doReturn(null).when(org).getIdentity(trans, "test"); + } catch (OrganizationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + result = funcObj.addUserRole(trans, "test", "test", "test"); + assertTrue(result.status == Result.ERR_BadData); + + try { + Mockito.doThrow(OrganizationException.class).when(org).getIdentity(trans, "test"); + } catch (OrganizationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + result = funcObj.addUserRole(trans, "test", "test", "test"); + assertTrue(result.status == 20); + } + @Test + public void testExtendUserRole() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + List<UserRoleDAO.Data> urDataAl = new ArrayList<>(); + UserRoleDAO.Data urData = new UserRoleDAO.Data(); + urData.ns="test"; + urData.rname="test"; + urData.user="test"; + urData.expires=new Date(); + urDataAl.add(urData); + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + CachedRoleDAO roleDAO = Mockito.mock(CachedRoleDAO.class); +// List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); +// NsDAO.Data dataObj = new NsDAO.Data(); +// dataObj.type=1; +// dataAl.add(dataObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Result<List<UserRoleDAO.Data>> retValSuc = new Result<List<UserRoleDAO.Data>>(urDataAl,0,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + Mockito.doReturn(retValSuc).when(roleDAO).read(Mockito.any(), Mockito.anyString(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); + setQuestionCachedRoleDao(ques, roleDAO); + + Organization org = Mockito.mock(Organization.class); + Mockito.doReturn(org).when(trans).org(); + Mockito.doReturn(Mockito.mock(GregorianCalendar.class)).when(org).expiration(Mockito.any(), Mockito.any()); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.extendUserRole(trans, urData, false); + assertNull(result); + + Mockito.doReturn(retVal).when(roleDAO).read(Mockito.any(), Mockito.anyString(), Mockito.anyString()); + Mockito.doReturn(retValSuc).when(userRoleDAO).read(trans, urData); + result = funcObj.extendUserRole(trans, urData, true); + assertTrue(result.status == Status.ERR_RoleNotFound); + + Mockito.doReturn(retVal).when(userRoleDAO).read(trans, urData); + result = funcObj.extendUserRole(trans, urData, true); + assertTrue(result.status == Status.ERR_UserRoleNotFound); + } + + @Test + public void testGetUsersByRole() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + List<UserRoleDAO.Data> urDataAl = new ArrayList<>(); + UserRoleDAO.Data urData = new UserRoleDAO.Data(); + urData.ns="test"; + urData.rname="test"; + urData.user="test"; + urData.expires=new Date(); + urDataAl.add(urData); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(urDataAl,0,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); + + Function funcObj = new Function(trans, ques); + Result<List<String>> result = funcObj.getUsersByRole(trans, "test", false); + assertTrue(result.status == 0); + + result = funcObj.getUsersByRole(trans, "test", true); + assertTrue(result.status == 0); + + urData.expires=new Date(130,1,1); + result = funcObj.getUsersByRole(trans, "test", true); + assertTrue(result.status == 0); // -// CachedRoleDAO cachedRoleDAO = Mockito.mock(CachedRoleDAO.class); -// List<RoleDAO.Data> dataObj1 = new ArrayList<>(); -// RoleDAO.Data indData1 = new RoleDAO.Data(); -// indData1.ns = "test"; -// indData1.name = "test"; -// Set<String> permsSet = new HashSet<>(); -// permsSet.add("test|test"); -// indData1.perms = permsSet; -// dataObj1.add(indData1); -// Result<List<RoleDAO.Data>> retVal3 = new Result<List<RoleDAO.Data>>(dataObj1,0,"test",new String[0]); -// Mockito.doReturn(retVal3).when(cachedRoleDAO).readNS(trans, "test"); -// setQuestionCachedRoleDao(ques, cachedRoleDAO); + } + @Test + public void testDelUserRole() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + List<UserRoleDAO.Data> urDataAl = new ArrayList<>(); + UserRoleDAO.Data urData = new UserRoleDAO.Data(); + urData.ns="test"; + urData.rname="test"; + urData.user="test"; + urData.expires=new Date(); + urDataAl.add(urData); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(urDataAl,0,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).read(Mockito.any(), Mockito.any( UserRoleDAO.Data.class)); + setQuestionUserRoleDao(ques, userRoleDAO); + + Function funcObj = new Function(trans, ques); + Result<Void> result = funcObj.delUserRole(trans, "test", "test", "test"); + assertNull(result); + + retVal = new Result<List<UserRoleDAO.Data>>(urDataAl,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).read(Mockito.any(), Mockito.any( UserRoleDAO.Data.class)); + result = funcObj.delUserRole(trans, "test", "test", "test"); +// assertTrue(result.status ==1); // -// funcObj = new Function(trans, ques); -// result = funcObj.deleteNS(trans, "test"); -// assertTrue(result.status == Status.ERR_DependencyExists); -// -// Mockito.doReturn(true).when(trans).requested(REQD_TYPE.force); -// funcObj = new Function(trans, ques); -// result = funcObj.deleteNS(trans, "test"); -// assertTrue(result.status == 2); -// } -// @Test -// public void test4DeleteNsMayUserSuc() { -// Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); -// Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); -// Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); -// Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); -// try { -// Define.set(access); -// } catch (CadiException e) { -// // TODO Auto-generated catch block -// e.printStackTrace(); -// } -// CachedNSDAO nsDaoObj = Mockito.mock(CachedNSDAO.class); + } + + @Test + public void testCreateFuture() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + FutureDAO.Data data = new FutureDAO.Data(); + data.memo = "test"; + NsDAO.Data nsd = new NsDAO.Data(); + nsd.name = "test"; + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + List<UserRoleDAO.Data> urDataAl = new ArrayList<>(); + UserRoleDAO.Data urData = new UserRoleDAO.Data(); + urData.ns="test"; + urData.rname="test"; + urData.user="test"; + urData.expires=new Date(); + urDataAl.add(urData); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(urDataAl,0,"test",new String[0]); + Result<List<UserRoleDAO.Data>> retValFail = new Result<List<UserRoleDAO.Data>>(urDataAl,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).read(Mockito.any(), Mockito.any( UserRoleDAO.Data.class)); + setQuestionUserRoleDao(ques, userRoleDAO); + + Function funcObj = new Function(trans, ques); + Result<String> result = funcObj.createFuture(trans, data, "test", "test", nsd, FUTURE_OP.A); + assertTrue(result.status == 20); + + Organization org = Mockito.mock(Organization.class); + Mockito.doReturn(org).when(trans).org(); + Identity iden=Mockito.mock(Identity.class); + try { + Mockito.doReturn(iden).when(org).getIdentity(trans, "test"); + Mockito.doReturn("test").when(iden).mayOwn(); + } catch (OrganizationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + FutureDAO.Data futureData = new FutureDAO.Data(); + data.memo = "test"; + FutureDAO futureDaoObj = Mockito.mock(FutureDAO.class); + Result<FutureDAO.Data> retValFuture = new Result<FutureDAO.Data>(futureData,0,"test",new String[0]); + Mockito.doReturn(retValFuture).when(futureDaoObj).create(Mockito.any(), Mockito.any( FutureDAO.Data.class), Mockito.anyString()); + setQuestionFutureDao(ques, futureDaoObj); + + ApprovalDAO.Data approvalData = new ApprovalDAO.Data(); + data.memo = "test"; + ApprovalDAO approvalDaoObj = Mockito.mock(ApprovalDAO.class); + Result<ApprovalDAO.Data> retValApproval = new Result<ApprovalDAO.Data>(approvalData,0,"test",new String[0]); + Mockito.doReturn(retValApproval).when(approvalDaoObj).create(Mockito.any(), Mockito.any( ApprovalDAO.Data.class)); + setQuestionApprovalDao(ques, approvalDaoObj); + + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + result = funcObj.createFuture(trans, data, "test", "test", nsd, FUTURE_OP.A); + assertTrue(result.status == 0); + + result = funcObj.createFuture(trans, data, "test", "test", null, FUTURE_OP.A); + assertTrue(result.status == 20); + + Mockito.doReturn(retValFail).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + result = funcObj.createFuture(trans, data, "test", "test", nsd, FUTURE_OP.A); + assertTrue(result.status == Result.ERR_NotFound); + + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + try { + Mockito.doReturn(null).when(org).getIdentity(trans, "test"); + } catch (OrganizationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + result = funcObj.createFuture(trans, data, "test", "test", nsd, FUTURE_OP.A); + assertTrue(result.status == Result.ERR_NotFound); + + try { + Mockito.doReturn(iden).when(org).getIdentity(trans, "test"); + } catch (OrganizationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + result = funcObj.createFuture(trans, data, "test", "test", nsd, FUTURE_OP.C); + assertTrue(result.status == 0); + + retValApproval = new Result<ApprovalDAO.Data>(null,1,"test",new String[0]); + Mockito.doReturn(retValApproval).when(approvalDaoObj).create(Mockito.any(), Mockito.any( ApprovalDAO.Data.class)); + result = funcObj.createFuture(trans, data, "test", "test", nsd, FUTURE_OP.A); + assertTrue(result.status == 8); + } + @Test + public void testUbLookup() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Object[] objArr = new Object[10]; + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + List<UserRoleDAO.Data> urDataAl = new ArrayList<>(); + UserRoleDAO.Data urData = new UserRoleDAO.Data(); + urData.ns="test"; + urData.rname="test"; + urData.user="test"; + urData.expires=new Date(); + urDataAl.add(urData); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(urDataAl,0,"test",new String[0]); + Result<List<UserRoleDAO.Data>> retValFail = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).read(trans, objArr); + setQuestionUserRoleDao(ques, userRoleDAO); + + Function funcObj = new Function(trans, ques); + funcObj.urDBLookup.get(trans, objArr); + + Mockito.doReturn(retValFail).when(userRoleDAO).read(trans, objArr); + funcObj.urDBLookup.get(trans, objArr); + } + + @Test + public void testPerformFutureOp() { + 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(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + try { + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + FutureDAO.Data futureDataDaoObj = new FutureDAO.Data(); + futureDataDaoObj.memo="test"; + futureDataDaoObj.target = "test"; + futureDataDaoObj.id = new UUID(10L,10L); + + final List<ApprovalDAO.Data> apprs = new ArrayList<>(); + ApprovalDAO.Data approvalObj = new ApprovalDAO.Data(); + approvalObj.status = "approved"; + approvalObj.type = "owner"; + apprs.add(approvalObj); + Lookup<List<ApprovalDAO.Data>> lookupApprovalObj = new Lookup<List<ApprovalDAO.Data>>() { + @Override + public List<ApprovalDAO.Data> get(AuthzTrans trans, Object ... keys) { + return apprs; + } + }; + + final UserRoleDAO.Data userObj = new UserRoleDAO.Data(); + Lookup<UserRoleDAO.Data> lookupUserObj = new Lookup<UserRoleDAO.Data>() { + @Override + public UserRoleDAO.Data get(AuthzTrans trans, Object ... keys) { + return userObj; + } + }; + + FutureDAO.Data futureData = new FutureDAO.Data(); +// data.memo = "test"; + FutureDAO futureDaoObj = Mockito.mock(FutureDAO.class); + Result<FutureDAO.Data> retValFuture = new Result<FutureDAO.Data>(futureData,0,"test",new String[0]); + Mockito.doReturn(retValFuture).when(futureDaoObj).delete(Mockito.any(), Mockito.any( FutureDAO.Data.class), Mockito.anyBoolean()); + setQuestionFutureDao(ques, futureDaoObj); + + CachedUserRoleDAO userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); // List<NsDAO.Data> dataAl = new ArrayList<NsDAO.Data>(); // NsDAO.Data dataObj = new NsDAO.Data(); // dataObj.type=1; // dataAl.add(dataObj); -// Result<List<NsDAO.Data>> retVal = new Result<List<NsDAO.Data>>(dataAl,0,"test",new String[0]); -// Mockito.doReturn(retVal).when(nsDaoObj).read(Mockito.any(), Mockito.anyString()); -// setQuestion(ques, nsDaoObj); + Result<List<UserRoleDAO.Data>> retVal = new Result<List<UserRoleDAO.Data>>(null,1,"test",new String[0]); + Mockito.doReturn(retVal).when(userRoleDAO).readByRole(Mockito.any(), Mockito.anyString()); + setQuestionUserRoleDao(ques, userRoleDAO); // // Result<NsDAO.Data> retVal2 = new Result<NsDAO.Data>(null,0,"test",new String[0]); // Mockito.doReturn(retVal2).when(ques).mayUser(trans, null,retVal.value.get(0), Access.write); // -// Function funcObj = new Function(trans, ques); -// Result<Void> result = funcObj.deleteNS(trans, "test"); -// assertTrue(result.status == 1); + Function funcObj = new Function(trans, ques); + Result<Function.OP_STATUS> result = funcObj.performFutureOp(trans, FUTURE_OP.A, futureDataDaoObj, lookupApprovalObj, lookupUserObj); + assertTrue(result.status == 0); + + approvalObj.status = "approved"; + approvalObj.type = "supervisor"; + result = funcObj.performFutureOp(trans, FUTURE_OP.A, futureDataDaoObj, lookupApprovalObj, lookupUserObj); + assertTrue(result.status == 0); + + approvalObj.status = "approved"; + approvalObj.type = ""; + result = funcObj.performFutureOp(trans, FUTURE_OP.A, futureDataDaoObj, lookupApprovalObj, lookupUserObj); + assertTrue(result.status == 0); + + approvalObj.status = "pending"; + approvalObj.type = "supervisor"; + result = funcObj.performFutureOp(trans, FUTURE_OP.A, futureDataDaoObj, lookupApprovalObj, lookupUserObj); + assertTrue(result.status == 0); + + approvalObj.status = "pending"; + approvalObj.type = "owner"; + result = funcObj.performFutureOp(trans, FUTURE_OP.A, futureDataDaoObj, lookupApprovalObj, lookupUserObj); + assertTrue(result.status == 0); + + approvalObj.status = "pending"; + approvalObj.type = ""; + result = funcObj.performFutureOp(trans, FUTURE_OP.A, futureDataDaoObj, lookupApprovalObj, lookupUserObj); + assertTrue(result.status == 0); + + approvalObj.status = "denied"; + approvalObj.type = ""; + result = funcObj.performFutureOp(trans, FUTURE_OP.A, futureDataDaoObj, lookupApprovalObj, lookupUserObj); + assertTrue(result.status == 0); + + retValFuture = new Result<FutureDAO.Data>(futureData,1,"test",new String[0]); + Mockito.doReturn(retValFuture).when(futureDaoObj).delete(Mockito.any(), Mockito.any( FutureDAO.Data.class), Mockito.anyBoolean()); + result = funcObj.performFutureOp(trans, FUTURE_OP.A, futureDataDaoObj, lookupApprovalObj, lookupUserObj); + System.out.println(result); + assertTrue(result.status == 0); // -// } + } } |