2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * Modifications Copyright (C) 2020 Nordix Foundation.
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END============================================
23 * ===================================================================
27 package org.onap.clamp.clds.it;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertTrue;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import org.apache.camel.Exchange;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.clamp.authorization.AuthorizationController;
42 import org.onap.clamp.authorization.SecureServicePermission;
43 import org.onap.clamp.clds.exception.NotAuthorizedException;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.security.core.Authentication;
47 import org.springframework.security.core.GrantedAuthority;
48 import org.springframework.security.core.authority.SimpleGrantedAuthority;
49 import org.springframework.security.core.context.SecurityContext;
50 import org.springframework.security.core.context.SecurityContextHolder;
51 import org.springframework.test.context.junit4.SpringRunner;
54 * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
55 * and stored procedures.
57 @RunWith(SpringRunner.class)
58 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
59 public class AuthorizationControllerItCase {
62 private AuthorizationController auth;
64 private static SecurityContext sc = SecurityContextHolder.getContext();
67 * Setup the variable before the tests execution.
70 public static void setupBefore() {
72 sc.setAuthentication(new Authentication() {
74 public Collection<? extends GrantedAuthority> getAuthorities() {
75 return Arrays.asList(new SimpleGrantedAuthority(
76 new SecureServicePermission("permission-type-cl", "dev", "read").getKey()),
77 new SimpleGrantedAuthority(new SecureServicePermission("permission-type-cl-manage", "dev",
79 new SimpleGrantedAuthority(new SecureServicePermission("permission-type-filter-vf", "dev",
80 "12345-55555-55555-5555").getKey()));
84 public Object getCredentials() {
89 public Object getDetails() {
94 public Object getPrincipal() {
99 public boolean isAuthenticated() {
104 public void setAuthenticated(boolean authenticatedFlag) throws IllegalArgumentException {
109 public String getName() {
118 public static void afterCleanup() {
119 sc.setAuthentication(null);
123 public void testIsUserPermitted() {
124 assertEquals(AuthorizationController.getPrincipalName(sc),"admin");
125 assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "dev", "read")));
126 assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl-manage", "dev", "DEPLOY")));
127 assertTrue(auth.isUserPermitted(
128 new SecureServicePermission("permission-type-filter-vf", "dev", "12345-55555-55555-5555")));
129 assertFalse(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "test", "read")));
132 @Test(expected = NotAuthorizedException.class)
133 public void testIfAuthorizeThrowException() {
134 Exchange ex = Mockito.mock(Exchange.class);
135 auth.authorize(ex, "cl", "test", "read");