Reduce the use of AttrBuilders
[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.DataContainerNodeBuilder;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 public class NnToXmlWithChoiceTest extends AbstractBodyReaderTest {
38
39     private final NormalizedNodeXmlBodyWriter xmlBodyWriter;
40     private static SchemaContext schemaContext;
41
42     public NnToXmlWithChoiceTest() {
43         super(schemaContext, null);
44         xmlBodyWriter = new NormalizedNodeXmlBodyWriter();
45     }
46
47     @BeforeClass
48     public static void initialization() {
49         schemaContext = schemaContextLoader("/nn-to-xml/choice", schemaContext);
50     }
51
52     @Test
53     public void cnSnToXmlWithYangChoice() throws Exception {
54         NormalizedNodeContext normalizedNodeContext = prepareNNC("lf1",
55                 "String data1");
56         OutputStream output = new ByteArrayOutputStream();
57         xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null,
58                     mediaType, null, output);
59         assertTrue(output.toString().contains("<lf1>String data1</lf1>"));
60
61         normalizedNodeContext = prepareNNC("lf2", "String data2");
62         output = new ByteArrayOutputStream();
63
64         xmlBodyWriter.writeTo(normalizedNodeContext, null, null, null,
65                     mediaType, null, output);
66         assertTrue(output.toString().contains("<lf2>String data2</lf2>"));
67     }
68
69     private static NormalizedNodeContext prepareNNC(final String name, final Object value) {
70
71         final QName contQname = QName.create("module:with:choice", "2013-12-18",
72                 "cont");
73         final QName lf = QName.create("module:with:choice", "2013-12-18", name);
74         final QName choA = QName.create("module:with:choice", "2013-12-18", "choA");
75
76         final DataSchemaNode contSchemaNode = schemaContext
77                 .getDataChildByName(contQname);
78         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> dataContainerNodeAttrBuilder = Builders
79                 .containerBuilder((ContainerSchemaNode) contSchemaNode);
80
81         final DataSchemaNode choiceSchemaNode = ((ContainerSchemaNode) contSchemaNode)
82                 .getDataChildByName(choA);
83         assertTrue(choiceSchemaNode instanceof ChoiceSchemaNode);
84
85         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> dataChoice = Builders
86                 .choiceBuilder((ChoiceSchemaNode) choiceSchemaNode);
87
88         final List<DataSchemaNode> instanceLf = ControllerContext
89                 .findInstanceDataChildrenByName(
90                         (DataNodeContainer) contSchemaNode, lf.getLocalName());
91         final DataSchemaNode schemaLf = Iterables.getFirst(instanceLf, null);
92
93         dataChoice.withChild(Builders.leafBuilder((LeafSchemaNode) schemaLf)
94                 .withValue(value).build());
95
96         dataContainerNodeAttrBuilder.withChild(dataChoice.build());
97
98         final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext(
99                 new InstanceIdentifierContext<>(null,
100                         contSchemaNode, null, schemaContext),
101                 dataContainerNodeAttrBuilder.build());
102
103         return testNormalizedNodeContext;
104     }
105
106     @Override
107     protected MediaType getMediaType() {
108         return null;
109     }
110 }