### Question:
AccountTelemetryService { public String testRulePatterns(List<AnonymizationRule> rules, String input) { String output = input; for (AnonymizationRule rule : rules) { String decodedRule = new String(Base64.getDecoder().decode(rule.getValue().getBytes())); Pattern p = createAndCheckPattern(decodedRule); Replacer replacer = p.replacer(rule.getReplacement()); output = replacer.replace(output); } return output; } AccountTelemetryService(AccountTelemetryRepository accountTelemetryRepository); AccountTelemetry create(AccountTelemetry telemetry, String accountId); AccountTelemetry getOrDefault(String accountId); Features updateFeatures(String accountId, Features newFeatures); AccountTelemetry createDefaultAccuontTelemetry(); String testRulePatterns(List<AnonymizationRule> rules, String input);  }### Answer:
@Test public void testRulePattern() { String input = "My email card number is: 1111-2222-3333-4444 and 55555-11111"; List<AnonymizationRule> rules = new ArrayList<>(); AnonymizationRule rule1 = new AnonymizationRule(); rule1.setReplacement("[REDACTED]"); String pattern1 = "\\d{4}[^\\w]\\d{4}[^\\w]\\d{4}[^\\w]\\d{4}"; String encodedPattern1 = new String(Base64.getEncoder().encode(pattern1.getBytes())); rule1.setValue(encodedPattern1); AnonymizationRule rule2 = new AnonymizationRule(); rule2.setReplacement("[REDACTED]"); String pattern2 = "\\d{5}[^\\w]\\d{5}"; String encodedPattern2 = new String(Base64.getEncoder().encode(pattern2.getBytes())); rule2.setValue(encodedPattern2); rules.add(rule1); rules.add(rule2); String output = underTest.testRulePatterns(rules, input); assertThat(output).isEqualTo("My email card number is: [REDACTED] and [REDACTED]"); }
@Test public void testRulePatternsNoMatch() { String input = "My email card number is: 1111-2222-3333-4444"; List<AnonymizationRule> rules = new ArrayList<>(); AnonymizationRule rule = new AnonymizationRule(); rule.setReplacement("[REDACTED]"); String pattern = "\\d{8}[^\\w]\\d{8}[^\\w]\\d{8}[^\\w]\\d{8}"; String encodedPattern = new String(Base64.getEncoder().encode(pattern.getBytes())); rule.setValue(encodedPattern); rules.add(rule); String output = underTest.testRulePatterns(rules, input); assertEquals(input, output); }
@Test public void testRulePatternWithInvalidPattern() { String input = "My email card number is: 1111-2222-3333-4444"; List<AnonymizationRule> rules = new ArrayList<>(); AnonymizationRule rule = new AnonymizationRule(); rule.setReplacement("[REDACTED]"); String pattern = "\\d{8}[^[["; String encodedPattern = new String(Base64.getEncoder().encode(pattern.getBytes())); rule.setValue(encodedPattern); rules.add(rule); assertThrows(BadRequestException.class, () -> { underTest.testRulePatterns(rules, input); }); }