Eliminate use of SchemaNode.getPath() in TestRestconfUtils
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / TestRestconfUtils.java
index 39d4292d1806e6356311ac68425cb8fb5c6b80f1..1ecdef936d2b7f9b54c315f34bb2614876bf97e6 100644 (file)
@@ -14,16 +14,18 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
+import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
+import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
@@ -68,12 +70,8 @@ public final class TestRestconfUtils {
         return schemaContext;
     }
 
-    public static NormalizedNodeContext loadNormalizedContextFromJsonFile() {
-        throw new AbstractMethodError("Not implemented yet");
-    }
-
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static NormalizedNodeContext loadNormalizedContextFromXmlFile(final String pathToInputFile,
+    public static NormalizedNodePayload loadNormalizedContextFromXmlFile(final String pathToInputFile,
             final String uri, final EffectiveModelContext schemaContext) {
         final InstanceIdentifierContext<?> iiContext =
                 ParserIdentifier.toInstanceIdentifier(uri, schemaContext, Optional.empty());
@@ -81,7 +79,7 @@ public final class TestRestconfUtils {
         try {
             final Document doc = UntrustedXML.newDocumentBuilder().parse(inputStream);
             final NormalizedNode nn = parse(iiContext, doc);
-            return new NormalizedNodeContext(iiContext, nn);
+            return NormalizedNodePayload.of(iiContext, nn);
         } catch (final Exception e) {
             LOG.error("Load xml file " + pathToInputFile + " fail.", e);
         }
@@ -121,8 +119,14 @@ public final class TestRestconfUtils {
 
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
-        final XmlParserStream xmlParser = XmlParserStream.create(writer,
-            SchemaInferenceStack.ofInstantiatedPath(iiContext.getSchemaContext(), schemaNode.getPath()).toInference());
+
+        final SchemaInferenceStack stack = SchemaInferenceStack.of(iiContext.getSchemaContext());
+        iiContext.getInstanceIdentifier().getPathArguments().stream()
+                .filter(p -> !(p instanceof NodeIdentifierWithPredicates))
+                .filter(p -> !(p instanceof AugmentationIdentifier))
+                .forEach(p -> stack.enterSchemaTree(p.getNodeType()));
+
+        final XmlParserStream xmlParser = XmlParserStream.create(writer, stack.toInference());
 
         if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode) {
             xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
@@ -132,7 +136,7 @@ public final class TestRestconfUtils {
         return null;
     }
 
-    public static Collection<File> loadFiles(final String resourceDirectory) throws FileNotFoundException {
+    public static List<File> loadFiles(final String resourceDirectory) throws FileNotFoundException {
         final String path = TestRestconfUtils.class.getResource(resourceDirectory).getPath();
         final File testDir = new File(path);
         final String[] fileList = testDir.list();
@@ -141,7 +145,8 @@ public final class TestRestconfUtils {
             throw new FileNotFoundException(resourceDirectory);
         }
         for (final String fileName : fileList) {
-            if (new File(testDir, fileName).isDirectory() == false) {
+            if (fileName.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)
+                && !new File(testDir, fileName).isDirectory()) {
                 testFiles.add(new File(testDir, fileName));
             }
         }