c1033d2d042c44b89d6f0a642c5dcb69aa2ec4a5
[vid.git] / vid-app-common / src / test / java / org / onap / vid / roles / RoleProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.roles;
22
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.when;
26 import static org.mockito.MockitoAnnotations.initMocks;
27
28 import com.google.common.collect.ImmutableMap;
29 import java.util.List;
30 import java.util.Map;
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;
41
42 public class RoleProviderTest {
43
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";
51
52     @Mock
53     private AaiService aaiService;
54
55     @Mock
56     private HttpServletRequest request;
57
58     @Mock
59     private AaiResponse<SubscriberList> subscriberListResponse;
60
61     private RoleProvider roleProvider;
62
63
64     @BeforeMethod
65     public void setUp() {
66         initMocks(this);
67         roleProvider = new RoleProvider(aaiService, httpServletRequest -> 5, httpServletRequest -> createRoles());
68     }
69
70     @Test
71     public void shouldSplitRolesWhenDelimiterIsPresent() {
72         String roles = "role_a___role_b";
73
74         assertThat(roleProvider.splitRole(roles, "")).isEqualTo(new String[]{"role_a", "role_b"});
75     }
76
77
78     @Test
79     public void shouldProperlyCreateRoleFromCorrectArray() throws RoleParsingException {
80         setSubscribers();
81         String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT};
82
83         Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
84
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);
89     }
90
91     @Test
92     public void shouldProperlyCreateRoleWhenTenantIsNotProvided() throws RoleParsingException {
93         setSubscribers();
94
95         String[] roleParts = {SAMPLE_SUBSCRIBER, SAMPLE_SERVICE};
96
97         Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX);
98
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();
103     }
104
105     @Test(expectedExceptions = RoleParsingException.class)
106     public void shouldRaiseExceptionWhenRolePartsAreIncomplete() throws RoleParsingException {
107         setSubscribers();
108
109         roleProvider.createRoleFromStringArr(new String[]{SAMPLE_SUBSCRIBER}, SAMPLE_ROLE_PREFIX);
110     }
111
112     @Test
113     public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() {
114         Role expectedRole = new Role(EcompRole.READ, SAMPLE_CUSTOMER_ID, SAMPLE_SERVICE, SAMPLE_TENANT);
115         setSubscribers();
116
117         List<Role> userRoles = roleProvider.getUserRoles(request);
118
119
120         assertThat(userRoles.size()).isEqualTo(1);
121         Role actualRole = userRoles.get(0);
122
123         assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant());
124         assertThat(actualRole.getSubscribeName()).isEqualTo(expectedRole.getSubscribeName());
125         assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType());
126     }
127
128     @Test
129     public void shouldReturnReadOnlyPermissionWhenRolesAreEmpty() {
130         assertThat(roleProvider.userPermissionIsReadOnly(Lists.emptyList())).isTrue();
131     }
132
133     @Test
134     public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() {
135         assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT)))).isFalse();
136     }
137
138     @Test
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);
142
143         assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue();
144     }
145
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);
153     }
154
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);
161     }
162 }