/*
* Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.serializer;
import static org.junit.Assert.assertNotNull;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class Bug6392Test {
private static final DocumentBuilderFactory BUILDERFACTORY;
static {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setCoalescing(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
BUILDERFACTORY = factory;
}
@Test
public void testLenientParsingOfUnkeyedListEntries() throws Exception {
final SchemaContext schemaContext = YangParserTestUtils.parseYangSource("/bug6392/foo.yang");
assertNotNull(schemaContext);
final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2017-03-10");
final Module module = schemaContext.findModuleByName("foo", revision);
assertNotNull(module);
final ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) module.getDataChildByName(
QName.create(module.getQNameModule(), "root-cont"));
assertNotNull(containerSchemaNode);
final Document doc = loadDocument("/bug6392/foo.xml");
final ContainerNode result = DomToNormalizedNodeParserFactory
.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext, false).getContainerNodeParser()
.parse(Collections.singletonList(doc.getDocumentElement()), containerSchemaNode);
assertNotNull(result);
}
private static Document loadDocument(final String xmlPath) throws IOException, SAXException {
final InputStream resourceAsStream = Bug6392Test.class.getResourceAsStream(xmlPath);
final Document currentConfigElement = readXmlToDocument(resourceAsStream);
Preconditions.checkNotNull(currentConfigElement);
return currentConfigElement;
}
private static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
final DocumentBuilder dBuilder;
try {
dBuilder = BUILDERFACTORY.newDocumentBuilder();
} catch (final ParserConfigurationException e) {
throw new RuntimeException("Failed to parse XML document", e);
}
final Document doc = dBuilder.parse(xmlContent);
doc.getDocumentElement().normalize();
return doc;
}
}