You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/04/18 14:02:47 UTC

[GitHub] houshengbo closed pull request #277: Remove JSON.parseFull references

houshengbo closed pull request #277: Remove JSON.parseFull references
URL: https://github.com/apache/incubator-openwhisk-cli/pull/277
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/tests/src/test/scala/whisk/core/cli/test/ApiGwCliBasicTests.scala b/tests/src/test/scala/whisk/core/cli/test/ApiGwCliBasicTests.scala
index 0a44abe1..2c7e64e9 100644
--- a/tests/src/test/scala/whisk/core/cli/test/ApiGwCliBasicTests.scala
+++ b/tests/src/test/scala/whisk/core/cli/test/ApiGwCliBasicTests.scala
@@ -36,11 +36,13 @@ import common.WhiskProperties
 import common.{TestUtils, WhiskProperties, WskProps}
 
 import scala.concurrent.duration.DurationInt
-import scala.util.parsing.json.JSON
 import scala.util.matching.Regex
 
 import org.apache.commons.io.FileUtils
 
+import spray.json._
+import spray.json.DefaultJsonProtocol._
+
 /**
  * Tests for testing the CLI "api" subcommand.  Most of these tests require a deployed backend.
  */
@@ -203,9 +205,8 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
     tmpFileName
   }
 
-  def getParametersFromJson(rr: RunResult, pathName: String): List[Map[String, Any]] = {
-    val parsed = JSON.parseFull(rr.stdout).asInstanceOf[Option[Map[String, Map[String, Map[String, Map[String, List[Map[String, String]]]]]]]]
-    parsed.get("paths").get(pathName).get("get").get("parameters").get
+  def getParametersFromJson(json: JsObject, pathName: String): Vector[JsValue] = {
+    json.fields("paths").asJsObject.fields(pathName).asJsObject.fields("get").asJsObject.fields("parameters").convertTo[JsArray].elements
   }
 
   def getSslConfig(): RestAssuredConfig = {
@@ -213,12 +214,12 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
     new RestAssuredConfig().sslConfig(new SSLConfig().keystore("keystore", WhiskProperties.getSslCertificateChallenge).allowAllHostnames())
   }
 
-  def validateParameter(parameter: Map[String, Any], name: String, in: String, required: Boolean, pType: String, description: String): Unit = {
-    parameter.get("name").get should be(name)
-    parameter.get("in").get should be(in)
-    parameter.get("required") should be(Some(required))
-    parameter.get("type").get should be(pType)
-    parameter.get("description").get should be(description)
+  def validateParameter(parameter: JsObject, name: String, in: String, required: Boolean, pType: String, description: String): Unit = {
+    parameter.fields("name") should be(name.toJson)
+    parameter.fields("in") should be(in.toJson)
+    parameter.fields("required") should be(required.toJson)
+    parameter.fields("type") should be(pType.toJson)
+    parameter.fields("description") should be(description.toJson)
   }
 
   behavior of "Wsk api"
@@ -228,7 +229,6 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
   it should "create the API when swagger file contains path parameters" in withAssetCleaner(
     wskprops) { (wp, assetHelper) =>
     val actionName = "cli_apigwtest_path_param_swagger_action"
-    var exception: Throwable = null
     val apiName = "/guest/v1"
     val reqPath = "\\$\\(request.path\\)"
     val testRelPath = "/api2/greeting2/{name}"
@@ -262,7 +262,6 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
       verifyApiDeleted(rr)
 
       //Create the api using the swagger file.
-
       rr = apiCreate(swagger = Some(swaggerFile.getAbsolutePath()), expectedExitCode = SUCCESS_EXIT)
       verifyApiCreated(rr)
       val swaggerApiUrl = getSwaggerUrl(rr).replace("{name}", testUrlName)
@@ -270,9 +269,10 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
       //Lets validate that the swagger we get from the create contains the correct info.
       rr = apiGet(basepathOrApiName = Some(apiName))
       rr.stdout should include regex (s"""target-url.*${actionName}.http${reqPath}""")
-      val params = getParametersFromJson(rr, testRelPath)
+
+      val params = getParametersFromJson(rr.stdout.parseJson.asJsObject, testRelPath)
       params.size should be(1)
-      validateParameter(params(0), "name", "path", true, "string", "Default description for 'name'")
+      validateParameter(params(0).asJsObject, "name", "path", true, "string", "Default description for 'name'")
 
       //Lets call the swagger url so we can make sure the response is valid and contains our path in the ow path
       val apiToInvoke = s"$swaggerApiUrl"
@@ -282,16 +282,12 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
         response.statusCode should be(200)
         response
       }, 6, Some(2.second))
-      val jsonReponse = JSON.parseFull(response.asString()).asInstanceOf[Option[Map[String, String]]].get
-      jsonReponse.get("__ow_path").get should not be ("")
-      jsonReponse.get("__ow_path").get should include (testRelPathGet)
+      val jsonResponse = response.body.asString.parseJson.asJsObject
 
-    } catch {
-      case unknown: Throwable => exception = unknown
+      jsonResponse.fields("__ow_path").toString should include (testRelPathGet)
     } finally {
       apiDelete(basepathOrApiName = apiName)
     }
