diff options
author | Jonathan Gathman <jonathan.gathman@att.com> | 2019-02-21 13:27:44 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-02-21 13:27:44 +0000 |
commit | 815bb520743dc523f69bebc367391872888fd5ef (patch) | |
tree | 589faa8041ccaf5cadd3a6ce44aae7be2efcf025 /auth/auth-batch/src/test | |
parent | dd097a4b411cd78ced737548a43e019b1f3172f1 (diff) | |
parent | 3ceb063fb173412ad0f499ec07a753f678a80232 (diff) |
Merge "Add more junits to auth-batch"
Diffstat (limited to 'auth/auth-batch/src/test')
9 files changed, 1999 insertions, 18 deletions
diff --git a/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_ActionDAO.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_ActionDAO.java index 9376da4b..39743896 100644 --- a/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_ActionDAO.java +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_ActionDAO.java @@ -21,37 +21,46 @@ package org.onap.aaf.auth.batch.actions.test; -import static org.junit.Assert.*; -import org.junit.After; +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.IOException; +import java.util.Properties; + import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.hl.Function; +import org.onap.aaf.auth.dao.hl.Question; import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; import org.onap.aaf.misc.env.APIException; +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.Configuration; -import com.datastax.driver.core.Cluster.Initializer; -import com.datastax.driver.core.Host.StateListener; - -import static org.mockito.Mockito.*; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.Collection; -import java.util.List; - -import org.junit.Test; +import com.datastax.driver.core.ColumnDefinitions; +import com.datastax.driver.core.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; public class JU_ActionDAO { - AuthzTrans aTrans; - Cluster cluster; - ActionDAOStub actionDAOStub; - ActionDAOStub actionDAOStub1; + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + ActionDAO actionObj; private class ActionDAOStub extends ActionDAO { @@ -75,6 +84,34 @@ public class JU_ActionDAO { @Before public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj=Mockito.mock(Session.class); + PreparedStatement psObj =Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + Define.set(access); + actionObj = new ActionDAOStub(trans, cluster, true); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } // Cluster.Initializer cInit = mock(Cluster.Initializer.class); // Cluster.Builder cBuild = new Cluster.Builder(); // cBuild.addContactPoint("test"); @@ -91,5 +128,44 @@ public class JU_ActionDAO { // actionDAOStub = new ActionDAOStub(aTrans,cluster,true); // actionDAOStub1 = new ActionDAOStub(aTrans, actionDAOStub); } + + @Test + public void testGetSession() { + try { + Session session = actionObj.getSession(trans); + assertTrue(session.toString().contains("Mock for Session")); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testQuestion() { + Question retVal = actionObj.question(); + assertTrue(retVal.toString().contains("org.onap.aaf.auth.dao.hl.Question")); + } + + @Test + public void testFunction() { + Function retVal = actionObj.function(); + assertTrue(retVal.toString().contains("org.onap.aaf.auth.dao.hl.Function")); + } + + @Test + public void testClose() { + actionObj.close(trans); +// assertTrue(session.toString().contains("Mock for Session")); + } + + @Test + public void testCloseFalse() { + actionObj = new ActionDAOStub(trans, Mockito.mock(ActionDAO.class)); + actionObj.close(trans); +// assertTrue(session.toString().contains("Mock for Session")); + } } diff --git a/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_CacheTouch.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_CacheTouch.java new file mode 100644 index 00000000..99e11a5a --- /dev/null +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_CacheTouch.java @@ -0,0 +1,124 @@ +/** + * ============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.batch.actions.test; + +import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.io.IOException; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.batch.actions.CacheTouch; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +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.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; + +public class JU_CacheTouch { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + CacheTouch actionObj; + + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj=Mockito.mock(Session.class); + PreparedStatement psObj =Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + Define.set(access); + actionObj = new CacheTouch(trans, cluster, true); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testExec() { + Result<Void> session = actionObj.exec(trans,"test","test"); + assertTrue(session.toString().contains("Success")); + + } + @Test + public void testExecElse() { + try { + actionObj = new CacheTouch(trans, cluster, false); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Result<Void> session = actionObj.exec(trans,"test","test"); + assertTrue(session.toString().contains("No Cache Data named test")); + + } + + @Test + public void test2Argonstructor() { + actionObj = new CacheTouch(trans, Mockito.mock(ActionDAO.class)); + + + } + +} diff --git a/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_RoleCreate.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_RoleCreate.java new file mode 100644 index 00000000..cf563fdf --- /dev/null +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_RoleCreate.java @@ -0,0 +1,249 @@ +/** + * ============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.batch.actions.test; + +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.Modifier; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.batch.actions.RoleCreate; +import org.onap.aaf.auth.batch.helpers.Role; +import org.onap.aaf.auth.batch.helpers.UserRole; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cached.CachedRoleDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +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.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; + +public class JU_RoleCreate { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + RoleCreate createObj; + + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj=Mockito.mock(Session.class); + PreparedStatement psObj =Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + Define.set(access); + createObj = new RoleCreate(trans, cluster, true); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testExec() { + Result<RoleDAO.Data> retVal = createObj.exec(trans,Mockito.mock(Role.class),"test"); + assertTrue(retVal.toString().contains("Success")); + + } + @Test + public void testExecElse() { + Question ques = Mockito.mock(Question.class); + try { + Role roleObj = new Role("test","test","test",new HashSet()); + + CachedRoleDAO roleDaoObj = Mockito.mock(CachedRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(roleDaoObj).create(Mockito.any(), Mockito.any()); + + createObj = new RoleCreateImpl(trans, cluster, false, ques, roleDaoObj); + Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseDateLess() { + Question ques = Mockito.mock(Question.class); + try { + Role roleObj = new Role("test","test","test",new HashSet()); + + CachedRoleDAO userRoleDaoObj = Mockito.mock(CachedRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy"); + try { + data.expires = sdf.parse("01/01/2100"); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).create(Mockito.any(), Mockito.any()); + + createObj = new RoleCreateImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseNok() { + Question ques = Mockito.mock(Question.class); + try { + Role roleObj = new Role("test","test","test",new HashSet()); + + CachedRoleDAO userRoleDaoObj = Mockito.mock(CachedRoleDAO.class); + + Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).create(Mockito.any(), Mockito.any()); + + createObj = new RoleCreateImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<RoleDAO.Data> session = createObj.exec(trans, roleObj, "test"); + assertTrue(session.toString().contains("test")); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void test2Argonstructor() { + createObj = new RoleCreate(trans, Mockito.mock(ActionDAO.class)); + } + + class RoleCreateImpl extends RoleCreate{ + + public RoleCreateImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedRoleDAO userRoleDaoObj) throws APIException, IOException { + super(trans, cluster, dryRun); + setQuestion(ques, userRoleDaoObj); + } + + public void setQuestion(Question ques, CachedRoleDAO userRoleDaoObj) { + Field field, nsDaoField; + try { + field = RoleCreateImpl.class.getSuperclass().getSuperclass().getDeclaredField("q"); + nsDaoField = Question.class.getDeclaredField("roleDAO"); + + field.setAccessible(true); + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL); + + field.set(this, ques); + nsDaoField.set(ques, userRoleDaoObj); + } 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-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_RoleDeleteTest.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_RoleDeleteTest.java new file mode 100644 index 00000000..83899fd9 --- /dev/null +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_RoleDeleteTest.java @@ -0,0 +1,249 @@ +/** + * ============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.batch.actions.test; + +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.Modifier; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.batch.actions.RoleDelete; +import org.onap.aaf.auth.batch.helpers.Role; +import org.onap.aaf.auth.batch.helpers.UserRole; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cached.CachedRoleDAO; +import org.onap.aaf.auth.dao.cass.RoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +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.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; + +public class JU_RoleDeleteTest { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + RoleDelete createObj; + + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj=Mockito.mock(Session.class); + PreparedStatement psObj =Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + Define.set(access); + createObj = new RoleDelete(trans, cluster, true); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testExec() { + Result<Void> retVal = createObj.exec(trans,Mockito.mock(Role.class),"test"); + assertTrue(retVal.toString().contains("Success")); + + } + @Test + public void testExecElse() { + Question ques = Mockito.mock(Question.class); + try { + Role roleObj = new Role("test","test","test",new HashSet()); + + CachedRoleDAO roleDaoObj = Mockito.mock(CachedRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(roleDaoObj).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); + + createObj = new RoleDeleteImpl(trans, cluster, false, ques, roleDaoObj); + Result<Void> session = createObj.exec(trans, roleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseDateLess() { + Question ques = Mockito.mock(Question.class); + try { + Role roleObj = new Role("test","test","test",new HashSet()); + + CachedRoleDAO userRoleDaoObj = Mockito.mock(CachedRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy"); + try { + data.expires = sdf.parse("01/01/2100"); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); + + createObj = new RoleDeleteImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = createObj.exec(trans, roleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseNok() { + Question ques = Mockito.mock(Question.class); + try { + Role roleObj = new Role("test","test","test",new HashSet()); + + CachedRoleDAO userRoleDaoObj = Mockito.mock(CachedRoleDAO.class); + + Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).delete(Mockito.any(), Mockito.any(), Mockito.anyBoolean()); + + createObj = new RoleDeleteImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = createObj.exec(trans, roleObj, "test"); + assertTrue(session.toString().contains("test")); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void test2Argonstructor() { + createObj = new RoleDelete(trans, Mockito.mock(ActionDAO.class)); + } + + class RoleDeleteImpl extends RoleDelete{ + + public RoleDeleteImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedRoleDAO userRoleDaoObj) throws APIException, IOException { + super(trans, cluster, dryRun); + setQuestion(ques, userRoleDaoObj); + } + + public void setQuestion(Question ques, CachedRoleDAO userRoleDaoObj) { + Field field, nsDaoField; + try { + field = RoleDeleteImpl.class.getSuperclass().getSuperclass().getDeclaredField("q"); + nsDaoField = Question.class.getDeclaredField("roleDAO"); + + field.setAccessible(true); + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL); + + field.set(this, ques); + nsDaoField.set(ques, userRoleDaoObj); + } 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-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URAdd.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URAdd.java new file mode 100644 index 00000000..50fb962b --- /dev/null +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URAdd.java @@ -0,0 +1,246 @@ +/** + * ============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.batch.actions.test; + +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.Modifier; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.batch.actions.URAdd; +import org.onap.aaf.auth.batch.helpers.UserRole; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +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.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; + +public class JU_URAdd { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + URAdd addObj; + + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj=Mockito.mock(Session.class); + PreparedStatement psObj =Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + Define.set(access); + addObj = new URAdd(trans, cluster, true); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testExec() { + Result<Data> retVal = addObj.exec(trans,Mockito.mock(UserRole.class),"test"); + assertTrue(retVal.toString().contains("Success")); + + } + @Test + public void testExecElse() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd()); + + addObj = new URAddImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Data> session = addObj.exec(trans, userRoleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseDateLess() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy"); + try { + data.expires = sdf.parse("01/01/2100"); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd()); + + addObj = new URAddImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Data> session = addObj.exec(trans, userRoleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseNok() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).create(trans, userRoleObj.urdd()); + + addObj = new URAddImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Data> session = addObj.exec(trans, userRoleObj, "test"); + assertTrue(session.toString().contains("test")); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void test2Argonstructor() { + addObj = new URAdd(trans, Mockito.mock(ActionDAO.class)); + } + + class URAddImpl extends URAdd{ + + public URAddImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedUserRoleDAO userRoleDaoObj) throws APIException, IOException { + super(trans, cluster, dryRun); + setQuestion(ques, userRoleDaoObj); +// q =new Question(trans, cluster, CassAccess.KEYSPACE, false); +// q = ques; + // TODO Auto-generated constructor stub + } + + public void setQuestion(Question ques, CachedUserRoleDAO userRoleDaoObj) { + Field field, nsDaoField; + try { + field = URAddImpl.class.getSuperclass().getSuperclass().getDeclaredField("q"); + nsDaoField = Question.class.getDeclaredField("userRoleDAO"); + + field.setAccessible(true); + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL); + + field.set(this, ques); + nsDaoField.set(ques, userRoleDaoObj); + } 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-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URDelete.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URDelete.java new file mode 100644 index 00000000..6e92591a --- /dev/null +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URDelete.java @@ -0,0 +1,246 @@ +/** + * ============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.batch.actions.test; + +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.Modifier; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.batch.actions.URDelete; +import org.onap.aaf.auth.batch.helpers.UserRole; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +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.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; + +public class JU_URDelete { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + URDelete deleteObj; + + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj=Mockito.mock(Session.class); + PreparedStatement psObj =Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + Define.set(access); + deleteObj = new URDelete(trans, cluster, true); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testExec() { + Result<Void> retVal = deleteObj.exec(trans,Mockito.mock(UserRole.class),"test"); + assertTrue(retVal.toString().contains("Success")); + + } + @Test + public void testExecElse() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).delete(trans, userRoleObj.urdd(), true); + + deleteObj = new URDeleteImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = deleteObj.exec(trans, userRoleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseDateLess() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy"); + try { + data.expires = sdf.parse("01/01/2100"); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).delete(trans, userRoleObj.urdd(), true); + + deleteObj = new URDeleteImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = deleteObj.exec(trans, userRoleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseNok() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).delete(trans, userRoleObj.urdd(), true); + + deleteObj = new URDeleteImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = deleteObj.exec(trans, userRoleObj, "test"); + assertTrue(session.toString().contains("test")); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void test2Argonstructor() { + deleteObj = new URDelete(trans, Mockito.mock(ActionDAO.class)); + } + + class URDeleteImpl extends URDelete{ + + public URDeleteImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedUserRoleDAO userRoleDaoObj) throws APIException, IOException { + super(trans, cluster, dryRun); + setQuestion(ques, userRoleDaoObj); +// q =new Question(trans, cluster, CassAccess.KEYSPACE, false); +// q = ques; + // TODO Auto-generated constructor stub + } + + public void setQuestion(Question ques, CachedUserRoleDAO userRoleDaoObj) { + Field field, nsDaoField; + try { + field = URDeleteImpl.class.getSuperclass().getSuperclass().getDeclaredField("q"); + nsDaoField = Question.class.getDeclaredField("userRoleDAO"); + + field.setAccessible(true); + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL); + + field.set(this, ques); + nsDaoField.set(ques, userRoleDaoObj); + } 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-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URFutureApproveExec.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URFutureApproveExec.java new file mode 100644 index 00000000..35409042 --- /dev/null +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URFutureApproveExec.java @@ -0,0 +1,264 @@ +/** + * ============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.batch.actions.test; + +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.Modifier; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.batch.actions.URFutureApproveExec; +import org.onap.aaf.auth.batch.actions.test.JU_URPunt.URPuntImpl; +import org.onap.aaf.auth.batch.helpers.Approval; +import org.onap.aaf.auth.batch.helpers.Future; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cass.FutureDAO; +import org.onap.aaf.auth.dao.hl.Function; +import org.onap.aaf.auth.dao.hl.Function.FUTURE_OP; +import org.onap.aaf.auth.dao.hl.Function.OP_STATUS; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +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.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; + +import io.netty.util.internal.SystemPropertyUtil; + + + +public class JU_URFutureApproveExec { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + URFutureApproveExec actionObj; + + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj=Mockito.mock(Session.class); + PreparedStatement psObj =Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + Define.set(access); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testExec() { + try { + actionObj = new URFutureApproveExec(trans, cluster, true); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Approval approval = Mockito.mock(Approval.class); + List<Approval> approvalAL = new ArrayList<>(); + approvalAL.add(approval); + Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,Mockito.mock(Future.class)); + assertTrue(8 == retVal.status); + + } + + @Test + public void testExecElseOpStatusD() { + Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.D, 0, "test", new String[0]); + try { + actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L); + List<Approval> approvalAL = new ArrayList<>(); + + Future futureObj = new Future(null, "", "", new Date(), new Date(), null); + + approvalAL.add(approval); + Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj); + assertTrue(0 == retVal.status && "test".equals(retVal.toString())); + + } + + @Test + public void testExecElseOpStatusE() { + Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.E, 0, "test", new String[0]); + try { + actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L); + List<Approval> approvalAL = new ArrayList<>(); + + Future futureObj = new Future(null, "", "", new Date(), new Date(), null); + + approvalAL.add(approval); + Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj); + assertTrue(0 == retVal.status && "test".equals(retVal.toString())); + + } + + @Test + public void testExecElseOpStatusL() { + Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.L, 0, "test", new String[0]); + try { + actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L); + List<Approval> approvalAL = new ArrayList<>(); + + Future futureObj = new Future(null, "", "", new Date(), new Date(), null); + + approvalAL.add(approval); + Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj); + assertTrue(0 == retVal.status && "test".equals(retVal.toString())); + + } + + @Test + public void testExecElseOpStatusP() { + Result<OP_STATUS> retValD = new Result<OP_STATUS>(OP_STATUS.P, 0, "test", new String[0]); + try { + actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L); + List<Approval> approvalAL = new ArrayList<>(); + + Future futureObj = new Future(null, "", "", new Date(), new Date(), null); + + approvalAL.add(approval); + Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj); + assertTrue(0 == retVal.status && "test".equals(retVal.toString())); + + } + + @Test + public void testExecElseNok() { + Result<OP_STATUS> retValD = new Result<OP_STATUS>(null, 1, "test", new String[0]); + try { + actionObj = new URFutureApproveExecImpl(trans, cluster, false, retValD); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Approval approval = new Approval(null, null, "", new Date(), "test", "", "", "", "", 0L); + List<Approval> approvalAL = new ArrayList<>(); + + Future futureObj = new Future(null, "", "", new Date(), new Date(), null); + + approvalAL.add(approval); + Result<OP_STATUS> retVal = actionObj.exec(trans,approvalAL,futureObj); + System.out.println(retVal); + assertTrue(1 == retVal.status); + + } + + @Test + public void test2Argonstructor() { + actionObj = new URFutureApproveExec(trans, Mockito.mock(ActionDAO.class)); + } + + class URFutureApproveExecImpl extends URFutureApproveExec{ + + public URFutureApproveExecImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Result<OP_STATUS> retValD) + throws APIException, IOException { + super(trans, cluster, dryRun); + setFunction(Mockito.mock(Function.class)); + Mockito.doReturn(retValD).when(f).performFutureOp(Mockito.any(), Mockito.any(), Mockito.any(),Mockito.any(),Mockito.any()); + } + + public void setFunction(Function f) { + Field field; + try { + field = URFutureApproveExecImpl.class.getSuperclass().getSuperclass().getDeclaredField("f"); + + field.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + + field.set(this, f); + } 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-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URModify.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URModify.java new file mode 100644 index 00000000..eb9a4bf9 --- /dev/null +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URModify.java @@ -0,0 +1,281 @@ +/** + * ============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.batch.actions.test; + +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.Modifier; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.batch.actions.URModify; +import org.onap.aaf.auth.batch.actions.URPunt; +import org.onap.aaf.auth.batch.actions.URModify.Modify; +import org.onap.aaf.auth.batch.actions.test.JU_URPunt.URPuntImpl; +import org.onap.aaf.auth.batch.helpers.UserRole; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO; +import org.onap.aaf.auth.dao.cass.Status; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +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.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; + +public class JU_URModify { + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + URModify modifyObj; + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj = Mockito.mock(Session.class); + PreparedStatement psObj = Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(), Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS, "org.osaaf.aaf"); + Define.set(access); + modifyObj = new URModify(trans, cluster, true); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testURPrint() { + UserRole ur = new UserRole("user", "ns", "rname", Calendar.getInstance().getTime()); + Result<Void> retVal = modifyObj.exec(trans, ur, Mockito.mock(Modify.class)); + assertTrue(retVal.toString().contains("Success")); + } + + @Test + public void testExecElse() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test", "test", "test", new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + data.user="test"; + data.role="test.test"; + dataAL.add(data); + Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 0, "test", new String[0]); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL, 0, "test", new String[0]); + + Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role()); + Mockito.doReturn(retVal1).when(userRoleDaoObj).update(trans, data); + + modifyObj = new URModifyImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = modifyObj.exec(trans, userRoleObj, Mockito.mock(Modify.class)); + assertTrue(Status.ERR_UserRoleNotFound == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseUpdateFail() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test", "test", "test", new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + data.user="test"; + data.role="test.test"; + dataAL.add(data); + Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 0, "test", new String[0]); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL, 1, "test", new String[0]); + + Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role()); + Mockito.doReturn(retVal1).when(userRoleDaoObj).update(trans, data); + + modifyObj = new URModifyImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = modifyObj.exec(trans, userRoleObj, Mockito.mock(Modify.class)); + assertTrue(Status.ERR_UserRoleNotFound == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseUserDiff() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test", "test", "test", new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + dataAL.add(data); + Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 0, "test", new String[0]); + + Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role()); + + modifyObj = new URModifyImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = modifyObj.exec(trans, userRoleObj, Mockito.mock(Modify.class)); + assertTrue(Status.ERR_Denied == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseReadNOK() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test", "test", "test", new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + dataAL.add(data); + Result<List<Data>> retVal = new Result<List<Data>>(dataAL, 1, "test", new String[0]); + + Mockito.doReturn(retVal).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role()); + + modifyObj = new URModifyImpl(trans, cluster, false, ques, userRoleDaoObj); + Result<Void> session = modifyObj.exec(trans, userRoleObj, Mockito.mock(Modify.class)); + assertTrue(1 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void test2Argonstructor() { + modifyObj = new URModify(trans, Mockito.mock(ActionDAO.class)); + } + + class URModifyImpl extends URModify{ + + public URModifyImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, Question ques, CachedUserRoleDAO userRoleDaoObj) throws APIException, IOException { + super(trans, cluster, dryRun); + setQuestion(ques, userRoleDaoObj); +// q =new Question(trans, cluster, CassAccess.KEYSPACE, false); +// q = ques; + // TODO Auto-generated constructor stub + } + + public void setQuestion(Question ques, CachedUserRoleDAO userRoleDaoObj) { + Field field, nsDaoField; + try { + field = URPuntImpl.class.getSuperclass().getSuperclass().getSuperclass().getDeclaredField("q"); + nsDaoField = Question.class.getDeclaredField("userRoleDAO"); + + field.setAccessible(true); + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL); + + field.set(this, ques); + nsDaoField.set(ques, userRoleDaoObj); + } 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-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URPunt.java b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URPunt.java new file mode 100644 index 00000000..434ec418 --- /dev/null +++ b/auth/auth-batch/src/test/java/org/onap/aaf/auth/batch/actions/test/JU_URPunt.java @@ -0,0 +1,246 @@ +/** + * ============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.batch.actions.test; + +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.Modifier; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.aaf.auth.batch.actions.ActionDAO; +import org.onap.aaf.auth.batch.actions.URPunt; +import org.onap.aaf.auth.batch.helpers.UserRole; +import org.onap.aaf.auth.common.Define; +import org.onap.aaf.auth.dao.cached.CachedUserRoleDAO; +import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data; +import org.onap.aaf.auth.dao.hl.Question; +import org.onap.aaf.auth.env.AuthzTrans; +import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.misc.env.APIException; +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.PreparedId; +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.Session; + +public class JU_URPunt { + + @Mock + AuthzTrans trans; + @Mock + Cluster cluster; + @Mock + PropAccess access; + + @Mock + URPunt actionObj; + + + @Before + public void setUp() throws APIException, IOException { + initMocks(this); + Session sessionObj=Mockito.mock(Session.class); + PreparedStatement psObj =Mockito.mock(PreparedStatement.class); + try { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).init(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).warn(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).debug(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).info(); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + Mockito.doReturn("10").when(trans).getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF); + Mockito.doReturn(Mockito.mock(TimeTaken.class)).when(trans).start(Mockito.anyString(),Mockito.anyInt()); + Mockito.doReturn(sessionObj).when(cluster).connect("authz"); + Mockito.doReturn(psObj).when(sessionObj).prepare(Mockito.anyString()); + + Mockito.doReturn(Mockito.mock(ColumnDefinitions.class)).when(psObj).getVariables(); + Mockito.doReturn(Mockito.mock(PreparedId.class)).when(psObj).getPreparedId(); + Mockito.doReturn(Mockito.mock(Properties.class)).when(access).getProperties(); + Mockito.doReturn("test.test").when(access).getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf"); + Define.set(access); + actionObj = new URPunt(trans, cluster, 10, 10, true); + } catch (APIException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (CadiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void testExec() { + Result<Void> retVal = actionObj.exec(trans,Mockito.mock(UserRole.class),"test"); + assertTrue(retVal.toString().contains("Success")); + + } + @Test + public void testExecElse() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + data.expires = new Date(); + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role()); + + actionObj = new URPuntImpl(trans, cluster, false, 10, 10, ques, userRoleDaoObj); + Result<Void> session = actionObj.exec(trans, userRoleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseDateLess() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + List<Data> dataAL = new ArrayList<Data>(); + Data data = new Data(); + DateFormat sdf = new SimpleDateFormat("mm/dd/yyyy"); + try { + data.expires = sdf.parse("01/01/2100"); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + dataAL.add(data); + Result<List<Data>> retVal1 = new Result<List<Data>>(dataAL,0,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role()); + + actionObj = new URPuntImpl(trans, cluster, false, 0, 0,ques, userRoleDaoObj); + Result<Void> session = actionObj.exec(trans, userRoleObj, "test"); + assertTrue(0 == session.status); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void testExecElseNok() { + Question ques = Mockito.mock(Question.class); + try { + UserRole userRoleObj = new UserRole("test","test","test",new Date()); + CachedUserRoleDAO userRoleDaoObj = Mockito.mock(CachedUserRoleDAO.class); + + Result<Void> retVal1 = new Result<Void>(null,1,"test",new String[0]); + + Mockito.doReturn(retVal1).when(userRoleDaoObj).read(trans, userRoleObj.user(), userRoleObj.role()); + + actionObj = new URPuntImpl(trans, cluster, false, 10, 10, ques, userRoleDaoObj); + Result<Void> session = actionObj.exec(trans, userRoleObj, "test"); + assertTrue(session.toString().contains("test")); + } catch (APIException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void test2Argonstructor() { + actionObj = new URPunt(trans, Mockito.mock(ActionDAO.class), 10, 10); + } + + class URPuntImpl extends URPunt{ + + public URPuntImpl(AuthzTrans trans, Cluster cluster, boolean dryRun, int months, int range,Question ques, CachedUserRoleDAO userRoleDaoObj) throws APIException, IOException { + super(trans, cluster, months, range, dryRun); + setQuestion(ques, userRoleDaoObj); +// q =new Question(trans, cluster, CassAccess.KEYSPACE, false); +// q = ques; + // TODO Auto-generated constructor stub + } + + public void setQuestion(Question ques, CachedUserRoleDAO userRoleDaoObj) { + Field field, nsDaoField; + try { + field = URPuntImpl.class.getSuperclass().getSuperclass().getSuperclass().getDeclaredField("q"); + nsDaoField = Question.class.getDeclaredField("userRoleDAO"); + + field.setAccessible(true); + nsDaoField.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + modifiersField.setInt(nsDaoField, field.getModifiers() & ~Modifier.FINAL); + + field.set(this, ques); + nsDaoField.set(ques, userRoleDaoObj); + } 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(); + } + } + } +} |