|
| 1 | +package com.tsystems.sbs; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.regex.Matcher; |
| 7 | +import java.util.regex.Pattern; |
| 8 | + |
| 9 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 10 | +import static org.hamcrest.Matchers.greaterThan; |
| 11 | +import static org.junit.Assert.assertEquals; |
| 12 | + |
| 13 | + |
| 14 | +public class DefaultRegexpPairsDDTest { |
| 15 | + private List<RegexpPair> getDefaultRegexpPairs() { |
| 16 | + return DefaultRegexpPairs.getDefaultRegexesDD(); |
| 17 | + } |
| 18 | + @Test |
| 19 | + public void testDefaultPairsList() { |
| 20 | + List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); |
| 21 | + assertThat(defaultRegexpPairs.size(), greaterThan(0)); |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testDefaultPairsApi() { |
| 27 | + List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); |
| 28 | + |
| 29 | + // Define the input string 32 characters |
| 30 | + String input = "curl -X GET \"https://api.datadoghq.eu/api/v1/validate\" -H \"Accept: application/json\" -H \"DD-API-KEY: characteristicallycharacteristic\""; |
| 31 | + String expected = "curl -X GET \"https://api.datadoghq.eu/api/v1/validate\" -H \"Accept: application/json\" -H \"DD-API-KEY: ********\""; |
| 32 | + |
| 33 | + |
| 34 | + StringBuilder replacedInput = new StringBuilder(input); |
| 35 | + |
| 36 | + for (RegexpPair pair : defaultRegexpPairs) { |
| 37 | + String pattern = pair.getRegexp(); |
| 38 | + String replacement = pair.getReplacement(); |
| 39 | + |
| 40 | + Pattern regexPattern = Pattern.compile(pattern); |
| 41 | + Matcher matcher = regexPattern.matcher(replacedInput); |
| 42 | + |
| 43 | + while (matcher.find()) { |
| 44 | + String matchedPattern = matcher.group(); |
| 45 | + String replacedString = replacement; |
| 46 | + |
| 47 | + // Replace all occurrences of $n with the matched groups |
| 48 | + for (int i = 1; i <= matcher.groupCount(); i++) { |
| 49 | + String group = matcher.group(i); |
| 50 | + replacedString = replacedString.replace("$" + i, group); |
| 51 | + } |
| 52 | + |
| 53 | + replacedInput.replace(matcher.start(), matcher.end(), replacedString); |
| 54 | + matcher.region(matcher.start() + replacedString.length(), replacedInput.length()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + String replacedInputString = replacedInput.toString(); |
| 59 | + System.out.println("Replaced input result: " + replacedInputString); |
| 60 | + |
| 61 | + // Test the behavior |
| 62 | + assertEquals(expected, replacedInputString); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testDefaultPairsKey() { |
| 67 | + List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); |
| 68 | + |
| 69 | + // Define the input string 32 characters |
| 70 | + String input = "datadog key = 3c0c3965368a6b10f7640dbda46abfd2 secret= 3c0c3965368a6b10f7640dbda46abfdca981c2d3"; |
| 71 | + String expected = "datadog key = ******** secret= ********"; |
| 72 | + |
| 73 | + |
| 74 | + StringBuilder replacedInput = new StringBuilder(input); |
| 75 | + |
| 76 | + for (RegexpPair pair : defaultRegexpPairs) { |
| 77 | + String pattern = pair.getRegexp(); |
| 78 | + String replacement = pair.getReplacement(); |
| 79 | + |
| 80 | + Pattern regexPattern = Pattern.compile(pattern); |
| 81 | + Matcher matcher = regexPattern.matcher(replacedInput); |
| 82 | + |
| 83 | + while (matcher.find()) { |
| 84 | + String matchedPattern = matcher.group(); |
| 85 | + String replacedString = replacement; |
| 86 | + |
| 87 | + // Replace all occurrences of $n with the matched groups |
| 88 | + for (int i = 1; i <= matcher.groupCount(); i++) { |
| 89 | + String group = matcher.group(i); |
| 90 | + replacedString = replacedString.replace("$" + i, group); |
| 91 | + } |
| 92 | + |
| 93 | + replacedInput.replace(matcher.start(), matcher.end(), replacedString); |
| 94 | + matcher.region(matcher.start() + replacedString.length(), replacedInput.length()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + String replacedInputString = replacedInput.toString(); |
| 99 | + System.out.println("Replaced input result: " + replacedInputString); |
| 100 | + |
| 101 | + // Test the behavior |
| 102 | + assertEquals(expected, replacedInputString); |
| 103 | + } |
| 104 | + @Test |
| 105 | + public void testDefaultPairsToken() { |
| 106 | + List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); |
| 107 | + |
| 108 | + // Define the input string 32 characters |
| 109 | + String input = "dAtAdOg token = \"3c0c3965368a6b10f7640dbda46abfdc\";"; |
| 110 | + String expected = "dAtAdOg token = \"********\";"; |
| 111 | + |
| 112 | + |
| 113 | + StringBuilder replacedInput = new StringBuilder(input); |
| 114 | + |
| 115 | + for (RegexpPair pair : defaultRegexpPairs) { |
| 116 | + String pattern = pair.getRegexp(); |
| 117 | + String replacement = pair.getReplacement(); |
| 118 | + |
| 119 | + Pattern regexPattern = Pattern.compile(pattern); |
| 120 | + Matcher matcher = regexPattern.matcher(replacedInput); |
| 121 | + |
| 122 | + while (matcher.find()) { |
| 123 | + String matchedPattern = matcher.group(); |
| 124 | + String replacedString = replacement; |
| 125 | + |
| 126 | + // Replace all occurrences of $n with the matched groups |
| 127 | + for (int i = 1; i <= matcher.groupCount(); i++) { |
| 128 | + String group = matcher.group(i); |
| 129 | + replacedString = replacedString.replace("$" + i, group); |
| 130 | + } |
| 131 | + |
| 132 | + replacedInput.replace(matcher.start(), matcher.end(), replacedString); |
| 133 | + matcher.region(matcher.start() + replacedString.length(), replacedInput.length()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + String replacedInputString = replacedInput.toString(); |
| 138 | + System.out.println("Replaced input result: " + replacedInputString); |
| 139 | + |
| 140 | + // Test the behavior |
| 141 | + assertEquals(expected, replacedInputString); |
| 142 | + } |
| 143 | +} |
| 144 | + |
0 commit comments