2 * Copyright (c) 2017 Cisco Systems, Inc. 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.impl.schema.transform.dom.serializer;
11 import static org.junit.Assert.assertNotNull;
13 import com.google.common.base.Preconditions;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.Collections;
17 import java.util.Date;
18 import javax.xml.parsers.DocumentBuilder;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import javax.xml.parsers.ParserConfigurationException;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
26 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
31 import org.w3c.dom.Document;
32 import org.xml.sax.SAXException;
34 public class Bug6392Test {
36 private static final DocumentBuilderFactory BUILDERFACTORY;
39 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
40 factory.setNamespaceAware(true);
41 factory.setCoalescing(true);
42 factory.setIgnoringElementContentWhitespace(true);
43 factory.setIgnoringComments(true);
44 BUILDERFACTORY = factory;
48 public void testLenientParsingOfUnkeyedListEntries() throws Exception {
49 final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug6392/foo.yang");
50 assertNotNull(schemaContext);
52 final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2017-03-10");
54 final Module module = schemaContext.findModuleByName("foo", revision);
55 assertNotNull(module);
57 final ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) module.getDataChildByName(
58 QName.create(module.getQNameModule(), "root-cont"));
59 assertNotNull(containerSchemaNode);
61 final Document doc = loadDocument("/bug6392/foo.xml");
63 final ContainerNode result = DomToNormalizedNodeParserFactory
64 .getInstance(DomUtils.defaultValueCodecProvider(), schemaContext, false).getContainerNodeParser()
65 .parse(Collections.singletonList(doc.getDocumentElement()), containerSchemaNode);
66 assertNotNull(result);
69 private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
70 final InputStream resourceAsStream = Bug6392Test.class.getResourceAsStream(xmlPath);
72 final Document currentConfigElement = readXmlToDocument(resourceAsStream);
73 Preconditions.checkNotNull(currentConfigElement);
74 return currentConfigElement;
77 private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
78 final DocumentBuilder dBuilder;
80 dBuilder = BUILDERFACTORY.newDocumentBuilder();
81 } catch (final ParserConfigurationException e) {
82 throw new RuntimeException("Failed to parse XML document", e);
84 final Document doc = dBuilder.parse(xmlContent);
86 doc.getDocumentElement().normalize();