diff options
author | 2018-08-12 14:51:28 +0300 | |
---|---|---|
committer | 2018-08-12 15:02:57 +0300 | |
commit | 4a4dcc5185f8ba5a28c7f9fef509f32c0c2389e6 (patch) | |
tree | 23e55ee7e1ad9b91bcc3ef1dbe1fb7b183f8b2b6 /vid-automation/src/main/java/vid/automation/test/services/CategoryParamsService.java | |
parent | 661a24fd57de02869a9771761e0fcba7eb77d121 (diff) |
vid-automation selenium tests
Change-Id: I6c1b0a0cf3bbfa4314c81f0cc72507db805ec632
Issue-ID: VID-281
Signed-off-by: Sonsino, Ofir (os0695) <os0695@intl.att.com>
Diffstat (limited to 'vid-automation/src/main/java/vid/automation/test/services/CategoryParamsService.java')
-rw-r--r-- | vid-automation/src/main/java/vid/automation/test/services/CategoryParamsService.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/vid-automation/src/main/java/vid/automation/test/services/CategoryParamsService.java b/vid-automation/src/main/java/vid/automation/test/services/CategoryParamsService.java new file mode 100644 index 00000000..4fef91ff --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/services/CategoryParamsService.java @@ -0,0 +1,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; + } + +} |