Merge "Fixed generic Sonar issue in the clamp project Remove duplicated literal in...
[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  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31
32 import java.io.IOException;
33 import java.util.LinkedList;
34 import java.util.List;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.onap.clamp.authorization.AuthorizationController;
41 import org.onap.clamp.clds.service.SecureServicePermission;
42 import org.onap.clamp.util.PrincipalUtils;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
45 import org.springframework.security.core.Authentication;
46 import org.springframework.security.core.GrantedAuthority;
47 import org.springframework.security.core.authority.SimpleGrantedAuthority;
48 import org.springframework.security.core.context.SecurityContext;
49 import org.springframework.security.core.userdetails.User;
50 import org.springframework.test.context.junit4.SpringRunner;
51
52 /**
53  * Test CldsDAO calls through CldsModel and CldsEvent. This really test the DB
54  * and stored procedures.
55  */
56 @RunWith(SpringRunner.class)
57 @SpringBootTest
58 public class AuthorizationControllerItCase {
59
60     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(AuthorizationControllerItCase.class);
61     private Authentication authentication;
62     private List<GrantedAuthority> authList = new LinkedList<GrantedAuthority>();
63
64     /**
65      * Setup the variable before the tests execution.
66      *
67      * @throws IOException
68      *         In case of issues when opening the files
69      */
70     @Before
71     public void setupBefore() throws IOException {
72         authList.add(new SimpleGrantedAuthority("permission-type-cl-manage|dev|*"));
73         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|read"));
74         authList.add(new SimpleGrantedAuthority("permission-type-cl|dev|update"));
75         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|read"));
76         authList.add(new SimpleGrantedAuthority("permission-type-template|dev|update"));
77         authList.add(new SimpleGrantedAuthority("permission-type-filter-vf|dev|*"));
78         authList.add(new SimpleGrantedAuthority("permission-type-cl-event|dev|*"));
79
80         authentication = new UsernamePasswordAuthenticationToken(new User("admin", "", authList), "", authList);
81     }
82
83     @Test
84     public void testIsUserPermittedNoException() {
85         SecurityContext securityContext = Mockito.mock(SecurityContext.class);
86         Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
87         PrincipalUtils.setSecurityContext(securityContext);
88
89         AuthorizationController auth = new AuthorizationController();
90         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl","dev","read")));
91         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-cl-manage","dev","DEPLOY")));
92         assertTrue(auth.isUserPermitted(new SecureServicePermission("permission-type-filter-vf","dev","12345-55555-55555-5555")));
93         assertFalse(auth.isUserPermitted(new SecureServicePermission("permission-type-cl","test","read")));
94     }
95 }