X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Fjson%2Fto%2Fcnsn%2Ftest%2FJsonToCnSnTest.java;h=47e329cc3ef11a4cdfc8842c9a1285a445989517;hb=706137d6330c1611066dd74f3613338a161aac93;hp=415d58e53d3f43684aaca2edfde78eee91a033ca;hpb=290b7c23512204074fcd0fb7163b0adea48823fd;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 415d58e53d..47e329cc3e 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 @@ -8,21 +8,21 @@ 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; @@ -32,8 +32,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); @@ -116,44 +114,39 @@ 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) { + TestUtils.readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE); + } catch (RestconfDocumentedException e) { cause1 = e; } assertNotNull(cause1); - assertTrue(cause1 - .getCause() - .getMessage() - .contains( - "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.")); + assertTrue(cause1.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) { + .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE); + } 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() - .contains( - "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.")); + assertTrue(cause3.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.")); } @@ -178,14 +171,22 @@ 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 @@ -278,15 +279,9 @@ public class JsonToCnSnTest { @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; - } + loadAndNormalizeData("/common/augment/json/dataa.json", "/common/augment/yang", "cont", "main"); + loadAndNormalizeData("/common/augment/json/datab.json", "/common/augment/yang", "cont", "main"); - assertFalse(exceptionCaught); } private void simpleTest(final String jsonPath, final String yangPath, final String topLevelElementName, final String namespace, @@ -399,8 +394,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")); }