-    assert(exception == null)
   }
 
   it should "reject an api commands with an invalid path parameter" in {
diff --git a/tests/src/test/scala/whisk/core/cli/test/ApiGwCliTests.scala b/tests/src/test/scala/whisk/core/cli/test/ApiGwCliTests.scala
index 4dfe20fb..8fa0f61a 100644
--- a/tests/src/test/scala/whisk/core/cli/test/ApiGwCliTests.scala
+++ b/tests/src/test/scala/whisk/core/cli/test/ApiGwCliTests.scala
@@ -26,7 +26,8 @@ import org.junit.runner.RunWith
 import org.scalatest.junit.JUnitRunner
 
 import scala.concurrent.duration.DurationInt
-import scala.util.parsing.json.JSON
+
+import spray.json._
 
 /**
   * Tests for basic CLI usage. Some of these tests require a deployed backend.
@@ -84,7 +85,6 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
     val testUrlOp = "get"
     val testApiName = testName + " API Name"
     val actionName = testName + "_action"
-    var exception: Throwable = null
     val reqPath = "\\$\\(request.path\\)"
 
     // Create the action for the API.  It must be a "web-action" action.
@@ -111,10 +111,10 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
       rr.stdout should include(s"${actionName}")
       rr.stdout should include regex (""""cors":\s*\{\s*\n\s*"enabled":\s*true""")
       rr.stdout should include regex (s"""target-url.*${actionName}.http${reqPath}""")
-      val params = getParametersFromJson(rr, testRelPath)
+      val params = getParametersFromJson(rr.stdout.parseJson.asJsObject, testRelPath)
       params.size should be(2)
-      validateParameter(params(0), "with", "path", true, "string", "Default description for 'with'")
-      validateParameter(params(1), "path", "path", true, "string", "Default description for 'path'")
+      validateParameter(params(0).asJsObject, "with", "path", true, "string", "Default description for 'with'")
+      validateParameter(params(1).asJsObject, "path", "path", true, "string", "Default description for 'path'")
 
       //Lets call the swagger url so we can make sure the response is valid and contains our path in the ow path
       val apiToInvoke = s"$swaggerApiUrl"
@@ -124,15 +124,12 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
         response.statusCode should be(200)
         response
       }, 6, Some(2.second))
-      val jsonReponse = JSON.parseFull(response.asString()).asInstanceOf[Option[Map[String, String]]].get
-      jsonReponse.get("__ow_path").get should not be ("")
-      jsonReponse.get("__ow_path").get should include (testRelPathGet)
-    } catch {
-      case unknown: Throwable => exception = unknown
+      val jsonResponse = response.body.asString.parseJson.asJsObject
+
+      jsonResponse.fields("__ow_path").toString should include (testRelPathGet)
     } finally {
       apiDelete(basepathOrApiName = testBasePath)
     }
-    assert(exception == null)
   }
 
   it should "create api with path parameters and pass them into the action bound to the api" in withAssetCleaner(
@@ -148,7 +145,6 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
     val testUrlOp = "get"
     val testApiName = testName + " API Name"
     val actionName = testName + "_action"
-    var exception: Throwable = null
     val reqPath = "\\$\\(request.path\\)"
     // Create the action for the API.  It must be a "web-action" action.
     val file = TestUtils.getTestActionFilename(s"echo-web-http.js")
@@ -175,14 +171,14 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
       rr.stdout should include(s"${actionName}")
       rr.stdout should include regex (""""cors":\s*\{\s*\n\s*"enabled":\s*true""")
       rr.stdout should include regex (s"""target-url.*${actionName}.http${reqPath}""")
-      val params = getParametersFromJson(rr, testRelPath)
+      val params = getParametersFromJson(rr.stdout.parseJson.asJsObject, testRelPath)
 
       // should have 4, not 5 parameter definitions (i.e. don't define "extra" twice
       params.size should be(4)
-      validateParameter(params(0), "with", "path", true, "string", "Default description for 'with'")
-      validateParameter(params(1), "double", "path", true, "string", "Default description for 'double'")
-      validateParameter(params(2), "extra", "path", true, "string", "Default description for 'extra'")
-      validateParameter(params(3), "path", "path", true, "string", "Default description for 'path'")
+      validateParameter(params(0).asJsObject, "with", "path", true, "string", "Default description for 'with'")
+      validateParameter(params(1).asJsObject, "double", "path", true, "string", "Default description for 'double'")
+      validateParameter(params(2).asJsObject, "extra", "path", true, "string", "Default description for 'extra'")
+      validateParameter(params(3).asJsObject, "path", "path", true, "string", "Default description for 'path'")
 
       //Lets call the swagger url so we can make sure the response is valid and contains our path in the ow path
       val apiToInvoke = s"$swaggerApiUrl"
@@ -192,14 +188,11 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
         response.statusCode should be(200)
         response
       }, 6, Some(2.second))
-      val jsonReponse = JSON.parseFull(response.asString()).asInstanceOf[Option[Map[String, String]]].get
-      jsonReponse.get("__ow_path").get should not be ("")
-      jsonReponse.get("__ow_path").get should include (testRelPathGet)
-    } catch {
-      case unknown: Throwable => exception = unknown
+      val jsonResponse = response.body.asString.parseJson.asJsObject
+
+      jsonResponse.fields("__ow_path").toString should include (testRelPathGet)
     } finally {
       apiDelete(basepathOrApiName = testBasePath)
     }
-    assert(exception == null)
   }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services