X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Fjson%2Fto%2Fcnsn%2Ftest%2FJsonToCnSnTest.java;h=7b71e42ab8ed669516c4e724231628c8d6bd4908;hb=484151212f586dd533984a11063c92799c414ec8;hp=3a90e73acbc4e8756ca88f127f2065245ac8dd05;hpb=b29204146ca6957b5f968e07d9e7e2052ba70ef1;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java index 3a90e73acb..7b71e42ab8 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java @@ -1,21 +1,27 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import java.io.ByteArrayInputStream; +import java.io.InputStream; import java.util.HashSet; import java.util.List; import java.util.Set; - -import javax.ws.rs.WebApplicationException; - import org.junit.Ignore; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider; import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper; -import org.opendaylight.controller.sal.restconf.impl.ResponseException; +import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException; import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.CompositeNode; @@ -25,8 +31,6 @@ import org.opendaylight.yangtools.yang.model.api.Module; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.JsonSyntaxException; - public class JsonToCnSnTest { private static final Logger LOG = LoggerFactory.getLogger(JsonToCnSnTest.class); @@ -51,13 +55,13 @@ public class JsonToCnSnTest { CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-leaflist-items.json", true, JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); - assertEquals(3, compositeNode.getChildren().size()); + assertEquals(3, compositeNode.getValue().size()); boolean lflst1_1 = false; boolean lflst1_2 = false; boolean lflst1_3 = false; - for (Node node : compositeNode.getChildren()) { + for (Node node : compositeNode.getValue()) { assertEquals("lflst1", node.getNodeType().getLocalName()); assertTrue(node instanceof SimpleNode); SimpleNode simpleNode = (SimpleNode) node; @@ -77,8 +81,8 @@ public class JsonToCnSnTest { } /** - * List contains 4 items and in every item are other elements. It is - * supposed that there should be: lf11, lflst11, cont11, lst11 + * List contains 4 items and in every item are other elements. It is supposed that there should be: lf11, lflst11, + * cont11, lst11 */ @Test public void multipleItemsInListTest() { @@ -98,9 +102,9 @@ public class JsonToCnSnTest { assertNotNull(compositeNode); assertEquals("cont", compositeNode.getNodeType().getLocalName()); - assertNotNull(compositeNode.getChildren()); - assertEquals(1, compositeNode.getChildren().size()); - Node lfNode = compositeNode.getChildren().iterator().next(); + assertNotNull(compositeNode.getValue()); + assertEquals(1, compositeNode.getValue().size()); + Node lfNode = compositeNode.getValue().iterator().next(); assertTrue(lfNode instanceof SimpleNode); assertEquals(null, ((SimpleNode) lfNode).getValue()); @@ -109,50 +113,53 @@ public class JsonToCnSnTest { @Test public void incorrectTopLevelElementsTest() { - Throwable cause1 = null; + RestconfDocumentedException cause1 = null; try { TestUtils .readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE); - } catch (WebApplicationException e) { + } catch (RestconfDocumentedException e) { cause1 = e; } assertNotNull(cause1); assertTrue(cause1 - .getCause() - .getMessage() + .getErrors() + .get(0) + .getErrorMessage() .contains( "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.")); - Throwable cause2 = null; + RestconfDocumentedException cause2 = null; try { TestUtils .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE); - } catch (WebApplicationException e) { + } catch (RestconfDocumentedException e) { cause2 = e; } assertNotNull(cause2); - assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element")); + assertTrue(cause2.getErrors().get(0).getErrorMessage().contains("Json Object should contain one element")); - Throwable cause3 = null; + RestconfDocumentedException cause3 = null; try { TestUtils - .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE); - } catch (WebApplicationException e) { + + .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE); + } catch (RestconfDocumentedException e) { cause3 = e; } assertNotNull(cause3); assertTrue(cause3 - .getCause() - .getMessage() + .getErrors() + .get(0) + .getErrorMessage() .contains( "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.")); } /** - * if leaf list with no data is in json then no corresponding data is - * created in composite node. if leaf with no data then exception is raised + * if leaf list with no data is in json then no corresponding data is created in composite node. if leaf with no + * data then exception is raised */ @Test public void emptyDataReadTest() { @@ -163,7 +170,7 @@ public class JsonToCnSnTest { assertEquals("cont", compositeNode.getNodeType().getLocalName()); assertTrue(compositeNode instanceof CompositeNode); - List> children = ((CompositeNode) compositeNode).getChildren(); + List> children = compositeNode.getValue(); assertEquals(1, children.size()); assertEquals("lflst2", children.get(0).getNodeType().getLocalName()); assertEquals("45", children.get(0).getValue()); @@ -171,19 +178,26 @@ public class JsonToCnSnTest { String reason = null; try { TestUtils.readInputToCnSn("/json-to-cnsn/empty-data1.json", true, JsonToCompositeNodeProvider.INSTANCE); - } catch (JsonSyntaxException e) { - reason = e.getMessage(); + } catch (RestconfDocumentedException e) { + reason = e.getErrors().get(0).getErrorMessage(); } assertTrue(reason.contains("Expected value at line")); } + @Test + public void testJsonBlankInput() throws Exception { + InputStream inputStream = new ByteArrayInputStream("".getBytes()); + CompositeNode compositeNode = JsonToCompositeNodeProvider.INSTANCE.readFrom(null, null, null, null, null, + inputStream); + assertNull(compositeNode); + } + /** - * Tests whether namespace stay unchanged if concrete values are - * present in composite or simple node and if the method for update is - * called. - * + * Tests whether namespace stay unchanged if concrete values are present in composite or simple node and if + * the method for update is called. + * */ @Test public void notSupplyNamespaceIfAlreadySupplied() { @@ -226,13 +240,13 @@ public class JsonToCnSnTest { assertEquals("cont", compositeNode.getNodeType().getLocalName()); - List> childs = compositeNode.getChildren(); + List> childs = compositeNode.getValue(); assertEquals(1, childs.size()); Node nd = childs.iterator().next(); assertTrue(nd instanceof CompositeNode); assertEquals("cont1", nd.getNodeType().getLocalName()); - childs = ((CompositeNode) nd).getChildren(); + childs = ((CompositeNode) nd).getValue(); assertEquals(4, childs.size()); SimpleNode lf11 = null; SimpleNode lf12 = null; @@ -267,28 +281,23 @@ public class JsonToCnSnTest { assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName()); assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString()); } - + @Ignore @Test public void loadDataAugmentedSchemaMoreEqualNamesTest() { - boolean exceptionCaught = false; - try { - loadAndNormalizeData("/common/augment/json/dataa.json", "/common/augment/yang", "cont", "main"); - loadAndNormalizeData("/common/augment/json/datab.json", "/common/augment/yang", "cont", "main"); - } catch (ResponseException e) { - exceptionCaught = true; - } - - assertFalse(exceptionCaught); + loadAndNormalizeData("/common/augment/json/dataa.json", "/common/augment/yang", "cont", "main"); + loadAndNormalizeData("/common/augment/json/datab.json", "/common/augment/yang", "cont", "main"); + } - private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace, - String moduleName) { + private void simpleTest(final String jsonPath, final String yangPath, final String topLevelElementName, + final String namespace, final String moduleName) { CompositeNode compNode = loadAndNormalizeData(jsonPath, yangPath, topLevelElementName, moduleName); verifyCompositeNode(compNode, namespace); } - private CompositeNode loadAndNormalizeData(String jsonPath, String yangPath, String topLevelElementName, String moduleName) { + private CompositeNode loadAndNormalizeData(final String jsonPath, final String yangPath, + final String topLevelElementName, final String moduleName) { CompositeNode compositeNode = TestUtils.readInputToCnSn(jsonPath, false, JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); @@ -305,8 +314,8 @@ public class JsonToCnSnTest { return compNode; } - private void verityMultipleItemsInList(CompositeNode compositeNode) { - List> childrenNodes = compositeNode.getChildren(); + private void verityMultipleItemsInList(final CompositeNode compositeNode) { + List> childrenNodes = compositeNode.getValue(); assertEquals(4, childrenNodes.size()); boolean lf11Found = false; boolean cont11Found = false; @@ -315,7 +324,7 @@ public class JsonToCnSnTest { assertEquals("lst1", lst1Item.getNodeType().getLocalName()); assertTrue(lst1Item instanceof CompositeNode); - List> childrenLst1 = ((CompositeNode) lst1Item).getChildren(); + List> childrenLst1 = ((CompositeNode) lst1Item).getValue(); assertEquals(1, childrenLst1.size()); String localName = childrenLst1.get(0).getNodeType().getLocalName(); if (localName.equals("lf11")) { @@ -331,7 +340,7 @@ public class JsonToCnSnTest { } else if (localName.equals("lst11")) { lst11Found = true; assertTrue(childrenLst1.get(0) instanceof CompositeNode); - assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size()); + assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getValue().size()); } } @@ -340,7 +349,7 @@ public class JsonToCnSnTest { assertTrue(lst11Found); } - private void verifyCompositeNode(CompositeNode compositeNode, String namespace) { + private void verifyCompositeNode(final CompositeNode compositeNode, final String namespace) { boolean cont1Found = false; boolean lst1Found = false; boolean lflst1_1Found = false; @@ -350,16 +359,16 @@ public class JsonToCnSnTest { // assertEquals(namespace, // compositeNode.getNodeType().getNamespace().toString()); - for (Node node : compositeNode.getChildren()) { + for (Node node : compositeNode.getValue()) { if (node.getNodeType().getLocalName().equals("cont1")) { if (node instanceof CompositeNode) { cont1Found = true; - assertEquals(0, ((CompositeNode) node).getChildren().size()); + assertEquals(0, ((CompositeNode) node).getValue().size()); } } else if (node.getNodeType().getLocalName().equals("lst1")) { if (node instanceof CompositeNode) { lst1Found = true; - assertEquals(0, ((CompositeNode) node).getChildren().size()); + assertEquals(0, ((CompositeNode) node).getValue().size()); } } else if (node.getNodeType().getLocalName().equals("lflst1")) { if (node instanceof SimpleNode) { @@ -392,8 +401,8 @@ public class JsonToCnSnTest { try { TestUtils.readInputToCnSn("/json-to-cnsn/unsupported-json-format.json", true, JsonToCompositeNodeProvider.INSTANCE); - } catch (WebApplicationException e) { - exceptionMessage = e.getCause().getMessage(); + } catch (RestconfDocumentedException e) { + exceptionMessage = e.getErrors().get(0).getErrorMessage(); } assertTrue(exceptionMessage.contains("Root element of Json has to be Object")); }