X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-cass%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Fdirect%2Ftest%2FJU_DirectAAFLur.java;h=0ed6e60c96a67ed0df22a620d8527e54702a0a77;hb=3d1706fcbe7f95830ff6fd23cf679ee55c6d0595;hp=79d30c4c4fe25c3a8420911dd2878823a6f96693;hpb=71037c39a37d3549dcfe31926832a657744fbe05;p=aaf%2Fauthz.git diff --git a/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFLur.java b/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFLur.java index 79d30c4c..0ed6e60c 100644 --- a/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFLur.java +++ b/auth/auth-cass/src/test/java/org/onap/aaf/auth/direct/test/JU_DirectAAFLur.java @@ -1,63 +1,243 @@ -/******************************************************************************* +/** * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 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==================================================== - * * - * * - ******************************************************************************/ + * 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.direct.test; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.MockitoAnnotations.initMocks; import java.security.Principal; +import java.util.ArrayList; +import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aaf.auth.dao.cass.NsSplit; +import org.onap.aaf.auth.dao.cass.PermDAO; +import org.onap.aaf.auth.dao.cass.PermDAO.Data; import org.onap.aaf.auth.dao.hl.Question; import org.onap.aaf.auth.direct.DirectAAFLur; +import org.onap.aaf.auth.direct.DirectAAFLur.PermPermission; 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.env.NullTrans; +import org.onap.aaf.auth.layer.Result; import org.onap.aaf.cadi.Permission; -import org.powermock.modules.junit4.PowerMockRunner; -@RunWith(PowerMockRunner.class) -public class JU_DirectAAFLur { - -public static AuthzEnv env; -public static Question question; -public DirectAAFLur directAAFLur; +import org.onap.aaf.cadi.aaf.AAFPermission; +import org.onap.aaf.cadi.lur.LocalPermission; +import org.onap.aaf.misc.env.LogTarget; +import org.onap.aaf.misc.env.Trans; +@RunWith(MockitoJUnitRunner.class) +public class JU_DirectAAFLur { + @Mock + AuthzEnv env; + + @Mock + Question question; + + @Mock + Principal bait; + + @Mock + Permission pond; + + @Mock + AuthzTrans trans; + + @Before + public void setUp() throws Exception { + initMocks(this); + Mockito.when(env.newTransNoAvg()).thenReturn(trans); + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(trans).error(); + } + + public void testFish() { + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + + List rsVal = new ArrayList(); + Result> rs = new Result>(rsVal,0,"test",new Object[0]); + Mockito.when(question.getPermsByUser(env.newTransNoAvg(), bait.getName(), false)).thenReturn(rs); + aafLurObj.fish(bait, pond); + } - @Before - public void setUp() - { - directAAFLur = new DirectAAFLur(env, question); - } - - @Test - public void testFish() - { - - Principal bait = null; - Permission pond=null; - directAAFLur.fish(bait, pond); - - assertTrue(true); - - } - + @Test + public void testFishSecondMtd() { + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + + List rsVal = new ArrayList(); + PermDAO.Data pd = new PermDAO.Data(); + pd.ns = "ns"; + pd.type = "name"; + pd.instance = null; + rsVal.add(pd); + pd = new PermDAO.Data(); + pd.ns = "ns"; + pd.type = "name"; + pd.instance = "instance"; + pd.action = null; + rsVal.add(pd); + + pd = new PermDAO.Data(); + pd.ns = "ns"; + pd.type = "name"; + pd.instance = "instance1"; + rsVal.add(pd); + pd = new PermDAO.Data(); + pd.ns = "ns1"; + pd.type = "name"; + rsVal.add(pd); + pd = new PermDAO.Data(); + pd.ns = "ns"; + pd.type = "name1"; + rsVal.add(pd); + + pd = new PermDAO.Data(); + pd.ns = "ns"; + pd.type = "name"; + pd.instance = "instance"; + pd.action = "action"; + rsVal.add(pd); + + pond = new DirectAAFLur.PermPermission("ns", "name", "instance", "action"); + + Result> rs = new Result>(rsVal,0,"test",new Object[0]); + Mockito.when(question.getPermsByUser(env.newTransNoAvg(), bait.getName(), false)).thenReturn(rs); + aafLurObj.fish(bait, pond); + + pond = new AAFPermission("ns", "name", "instance", "action"); + + Mockito.when(question.getPermsByUser(env.newTransNoAvg(), bait.getName(), false)).thenReturn(rs); + aafLurObj.fish(bait, pond); + + rs = new Result>(rsVal,1,"test",new Object[0]); + Mockito.when(question.getPermsByUser(env.newTransNoAvg(), bait.getName(), false)).thenReturn(rs); + aafLurObj.fish(bait, pond); + rs = new Result>(rsVal,4,"test",new Object[0]); + Mockito.when(question.getPermsByUser(env.newTransNoAvg(), bait.getName(), false)).thenReturn(rs); + aafLurObj.fish(bait, pond); + rs = new Result>(rsVal,25,"test",new Object[0]); + Mockito.when(question.getPermsByUser(env.newTransNoAvg(), bait.getName(), false)).thenReturn(rs); + aafLurObj.fish(bait, pond); + } + + @Test + public void testFishAll() { + Mockito.doReturn(Mockito.mock(LogTarget.class)).when(env).error(); + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + + List rsVal = new ArrayList(); + PermDAO.Data pd = new PermDAO.Data(); + pd.ns = "ns"; + pd.type = "name"; + pd.instance = null; + rsVal.add(pd); + + pond = new DirectAAFLur.PermPermission("ns", "name", "instance", "action"); + List permissions = new ArrayList<>(); + permissions.add(pond); + + Result> rs = new Result>(rsVal,0,"test",new Object[0]); + Mockito.when(question.getPermsByUser(env.newTrans(), bait.getName(), false)).thenReturn(rs); + aafLurObj.fishAll(bait, permissions); + assertTrue(permissions.size() == 2); + + rs = new Result>(rsVal,1,"test",new Object[0]); + Mockito.when(question.getPermsByUser(env.newTrans(), bait.getName(), false)).thenReturn(rs); + aafLurObj.fishAll(bait, permissions); + } + + @Test + public void testDestroy() { + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + aafLurObj.destroy(); + } + + @Test + public void testHandlesExclusively() { + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + pond = new DirectAAFLur.PermPermission("ns", "name", "instance", "action"); + assertFalse(aafLurObj.handlesExclusively(pond)); + } + + @Test + public void testToString() { + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + assertTrue(aafLurObj.toString().contains("DirectAAFLur is enabled")); + } + + @Test + public void testHandles() { + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + assertTrue(aafLurObj.handles(null)); + } + + @Test + public void testCreatePerm() { + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + Permission retVal = aafLurObj.createPerm("test"); + assertTrue(retVal instanceof LocalPermission); + + NsSplit nss = new NsSplit("test", "test"); + Result rs = new Result(nss,0,"test",new Object[0]); + Mockito.when(question.deriveNsSplit(NullTrans.singleton() , "test")).thenReturn(rs); + retVal = aafLurObj.createPerm("test|1|2"); + assertTrue(retVal instanceof PermPermission); + + rs = new Result(null,1,"test",new Object[0]); + Mockito.when(question.deriveNsSplit(NullTrans.singleton() , "test")).thenReturn(rs); + retVal = aafLurObj.createPerm("test|1|2"); + assertTrue(retVal instanceof LocalPermission); + } + + @Test + public void testClear() { + AuthzTransImpl trans = Mockito.mock(AuthzTransImpl.class); + Mockito.when(env.newTrans()).thenReturn(trans); + DirectAAFLur aafLurObj = new DirectAAFLur(env, question); + StringBuilder sb = new StringBuilder(); + Mockito.when(trans.auditTrail(0, sb)).thenReturn(Mockito.mock(Trans.Metric.class)); + aafLurObj.clear(bait, sb); + } + + @Test + public void testPermPermission() { + AuthzTransImpl trans = Mockito.mock(AuthzTransImpl.class); + NsSplit nss = new NsSplit("test", "test"); + Result rs = new Result(nss,0,"test",new Object[0]); + Mockito.when(question.deriveNsSplit(trans , "test")).thenReturn(rs); + PermPermission pp = new PermPermission(trans, question, "test|test|test|test"); + + assertTrue("test".equalsIgnoreCase(pp.getKey())); + assertTrue("AAFLUR".equalsIgnoreCase(pp.permType())); + + assertFalse(pp.match(null)); + + pond = new AAFPermission("test.test", "test", "test", "test"); + assertTrue(pp.match(pond)); + } }