Advertisement
ibragimova_mariam

CheckRecord.java

Jul 12th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package com.exactprosystems.clearth.automation.actions;
  2.  
  3. import com.exactprosystems.clearth.automation.Action;
  4. import com.exactprosystems.clearth.automation.GlobalContext;
  5. import com.exactprosystems.clearth.automation.MatrixContext;
  6. import com.exactprosystems.clearth.automation.StepContext;
  7. import com.exactprosystems.clearth.automation.exceptions.ResultException;
  8. import com.exactprosystems.clearth.automation.report.Result;
  9. import com.exactprosystems.clearth.automation.report.ResultDetail;
  10. import com.exactprosystems.clearth.automation.report.results.DefaultResult;
  11. import com.exactprosystems.clearth.automation.report.results.DetailedResult;
  12. import com.exactprosystems.clearth.utils.ComparisonUtils;
  13.  
  14. import java.util.ArrayList;
  15. import java.util.Map;
  16.  
  17. public class CheckRecord extends Action {
  18.  
  19. private static final String RECORD_ID = "RecordId";
  20.  
  21. public static ArrayList<Map<String, String>> getTableFromContextByKey(MatrixContext context, String key) {
  22. @SuppressWarnings("unchecked") ArrayList<Map<String, String>> table =
  23. (ArrayList<Map<String, String>>) context.getContext(key);
  24. return table;
  25. }
  26.  
  27. private int getRowByParam(ArrayList<Map<String, String>> table, String matrixRecordId) {
  28. int recordIdIndex = -1;
  29. for (int i = 0; i < table.size(); i++) {
  30. if (ComparisonUtils.compareValues(matrixRecordId, table.get(i).get(RECORD_ID)) &&
  31. ComparisonUtils.compareValues(LoadFile.ROW_UNCHECKED, table.get(i).get(LoadFile.ROW_STATUS))){
  32. recordIdIndex = i;
  33. break;
  34. }
  35. }
  36. return recordIdIndex;
  37. }
  38.  
  39. private void setStatus(ArrayList<Map<String, String>> table, int recordIdIndex, String status) {
  40. table.get(recordIdIndex).put(LoadFile.ROW_STATUS, status);
  41. }
  42.  
  43. private DetailedResult getResult(ArrayList<Map<String, String>> table, int recordIdIndex) {
  44. DetailedResult result = new DetailedResult();
  45. boolean isRecordsIdentical = true;
  46. for (String param : inputParams.keySet()) {
  47. String exp = inputParams.get(param),
  48. act = table.get(recordIdIndex).get(param);
  49. ResultDetail rd = new ResultDetail(param, exp, act, exp.equals(act));
  50. result.addResultDetail(rd);
  51. if (!rd.isIdentical()) {
  52. isRecordsIdentical = false;
  53. }
  54. }
  55. if(!isRecordsIdentical)
  56. result.setComment("Rows are not identical");
  57.  
  58. setStatus(table, recordIdIndex, isRecordsIdentical ? LoadFile.ROW_SUCCESS: LoadFile.ROW_FAIL);
  59.  
  60. return result;
  61. }
  62.  
  63. @Override
  64. protected Result run(StepContext stepContext, MatrixContext matrixContext, GlobalContext globalContext) {
  65. String recordIdMatrixValue;
  66. try {
  67. recordIdMatrixValue = LoadFile.getValueByParam(RECORD_ID, inputParams);
  68. } catch (ResultException e) {
  69. return DefaultResult.failedWithException(e);
  70. }
  71.  
  72. ArrayList<Map<String, String>> table = getTableFromContextByKey(matrixContext, LoadFile.DATA_TABLE);
  73. if (table == null) {
  74. return DefaultResult.failedWithComment("Do not get the table with data. Check the LoadFile action");
  75. }
  76.  
  77. int recordIdIndex = getRowByParam(table, recordIdMatrixValue);
  78. if (recordIdIndex < 0) {
  79. return DefaultResult.failedWithComment(RECORD_ID + " param is absent in record");
  80. }
  81.  
  82. return getResult(table, recordIdIndex);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement