2bc06efd68f6ff4c046496969c38dec20b2b75b9
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / security / portal / TestUserManager.java
1 /*
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
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
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
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=========================================================
21 *
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.onap.aai.sparky.security.portal;
27
28 import static org.hamcrest.Matchers.empty;
29 import static org.hamcrest.Matchers.is;
30 import static org.junit.Assert.assertThat;
31
32 import java.io.File;
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;
42
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;
53
54 import com.google.gson.Gson;
55
56 @RunWith(PowerMockRunner.class)
57 // @PrepareForTest(RolesConfig.class)
58 public class TestUserManager {
59
60   private static final String LOGINID_3 = "3";
61   private static File noFile;
62   private static File concurrentUsers;
63   private static File concurrentEditUsers;
64
65   private static final Gson GSON = new Gson();
66   private static final String LOGINID_1 = "1";
67   private static final String LOGINID_2 = "2";
68
69   enum TestData {
70     // @formatter:off
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");
75
76     private String filename;
77
78     TestData(String filename) {
79       this.filename = filename;
80     }
81
82     public String getFilename() {
83       return this.filename;
84     }
85     // @formatter:on
86   }
87
88   @BeforeClass
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();
93   }
94
95   @AfterClass
96   public static void tearDownAfterClass() throws Exception {
97     Files.deleteIfExists(concurrentUsers.toPath());
98     Files.deleteIfExists(concurrentEditUsers.toPath());
99   }
100
101   @Before
102   public void setUp() throws Exception {
103     EcompUser user1 = new EcompUser();
104     user1.setLoginId(LOGINID_1);
105
106     EcompUser user2 = new EcompUser();
107     user2.setLoginId(LOGINID_2);
108
109     List<EcompUser> users = Arrays.asList(user1, user2);
110     Files.write(concurrentEditUsers.toPath(), GSON.toJson(users).getBytes());
111
112     // Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
113     // TestData.ROLES_CONFIG_FILE.getFilename());
114   }
115
116   @After
117   public void tearDown() throws Exception {
118     Files.deleteIfExists(concurrentUsers.toPath());
119     Files.deleteIfExists(concurrentEditUsers.toPath());
120   }
121
122   @Test
123   public void testGetUsersNoFile() throws Exception {
124     UserManager userManager = new UserManager(noFile);
125     List<EcompUser> users = userManager.getUsers();
126
127     assertThat(users, empty());
128   }
129
130   @Test
131   public void testConcurrentPush() throws Exception {
132     Callable<EcompUser> pushTask = () -> {
133       return pushTask(concurrentUsers, String.valueOf(NodeUtils.getRandomTxnId()));
134     };
135
136     List<Callable<EcompUser>> callables =
137         Arrays.asList(pushTask, pushTask, pushTask, pushTask, pushTask);
138
139     ExecutorService executor = Executors.newWorkStealingPool();
140     executor.invokeAll(callables).stream().map(future -> {
141       try {
142         return future.get();
143       } catch (Exception e) {
144         throw new IllegalStateException(e);
145       }
146     });
147
148     UserManager userManager = new UserManager(concurrentUsers);
149     assertThat(userManager.getUsers().size(), is(5));
150   }
151
152   @Test
153   public void testConcurrentPushAndEdit() throws Exception {
154     Callable<EcompUser> pushTaskRandomId = () -> {
155       return pushTask(concurrentEditUsers, String.valueOf(NodeUtils.getRandomTxnId()));
156     };
157
158     Callable<EcompUser> pushTaskId3 = () -> {
159       return pushTask(concurrentEditUsers, LOGINID_3);
160     };
161
162     Callable<EcompUser> editTaskId1 = () -> {
163       return editTask(LOGINID_1, "Bob");
164     };
165
166     Callable<EcompUser> editTaskId2 = () -> {
167       return editTask(LOGINID_2, "Jen");
168     };
169
170     Callable<EcompUser> editTaskId3 = () -> {
171       return editTask(LOGINID_3, "Amy");
172     };
173
174     List<Callable<EcompUser>> callables =
175         Arrays.asList(pushTaskRandomId, pushTaskRandomId, pushTaskId3, editTaskId1,
176             pushTaskRandomId, pushTaskRandomId, editTaskId3, editTaskId2, pushTaskRandomId);
177
178     ExecutorService executor = Executors.newWorkStealingPool();
179     List<EcompUser> userTasks = executor.invokeAll(callables).stream().map(future -> {
180       try {
181         return future.get();
182       } catch (Exception e) {
183         throw new IllegalStateException(e);
184       }
185     }).collect(Collectors.toList());
186
187     assertThat(userTasks.size(), is(9));
188
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"));
194   }
195
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);
201     return user;
202   }
203
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);
210     return user;
211   }
212 }