Bug 2358 - Remove tests cnsn to json and add tests nn to json
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / nn / to / json / test / NnToJsonNotExistingLeafTypeTest.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.json.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import com.google.common.base.Preconditions;
14 import org.junit.Test;
15 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
16 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
17 import org.opendaylight.controller.sal.restconf.impl.test.DummyType;
18 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
23 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
31
32 public class NnToJsonNotExistingLeafTypeTest {
33
34     @Test
35     public void incorrectTopLevelElementTest() {
36         final NormalizedNodeContext normalizedNodeContext = prepareNormalizedNode();
37         assertNotNull(normalizedNodeContext);
38         assertEquals(normalizedNodeContext.getData().getNodeType()
39                 .getLocalName(), "cont");
40
41         final String output = NormalizedNodes.toStringTree(normalizedNodeContext
42                 .getData());
43         assertNotNull(output);
44         assertTrue(output.contains("lf1"));
45     }
46
47     private NormalizedNodeContext prepareNormalizedNode() {
48         final QName lf1 = QName.create("simple:uri", "2012-12-17", "lf1");
49
50         final DataSchemaNode contSchemaNode = prepareDataSchemaNode();
51
52         Preconditions.checkState(contSchemaNode instanceof ContainerSchemaNode);
53         final ContainerSchemaNode conContSchemaNode = (ContainerSchemaNode) contSchemaNode;
54
55         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> contContBuilder = Builders
56                 .containerBuilder(conContSchemaNode);
57
58         final DataSchemaNode lf1SchemaNode = conContSchemaNode
59                 .getDataChildByName(lf1);
60         Preconditions.checkState(lf1SchemaNode instanceof LeafSchemaNode);
61
62         final String lf1String = "";
63         contContBuilder.withChild(Builders
64                 .leafBuilder((LeafSchemaNode) lf1SchemaNode)
65                 .withValue(lf1String).build());
66
67         final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext(
68                 new InstanceIdentifierContext<DataSchemaNode>(null,
69                         contSchemaNode, null, null), contContBuilder.build());
70
71         return testNormalizedNodeContext;
72     }
73
74     private DataSchemaNode prepareDataSchemaNode() {
75         final ContainerSchemaNodeBuilder contBuild = new ContainerSchemaNodeBuilder(
76                 "module", 1, TestUtils.buildQName("cont", "simple:uri",
77                         "2012-12-17"), SchemaPath.create(true,
78                         QName.create("dummy")));
79         final LeafSchemaNodeBuilder leafBuild = new LeafSchemaNodeBuilder("module",
80                 2, TestUtils.buildQName("lf1", "simple:uri", "2012-12-17"),
81                 SchemaPath.create(true, QName.create("dummy")));
82         leafBuild.setType(new DummyType());
83         leafBuild.setConfiguration(true);
84
85         contBuild.addChildNode(leafBuild);
86         return contBuild.build();
87     }
88
89 }