24dba17c90cdff4e9a87d0a059160357dcbaad88
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / CnSnToJsonNotExistingLeafTypeTest.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.cnsn.to.json.test;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.IOException;
14 import java.util.Collections;
15 import javax.ws.rs.WebApplicationException;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
19 import org.opendaylight.controller.sal.restconf.impl.test.DummyType;
20 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
21 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
25 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
26 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
27 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class CnSnToJsonNotExistingLeafTypeTest extends YangAndXmlAndDataSchemaLoader {
37
38     private static final Logger LOG = LoggerFactory.getLogger(CnSnToJsonNotExistingLeafTypeTest.class);
39
40     @BeforeClass
41     public static void initialize() {
42         dataLoad("/cnsn-to-json/simple-data-types");
43     }
44
45     @Test
46     public void incorrectTopLevelElementTest() throws WebApplicationException, IOException {
47         String jsonOutput = null;
48         jsonOutput = TestUtils
49                 .writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(),
50                         Collections.<Module>emptySet(), prepareDataSchemaNode(),
51                         StructuredDataToJsonProvider.INSTANCE);
52         assertNotNull(jsonOutput);
53         assertTrue(jsonOutput.contains("\"lf1\": \"\""));
54     }
55
56     private CompositeNode prepareCompositeNode() {
57         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
58                 TestUtils.buildQName("cont", "simple:uri", "2012-12-17"), null, null, ModifyAction.CREATE, null);
59         MutableSimpleNode<?> lf1 = NodeFactory.createMutableSimpleNode(
60                 TestUtils.buildQName("lf1", "simple:uri", "2012-12-17"), cont, "any value", ModifyAction.CREATE, null);
61         cont.getValue().add(lf1);
62         cont.init();
63         return cont;
64     }
65
66     private DataSchemaNode prepareDataSchemaNode() {
67         ContainerSchemaNodeBuilder contBuild = new ContainerSchemaNodeBuilder("module", 1, TestUtils.buildQName("cont",
68                 "simple:uri", "2012-12-17"), SchemaPath.create(true, QName.create("dummy")));
69         LeafSchemaNodeBuilder leafBuild = new LeafSchemaNodeBuilder("module", 2, TestUtils.buildQName("lf1",
70                 "simple:uri", "2012-12-17"), SchemaPath.create(true, QName.create("dummy")));
71         leafBuild.setType(new DummyType());
72         leafBuild.setConfiguration(true);
73
74         contBuild.addChildNode(leafBuild);
75         return contBuild.build();
76     }
77
78 }