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