2 * Copyright (c) 2017 Pantheon Technologies 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
9 package org.opendaylight.yangtools.yang.data.codec.xml;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.fail;
15 import java.io.InputStream;
16 import javax.xml.stream.XMLStreamReader;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.util.xml.UntrustedXML;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 public class StrictParsingModeTest {
32 public void testLenientParsing() throws Exception {
33 // unknown child nodes in the top-level-container node will be skipped when the strictParsing is set to false
34 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource(
35 "/strict-parsing-mode-test/foo.yang");
36 final Module fooModule = schemaContext.getModules().iterator().next();
37 final ContainerSchemaNode topLevelContainer = (ContainerSchemaNode) fooModule.findDataChildByName(
38 QName.create(fooModule.getQNameModule(), "top-level-container")).get();
40 final InputStream resourceAsStream = StrictParsingModeTest.class.getResourceAsStream(
41 "/strict-parsing-mode-test/foo.xml");
43 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
45 final NormalizedNodeResult result = new NormalizedNodeResult();
46 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
48 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topLevelContainer, false);
49 xmlParser.parse(reader);
51 final NormalizedNode<?, ?> transformedInput = result.getResult();
52 assertNotNull(transformedInput);
56 public void testStrictParsing() throws Exception {
57 // should fail because strictParsing is switched on and the top-level-container node contains child nodes
58 // which are not defined in the provided YANG model
59 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource(
60 "/strict-parsing-mode-test/foo.yang");
61 final Module fooModule = schemaContext.getModules().iterator().next();
62 final ContainerSchemaNode topLevelContainer = (ContainerSchemaNode) fooModule.findDataChildByName(
63 QName.create(fooModule.getQNameModule(), "top-level-container")).get();
65 final InputStream resourceAsStream = StrictParsingModeTest.class.getResourceAsStream(
66 "/strict-parsing-mode-test/foo.xml");
68 final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
70 final NormalizedNodeResult result = new NormalizedNodeResult();
71 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
73 final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schemaContext, topLevelContainer, true);
75 xmlParser.parse(reader);
76 fail("IllegalStateException should have been thrown because of an unknown child node.");
77 } catch (IllegalStateException ex) {
78 assertEquals("Schema for node with name unknown-container-a and namespace foo does not exist at "
79 + "AbsoluteSchemaPath{path=[(foo)top-level-container]}", ex.getMessage());