*/
package org.opendaylight.yangtools.yang.data.codec.xml;
-import static java.util.Objects.requireNonNull;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.augmentationBuilder;
-import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.choiceBuilder;
-import static org.opendaylight.yangtools.yang.data.impl.schema.Builders.containerBuilder;
-import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
-import com.google.common.collect.ImmutableSet;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.StringWriter;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.List;
import javax.xml.stream.XMLOutputFactory;
-import javax.xml.stream.XMLStreamWriter;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.ElementNameAndTextQualifier;
import org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener;
import org.custommonkey.xmlunit.XMLUnit;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import org.opendaylight.yangtools.util.xml.UntrustedXML;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.common.Uint32;
-import org.opendaylight.yangtools.yang.common.XMLNamespace;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
-import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
-import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
-import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
-import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
-import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeBuilder;
-import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
-import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizationResultHolder;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.xml.sax.SAXException;
-@RunWith(Parameterized.class)
-public class NormalizedNodeXmlTranslationTest {
- private final EffectiveModelContext schema;
-
- @Parameterized.Parameters()
- public static Collection<Object[]> data() {
- return Arrays.asList(new Object[][] {
- { "/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok.xml", augmentChoiceHell() },
- { "/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok2.xml", null },
- { "/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok3.xml", augmentChoiceHell2() },
- { "/schema/test.yang", "/schema/simple.xml", null },
- { "/schema/test.yang", "/schema/simple2.xml", null },
- // TODO check attributes
- { "/schema/test.yang", "/schema/simple_xml_with_attributes.xml", withAttributes() }
- });
- }
-
- private static final QNameModule MODULE = QNameModule.create(
- XMLNamespace.of("urn:opendaylight:params:xml:ns:yang:controller:test"), Revision.of("2014-03-13"));
+public class NormalizedNodeXmlTranslationTest extends AbstractXmlTest {
+ private static final QNameModule MODULE =
+ QNameModule.of("urn:opendaylight:params:xml:ns:yang:controller:test", "2014-03-13");
private static ContainerNode augmentChoiceHell2() {
- final NodeIdentifier container = getNodeIdentifier("container");
- final QName augmentChoice1QName = QName.create(container.getNodeType(), "augment-choice1");
- final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
- final QName containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
- final QName leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
-
- final AugmentationIdentifier aug1Id = new AugmentationIdentifier(ImmutableSet.of(augmentChoice1QName));
- final AugmentationIdentifier aug2Id = new AugmentationIdentifier(ImmutableSet.of(augmentChoice2QName));
- final NodeIdentifier augmentChoice1Id = new NodeIdentifier(augmentChoice1QName);
- final NodeIdentifier augmentChoice2Id = new NodeIdentifier(augmentChoice2QName);
- final NodeIdentifier containerId = new NodeIdentifier(containerQName);
-
- return containerBuilder().withNodeIdentifier(container)
- .withChild(augmentationBuilder().withNodeIdentifier(aug1Id)
- .withChild(choiceBuilder().withNodeIdentifier(augmentChoice1Id)
- .withChild(augmentationBuilder().withNodeIdentifier(aug2Id)
- .withChild(choiceBuilder().withNodeIdentifier(augmentChoice2Id)
- .withChild(containerBuilder().withNodeIdentifier(containerId)
- .withChild(leafNode(leafQName, "leaf-value"))
- .build())
- .build())
- .build())
- .build())
- .build()).build();
+ final var container = getNodeIdentifier("container");
+ final var augmentChoice1QName = QName.create(container.getNodeType(), "augment-choice1");
+ final var augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
+ final var containerQName = QName.create(augmentChoice1QName, "case11-choice-case-container");
+ final var leafQName = QName.create(augmentChoice1QName, "case11-choice-case-leaf");
+
+ return ImmutableNodes.newContainerBuilder()
+ .withNodeIdentifier(container)
+ .withChild(ImmutableNodes.newChoiceBuilder()
+ .withNodeIdentifier(new NodeIdentifier(augmentChoice1QName))
+ .withChild(ImmutableNodes.newChoiceBuilder()
+ .withNodeIdentifier(new NodeIdentifier(augmentChoice2QName))
+ .withChild(ImmutableNodes.newContainerBuilder()
+ .withNodeIdentifier(new NodeIdentifier(containerQName))
+ .withChild(ImmutableNodes.leafNode(leafQName, "leaf-value"))
+ .build())
+ .build())
+ .build())
+ .build();
}
private static ContainerNode withAttributes() {
- final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> b = containerBuilder();
- b.withNodeIdentifier(getNodeIdentifier("container"));
-
- final CollectionNodeBuilder<MapEntryNode, SystemMapNode> listBuilder =
- Builders.mapBuilder().withNodeIdentifier(getNodeIdentifier("list"));
-
- final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> list1Builder = Builders
- .mapEntryBuilder().withNodeIdentifier(NodeIdentifierWithPredicates.of(
- getNodeIdentifier("list").getNodeType(),
- getNodeIdentifier("uint32InList").getNodeType(), Uint32.valueOf(3)));
- final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> uint32InListBuilder = Builders
- .leafBuilder().withNodeIdentifier(getNodeIdentifier("uint32InList"));
-
- list1Builder.withChild(uint32InListBuilder.withValue(Uint32.valueOf(3)).build());
-
- listBuilder.withChild(list1Builder.build());
- b.withChild(listBuilder.build());
-
- final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> booleanBuilder = Builders
- .leafBuilder().withNodeIdentifier(getNodeIdentifier("boolean"));
- booleanBuilder.withValue(Boolean.FALSE);
- b.withChild(booleanBuilder.build());
-
- final ListNodeBuilder<Object, SystemLeafSetNode<Object>> leafListBuilder = Builders.leafSetBuilder()
- .withNodeIdentifier(getNodeIdentifier("leafList"));
-
- final NormalizedNodeBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafList1Builder = Builders
- .leafSetEntryBuilder().withNodeIdentifier(
- new NodeWithValue(getNodeIdentifier("leafList").getNodeType(), "a"));
-
- leafList1Builder.withValue("a");
-
- leafListBuilder.withChild(leafList1Builder.build());
- b.withChild(leafListBuilder.build());
-
- return b.build();
+ return ImmutableNodes.newContainerBuilder()
+ .withNodeIdentifier(getNodeIdentifier("container"))
+ .withChild(ImmutableNodes.newSystemMapBuilder()
+ .withNodeIdentifier(getNodeIdentifier("list"))
+ .withChild(ImmutableNodes.newMapEntryBuilder()
+ .withNodeIdentifier(NodeIdentifierWithPredicates.of(getNodeIdentifier("list").getNodeType(),
+ getNodeIdentifier("uint32InList").getNodeType(), Uint32.valueOf(3)))
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("uint32InList"), Uint32.valueOf(3)))
+ .build())
+ .build())
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("boolean"), Boolean.FALSE))
+ .withChild(ImmutableNodes.newSystemLeafSetBuilder()
+ .withNodeIdentifier(getNodeIdentifier("leafList"))
+ .withChild(ImmutableNodes.leafSetEntry(getNodeIdentifier("leafList").getNodeType(), "a"))
+ .build())
+ .build();
}
private static ContainerNode augmentChoiceHell() {
-
- final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> b = containerBuilder();
- b.withNodeIdentifier(getNodeIdentifier("container"));
-
- b.withChild(choiceBuilder()
+ return ImmutableNodes.newContainerBuilder()
+ .withNodeIdentifier(getNodeIdentifier("container"))
+ .withChild(ImmutableNodes.newChoiceBuilder()
.withNodeIdentifier(getNodeIdentifier("ch2"))
- .withChild(
- Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("c2Leaf")).withValue("2").build())
- .withChild(
- choiceBuilder()
- .withNodeIdentifier(getNodeIdentifier("c2DeepChoice"))
- .withChild(
- Builders.leafBuilder()
- .withNodeIdentifier(getNodeIdentifier("c2DeepChoiceCase1Leaf2"))
- .withValue("2").build()).build()).build());
-
- b.withChild(choiceBuilder()
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("c2Leaf"), "2"))
+ .withChild(ImmutableNodes.newChoiceBuilder()
+ .withNodeIdentifier(getNodeIdentifier("c2DeepChoice"))
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("c2DeepChoiceCase1Leaf2"), "2"))
+ .build())
+ .build())
+ .withChild(ImmutableNodes.newChoiceBuilder()
.withNodeIdentifier(getNodeIdentifier("ch3"))
- .withChild(
- Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("c3Leaf")).withValue("3").build())
- .build());
-
- b.withChild(augmentationBuilder()
- .withNodeIdentifier(getAugmentIdentifier("augLeaf"))
- .withChild(
- Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("augLeaf")).withValue("augment")
- .build()).build());
-
- b.withChild(augmentationBuilder()
- .withNodeIdentifier(getAugmentIdentifier("ch"))
- .withChild(
- choiceBuilder()
- .withNodeIdentifier(getNodeIdentifier("ch"))
- .withChild(
- Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("c1Leaf"))
- .withValue("1").build())
- .withChild(
- augmentationBuilder()
- .withNodeIdentifier(
- getAugmentIdentifier("c1Leaf_AnotherAugment", "deepChoice"))
- .withChild(
- Builders.leafBuilder()
- .withNodeIdentifier(
- getNodeIdentifier("c1Leaf_AnotherAugment"))
- .withValue("1").build())
- .withChild(
- choiceBuilder()
- .withNodeIdentifier(getNodeIdentifier("deepChoice"))
- .withChild(
- Builders.leafBuilder()
- .withNodeIdentifier(
- getNodeIdentifier("deepLeafc1"))
- .withValue("1").build()).build())
- .build()).build()).build());
-
- return b.build();
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("c3Leaf"), "3"))
+ .build())
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("augLeaf"), "augment"))
+ .withChild(ImmutableNodes.newChoiceBuilder()
+ .withNodeIdentifier(getNodeIdentifier("ch"))
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("c1Leaf"), "1"))
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("c1Leaf_AnotherAugment"), "1"))
+ .withChild(ImmutableNodes.newChoiceBuilder()
+ .withNodeIdentifier(getNodeIdentifier("deepChoice"))
+ .withChild(ImmutableNodes.leafNode(getNodeIdentifier("deepLeafc1"), "1"))
+ .build())
+ .build())
+ .build();
}
private static NodeIdentifier getNodeIdentifier(final String localName) {
return new NodeIdentifier(QName.create(MODULE, localName));
}
- private static AugmentationIdentifier getAugmentIdentifier(final String... childNames) {
- final Set<QName> qn = new HashSet<>();
-
- for (final String childName : childNames) {
- qn.add(getNodeIdentifier(childName).getNodeType());
- }
-
- return new AugmentationIdentifier(qn);
- }
-
- private final ContainerNode expectedNode;
- private final String xmlPath;
-
- public NormalizedNodeXmlTranslationTest(final String yangPath, final String xmlPath,
- final ContainerNode expectedNode) {
- schema = YangParserTestUtils.parseYangResource(yangPath);
- this.xmlPath = xmlPath;
- this.expectedNode = expectedNode;
+ @ParameterizedTest
+ @MethodSource("data")
+ void testTranslationRepairing(final String yangPath, final String xmlPath, final ContainerNode expectedNode)
+ throws Exception {
+ testTranslation(TestFactories.REPAIRING_OUTPUT_FACTORY, yangPath, xmlPath, expectedNode);
}
- @Test
- public void testTranslationRepairing() throws Exception {
- testTranslation(TestFactories.REPAIRING_OUTPUT_FACTORY);
+ @ParameterizedTest
+ @MethodSource("data")
+ void testTranslation(final String yangPath, final String xmlPath, final ContainerNode expectedNode)
+ throws Exception {
+ testTranslation(TestFactories.DEFAULT_OUTPUT_FACTORY, yangPath, xmlPath, expectedNode);
}
- @Test
- public void testTranslation() throws Exception {
- testTranslation(TestFactories.DEFAULT_OUTPUT_FACTORY);
- }
-
- private void testTranslation(final XMLOutputFactory factory) throws Exception {
+ private static void testTranslation(final XMLOutputFactory factory, final String yangPath, final String xmlPath,
+ final ContainerNode expectedNode) throws Exception {
+ final var schema = YangParserTestUtils.parseYangResource(yangPath);
final var resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream(xmlPath);
final var reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
assertEquals(expectedNode, built);
}
- final Document document = UntrustedXML.newDocumentBuilder().newDocument();
- final DOMResult domResult = new DOMResult(document);
-
- final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);
-
- final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter
- .create(xmlStreamWriter, schema);
-
- final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
- xmlNormalizedNodeStreamWriter);
-
+ final var document = UntrustedXML.newDocumentBuilder().newDocument();
+ final var domResult = new DOMResult(document);
+ final var xmlStreamWriter = factory.createXMLStreamWriter(domResult);
+ final var xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlStreamWriter, schema);
+ final var normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(xmlNormalizedNodeStreamWriter);
normalizedNodeWriter.write(built);
- final Document doc = loadDocument(xmlPath);
+ final var doc = loadDocument(xmlPath);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
diff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
diff.overrideElementQualifier(new ElementNameAndTextQualifier());
- // FIXME the comparison cannot be performed, since the qualifiers supplied by XMlUnit do not work correctly in
+ // FIXME the comparison cannot be performed, since the qualifiers supplied by XMLUnit do not work correctly in
// this case
// We need to implement custom qualifier so that the element ordering does not mess the DIFF
// dd.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(100, true));
// assertTrue(dd.toString(), dd.similar());
- //new XMLTestCase() {}.assertXMLEqual(diff, true);
- }
-
- private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
- final InputStream resourceAsStream = NormalizedNodeXmlTranslationTest.class.getResourceAsStream(xmlPath);
- return requireNonNull(readXmlToDocument(resourceAsStream));
+ // XMLAssert.assertXMLEqual(diff, true);
}
- private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
- final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
- doc.getDocumentElement().normalize();
- return doc;
- }
-
- private static String toString(final Node xml) {
- try {
- final Transformer transformer = TransformerFactory.newInstance().newTransformer();
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
-
- final StreamResult result = new StreamResult(new StringWriter());
- final DOMSource source = new DOMSource(xml);
- transformer.transform(source, result);
-
- return result.getWriter().toString();
- } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
- throw new RuntimeException("Unable to serialize xml element " + xml, e);
- }
+ static List<Arguments> data() {
+ return List.of(
+ Arguments.of("/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok.xml", augmentChoiceHell()),
+ Arguments.of("/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok2.xml", null),
+ Arguments.of("/schema/augment_choice_hell.yang", "/schema/augment_choice_hell_ok3.xml",
+ augmentChoiceHell2()),
+ Arguments.of("/schema/test.yang", "/schema/simple.xml", null),
+ Arguments.of("/schema/test.yang", "/schema/simple2.xml", null),
+ // TODO check attributes
+ Arguments.of("/schema/test.yang", "/schema/simple_xml_with_attributes.xml", withAttributes()));
}
}