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.junit.Assert.assertThat;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.concurrent.Callable;
39 import java.util.concurrent.ExecutorService;
40 import java.util.concurrent.Executors;
41 import java.util.stream.Collectors;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.onap.aai.sparky.security.portal.UserManager;
50 import org.onap.aai.sparky.util.NodeUtils;
51 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
52 import org.powermock.modules.junit4.PowerMockRunner;
54 import com.google.gson.Gson;
56 @RunWith(PowerMockRunner.class)
57 //@PrepareForTest(RolesConfig.class)
58 public class TestUserManager {
60 private static final String LOGINID_3 = "3";
61 private static File noFile;
62 private static File concurrentUsers;
63 private static File concurrentEditUsers;
65 private static final Gson GSON = new Gson();
66 private static final String LOGINID_1 = "1";
67 private static final String LOGINID_2 = "2";
71 NO_FILE ("src/test/resources/portal/no-users.config"),
72 CONCURRENT_USERS ("src/test/resources/portal/concurrent-users.config"),
73 CONCURRENT_EDIT_USERS ("src/test/resources/portal/concurrent-edit-users.config");
74 // ROLES_CONFIG_FILE ("src/test/resources/portal/roles.config");
76 private String filename;
77 TestData(String filename) {this.filename = filename;}
78 public String getFilename() {return this.filename;}
83 public static void setUpBeforeClass() throws Exception {
84 noFile = Paths.get(TestData.NO_FILE.getFilename()).toFile();
85 concurrentUsers = Paths.get(TestData.CONCURRENT_USERS.getFilename()).toFile();
86 concurrentEditUsers = Paths.get(TestData.CONCURRENT_EDIT_USERS.getFilename()).toFile();
90 public static void tearDownAfterClass() throws Exception {
91 Files.deleteIfExists(concurrentUsers.toPath());
92 Files.deleteIfExists(concurrentEditUsers.toPath());
96 public void setUp() throws Exception {
97 EcompUser user1 = new EcompUser();
98 user1.setLoginId(LOGINID_1);
100 EcompUser user2 = new EcompUser();
101 user2.setLoginId(LOGINID_2);
103 List<EcompUser> users = Arrays.asList(user1, user2);
104 Files.write(concurrentEditUsers.toPath(), GSON.toJson(users).getBytes());
106 // Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
107 // TestData.ROLES_CONFIG_FILE.getFilename());
111 public void tearDown() throws Exception {
112 Files.deleteIfExists(concurrentUsers.toPath());
113 Files.deleteIfExists(concurrentEditUsers.toPath());
117 public void testGetUsersNoFile() throws Exception {
118 UserManager userManager = new UserManager(noFile);
119 List<EcompUser> users = userManager.getUsers();
121 assertThat(users, empty());
125 public void testConcurrentPush() throws Exception {
126 Callable<EcompUser> pushTask = () -> {
127 return pushTask(concurrentUsers, String.valueOf(NodeUtils.getRandomTxnId()));
130 List<Callable<EcompUser>> callables = Arrays.asList(pushTask, pushTask, pushTask, pushTask,
133 ExecutorService executor = Executors.newWorkStealingPool();
134 executor.invokeAll(callables).stream().map(future -> {
137 } catch (Exception e) {
138 throw new IllegalStateException(e);
142 UserManager userManager = new UserManager(concurrentUsers);
143 assertThat(userManager.getUsers().size(), is(5));
147 public void testConcurrentPushAndEdit() throws Exception {
148 Callable<EcompUser> pushTaskRandomId = () -> {
149 return pushTask(concurrentEditUsers, String.valueOf(NodeUtils.getRandomTxnId()));
152 Callable<EcompUser> pushTaskId3 = () -> {
153 return pushTask(concurrentEditUsers, LOGINID_3);
156 Callable<EcompUser> editTaskId1 = () -> {
157 return editTask(LOGINID_1, "Bob");
160 Callable<EcompUser> editTaskId2 = () -> {
161 return editTask(LOGINID_2, "Jen");
164 Callable<EcompUser> editTaskId3 = () -> {
165 return editTask(LOGINID_3, "Amy");
168 List<Callable<EcompUser>> callables = Arrays.asList(pushTaskRandomId, pushTaskRandomId,
169 pushTaskId3, editTaskId1, pushTaskRandomId, pushTaskRandomId, editTaskId3, editTaskId2,
172 ExecutorService executor = Executors.newWorkStealingPool();
173 List<EcompUser> userTasks = executor.invokeAll(callables).stream().map(future -> {
176 } catch (Exception e) {
177 throw new IllegalStateException(e);
179 }).collect(Collectors.toList());
181 assertThat(userTasks.size(), is(9));
183 UserManager userManager = new UserManager(concurrentEditUsers);
184 assertThat(userManager.getUsers().size(), is(8));
185 assertThat(userManager.getUser(LOGINID_1).get().getFirstName(), is("Bob"));
186 assertThat(userManager.getUser(LOGINID_2).get().getFirstName(), is("Jen"));
187 assertThat(userManager.getUser(LOGINID_3).get().getFirstName(), is("Amy"));
190 private EcompUser pushTask(File fileStore, String loginId) throws IOException {
191 UserManager userManager = new UserManager(fileStore);
192 EcompUser user = new EcompUser();
193 user.setLoginId(loginId);
194 userManager.pushUser(user);
198 private EcompUser editTask(String loginId, String firstName) throws IOException {
199 UserManager userManager = new UserManager(concurrentEditUsers);
200 EcompUser user = new EcompUser();
201 user.setLoginId(loginId);
202 user.setFirstName(firstName);
203 userManager.editUser(loginId, user);