Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / impl / aaf / RoleAuthorizationHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.impl.aaf;
22
23 import org.aspectj.lang.JoinPoint;
24 import org.aspectj.lang.Signature;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
31 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
32 import org.openecomp.sdc.be.components.impl.aaf.RoleAuthorizationHandler;
33 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.servlets.BeGenericServlet;
37 import org.openecomp.sdc.common.api.ConfigurationSource;
38 import org.openecomp.sdc.common.api.FilterDecisionEnum;
39 import org.openecomp.sdc.common.impl.ExternalConfiguration;
40 import org.openecomp.sdc.common.impl.FSConfigurationSource;
41 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
42 import sun.reflect.annotation.AnnotationParser;
43
44 import javax.servlet.http.HttpServletRequest;
45 import java.util.Collections;
46
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.assertj.core.api.Assertions.catchThrowable;
49 import static org.mockito.Mockito.when;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class RoleAuthorizationHandlerTest {
53
54     private RoleAuthorizationHandler roleAuthorizationHandler = new RoleAuthorizationHandler();
55     @Mock
56     JoinPoint joinPoint;
57     @Mock
58     Signature signature;
59     @Mock
60     BeGenericServlet beGenericServlet;
61     @Mock
62     HttpServletRequest httpServletRequest;
63
64
65     private static ConfigurationSource configurationSource = new FSConfigurationSource(
66             ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be/auth");
67     static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
68
69     @Before
70     public void setUp() {
71         when(joinPoint.getSignature()).thenReturn(signature);
72         when(signature.toShortString()).thenReturn("methodName");
73         when(joinPoint.getThis()).thenReturn(beGenericServlet);
74         when(beGenericServlet.getServletRequest()).thenReturn(httpServletRequest);
75         ThreadLocalsHolder.setApiType(FilterDecisionEnum.EXTERNAL);
76     }
77
78     @Test
79     public void testAuthorizeRoleOnePermittedRole() {
80         String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE};
81         PermissionAllowed rolesAllowed =
82                 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
83         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
84                 .thenReturn(true);
85         roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
86     }
87
88     @Test
89     public void testAuthorizeRoleTwoPermittedRole() {
90         String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE, AafPermission.PermNames.READ_VALUE};
91         PermissionAllowed rolesAllowed =
92                 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
93         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
94                 .thenReturn(true);
95         roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed);
96     }
97
98     @Test
99     public void testAuthorizeRoleNonPermittedRole() {
100         String[] permsAllowed = {AafPermission.PermNames.WRITE_VALUE, AafPermission.PermNames.READ_VALUE};
101         PermissionAllowed rolesAllowed =
102                 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
103         when(httpServletRequest.isUserInRole(AafPermission.getEnumByString(permsAllowed[0]).getFullPermission()))
104                 .thenReturn(false);
105
106         ComponentException thrown = (ComponentException) catchThrowable(()->roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
107         assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
108     }
109
110     @Test
111     public void testAuthorizeRoleEmptyRole() {
112         String[] permsAllowed = {};
113         PermissionAllowed rolesAllowed =
114                 (PermissionAllowed) AnnotationParser.annotationForMap(PermissionAllowed.class, Collections.singletonMap("value", permsAllowed));
115
116         ComponentException thrown = (ComponentException) catchThrowable(()->roleAuthorizationHandler.authorizeRole(joinPoint, rolesAllowed));
117         assertThat(thrown.getActionStatus()).isEqualTo(ActionStatus.AUTH_FAILED);
118     }
119 }