UserRolesController methods up
[portal.git] / portal-BE / src / test / java / org / onap / portal / controller / UserRolesControllerTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software 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  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.controller;
42
43 import static org.junit.jupiter.api.Assertions.*;
44
45 import java.io.File;
46 import java.io.FileNotFoundException;
47 import java.io.FileReader;
48 import java.sql.Connection;
49 import java.sql.SQLException;
50 import java.time.LocalDateTime;
51 import java.util.Arrays;
52 import java.util.Collections;
53 import java.util.HashSet;
54 import javax.persistence.EntityManager;
55 import javax.persistence.EntityManagerFactory;
56 import javax.persistence.Persistence;
57 import javax.servlet.http.HttpServletRequest;
58 import javax.sql.DataSource;
59 import org.h2.tools.RunScript;
60 import org.hibernate.Session;
61 import org.hibernate.jdbc.Work;
62 import org.junit.Before;
63 import org.junit.BeforeClass;
64 import org.junit.Ignore;
65 import org.junit.jupiter.api.Test;
66 import org.junit.runner.RunWith;
67 import org.onap.portal.domain.db.fn.FnApp;
68 import org.onap.portal.domain.db.fn.FnLanguage;
69 import org.onap.portal.domain.db.fn.FnRole;
70 import org.onap.portal.domain.db.fn.FnUser;
71 import org.onap.portal.domain.db.fn.FnUserRole;
72 import org.onap.portal.domain.dto.ecomp.ExternalSystemAccess;
73 import org.onap.portal.framework.MockitoTestSuite;
74 import org.onap.portal.service.fn.FnAppService;
75 import org.onap.portal.service.fn.FnLanguageService;
76 import org.onap.portal.service.fn.FnLuTimezoneService;
77 import org.onap.portal.service.fn.FnRoleService;
78 import org.onap.portal.service.fn.FnUserRoleService;
79 import org.onap.portal.service.fn.FnUserService;
80 import org.springframework.beans.factory.annotation.Autowired;
81 import org.springframework.boot.test.context.SpringBootTest;
82 import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
83 import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
84 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
85 import org.springframework.test.context.TestPropertySource;
86 import org.springframework.test.context.junit4.SpringRunner;
87 import org.springframework.test.web.servlet.MockMvc;
88 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
89 import org.springframework.transaction.annotation.Transactional;
90 import org.springframework.web.context.WebApplicationContext;
91
92 @RunWith(SpringRunner.class)
93 @SpringBootTest
94 @Transactional
95 @TestPropertySource(locations = "classpath:test.properties")
96 class UserRolesControllerTest {
97        @Autowired
98        private UserRolesController userRolesController;
99        @Autowired
100        private FnUserService fnUserService;
101        @Autowired
102        private FnUserRoleService fnUserRoleService;
103        @Autowired
104        private FnLanguageService fnLanguageService;
105        @Autowired
106        private FnAppService fnAppService;
107        @Autowired
108        private FnRoleService fnRoleService;
109
110        @Test
111        void checkIfUserIsSuperAdminFalse() {
112               UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("ps0001",
113                       "demo123");
114               //Given
115               boolean expected = false;
116               //When
117               boolean actual = userRolesController.checkIfUserIsSuperAdmin(principal);
118               //Then
119               assertEquals(expected, actual);
120        }
121
122        @Test
123        void checkIfUserIsSuperAdminTrue() {
124               UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
125                       "demo123");
126               //Given
127               boolean expected = true;
128               //When
129               boolean actual = userRolesController.checkIfUserIsSuperAdmin(principal);
130               //Then
131               assertEquals(expected, actual);
132        }
133
134        @Test
135        void readExternalRequestAccess() {
136               ExternalSystemAccess expected = new ExternalSystemAccess("external_access_enable", false);
137               ExternalSystemAccess actual = userRolesController.readExternalRequestAccess();
138
139               assertEquals(expected.getAccessValue(), actual.getAccessValue());
140               assertEquals(expected.getKey(), actual.getKey());
141        }
142 }