RoleValidatorByOwningEntity permits by PermissionPropertiesOwningEntity
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / ServicePermissionsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.onap.vid.controller;
22
23 import static java.lang.Boolean.FALSE;
24 import static java.lang.Boolean.TRUE;
25 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.Matchers.is;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31
32 import org.jetbrains.annotations.NotNull;
33 import org.onap.vid.aai.model.Permissions;
34 import org.onap.vid.roles.PermissionPropertiesSubscriberAndServiceType;
35 import org.onap.vid.roles.RoleProvider;
36 import org.onap.vid.roles.RoleValidator;
37 import org.springframework.mock.web.MockHttpServletRequest;
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
40
41 public class ServicePermissionsTest {
42
43     @DataProvider
44     public static Object[][] trueAndFalse() {
45         return new Object[][]{{TRUE}, {FALSE}};
46     }
47
48
49     @Test(dataProvider = "trueAndFalse")
50     public void servicePermissions_responseMatchesMockedRoleValidator(boolean expected) {
51         String subscriberId = randomAlphanumeric(8);
52         String serviceType = randomAlphanumeric(8);
53
54         RoleProvider roleProvider = mock(RoleProvider.class);
55         RoleValidator roleValidator = mock(RoleValidator.class);
56         when(roleProvider.getUserRolesValidator(any())).thenReturn(roleValidator);
57         when(roleValidator.isServicePermitted(new PermissionPropertiesSubscriberAndServiceType(subscriberId, serviceType))).thenReturn(expected);
58
59         AaiController2 aaiController2 = new AaiController2(null, roleProvider, null, null);
60
61         Permissions permissions = aaiController2.servicePermissions(unimportantRequest(), subscriberId, serviceType);
62         assertThat(permissions, is(new Permissions(expected)));
63     }
64
65     @NotNull
66     private MockHttpServletRequest unimportantRequest() {
67         return new MockHttpServletRequest("", "");
68     }
69 }