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 java.util.Collections.emptyMap;
25 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
29 import static org.mockito.MockitoAnnotations.initMocks;
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.google.common.collect.ImmutableMap;
34 import java.util.List;
36 import javax.servlet.http.HttpServletRequest;
37 import org.assertj.core.util.Lists;
38 import org.mockito.Mock;
39 import org.onap.vid.aai.AaiResponse;
40 import org.onap.vid.aai.exceptions.RoleParsingException;
41 import org.onap.vid.category.CategoryParametersResponse;
42 import org.onap.vid.model.CategoryParameter.Family;
43 import org.onap.vid.model.Subscriber;
44 import org.onap.vid.model.SubscriberList;
45 import org.onap.vid.services.AaiService;
46 import org.onap.vid.services.CategoryParameterService;
47 import org.testng.Assert;
48 import org.testng.annotations.BeforeMethod;
49 import org.testng.annotations.DataProvider;
50 import org.testng.annotations.Test;
52 public class RoleProviderTest {
54 private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber";
55 private static final String SAMPLE_SUBSCRIBER_ID = "subscriberId";
56 private static final String SERVICE_TYPE_LOGS = "LOGS";
57 private static final String TENANT_PERMITTED = "PERMITTED";
58 private static final String SAMPLE_SERVICE = "sampleService";
59 private static final String SAMPLE_TENANT = "sampleTenant";
60 private static final String SAMPLE_ROLE_PREFIX = "prefix";
61 private static final String EXISTING_OWNING_ENTITY_NAME = "WayneHolland";
62 private static final String EXISTING_OWNING_ENTITY_ID = "d61e6f2d-12fa-4cc2-91df-7c244011d6fc";
63 private static final String NOT_EXISTING_OWNING_ENTITY_NAME = "notExistingOwningEntity";
66 private AaiService aaiService;
69 private HttpServletRequest request;
72 private AaiResponse<SubscriberList> subscriberListResponse;
75 private RoleValidatorFactory roleValidatorFactory;
78 private CategoryParameterService categoryParameterService;
80 private RoleProvider roleProvider;
86 roleProvider = new RoleProvider(aaiService, roleValidatorFactory, httpServletRequest -> 5,
87 httpServletRequest -> createRoles(),
88 categoryParameterService);
90 when(categoryParameterService.getCategoryParameters(any()))
91 .thenReturn(new CategoryParametersResponse(emptyMap()));
95 public void shouldSplitRolesWhenDelimiterIsPresent() {
96 String roles = "role_a___role_b";
98 assertThat(roleProvider.splitRole(roles, "")).isEqualTo(new String[]{"role_a", "role_b"});
103 public void shouldProperlyCreateRoleFromCorrectArray() throws RoleParsingException {
105 String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
107 Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX, emptyMap());
109 assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
110 assertThat(role.getSubscriberId()).isEqualTo(SAMPLE_SUBSCRIBER_ID);
111 assertThat(role.getTenant()).isEqualTo(SAMPLE_TENANT);
112 assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
116 public void shouldProperlyCreateRoleWhenTenantIsNotProvided() throws RoleParsingException {
119 String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
121 Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX, emptyMap());
123 assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
124 assertThat(role.getSubscriberId()).isEqualTo(SAMPLE_SUBSCRIBER_ID);
125 assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
126 assertThat(role.getTenant()).isNullOrEmpty();
129 @Test(expectedExceptions = RoleParsingException.class)
130 public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
133 roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX, emptyMap());
137 public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() {
138 Role expectedRole = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER_ID, SAMPLE_SERVICE, SAMPLE_TENANT, owningEntityId());
141 List<Role> userRoles = roleProvider.getUserRoles(request);
144 assertThat(userRoles.size()).isEqualTo(1);
145 Role actualRole = userRoles.get(0);
147 assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant());
148 assertThat(actualRole.getSubscriberId()).isEqualTo(expectedRole.getSubscriberId());
149 assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType());
153 public void shouldReturnReadOnlyPermissionWhenRolesAreEmpty() {
154 assertThat(roleProvider.userPermissionIsReadOnly(Lists.emptyList())).isTrue();
158 public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() {
159 assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(
160 EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT, owningEntityId())))).isFalse();
164 public void userShouldHavePermissionToReadLogsWhenServiceAndTenantAreCorrect() {
165 Role withoutPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT, owningEntityId());
166 Role withPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SERVICE_TYPE_LOGS, TENANT_PERMITTED, owningEntityId());
168 assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue();
172 public void getUserRolesValidator_shouldReturnValidatorFromFactory() {
173 RoleValidator expectedRoleValidator = new AlwaysValidRoleValidator();
174 when(roleValidatorFactory.by(any())).thenReturn(expectedRoleValidator);
176 RoleValidator result = roleProvider.getUserRolesValidator(request);
178 assertThat(result).isEqualTo(expectedRoleValidator);
182 public static Object[][] owningEntityNameAndId() {
183 return new Object[][] {
184 {"owning entity name exist on the response, id is returned ", EXISTING_OWNING_ENTITY_NAME, EXISTING_OWNING_ENTITY_ID},
185 {"owning entity name dont exist on the response, name is returned", NOT_EXISTING_OWNING_ENTITY_NAME, NOT_EXISTING_OWNING_ENTITY_NAME},
189 @Test(dataProvider = "owningEntityNameAndId")
190 public void translateOwningEntityNameToOwningEntityId_shouldTranslateNameToId(String description,
191 String owningEntityName, String expectedId) {
192 String owningEntityId = roleProvider.translateOwningEntityNameToOwningEntityId(owningEntityName,
194 EXISTING_OWNING_ENTITY_NAME, EXISTING_OWNING_ENTITY_ID,
198 Assert.assertEquals(owningEntityId, expectedId);
203 private String owningEntityId() {
204 return EXISTING_OWNING_ENTITY_ID;
207 private void setSubscribers() {
208 Subscriber subscriber = new Subscriber();
209 subscriber.subscriberName = SAMPLE_SUBSCRIBER;
210 subscriber.globalCustomerId = SAMPLE_SUBSCRIBER_ID;
211 SubscriberList subscriberList = new SubscriberList(Lists.list(subscriber));
212 when(aaiService.getFullSubscriberList()).thenReturn(subscriberListResponse);
213 when(subscriberListResponse.getT()).thenReturn(subscriberList);
216 private Map<Long, org.onap.portalsdk.core.domain.Role> createRoles() {
217 org.onap.portalsdk.core.domain.Role role1 = new org.onap.portalsdk.core.domain.Role();
218 role1.setName("read___role2");
219 org.onap.portalsdk.core.domain.Role role2 = new org.onap.portalsdk.core.domain.Role();
220 role2.setName("sampleSubscriber___sampleService___sampleTenant");
221 return ImmutableMap.of(1L, role1, 2L, role2);
225 public void owningEntityNameToOwningEntityIdMapper_readsOwningEntityCorrectly() throws JsonProcessingException {
227 final String categoryParametersResponse = ""
229 + " \"categoryParameters\": { "
230 + " \"lineOfBusiness\": [ "
231 + " { \"id\": \"ONAP\", \"name\": \"ONAP\" }, "
232 + " { \"id\": \"zzz1\", \"name\": \"zzz1\" } "
234 + " \"owningEntity\": [ "
235 + " { \"id\": \"aaa1\", \"name\": \"aaa1\" }, "
236 + " { \"id\": \"" + EXISTING_OWNING_ENTITY_ID + "\", \"name\": \"" + EXISTING_OWNING_ENTITY_NAME + "\" }, "
237 + " { \"id\": \"Melissa\", \"name\": \"Melissa\" } ], "
239 + " { \"id\": \"WATKINS\", \"name\": \"WATKINS\" }, "
240 + " { \"id\": \"x1\", \"name\": \"x1\" }, "
241 + " { \"id\": \"yyy1\", \"name\": \"yyy1\" } "
243 + " \"platform\": [ "
244 + " { \"id\": \"platform\", \"name\": \"platform\" }, "
245 + " { \"id\": \"xxx1\", \"name\": \"xxx1\" } "
250 CategoryParametersResponse categoryParameterResponse =
251 new ObjectMapper().readValue(categoryParametersResponse, CategoryParametersResponse.class);
253 when(categoryParameterService.getCategoryParameters(Family.PARAMETER_STANDARDIZATION))
254 .thenReturn(categoryParameterResponse);
256 org.hamcrest.MatcherAssert.assertThat(roleProvider.owningEntityNameToOwningEntityIdMapper(),
257 jsonEquals(ImmutableMap.of(
259 "Melissa", "Melissa",
260 EXISTING_OWNING_ENTITY_NAME, EXISTING_OWNING_ENTITY_ID