rule validation hotfix:
This commit is contained in:
parent
2860089712
commit
8d8d83bc25
@ -1,10 +1,12 @@
|
|||||||
package com.iqser.red.service.redaction.v1.server.model.drools;
|
package com.iqser.red.service.redaction.v1.server.model.drools;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@ -39,6 +41,12 @@ public final class RuleFileBluePrint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Set<String> getImportSplitByKeyword() {
|
||||||
|
|
||||||
|
return Arrays.stream(imports.replaceAll("\n", "").split("import")).map(String::trim).collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<BasicRule> findRulesByIdentifier(RuleIdentifier ruleIdentifier) {
|
public List<BasicRule> findRulesByIdentifier(RuleIdentifier ruleIdentifier) {
|
||||||
|
|
||||||
if (Objects.isNull(ruleIdentifier.unit())) {
|
if (Objects.isNull(ruleIdentifier.unit())) {
|
||||||
|
|||||||
@ -1,12 +1,15 @@
|
|||||||
package com.iqser.red.service.redaction.v1.server.service.drools;
|
package com.iqser.red.service.redaction.v1.server.service.drools;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.kie.api.builder.KieBuilder;
|
import org.kie.api.builder.KieBuilder;
|
||||||
import org.kie.api.builder.Message;
|
import org.kie.api.builder.Message;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.RuleFileType;
|
import com.iqser.red.service.persistence.service.v1.api.shared.model.RuleFileType;
|
||||||
import com.iqser.red.service.redaction.v1.model.DroolsSyntaxErrorMessage;
|
import com.iqser.red.service.redaction.v1.model.DroolsSyntaxErrorMessage;
|
||||||
import com.iqser.red.service.redaction.v1.model.DroolsSyntaxValidation;
|
import com.iqser.red.service.redaction.v1.model.DroolsSyntaxValidation;
|
||||||
@ -46,7 +49,7 @@ public class DroolsSyntaxValidationService {
|
|||||||
case COMPONENT -> RuleFileParser.buildBluePrintFromRulesString(RuleManagementResources.getBaseComponentRuleFileString());
|
case COMPONENT -> RuleFileParser.buildBluePrintFromRulesString(RuleManagementResources.getBaseComponentRuleFileString());
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!ruleFileBluePrint.getImports().equals(baseRuleFileBluePrint.getImports())) {
|
if (!importsAreValid(baseRuleFileBluePrint, ruleFileBluePrint)) {
|
||||||
customSyntaxValidation.getDroolsSyntaxErrorMessages()
|
customSyntaxValidation.getDroolsSyntaxErrorMessages()
|
||||||
.add(DroolsSyntaxErrorMessage.builder()
|
.add(DroolsSyntaxErrorMessage.builder()
|
||||||
.line(ruleFileBluePrint.getImportLine())
|
.line(ruleFileBluePrint.getImportLine())
|
||||||
@ -56,8 +59,10 @@ public class DroolsSyntaxValidationService {
|
|||||||
}
|
}
|
||||||
if (!ruleFileBluePrint.getGlobals().equals(baseRuleFileBluePrint.getGlobals())) {
|
if (!ruleFileBluePrint.getGlobals().equals(baseRuleFileBluePrint.getGlobals())) {
|
||||||
customSyntaxValidation.getDroolsSyntaxErrorMessages()
|
customSyntaxValidation.getDroolsSyntaxErrorMessages()
|
||||||
.add(DroolsSyntaxErrorMessage.builder().line(ruleFileBluePrint.getGlobalsLine())
|
.add(DroolsSyntaxErrorMessage.builder()
|
||||||
.column(0).message(String.format("Changing the globals is not allowed! Must be: %n%s", baseRuleFileBluePrint.getGlobals()))
|
.line(ruleFileBluePrint.getGlobalsLine())
|
||||||
|
.column(0)
|
||||||
|
.message(String.format("Changing the globals is not allowed! Must be: %n%s", baseRuleFileBluePrint.getGlobals()))
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
baseRuleFileBluePrint.getQueries().forEach(basicQuery -> {
|
baseRuleFileBluePrint.getQueries().forEach(basicQuery -> {
|
||||||
@ -72,9 +77,8 @@ public class DroolsSyntaxValidationService {
|
|||||||
});
|
});
|
||||||
baseRuleFileBluePrint.streamAllRules().forEach(basicRule -> {
|
baseRuleFileBluePrint.streamAllRules().forEach(basicRule -> {
|
||||||
if (!validateRuleIsPresent(basicRule, ruleFileBluePrint)) {
|
if (!validateRuleIsPresent(basicRule, ruleFileBluePrint)) {
|
||||||
customSyntaxValidation.getDroolsSyntaxErrorMessages()
|
int line = ruleFileBluePrint.findRulesByIdentifier(basicRule.getIdentifier()).stream().findFirst().map(BasicRule::getLine).orElse(basicRule.getLine());
|
||||||
.add(DroolsSyntaxErrorMessage.builder()
|
customSyntaxValidation.getDroolsSyntaxErrorMessages().add(DroolsSyntaxErrorMessage.builder().line(line)
|
||||||
.line(basicRule.getLine())
|
|
||||||
.column(0)
|
.column(0)
|
||||||
.message(String.format("Changing or removing the rule %s is not allowed! Must be: %n%s", basicRule.getName(), basicRule.getCode()))
|
.message(String.format("Changing or removing the rule %s is not allowed! Must be: %n%s", basicRule.getName(), basicRule.getCode()))
|
||||||
.build());
|
.build());
|
||||||
@ -84,9 +88,26 @@ public class DroolsSyntaxValidationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean importsAreValid(RuleFileBluePrint baseRuleFileBluePrint, RuleFileBluePrint ruleFileBluePrint) {
|
||||||
|
|
||||||
|
// imports may shrink, but not add anything new!
|
||||||
|
Set<String> baseImports = baseRuleFileBluePrint.getImportSplitByKeyword();
|
||||||
|
Set<String> imports = ruleFileBluePrint.getImportSplitByKeyword();
|
||||||
|
return Sets.difference(imports, baseImports).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private static boolean validateRuleIsPresent(BasicRule basicRule, RuleFileBluePrint ruleFileBluePrint) {
|
private static boolean validateRuleIsPresent(BasicRule basicRule, RuleFileBluePrint ruleFileBluePrint) {
|
||||||
|
|
||||||
return ruleFileBluePrint.findRulesByIdentifier(basicRule.getIdentifier()).stream().anyMatch(otherRule -> otherRule.getCode().equals(basicRule.getCode()));
|
return ruleFileBluePrint.findRulesByIdentifier(basicRule.getIdentifier()).stream().anyMatch(otherRule -> rulesMatch(basicRule, otherRule));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static boolean rulesMatch(BasicRule basicRule, BasicRule otherRule) {
|
||||||
|
|
||||||
|
// Name only needs to match with to lower case, everything else must match exactly
|
||||||
|
return otherRule.getName().toLowerCase(Locale.ENGLISH).equals(basicRule.getName().toLowerCase(Locale.ROOT)) && //
|
||||||
|
otherRule.getCode().replace(otherRule.getName(), "").equals(basicRule.getCode().replace(basicRule.getName(), ""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -101,7 +122,9 @@ public class DroolsSyntaxValidationService {
|
|||||||
var versionId = System.currentTimeMillis();
|
var versionId = System.currentTimeMillis();
|
||||||
var testRules = "test-rules";
|
var testRules = "test-rules";
|
||||||
KieBuilder kieBuilder = kieContainerCreationService.registerNewKieContainerVersion(testRules,
|
KieBuilder kieBuilder = kieContainerCreationService.registerNewKieContainerVersion(testRules,
|
||||||
versionId, rules.getRulesString(), RuleFileType.valueOf(rules.getRuleFileType()));
|
versionId,
|
||||||
|
rules.getRulesString(),
|
||||||
|
RuleFileType.valueOf(rules.getRuleFileType()));
|
||||||
return buildDroolsCompilerSyntaxValidation(kieBuilder);
|
return buildDroolsCompilerSyntaxValidation(kieBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -58,6 +58,22 @@ class DroolsSyntaxValidationServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SneakyThrows
|
||||||
|
void testRulesWithRemovedImportsAndChangedName() {
|
||||||
|
|
||||||
|
DroolsSyntaxValidationService droolsSyntaxValidationService = new DroolsSyntaxValidationService(new KieContainerCreationService(rulesClient));
|
||||||
|
var rulesFile = new ClassPathResource("drools/rules.drl");
|
||||||
|
|
||||||
|
String rulesString = new String(rulesFile.getInputStream().readAllBytes());
|
||||||
|
rulesString = rulesString.replaceAll("import com.iqser.red.service.redaction.v1.server.model.document.nodes.TableCell;", "");
|
||||||
|
rulesString = rulesString.replaceAll("rule \"LDS.0.0: Run local dictionary search\"", "rule \"LDS.0.0: run local dictionary search\"");
|
||||||
|
DroolsSyntaxValidation droolsSyntaxValidation = droolsSyntaxValidationService.testRules(new RuleValidationModel(RuleFileType.ENTITY.name(), rulesString));
|
||||||
|
droolsSyntaxValidation.getDroolsSyntaxErrorMessages().forEach(System.out::println);
|
||||||
|
assertTrue(droolsSyntaxValidation.isCompiled());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
void testAllRules() {
|
void testAllRules() {
|
||||||
@ -156,7 +172,8 @@ class DroolsSyntaxValidationServiceTest {
|
|||||||
var rulesFile = new ClassPathResource("drools/rules.drl");
|
var rulesFile = new ClassPathResource("drools/rules.drl");
|
||||||
|
|
||||||
String rulesString = new String(rulesFile.getInputStream().readAllBytes());
|
String rulesString = new String(rulesFile.getInputStream().readAllBytes());
|
||||||
String corruptedRules = rulesString.replaceAll("import com.iqser.red.service.redaction.v1.server.model.document.nodes.ImageType;", "");
|
String corruptedRules = rulesString.replaceAll("import com.iqser.red.service.redaction.v1.server.model.document.nodes.ImageType;",
|
||||||
|
"import com.iqser.red.service.redaction.v1.server.model.document.nodes.ImageType;\nimport com.iqser.red.service.redaction.v1.server.model.document.nodes.SomethingElse;");
|
||||||
|
|
||||||
DroolsSyntaxValidation droolsSyntaxValidation = droolsSyntaxValidationService.testRules(new RuleValidationModel(RuleFileType.ENTITY.name(), corruptedRules));
|
DroolsSyntaxValidation droolsSyntaxValidation = droolsSyntaxValidationService.testRules(new RuleValidationModel(RuleFileType.ENTITY.name(), corruptedRules));
|
||||||
assertFalse(droolsSyntaxValidation.isCompiled());
|
assertFalse(droolsSyntaxValidation.isCompiled());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user