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%2Ftest%2FTestUtils.java;h=bc941b997e35dd63c8a3250cb52e8ccf66e27314;hb=b2d4575c4425e3b3d5aeaf1190e01e5d5a5286aa;hp=3d06e4a759985f1eb938576b7bac10e9be9b8aae;hpb=c88736a564601862c255247f2cfd99f5ef7e9ef2;p=controller.git 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 3d06e4a759..bc941b997e 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 @@ -10,16 +10,19 @@ import java.io.*; import java.net.*; import java.sql.Date; import java.util.*; +import java.util.concurrent.Future; import javax.ws.rs.WebApplicationException; +import javax.xml.parsers.*; import javax.xml.stream.XMLStreamException; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; 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.*; import org.opendaylight.yangtools.yang.data.api.*; import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder; import org.opendaylight.yangtools.yang.model.api.*; @@ -27,6 +30,9 @@ import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.slf4j.*; import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import com.google.common.base.Preconditions; final class TestUtils { @@ -91,7 +97,19 @@ final class TestUtils { return (CompositeNode) dataTree; } + public static Document loadDocumentFrom(InputStream inputStream) { + try { + DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); + return docBuilder.parse(inputStream); + } catch (SAXException | IOException | ParserConfigurationException e) { + logger.error("Error during loading Document from XML", e); + return null; + } + } + public static String getDocumentInPrintableForm(Document doc) { + Preconditions.checkNotNull(doc); try { ByteArrayOutputStream out = new ByteArrayOutputStream(); TransformerFactory tf = TransformerFactory.newInstance(); @@ -114,6 +132,11 @@ final class TestUtils { } static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, String outputPath) { + return convertCompositeNodeDataAndYangToJson(compositeNode, yangPath, outputPath, null, null); + } + + static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, + String outputPath, String searchedModuleName, String searchedDataSchemaName) { String jsonResult = null; Set modules = null; @@ -124,30 +147,54 @@ final class TestUtils { } 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(); + } + assertNotNull("Module is missing", module); + assertNotNull("Composite node can't be null", compositeNode); 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(); + ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); + 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(); - try { - outputToFile(byteArrayOS, outputPath); - } catch (IOException e) { - System.out.println("Output file wasn't cloased sucessfuly."); } + } else if (module.getChildNodes().size() == 1) { + dataSchemaNode = module.getChildNodes().iterator().next(); + } + assertNotNull(dataSchemaNode); + // SchemaContextUtil. + 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()); + + jsonResult = byteArrayOS.toString(); + try { + outputToFile(byteArrayOS, outputPath); + } catch (IOException e) { + System.out.println("Output file wasn't cloased sucessfuly."); + } + return jsonResult; } @@ -272,9 +319,11 @@ final class TestUtils { ControllerContext controllerContext = mock(ControllerContext.class); BrokerFacade broker = mock(BrokerFacade.class); + RpcResult rpcResult = 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( - new DummyFuture()); + future); restconf.setControllerContext(controllerContext); restconf.setBroker(broker); @@ -298,15 +347,15 @@ final class TestUtils { if (modules.size() < 1) { return null; } - - Module moduleRes = null; + + Module moduleRes = null; if (modules.size() > 1) { if (moduleName == null) { return null; } else { - for (Module module: modules) { + for (Module module : modules) { if (module.getName().equals(moduleName)) { - moduleRes = module; + moduleRes = module; } } if (moduleRes == null) { @@ -316,7 +365,7 @@ final class TestUtils { } else { moduleRes = modules.iterator().next(); } - + if (moduleRes.getChildNodes() == null) { return null; }