aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/services/CategoryParamsService.java
blob: 4fef91fff20b1ce16cd314ce1df0b04a2aa2e36f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package vid.automation.test.services;

import com.google.common.primitives.Ints;
import vid.automation.test.model.CategoryOption;
import vid.automation.test.model.CategoryOptionList;
import vid.automation.test.utils.DB_CONFIG;
import vid.automation.test.utils.ReadFile;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;

/**
 * Created by itzikliderman on 08/09/2017.
 */
public class CategoryParamsService {
    private List<CategoryOption> categoryParams;

    public CategoryParamsService() {
        categoryParams = getCategoryParamsFromJson();
        categoryParams.forEach(this::prepareCategoryParam);
    }

    List<CategoryOption> getCategoryParamsFromJson() {
        CategoryOptionList categoryParamsObject = null;
        categoryParamsObject = ReadFile.getJsonFile("categoryParams", CategoryOptionList.class);
        return categoryParamsObject.categories;
    }

    private void prepareCategoryParam(CategoryOption categoryParam) {
        /*
        Creates a category parameter option in the DB.
         */

        dropCategoryParam(categoryParam);

        System.out.println("Preparing category parameter '" + categoryParam.categoryId + "': " + categoryParam.name);
        System.out.println("Connecting database...");

        try (Connection connection = DriverManager.getConnection(DB_CONFIG.url, DB_CONFIG.username, DB_CONFIG.password)) {

            System.out.println("Database connected!");

            ///////////////////////////////
            // Add category param option
            Statement stmt = connection.createStatement();

            int id = getId(categoryParam.name);
            stmt.addBatch("INSERT INTO `vid_category_parameter_option` (`CATEGORY_OPT_DB_ID`, `CATEGORY_OPT_APP_ID`, `NAME`, `CATEGORY_ID`) " +
                    "VALUES (" + id + ", '" + (categoryParam.appId != null ? categoryParam.appId : categoryParam.name) + "', '" + categoryParam.name + "', '" + categoryParam.categoryId + "') " +
                    "ON DUPLICATE KEY UPDATE NAME='"+categoryParam.name+"'");

            int[] executeBatch = stmt.executeBatch();
            assertThat(Ints.asList(executeBatch), everyItem(greaterThan(0)));

        } catch (SQLException e) {
            throw new IllegalStateException("Cannot connect the database!", e);
        }

    }

    private void dropCategoryParam(CategoryOption categoryParam) {
        System.out.println("Dropping categoryParam '" + categoryParam.name + "'");
        System.out.println("Connecting database...");

        try (Connection connection = DriverManager.getConnection(DB_CONFIG.url, DB_CONFIG.username, DB_CONFIG.password)) {

            System.out.println("Database connected!");

            Statement stmt = connection.createStatement();
            int id = getId(categoryParam.name);
            stmt.addBatch("DELETE FROM `vid_category_parameter_option` WHERE `CATEGORY_OPT_DB_ID` = '" + id + "'");
            int[] executeBatch = stmt.executeBatch();

        } catch (SQLException e) {
            throw new IllegalStateException("Cannot connect the database!", e);
        }
    }

    private int getId(String id) {
        return (Math.abs(id.hashCode()) % 100000) * 1000;
    }

}