Merge changes Ia268965a,Iefa79f99
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / TestUtils.java
index 1d8d7495f9dfabae21bdddb737401032af34724b..89d24ad0579b9b78a77a68f121d8f13ca75ef399 100644 (file)
@@ -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<Module> 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<Module> 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<Module> resolveModules(String yangPath) {
         Set<Module> modules = null;
 
         try {
@@ -143,32 +175,48 @@ 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();
-            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();
+        }
+        return dataSchemaNode;
+    }
+
+    static String writeCompNodeWithSchemaContextToJson(CompositeNode compositeNode, String outputPath,
+            Set<Module> 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();
+        try {
+            outputToFile(byteArrayOS, outputPath);
+        } catch (IOException e) {
+            System.out.println("Output file wasn't cloased sucessfuly.");
         }
+
         return jsonResult;
     }
 
@@ -296,7 +344,8 @@ final class TestUtils {
         RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build();
         Future<RpcResult<TransactionStatus>> 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);
@@ -320,15 +369,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) {
@@ -338,7 +387,7 @@ final class TestUtils {
         } else {
             moduleRes = modules.iterator().next();
         }
-        
+
         if (moduleRes.getChildNodes() == null) {
             return null;
         }