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"),
79 PORTAL_AUTHENTICATION_PROPERTIES ("src/test/resources/portal/portal-authentication.properties"),
80 ROLES_CONFIG_FILE ("src/test/resources/portal/roles.config");
82 private String filename;
83 TestData(String filename) {this.filename = filename;}
84 public String getFilename() {return this.filename;}
89 private UserManager userManager = new UserManager(testUsersFile);
92 private PortalRestAPIServiceImpl portalApi = new PortalRestAPIServiceImpl();
95 public static void setUpBeforeClass() throws Exception {
96 testUsersFile = Paths.get(TestData.TEST_USERS.getFilename()).toFile();
100 public static void tearDownAfterClass() throws Exception {
101 Files.deleteIfExists(testUsersFile.toPath());
105 public void setUp() throws Exception {
106 Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
107 TestData.ROLES_CONFIG_FILE.getFilename());
111 public void tearDown() throws Exception {
112 Files.deleteIfExists(testUsersFile.toPath());
116 public void testPushAndGetUser() throws Exception {
117 EcompUser user = new EcompUser();
118 user.setLoginId(LOGINID_1);
120 portalApi.pushUser(user);
121 EcompUser storedUser = portalApi.getUser(user.getLoginId());
123 assertThat(storedUser.getLoginId(), is(user.getLoginId()));
126 @Test(expected = PortalAPIException.class)
127 public void testCannotPushUserTwice() throws Exception {
128 EcompUser user = new EcompUser();
129 user.setLoginId(LOGINID_1);
131 portalApi.pushUser(user);
132 portalApi.pushUser(user);
135 @Test(expected = PortalAPIException.class)
136 public void testGetUnknownUser() throws Exception {
137 EcompUser user = new EcompUser();
138 user.setLoginId(LOGINID_1);
139 portalApi.pushUser(user);
141 portalApi.getUser("does-not-exist");
145 public void testGetUsers() throws Exception {
146 EcompUser user = new EcompUser();
147 user.setLoginId(LOGINID_1);
149 EcompUser user2 = new EcompUser();
150 user2.setLoginId(LOGINID_2);
152 portalApi.pushUser(user);
153 portalApi.pushUser(user2);
155 List<EcompUser> users = portalApi.getUsers();
157 assertThat(users.size(), is(2));
158 assertThat(users.get(0).getLoginId(), is(LOGINID_1));
159 assertThat(users.get(1).getLoginId(), is(LOGINID_2));
163 public void testEditUser() throws Exception {
164 EcompUser user = new EcompUser();
165 user.setLoginId(LOGINID_1);
166 user.setFirstName("Bob");
168 portalApi.pushUser(user);
170 user.setFirstName("Jen");
171 portalApi.editUser(LOGINID_1, user);
173 assertThat(portalApi.getUser(LOGINID_1).getFirstName(), is("Jen"));
176 @Test(expected = PortalAPIException.class)
177 public void testEditUnknowUser() throws Exception {
178 EcompUser user = new EcompUser();
179 user.setLoginId(LOGINID_1);
180 portalApi.pushUser(user);
182 portalApi.editUser("does-no-exist", new EcompUser());
186 public void testGetRoles() throws Exception {
187 EcompUser user = new EcompUser();
188 user.setLoginId(LOGINID_1);
189 user.setRoles(new HashSet<>(portalApi.getAvailableRoles()));
191 portalApi.pushUser(user);
193 List<EcompRole> userRoles = portalApi.getUserRoles(LOGINID_1);
195 assertThat(userRoles.size(), is(1));
196 assertThat(userRoles.get(0).getId(), is(1L));
197 assertThat(userRoles.get(0).getName(), is(VIEW_ROLE));
201 public void testPushUserRoles() throws Exception {
202 EcompUser user = new EcompUser();
203 user.setLoginId(LOGINID_1);
204 portalApi.pushUser(user);
206 EcompUser storedUser = portalApi.getUser(LOGINID_1);
207 assertThat(storedUser.getRoles(), nullValue());
209 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
211 Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
212 ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
214 assertThat(rolesList.size(), is(1));
215 assertThat(rolesList.get(0).getId(), is(1L));
216 assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
220 public void testCannotPushRoleTwice() throws Exception {
221 EcompUser user = new EcompUser();
222 user.setLoginId(LOGINID_1);
223 portalApi.pushUser(user);
225 EcompUser storedUser = portalApi.getUser(LOGINID_1);
226 assertThat(storedUser.getRoles(), nullValue());
228 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
229 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
231 Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
232 ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
234 assertThat(rolesList.size(), is(1));
235 assertThat(rolesList.get(0).getId(), is(1L));
236 assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
240 public void testDeleteUserRoles() throws Exception {
241 EcompUser user = new EcompUser();
242 user.setLoginId(LOGINID_1);
243 user.setFirstName("Bob");
244 List<EcompRole> availableRoles = portalApi.getAvailableRoles();
245 user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
247 portalApi.pushUser(user);
249 portalApi.pushUserRole(LOGINID_1, new ArrayList<EcompRole>());
251 EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
253 assertThat(userWithNoRoles.getRoles(), empty());
257 public void testPushNullRoles() throws Exception {
258 EcompUser user = new EcompUser();
259 user.setLoginId(LOGINID_1);
260 user.setFirstName("Bob");
261 List<EcompRole> availableRoles = portalApi.getAvailableRoles();
262 user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
264 portalApi.pushUser(user);
265 portalApi.pushUserRole(LOGINID_1, null);
267 EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
269 assertThat(userWithNoRoles.getRoles(), empty());
273 public void testIsAppAuthenticated() throws Exception {
274 Whitebox.setInternalState(PortalAuthenticationConfig.class, "AUTHENTICATION_CONFIG_FILE",
275 TestData.PORTAL_AUTHENTICATION_PROPERTIES.getFilename());
277 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
278 when(request.getHeader("username")).thenReturn("testuser");
279 when(request.getHeader("password")).thenReturn("testpassword");
281 assertThat(portalApi.isAppAuthenticated(request), is(true));