RED-9472: seperation of system rules

This commit is contained in:
yhampe 2024-10-21 10:33:52 +02:00
parent 848de2bc4f
commit d497c3689f

View File

@ -6,8 +6,8 @@ import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Assert;
import org.springframework.stereotype.Service;
import com.iqser.red.service.redaction.v1.model.RuleBuilderModel;
@ -61,6 +61,14 @@ public class RuleBuilderService {
log.info("starting to merge user rules update with system rules");
RuleFileBluePrint ruleFileBluePrintExisting = RuleFileParser.buildBluePrintFromRulesString(existingRules);
RuleFileBluePrint mergedRuleFileBlueprint = RuleFileParser.buildBluePrintFromRulesString(userUpdatedRules, true);
mergedRuleFileBlueprint.getRuleClasses()
.forEach(ruleClass -> {
if (systemRules.stream()
.map(RuleType::name)
.toList().contains(ruleClass.ruleType().name())) {
throw new RuntimeException("No system rule updates allowed in user rule update.");
}
});
removeAllRulesExceptSystemRulesAndCheck(ruleFileBluePrintExisting);
ruleFileBluePrintExisting.getRuleClasses()
.stream()
@ -71,6 +79,16 @@ public class RuleBuilderService {
.forEach(mergedRuleFileBlueprint::addRule);
mergedRuleFileBlueprint.setImports(ruleFileBluePrintExisting.getImports() + mergedRuleFileBlueprint.getImports());
mergedRuleFileBlueprint.setGlobals(ruleFileBluePrintExisting.getGlobals() + mergedRuleFileBlueprint.getGlobals());
mergedRuleFileBlueprint.setFunctions(Stream.concat(ruleFileBluePrintExisting.getFunctions()
.stream(),
mergedRuleFileBlueprint.getFunctions()
.stream())
.toList());
mergedRuleFileBlueprint.setDeclarations(Stream.concat(ruleFileBluePrintExisting.getDeclarations()
.stream(),
mergedRuleFileBlueprint.getDeclarations()
.stream())
.toList());
log.info("finished merging user rules update with system rules");
RuleMergingResult mergingResult = RuleMergingResult.builder()
.mergedRules(RuleFileFactory.buildRuleString(ruleFileBluePrintExisting, false, false))
@ -89,7 +107,11 @@ public class RuleBuilderService {
.collect(Collectors.toSet());
removeAllRulesExceptSystemRules(ruleFileBluePrint);
ruleFileBluePrint.getRuleClasses()
.forEach(ruleClass -> Assert.assertTrue("there was an error removing all rules except system rules", systemRuleNames.contains(ruleClass.ruleType().name())));
.forEach(ruleClass -> {
if (systemRuleNames.contains(ruleClass.ruleType().name())) {
throw new RuntimeException("there was an error removing all rules except system rules");
}
});
}
@ -122,7 +144,9 @@ public class RuleBuilderService {
.ifPresent(ruleClass -> ruleClass.ruleUnits()
.stream()
.forEach(remainingSystemRules::add));
Assert.assertTrue("There was an error removing the system rules from the file", remainingSystemRules.isEmpty());
if (!remainingSystemRules.isEmpty()) {
throw new RuntimeException("There was an error removing the system rules from the file");
}
}
}