SurvivorshipManager extends KnowledgeManager { public Map<String, List<String>> checkConflictRuleValid() { Map<String, List<String>> returnMap = new HashMap<>(); List<String> ruleColumnList = new ArrayList<>(); List<Column> executeOrder = new ArrayList<>(); Map<String, List<String>> cycDepenStatus = new HashMap<>(); for (RuleDefinition ruleDef : this.getRuleDefinitionList()) { ruleColumnList.add(ruleDef.getTargetColumn()); } for (Column col : this.getColumnList()) { String conflictTargetColumnName = col.getName(); for (ConflictRuleDefinition conRuleDef : col.getConflictResolveList()) { if (!ruleColumnList.contains(conflictTargetColumnName)) { List<String> messageList = returnMap.get(conflictTargetColumnName); if (messageList == null) { messageList = new ArrayList<>(); returnMap.put(conflictTargetColumnName, messageList); } String errorMessage = conflictTargetColumnName + " does not contain any survived value"; if (!messageList.contains(errorMessage)) { messageList.add(errorMessage); } } if (Function.SurviveAs == conRuleDef.getFunction()) { String conflictRefColumnName = conRuleDef.getReferenceColumn(); if (!ruleColumnList.contains(conflictRefColumnName)) { List<String> messageList = returnMap.get(conflictRefColumnName); if (messageList == null) { messageList = new ArrayList<>(); returnMap.put(conflictRefColumnName, messageList); } String errorMessage = conflictRefColumnName + " does not contain any survived value"; if (!messageList.contains(errorMessage)) { messageList.add(errorMessage); } } if (cycDepenStatus.get(conflictTargetColumnName) == null) { cycDepenStatus.put(conflictTargetColumnName, new ArrayList<String>()); } boolean checkCycDependency = checkCycDependency(conflictTargetColumnName, conflictRefColumnName, executeOrder, cycDepenStatus); if (!checkCycDependency) { List<String> messageList = returnMap.get(conflictRefColumnName); if (messageList == null) { messageList = new ArrayList<>(); returnMap.put(conflictRefColumnName, messageList); } String errorMessage = conflictTargetColumnName + " cannot be survived as " + conflictRefColumnName + " because of circular dependency"; if (!messageList.contains(errorMessage)) { messageList.add(errorMessage); } } break; } } } if (returnMap.size() == 0) { this.getDataSet().setColumnOrder(fillOtherColumn(executeOrder)); } return returnMap; } SurvivorshipManager(String rulePath, String packageName); KnowledgeBase getKnowledgeBase(); DataSet getDataSet(); void initKnowledgeBase(); void initKnowledgeBase(Map<org.kie.api.io.ResourceType, List<InputStream>> streamsMap); static Resource newResource(String packagePath, boolean isClassPathResource); static Resource newResource(InputStream stream); Column getColumnByName(String columnName); boolean runSessionWithJava(Object[][] data); boolean runSession(Object[][] data); Map<String, List<String>> checkConflictRuleValid(); Map<String, Object> getSurvivorMap(); List<HashSet<String>> getConflictList(); HashSet<String> getConflictsOfSurvivor();  }
@Test public void testCheckConflictRuleValid() { manager = new SurvivorshipManager(SampleData.RULE_PATH, SampleData.PKG_NAME); for (String str : SampleDataConflict.COLUMNS_CONFLICT.keySet()) { Column column = new Column(str, SampleDataConflict.COLUMNS_CONFLICT.get(str)); for (ConflictRuleDefinition element : SampleDataConflictCheckRule.RULES_CONFLICT_RESOLVE) { if (column.getName().equals(element.getTargetColumn())) { column.getConflictResolveList().add(element); } } manager.getColumnList().add(column); } for (RuleDefinition element : SampleDataConflictCheckRule.RULES) { manager.addRuleDefinition(element); } Map<String, List<String>> checkConflictRuleValid = manager.checkConflictRuleValid(); assertTrue("firstName is not exist survived value so that it must be invalid value", checkConflictRuleValid.containsKey("firstName")); assertEquals("firstName is not exist survived value so that it must be invalid value", "firstName does not contain any survived value", checkConflictRuleValid.get("firstName").get(0)); assertTrue("lastName is not exist survived value so that it must be invalid value", checkConflictRuleValid.containsKey("lastName")); assertEquals("lastName is not exist survived value so that it must be invalid value", "lastName does not contain any survived value", checkConflictRuleValid.get("lastName").get(0)); }
@Test public void testCheckConflictRuleValidWith2RulesCycDependency() { manager = new SurvivorshipManager(SampleData.RULE_PATH, SampleData.PKG_NAME); for (String str : SampleDataConflict.COLUMNS_CONFLICT.keySet()) { Column column = new Column(str, SampleDataConflict.COLUMNS_CONFLICT.get(str)); for (ConflictRuleDefinition element : SampleDataConflictCheckRule.RULES_CONFLICT_RESOLVE_CASE2) { if (column.getName().equals(element.getTargetColumn())) { column.getConflictResolveList().add(element); } } manager.getColumnList().add(column); } for (RuleDefinition element : SampleDataConflictCheckRule.RULES) { manager.addRuleDefinition(element); } Map<String, List<String>> checkConflictRuleValid = manager.checkConflictRuleValid(); assertTrue("city1 and city2 should be exist circular dependency", checkConflictRuleValid.containsKey("city1")); assertEquals("city2 cannot be survived as city1 because of circular dependency", "city2 cannot be survived as city1 because of circular dependency", checkConflictRuleValid.get("city1").get(0)); assertTrue("city2 and city1 should be exist circular dependency", checkConflictRuleValid.containsKey("city2")); assertEquals("city1 cannot be survived as city2 because of circular dependency", "city1 cannot be survived as city2 because of circular dependency", checkConflictRuleValid.get("city2").get(0)); }
@Test public void testCheckConflictRuleValidWith3RulesCycDependency() { manager = new SurvivorshipManager(SampleData.RULE_PATH, SampleData.PKG_NAME); for (String str : SampleDataConflict.COLUMNS_CONFLICT.keySet()) { Column column = new Column(str, SampleDataConflict.COLUMNS_CONFLICT.get(str)); for (ConflictRuleDefinition element : SampleDataConflictCheckRule.RULES_CONFLICT_RESOLVE_CASE3) { if (column.getName().equals(element.getTargetColumn())) { column.getConflictResolveList().add(element); } } manager.getColumnList().add(column); } for (RuleDefinition element : SampleDataConflictCheckRule.RULES_CASE3) { manager.addRuleDefinition(element); } Map<String, List<String>> checkConflictRuleValid = manager.checkConflictRuleValid(); assertTrue("city1 and city2 should be exist circular dependency", checkConflictRuleValid.containsKey("city1")); assertTrue("city2 and id should be exist circular dependency", checkConflictRuleValid.containsKey("city2")); assertTrue("id and city1 should be exist circular dependency", checkConflictRuleValid.containsKey("id")); assertEquals("city2 cannot be survived as city1 because of circular dependency", "city2 cannot be survived as city1 because of circular dependency", checkConflictRuleValid.get("city1").get(0)); assertEquals("id cannot be survived as city2 because of circular dependency", "id cannot be survived as city2 because of circular dependency", checkConflictRuleValid.get("city2").get(0)); assertEquals("city1 cannot be survived as id because of circular dependency", "city1 cannot be survived as id because of circular dependency", checkConflictRuleValid.get("id").get(0)); }