Merge "Fixed for bug : 1171 - issue while creating subnet"
[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.writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(),
50                 Collections.<Module> emptySet(), prepareDataSchemaNode(), StructuredDataToJsonProvider.INSTANCE);
51         assertNotNull(jsonOutput);
52
53         // pattern for e.g. > "lf1" : "" < or >"lf1":""<
54         assertTrue(containsStringData(jsonOutput, "\"lf1\"", ":", "\"\""));
55     }
56
57     private CompositeNode prepareCompositeNode() {
58         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
59                 TestUtils.buildQName("cont", "simple:uri", "2012-12-17"), null, null, ModifyAction.CREATE, null);
60         MutableSimpleNode<?> lf1 = NodeFactory.createMutableSimpleNode(
61                 TestUtils.buildQName("lf1", "simple:uri", "2012-12-17"), cont, "any value", ModifyAction.CREATE, null);
62         cont.getValue().add(lf1);
63         cont.init();
64         return cont;
65     }
66
67     private DataSchemaNode prepareDataSchemaNode() {
68         ContainerSchemaNodeBuilder contBuild = new ContainerSchemaNodeBuilder("module", 1, TestUtils.buildQName("cont",
69                 "simple:uri", "2012-12-17"), SchemaPath.create(true, QName.create("dummy")));
70         LeafSchemaNodeBuilder leafBuild = new LeafSchemaNodeBuilder("module", 2, TestUtils.buildQName("lf1",
71                 "simple:uri", "2012-12-17"), SchemaPath.create(true, QName.create("dummy")));
72         leafBuild.setType(new DummyType());
73         leafBuild.setConfiguration(true);
74
75         contBuild.addChildNode(leafBuild);
76         return contBuild.build();
77     }
78
79 }