Merge "Retrieve worfklows by model"
[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 org.jetbrains.annotations.NotNull;
24 import org.onap.vid.aai.model.Permissions;
25 import org.onap.vid.roles.RoleProvider;
26 import org.onap.vid.roles.RoleValidator;
27 import org.springframework.mock.web.MockHttpServletRequest;
28 import org.testng.annotations.DataProvider;
29 import org.testng.annotations.Test;
30
31 import static java.lang.Boolean.FALSE;
32 import static java.lang.Boolean.TRUE;
33 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
34 import static org.hamcrest.MatcherAssert.assertThat;
35 import static org.hamcrest.Matchers.is;
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
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);
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 }