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.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.openecomp.portalsdk.core.restful.domain.EcompUser;
50 import org.openecomp.sparky.util.NodeUtils;
51 import org.powermock.modules.junit4.PowerMockRunner;
53 import com.google.gson.Gson;
55 @RunWith(PowerMockRunner.class)
56 //@PrepareForTest(RolesConfig.class)
57 public class TestUserManager {
59 private static final String LOGINID_3 = "3";
60 private static File noFile;
61 private static File concurrentUsers;
62 private static File concurrentEditUsers;
64 private static final Gson GSON = new Gson();
65 private static final String LOGINID_1 = "1";
66 private static final String LOGINID_2 = "2";
70 NO_FILE ("src/test/resources/portal/no-users.config"),
71 CONCURRENT_USERS ("src/test/resources/portal/concurrent-users.config"),
72 CONCURRENT_EDIT_USERS ("src/test/resources/portal/concurrent-edit-users.config");
73 // ROLES_CONFIG_FILE ("src/test/resources/portal/roles.config");
75 private String filename;
76 TestData(String filename) {this.filename = filename;}
77 public String getFilename() {return this.filename;}
82 public static void setUpBeforeClass() throws Exception {
83 noFile = Paths.get(TestData.NO_FILE.getFilename()).toFile();
84 concurrentUsers = Paths.get(TestData.CONCURRENT_USERS.getFilename()).toFile();
85 concurrentEditUsers = Paths.get(TestData.CONCURRENT_EDIT_USERS.getFilename()).toFile();
89 public static void tearDownAfterClass() throws Exception {
90 Files.deleteIfExists(concurrentUsers.toPath());
91 Files.deleteIfExists(concurrentEditUsers.toPath());
95 public void setUp() throws Exception {
96 EcompUser user1 = new EcompUser();
97 user1.setLoginId(LOGINID_1);
99 EcompUser user2 = new EcompUser();
100 user2.setLoginId(LOGINID_2);
102 List<EcompUser> users = Arrays.asList(user1, user2);
103 Files.write(concurrentEditUsers.toPath(), GSON.toJson(users).getBytes());
105 // Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
106 // TestData.ROLES_CONFIG_FILE.getFilename());
110 public void tearDown() throws Exception {
111 Files.deleteIfExists(concurrentUsers.toPath());
112 Files.deleteIfExists(concurrentEditUsers.toPath());
116 public void testGetUsersNoFile() throws Exception {
117 UserManager userManager = new UserManager(noFile);
118 List<EcompUser> users = userManager.getUsers();
120 assertThat(users, empty());
124 public void testConcurrentPush() throws Exception {
125 Callable<EcompUser> pushTask = () -> {
126 return pushTask(concurrentUsers, String.valueOf(NodeUtils.getRandomTxnId()));
129 List<Callable<EcompUser>> callables = Arrays.asList(pushTask, pushTask, pushTask, pushTask,
132 ExecutorService executor = Executors.newWorkStealingPool();
133 executor.invokeAll(callables).stream().map(future -> {
136 } catch (Exception e) {
137 throw new IllegalStateException(e);
141 UserManager userManager = new UserManager(concurrentUsers);
142 assertThat(userManager.getUsers().size(), is(5));
146 public void testConcurrentPushAndEdit() throws Exception {
147 Callable<EcompUser> pushTaskRandomId = () -> {
148 return pushTask(concurrentEditUsers, String.valueOf(NodeUtils.getRandomTxnId()));
151 Callable<EcompUser> pushTaskId3 = () -> {
152 return pushTask(concurrentEditUsers, LOGINID_3);
155 Callable<EcompUser> editTaskId1 = () -> {
156 return editTask(LOGINID_1, "Bob");
159 Callable<EcompUser> editTaskId2 = () -> {
160 return editTask(LOGINID_2, "Jen");
163 Callable<EcompUser> editTaskId3 = () -> {
164 return editTask(LOGINID_3, "Amy");
167 List<Callable<EcompUser>> callables = Arrays.asList(pushTaskRandomId, pushTaskRandomId,
168 pushTaskId3, editTaskId1, pushTaskRandomId, pushTaskRandomId, editTaskId3, editTaskId2,
171 ExecutorService executor = Executors.newWorkStealingPool();
172 List<EcompUser> userTasks = executor.invokeAll(callables).stream().map(future -> {
175 } catch (Exception e) {
176 throw new IllegalStateException(e);
178 }).collect(Collectors.toList());
180 assertThat(userTasks.size(), is(9));
182 UserManager userManager = new UserManager(concurrentEditUsers);
183 assertThat(userManager.getUsers().size(), is(8));
184 assertThat(userManager.getUser(LOGINID_1).get().getFirstName(), is("Bob"));
185 assertThat(userManager.getUser(LOGINID_2).get().getFirstName(), is("Jen"));
186 assertThat(userManager.getUser(LOGINID_3).get().getFirstName(), is("Amy"));
189 private EcompUser pushTask(File fileStore, String loginId) throws IOException {
190 UserManager userManager = new UserManager(fileStore);
191 EcompUser user = new EcompUser();
192 user.setLoginId(loginId);
193 userManager.pushUser(user);
197 private EcompUser editTask(String loginId, String firstName) throws IOException {
198 UserManager userManager = new UserManager(concurrentEditUsers);
199 EcompUser user = new EcompUser();
200 user.setLoginId(loginId);
201 user.setFirstName(firstName);
202 userManager.editUser(loginId, user);