Unit test fails due to cleanup
[clamp.git] / src / test / java / org / onap / clamp / clds / it / AuthorizationControllerItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
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
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
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  * ===================================================================
24  *
25  */
26
27 package org.onap.clamp.clds.it;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertTrue;
32
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;
52
53 /**
54  * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
55  * and stored procedures.
56  */
57 @RunWith(SpringRunner.class)
58 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
59 public class AuthorizationControllerItCase {
60
61     @Autowired
62     private AuthorizationController auth;
63
64     private static SecurityContext sc = SecurityContextHolder.getContext();
65
66     /**
67      * Setup the variable before the tests execution.
68      */
69     @BeforeClass
70     public static void setupBefore() {
71
72         sc.setAuthentication(new Authentication() {
73             @Override
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",
78                                 "DEPLOY").getKey()),
79                         new SimpleGrantedAuthority(new SecureServicePermission("permission-type-filter-vf", "dev",
80                                 "12345-55555-55555-5555").getKey()));
81             }
82
83             @Override
84             public Object getCredentials() {
85                 return null;
86             }
87
88             @Override
89             public Object getDetails() {
90                 return null;
91             }
92
93             @Override
94             public Object getPrincipal() {
95                 return "admin";
96             }
97
98             @Override
99             public boolean isAuthenticated() {
100                 return true;
101             }
102
103             @Override
104             public void setAuthenticated(boolean authenticatedFlag) throws IllegalArgumentException {
105
106             }
107
108             @Override
109             public String getName() {
110                 return "admin";
111             }
112         });
113
114     }
115
116     @AfterClass
117
118     public static void afterCleanup() {
119         sc.setAuthentication(null);
120     }
121
122     @Test
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")));
130     }
131
132     @Test(expected = NotAuthorizedException.class)
133     public void testIfAuthorizeThrowException() {
134         Exchange ex = Mockito.mock(Exchange.class);
135         auth.authorize(ex, "cl", "test", "read");
136     }
137 }