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