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=309fb6441ad91ab6db16282a30d687914979259b;hp=c43de5d9e8525091eb535703381cb222ac42b9ca;hb=d468a5db1e6fe2c3949d3f6227c0645c6777ecb5;hpb=d6e3e28bf86638685e55289d6cd9cb749838a75e 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 c43de5d9e8..309fb6441a 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 @@ -13,9 +13,7 @@ import java.util.*; import java.util.concurrent.Future; import javax.ws.rs.WebApplicationException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.*; import javax.xml.stream.XMLStreamException; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; @@ -24,8 +22,7 @@ import javax.xml.transform.stream.StreamResult; import org.opendaylight.controller.md.sal.common.api.TransactionStatus; import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; import org.opendaylight.controller.sal.restconf.impl.*; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.*; import org.opendaylight.yangtools.yang.data.api.*; import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder; import org.opendaylight.yangtools.yang.model.api.*; @@ -99,7 +96,7 @@ final class TestUtils { } return (CompositeNode) dataTree; } - + public static Document loadDocumentFrom(InputStream inputStream) { try { DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); @@ -135,7 +132,42 @@ final class TestUtils { } static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, String outputPath) { - String jsonResult = null; + return convertCompositeNodeDataAndYangToJson(compositeNode, yangPath, outputPath, null, null); + } + + static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, + String outputPath, String searchedModuleName, String searchedDataSchemaName) { + Set modules = resolveModules(yangPath); + Module module = resolveModule(searchedModuleName, modules); + DataSchemaNode dataSchemaNode = resolveDataSchemaNode(module, searchedDataSchemaName); + + try { + return writeCompNodeWithSchemaContextToJson(compositeNode, outputPath, modules, dataSchemaNode); + } catch (WebApplicationException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + 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) { + if (m.getName().equals(searchedModuleName)) { + module = m; + break; + } + } + } else if (modules.size() == 1) { + module = modules.iterator().next(); + } + return module; + } + + static Set resolveModules(String yangPath) { Set modules = null; try { @@ -143,32 +175,50 @@ final class TestUtils { } catch (FileNotFoundException e) { e.printStackTrace(); } - assertNotNull("modules can't be null.", modules); - assertNotNull("Composite node can't be null", compositeNode); + return modules; + } - StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE; - for (Module module : modules) { - ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); - for (DataSchemaNode dataSchemaNode : module.getChildNodes()) { - StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode); - try { - structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS); - } catch (WebApplicationException | IOException e) { - e.printStackTrace(); + static DataSchemaNode resolveDataSchemaNode(Module module, String searchedDataSchemaName) { + assertNotNull("Module is missing", module); + + DataSchemaNode dataSchemaNode = null; + if (searchedDataSchemaName != null) { + for (DataSchemaNode dsn : module.getChildNodes()) { + if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) { + dataSchemaNode = dsn; } - assertFalse( - "Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(), - byteArrayOS.toString().isEmpty()); } - jsonResult = byteArrayOS.toString(); + } 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); + assertNotNull("Composite node can't be null", compositeNode); + ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); + + 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(); + if (outputPath != null) { try { outputToFile(byteArrayOS, outputPath); } catch (IOException e) { System.out.println("Output file wasn't cloased sucessfuly."); } - } + return jsonResult; } @@ -293,10 +343,12 @@ final class TestUtils { ControllerContext controllerContext = mock(ControllerContext.class); BrokerFacade broker = mock(BrokerFacade.class); - RpcResult rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build(); + RpcResult rpcResult = new DummyRpcResult.Builder().result( + TransactionStatus.COMMITED).build(); Future> future = DummyFuture.builder().rpcResult(rpcResult).build(); when(controllerContext.toInstanceIdentifier(any(String.class))).thenReturn(instIdAndSchema); - when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(future); + when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn( + future); restconf.setControllerContext(controllerContext); restconf.setBroker(broker); @@ -359,5 +411,20 @@ final class TestUtils { } } } + + public static void prepareMockForRestconfBeforeNormalization(Set modules, DataSchemaNode dataSchemaNode, + RestconfImpl restconf) { + ControllerContext instance = ControllerContext.getInstance(); + instance.setSchemas(TestUtils.loadSchemaContext(modules)); + restconf.setControllerContext(ControllerContext.getInstance()); + + BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class); + when(mockedBrokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))) + .thenReturn( + new DummyFuture.Builder().rpcResult( + new DummyRpcResult.Builder().result(TransactionStatus.COMMITED) + .build()).build()); + restconf.setBroker(mockedBrokerFacade); + } }