2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.clds.it;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
32 import java.util.List;
34 import org.apache.camel.Exchange;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.onap.clamp.authorization.AuthorizationController;
40 import org.onap.clamp.clds.exception.NotAuthorizedException;
41 import org.onap.clamp.clds.service.SecureServicePermission;
42 import org.onap.clamp.util.PrincipalUtils;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
46 import org.springframework.security.core.GrantedAuthority;
47 import org.springframework.security.core.context.SecurityContext;
48 import org.springframework.security.core.userdetails.User;
49 import org.springframework.test.context.junit4.SpringRunner;
52 * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
53 * and stored procedures.
55 @RunWith(SpringRunner.class)
57 public class AuthorizationControllerItCase {
59 private PermissionTestDefaultHelper permissionTestHelper = new PermissionTestDefaultHelper();
62 // MockEnvironment env;
65 private AuthorizationController auth;
68 * Setup the variable before the tests execution.
71 public void setupBefore() {
72 // permissionTestHelper.setupMockEnv(env);
73 List<GrantedAuthority> authList = permissionTestHelper.getAuthList();
75 SecurityContext securityContext = Mockito.mock(SecurityContext.class);
76 Mockito.when(securityContext.getAuthentication())
77 .thenReturn(new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList));
78 PrincipalUtils.setSecurityContext(securityContext);
82 public void testIsUserPermitted() {
83 assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "dev", "read")));
84 assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl-manage", "dev", "DEPLOY")));
85 assertTrue(auth.isUserPermitted(
86 new SecureServicePermission("permission-type-filter-vf", "dev", "12345-55555-55555-5555")));
87 assertFalse(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "test", "read")));
91 public void testIfUserAuthorize() {
92 Exchange ex = Mockito.mock(Exchange.class);
95 .doActionOnAllPermissions(((type, instance, action) -> auth.authorize(ex, type, instance, action)));
96 } catch (NotAuthorizedException e) {
101 @Test(expected = NotAuthorizedException.class)
102 public void testIfAuthorizeThrowException() {
103 Exchange ex = Mockito.mock(Exchange.class);
104 auth.authorize(ex, "cl", "test", "read");