1 package org.onap.vid.roles;
4 import com.google.common.collect.ImmutableMap;
5 import io.joshworks.restclient.http.HttpResponse;
6 import org.assertj.core.util.Lists;
7 import org.mockito.Mock;
8 import org.onap.vid.aai.exceptions.RoleParsingException;
9 import org.onap.vid.model.Subscriber;
10 import org.onap.vid.model.SubscriberList;
11 import org.onap.vid.services.AaiService;
12 import org.testng.annotations.BeforeMethod;
13 import org.testng.annotations.Test;
15 import javax.servlet.http.HttpServletRequest;
16 import java.util.List;
19 import static org.assertj.core.api.Assertions.assertThat;
20 import static org.mockito.Mockito.when;
21 import static org.mockito.MockitoAnnotations.initMocks;
23 public class RoleProviderTest {
25 private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber";
26 private static final String SAMPLE_CUSTOMER_ID = "sampleCustomerId";
27 private static final String SERVICE_TYPE_LOGS = "LOGS";
28 private static final String TENANT_PERMITTED = "PERMITTED";
29 private static final String SAMPLE_SERVICE = "sampleService";
30 private static final String SAMPLE_TENANT = "sampleTenant";
31 private static final String SAMPLE_ROLE_PREFIX = "prefix";
34 private AaiService aaiService;
37 private HttpServletRequest request;
40 private HttpResponse<SubscriberList> subscriberListHttpResponse;
43 private RoleProvider roleProvider;
49 roleProvider = new RoleProvider(aaiService, httpServletRequest -> 5, httpServletRequest -> createRoles());
53 public void shouldSplitRolesWhenDelimiterIsPresent() {
54 String roles = "role_a___role_b";
56 assertThat(roleProvider.splitRole(roles, "")).isEqualTo(new String[]{"role_a", "role_b"});
61 public void shouldProperlyCreateRoleFromCorrectArray() throws RoleParsingException {
63 String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
65 Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
67 assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
68 assertThat(role.getSubscribeName()).isEqualTo(SAMPLE_CUSTOMER_ID);
69 assertThat(role.getTenant()).isEqualTo(SAMPLE_TENANT);
70 assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
74 public void shouldProperlyCreateRoleWhenTenantIsNotProvided() throws RoleParsingException {
77 String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
79 Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
81 assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ);
82 assertThat(role.getSubscribeName()).isEqualTo(SAMPLE_CUSTOMER_ID);
83 assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE);
84 assertThat(role.getTenant()).isNullOrEmpty();
87 @Test(expectedExceptions = RoleParsingException.class)
88 public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
91 roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX);
95 public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() {
96 Role expectedRole = new Role(EcompRole.READ, SAMPLE_CUSTOMER_ID, SAMPLE_SERVICE, SAMPLE_TENANT);
99 List<Role> userRoles = roleProvider.getUserRoles(request);
102 assertThat(userRoles.size()).isEqualTo(1);
103 Role actualRole = userRoles.get(0);
105 assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant());
106 assertThat(actualRole.getSubscribeName()).isEqualTo(expectedRole.getSubscribeName());
107 assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType());
111 public void shouldReturnReadOnlyPermissionWhenRolesAreEmpty() {
112 assertThat(roleProvider.userPermissionIsReadOnly(Lists.emptyList())).isTrue();
116 public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() {
117 assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT)))).isFalse();
121 public void userShouldHavePermissionToReadLogsWhenServiceAndTenantAreCorrect() {
122 Role withoutPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT);
123 Role withPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SERVICE_TYPE_LOGS, TENANT_PERMITTED);
125 assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue();
128 private void setSubscribers() {
129 Subscriber subscriber = new Subscriber();
130 subscriber.subscriberName = SAMPLE_SUBSCRIBER;
131 subscriber.globalCustomerId = SAMPLE_CUSTOMER_ID;
132 SubscriberList subscriberList = new SubscriberList(Lists.list(subscriber));
133 when(aaiService.getFullSubscriberList()).thenReturn(subscriberListHttpResponse);
134 when(subscriberListHttpResponse.getBody()).thenReturn(subscriberList);
137 private Map<Long, org.onap.portalsdk.core.domain.Role> createRoles() {
138 org.onap.portalsdk.core.domain.Role role1 = new org.onap.portalsdk.core.domain.Role();
139 role1.setName("read___role2");
140 org.onap.portalsdk.core.domain.Role role2 = new org.onap.portalsdk.core.domain.Role();
141 role2.setName("sampleSubscriber___sampleService___sampleTenant");
142 return ImmutableMap.of(1L, role1, 2L, role2);