Remove deprecated Yin/YangStatementSourceImpl
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / dom / serializer / Bug6392Test.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.serializer;
10
11 import static org.junit.Assert.assertNotNull;
12
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;
33
34 public class Bug6392Test {
35
36     private static final DocumentBuilderFactory BUILDERFACTORY;
37
38     static {
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;
45     }
46
47     @Test
48     public void testLenientParsingOfUnkeyedListEntries() throws Exception {
49         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug6392/foo.yang");
50         assertNotNull(schemaContext);
51
52         final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2017-03-10");
53
54         final Module module = schemaContext.findModuleByName("foo", revision);
55         assertNotNull(module);
56
57         final ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) module.getDataChildByName(
58                 QName.create(module.getQNameModule(), "root-cont"));
59         assertNotNull(containerSchemaNode);
60
61         final Document doc = loadDocument("/bug6392/foo.xml");
62
63         final ContainerNode result = DomToNormalizedNodeParserFactory
64                 .getInstance(DomUtils.defaultValueCodecProvider(), schemaContext, false).getContainerNodeParser()
65                 .parse(Collections.singletonList(doc.getDocumentElement()), containerSchemaNode);
66         assertNotNull(result);
67     }
68
69     private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
70         final InputStream resourceAsStream = Bug6392Test.class.getResourceAsStream(xmlPath);
71
72         final Document currentConfigElement = readXmlToDocument(resourceAsStream);
73         Preconditions.checkNotNull(currentConfigElement);
74         return currentConfigElement;
75     }
76
77     private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
78         final DocumentBuilder dBuilder;
79         try {
80             dBuilder = BUILDERFACTORY.newDocumentBuilder();
81         } catch (final ParserConfigurationException e) {
82             throw new RuntimeException("Failed to parse XML document", e);
83         }
84         final Document doc = dBuilder.parse(xmlContent);
85
86         doc.getDocumentElement().normalize();
87         return doc;
88     }
89 }