RoleValidatorsComposer composite several RoleValidators together
[vid.git] / vid-app-common / src / test / java / org / onap / vid / roles / RoleValidatorsComposerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2020 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 import static java.util.stream.Collectors.toList;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verifyZeroInteractions;
28 import static org.mockito.Mockito.withSettings;
29
30 import com.google.common.collect.ImmutableSet;
31 import java.util.function.Function;
32 import java.util.stream.Stream;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36
37 public class RoleValidatorsComposerTest {
38
39     private RoleValidator alwaysTrueRoles;
40     private RoleValidator alwaysFalseRoles;
41
42     @DataProvider
43     public static Object[][] allInterfaceFunctions() {
44         return Stream.<Function<RoleValidator, Boolean>>of(
45             (RoleValidator o) -> o.isSubscriberPermitted("subscriberId"),
46             (RoleValidator o) -> o.isServicePermitted(new PermissionProperties("subscriberId", "serviceType")),
47             (RoleValidator o) -> o.isTenantPermitted("subscriberId", "serviceType", "tenantName")
48         ).map(it -> new Object[]{it}).collect(toList()).toArray(new Object[][]{});
49     }
50
51     @BeforeMethod
52     public void setUp() {
53         alwaysTrueRoles = mock(RoleValidator.class, withSettings().defaultAnswer(o -> true));
54         alwaysFalseRoles = mock(RoleValidator.class);
55     }
56
57     @Test(dataProvider = "allInterfaceFunctions")
58     public void emptyComposite_returnsFalse(Function<RoleValidator, Boolean> interfaceFunction) {
59         RoleValidatorsComposer underTest = new RoleValidatorsComposer();
60
61         assertThat(
62             interfaceFunction.apply(underTest),
63             is(false)
64         );
65
66     }
67
68     @Test(dataProvider = "allInterfaceFunctions")
69     public void falseAndTrueComposite_returnsTrue(Function<RoleValidator, Boolean> interfaceFunction) {
70         RoleValidatorsComposer underTest =
71             new RoleValidatorsComposer(alwaysFalseRoles, alwaysFalseRoles, alwaysTrueRoles);
72
73         assertThat(
74             interfaceFunction.apply(underTest),
75             is(true)
76         );
77     }
78
79     @Test(dataProvider = "allInterfaceFunctions")
80     public void trueAndFalseComposite_returnsTrueAndShortCircuits(Function<RoleValidator, Boolean> interfaceFunction) {
81         RoleValidatorsComposer underTest = new RoleValidatorsComposer(alwaysTrueRoles, alwaysFalseRoles);
82
83         assertThat(
84             interfaceFunction.apply(underTest),
85             is(true)
86         );
87
88         verifyZeroInteractions(alwaysFalseRoles);
89     }
90
91     @Test(dataProvider = "allInterfaceFunctions")
92     public void falseAndFalseComposite_returnsFalse(Function<RoleValidator, Boolean> interfaceFunction) {
93         RoleValidatorsComposer underTest = new RoleValidatorsComposer(alwaysFalseRoles, alwaysFalseRoles);
94         
95         assertThat(
96             interfaceFunction.apply(underTest),
97             is(false)
98         );
99     }
100
101     @Test
102     public void secondaryConstructor_givenSetIfValidators_returnsTrue() {
103         RoleValidatorsComposer underTest = new RoleValidatorsComposer(
104             ImmutableSet.of(alwaysTrueRoles)
105         );
106
107         assertThat(underTest.isSubscriberPermitted("anything"), is(true));
108     }
109
110 }