ac3da50ab918c1a20077ec4c8abf0338ba2af57f
[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.RoleProvider;
35 import org.onap.vid.roles.RoleValidator;
36 import org.springframework.mock.web.MockHttpServletRequest;
37 import org.testng.annotations.DataProvider;
38 import org.testng.annotations.Test;
39
40 public class ServicePermissionsTest {
41
42     @DataProvider
43     public static Object[][] trueAndFalse() {
44         return new Object[][]{{TRUE}, {FALSE}};
45     }
46
47
48     @Test(dataProvider = "trueAndFalse")
49     public void servicePermissions_responseMatchesMockedRoleValidator(boolean expected) {
50         String subscriberId = randomAlphanumeric(8);
51         String serviceType = randomAlphanumeric(8);
52
53         RoleProvider roleProvider = mock(RoleProvider.class);
54         RoleValidator roleValidator = mock(RoleValidator.class);
55         when(roleProvider.getUserRolesValidator(any())).thenReturn(roleValidator);
56         when(roleValidator.isServicePermitted(subscriberId, serviceType)).thenReturn(expected);
57
58         AaiController2 aaiController2 = new AaiController2(null, roleProvider, null, null);
59
60         Permissions permissions = aaiController2.servicePermissions(unimportantRequest(), subscriberId, serviceType);
61         assertThat(permissions, is(new Permissions(expected)));
62     }
63
64     @NotNull
65     private MockHttpServletRequest unimportantRequest() {
66         return new MockHttpServletRequest("", "");
67     }
68 }