From db41ae6ccfd603f4955141017b41a5bda30c9add Mon Sep 17 00:00:00 2001 From: "Tschaen, Brendan" Date: Sun, 2 Jun 2019 19:00:24 -0400 Subject: AuthUtil tests added Change-Id: I78a13b43c85cb208c217841ed85eaf0284aad930 Issue-ID: MUSIC-405 Signed-off-by: Tschaen, Brendan --- .../org/onap/music/authentication/AuthUtil.java | 20 ++-- src/main/java/org/onap/music/main/MusicUtil.java | 30 ----- .../org/onap/music/unittests/MusicUtilTest.java | 9 ++ .../unittests/authentication/AuthUtilTest.java | 123 +++++++++++++++++++++ 4 files changed, 141 insertions(+), 41 deletions(-) create mode 100644 src/test/java/org/onap/music/unittests/authentication/AuthUtilTest.java (limited to 'src') diff --git a/src/main/java/org/onap/music/authentication/AuthUtil.java b/src/main/java/org/onap/music/authentication/AuthUtil.java index 999acc75..51e3dac0 100644 --- a/src/main/java/org/onap/music/authentication/AuthUtil.java +++ b/src/main/java/org/onap/music/authentication/AuthUtil.java @@ -134,7 +134,11 @@ public class AuthUtil { */ public static boolean isAccessAllowed(ServletRequest request, String nameSpace) throws Exception { - if (nameSpace.isEmpty()) { + if (request==null) { + throw new Exception("Request cannot be null"); + } + + if (nameSpace==null || nameSpace.isEmpty()) { throw new Exception("NameSpace not Declared!"); } @@ -143,9 +147,7 @@ public class AuthUtil { //logger.info(EELFLoggerDelegate.applicationLogger, // "AAFPermission of the requested MechId for all the namespaces: " + aafPermsList); - String requestUri = null; logger.debug(EELFLoggerDelegate.applicationLogger, "Requested nameSpace: " + nameSpace); - HttpServletRequest httpRequest = null; List aafPermsFinalList = filterNameSpacesAAFPermissions(nameSpace, aafPermsList); @@ -154,10 +156,8 @@ public class AuthUtil { "AuthUtil list of AAFPermission for the specific namespace ::::::::::::::::::::::::::::::::::::::::::::" + aafPermsFinalList); - if (null != request) { - httpRequest = (HttpServletRequest) request; - requestUri = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length() + 1); - } + HttpServletRequest httpRequest = (HttpServletRequest) request; + String requestUri = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length() + 1); logger.debug(EELFLoggerDelegate.applicationLogger, "AuthUtil requestUri ::::::::::::::::::::::::::::::::::::::::::::" + requestUri); @@ -222,10 +222,8 @@ public class AuthUtil { String[] subPath = null; //String type = null; //type = keyArray[0]; - String instance = null; - instance = keyArray[1]; - String action = null; - action = keyArray[2]; + String instance = keyArray[1]; + String action = keyArray[2]; //if the instance & action both are * , then allow if ("*".equalsIgnoreCase(instance) && "*".equalsIgnoreCase(action)) { diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java index 17e3fcf6..9ffa2503 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -653,36 +653,6 @@ public class MusicUtil { public static ConsistencyLevel getConsistencyLevel(String consistency) { return consistencyName.get(consistency.toUpperCase()); - } - - public static void loadProperties() throws Exception { - Properties prop = new Properties(); - InputStream input = null; - try { - // load the properties file - input = MusicUtil.class.getClassLoader().getResourceAsStream("music.properties"); - prop.load(input); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file.", ex); - throw new Exception(); - } finally { - if (input != null) { - try { - input.close(); - } catch (IOException e) { - e.printStackTrace(); - logger.error(EELFLoggerDelegate.errorLogger, e); - } - } - } - // get the property value and return it - MusicUtil.setMyCassaHost(prop.getProperty("cassandra.host")); - MusicUtil.setCassName(prop.getProperty("cassandra.user")); - MusicUtil.setCassPwd(prop.getProperty("cassandra.password")); - MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty("cassandra.port"))); - MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty("notify.timeout"))); - MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty("notify.interval"))); - MusicUtil.setCacheObjectMaxLife(Integer.parseInt(prop.getProperty("cacheobject.maxlife"))); } public static void setNotifyInterval(int notifyinterval) { diff --git a/src/test/java/org/onap/music/unittests/MusicUtilTest.java b/src/test/java/org/onap/music/unittests/MusicUtilTest.java index 2b6e0f5a..930959ba 100644 --- a/src/test/java/org/onap/music/unittests/MusicUtilTest.java +++ b/src/test/java/org/onap/music/unittests/MusicUtilTest.java @@ -33,6 +33,8 @@ import java.util.UUID; import org.junit.Test; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.main.MusicUtil; +import org.onap.music.main.PropertiesLoader; +import org.springframework.test.context.TestPropertySource; import com.datastax.driver.core.DataType; public class MusicUtilTest { @@ -294,4 +296,11 @@ public class MusicUtilTest { MusicUtil.setMessageIdRequired("msgIdRequired"); assertEquals("msgIdRequired", MusicUtil.getMessageIdRequired()); } + + @Test + public void testLoadProperties() { + PropertiesLoader pl = new PropertiesLoader(); + pl.loadProperties(); + } + } diff --git a/src/test/java/org/onap/music/unittests/authentication/AuthUtilTest.java b/src/test/java/org/onap/music/unittests/authentication/AuthUtilTest.java new file mode 100644 index 00000000..b578bd66 --- /dev/null +++ b/src/test/java/org/onap/music/unittests/authentication/AuthUtilTest.java @@ -0,0 +1,123 @@ +/* + * ============LICENSE_START========================================== + * org.onap.music + * =================================================================== + * Copyright (c) 2019 AT&T Intellectual Property + * =================================================================== + * 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.music.unittests.authentication; + +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; +import javax.servlet.ServletRequest; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.aaf.cadi.CadiWrap; +import org.onap.aaf.cadi.Permission; +import org.onap.aaf.cadi.aaf.AAFPermission; +import org.onap.music.authentication.AuthUtil; + +public class AuthUtilTest { + + @Test + public void testGetAAFPermissions() { + CadiWrap cw = Mockito.mock(CadiWrap.class); + List permList = new ArrayList(); + Permission perm1 = Mockito.mock(AAFPermission.class); + permList.add(perm1); + Mockito.when(cw.getPermissions(Mockito.any())).thenReturn(permList); + + List returnedPerm = AuthUtil.getAAFPermissions(cw); + + assertEquals(perm1, returnedPerm.get(0)); + } + + @Test + public void testDecodeFunctionCode() throws Exception { + String toDecode = "some%2dthing.something.%2a"; + String decoded = AuthUtil.decodeFunctionCode(toDecode); + + assertEquals("some-thing.something.*", decoded); + } + + @Test + public void testIsAccessAllowed() throws Exception { + System.out.println("Request perms"); + assertTrue(AuthUtil.isAccessAllowed(createRequest("*", "*"), "testns")); + } + + @Test + public void testIsAccessNotAllowed() throws Exception { + System.out.println("Request to write when have read perms"); + assertFalse(AuthUtil.isAccessAllowed(createRequest("POST", "GET"), "testns")); + } + + @Test + public void testIsAccessAllowedNullRequest() { + try { + assertFalse(AuthUtil.isAccessAllowed(null, "namespace")); + fail("Should throw exception"); + } catch (Exception e) { + } + } + + @Test + public void testIsAccessAllowedNullNamespace() { + try { + assertFalse(AuthUtil.isAccessAllowed(createRequest(), null)); + fail("Should throw exception"); + } catch (Exception e) { + } + } + + @Test + public void testIsAccessAllowedEmptyNamespace() { + try { + assertFalse(AuthUtil.isAccessAllowed(createRequest(), "")); + fail("Should throw exception"); + } catch (Exception e) { + } + } + + /** + * + * @param permRequested 'PUT', 'POST', 'GET', or 'DELETE' + * @param permGranted '*' or 'GET' + * @return + */ + private ServletRequest createRequest(String permRequested, String permGranted) { + CadiWrap cw = Mockito.mock(CadiWrap.class); + List permList = new ArrayList(); + AAFPermission perm1 = Mockito.mock(AAFPermission.class); + Mockito.when(perm1.getType()).thenReturn("testns"); + Mockito.when(perm1.getKey()).thenReturn("org.onap.music.api.user.access|testns|" + permGranted); + + permList.add(perm1); + Mockito.when(cw.getPermissions(Mockito.any())).thenReturn(permList); + Mockito.when(cw.getRequestURI()).thenReturn("/v2/locks/create/testns.MyTable.Field1"); + Mockito.when(cw.getContextPath()).thenReturn("/v2/locks/create"); + Mockito.when(cw.getMethod()).thenReturn(permRequested); + + return cw; + } + + private ServletRequest createRequest() { + return createRequest("POST","*"); + } +} -- cgit 1.2.3-korg