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