6fbb4683e5b037f38f04e49d96d8294cc4088113
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / nn / to / xml / test / NnToXmlWithChoiceTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.restconf.impl.nn.to.xml.test;
9
10 import static org.junit.Assert.assertTrue;
11
12 import com.google.common.collect.Iterables;
13 import java.io.ByteArrayOutputStream;
14 import java.io.OutputStream;
15 import java.util.List;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
20 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
21 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
22 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
23 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 public class NnToXmlWithChoiceTest extends AbstractBodyReaderTest {
39
40     private final NormalizedNodeXmlBodyWriter xmlBodyWriter;
41     private static SchemaContext schemaContext;
42
43     public NnToXmlWithChoiceTest() {
44         super(schemaContext, null);
45         xmlBodyWriter = new NormalizedNodeXmlBodyWriter();
46     }
47
48     @BeforeClass
49     public static void initialization() {
50         schemaContext = schemaContextLoader("/nn-to-xml/choice", schemaContext);
51     }
52
53     @Test
54     public void cnSnToXmlWithYangChoice() throws Exception {
55         NormalizedNodeContext normalizedNodeContext = prepareNNC("lf1",
56                 "String data1");
57         OutputStream output = new ByteArrayOutputStream();
58         xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null,
59                     mediaType, null, output);
60         assertTrue(output.toString().contains("<lf1>String data1</lf1>"));
61
62         normalizedNodeContext = prepareNNC("lf2", "String data2");
63         output = new ByteArrayOutputStream();
64
65         xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null,
66                     mediaType, null, output);
67         assertTrue(output.toString().contains("<lf2>String data2</lf2>"));
68     }
69
70     private static NormalizedNodeContext prepareNNC(final String name, final Object value) {
71
72         final QName contQname = QName.create("module:with:choice", "2013-12-18",
73                 "cont");
74         final QName lf = QName.create("module:with:choice", "2013-12-18", name);
75         final QName choA = QName.create("module:with:choice", "2013-12-18", "choA");
76
77         final DataSchemaNode contSchemaNode = schemaContext
78                 .getDataChildByName(contQname);
79         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> dataContainerNodeAttrBuilder = Builders
80                 .containerBuilder((ContainerSchemaNode) contSchemaNode);
81
82         final DataSchemaNode choiceSchemaNode = ((ContainerSchemaNode) contSchemaNode)
83                 .getDataChildByName(choA);
84         assertTrue(choiceSchemaNode instanceof ChoiceSchemaNode);
85
86         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> dataChoice = Builders
87                 .choiceBuilder((ChoiceSchemaNode) choiceSchemaNode);
88
89         final List<DataSchemaNode> instanceLf = ControllerContext
90                 .findInstanceDataChildrenByName(
91                         (DataNodeContainer) contSchemaNode, lf.getLocalName());
92         final DataSchemaNode schemaLf = Iterables.getFirst(instanceLf, null);
93
94         dataChoice.withChild(Builders.leafBuilder((LeafSchemaNode) schemaLf)
95                 .withValue(value).build());
96
97         dataContainerNodeAttrBuilder.withChild(dataChoice.build());
98
99         final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext(
100                 new InstanceIdentifierContext<>(null,
101                         contSchemaNode, null, schemaContext),
102                 dataContainerNodeAttrBuilder.build());
103
104         return testNormalizedNodeContext;
105     }
106
107     @Override
108     protected MediaType getMediaType() {
109         return null;
110     }
111 }