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