1 package vid.automation.test.services;
3 import com.google.common.primitives.Ints;
4 import org.apache.commons.lang3.StringUtils;
5 import vid.automation.test.model.User;
6 import vid.automation.test.model.UsersObject;
7 import vid.automation.test.utils.DB_CONFIG;
8 import vid.automation.test.utils.ReadFile;
10 import java.sql.Connection;
11 import java.sql.DriverManager;
12 import java.sql.SQLException;
13 import java.sql.Statement;
14 import java.util.HashMap;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.ListIterator;
19 import static org.hamcrest.CoreMatchers.everyItem;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.greaterThan;
24 * Created by itzikliderman on 08/09/2017.
26 public class UsersService {
27 private HashMap<String, User> users;
29 public UsersService() {
30 users = getUsersFromJson();
31 users.forEach(this::prepareUser);
34 HashMap<String, User> getUsersFromJson() {
35 UsersObject usersObject = null;
36 usersObject = ReadFile.getJsonFile("users", UsersObject.class);
37 return usersObject.users;
40 public User getUser(String userId) {
41 User res = users.get(userId);
42 System.out.println("getUser userId='" + userId + "' returned: " + res);
45 throw new RuntimeException("user id '" + userId + "' is not defined (these ARE defined: " + users.keySet() + ")");
52 private void prepareUser(String userId, User user) {
54 Creates a user in the DB, were:
55 - Login user name is a deterministic number, hashed from the userId string, with 3 trailing zeroes,
56 and two leading letters from the userId itself; e.g. "mo26063000" for mobility.
57 - Login user name == user password
58 - 'user.credentials.userId' and 'user.credentials.password' input fields are overridden with the generated values.
59 - Roles are "read" (roleId==16) and all other roles in object (like subscriberName___serviceType___tenant).
60 - Yielded role ids are the successive numbers after the user name. e.g "57174000", "57174001", "57174002".
65 System.out.println("Preparing user '" + userId + "': " + user);
66 System.out.println("Connecting database...");
68 try (Connection connection = DriverManager.getConnection(DB_CONFIG.url, DB_CONFIG.username, DB_CONFIG.password)) {
70 System.out.println("Database connected!");
72 ///////////////////////////////
73 // Add user with specific roles
74 Statement stmt = connection.createStatement();
75 int userNumber = getUserNumber(userId);
76 user.credentials.userId = getLoginId(userId);
77 user.credentials.password = getLoginId(userId);
79 stmt.addBatch("INSERT INTO `fn_user` (`USER_ID`, `ORG_USER_ID`, `FIRST_NAME`, `LOGIN_ID`, `LOGIN_PWD`) " +
80 "VALUES (" + userNumber + ", '" + userId + "', '" + userId + "', '" + user.credentials.userId + "', '" + user.credentials.password + "')");
82 List<String> roles = user.roles != null ? user.roles : new LinkedList<>();
83 roles.add("Standard User");
85 ListIterator<String> iter = roles.listIterator();
86 while (iter.hasNext()) {
87 int roleNumber = userNumber + iter.nextIndex();
89 String sql = "INSERT INTO `fn_role` (`ROLE_ID`, `ROLE_NAME`, `ACTIVE_YN`, `PRIORITY`) VALUES (" + roleNumber + ", '" + iter.next() + "', 'Y', " + 5 + ")";
90 System.out.println(sql);
92 String sql2 = "INSERT INTO `fn_user_role` (`USER_ID`, `ROLE_ID`, `PRIORITY`, `APP_ID`) VALUES (" + userNumber + ", " + roleNumber + ", NULL, 1)";
93 System.out.println(sql2);
96 stmt.addBatch("INSERT INTO `fn_user_role` (`USER_ID`, `ROLE_ID`, `PRIORITY`, `APP_ID`) VALUES (" + userNumber + ", 16, NULL, 1)");
98 int[] executeBatch = stmt.executeBatch();
99 assertThat(Ints.asList(executeBatch), everyItem(greaterThan(0)));
101 } catch (SQLException e) {
102 throw new IllegalStateException("Cannot connect the database!", e);
107 private void dropUser(String userId) {
108 System.out.println("Dropping user '" + userId + "'");
109 System.out.println("Connecting database...");
111 try (Connection connection = DriverManager.getConnection(DB_CONFIG.url, DB_CONFIG.username, DB_CONFIG.password)) {
113 System.out.println("Database connected!");
115 int userNumber = getUserNumber(userId);
116 Statement stmt = connection.createStatement();
117 stmt.addBatch("DELETE FROM `fn_user_role` WHERE `USER_ID` = " + userNumber);
118 stmt.addBatch("DELETE FROM `fn_role` WHERE `ROLE_ID` BETWEEN " + userNumber + " AND " + (userNumber + 100));
119 stmt.addBatch("DELETE FROM `fn_user` WHERE `USER_ID` = " + userNumber);
120 int[] executeBatch = stmt.executeBatch();
122 } catch (SQLException e) {
123 throw new IllegalStateException("Cannot connect the database!", e);
127 private int getUserNumber(String userId) {
128 return (Math.abs(userId.hashCode()) % 100000) * 1000;
131 private String getLoginId(String userId) {
132 int userNumber = getUserNumber(userId);
133 final String twoCharacters = StringUtils.substring(userId,0, 2).toLowerCase();
134 return String.format("%s%d", twoCharacters, userNumber);