2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2019 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.unittests.authentication;
25 import static org.junit.Assert.*;
26 import java.util.ArrayList;
27 import java.util.List;
28 import javax.servlet.ServletRequest;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.onap.aaf.cadi.CadiWrap;
32 import org.onap.aaf.cadi.Permission;
33 import org.onap.aaf.cadi.aaf.AAFPermission;
34 import org.onap.music.authentication.AuthUtil;
36 public class AuthUtilTest {
39 public void testGetAAFPermissions() {
40 CadiWrap cw = Mockito.mock(CadiWrap.class);
41 List<Permission> permList = new ArrayList<Permission>();
42 Permission perm1 = Mockito.mock(AAFPermission.class);
44 Mockito.when(cw.getPermissions(Mockito.any())).thenReturn(permList);
46 List<AAFPermission> returnedPerm = AuthUtil.getAAFPermissions(cw);
48 assertEquals(perm1, returnedPerm.get(0));
52 public void testDecodeFunctionCode() throws Exception {
53 String toDecode = "some%2dthing.something.%2a";
54 String decoded = AuthUtil.decodeFunctionCode(toDecode);
56 assertEquals("some-thing.something.*", decoded);
60 public void testIsAccessAllowed() throws Exception {
61 System.out.println("Request perms");
62 assertTrue(AuthUtil.isAccessAllowed(createRequest("*", "*"), "testns"));
66 public void testIsAccessNotAllowed() throws Exception {
67 System.out.println("Request to write when have read perms");
68 assertFalse(AuthUtil.isAccessAllowed(createRequest("POST", "GET"), "testns"));
72 public void testIsAccessAllowedNullRequest() {
74 assertFalse(AuthUtil.isAccessAllowed(null, "namespace"));
75 fail("Should throw exception");
76 } catch (Exception e) {
81 public void testIsAccessAllowedNullNamespace() {
83 assertFalse(AuthUtil.isAccessAllowed(createRequest(), null));
84 fail("Should throw exception");
85 } catch (Exception e) {
90 public void testIsAccessAllowedEmptyNamespace() {
92 assertFalse(AuthUtil.isAccessAllowed(createRequest(), ""));
93 fail("Should throw exception");
94 } catch (Exception e) {
100 * @param permRequested 'PUT', 'POST', 'GET', or 'DELETE'
101 * @param permGranted '*' or 'GET'
104 private ServletRequest createRequest(String permRequested, String permGranted) {
105 CadiWrap cw = Mockito.mock(CadiWrap.class);
106 List<Permission> permList = new ArrayList<Permission>();
107 AAFPermission perm1 = Mockito.mock(AAFPermission.class);
108 Mockito.when(perm1.getType()).thenReturn("testns");
109 Mockito.when(perm1.getKey()).thenReturn("org.onap.music.api.user.access|testns|" + permGranted);
112 Mockito.when(cw.getPermissions(Mockito.any())).thenReturn(permList);
113 Mockito.when(cw.getRequestURI()).thenReturn("/v2/locks/create/testns.MyTable.Field1");
114 Mockito.when(cw.getContextPath()).thenReturn("/v2/locks/create");
115 Mockito.when(cw.getMethod()).thenReturn(permRequested);
120 private ServletRequest createRequest() {
121 return createRequest("POST","*");