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 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.netconf.sal.restconf.impl.InstanceIdentifierContext;
23 import org.opendaylight.netconf.sal.restconf.impl.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;
38 public class NnToXmlWithChoiceTest extends AbstractBodyReaderTest {
40 private final NormalizedNodeXmlBodyWriter xmlBodyWriter;
41 private static SchemaContext schemaContext;
43 public NnToXmlWithChoiceTest() throws NoSuchFieldException,
46 xmlBodyWriter = new NormalizedNodeXmlBodyWriter();
50 public static void initialization() {
51 schemaContext = schemaContextLoader("/nn-to-xml/choice", schemaContext);
52 controllerContext.setSchemas(schemaContext);
56 public void cnSnToXmlWithYangChoice() throws Exception {
57 NormalizedNodeContext normalizedNodeContext = prepareNNC("lf1",
59 OutputStream output = new ByteArrayOutputStream();
60 xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null,
61 mediaType, null, output);
62 assertTrue(output.toString().contains("<lf1>String data1</lf1>"));
64 normalizedNodeContext = prepareNNC("lf2", "String data2");
65 output = new ByteArrayOutputStream();
67 xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null,
68 mediaType, null, output);
69 assertTrue(output.toString().contains("<lf2>String data2</lf2>"));
72 private static NormalizedNodeContext prepareNNC(final String name, final Object value) {
74 final QName contQname = QName.create("module:with:choice", "2013-12-18",
76 final QName lf = QName.create("module:with:choice", "2013-12-18", name);
77 final QName choA = QName.create("module:with:choice", "2013-12-18", "choA");
79 final DataSchemaNode contSchemaNode = schemaContext
80 .getDataChildByName(contQname);
81 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> dataContainerNodeAttrBuilder = Builders
82 .containerBuilder((ContainerSchemaNode) contSchemaNode);
84 final DataSchemaNode choiceSchemaNode = ((ContainerSchemaNode) contSchemaNode)
85 .getDataChildByName(choA);
86 assertTrue(choiceSchemaNode instanceof ChoiceSchemaNode);
88 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> dataChoice = Builders
89 .choiceBuilder((ChoiceSchemaNode) choiceSchemaNode);
91 final List<DataSchemaNode> instanceLf = ControllerContext
92 .findInstanceDataChildrenByName(
93 (DataNodeContainer) contSchemaNode, lf.getLocalName());
94 final DataSchemaNode schemaLf = Iterables.getFirst(instanceLf, null);
96 dataChoice.withChild(Builders.leafBuilder((LeafSchemaNode) schemaLf)
97 .withValue(value).build());
99 dataContainerNodeAttrBuilder.withChild(dataChoice.build());
101 final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext(
102 new InstanceIdentifierContext<>(null,
103 contSchemaNode, null, schemaContext),
104 dataContainerNodeAttrBuilder.build());
106 return testNormalizedNodeContext;
110 protected MediaType getMediaType() {