X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Ftest%2FTestUtils.java;h=89d24ad0579b9b78a77a68f121d8f13ca75ef399;hp=bc941b997e35dd63c8a3250cb52e8ccf66e27314;hb=bab3649670c33b3b12a49c59fc1c5cabcbcb924e;hpb=0f4cc2d53eba0394556d689459009a538493970d diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java index bc941b997e..89d24ad057 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java @@ -137,16 +137,22 @@ final class TestUtils { static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, String outputPath, String searchedModuleName, String searchedDataSchemaName) { - String jsonResult = null; - Set modules = null; + Set modules = resolveModules(yangPath); + Module module = resolveModule(searchedModuleName, modules); + DataSchemaNode dataSchemaNode = resolveDataSchemaNode(module, searchedDataSchemaName); try { - modules = TestUtils.loadModules(ToJsonBasicDataTypesTest.class.getResource(yangPath).getPath()); - } catch (FileNotFoundException e) { + return writeCompNodeWithSchemaContextToJson(compositeNode, outputPath, modules, dataSchemaNode); + } catch (WebApplicationException | IOException e) { + // TODO Auto-generated catch block e.printStackTrace(); } - assertNotNull("modules can't be null.", modules); + return null; + } + + static Module resolveModule(String searchedModuleName, Set modules) { + assertNotNull("modules can't be null.", modules); Module module = null; if (searchedModuleName != null) { for (Module m : modules) { @@ -158,12 +164,24 @@ final class TestUtils { } else if (modules.size() == 1) { module = modules.iterator().next(); } - assertNotNull("Module is missing", module); + return module; + } - assertNotNull("Composite node can't be null", compositeNode); + static Set resolveModules(String yangPath) { + Set modules = null; + + try { + modules = TestUtils.loadModules(ToJsonBasicDataTypesTest.class.getResource(yangPath).getPath()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + return modules; + } + + static DataSchemaNode resolveDataSchemaNode(Module module, String searchedDataSchemaName) { + assertNotNull("Module is missing", module); - StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE; - ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); DataSchemaNode dataSchemaNode = null; if (searchedDataSchemaName != null) { for (DataSchemaNode dsn : module.getChildNodes()) { @@ -174,19 +192,23 @@ final class TestUtils { } else if (module.getChildNodes().size() == 1) { dataSchemaNode = module.getChildNodes().iterator().next(); } + return dataSchemaNode; + } + + static String writeCompNodeWithSchemaContextToJson(CompositeNode compositeNode, String outputPath, + Set modules, DataSchemaNode dataSchemaNode) throws IOException, WebApplicationException { + String jsonResult; + assertNotNull(dataSchemaNode); - // SchemaContextUtil. + assertNotNull("Composite node can't be null", compositeNode); + ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); - ControllerContext controllerContext = ControllerContext.getInstance(); - controllerContext.setSchemas(loadSchemaContext(modules)); - StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode); - try { - structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS); - } catch (WebApplicationException | IOException e) { - e.printStackTrace(); - } - assertFalse("Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(), - byteArrayOS.toString().isEmpty()); + ControllerContext contContext = ControllerContext.getInstance(); + contContext.setSchemas(loadSchemaContext(modules)); + + StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE; + structuredDataToJsonProvider.writeTo(new StructuredData(compositeNode, dataSchemaNode), null, null, null, null, + null, byteArrayOS); jsonResult = byteArrayOS.toString(); try {