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