TraceFile implements ToNode, ToSmithyBuilder<TraceFile> { public void validateModel(Model model) { Set<ShapeId> fileShapes = new HashSet<>(shapes.keySet()); Set<ShapeId> fileShapesCopy = new HashSet<>(fileShapes); Set<ShapeId> modelShapes = model.toSet().stream() .filter(shape -> !Prelude.isPreludeShape(shape)) .map(Shape::getId) .collect(Collectors.toSet()); fileShapes.removeAll(modelShapes); modelShapes.removeAll(fileShapesCopy); if (fileShapes.size() > 0 || modelShapes.size() > 0) { StringBuilder errorMessageBuilder = new StringBuilder().append("Model validation failed."); if (fileShapes.size() > 0) { errorMessageBuilder.append(" The following shapes are in the TraceFile, but missing from the model: "); fileShapes.stream().forEach(id -> { errorMessageBuilder.append(id.toString()).append(", "); }); errorMessageBuilder.append(". "); } if (modelShapes.size() > 0) { errorMessageBuilder.append("The following shapes are in the model, but missing from the TraceFile: "); modelShapes.stream().forEach(id -> { errorMessageBuilder.append(id.toString()).append(", "); }); errorMessageBuilder.append(". "); } throw new ExpectationNotMetException(errorMessageBuilder.toString(), SourceLocation.none()); } } private  TraceFile(Builder builder); static TraceFile fromNode(Node value); static Builder builder(); @Override ObjectNode toNode(); void validateModel(Model model); String getSmithyTrace(); TraceMetadata getMetadata(); Optional<ArtifactDefinitions> getArtifactDefinitions(); Map<ShapeId, List<ShapeLink>> getShapes(); @Override Builder toBuilder(); static final String SMITHY_TRACE_TEXT; static final String METADATA_TEXT; static final String DEFINITIONS_TEXT; static final String SHAPES_TEXT; static final String SMITHY_TRACE_VERSION; }
@Test void validateModelDoesNotThrowOnValidTraceFileModelPair() throws ExpectationNotMetException, URISyntaxException, FileNotFoundException { Assertions.assertDoesNotThrow(() -> { TraceFile traceFile = parseTraceFileFromFile(getClass().getResource("trace-for-simple-service-validation.json").toURI()); traceFile.validateModel(assembleModelTestHelper("simple-service.smithy")); }); }
@Test void validateModelThrowsOnTraceFileWithoutAllModelShapeIDs() throws ExpectationNotMetException, URISyntaxException, FileNotFoundException { Assertions.assertThrows(ExpectationNotMetException.class, () -> { TraceFile traceFile = parseTraceFileFromFile(getClass().getResource("trace-for-model-validation.json").toURI()); traceFile.validateModel(assembleModelTestHelper("service-with-shapeids.smithy")); }); }