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.json.test;
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;
32 public class NnToJsonNotExistingLeafTypeTest {
35 public void incorrectTopLevelElementTest() {
36 final NormalizedNodeContext normalizedNodeContext = prepareNormalizedNode();
37 assertNotNull(normalizedNodeContext);
38 assertEquals(normalizedNodeContext.getData().getNodeType()
39 .getLocalName(), "cont");
41 final String output = NormalizedNodes.toStringTree(normalizedNodeContext
43 assertNotNull(output);
44 assertTrue(output.contains("lf1"));
47 private NormalizedNodeContext prepareNormalizedNode() {
48 final QName lf1 = QName.create("simple:uri", "2012-12-17", "lf1");
50 final DataSchemaNode contSchemaNode = prepareDataSchemaNode();
52 Preconditions.checkState(contSchemaNode instanceof ContainerSchemaNode);
53 final ContainerSchemaNode conContSchemaNode = (ContainerSchemaNode) contSchemaNode;
55 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> contContBuilder = Builders
56 .containerBuilder(conContSchemaNode);
58 final DataSchemaNode lf1SchemaNode = conContSchemaNode
59 .getDataChildByName(lf1);
60 Preconditions.checkState(lf1SchemaNode instanceof LeafSchemaNode);
62 final String lf1String = "";
63 contContBuilder.withChild(Builders
64 .leafBuilder((LeafSchemaNode) lf1SchemaNode)
65 .withValue(lf1String).build());
67 final NormalizedNodeContext testNormalizedNodeContext = new NormalizedNodeContext(
68 new InstanceIdentifierContext<DataSchemaNode>(null,
69 contSchemaNode, null, null), contContBuilder.build());
71 return testNormalizedNodeContext;
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);
85 contBuild.addChildNode(leafBuild);
86 return contBuild.build();