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"), CONCURRENT_USERS(
72 "src/test/resources/portal/concurrent-users.config"), CONCURRENT_EDIT_USERS(
73 "src/test/resources/portal/concurrent-edit-users.config");
74 // ROLES_CONFIG_FILE ("src/test/resources/portal/roles.config");
76 private String filename;
78 TestData(String filename) {
79 this.filename = filename;
82 public String getFilename() {
89 public static void setUpBeforeClass() throws Exception {
90 noFile = Paths.get(TestData.NO_FILE.getFilename()).toFile();
91 concurrentUsers = Paths.get(TestData.CONCURRENT_USERS.getFilename()).toFile();
92 concurrentEditUsers = Paths.get(TestData.CONCURRENT_EDIT_USERS.getFilename()).toFile();
96 public static void tearDownAfterClass() throws Exception {
97 Files.deleteIfExists(concurrentUsers.toPath());
98 Files.deleteIfExists(concurrentEditUsers.toPath());
102 public void setUp() throws Exception {
103 EcompUser user1 = new EcompUser();
104 user1.setLoginId(LOGINID_1);
106 EcompUser user2 = new EcompUser();
107 user2.setLoginId(LOGINID_2);
109 List<EcompUser> users = Arrays.asList(user1, user2);
110 Files.write(concurrentEditUsers.toPath(), GSON.toJson(users).getBytes());
112 // Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
113 // TestData.ROLES_CONFIG_FILE.getFilename());
117 public void tearDown() throws Exception {
118 Files.deleteIfExists(concurrentUsers.toPath());
119 Files.deleteIfExists(concurrentEditUsers.toPath());
123 public void testGetUsersNoFile() throws Exception {
124 UserManager userManager = new UserManager(noFile);
125 List<EcompUser> users = userManager.getUsers();
127 assertThat(users, empty());
131 public void testConcurrentPush() throws Exception {
132 Callable<EcompUser> pushTask = () -> {
133 return pushTask(concurrentUsers, String.valueOf(NodeUtils.getRandomTxnId()));
136 List<Callable<EcompUser>> callables =
137 Arrays.asList(pushTask, pushTask, pushTask, pushTask, pushTask);
139 ExecutorService executor = Executors.newWorkStealingPool();
140 executor.invokeAll(callables).stream().map(future -> {
143 } catch (Exception e) {
144 throw new IllegalStateException(e);
148 UserManager userManager = new UserManager(concurrentUsers);
149 assertThat(userManager.getUsers().size(), is(5));
153 public void testConcurrentPushAndEdit() throws Exception {
154 Callable<EcompUser> pushTaskRandomId = () -> {
155 return pushTask(concurrentEditUsers, String.valueOf(NodeUtils.getRandomTxnId()));
158 Callable<EcompUser> pushTaskId3 = () -> {
159 return pushTask(concurrentEditUsers, LOGINID_3);
162 Callable<EcompUser> editTaskId1 = () -> {
163 return editTask(LOGINID_1, "Bob");
166 Callable<EcompUser> editTaskId2 = () -> {
167 return editTask(LOGINID_2, "Jen");
170 Callable<EcompUser> editTaskId3 = () -> {
171 return editTask(LOGINID_3, "Amy");
174 List<Callable<EcompUser>> callables =
175 Arrays.asList(pushTaskRandomId, pushTaskRandomId, pushTaskId3, editTaskId1,
176 pushTaskRandomId, pushTaskRandomId, editTaskId3, editTaskId2, pushTaskRandomId);
178 ExecutorService executor = Executors.newWorkStealingPool();
179 List<EcompUser> userTasks = executor.invokeAll(callables).stream().map(future -> {
182 } catch (Exception e) {
183 throw new IllegalStateException(e);
185 }).collect(Collectors.toList());
187 assertThat(userTasks.size(), is(9));
189 UserManager userManager = new UserManager(concurrentEditUsers);
190 assertThat(userManager.getUsers().size(), is(8));
191 assertThat(userManager.getUser(LOGINID_1).get().getFirstName(), is("Bob"));
192 assertThat(userManager.getUser(LOGINID_2).get().getFirstName(), is("Jen"));
193 assertThat(userManager.getUser(LOGINID_3).get().getFirstName(), is("Amy"));
196 private EcompUser pushTask(File fileStore, String loginId) throws IOException {
197 UserManager userManager = new UserManager(fileStore);
198 EcompUser user = new EcompUser();
199 user.setLoginId(loginId);
200 userManager.pushUser(user);
204 private EcompUser editTask(String loginId, String firstName) throws IOException {
205 UserManager userManager = new UserManager(concurrentEditUsers);
206 EcompUser user = new EcompUser();
207 user.setLoginId(loginId);
208 user.setFirstName(firstName);
209 userManager.editUser(loginId, user);