AlertMatcher { boolean matches(Alert alert) { for (Rule rule : rules) { if (rule.matches(alert)) { return true; } } return false; } AlertMatcher(List<AlertConfig.Matcher> matchers);  final List<Rule> rules; }
@Test void matchAppAndErrorCode() { var matcher = new AlertMatcher(List.of( matcher(List.of("website", "backoffice"), List.of("PATH_NOT_FOUND"), null, List.of()), matcher(List.of("frontend-website"), List.of("API_ERROR_401", "FORBIDDEN"), null, List.of()), matcher(List.of(), List.of("VALIDATION_ERROR"), null, List.of()), matcher(List.of(), List.of("UNAUTHORIZED"), null, List.of()) )); assertThat(matcher.matches(alert("website", "PATH_NOT_FOUND", Severity.WARN, "trace"))).isTrue(); assertThat(matcher.matches(alert("frontend-website", "FORBIDDEN", Severity.WARN, "trace"))).isTrue(); assertThat(matcher.matches(alert("customer-service", "FORBIDDEN", Severity.WARN, "trace"))).isFalse(); assertThat(matcher.matches(alert("website", "METHOD_NOT_ALLOWED", Severity.WARN, "trace"))).isFalse(); assertThat(matcher.matches(alert("customer-service", "VALIDATION_ERROR", Severity.WARN, "trace"))).isTrue(); assertThat(matcher.matches(alert("customer-service", "UNAUTHORIZED", Severity.WARN, "trace"))).isTrue(); }
@Test void matchesSeverityAndIndex() { var matcher = new AlertMatcher(List.of( matcher(List.of(), List.of(), Severity.ERROR, List.of("event")), matcher(List.of(), List.of(), Severity.WARN, List.of("trace", "stat")), matcher(List.of("website"), List.of("PATH_NOT_FOUND"), Severity.ERROR, List.of("trace")) )); assertThat(matcher.matches(alert("website", "UNAUTHORIZED", Severity.ERROR, "event"))).isTrue(); assertThat(matcher.matches(alert("customer-service", "UNAUTHORIZED", Severity.ERROR, "event"))).isTrue(); assertThat(matcher.matches(alert("website", "UNAUTHORIZED", Severity.ERROR, "trace"))).isFalse(); assertThat(matcher.matches(alert("customer-service", "UNAUTHORIZED", Severity.ERROR, "trace"))).isFalse(); assertThat(matcher.matches(alert("website", "UNAUTHORIZED", Severity.WARN, "trace"))).isTrue(); assertThat(matcher.matches(alert("customer-service", "UNAUTHORIZED", Severity.WARN, "trace"))).isTrue(); assertThat(matcher.matches(alert("website", "UNAUTHORIZED", Severity.WARN, "event"))).isFalse(); assertThat(matcher.matches(alert("customer-service", "UNAUTHORIZED", Severity.WARN, "event"))).isFalse(); assertThat(matcher.matches(alert("website", "PATH_NOT_FOUND", Severity.ERROR, "trace"))).isTrue(); assertThat(matcher.matches(alert("customer-service", "PATH_NOT_FOUND", Severity.ERROR, "trace"))).isFalse(); assertThat(matcher.matches(alert("website", "UNAUTHORIZED", Severity.ERROR, "trace"))).isFalse(); }