Search in sources :

Example 1 with NodePath

use of io.apicurio.datamodels.core.models.NodePath in project apicurio-data-models by Apicurio.

the class Oas20to30TransformationVisitor method mapNode.

private void mapNode(Node from, Node to) {
    NodePath nodePath = Library.createNodePath(from);
    String mapIndex = nodePath.toString();
    this._nodeMap.put(mapIndex, to);
}
Also used : NodePath(io.apicurio.datamodels.core.models.NodePath)

Example 2 with NodePath

use of io.apicurio.datamodels.core.models.NodePath in project apicurio-data-models by Apicurio.

the class TestValidationExtension method validateDocument.

@Override
public CompletableFuture<List<ValidationProblem>> validateDocument(Node document) {
    List<ValidationProblem> fakeProblems = new ArrayList<>();
    fakeProblems.add(new ValidationProblem("TEST-001", new NodePath("testpath"), "test", "Test problem", ValidationProblemSeverity.low));
    return CompletableFuture.completedFuture(fakeProblems);
}
Also used : ArrayList(java.util.ArrayList) ValidationProblem(io.apicurio.datamodels.core.models.ValidationProblem) NodePath(io.apicurio.datamodels.core.models.NodePath)

Example 3 with NodePath

use of io.apicurio.datamodels.core.models.NodePath in project apicurio-data-models by Apicurio.

the class IoTestRunner method runChild.

/**
 * @see org.junit.runners.ParentRunner#runChild(java.lang.Object, org.junit.runner.notification.RunNotifier)
 */
@Override
protected void runChild(IoTestCase child, RunNotifier notifier) {
    Description description = this.describeChild(child);
    Statement statement = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            String testCP = "fixtures/io/" + child.getTest();
            URL testUrl = Thread.currentThread().getContextClassLoader().getResource(testCP);
            Assert.assertNotNull("Test file not found on classpath: " + testCP, testUrl);
            // Read the test source
            String original = loadResource(testUrl);
            Assert.assertNotNull(original);
            // Parse into a Json object
            Object originalParsed = mapper.readTree(original);
            // Parse into a data model
            Document doc = Library.readDocument(originalParsed);
            Assert.assertNotNull("Document was null.", doc);
            // Make sure we read the appropriate number of "extra" properties
            ExtraPropertyDetectionVisitor epv = new ExtraPropertyDetectionVisitor();
            Library.visitTree(doc, epv, TraverserDirection.down);
            int actualExtraProps = epv.getExtraPropertyCount();
            int expectedExtraProps = child.getExtraProperties();
            Assert.assertEquals("Wrong number of extra properties found.", expectedExtraProps, actualExtraProps);
            // Write the data model back to JSON
            Object roundTripJs = Library.writeNode(doc);
            Assert.assertNotNull(roundTripJs);
            // Stringify the round trip object
            String roundTrip = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(roundTripJs);
            Assert.assertNotNull(roundTrip);
            assertJsonEquals(original, roundTrip);
            // Now test that we can create a Node Path to **every** node in the document!!
            List<Node> allNodes = getAllNodes(doc);
            for (Node node : allNodes) {
                try {
                    NodePath nodePath = Library.createNodePath(node);
                    Assert.assertNotNull(nodePath);
                    String path = nodePath.toString();
                    Assert.assertNotNull(nodePath);
                    nodePath = new NodePath(path);
                    Node resolvedNode = nodePath.resolve(doc);
                    Assert.assertNotNull("Failed to resolve node: " + nodePath.toString(), resolvedNode);
                    Assert.assertTrue("Path failed to resolve [" + node.getClass().getSimpleName() + "] to the proper node: " + path, node == resolvedNode);
                } catch (Throwable t) {
                    System.err.println("Failure/error testing node path: " + Library.createNodePath(node).toString());
                    throw t;
                }
            }
            // assert the results.  But at least it runs everything through the reader dispatchers.
            for (Node node : allNodes) {
                // Skip extensions and expressions.
                if (node instanceof Extension || node instanceof IOas30Expression) {
                    continue;
                }
                // TODO :: check the extra-property counts when doing partial i/o testing
                try {
                    Object partialNodeJson = Library.writeNode(node);
                    String partialNodeJsonStr = JsonCompat.stringify(partialNodeJson);
                    Node clonedNode = cloneNode(node);
                    // Skip any node that we can't clone.
                    if (clonedNode == null) {
                        continue;
                    }
                    Library.readNode(partialNodeJson, clonedNode);
                    Object finalJson = Library.writeNode(clonedNode);
                    String finalJsonStr = JsonCompat.stringify(finalJson);
                    // LoggerCompat.info("PARTIAL: %s", partialNodeJson);
                    // LoggerCompat.info("FINAL:   %s", finalJsonStr);
                    assertJsonEquals(partialNodeJsonStr, finalJsonStr);
                } catch (Throwable t) {
                    System.err.println("Failure/error testing partial I/O for node: " + Library.createNodePath(node).toString());
                    throw t;
                }
            }
        }
    };
    runLeaf(statement, description, notifier);
}
Also used : Description(org.junit.runner.Description) Statement(org.junit.runners.model.Statement) Node(io.apicurio.datamodels.core.models.Node) IOas30Expression(io.apicurio.datamodels.openapi.v3.models.IOas30Expression) Document(io.apicurio.datamodels.core.models.Document) URL(java.net.URL) NodePath(io.apicurio.datamodels.core.models.NodePath) Extension(io.apicurio.datamodels.core.models.Extension)

