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.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertTrue;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import org.apache.camel.Exchange;
35 import org.junit.BeforeClass;
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.authorization.SecureServicePermission;
41 import org.onap.clamp.clds.exception.NotAuthorizedException;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.security.core.Authentication;
45 import org.springframework.security.core.GrantedAuthority;
46 import org.springframework.security.core.authority.SimpleGrantedAuthority;
47 import org.springframework.security.core.context.SecurityContext;
48 import org.springframework.security.core.context.SecurityContextHolder;
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)
56 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
57 public class AuthorizationControllerItCase {
60 private AuthorizationController auth;
62 private static SecurityContext sc = SecurityContextHolder.getContext();
65 * Setup the variable before the tests execution.
68 public static void setupBefore() {
70 sc.setAuthentication(new Authentication() {
72 public Collection<? extends GrantedAuthority> getAuthorities() {
73 return Arrays.asList(new SimpleGrantedAuthority(
74 new SecureServicePermission("permission-type-cl", "dev", "read").getKey()),
75 new SimpleGrantedAuthority(new SecureServicePermission("permission-type-cl-manage", "dev",
77 new SimpleGrantedAuthority(new SecureServicePermission("permission-type-filter-vf", "dev",
78 "12345-55555-55555-5555").getKey()));
82 public Object getCredentials() {
87 public Object getDetails() {
92 public Object getPrincipal() {
97 public boolean isAuthenticated() {
102 public void setAuthenticated(boolean authenticatedFlag) throws IllegalArgumentException {
107 public String getName() {
115 public void testIsUserPermitted() {
116 assertEquals(AuthorizationController.getPrincipalName(sc),"admin");
117 assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "dev", "read")));
118 assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl-manage", "dev", "DEPLOY")));
119 assertTrue(auth.isUserPermitted(
120 new SecureServicePermission("permission-type-filter-vf", "dev", "12345-55555-55555-5555")));
121 assertFalse(auth.isUserPermitted(new SecureServicePermission("permission-type-cl", "test", "read")));
124 @Test(expected = NotAuthorizedException.class)
125 public void testIfAuthorizeThrowException() {
126 Exchange ex = Mockito.mock(Exchange.class);
127 auth.authorize(ex, "cl", "test", "read");