Merge from ECOMP's repository
[vid.git] / vid-automation / src / main / java / vid / automation / test / services / UsersService.java
1 package vid.automation.test.services;
2
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;
9
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;
18
19 import static org.hamcrest.CoreMatchers.everyItem;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.greaterThan;
22
23 /**
24  * Created by itzikliderman on 08/09/2017.
25  */
26 public class UsersService {
27     private HashMap<String, User> users;
28
29     public UsersService() {
30         users = getUsersFromJson();
31         users.forEach(this::prepareUser);
32     }
33
34     HashMap<String, User> getUsersFromJson() {
35         UsersObject usersObject = null;
36         usersObject = ReadFile.getJsonFile("users", UsersObject.class);
37         return usersObject.users;
38     }
39
40     public User getUser(String userId) {
41         User res = users.get(userId);
42         System.out.println("getUser userId='" + userId + "' returned: " + res);
43
44         if (res == null) {
45             throw new RuntimeException("user id '" + userId + "' is not defined (these ARE defined: " + users.keySet() + ")");
46         }
47
48         return res;
49     }
50
51
52     private void prepareUser(String userId, User user) {
53         /*
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".
61          */
62
63         dropUser(userId);
64
65         System.out.println("Preparing user '" + userId + "': " + user);
66         System.out.println("Connecting database...");
67
68         try (Connection connection = DriverManager.getConnection(DB_CONFIG.url, DB_CONFIG.username, DB_CONFIG.password)) {
69
70             System.out.println("Database connected!");
71
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);
78
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 + "')");
81
82             List<String> roles = user.roles != null ? user.roles : new LinkedList<>();
83             roles.add("Standard User");
84
85             ListIterator<String> iter = roles.listIterator();
86             while (iter.hasNext()) {
87                 int roleNumber = userNumber + iter.nextIndex();
88
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);
91                 stmt.addBatch(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);
94                 stmt.addBatch(sql2);
95             }
96             stmt.addBatch("INSERT INTO `fn_user_role` (`USER_ID`, `ROLE_ID`, `PRIORITY`, `APP_ID`) VALUES (" + userNumber + ", 16, NULL, 1)");
97
98             int[] executeBatch = stmt.executeBatch();
99             assertThat(Ints.asList(executeBatch), everyItem(greaterThan(0)));
100
101         } catch (SQLException e) {
102             throw new IllegalStateException("Cannot connect the database!", e);
103         }
104
105     }
106
107     private void dropUser(String userId) {
108         System.out.println("Dropping user '" + userId + "'");
109         System.out.println("Connecting database...");
110
111         try (Connection connection = DriverManager.getConnection(DB_CONFIG.url, DB_CONFIG.username, DB_CONFIG.password)) {
112
113             System.out.println("Database connected!");
114
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();
121
122         } catch (SQLException e) {
123             throw new IllegalStateException("Cannot connect the database!", e);
124         }
125     }
126
127     private int getUserNumber(String userId) {
128         return (Math.abs(userId.hashCode()) % 100000) * 1000;
129     }
130
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);
135     }
136
137 }