2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.nn.to.xml.test;
10 import static org.junit.Assert.assertTrue;
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;
36 public class NnToXmlWithChoiceTest extends AbstractBodyReaderTest {
38 private final NormalizedNodeXmlBodyWriter xmlBodyWriter;
39 private static EffectiveModelContext schemaContext;
41 public NnToXmlWithChoiceTest() {
42 super(schemaContext, null);
43 xmlBodyWriter = new NormalizedNodeXmlBodyWriter();
47 public static void initialization() {
48 schemaContext = schemaContextLoader("/nn-to-xml/choice", schemaContext);
52 public void cnSnToXmlWithYangChoice() throws Exception {
53 NormalizedNodeContext normalizedNodeContext = prepareNNC("lf1",
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>"));
60 normalizedNodeContext = prepareNNC("lf2", "String data2");
61 output = new ByteArrayOutputStream();
63 xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null,
64 mediaType, null, output);
65 assertTrue(output.toString().contains("<lf2>String data2</lf2>"));
68 private static NormalizedNodeContext prepareNNC(final String name, final Object value) {
70 final QName contQname = QName.create("module:with:choice", "2013-12-18",
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");
75 final DataSchemaNode contSchemaNode = schemaContext
76 .getDataChildByName(contQname);
77 final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataContainerNodeAttrBuilder = SchemaAwareBuilders
78 .containerBuilder((ContainerSchemaNode) contSchemaNode);
80 final DataSchemaNode choiceSchemaNode = ((ContainerSchemaNode) contSchemaNode)
81 .getDataChildByName(choA);
82 assertTrue(choiceSchemaNode instanceof ChoiceSchemaNode);
84 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> dataChoice = SchemaAwareBuilders
85 .choiceBuilder((ChoiceSchemaNode) choiceSchemaNode);
87 final var instanceLf = ControllerContext
88 .findInstanceDataChildrenByName(
89 (DataNodeContainer) contSchemaNode, lf.getLocalName());
90 final DataSchemaNode schemaLf = instanceLf.get(0).child;
92 dataChoice.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) schemaLf)
93 .withValue(value).build());
95 dataContainerNodeAttrBuilder.withChild(dataChoice.build());
97 final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext(
98 InstanceIdentifierContext.ofStack(SchemaInferenceStack.ofDataTreePath(schemaContext, contQname)),
99 dataContainerNodeAttrBuilder.build());
101 return testNormalizedNodeContext;
105 protected MediaType getMediaType() {