2 * ============LICENSE_START=======================================================
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
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============================================
19 * ===================================================================
23 package org.onap.clamp.clds.it;
25 import static org.onap.clamp.authorization.AuthorizationController.PERM_PREFIX;
26 import static org.onap.clamp.clds.config.ClampProperties.CONFIG_PREFIX;
28 import java.util.LinkedList;
29 import java.util.List;
32 import org.springframework.mock.env.MockEnvironment;
33 import org.springframework.security.core.GrantedAuthority;
34 import org.springframework.security.core.authority.SimpleGrantedAuthority;
36 public class PermissionTestHelper {
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<>();
43 * Permission Test Helper constructor
44 * Generate authList base on general permission collection
46 public PermissionTestHelper(Map<String, Map> permission) {
47 this.permission = permission;
48 this.createAuthList();
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));
59 List<GrantedAuthority> getAuthList() {
63 void setupMockEnv(MockEnvironment env) {
64 permission.forEach((type, instanceMap) -> env.withProperty(securityPrefix + type, type));
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);
76 public interface PermissionAction {
77 void doAction(String type, String instance, String action);