2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.roles;
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;
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;
37 public class RoleValidatorsComposerTest {
39 private RoleValidator alwaysTrueRoles;
40 private RoleValidator alwaysFalseRoles;
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[][]{});
53 alwaysTrueRoles = mock(RoleValidator.class, withSettings().defaultAnswer(o -> true));
54 alwaysFalseRoles = mock(RoleValidator.class);
57 @Test(dataProvider = "allInterfaceFunctions")
58 public void emptyComposite_returnsFalse(Function<RoleValidator, Boolean> interfaceFunction) {
59 RoleValidatorsComposer underTest = new RoleValidatorsComposer();
62 interfaceFunction.apply(underTest),
68 @Test(dataProvider = "allInterfaceFunctions")
69 public void falseAndTrueComposite_returnsTrue(Function<RoleValidator, Boolean> interfaceFunction) {
70 RoleValidatorsComposer underTest =
71 new RoleValidatorsComposer(alwaysFalseRoles, alwaysFalseRoles, alwaysTrueRoles);
74 interfaceFunction.apply(underTest),
79 @Test(dataProvider = "allInterfaceFunctions")
80 public void trueAndFalseComposite_returnsTrueAndShortCircuits(Function<RoleValidator, Boolean> interfaceFunction) {
81 RoleValidatorsComposer underTest = new RoleValidatorsComposer(alwaysTrueRoles, alwaysFalseRoles);
84 interfaceFunction.apply(underTest),
88 verifyZeroInteractions(alwaysFalseRoles);
91 @Test(dataProvider = "allInterfaceFunctions")
92 public void falseAndFalseComposite_returnsFalse(Function<RoleValidator, Boolean> interfaceFunction) {
93 RoleValidatorsComposer underTest = new RoleValidatorsComposer(alwaysFalseRoles, alwaysFalseRoles);
96 interfaceFunction.apply(underTest),
102 public void secondaryConstructor_givenSetIfValidators_returnsTrue() {
103 RoleValidatorsComposer underTest = new RoleValidatorsComposer(
104 ImmutableSet.of(alwaysTrueRoles)
107 assertThat(underTest.isSubscriberPermitted("anything"), is(true));