Example 4 with NodePath

use of io.apicurio.datamodels.core.models.NodePath in project apicurio-data-models by Apicurio.

the class NodePathIoTestRunner method runChild.

/**
 * @see org.junit.runners.ParentRunner#runChild(java.lang.Object, org.junit.runner.notification.RunNotifier)
 */
@Override
protected void runChild(NodePathIoTestCase child, RunNotifier notifier) {
    Description description = this.describeChild(child);
    Statement statement = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            String path = child.getPath();
            // Parse the path
            NodePath np = new NodePath(path);
            // Write that path back out to a string
            String actual = np.toString();
            // Compare
            String expected = path;
            Assert.assertEquals(expected, actual);
            // Get the sequence of segments from the path
            List<String> segments = np.toSegments();
            List<String> expectedSegments = child.getSegments();
            Assert.assertEquals(expectedSegments, segments);
        }
    };
    runLeaf(statement, description, notifier);
}
Also used : Description(org.junit.runner.Description) Statement(org.junit.runners.model.Statement) NodePath(io.apicurio.datamodels.core.models.NodePath)

Example 5 with NodePath

use of io.apicurio.datamodels.core.models.NodePath in project apicurio-data-models by Apicurio.

the class NodePathResolveTestRunner method runChild.

/**
 * @see org.junit.runners.ParentRunner#runChild(java.lang.Object, org.junit.runner.notification.RunNotifier)
 */
@Override
protected void runChild(NodePathResolveTestCase child, RunNotifier notifier) {
    Description description = this.describeChild(child);
    Statement statement = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            String testCP = "fixtures/paths/" + child.getTest();
            URL testUrl = Thread.currentThread().getContextClassLoader().getResource(testCP);
            Assert.assertNotNull(testUrl);
            // Read the test source
            String original = loadResource(testUrl);
            Assert.assertNotNull(original);
            // Parse into a Json object
            Object originalParsed = mapper.readTree(original);
            // Parse into a data model
            Document doc = Library.readDocument(originalParsed);
            Assert.assertNotNull(doc);
            // Parse the test path
            NodePath np = new NodePath(child.getPath());
            // Resolve the path to a node in the source
            Node resolvedNode = np.resolve(doc);
            Assert.assertNotNull(resolvedNode);
            // Compare source path to node path (test generating a node path from a node)
            NodePath createdPath = Library.createNodePath(resolvedNode);
            String expectedPath = child.getPath();
            String actualPath = createdPath.toString();
            Assert.assertEquals(expectedPath, actualPath);
            // Verify that the resolved node is what we expected it to be
            Object actualObj = Library.writeNode(resolvedNode);
            String actual = mapper.writeValueAsString(actualObj);
            String expectedCP = "fixtures/paths/" + child.getTest() + ".expected.json";
            URL expectedUrl = Thread.currentThread().getContextClassLoader().getResource(expectedCP);
            Assert.assertNotNull(testUrl);
            String expected = loadResource(expectedUrl);
            assertJsonEquals(expected, actual);
        }
    };
    runLeaf(statement, description, notifier);
}
Also used : Description(org.junit.runner.Description) Statement(org.junit.runners.model.Statement) Node(io.apicurio.datamodels.core.models.Node) Document(io.apicurio.datamodels.core.models.Document) URL(java.net.URL) NodePath(io.apicurio.datamodels.core.models.NodePath)

Aggregations

NodePath (io.apicurio.datamodels.core.models.NodePath)7 Description (org.junit.runner.Description)3 Statement (org.junit.runners.model.Statement)3 Document (io.apicurio.datamodels.core.models.Document)2 Node (io.apicurio.datamodels.core.models.Node)2 ValidationProblem (io.apicurio.datamodels.core.models.ValidationProblem)2 URL (java.net.URL)2 Extension (io.apicurio.datamodels.core.models.Extension)1 ValidationProblemSeverity (io.apicurio.datamodels.core.models.ValidationProblemSeverity)1 IOas30Expression (io.apicurio.datamodels.openapi.v3.models.IOas30Expression)1 ArrayList (java.util.ArrayList)1