create RoleValidatorFactory component.
[vid.git] / vid-app-common / src / test / java / org / onap / vid / roles / RoleProviderTest.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.roles;
22
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.MockitoAnnotations.initMocks;
29
30 import com.google.common.collect.ImmutableMap;
31 import java.util.List;
32 import java.util.Map;
33 import javax.servlet.http.HttpServletRequest;
34 import org.assertj.core.util.Lists;
35 import org.hamcrest.CoreMatchers;
36 import org.mockito.Mock;
37 import org.onap.vid.aai.AaiResponse;
38 import org.onap.vid.aai.exceptions.RoleParsingException;
39 import org.onap.vid.model.Subscriber;
40 import org.onap.vid.model.SubscriberList;
41 import org.onap.vid.services.AaiService;
42 import org.testng.annotations.BeforeMethod;
43 import org.testng.annotations.Test;
44
45 public class RoleProviderTest {
46
47     private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber";
48     private static final String SAMPLE_SUBSCRIBER_ID = "subscriberId";
49     private static final String SERVICE_TYPE_LOGS = "LOGS";
50     private static final String TENANT_PERMITTED = "PERMITTED";
51     private static final String SAMPLE_SERVICE = "sampleService";
52     private static final String SAMPLE_TENANT = "sampleTenant";
53     private static final String SAMPLE_ROLE_PREFIX = "prefix";
54
55     @Mock
56     private AaiService aaiService;
57
58     @Mock
59     private HttpServletRequest request;
60
61     @Mock
62     private AaiResponse<SubscriberList> subscriberListResponse;
63
64     @Mock
65     private RoleValidatorFactory roleValidatorFactory;
66
67     private RoleProvider roleProvider;
68
69
70     @BeforeMethod
71     public void setUp() {
72         initMocks(this);
73         roleProvider = new RoleProvider(aaiService, roleValidatorFactory, httpServletRequest -> 5, httpServletRequest -> createRoles());
74     }
75
76     @Test
77     public void shouldSplitRolesWhenDelimiterIsPresent() {
78         String roles = "role_a___role_b";
79
80         assertThat(roleProvider.splitRole(roles, "")).isEqualTo(new String[]{"role_a", "role_b"});
81     }
82
83
84     @Test
85     public void shouldProperlyCreateRoleFromCorrectArray() throws RoleParsingException {
86         setSubscribers();
87         String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
88
89         Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
90
91         assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
92         assertThat(role.getSubscriberId()).isEqualTo(SAMPLE_SUBSCRIBER_ID);
93         assertThat(role.getTenant()).isEqualTo(SAMPLE_TENANT);
94         assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
95     }
96
97     @Test
98     public void shouldProperlyCreateRoleWhenTenantIsNotProvided() throws RoleParsingException {
99         setSubscribers();
100
101         String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
102
103         Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
104
105         assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
106         assertThat(role.getSubscriberId()).isEqualTo(SAMPLE_SUBSCRIBER_ID);
107         assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
108         assertThat(role.getTenant()).isNullOrEmpty();
109     }
110
111     @Test(expectedExceptions = RoleParsingException.class)
112     public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
113         setSubscribers();
114
115         roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX);
116     }
117
118     @Test
119     public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() {
120         Role expectedRole = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER_ID, SAMPLE_SERVICE, SAMPLE_TENANT);
121         setSubscribers();
122
123         List<Role> userRoles = roleProvider.getUserRoles(request);
124
125
126         assertThat(userRoles.size()).isEqualTo(1);
127         Role actualRole = userRoles.get(0);
128
129         assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant());
130         assertThat(actualRole.getSubscriberId()).isEqualTo(expectedRole.getSubscriberId());
131         assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType());
132     }
133
134     @Test
135     public void shouldReturnReadOnlyPermissionWhenRolesAreEmpty() {
136         assertThat(roleProvider.userPermissionIsReadOnly(Lists.emptyList())).isTrue();
137     }
138
139     @Test
140     public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() {
141         assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT)))).isFalse();
142     }
143
144     @Test
145     public void userShouldHavePermissionToReadLogsWhenServiceAndTenantAreCorrect() {
146         Role withoutPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT);
147         Role withPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SERVICE_TYPE_LOGS, TENANT_PERMITTED);
148
149         assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue();
150     }
151
152     @Test
153     public void getUserRolesValidator_shouldReturnValidatorFromFactory() {
154         RoleValidator expectedRoleValidator = new AlwaysValidRoleValidator();
155         when(roleValidatorFactory.by(any())).thenReturn(expectedRoleValidator);
156
157         RoleValidator result = roleProvider.getUserRolesValidator(request);
158
159         assertThat(result).isEqualTo(expectedRoleValidator);
160     }
161
162     private void setSubscribers() {
163         Subscriber subscriber = new Subscriber();
164         subscriber.subscriberName = SAMPLE_SUBSCRIBER;
165         subscriber.globalCustomerId = SAMPLE_SUBSCRIBER_ID;
166         SubscriberList subscriberList = new SubscriberList(Lists.list(subscriber));
167         when(aaiService.getFullSubscriberList()).thenReturn(subscriberListResponse);
168         when(subscriberListResponse.getT()).thenReturn(subscriberList);
169     }
170
171     private Map<Long, org.onap.portalsdk.core.domain.Role> createRoles() {
172         org.onap.portalsdk.core.domain.Role role1 = new org.onap.portalsdk.core.domain.Role();
173         role1.setName("read___role2");
174         org.onap.portalsdk.core.domain.Role role2 = new org.onap.portalsdk.core.domain.Role();
175         role2.setName("sampleSubscriber___sampleService___sampleTenant");
176         return ImmutableMap.of(1L, role1, 2L, role2);
177     }
178 }