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.mockito.Mockito.when;
26 import static org.mockito.MockitoAnnotations.initMocks;
28 import com.google.common.collect.ImmutableMap;
29 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import org.assertj.core.util.Lists;
33 import org.mockito.Mock;
34 import org.onap.vid.aai.AaiResponse;
35 import org.onap.vid.aai.exceptions.RoleParsingException;
36 import org.onap.vid.model.Subscriber;
37 import org.onap.vid.model.SubscriberList;
38 import org.onap.vid.services.AaiService;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
42 public class RoleProviderTest {
44 private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber";
45 private static final String SAMPLE_CUSTOMER_ID = "sampleCustomerId";
46 private static final String SERVICE_TYPE_LOGS = "LOGS";
47 private static final String TENANT_PERMITTED = "PERMITTED";
48 private static final String SAMPLE_SERVICE = "sampleService";
49 private static final String SAMPLE_TENANT = "sampleTenant";
50 private static final String SAMPLE_ROLE_PREFIX = "prefix";
53 private AaiService aaiService;
56 private HttpServletRequest request;
59 private AaiResponse<SubscriberList> subscriberListResponse;
61 private RoleProvider roleProvider;
67 roleProvider = new RoleProvider(aaiService, httpServletRequest -> 5, httpServletRequest -> createRoles());
71 public void shouldSplitRolesWhenDelimiterIsPresent() {
72 String roles = "role_a___role_b";
74 assertThat(roleProvider.splitRole(roles, "")).isEqualTo(new String[]{"role_a", "role_b"});
79 public void shouldProperlyCreateRoleFromCorrectArray() throws RoleParsingException {
81 String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
83 Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
85 assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
86 assertThat(role.getSubscribeName()).isEqualTo(SAMPLE_CUSTOMER_ID);
87 assertThat(role.getTenant()).isEqualTo(SAMPLE_TENANT);
88 assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
92 public void shouldProperlyCreateRoleWhenTenantIsNotProvided() throws RoleParsingException {
95 String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
97 Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
99 assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
100 assertThat(role.getSubscribeName()).isEqualTo(SAMPLE_CUSTOMER_ID);
101 assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
102 assertThat(role.getTenant()).isNullOrEmpty();
105 @Test(expectedExceptions = RoleParsingException.class)
106 public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
109 roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX);
113 public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() {
114 Role expectedRole = new Role(EcompRole.READ, SAMPLE_CUSTOMER_ID, SAMPLE_SERVICE, SAMPLE_TENANT);
117 List<Role> userRoles = roleProvider.getUserRoles(request);
120 assertThat(userRoles.size()).isEqualTo(1);
121 Role actualRole = userRoles.get(0);
123 assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant());
124 assertThat(actualRole.getSubscribeName()).isEqualTo(expectedRole.getSubscribeName());
125 assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType());
129 public void shouldReturnReadOnlyPermissionWhenRolesAreEmpty() {
130 assertThat(roleProvider.userPermissionIsReadOnly(Lists.emptyList())).isTrue();
134 public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() {
135 assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT)))).isFalse();
139 public void userShouldHavePermissionToReadLogsWhenServiceAndTenantAreCorrect() {
140 Role withoutPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT);
141 Role withPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SERVICE_TYPE_LOGS, TENANT_PERMITTED);
143 assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue();
146 private void setSubscribers() {
147 Subscriber subscriber = new Subscriber();
148 subscriber.subscriberName = SAMPLE_SUBSCRIBER;
149 subscriber.globalCustomerId = SAMPLE_CUSTOMER_ID;
150 SubscriberList subscriberList = new SubscriberList(Lists.list(subscriber));
151 when(aaiService.getFullSubscriberList()).thenReturn(subscriberListResponse);
152 when(subscriberListResponse.getT()).thenReturn(subscriberList);
155 private Map<Long, org.onap.portalsdk.core.domain.Role> createRoles() {
156 org.onap.portalsdk.core.domain.Role role1 = new org.onap.portalsdk.core.domain.Role();
157 role1.setName("read___role2");
158 org.onap.portalsdk.core.domain.Role role2 = new org.onap.portalsdk.core.domain.Role();
159 role2.setName("sampleSubscriber___sampleService___sampleTenant");
160 return ImmutableMap.of(1L, role1, 2L, role2);