@Test public void mapsAndFiltersOverShapes() { List<ValidationEvent> events = Model.assembler() .addImport(getClass().getResource("validator-filtering-and-mapping.smithy")) .validatorFactory(new ValidatorFactory() { @Override public List<Validator> loadBuiltinValidators() { return Collections.emptyList(); } @Override public Optional<Validator> createValidator(String name, ObjectNode configuration) { return name.equals("hello") ? Optional.of( model -> model.shapes() .map(shape -> ValidationEvent.builder() .id(name) .shape(shape) .severity(Severity.WARNING) .message("Hello!") .build()) .collect(Collectors.toList())) : Optional.empty(); } }) .assemble() .getValidationEvents(); assertThat(events, not(empty())); Assertions.assertEquals(2, events.stream().filter(e -> e.getId().equals("hello")).count()); Assertions.assertEquals(4, events.stream().filter(e -> e.getId().equals("custom")).count()); for (ValidationEvent event : events) { if (event.getId().equals("custom")) { assertThat(event.getMessage(), equalTo("Test Hello!")); } } }
List<ValidationEvent> map(Model model, List<ValidationEvent> events) { List<ValidationEvent> filtered = new ArrayList<>(events.size()); Set<ShapeId> candidates = null; if (selector != null) { candidates = selector.runner() .model(model) .selectShapes() .stream() .map(Shape::getId) .collect(Collectors.toSet()); } for (ValidationEvent event : events) { if (!filterEvent(event, candidates)) { continue; } if (name.equals(id) && severity == null && message == null) { filtered.add(event); } else { ValidationEvent.Builder builder = event.toBuilder(); builder.id(id != null ? id : event.getId()); builder.severity(severity != null ? severity : event.getSeverity()); if (message != null) { builder.message(message.replace("{super}", event.getMessage())); } filtered.add(builder.build()); } } return filtered; }
ValidatorDefinition { List<ValidationEvent> map(Model model, List<ValidationEvent> events) { List<ValidationEvent> filtered = new ArrayList<>(events.size()); Set<ShapeId> candidates = null; if (selector != null) { candidates = selector.runner() .model(model) .selectShapes() .stream() .map(Shape::getId) .collect(Collectors.toSet()); } for (ValidationEvent event : events) { if (!filterEvent(event, candidates)) { continue; } if (name.equals(id) && severity == null && message == null) { filtered.add(event); } else { ValidationEvent.Builder builder = event.toBuilder(); builder.id(id != null ? id : event.getId()); builder.severity(severity != null ? severity : event.getSeverity()); if (message != null) { builder.message(message.replace("{super}", event.getMessage())); } filtered.add(builder.build()); } } return filtered; } }
ValidatorDefinition { List<ValidationEvent> map(Model model, List<ValidationEvent> events) { List<ValidationEvent> filtered = new ArrayList<>(events.size()); Set<ShapeId> candidates = null; if (selector != null) { candidates = selector.runner() .model(model) .selectShapes() .stream() .map(Shape::getId) .collect(Collectors.toSet()); } for (ValidationEvent event : events) { if (!filterEvent(event, candidates)) { continue; } if (name.equals(id) && severity == null && message == null) { filtered.add(event); } else { ValidationEvent.Builder builder = event.toBuilder(); builder.id(id != null ? id : event.getId()); builder.severity(severity != null ? severity : event.getSeverity()); if (message != null) { builder.message(message.replace("{super}", event.getMessage())); } filtered.add(builder.build()); } } return filtered; } ValidatorDefinition(String name, String id); }
ValidatorDefinition { List<ValidationEvent> map(Model model, List<ValidationEvent> events) { List<ValidationEvent> filtered = new ArrayList<>(events.size()); Set<ShapeId> candidates = null; if (selector != null) { candidates = selector.runner() .model(model) .selectShapes() .stream() .map(Shape::getId) .collect(Collectors.toSet()); } for (ValidationEvent event : events) { if (!filterEvent(event, candidates)) { continue; } if (name.equals(id) && severity == null && message == null) { filtered.add(event); } else { ValidationEvent.Builder builder = event.toBuilder(); builder.id(id != null ? id : event.getId()); builder.severity(severity != null ? severity : event.getSeverity()); if (message != null) { builder.message(message.replace("{super}", event.getMessage())); } filtered.add(builder.build()); } } return filtered; } ValidatorDefinition(String name, String id);  }
ValidatorDefinition { List<ValidationEvent> map(Model model, List<ValidationEvent> events) { List<ValidationEvent> filtered = new ArrayList<>(events.size()); Set<ShapeId> candidates = null; if (selector != null) { candidates = selector.runner() .model(model) .selectShapes() .stream() .map(Shape::getId) .collect(Collectors.toSet()); } for (ValidationEvent event : events) { if (!filterEvent(event, candidates)) { continue; } if (name.equals(id) && severity == null && message == null) { filtered.add(event); } else { ValidationEvent.Builder builder = event.toBuilder(); builder.id(id != null ? id : event.getId()); builder.severity(severity != null ? severity : event.getSeverity()); if (message != null) { builder.message(message.replace("{super}", event.getMessage())); } filtered.add(builder.build()); } } return filtered; } ValidatorDefinition(String name, String id);   }