Merge "Fix Blocker/Critical sonar issues"
[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"),
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");
75
76     private String filename;
77     TestData(String filename) {this.filename = filename;}
78     public String getFilename() {return this.filename;}
79     // @formatter:on
80   }
81
82   @BeforeClass
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();
87   }
88
89   @AfterClass
90   public static void tearDownAfterClass() throws Exception {
91     Files.deleteIfExists(concurrentUsers.toPath());
92     Files.deleteIfExists(concurrentEditUsers.toPath());
93   }
94
95   @Before
96   public void setUp() throws Exception {
97     EcompUser user1 = new EcompUser();
98     user1.setLoginId(LOGINID_1);
99
100     EcompUser user2 = new EcompUser();
101     user2.setLoginId(LOGINID_2);
102
103     List<EcompUser> users = Arrays.asList(user1, user2);
104     Files.write(concurrentEditUsers.toPath(), GSON.toJson(users).getBytes());
105
106 //    Whitebox.setInternalState(RolesConfig.class, "ROLES_CONFIG_FILE",
107 //        TestData.ROLES_CONFIG_FILE.getFilename());
108   }
109
110   @After
111   public void tearDown() throws Exception {
112     Files.deleteIfExists(concurrentUsers.toPath());
113     Files.deleteIfExists(concurrentEditUsers.toPath());
114   }
115
116   @Test
117   public void testGetUsersNoFile() throws Exception {
118     UserManager userManager = new UserManager(noFile);
119     List<EcompUser> users = userManager.getUsers();
120
121     assertThat(users, empty());
122   }
123
124   @Test
125   public void testConcurrentPush() throws Exception {
126     Callable<EcompUser> pushTask = () -> {
127       return pushTask(concurrentUsers, String.valueOf(NodeUtils.getRandomTxnId()));
128     };
129
130     List<Callable<EcompUser>> callables = Arrays.asList(pushTask, pushTask, pushTask, pushTask,
131         pushTask);
132
133     ExecutorService executor = Executors.newWorkStealingPool();
134     executor.invokeAll(callables).stream().map(future -> {
135       try {
136         return future.get();
137       } catch (Exception e) {
138         throw new IllegalStateException(e);
139       }
140     });
141
142     UserManager userManager = new UserManager(concurrentUsers);
143     assertThat(userManager.getUsers().size(), is(5));
144   }
145
146   @Test
147   public void testConcurrentPushAndEdit() throws Exception {
148     Callable<EcompUser> pushTaskRandomId = () -> {
149       return pushTask(concurrentEditUsers, String.valueOf(NodeUtils.getRandomTxnId()));
150     };
151
152     Callable<EcompUser> pushTaskId3 = () -> {
153       return pushTask(concurrentEditUsers, LOGINID_3);
154     };
155
156     Callable<EcompUser> editTaskId1 = () -> {
157       return editTask(LOGINID_1, "Bob");
158     };
159
160     Callable<EcompUser> editTaskId2 = () -> {
161       return editTask(LOGINID_2, "Jen");
162     };
163
164     Callable<EcompUser> editTaskId3 = () -> {
165       return editTask(LOGINID_3, "Amy");
166     };
167
168     List<Callable<EcompUser>> callables = Arrays.asList(pushTaskRandomId, pushTaskRandomId,
169         pushTaskId3, editTaskId1, pushTaskRandomId, pushTaskRandomId, editTaskId3, editTaskId2,
170         pushTaskRandomId);
171
172     ExecutorService executor = Executors.newWorkStealingPool();
173     List<EcompUser> userTasks = executor.invokeAll(callables).stream().map(future -> {
174       try {
175         return future.get();
176       } catch (Exception e) {
177         throw new IllegalStateException(e);
178       }
179     }).collect(Collectors.toList());
180
181     assertThat(userTasks.size(), is(9));
182
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"));
188   }
189
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);
195     return user;
196   }
197
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);
204     return user;
205   }
206 }