ba960d05b70ff685d9735eff1fa86cc5a81c8bc7
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / dom / serializer / LeafSetDomSerializerTest.java
1 /*
2  * Copyright (c) 2016 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 java.io.IOException;
12 import java.text.ParseException;
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
21 import org.w3c.dom.Element;
22
23 public class LeafSetDomSerializerTest {
24     private LeafSetEntryNodeDomSerializer temp;
25     private LeafListSchemaNode currentLeafList;
26     private LeafSetEntryNode tempLeafList;
27
28     @Test
29     public void leafSetDomSerializerTest() throws IOException, YangSyntaxErrorException, ParseException, ReactorException {
30         leafSetEntryNodeDomSerializerTest();
31         leafSetNodeDomSerializerTest();
32     }
33
34     private void leafSetEntryNodeDomSerializerTest() throws ReactorException, IOException, YangSyntaxErrorException,
35             ParseException {
36         final ContainerSchemaNode currentContainer = (ContainerSchemaNode) DomSerializerTestUtils.getSchemaContext()
37                 .findModuleByName("serializer-test", null)
38                 .getDataChildByName(DomSerializerTestUtils.generateQname("root"));
39         currentLeafList = (LeafListSchemaNode) currentContainer.getDataChildByName(DomSerializerTestUtils
40                 .generateQname("first-leaf-list"));
41
42         final YangInstanceIdentifier.NodeWithValue barPath = new YangInstanceIdentifier.NodeWithValue
43                 (DomSerializerTestUtils.generateQname("first-leaf-list"), "bar");
44         tempLeafList = ImmutableLeafSetEntryNodeBuilder.create()
45                 .withNodeIdentifier(barPath)
46                 .withValue("bar")
47                 .build();
48
49         temp = new LeafSetEntryNodeDomSerializer(DomSerializerTestUtils.DOC, DomSerializerTestUtils.CODEC_PROVIDER);
50         final Element element = temp.serializeLeaf(currentLeafList, tempLeafList);
51
52         DomSerializerTestUtils.testResults("<first-leaf-list xmlns=\"dom-serializer-test\">bar</first-leaf-list>",
53                 element);
54     }
55
56     private void leafSetNodeDomSerializerTest() {
57         final LeafSetNodeDomSerializer nodeDomSerializer = new LeafSetNodeDomSerializer(temp);
58         final LeafSetEntryNodeDomSerializer leafSetEntryNodeSerializer = (LeafSetEntryNodeDomSerializer)
59                 nodeDomSerializer.getLeafSetEntryNodeSerializer();
60         final Element element = temp.serializeLeaf(currentLeafList, tempLeafList);
61
62         leafSetEntryNodeSerializer.serializeLeaf(currentLeafList, tempLeafList);
63         DomSerializerTestUtils.testResults("<first-leaf-list xmlns=\"dom-serializer-test\">bar</first-leaf-list>",
64                 element);
65     }
66 }