2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.codec.xml;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import java.util.List;
15 import org.junit.jupiter.params.ParameterizedTest;
16 import org.junit.jupiter.params.provider.Arguments;
17 import org.junit.jupiter.params.provider.MethodSource;
18 import org.opendaylight.yangtools.util.xml.UntrustedXML;
19 import org.opendaylight.yangtools.yang.common.Empty;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.AnydataNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedAnydata;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
27 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizationResultHolder;
28 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
31 class AnydataNormalizeContentTest extends AbstractAnydataTest {
32 private static final QName BAR_QNAME = QName.create(FOO_QNAME, "bar"); // container level 2
33 private static final QName LIST_QNAME = QName.create(FOO_QNAME, "lst"); // list
34 private static final QName LEAF_LIST_QNAME = QName.create(FOO_QNAME, "my-leafs"); // leaf-list of type string
35 private static final QName LEAF_EMPTY_QNAME = QName.create(FOO_QNAME, "empty-leaf"); // leaf of type empty
36 private static final NodeIdentifier BAR_NODEID = NodeIdentifier.create(BAR_QNAME);
37 private static final NodeIdentifier LIST_NODEID = NodeIdentifier.create(LIST_QNAME);
38 private static final NodeIdentifier LEAF_LIST_NODEID = NodeIdentifier.create(LEAF_LIST_QNAME);
39 private static final NodeIdentifier LEAF_EMPTY_NODEID = NodeIdentifier.create(LEAF_EMPTY_QNAME);
40 private static final LeafNode<String> LEAF_NODE = ImmutableNodes.leafNode(CONT_LEAF_NODEID, "test");
42 private static final String ANYDATA_XML = "<foo xmlns=\"test-anydata\"><cont-leaf>test</cont-leaf></foo>";
43 private static final String ANYDATA_EMPTY_XML = "<foo xmlns=\"test-anydata\" />";
45 @ParameterizedTest(name = "Anydata normalize to {0}")
47 void anydataNormalize(final String testDesc, final String xml, final Inference inference,
48 final NormalizedNode expectedData) throws Exception {
50 final var reader = UntrustedXML.createXMLStreamReader(toInputStream(xml));
51 final var result = new NormalizationResultHolder();
52 final var streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
53 final var xmlParser = XmlParserStream.create(streamWriter, Inference.ofDataTreePath(SCHEMA_CONTEXT, FOO_QNAME));
54 xmlParser.parse(reader);
56 final AnydataNode<?> anydataNode = assertInstanceOf(AnydataNode.class, result.getResult().data());
57 final DOMSourceAnydata domSourceAnydata = assertInstanceOf(DOMSourceAnydata.class, anydataNode.body());
59 final NormalizedAnydata normalizedAnydata = domSourceAnydata.normalizeTo(inference);
60 assertNotNull(normalizedAnydata);
61 assertEquals(expectedData, normalizedAnydata.getData());
64 private static List<Arguments> anydataNormalize() {
65 // test case descriptor, xml, inference, expected normalized data
67 Arguments.of("container (root level)",
69 Inference.ofDataTreePath(SCHEMA_CONTEXT, CONT_QNAME),
70 ImmutableNodes.newContainerBuilder().withNodeIdentifier(CONT_NODEID).withChild(LEAF_NODE).build()),
71 Arguments.of("container (level 2)",
73 Inference.ofDataTreePath(SCHEMA_CONTEXT, CONT_QNAME, BAR_QNAME),
74 ImmutableNodes.newContainerBuilder().withNodeIdentifier(BAR_NODEID).withChild(LEAF_NODE).build()),
75 Arguments.of("empty container",
77 Inference.ofDataTreePath(SCHEMA_CONTEXT, CONT_QNAME, BAR_QNAME),
78 ImmutableNodes.newContainerBuilder().withNodeIdentifier(BAR_NODEID).build()),
79 Arguments.of("single list element",
81 Inference.ofDataTreePath(SCHEMA_CONTEXT, LIST_QNAME),
82 ImmutableNodes.newUnkeyedListBuilder()
83 .withNodeIdentifier(LIST_NODEID)
84 .withChild(ImmutableNodes.newUnkeyedListEntryBuilder()
85 .withNodeIdentifier(LIST_NODEID)
89 Arguments.of("single empty list element",
91 Inference.ofDataTreePath(SCHEMA_CONTEXT, LIST_QNAME),
92 ImmutableNodes.newUnkeyedListBuilder()
93 .withNodeIdentifier(LIST_NODEID)
94 .withChild(ImmutableNodes.newUnkeyedListEntryBuilder()
95 .withNodeIdentifier(LIST_NODEID)
98 Arguments.of("single empty leaf-list element",
100 Inference.ofDataTreePath(SCHEMA_CONTEXT, LIST_QNAME, LEAF_LIST_QNAME),
101 ImmutableNodes.newSystemLeafSetBuilder()
102 .withNodeIdentifier(LEAF_LIST_NODEID)
103 .withChild(ImmutableNodes.leafSetEntry(LEAF_LIST_QNAME, ""))
105 Arguments.of("leaf of type empty",
107 Inference.ofDataTreePath(SCHEMA_CONTEXT, CONT_QNAME, LEAF_EMPTY_QNAME),
108 ImmutableNodes.leafNode(LEAF_EMPTY_NODEID, Empty.value())));