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