Checkstyle fixes
[clamp.git] / src / test / java / org / onap / clamp / clds / it / PermissionTestHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Samsung. 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  */
22
23 package org.onap.clamp.clds.it;
24
25 import static org.onap.clamp.authorization.AuthorizationController.PERM_PREFIX;
26 import static org.onap.clamp.clds.config.ClampProperties.CONFIG_PREFIX;
27
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.springframework.mock.env.MockEnvironment;
33 import org.springframework.security.core.GrantedAuthority;
34 import org.springframework.security.core.authority.SimpleGrantedAuthority;
35
36 public class PermissionTestHelper {
37
38     private static final String securityPrefix = CONFIG_PREFIX + PERM_PREFIX;
39     private final Map<String, Map<?, ?>> permission;
40     private static final List<GrantedAuthority> authList = new LinkedList<>();
41
42     /**
43      * Permission Test Helper constructor. Generate authList base on general
44      * permission collection.
45      */
46     public PermissionTestHelper(Map<String, Map<?, ?>> permission) {
47         this.permission = permission;
48         this.createAuthList();
49     }
50
51     private void createAuthList() {
52         permission.forEach((type, instanceMap) -> instanceMap.forEach((instance, actionList) -> {
53             for (String action : (String[]) actionList) {
54                 authList.add(new SimpleGrantedAuthority(type + "|" + instance + "|" + action));
55             }
56         }));
57     }
58
59     List<GrantedAuthority> getAuthList() {
60         return authList;
61     }
62
63     void setupMockEnv(MockEnvironment env) {
64         permission.forEach((type, instanceMap) -> env.withProperty(securityPrefix + type, type));
65     }
66
67     void doActionOnAllPermissions(PermissionAction action) {
68         permission.forEach((type, instanceMap) -> instanceMap.forEach((instance, actionList) -> {
69             for (String actionName : (String[]) actionList) {
70                 action.doAction(type, (String) instance, actionName);
71             }
72         }));
73     }
74
75     @FunctionalInterface
76     public interface PermissionAction {
77         void doAction(String type, String instance, String action);
78     }
79 }