[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / authentication / ApiPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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.dmaap.dbcapi.authentication;
22
23 import org.junit.Test;
24 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
25
26 import java.util.Properties;
27
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30
31 public class ApiPolicyTest {
32
33     private Properties properties = new Properties();
34     private ApiPolicy apiPolicy;
35
36     @Test
37     public void check_shouldExecuteAuthorizationApi() throws Exception {
38         properties.put("ApiPermission.Class", "org.onap.dmaap.dbcapi.authentication.ApiPolicyTest$DummyApiAuthorization");
39         apiPolicy = new ApiPolicy(properties);
40
41         apiPolicy.check("mechId", "pwd", new DmaapPerm("api.perm", "*", "GET"));
42
43         assertTrue(((DummyApiAuthorization) apiPolicy.getPerm()).isCheckExecuted());
44     }
45
46     @Test
47     public void isPermissionClassSet_shouldReturnTrueForValidApiPermClass() {
48         properties.put("ApiPermission.Class", "org.onap.dmaap.dbcapi.authentication.ApiPolicyTest$DummyApiAuthorization");
49         apiPolicy = new ApiPolicy(properties);
50
51         assertTrue(apiPolicy.isPermissionClassSet());
52     }
53
54     @Test
55     public void isPermissionClassSet_shouldReturnFalseWhenPropertyIsNotSet() {
56         apiPolicy = new ApiPolicy(properties);
57
58         assertFalse(apiPolicy.isPermissionClassSet());
59     }
60
61     @Test
62     public void isPermissionClassSet_shouldReturnFalseWhenWrongClassIsSet() {
63         properties.put("ApiPermission.Class", "org.onap.dmaap.dbcapi.authentication.NotExisting");
64         apiPolicy = new ApiPolicy(properties);
65
66         assertFalse(apiPolicy.isPermissionClassSet());
67     }
68
69     public static class DummyApiAuthorization implements ApiAuthorizationCheckInterface {
70
71         private boolean checkExecuted = false;
72
73         @Override
74         public void check(String mechid, String pwd, DmaapPerm p) {
75             checkExecuted = true;
76         }
77
78         boolean isCheckExecuted() {
79             return checkExecuted;
80         }
81     }
82 }