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.cnsn.to.json.test;
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;
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;
37 public class CnSnToJsonNotExistingLeafTypeTest extends YangAndXmlAndDataSchemaLoader {
39 private static final Logger LOG = LoggerFactory.getLogger(CnSnToJsonNotExistingLeafTypeTest.class);
42 public static void initialize() {
43 dataLoad("/cnsn-to-json/simple-data-types");
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);
53 // pattern for e.g. > "lf1" : "" < or >"lf1":""<
54 assertTrue(containsStringData(jsonOutput, "\"lf1\"", ":", "\"\""));
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);
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);
75 contBuild.addChildNode(leafBuild);
76 return contBuild.build();