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.openecomp.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.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
55 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
56 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
57 import org.openecomp.sparky.security.portal.config.PortalAuthenticationConfig;
58 import org.openecomp.sparky.security.portal.config.RolesConfig;
59 import org.powermock.core.classloader.annotations.PowerMockIgnore;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
62 import org.powermock.reflect.Whitebox;
64 @PowerMockIgnore({ "javax.crypto.*" })
65 @RunWith(PowerMockRunner.class)
66 @PrepareForTest({ PortalAuthenticationConfig.class, RolesConfig.class })
67 public class TestPortalRestAPIServiceImpl {
69 private static File testUsersFile;
70 private static final String LOGINID_1 = "200";
71 private static final String LOGINID_2 = "201";
72 private static final String VIEW_ROLE = "View";
76 TEST_USERS ("src/test/resources/portal/test-users.config"),
77 PORTAL_AUTHENTICATION_PROPERTIES ("src/test/resources/portal/portal-authentication.properties"),
78 ROLES_CONFIG_FILE ("src/test/resources/portal/roles.config");
80 private String filename;
81 TestData(String filename) {this.filename = filename;}
82 public String getFilename() {return this.filename;}
87 private UserManager userManager = new UserManager(testUsersFile);
90 private PortalRestAPIServiceImpl portalApi = new PortalRestAPIServiceImpl();
93 public static void setUpBeforeClass() throws Exception {
94 testUsersFile = Paths.get(TestData.TEST_USERS.getFilename()).toFile();
98 public static void tearDownAfterClass() throws Exception {
99 Files.deleteIfExists(testUsersFile.toPath());
103 public void setUp() throws Exception {
104 Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
105 TestData.ROLES_CONFIG_FILE.getFilename());
109 public void tearDown() throws Exception {
110 Files.deleteIfExists(testUsersFile.toPath());
114 public void testPushAndGetUser() throws Exception {
115 EcompUser user = new EcompUser();
116 user.setLoginId(LOGINID_1);
118 portalApi.pushUser(user);
119 EcompUser storedUser = portalApi.getUser(user.getLoginId());
121 assertThat(storedUser.getLoginId(), is(user.getLoginId()));
124 @Test(expected = PortalAPIException.class)
125 public void testCannotPushUserTwice() throws Exception {
126 EcompUser user = new EcompUser();
127 user.setLoginId(LOGINID_1);
129 portalApi.pushUser(user);
130 portalApi.pushUser(user);
133 @Test(expected = PortalAPIException.class)
134 public void testGetUnknownUser() throws Exception {
135 EcompUser user = new EcompUser();
136 user.setLoginId(LOGINID_1);
137 portalApi.pushUser(user);
139 portalApi.getUser("does-not-exist");
143 public void testGetUsers() throws Exception {
144 EcompUser user = new EcompUser();
145 user.setLoginId(LOGINID_1);
147 EcompUser user2 = new EcompUser();
148 user2.setLoginId(LOGINID_2);
150 portalApi.pushUser(user);
151 portalApi.pushUser(user2);
153 List<EcompUser> users = portalApi.getUsers();
155 assertThat(users.size(), is(2));
156 assertThat(users.get(0).getLoginId(), is(LOGINID_1));
157 assertThat(users.get(1).getLoginId(), is(LOGINID_2));
161 public void testEditUser() throws Exception {
162 EcompUser user = new EcompUser();
163 user.setLoginId(LOGINID_1);
164 user.setFirstName("Bob");
166 portalApi.pushUser(user);
168 user.setFirstName("Jen");
169 portalApi.editUser(LOGINID_1, user);
171 assertThat(portalApi.getUser(LOGINID_1).getFirstName(), is("Jen"));
174 @Test(expected = PortalAPIException.class)
175 public void testEditUnknowUser() throws Exception {
176 EcompUser user = new EcompUser();
177 user.setLoginId(LOGINID_1);
178 portalApi.pushUser(user);
180 portalApi.editUser("does-no-exist", new EcompUser());
184 public void testGetRoles() throws Exception {
185 EcompUser user = new EcompUser();
186 user.setLoginId(LOGINID_1);
187 user.setRoles(new HashSet<>(portalApi.getAvailableRoles()));
189 portalApi.pushUser(user);
191 List<EcompRole> userRoles = portalApi.getUserRoles(LOGINID_1);
193 assertThat(userRoles.size(), is(1));
194 assertThat(userRoles.get(0).getId(), is(1L));
195 assertThat(userRoles.get(0).getName(), is(VIEW_ROLE));
199 public void testPushUserRoles() throws Exception {
200 EcompUser user = new EcompUser();
201 user.setLoginId(LOGINID_1);
202 portalApi.pushUser(user);
204 EcompUser storedUser = portalApi.getUser(LOGINID_1);
205 assertThat(storedUser.getRoles(), nullValue());
207 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
209 Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
210 ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
212 assertThat(rolesList.size(), is(1));
213 assertThat(rolesList.get(0).getId(), is(1L));
214 assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
218 public void testCannotPushRoleTwice() throws Exception {
219 EcompUser user = new EcompUser();
220 user.setLoginId(LOGINID_1);
221 portalApi.pushUser(user);
223 EcompUser storedUser = portalApi.getUser(LOGINID_1);
224 assertThat(storedUser.getRoles(), nullValue());
226 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
227 portalApi.pushUserRole(LOGINID_1, UserManager.getRoles());
229 Set<EcompRole> storedUserRoles = portalApi.getUser(LOGINID_1).getRoles();
230 ArrayList<EcompRole> rolesList = new ArrayList<>(storedUserRoles);
232 assertThat(rolesList.size(), is(1));
233 assertThat(rolesList.get(0).getId(), is(1L));
234 assertThat(rolesList.get(0).getName(), is(VIEW_ROLE));
238 public void testDeleteUserRoles() throws Exception {
239 EcompUser user = new EcompUser();
240 user.setLoginId(LOGINID_1);
241 user.setFirstName("Bob");
242 List<EcompRole> availableRoles = portalApi.getAvailableRoles();
243 user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
245 portalApi.pushUser(user);
247 portalApi.pushUserRole(LOGINID_1, new ArrayList<EcompRole>());
249 EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
251 assertThat(userWithNoRoles.getRoles(), empty());
255 public void testPushNullRoles() throws Exception {
256 EcompUser user = new EcompUser();
257 user.setLoginId(LOGINID_1);
258 user.setFirstName("Bob");
259 List<EcompRole> availableRoles = portalApi.getAvailableRoles();
260 user.setRoles(new LinkedHashSet<EcompRole>(availableRoles));
262 portalApi.pushUser(user);
263 portalApi.pushUserRole(LOGINID_1, null);
265 EcompUser userWithNoRoles = portalApi.getUser(LOGINID_1);
267 assertThat(userWithNoRoles.getRoles(), empty());