2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.onap.aai.sparky.security.portal;
28 import static org.hamcrest.Matchers.empty;
29 import static org.hamcrest.Matchers.is;
30 import static org.hamcrest.Matchers.nullValue;
31 import static org.junit.Assert.assertThat;
32 import static org.mockito.Mockito.when;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.util.ArrayList;
38 import java.util.HashSet;
39 import java.util.LinkedHashSet;
40 import java.util.List;
43 import javax.servlet.http.HttpServletRequest;
45 import org.junit.After;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.onap.aai.sparky.security.portal.PortalRestAPIServiceImpl;
55 import org.onap.aai.sparky.security.portal.UserManager;
56 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
57 import org.onap.aai.sparky.security.portal.config.RolesConfig;
58 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
59 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
60 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
61 import org.powermock.core.classloader.annotations.PowerMockIgnore;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
64 import org.powermock.reflect.Whitebox;
66 @PowerMockIgnore({"javax.crypto.*"})
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({PortalAuthenticationConfig.class, RolesConfig.class})
69 public class TestPortalRestAPIServiceImpl {
71 private static File testUsersFile;
72 private static final String LOGINID_1 = "200";
73 private static final String LOGINID_2 = "201";
74 private static final String VIEW_ROLE = "View";
78 TEST_USERS("src/test/resources/portal/test-users.config"), PORTAL_AUTHENTICATION_PROPERTIES(
79 "src/test/resources/portal/portal-authentication.properties"), ROLES_CONFIG_FILE(
80 "src/test/resources/portal/roles.config");
82 private String filename;
84 TestData(String filename) {
85 this.filename = filename;
88 public String getFilename() {
95 private UserManager userManager = new UserManager(testUsersFile);
98 private PortalRestAPIServiceImpl portalApi = new PortalRestAPIServiceImpl();
101 public static void setUpBeforeClass() throws Exception {
102 testUsersFile = Paths.get(TestData.TEST_USERS.getFilename()).toFile();
106 public static void tearDownAfterClass() throws Exception {
107 Files.deleteIfExists(testUsersFile.toPath());
111 public void setUp() throws Exception {
112 Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
113 TestData.ROLES_CONFIG_FILE.getFilename());
117 public void tearDown() throws Exception {
118 Files.deleteIfExists(testUsersFile.toPath());
122 public void testPushAndGetUser() throws Exception {
123 EcompUser user = new EcompUser();
124 user.setLoginId(LOGINID_1);
126 portalApi.pushUser(user);
127 EcompUser storedUser = portalApi.getUser(user.getLoginId());
129 assertThat(storedUser.getLoginId(), is(user.getLoginId()));
132 @Test(expected = PortalAPIException.class)
133 public void testCannotPushUserTwice() throws Exception {
134 EcompUser user = new EcompUser();
135 user.setLoginId(LOGINID_1);
137 portalApi.pushUser(user);
138 portalApi.pushUser(user);
141 @Test(expected = PortalAPIException.class)
142 public void testGetUnknownUser() throws Exception {
143 EcompUser user = new EcompUser();
144 user.setLoginId(LOGINID_1);
145 portalApi.pushUser(user);
147 portalApi.getUser("does-not-exist");
151 public void testGetUsers() throws Exception {
152 EcompUser user = new EcompUser();
153 user.setLoginId(LOGINID_1);
155 EcompUser user2 = new EcompUser();
156 user2.setLoginId(LOGINID_2);
158 portalApi.pushUser(user);
159 portalApi.pushUser(user2);
161 List<EcompUser> users = portalApi.getUsers();
163 assertThat(users.size(), is(2));
164 assertThat(users.get(0).getLoginId(), is(LOGINID_1));
165 assertThat(users.get(1).getLoginId(), is(LOGINID_2));
169 public void testEditUser() throws Exception {
170 EcompUser user = new EcompUser();
171 user.setLoginId(LOGINID_1);
172 user.setFirstName("Bob");
174 portalApi.pushUser(user);
176 user.setFirstName("Jen");
177 portalApi.editUser(LOGINID_1, user);
179 assertThat(portalApi.getUser(LOGINID_1).getFirstName(), is("Jen"));
182 @Test(expected = PortalAPIException.class)
183 public void testEditUnknowUser() throws Exception {
184 EcompUser user = new EcompUser();
185 user.setLoginId(LOGINID_1);
186 portalApi.pushUser(user);
188 portalApi.editUser("does-no-exist", new EcompUser());
192 public void testGetRoles() throws Exception {
193 EcompUser user = new EcompUser();
194 user.setLoginId(LOGINID_1);
195 user.setRoles(new HashSet<>(portalApi.getAvailableRoles()));
197 portalApi.pushUser(user);
199 List<EcompRole> userRoles = portalApi.getUserRoles(LOGINID_1);
201 assertThat(userRoles.size(), is(1));
202 assertThat(userRoles.get(0).getId(), is(1L));
203 assertThat(userRoles.get(0).getName(), is(VIEW_ROLE));
207 public void testPushUserRoles() throws Exception {
208 EcompUser user = new EcompUser();
209 user.setLoginId(LOGINID_1);
210 portalApi.pushUser(user);
212 EcompUser storedUser = portalApi.getUser(LOGINID_1);
213 assertThat(storedUser.getRoles(), nullValue());
215 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
217 Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
218 ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
220 assertThat(rolesList.size(), is(1));
221 assertThat(rolesList.get(0).getId(), is(1L));
222 assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
226 public void testCannotPushRoleTwice() throws Exception {
227 EcompUser user = new EcompUser();
228 user.setLoginId(LOGINID_1);
229 portalApi.pushUser(user);
231 EcompUser storedUser = portalApi.getUser(LOGINID_1);
232 assertThat(storedUser.getRoles(), nullValue());
234 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
235 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
237 Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
238 ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
240 assertThat(rolesList.size(), is(1));
241 assertThat(rolesList.get(0).getId(), is(1L));
242 assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
246 public void testDeleteUserRoles() throws Exception {
247 EcompUser user = new EcompUser();
248 user.setLoginId(LOGINID_1);
249 user.setFirstName("Bob");
250 List<EcompRole> availableRoles = portalApi.getAvailableRoles();
251 user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
253 portalApi.pushUser(user);
255 portalApi.pushUserRole(LOGINID_1, new ArrayList<EcompRole>());
257 EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
259 assertThat(userWithNoRoles.getRoles(), empty());
263 public void testPushNullRoles() throws Exception {
264 EcompUser user = new EcompUser();
265 user.setLoginId(LOGINID_1);
266 user.setFirstName("Bob");
267 List<EcompRole> availableRoles = portalApi.getAvailableRoles();
268 user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
270 portalApi.pushUser(user);
271 portalApi.pushUserRole(LOGINID_1, null);
273 EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
275 assertThat(userWithNoRoles.getRoles(), empty());
279 public void testIsAppAuthenticated() throws Exception {
280 Whitebox.setInternalState(PortalAuthenticationConfig.class, "AUTHENTICATION_CONFIG_FILE",
281 TestData.PORTAL_AUTHENTICATION_PROPERTIES.getFilename());
283 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
284 when(request.getHeader("username")).thenReturn("testuser");
285 when(request.getHeader("password")).thenReturn("testpassword");
287 assertThat(portalApi.isAppAuthenticated(request), is(true));