From 23dfff56554c777cf16b0b00bde068bfe8994ecd Mon Sep 17 00:00:00 2001 From: Instrumental Date: Thu, 3 Jan 2019 15:15:26 -0600 Subject: Correct JUnit Dirs Issue-ID: AAF-694 Change-Id: Ib1c896d849bfa80768ba17c2d071a1c05d2ac488 Signed-off-by: Instrumental --- .../onap/aaf/auth/dao/JU_CachedPermDAOTest.java | 166 --------- .../onap/aaf/auth/dao/JU_CachedUserRoleDAO.java | 180 ---------- .../org/onap/aaf/auth/dao/JU_CassExecutor.java | 154 --------- .../java/org/onap/aaf/auth/dao/JU_PermLookup.java | 383 --------------------- .../aaf/auth/dao/cached/JU_CachedPermDAOTest.java | 166 +++++++++ .../aaf/auth/dao/cached/JU_CachedUserRoleDAO.java | 180 ++++++++++ .../org/onap/aaf/auth/dao/hl/JU_CassExecutor.java | 154 +++++++++ .../org/onap/aaf/auth/dao/hl/JU_PermLookup.java | 383 +++++++++++++++++++++ 8 files changed, 883 insertions(+), 883 deletions(-) delete mode 100644 auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedPermDAOTest.java delete mode 100644 auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedUserRoleDAO.java delete mode 100644 auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CassExecutor.java delete mode 100644 auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_PermLookup.java create mode 100644 auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedPermDAOTest.java create mode 100644 auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedUserRoleDAO.java create mode 100644 auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_CassExecutor.java create mode 100644 auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_PermLookup.java diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedPermDAOTest.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedPermDAOTest.java deleted file mode 100644 index 66c32a7b..00000000 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedPermDAOTest.java +++ /dev/null @@ -1,166 +0,0 @@ -/** - * ============LICENSE_START==================================================== - * org.onap.aaf - * =========================================================================== - * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. - * =========================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END==================================================== - * - */ -package org.onap.aaf.auth.dao.cached; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; - -import java.util.List; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.onap.aaf.auth.dao.CIDAO; -import org.onap.aaf.auth.dao.cass.PermDAO; -import org.onap.aaf.auth.dao.cass.PermDAO.Data; -import org.onap.aaf.auth.dao.cass.RoleDAO; -import org.onap.aaf.auth.dao.cass.Status; -import org.onap.aaf.auth.env.AuthzTrans; -import org.onap.aaf.auth.env.AuthzTransImpl; -import org.onap.aaf.auth.layer.Result; -import org.onap.aaf.misc.env.LogTarget; - -public class JU_CachedPermDAOTest { - - @Mock - private CIDAO info; - @Mock - private PermDAO dao; - - @Mock - RoleDAO.Data role; - - @Mock - private PermDAO.Data perm; - - @Mock - private AuthzTrans trans; - @Mock - private Result> value; - - @Before - public void setUp() throws Exception { - initMocks(this); - - when(dao.readNS(trans, "ns")).thenReturn(value); - when(trans.debug()).thenReturn(new LogTarget() { - - @Override - public void printf(String fmt, Object... vars) {} - - @Override - public void log(Throwable e, Object... msgs) { - e.getMessage(); - e.printStackTrace(); - msgs.toString(); - - } - - @Override - public void log(Object... msgs) { - } - - @Override - public boolean isLoggable() { - - return true; - } - }); - } - - @Test - public void testReadNS() { - when(value.isOKhasData()).thenReturn(true); - when(value.isOK()).thenReturn(false); - CachedPermDAO ccDao = new CachedPermDAO(dao, info, 100l); - - Result> result = ccDao.readNS(trans, "ns"); - - assertEquals(result, value); - - when(value.isOKhasData()).thenReturn(false); - - result = ccDao.readNS(trans, "ns"); - - assertEquals(result.status, Status.ERR_PermissionNotFound); - - ccDao.readChildren(trans, "ns", "type"); - - verify(dao).readChildren(trans, "ns", "type"); - } - - @Test - public void testReadByTypeSuccess() { - CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result> retVal1 = new Result>(null,1,"test4",new String[0]); - Mockito.doReturn(retVal1).when(dao).readByType(trans, "test4",""); - Result> retVal = roleDaoObj.readByType(trans, "test4",""); -// System.out.println(retVal.status); - //retVal.status = 0; - assertEquals("1", Integer.toString(retVal.status)); - } - - @Test - public void testReadByTypeFailure() { - CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result> retVal1 = new Result>(null,0,"test3123",new String[0]); - Mockito.doReturn(retVal1).when(dao).readByType(trans, "test3",""); - Result> retVal = roleDaoObj.readByType(trans, "test3",""); - //System.out.println(retVal.status); - assertEquals("23", Integer.toString(retVal.status)); - } - - @Test - public void testAddRole() { - CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10); - Result retVal1 = new Result(null,0,"testAddRole",new String[0]); - Mockito.doReturn(retVal1).when(info).touch(trans, null,null); - Mockito.doReturn(retVal1).when(dao).addRole(trans, perm,null); - Result retVal = roleDaoObj.addRole(trans, perm, role); -// System.out.println("ret value is::"+retVal); - assertEquals("testAddRole", retVal.toString()); - } - - @Test - public void testDelRole() { - CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10); - Result retVal1 = new Result(null,0,"testAddRole",new String[0]); - Mockito.doReturn(retVal1).when(info).touch(trans, null,null); - Mockito.doReturn(retVal1).when(dao).delRole(trans, perm,null); - Result retVal = roleDaoObj.delRole(trans, perm, role); -// System.out.println(retVal); - assertEquals("testAddRole", retVal.toString()); - } - - @Test - public void testAddDescription() { - CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result retVal1 = new Result(null,0,"test1",new String[0]); - Mockito.doReturn(retVal1).when(dao).addDescription(trans, "","","","",""); - Result retVal = roleDaoObj.addDescription(trans, "", "","","",""); - //System.out.println(retVal.status); - assertEquals("0", Integer.toString(retVal.status)); - } - -} diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedUserRoleDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedUserRoleDAO.java deleted file mode 100644 index 8dcf8e44..00000000 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CachedUserRoleDAO.java +++ /dev/null @@ -1,180 +0,0 @@ -/** - * ============LICENSE_START==================================================== - * org.onap.aaf - * =========================================================================== - * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. - * =========================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END==================================================== - * - */ -package org.onap.aaf.auth.dao.cached; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; - -import java.util.List; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.onap.aaf.auth.dao.CIDAO; -import org.onap.aaf.auth.dao.cass.PermDAO; -import org.onap.aaf.auth.dao.cass.RoleDAO; -import org.onap.aaf.auth.dao.cass.UserRoleDAO; -import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; -import org.onap.aaf.auth.env.AuthzEnv; -import org.onap.aaf.auth.env.AuthzTrans; -import org.onap.aaf.auth.env.AuthzTransImpl; -import org.onap.aaf.auth.layer.Result; -import org.onap.aaf.cadi.principal.TaggedPrincipal; -import org.onap.aaf.misc.env.LogTarget; -import org.onap.aaf.misc.env.Slot; -import org.powermock.modules.junit4.PowerMockRunner; - -@RunWith(PowerMockRunner.class) -public class JU_CachedUserRoleDAO { - - @Mock - UserRoleDAO dao; - - @Mock - CIDAO info; - - @Mock - AuthzTransImpl trans; - - @Mock - RoleDAO.Data data; - - @Mock - PermDAO.Data permData; - - @Before - public void setUp() throws Exception { - initMocks(this); - when(trans.debug()).thenReturn(new LogTarget() { - - @Override - public void printf(String fmt, Object... vars) {} - - @Override - public void log(Throwable e, Object... msgs) { - e.getMessage(); - e.printStackTrace(); - msgs.toString(); - - } - - @Override - public void log(Object... msgs) { - } - - @Override - public boolean isLoggable() { - - return true; - } - }); - } - - private class TaggedPrincipalStub extends TaggedPrincipal { - String name="TaggedPrincipalStub"; - public TaggedPrincipalStub() { super(); } - public TaggedPrincipalStub(final TagLookup tl) { super(tl); } - @Override public String getName() { return name; } - @Override public String tag() { return null; } - } - - @Test - public void testReadName() { - CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L); - Result> retVal1 = new Result>(null,0,"test4",new String[0]); - Mockito.doReturn(retVal1).when(dao).readByUser(trans, "test4"); -// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); - Result> retVal = roleDaoObj.readByUser(trans, "test4"); - //System.out.println(retVal.status); - //retVal.status = 0; - assertEquals("25", Integer.toString(retVal.status)); - } - - @Test - public void testReadNameUser() { - CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L); - Result> retVal1 = new Result>(null,1,"TaggedPrincipalStub",new String[0]); - AuthzEnv env = Mockito.mock(AuthzEnv.class); - AuthzTransImpl transTemp = new AuthzTransImpl(env) { - @Override - public T get(Slot slot, T deflt) { - Object o=null; - return (T)o; - } - - }; - transTemp.setUser(new TaggedPrincipalStub()); - Mockito.doReturn(retVal1).when(info).touch(trans, null,null); - Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub"); - roleDaoObj.invalidate("TaggedPrincipalStub"); - Result> retVal = roleDaoObj.readByUser(transTemp, "TaggedPrincipalStub"); -// System.out.println(retVal.status); - assertEquals("1", Integer.toString(retVal.status)); - } - - @Test - public void testReadByRoleSuccess() { - CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);// - Result> retVal1 = new Result>(null,1,"test",new String[0]); - Mockito.doReturn(retVal1).when(dao).readByRole(trans, ""); - roleDaoObj.invalidate(""); - Result> retVal = roleDaoObj.readByRole(trans, ""); - //System.out.println(retVal.status); - assertEquals("1", Integer.toString(retVal.status)); - } - @Test - public void testReadByRoleFailure() { - CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);// - Result> retVal1 = new Result>(null,0,"test1",new String[0]); - Mockito.doReturn(retVal1).when(dao).readByRole(trans, ""); - roleDaoObj.invalidate(""); - Result> retVal = roleDaoObj.readByRole(trans, ""); - //System.out.println(retVal.status); - assertEquals("25", Integer.toString(retVal.status)); - } - - @Test - public void testReadUserInRole() { - CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// - Result> retVal1 = new Result>(null,0,"TaggedPrincipalStub",new String[0]); - AuthzEnv env = Mockito.mock(AuthzEnv.class); - AuthzTransImpl transTemp = new AuthzTransImpl(env) { - @Override - public T get(Slot slot, T deflt) { - Object o=null; - return (T)o; - } - - }; - transTemp.setUser(new TaggedPrincipalStub()); - Mockito.doReturn(retVal1).when(info).touch(trans, null,null); - Mockito.doReturn(retVal1).when(dao).readByUserRole(transTemp, "",""); - Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub"); - Result> retVal = roleDaoObj.readUserInRole(transTemp, "TaggedPrincipalStub",""); - //System.out.println(retVal.status); - assertEquals("25", Integer.toString(retVal.status)); - } - - -} \ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CassExecutor.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CassExecutor.java deleted file mode 100644 index 5dd33c7e..00000000 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_CassExecutor.java +++ /dev/null @@ -1,154 +0,0 @@ -/** - * ============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.hl; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.mockito.MockitoAnnotations.initMocks; - -import java.util.Properties; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.runners.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; -import org.onap.aaf.auth.common.Define; -import org.onap.aaf.auth.dao.cass.NsSplit; -import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; -import org.onap.aaf.auth.env.AuthzTransImpl; -import org.onap.aaf.auth.layer.Result; -import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.CadiException; - - -@RunWith(MockitoJUnitRunner.class) -public class JU_CassExecutor { - - - - @Mock - AuthzTransImpl trans; - - @Mock - Question q; - - @Mock - Access access; - - Function f; - - @Before - public void setUp() throws Exception { - initMocks(this); - try { - Mockito.doReturn("0.0").when(access).getProperty("aaf_root_ns","org.osaaf.aaf"); - Mockito.doReturn(new Properties()).when(access).getProperties(); - Define.set(access); - } catch (CadiException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - f =new Function(trans, q); - } - - @Test - public void testHasPermission() { - - CassExecutor cassExecutorObj =new CassExecutor(trans, f); - Mockito.doReturn(false).when(q).isGranted(trans, "","","","",""); - boolean retVal = cassExecutorObj.hasPermission("", "", "", "", ""); -// System.out.println(retVal); - assertFalse(retVal); - } - - @Test - public void testInRole() { - - CassExecutor cassExecutorObj =new CassExecutor(trans, f); - Result retVal1 = new Result(null,1,"",new String[0]); - Mockito.doReturn(retVal1).when(q).deriveNsSplit(trans, "test"); - - boolean retVal = cassExecutorObj.inRole("test"); -// System.out.println(retVal); - assertFalse(retVal); - } - - @Test - public void testNamespace() { - f =new Function(trans, q); - CassExecutor cassExecutorObj =new CassExecutor(trans, f); - Result retVal1 = new Result(null,1,"",new String[0]); - Mockito.doReturn(retVal1).when(q).validNSOfDomain(trans, null); - - String retVal=""; - try { - retVal = cassExecutorObj.namespace(); - } catch (Exception e) { - System.out.println(e.getMessage()); - assertEquals("33", e.getMessage()); - } - System.out.println(retVal); -// assertFalse(retVal); - } - - @Test - public void testId() { - Mockito.doReturn("").when(trans).user(); - CassExecutor cassExecutorObj =new CassExecutor(trans, f); - String retVal = cassExecutorObj.id(); - assertEquals("", retVal); - } - - @Test - public void testNamespaceSuccess() { - Mockito.doAnswer(new Answer() { - private int count = 0; - - public Object answer(InvocationOnMock invocation) { - if (count++ == 1) - return "test@test.com"; - - return null; - } - }).when(trans).user(); - f =new Function(trans, q); - CassExecutor cassExecutorObj =new CassExecutor(trans, f); - Result retVal1 = new Result(null,0,"",new String[0]); - Mockito.doReturn(retVal1).when(q).validNSOfDomain(trans, null); - - - String retVal=""; - try { - retVal = cassExecutorObj.namespace(); - } catch (Exception e) { - e.printStackTrace(); - System.out.println(e.getMessage()); -// assertNull( e.getMessage()); - } -// System.out.println(retVal); -// assertFalse(retVal); - } - -} \ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_PermLookup.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_PermLookup.java deleted file mode 100644 index e63d4b52..00000000 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/JU_PermLookup.java +++ /dev/null @@ -1,383 +0,0 @@ -/** - * ============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.hl; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import org.onap.aaf.auth.common.Define; -import org.onap.aaf.auth.dao.cached.CachedRoleDAO; -import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO; -import org.onap.aaf.auth.dao.cass.PermDAO.Data; -import org.onap.aaf.auth.dao.cass.RoleDAO; -import org.onap.aaf.auth.dao.cass.UserRoleDAO; -import org.onap.aaf.auth.env.AuthzTrans; -import org.onap.aaf.auth.layer.Result; -import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.misc.env.LogTarget; - - -@RunWith(MockitoJUnitRunner.class) -public class JU_PermLookup { - - - - @Mock - AuthzTrans trans; - - @Mock - Question q; - - @Mock - Access access; - - Function f; - - @Before - public void setUp() throws Exception { - initMocks(this); - try { - Mockito.doReturn("0.0").when(access).getProperty("aaf_root_ns","org.osaaf.aaf"); - Mockito.doReturn(new Properties()).when(access).getProperties(); - Define.set(access); - - when(trans.error()).thenReturn(new LogTarget() { - - @Override - public void printf(String fmt, Object... vars) {} - - @Override - public void log(Throwable e, Object... msgs) { - e.getMessage(); - e.printStackTrace(); - msgs.toString(); - - } - - @Override - public void log(Object... msgs) { - } - - @Override - public boolean isLoggable() { - - return true; - } - }); - } catch (CadiException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - f =new Function(trans, q); - } - - - @Test - public void testPerm() { - - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - -// System.out.println(cassExecutorObj); -// assertFalse(retVal); - } - - @Test - public void testGetUserRole() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - retVal1.value = new ArrayList(); - UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); - - dataObj.expires = new Date(); - - retVal1.value.add(dataObj); - Mockito.doReturn(true).when(retVal1).isOKhasData(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - Result> userRoles = cassExecutorObj.getUserRoles(); - - //System.out.println(""+userRoles.status); - assertEquals(24,userRoles.status); - } - - @Test - public void testGetUserRolesFirstIf() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - retVal1.value = new ArrayList(); - - Mockito.doReturn(false).when(retVal1).isOKhasData(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - Result> userRoles = cassExecutorObj.getUserRoles(); - -// System.out.println("output is"+userRoles.status); - assertEquals(0,userRoles.status); - } - - @Test - public void testGetUserRolesSecondIf() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - retVal1.value = new ArrayList(); - UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); - - Date dt = new Date(); - Calendar c = Calendar.getInstance(); - c.setTime(dt); - c.add(Calendar.DATE, 1); - dataObj.expires = c.getTime(); - - retVal1.value.add(dataObj); - Mockito.doReturn(true).when(retVal1).isOKhasData(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - Result> userRoles = cassExecutorObj.getUserRoles(); - - //System.out.println(userRoles.status); - assertEquals("Success",userRoles.details); - Result> userRoles1 = cassExecutorObj.getUserRoles(); - - //System.out.println(userRoles1.status); - assertEquals(0, userRoles1.status); - } - - @Test - public void testGetRole() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - retVal1.value = new ArrayList(); - UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); - - dataObj.expires = new Date(); - - retVal1.value.add(dataObj); - Mockito.doReturn(false).when(retVal1).isOKhasData(); - Mockito.doReturn(true).when(retVal1).isOK(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - Result> userRoles = cassExecutorObj.getRoles(); - - //System.out.println(""+userRoles.status); - assertEquals(4,userRoles.status); - } - - @Test - public void testGetRoleFirstIf() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - q.roleDAO = Mockito.mock(CachedRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - retVal1.value = new ArrayList(); - UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); - - dataObj.expires = new Date(); - dataObj.ns=""; - dataObj.rname=""; - - retVal1.value.add(dataObj); - Mockito.doReturn(false).when(retVal1).isOKhasData(); - Mockito.doReturn(false).when(retVal1).isOK(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - Mockito.doReturn(retVal1).when(q.roleDAO).read(trans,"",""); - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - Result> userRoles = cassExecutorObj.getRoles(); - -// System.out.println(""+userRoles.status); - assertEquals(0,userRoles.status); - } - - @Test - public void testGetRoleSecondIf() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - q.roleDAO = Mockito.mock(CachedRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - retVal1.value = new ArrayList(); - UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); - - dataObj.expires = new Date(); - dataObj.ns=""; - dataObj.rname=""; - - retVal1.value.add(dataObj); - Mockito.doReturn(false).when(retVal1).isOKhasData(); - Mockito.doReturn(true).when(retVal1).isOK(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - Mockito.doReturn(retVal1).when(q.roleDAO).read(trans,"",""); - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - Result> userRoles = cassExecutorObj.getRoles(); - userRoles = cassExecutorObj.getRoles(); - -// System.out.println(""+userRoles.status); - assertEquals(0,userRoles.status); - } - @Test - public void testGetPerms() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - retVal1.value = new ArrayList(); - Mockito.doReturn(false).when(retVal1).isOKhasData(); - Mockito.doReturn(true).when(retVal1).isOK(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - Result> userRoles = cassExecutorObj.getPermNames(); - userRoles = cassExecutorObj.getPermNames(); - - //System.out.println(""+userRoles.status); - assertEquals(0,userRoles.status); - } - @Test - public void testGetPermsRrldOk() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - retVal1.value = new ArrayList(); - UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); - - dataObj.expires = new Date(); - - retVal1.value.add(dataObj); - Mockito.doReturn(false).when(retVal1).isOKhasData(); - Mockito.doReturn(true).when(retVal1).isOK(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); - Result> userRoles = cassExecutorObj.getPermNames(); - - //System.out.println(""+userRoles.status); - assertEquals(4,userRoles.status); - } - - - @Test - public void testGetPerm() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - q.roleDAO = Mockito.mock(CachedRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - Result> retVal2 = Mockito.mock(Result.class); - - retVal1.value = new ArrayList(); - retVal2.value = new ArrayList(); - UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); - - Date dt = new Date(); - Calendar c = Calendar.getInstance(); - c.setTime(dt); - c.add(Calendar.DATE, 1); - dataObj.expires = c.getTime(); - dataObj.ns = ""; - dataObj.rname=""; - - RoleDAO.Data dataObj1 = Mockito.mock( RoleDAO.Data.class); - Set permSet = new HashSet(); - permSet.add("test"); - Mockito.doReturn(permSet).when(dataObj1).perms(false); - - dt = new Date(); - c = Calendar.getInstance(); - c.setTime(dt); - c.add(Calendar.DATE, 1); - dataObj1.ns = "test"; - dataObj1.perms = permSet; - - retVal1.value.add(dataObj); - retVal2.value.add(dataObj1); - Mockito.doReturn(true).when(retVal1).isOKhasData(); - Mockito.doReturn(true).when(retVal1).isOK(); - Mockito.doReturn(true).when(retVal2).isOK(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - Mockito.doReturn(retVal2).when(q.roleDAO).read(trans,"",""); - - - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); -// -// Mockito.doReturn(retVal2).when(cassExecutorObj).getPermNames(); - Result> userRoles = cassExecutorObj.getPerms(true); -// userRoles = cassExecutorObj.getPerms(false); - -// System.out.println(""+userRoles.status); - assertEquals(0,userRoles.status); - } - - @Test - public void testGetPermFalse() { - q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); - q.roleDAO = Mockito.mock(CachedRoleDAO.class); - Result> retVal1 = Mockito.mock(Result.class); - Result> retVal2 = Mockito.mock(Result.class); - - retVal1.value = new ArrayList(); - retVal2.value = new ArrayList(); - UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); - - Date dt = new Date(); - Calendar c = Calendar.getInstance(); - c.setTime(dt); - c.add(Calendar.DATE, 1); - dataObj.expires = c.getTime(); - dataObj.ns = ""; - dataObj.rname=""; - - RoleDAO.Data dataObj1 = Mockito.mock( RoleDAO.Data.class); - Set permSet = new HashSet(); - permSet.add("test"); - Mockito.doReturn(permSet).when(dataObj1).perms(false); - - dt = new Date(); - c = Calendar.getInstance(); - c.setTime(dt); - c.add(Calendar.DATE, 1); - dataObj1.ns = "test"; - dataObj1.perms = permSet; - - retVal1.value.add(dataObj); - retVal2.value.add(dataObj1); - Mockito.doReturn(true).when(retVal1).isOKhasData(); - Mockito.doReturn(true).when(retVal1).isOK(); - Mockito.doReturn(true).when(retVal2).isOK(); - Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); - Mockito.doReturn(retVal2).when(q.roleDAO).read(trans,"",""); - - - PermLookup cassExecutorObj =PermLookup.get(trans, q,""); -// -// Mockito.doReturn(retVal2).when(cassExecutorObj).getPermNames(); - Result> userRoles = cassExecutorObj.getPerms(false); - userRoles = cassExecutorObj.getPerms(false); - -// System.out.println(""+userRoles.status); - assertEquals(0,userRoles.status); - } - -} \ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedPermDAOTest.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedPermDAOTest.java new file mode 100644 index 00000000..66c32a7b --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedPermDAOTest.java @@ -0,0 +1,166 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.dao.cached; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.CIDAO; +import org.onap.aaf.auth.dao.cass.PermDAO; +import org.onap.aaf.auth.dao.cass.PermDAO.Data; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.Status; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.env.AuthzTransImpl; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.misc.env.LogTarget; + +public class JU_CachedPermDAOTest { + + @Mock + private CIDAO info; + @Mock + private PermDAO dao; + + @Mock + RoleDAO.Data role; + + @Mock + private PermDAO.Data perm; + + @Mock + private AuthzTrans trans; + @Mock + private Result> value; + + @Before + public void setUp() throws Exception { + initMocks(this); + + when(dao.readNS(trans, "ns")).thenReturn(value); + when(trans.debug()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + } + + @Test + public void testReadNS() { + when(value.isOKhasData()).thenReturn(true); + when(value.isOK()).thenReturn(false); + CachedPermDAO ccDao = new CachedPermDAO(dao, info, 100l); + + Result> result = ccDao.readNS(trans, "ns"); + + assertEquals(result, value); + + when(value.isOKhasData()).thenReturn(false); + + result = ccDao.readNS(trans, "ns"); + + assertEquals(result.status, Status.ERR_PermissionNotFound); + + ccDao.readChildren(trans, "ns", "type"); + + verify(dao).readChildren(trans, "ns", "type"); + } + + @Test + public void testReadByTypeSuccess() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result> retVal1 = new Result>(null,1,"test4",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByType(trans, "test4",""); + Result> retVal = roleDaoObj.readByType(trans, "test4",""); +// System.out.println(retVal.status); + //retVal.status = 0; + assertEquals("1", Integer.toString(retVal.status)); + } + + @Test + public void testReadByTypeFailure() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result> retVal1 = new Result>(null,0,"test3123",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByType(trans, "test3",""); + Result> retVal = roleDaoObj.readByType(trans, "test3",""); + //System.out.println(retVal.status); + assertEquals("23", Integer.toString(retVal.status)); + } + + @Test + public void testAddRole() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10); + Result retVal1 = new Result(null,0,"testAddRole",new String[0]); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).addRole(trans, perm,null); + Result retVal = roleDaoObj.addRole(trans, perm, role); +// System.out.println("ret value is::"+retVal); + assertEquals("testAddRole", retVal.toString()); + } + + @Test + public void testDelRole() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10); + Result retVal1 = new Result(null,0,"testAddRole",new String[0]); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).delRole(trans, perm,null); + Result retVal = roleDaoObj.delRole(trans, perm, role); +// System.out.println(retVal); + assertEquals("testAddRole", retVal.toString()); + } + + @Test + public void testAddDescription() { + CachedPermDAO roleDaoObj =new CachedPermDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result retVal1 = new Result(null,0,"test1",new String[0]); + Mockito.doReturn(retVal1).when(dao).addDescription(trans, "","","","",""); + Result retVal = roleDaoObj.addDescription(trans, "", "","","",""); + //System.out.println(retVal.status); + assertEquals("0", Integer.toString(retVal.status)); + } + +} diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedUserRoleDAO.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedUserRoleDAO.java new file mode 100644 index 00000000..8dcf8e44 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/cached/JU_CachedUserRoleDAO.java @@ -0,0 +1,180 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.dao.cached; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.dao.CIDAO; +import org.onap.aaf.auth.dao.cass.PermDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.env.AuthzEnv; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.env.AuthzTransImpl; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.principal.TaggedPrincipal; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.Slot; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class JU_CachedUserRoleDAO { + + @Mock + UserRoleDAO dao; + + @Mock + CIDAO info; + + @Mock + AuthzTransImpl trans; + + @Mock + RoleDAO.Data data; + + @Mock + PermDAO.Data permData; + + @Before + public void setUp() throws Exception { + initMocks(this); + when(trans.debug()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + } + + private class TaggedPrincipalStub extends TaggedPrincipal { + String name="TaggedPrincipalStub"; + public TaggedPrincipalStub() { super(); } + public TaggedPrincipalStub(final TagLookup tl) { super(tl); } + @Override public String getName() { return name; } + @Override public String tag() { return null; } + } + + @Test + public void testReadName() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L); + Result> retVal1 = new Result>(null,0,"test4",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByUser(trans, "test4"); +// Mockito.when(roleDaoObj.get(Mockito.any(), Mockito.any(String.class), Mockito.any())).thenReturn(retVal1); + Result> retVal = roleDaoObj.readByUser(trans, "test4"); + //System.out.println(retVal.status); + //retVal.status = 0; + assertEquals("25", Integer.toString(retVal.status)); + } + + @Test + public void testReadNameUser() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10L); + Result> retVal1 = new Result>(null,1,"TaggedPrincipalStub",new String[0]); + AuthzEnv env = Mockito.mock(AuthzEnv.class); + AuthzTransImpl transTemp = new AuthzTransImpl(env) { + @Override + public T get(Slot slot, T deflt) { + Object o=null; + return (T)o; + } + + }; + transTemp.setUser(new TaggedPrincipalStub()); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub"); + roleDaoObj.invalidate("TaggedPrincipalStub"); + Result> retVal = roleDaoObj.readByUser(transTemp, "TaggedPrincipalStub"); +// System.out.println(retVal.status); + assertEquals("1", Integer.toString(retVal.status)); + } + + @Test + public void testReadByRoleSuccess() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);// + Result> retVal1 = new Result>(null,1,"test",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByRole(trans, ""); + roleDaoObj.invalidate(""); + Result> retVal = roleDaoObj.readByRole(trans, ""); + //System.out.println(retVal.status); + assertEquals("1", Integer.toString(retVal.status)); + } + @Test + public void testReadByRoleFailure() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 0);//Mockito.mock(CachedRoleDAO.class);// + Result> retVal1 = new Result>(null,0,"test1",new String[0]); + Mockito.doReturn(retVal1).when(dao).readByRole(trans, ""); + roleDaoObj.invalidate(""); + Result> retVal = roleDaoObj.readByRole(trans, ""); + //System.out.println(retVal.status); + assertEquals("25", Integer.toString(retVal.status)); + } + + @Test + public void testReadUserInRole() { + CachedUserRoleDAO roleDaoObj =new CachedUserRoleDAO(dao,info, 10);//Mockito.mock(CachedRoleDAO.class);// + Result> retVal1 = new Result>(null,0,"TaggedPrincipalStub",new String[0]); + AuthzEnv env = Mockito.mock(AuthzEnv.class); + AuthzTransImpl transTemp = new AuthzTransImpl(env) { + @Override + public T get(Slot slot, T deflt) { + Object o=null; + return (T)o; + } + + }; + transTemp.setUser(new TaggedPrincipalStub()); + Mockito.doReturn(retVal1).when(info).touch(trans, null,null); + Mockito.doReturn(retVal1).when(dao).readByUserRole(transTemp, "",""); + Mockito.doReturn(retVal1).when(dao).readByUser(transTemp, "TaggedPrincipalStub"); + Result> retVal = roleDaoObj.readUserInRole(transTemp, "TaggedPrincipalStub",""); + //System.out.println(retVal.status); + assertEquals("25", Integer.toString(retVal.status)); + } + + +} \ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_CassExecutor.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_CassExecutor.java new file mode 100644 index 00000000..5dd33c7e --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_CassExecutor.java @@ -0,0 +1,154 @@ +/** + * ============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.hl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cass.NsSplit; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.env.AuthzTransImpl; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CadiException; + + +@RunWith(MockitoJUnitRunner.class) +public class JU_CassExecutor { + + + + @Mock + AuthzTransImpl trans; + + @Mock + Question q; + + @Mock + Access access; + + Function f; + + @Before + public void setUp() throws Exception { + initMocks(this); + try { + Mockito.doReturn("0.0").when(access).getProperty("aaf_root_ns","org.osaaf.aaf"); + Mockito.doReturn(new Properties()).when(access).getProperties(); + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + f =new Function(trans, q); + } + + @Test + public void testHasPermission() { + + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + Mockito.doReturn(false).when(q).isGranted(trans, "","","","",""); + boolean retVal = cassExecutorObj.hasPermission("", "", "", "", ""); +// System.out.println(retVal); + assertFalse(retVal); + } + + @Test + public void testInRole() { + + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + Result retVal1 = new Result(null,1,"",new String[0]); + Mockito.doReturn(retVal1).when(q).deriveNsSplit(trans, "test"); + + boolean retVal = cassExecutorObj.inRole("test"); +// System.out.println(retVal); + assertFalse(retVal); + } + + @Test + public void testNamespace() { + f =new Function(trans, q); + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + Result retVal1 = new Result(null,1,"",new String[0]); + Mockito.doReturn(retVal1).when(q).validNSOfDomain(trans, null); + + String retVal=""; + try { + retVal = cassExecutorObj.namespace(); + } catch (Exception e) { + System.out.println(e.getMessage()); + assertEquals("33", e.getMessage()); + } + System.out.println(retVal); +// assertFalse(retVal); + } + + @Test + public void testId() { + Mockito.doReturn("").when(trans).user(); + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + String retVal = cassExecutorObj.id(); + assertEquals("", retVal); + } + + @Test + public void testNamespaceSuccess() { + Mockito.doAnswer(new Answer() { + private int count = 0; + + public Object answer(InvocationOnMock invocation) { + if (count++ == 1) + return "test@test.com"; + + return null; + } + }).when(trans).user(); + f =new Function(trans, q); + CassExecutor cassExecutorObj =new CassExecutor(trans, f); + Result retVal1 = new Result(null,0,"",new String[0]); + Mockito.doReturn(retVal1).when(q).validNSOfDomain(trans, null); + + + String retVal=""; + try { + retVal = cassExecutorObj.namespace(); + } catch (Exception e) { + e.printStackTrace(); + System.out.println(e.getMessage()); +// assertNull( e.getMessage()); + } +// System.out.println(retVal); +// assertFalse(retVal); + } + +} \ No newline at end of file diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_PermLookup.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_PermLookup.java new file mode 100644 index 00000000..e63d4b52 --- /dev/null +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/dao/hl/JU_PermLookup.java @@ -0,0 +1,383 @@ +/** + * ============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.hl; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cached.CachedRoleDAO; +import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO; +import org.onap.aaf.auth.dao.cass.PermDAO.Data; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.misc.env.LogTarget; + + +@RunWith(MockitoJUnitRunner.class) +public class JU_PermLookup { + + + + @Mock + AuthzTrans trans; + + @Mock + Question q; + + @Mock + Access access; + + Function f; + + @Before + public void setUp() throws Exception { + initMocks(this); + try { + Mockito.doReturn("0.0").when(access).getProperty("aaf_root_ns","org.osaaf.aaf"); + Mockito.doReturn(new Properties()).when(access).getProperties(); + Define.set(access); + + when(trans.error()).thenReturn(new LogTarget() { + + @Override + public void printf(String fmt, Object... vars) {} + + @Override + public void log(Throwable e, Object... msgs) { + e.getMessage(); + e.printStackTrace(); + msgs.toString(); + + } + + @Override + public void log(Object... msgs) { + } + + @Override + public boolean isLoggable() { + + return true; + } + }); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + f =new Function(trans, q); + } + + + @Test + public void testPerm() { + + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + +// System.out.println(cassExecutorObj); +// assertFalse(retVal); + } + + @Test + public void testGetUserRole() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + + retVal1.value.add(dataObj); + Mockito.doReturn(true).when(retVal1).isOKhasData(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result> userRoles = cassExecutorObj.getUserRoles(); + + //System.out.println(""+userRoles.status); + assertEquals(24,userRoles.status); + } + + @Test + public void testGetUserRolesFirstIf() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList(); + + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result> userRoles = cassExecutorObj.getUserRoles(); + +// System.out.println("output is"+userRoles.status); + assertEquals(0,userRoles.status); + } + + @Test + public void testGetUserRolesSecondIf() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + Date dt = new Date(); + Calendar c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj.expires = c.getTime(); + + retVal1.value.add(dataObj); + Mockito.doReturn(true).when(retVal1).isOKhasData(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result> userRoles = cassExecutorObj.getUserRoles(); + + //System.out.println(userRoles.status); + assertEquals("Success",userRoles.details); + Result> userRoles1 = cassExecutorObj.getUserRoles(); + + //System.out.println(userRoles1.status); + assertEquals(0, userRoles1.status); + } + + @Test + public void testGetRole() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + + retVal1.value.add(dataObj); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result> userRoles = cassExecutorObj.getRoles(); + + //System.out.println(""+userRoles.status); + assertEquals(4,userRoles.status); + } + + @Test + public void testGetRoleFirstIf() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + q.roleDAO = Mockito.mock(CachedRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + dataObj.ns=""; + dataObj.rname=""; + + retVal1.value.add(dataObj); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(false).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + Mockito.doReturn(retVal1).when(q.roleDAO).read(trans,"",""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result> userRoles = cassExecutorObj.getRoles(); + +// System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + + @Test + public void testGetRoleSecondIf() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + q.roleDAO = Mockito.mock(CachedRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + dataObj.ns=""; + dataObj.rname=""; + + retVal1.value.add(dataObj); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + Mockito.doReturn(retVal1).when(q.roleDAO).read(trans,"",""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result> userRoles = cassExecutorObj.getRoles(); + userRoles = cassExecutorObj.getRoles(); + +// System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + @Test + public void testGetPerms() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList(); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result> userRoles = cassExecutorObj.getPermNames(); + userRoles = cassExecutorObj.getPermNames(); + + //System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + @Test + public void testGetPermsRrldOk() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + retVal1.value = new ArrayList(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + dataObj.expires = new Date(); + + retVal1.value.add(dataObj); + Mockito.doReturn(false).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); + Result> userRoles = cassExecutorObj.getPermNames(); + + //System.out.println(""+userRoles.status); + assertEquals(4,userRoles.status); + } + + + @Test + public void testGetPerm() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + q.roleDAO = Mockito.mock(CachedRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + Result> retVal2 = Mockito.mock(Result.class); + + retVal1.value = new ArrayList(); + retVal2.value = new ArrayList(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + Date dt = new Date(); + Calendar c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj.expires = c.getTime(); + dataObj.ns = ""; + dataObj.rname=""; + + RoleDAO.Data dataObj1 = Mockito.mock( RoleDAO.Data.class); + Set permSet = new HashSet(); + permSet.add("test"); + Mockito.doReturn(permSet).when(dataObj1).perms(false); + + dt = new Date(); + c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj1.ns = "test"; + dataObj1.perms = permSet; + + retVal1.value.add(dataObj); + retVal2.value.add(dataObj1); + Mockito.doReturn(true).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(true).when(retVal2).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + Mockito.doReturn(retVal2).when(q.roleDAO).read(trans,"",""); + + + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); +// +// Mockito.doReturn(retVal2).when(cassExecutorObj).getPermNames(); + Result> userRoles = cassExecutorObj.getPerms(true); +// userRoles = cassExecutorObj.getPerms(false); + +// System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + + @Test + public void testGetPermFalse() { + q.userRoleDAO = Mockito.mock(CachedUserRoleDAO.class); + q.roleDAO = Mockito.mock(CachedRoleDAO.class); + Result> retVal1 = Mockito.mock(Result.class); + Result> retVal2 = Mockito.mock(Result.class); + + retVal1.value = new ArrayList(); + retVal2.value = new ArrayList(); + UserRoleDAO.Data dataObj = Mockito.mock( UserRoleDAO.Data.class); + + Date dt = new Date(); + Calendar c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj.expires = c.getTime(); + dataObj.ns = ""; + dataObj.rname=""; + + RoleDAO.Data dataObj1 = Mockito.mock( RoleDAO.Data.class); + Set permSet = new HashSet(); + permSet.add("test"); + Mockito.doReturn(permSet).when(dataObj1).perms(false); + + dt = new Date(); + c = Calendar.getInstance(); + c.setTime(dt); + c.add(Calendar.DATE, 1); + dataObj1.ns = "test"; + dataObj1.perms = permSet; + + retVal1.value.add(dataObj); + retVal2.value.add(dataObj1); + Mockito.doReturn(true).when(retVal1).isOKhasData(); + Mockito.doReturn(true).when(retVal1).isOK(); + Mockito.doReturn(true).when(retVal2).isOK(); + Mockito.doReturn(retVal1).when(q.userRoleDAO).readByUser(trans,""); + Mockito.doReturn(retVal2).when(q.roleDAO).read(trans,"",""); + + + PermLookup cassExecutorObj =PermLookup.get(trans, q,""); +// +// Mockito.doReturn(retVal2).when(cassExecutorObj).getPermNames(); + Result> userRoles = cassExecutorObj.getPerms(false); + userRoles = cassExecutorObj.getPerms(false); + +// System.out.println(""+userRoles.status); + assertEquals(0,userRoles.status); + } + +} \ No newline at end of file -- cgit 1.2.3-korg