Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / 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.openecomp.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.openecomp.portalsdk.core.restful.domain.EcompUser;
50 import org.openecomp.sparky.util.NodeUtils;
51 import org.powermock.modules.junit4.PowerMockRunner;
52
53 import com.google.gson.Gson;
54
55 @RunWith(PowerMockRunner.class)
56 //@PrepareForTest(RolesConfig.class)
57 public class TestUserManager {
58
59   private static final String LOGINID_3 = "3";
60   private static File noFile;
61   private static File concurrentUsers;
62   private static File concurrentEditUsers;
63
64   private static final Gson GSON = new Gson();
65   private static final String LOGINID_1 = "1";
66   private static final String LOGINID_2 = "2";
67
68   enum TestData {
69     // @formatter:off
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");
74
75     private String filename;
76     TestData(String filename) {this.filename = filename;}
77     public String getFilename() {return this.filename;}
78     // @formatter:on
79   }
80
81   @BeforeClass
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();
86   }
87
88   @AfterClass
89   public static void tearDownAfterClass() throws Exception {
90     Files.deleteIfExists(concurrentUsers.toPath());
91     Files.deleteIfExists(concurrentEditUsers.toPath());
92   }
93
94   @Before
95   public void setUp() throws Exception {
96     EcompUser user1 = new EcompUser();
97     user1.setLoginId(LOGINID_1);
98
99     EcompUser user2 = new EcompUser();
100     user2.setLoginId(LOGINID_2);
101
102     List<EcompUser> users = Arrays.asList(user1, user2);
103     Files.write(concurrentEditUsers.toPath(), GSON.toJson(users).getBytes());
104
105 //    Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
106 //        TestData.ROLES_CONFIG_FILE.getFilename());
107   }
108
109   @After
110   public void tearDown() throws Exception {
111     Files.deleteIfExists(concurrentUsers.toPath());
112     Files.deleteIfExists(concurrentEditUsers.toPath());
113   }
114
115   @Test
116   public void testGetUsersNoFile() throws Exception {
117     UserManager userManager = new UserManager(noFile);
118     List<EcompUser> users = userManager.getUsers();
119
120     assertThat(users, empty());
121   }
122
123   @Test
124   public void testConcurrentPush() throws Exception {
125     Callable<EcompUser> pushTask = () -> {
126       return pushTask(concurrentUsers, String.valueOf(NodeUtils.getRandomTxnId()));
127     };
128
129     List<Callable<EcompUser>> callables = Arrays.asList(pushTask, pushTask, pushTask, pushTask,
130         pushTask);
131
132     ExecutorService executor = Executors.newWorkStealingPool();
133     executor.invokeAll(callables).stream().map(future -> {
134       try {
135         return future.get();
136       } catch (Exception e) {
137         throw new IllegalStateException(e);
138       }
139     });
140
141     UserManager userManager = new UserManager(concurrentUsers);
142     assertThat(userManager.getUsers().size(), is(5));
143   }
144
145   @Test
146   public void testConcurrentPushAndEdit() throws Exception {
147     Callable<EcompUser> pushTaskRandomId = () -> {
148       return pushTask(concurrentEditUsers, String.valueOf(NodeUtils.getRandomTxnId()));
149     };
150
151     Callable<EcompUser> pushTaskId3 = () -> {
152       return pushTask(concurrentEditUsers, LOGINID_3);
153     };
154
155     Callable<EcompUser> editTaskId1 = () -> {
156       return editTask(LOGINID_1, "Bob");
157     };
158
159     Callable<EcompUser> editTaskId2 = () -> {
160       return editTask(LOGINID_2, "Jen");
161     };
162
163     Callable<EcompUser> editTaskId3 = () -> {
164       return editTask(LOGINID_3, "Amy");
165     };
166
167     List<Callable<EcompUser>> callables = Arrays.asList(pushTaskRandomId, pushTaskRandomId,
168         pushTaskId3, editTaskId1, pushTaskRandomId, pushTaskRandomId, editTaskId3, editTaskId2,
169         pushTaskRandomId);
170
171     ExecutorService executor = Executors.newWorkStealingPool();
172     List<EcompUser> userTasks = executor.invokeAll(callables).stream().map(future -> {
173       try {
174         return future.get();
175       } catch (Exception e) {
176         throw new IllegalStateException(e);
177       }
178     }).collect(Collectors.toList());
179
180     assertThat(userTasks.size(), is(9));
181
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"));
187   }
188
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);
194     return user;
195   }
196
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);
203     return user;
204   }
205 }