Merge "Add AsyncInstantiationController tests"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / roles / RoleValidatorBySubscriberAndServiceTypeTest.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 java.util.List;
28 import org.testng.annotations.BeforeMethod;
29 import org.testng.annotations.Test;
30
31 public class RoleValidatorBySubscriberAndServiceTypeTest {
32
33     private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber";
34     private static final String NOT_MATCHING_SUBSCRIBER = "notMatchingSubscriber";
35     private static final String SAMPLE_SERVICE_TYPE = "sampleServiceType";
36     private static final String NOT_MATCHING_TENANT = "notMatchingTenant";
37     private static final String SAMPLE_TENANT = "sampleTenant";
38     private static final String SOME_OWNING_ENTITY_ID = "someOwningEntityId";
39
40     private static final Role SAMPLE_ROLE = new Role(
41         EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT, SOME_OWNING_ENTITY_ID);
42
43     private List<Role> roles = ImmutableList.of(SAMPLE_ROLE);
44     private RoleValidatorBySubscriberAndServiceType roleValidatorBySubscriberAndServiceType;
45
46     @BeforeMethod
47     public void setUp() {
48         roleValidatorBySubscriberAndServiceType = new RoleValidatorBySubscriberAndServiceType(roles);
49     }
50
51     @Test
52     public void shouldPermitSubscriberWhenNameMatchesAndRolesAreEnabled() {
53         assertThat(roleValidatorBySubscriberAndServiceType.isSubscriberPermitted(SAMPLE_SUBSCRIBER)).isTrue();
54     }
55
56     @Test
57     public void shouldNotPermitSubscriberWhenNameNotMatches() {
58         assertThat(roleValidatorBySubscriberAndServiceType.isSubscriberPermitted(NOT_MATCHING_SUBSCRIBER)).isFalse();
59     }
60
61     @Test
62     public void shouldPermitServiceWhenNamesMatches() {
63         assertThat(roleValidatorBySubscriberAndServiceType.isServicePermitted(
64             new PermissionPropertiesSubscriberAndServiceType(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE))).isTrue();
65     }
66
67     @Test
68     public void isServicePermitted_serviceWithAllPermissionProperties_isPermitted() {
69         assertThat(roleValidatorBySubscriberAndServiceType.isServicePermitted(
70             new AllPermissionProperties(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SOME_OWNING_ENTITY_ID))).isTrue();
71     }
72
73     @Test
74     public void shouldNotPermitServiceWhenSubscriberNameNotMatches() {
75         assertThat(
76             roleValidatorBySubscriberAndServiceType.isServicePermitted(
77                 new PermissionPropertiesSubscriberAndServiceType(NOT_MATCHING_SUBSCRIBER, SAMPLE_SERVICE_TYPE))).isFalse();
78     }
79
80     @Test
81     public void shouldNotPermitServiceWhenServiceTypeNotMatches() {
82         assertThat(roleValidatorBySubscriberAndServiceType.isServicePermitted(
83             new PermissionPropertiesSubscriberAndServiceType(SAMPLE_SUBSCRIBER, NOT_MATCHING_SUBSCRIBER))).isFalse();
84     }
85
86     @Test
87     public void isServicePermitted_owningEntityPermissionProperties_isNotPermitted() {
88         assertThat(roleValidatorBySubscriberAndServiceType.isServicePermitted(
89             new PermissionPropertiesOwningEntity(SAMPLE_SUBSCRIBER))).isFalse();
90     }
91
92     @Test
93     public void shouldPermitTenantWhenNameMatches() {
94         assertThat(roleValidatorBySubscriberAndServiceType
95             .isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT)).isTrue();
96     }
97
98     @Test
99     public void shouldPermitTenantWhenNameMatchesCaseInsensitive() {
100         assertThat(roleValidatorBySubscriberAndServiceType
101             .isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT.toUpperCase())).isTrue();
102     }
103
104
105     @Test
106     public void shouldNotPermitTenantWhenNameNotMatches() {
107         assertThat(roleValidatorBySubscriberAndServiceType
108             .isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, NOT_MATCHING_TENANT)).isFalse();
109     }
110
111 }