Replaced tests for roles
[vid.git] / vid-app-common / src / test / java / org / onap / vid / roles / RoleProviderTest.java
1 package org.onap.vid.roles;
2
3
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;
14
15 import javax.servlet.http.HttpServletRequest;
16 import java.util.List;
17 import java.util.Map;
18
19 import static org.assertj.core.api.Assertions.assertThat;
20 import static org.mockito.Mockito.when;
21 import static org.mockito.MockitoAnnotations.initMocks;
22
23 public class RoleProviderTest {
24
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";
32
33     @Mock
34     private AaiService aaiService;
35
36     @Mock
37     private HttpServletRequest request;
38
39     @Mock
40     private HttpResponse<SubscriberList> subscriberListHttpResponse;
41
42
43     private RoleProvider roleProvider;
44
45
46     @BeforeMethod
47     public void setUp() {
48         initMocks(this);
49         roleProvider = new RoleProvider(aaiService, httpServletRequest -> 5, httpServletRequest -> createRoles());
50     }
51
52     @Test
53     public void shouldSplitRolesWhenDelimiterIsPresent() {
54         String roles = "role_a___role_b";
55
56         assertThat(roleProvider.splitRole(roles, "")).isEqualTo(new String[]{"role_a", "role_b"});
57     }
58
59
60     @Test
61     public void shouldProperlyCreateRoleFromCorrectArray() throws RoleParsingException {
62         setSubscribers();
63         String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
64
65         Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
66
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);
71     }
72
73     @Test
74     public void shouldProperlyCreateRoleWhenTenantIsNotProvided() throws RoleParsingException {
75         setSubscribers();
76
77         String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
78
79         Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
80
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();
85     }
86
87     @Test(expectedExceptions = RoleParsingException.class)
88     public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
89         setSubscribers();
90
91         roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX);
92     }
93
94     @Test
95     public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() {
96         Role expectedRole = new Role(EcompRole.READ, SAMPLE_CUSTOMER_ID, SAMPLE_SERVICE, SAMPLE_TENANT);
97         setSubscribers();
98
99         List<Role> userRoles = roleProvider.getUserRoles(request);
100
101
102         assertThat(userRoles.size()).isEqualTo(1);
103         Role actualRole = userRoles.get(0);
104
105         assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant());
106         assertThat(actualRole.getSubscribeName()).isEqualTo(expectedRole.getSubscribeName());
107         assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType());
108     }
109
110     @Test
111     public void shouldReturnReadOnlyPermissionWhenRolesAreEmpty() {
112         assertThat(roleProvider.userPermissionIsReadOnly(Lists.emptyList())).isTrue();
113     }
114
115     @Test
116     public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() {
117         assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT)))).isFalse();
118     }
119
120     @Test
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);
124
125         assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue();
126     }
127
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);
135     }
136
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);
143     }
144 }