cypress test - leave only group1 and other will run in groupRemain
[vid.git] / vid-app-common / src / test / java / org / onap / vid / roles / RoleValidatorByRolesTest.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
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.ImmutableMap;
28 import java.util.List;
29 import java.util.Map;
30 import org.onap.vid.mso.rest.RequestDetails;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
33
34 public class RoleValidatorByRolesTest {
35
36     private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber";
37     private static final String NOT_MATCHING_SUBSCRIBER = "notMatchingSubscriber";
38     private static final String SAMPLE_SERVICE_TYPE = "sampleServiceType";
39     private static final String NOT_MATCHING_TENANT = "notMatchingTenant";
40     private static final String SAMPLE_TENANT = "sampleTenant";
41
42     private static final Role SAMPLE_ROLE = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT);
43
44     private List<Role> roles = ImmutableList.of(SAMPLE_ROLE);
45     private Map<String, Object> subscriberInfo = ImmutableMap.of("globalSubscriberId", SAMPLE_SUBSCRIBER);
46     private Map<String, Object> requestParameters = ImmutableMap.of("subscriptionServiceType", SAMPLE_SERVICE_TYPE);
47     private Map<String, Object> requestDetailsProperties = ImmutableMap.of("subscriberInfo", subscriberInfo, "requestParameters", requestParameters);
48     private RequestDetails requestDetails;
49     private RoleValidatorByRoles roleValidator;
50
51     @BeforeMethod
52     public void setUp() {
53         roleValidator = new RoleValidatorByRoles(roles);
54         requestDetails = new RequestDetails();
55     }
56
57     @Test
58     public void shouldPermitSubscriberWhenNameMatchesAndRolesAreEnabled() {
59         assertThat(roleValidator.isSubscriberPermitted(SAMPLE_SUBSCRIBER)).isTrue();
60     }
61
62     @Test
63     public void shouldNotPermitSubscriberWhenNameNotMatches() {
64         assertThat(roleValidator.isSubscriberPermitted(NOT_MATCHING_SUBSCRIBER)).isFalse();
65     }
66
67     @Test
68     public void shouldPermitServiceWhenNamesMatches() {
69         assertThat(roleValidator.isServicePermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE)).isTrue();
70     }
71
72
73     @Test
74     public void shouldNotPermitServiceWhenSubscriberNameNotMatches() {
75         assertThat(roleValidator.isServicePermitted(NOT_MATCHING_SUBSCRIBER, SAMPLE_SERVICE_TYPE)).isFalse();
76     }
77
78     @Test
79     public void shouldNotPermitServiceWhenServiceTypeNotMatches() {
80         assertThat(roleValidator.isServicePermitted(SAMPLE_SUBSCRIBER, NOT_MATCHING_SUBSCRIBER)).isFalse();
81     }
82
83     @Test
84     public void shouldPermitTenantWhenNameMatches() {
85         assertThat(roleValidator.isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT)).isTrue();
86     }
87
88
89     @Test
90     public void shouldNotPermitTenantWhenNameNotMatches() {
91         assertThat(roleValidator.isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, NOT_MATCHING_TENANT)).isFalse();
92     }
93
94     @Test
95     public void shouldValidateProperlySORequest() {
96         requestDetails.setAdditionalProperty("requestDetails", requestDetailsProperties);
97
98         assertThat(roleValidator.isMsoRequestValid(requestDetails)).isTrue();
99     }
100
101     @Test
102     public void shouldValidateUnknownSORequest() {
103         assertThat(roleValidator.isMsoRequestValid(new RequestDetails())).isTrue();
104     }
105
106     @Test
107     public void shouldRejectSORequestWhenSubscriberNotMatches() {
108         Map<String, Object> subscriberInfo = ImmutableMap.of("globalSubscriberId", "sample");
109         Map<String, Object> requestDetailsProperties = ImmutableMap.of("subscriberInfo", subscriberInfo, "requestParameters", requestParameters);
110         requestDetails.setAdditionalProperty("requestDetails", requestDetailsProperties);
111
112         assertThat(roleValidator.isMsoRequestValid(requestDetails)).isFalse();
113     }
114 }