2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.roles;
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;
30 import com.google.common.collect.ImmutableMap;
31 import java.util.List;
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;
45 public class RoleProviderTest {
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";
56 private AaiService aaiService;
59 private HttpServletRequest request;
62 private AaiResponse<SubscriberList> subscriberListResponse;
65 private RoleValidatorFactory roleValidatorFactory;
67 private RoleProvider roleProvider;
73 roleProvider = new RoleProvider(aaiService, roleValidatorFactory, httpServletRequest -> 5, httpServletRequest -> createRoles());
77 public void shouldSplitRolesWhenDelimiterIsPresent() {
78 String roles = "role_a___role_b";
80 assertThat(roleProvider.splitRole(roles, "")).isEqualTo(new String[]{"role_a", "role_b"});
85 public void shouldProperlyCreateRoleFromCorrectArray() throws RoleParsingException {
87 String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
89 Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
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);
98 public void shouldProperlyCreateRoleWhenTenantIsNotProvided() throws RoleParsingException {
101 String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
103 Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
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();
111 @Test(expectedExceptions = RoleParsingException.class)
112 public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
115 roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX);
119 public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() {
120 Role expectedRole = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER_ID, SAMPLE_SERVICE, SAMPLE_TENANT);
123 List<Role> userRoles = roleProvider.getUserRoles(request);
126 assertThat(userRoles.size()).isEqualTo(1);
127 Role actualRole = userRoles.get(0);
129 assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant());
130 assertThat(actualRole.getSubscriberId()).isEqualTo(expectedRole.getSubscriberId());
131 assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType());
135 public void shouldReturnReadOnlyPermissionWhenRolesAreEmpty() {
136 assertThat(roleProvider.userPermissionIsReadOnly(Lists.emptyList())).isTrue();
140 public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() {
141 assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT)))).isFalse();
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);
149 assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue();
153 public void getUserRolesValidator_shouldReturnValidatorFromFactory() {
154 RoleValidator expectedRoleValidator = new AlwaysValidRoleValidator();
155 when(roleValidatorFactory.by(any())).thenReturn(expectedRoleValidator);
157 RoleValidator result = roleProvider.getUserRolesValidator(request);
159 assertThat(result).isEqualTo(expectedRoleValidator);
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);
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);