RED-7834: fixes for migration

* remove color
* fix manualChangeOverwrite
* fix checkstyle
This commit is contained in:
Kilian Schuettler 2023-12-07 11:55:39 +01:00
parent ad6e628e0d
commit 9d876a32ce
7 changed files with 29 additions and 21 deletions

View File

@ -2,7 +2,6 @@ package com.iqser.red.service.redaction.v1.server.model.drools;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public record RuleType(String name) { public record RuleType(String name) {
static Pattern alphaNumeric = Pattern.compile("^[a-zA-Z0-9]+$"); static Pattern alphaNumeric = Pattern.compile("^[a-zA-Z0-9]+$");

View File

@ -123,7 +123,7 @@ public class Main {
private static String escapeRuleStringIfOptionIsSet(CommandLine finalCmd, String ruleFileString) { private static String escapeRuleStringIfOptionIsSet(CommandLine finalCmd, String ruleFileString) {
if (finalCmd.hasOption("e")) { if (finalCmd.hasOption("e")) {
ruleFileString = RuleFileIO.escapeAndWrap(ruleFileString); return RuleFileIO.escapeAndWrap(ruleFileString);
} }
return ruleFileString; return ruleFileString;
} }

View File

@ -96,7 +96,7 @@ public class RuleFileParser {
/** /**
* Creates a BluePrint from all redact manager, documine and component rule files * Creates a BluePrint from all redact manager, documine and component rule files.
* *
* @return RuleFileBluePrint containing all rules from any all rule files * @return RuleFileBluePrint containing all rules from any all rule files
*/ */

View File

@ -19,6 +19,7 @@ public record RuleIdentifier(@NonNull RuleType type, Integer unit, Integer id) {
return fromString(identifier); return fromString(identifier);
} }
public static RuleIdentifier fromRuleType(RuleType ruleType) { public static RuleIdentifier fromRuleType(RuleType ruleType) {
return new RuleIdentifier(ruleType, null, null); return new RuleIdentifier(ruleType, null, null);
@ -26,6 +27,7 @@ public record RuleIdentifier(@NonNull RuleType type, Integer unit, Integer id) {
public static RuleIdentifier fromString(String identifier) { public static RuleIdentifier fromString(String identifier) {
String[] values = identifier.split("\\."); String[] values = identifier.split("\\.");
RuleType type = RuleType.fromString(values[0]); RuleType type = RuleType.fromString(values[0]);
Integer group = values.length > 1 ? parseIntOrStar(values[1]) : null; Integer group = values.length > 1 ? parseIntOrStar(values[1]) : null;
@ -33,6 +35,7 @@ public record RuleIdentifier(@NonNull RuleType type, Integer unit, Integer id) {
return new RuleIdentifier(type, group, id); return new RuleIdentifier(type, group, id);
} }
public static Set<RuleIdentifier> fromListOfIdentifiersString(String input) { public static Set<RuleIdentifier> fromListOfIdentifiersString(String input) {
return Arrays.stream(input.split(",")).map(String::trim).map(RuleIdentifier::fromString).collect(Collectors.toSet()); return Arrays.stream(input.split(",")).map(String::trim).map(RuleIdentifier::fromString).collect(Collectors.toSet());
@ -40,8 +43,10 @@ public record RuleIdentifier(@NonNull RuleType type, Integer unit, Integer id) {
private static Integer parseIntOrStar(String value) { private static Integer parseIntOrStar(String value) {
value = value.replaceAll("\r","");
return !value.equals("*") ? Integer.parseInt(value) : null; String cleanedValue = value;
cleanedValue = cleanedValue.replaceAll("\r", "");
return !cleanedValue.equals("*") ? Integer.parseInt(cleanedValue) : null;
} }

View File

@ -28,8 +28,8 @@ public class RuleType {
public static RuleType fromString(String value) { public static RuleType fromString(String value) {
value = value.replaceAll("\r", ""); String cleanedValue = value.replaceAll("\r", "");
return new RuleType(value); return new RuleType(cleanedValue);
} }

View File

@ -39,7 +39,7 @@ import lombok.experimental.UtilityClass;
@UtilityClass @UtilityClass
public class OldRulesParser { public class OldRulesParser {
final static List<String> HEADERS = List.of("id", "old rule names", "old rule code", "translates to"); static List<String> HEADERS = List.of("id", "old rule names", "old rule code", "translates to");
@SneakyThrows @SneakyThrows
@ -117,7 +117,7 @@ public class OldRulesParser {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setHeader(HEADERS.toArray(String[]::new)).build(); CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setHeader(HEADERS.toArray(String[]::new)).build();
try (final CSVPrinter printer = new CSVPrinter(sw, csvFormat)) { try (CSVPrinter printer = new CSVPrinter(sw, csvFormat)) {
for (OldRulesCsvRecord record : records) { for (OldRulesCsvRecord record : records) {
printer.printRecord(Stream.of(record.id, RuleFileIO.escapeAndWrap(record.names), RuleFileIO.escapeAndWrap(record.code), record.translatesTo)); printer.printRecord(Stream.of(record.id, RuleFileIO.escapeAndWrap(record.names), RuleFileIO.escapeAndWrap(record.code), record.translatesTo));
} }
@ -162,17 +162,18 @@ public class OldRulesParser {
private List<RuleIdentifier> parseRuleIdentifiers(String value) { private List<RuleIdentifier> parseRuleIdentifiers(String value) {
if (value.startsWith("[")) { String cleanedValue = value;
value = value.substring(1); if (cleanedValue.startsWith("[")) {
cleanedValue = cleanedValue.substring(1);
} }
if (value.endsWith("]")) { if (cleanedValue.endsWith("]")) {
value = value.substring(0, value.length() - 1); cleanedValue = cleanedValue.substring(0, value.length() - 1);
} }
if (value.isEmpty() || value.isBlank()) { if (cleanedValue.isEmpty() || cleanedValue.isBlank()) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<RuleIdentifier> ruleIdentifiers = new LinkedList<>(); List<RuleIdentifier> ruleIdentifiers = new LinkedList<>();
for (String identifier : value.split(", ")) { for (String identifier : cleanedValue.split(", ")) {
ruleIdentifiers.add(RuleIdentifier.fromString(identifier)); ruleIdentifiers.add(RuleIdentifier.fromString(identifier));
} }
return ruleIdentifiers; return ruleIdentifiers;

View File

@ -20,9 +20,11 @@ public final class RuleFileIO {
if (rulesString.isBlank() || rulesString.isEmpty()) { if (rulesString.isBlank() || rulesString.isEmpty()) {
return rulesString; return rulesString;
} }
rulesString = rulesString.substring(1, rulesString.length() - 1);
rulesString = rulesString.translateEscapes(); String ruleStringToUnescape = rulesString;
return rulesString; ruleStringToUnescape = ruleStringToUnescape.substring(1, ruleStringToUnescape.length() - 1);
ruleStringToUnescape = ruleStringToUnescape.translateEscapes();
return ruleStringToUnescape;
} }
@ -43,9 +45,10 @@ public final class RuleFileIO {
public String escapeAndWrap(String rulesString) { public String escapeAndWrap(String rulesString) {
rulesString = StringEscapeUtils.escapeJava(rulesString); String rulesStringToEscape = rulesString;
rulesString = "\"" + rulesString + "\""; rulesStringToEscape = StringEscapeUtils.escapeJava(rulesStringToEscape);
return rulesString; rulesStringToEscape = "\"" + rulesStringToEscape + "\"";
return rulesStringToEscape;
} }