Commits

Rich Manalang [Atlassian]  committed be978eb

added response docs and tests

  • Participants
  • Parent commits 891a51e
  • Branches test

Comments (0)

Files changed (3)

File process-wadl.js

 //     
 function processWADL(url) {
   var debug = false,
+  useNamespace = false,
   // Turn this function to a jQuery promise object
   dfd = new jQuery.Deferred();
 
+  function addNamespace(selector){
+    if (!useNamespace) return selector;
+    return "ns2\\:" + selector;
+  }
+
   // Use jQuery ajax to get WADL from a URL
   jQuery.ajax({
     url:url,
     });
   }).success(function(x) {
     // Ajax call was successful
-    var outResources = [],
-    base = jQuery(x).find('resources').attr('base'),
-    top = jQuery(x).find('resources').find('>resource');
+    var outResources = [];
+
+    // Test to see if namespace is required
+    if (jQuery(x).find('resources').length === 0){
+      useNamespace = true;
+    }
 
+    var base = jQuery(x).find(addNamespace('resources')).attr('base'),
+    top = jQuery(x).find(addNamespace('resources')).find('>'+addNamespace('resource'));
+    
     // Helper function for extracting the doc CDATA section from
     // a node
-    function getDoc(node) {
-      var txt, doc = node.find('>doc');
+    function getDoc(node,html) {
+
+      var txt, 
+      html = html || false,
+      doc = node.find('>'+addNamespace('doc'));
       if (doc.length > 0) {
-        txt = doc.text();
+        if (html) {
+          try {
+            txt = doc.contents()[0].nextSibling.innerHTML.replace(/<(\/?)([^:>]*:)?([^>]+)>/g, "<$1$3>");
+          } catch(e){
+            txt = doc.text();
+          }
+        } else {
+          txt = doc.text();          
+        }
       }
       return txt || "";
     }
       obj.description = getDoc(resource);
 
       // Extract resource-wide params that live inside the path
-      params = resource.find('>param');
+      params = resource.find('>'+addNamespace('param'));
       if (params.length > 0) {
         // Reset resource-wide params if no template params in uri
         if (/\{/g.test(path)) {
       }
 
       // Look for methods inside resources
-      methods = resource.find('>method');
+      methods = resource.find('>'+addNamespace('method'));
       if (methods.length > 0) {
         // Methods found inside the resource. Extract them
         jQuery.each(methods, function() {
 
           // Look for params inside methods. If found
           // extract them
-          request = self.find('>request');
+          request = self.find('>'+addNamespace('request'));
           if (request.length > 0) {
             // Merge resource-wide params with method params
             jQuery.merge(methodObj.params, resourceWideParams);
-            params = request.find('>param');
+            params = request.find('>'+addNamespace('param'));
             jQuery.each(params, function() {
               var param = {},
               self = jQuery(this);
             methodObj.params = resourceWideParams;
           }
 
-          response = self.find('>response');
+          response = self.find('>'+addNamespace('response'));
           if (response.length > 0){
-            methodObj.representations = jQuery.map(response.find('>representation[mediaType]'), function(n){
+            methodObj.representations = {};
+            methodObj.representations.contentTypes = jQuery.map(response.find('>'+addNamespace('representation')+'[mediaType]'), function(n){
               return jQuery(n).attr('mediaType');
             });
+            methodObj.representations.responses = {};
+            jQuery.map(response.find('>'+addNamespace('representation')), function(n){
+              methodObj.representations.responses[jQuery(n).attr('status')] = getDoc(jQuery(n),true);
+            });
           }
 
           outMethods.push(methodObj);
         });
 
         // Loop through additional child resources
-        childResource = resource.find('>resource');
+        childResource = resource.find('>'+addNamespace('resource'));
         jQuery.each(childResource, function() {
           var self = jQuery(this);
           getResource(self, path, resourceWideParams);
       } else {
         // No methods found inside this resource.
         // Loop through additional child resources
-        childResource = resource.find('>resource');
+        childResource = resource.find('>'+addNamespace('resource'));
         jQuery.each(childResource, function() {
           var self = jQuery(this);
           getResource(self, path, resourceWideParams);

File tests/tests.html

+<html>
+<head>
+  <meta charset="utf-8" />
+  <title>Process WADL Tests</title>
+  <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.5.0.css" />
+</head>
+<body>
+  <div id="qunit"></div>
+  <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
+  <script src="http://code.jquery.com/qunit/qunit-1.5.0.js"></script>
+  <script src="../process-wadl.js"></script>
+  <script src="./tests.js"></script>
+</body>
+</html>

File tests/tests.js

+processWADL('test-wadl.xml').done(function(x){
+  
+  console.log(x);
+  
+  test('Parser found resources', function(){
+    ok(x.resources.length > 0, "No resources found")
+  });
+
+  test('Each resource should have a description, methods, and name', function(){
+    for(r in x.resources){
+      ok(typeof x.resources[r].description !== 'undefined', "description is missing");
+      ok(typeof x.resources[r].methods !== 'undefined', "methods is missing");
+      ok(typeof x.resources[r].name !== 'undefined', "name is missing");
+    }
+  });
+
+  test('Resource should have at least 1 method', function(){
+    for(r in x.resources){
+      ok(x.resources[r].methods.length > 0, "resource methods missing");
+    }
+  });
+
+  test('Methods should have a uri', function(){
+    for(r in x.resources){
+      for(m in x.resources[r].methods){
+        var method = x.resources[r].methods[m];
+        ok(methods.uri !== '', "method uri missing");
+      }
+    }
+  });
+
+});