Skip to content

Commit dfdf854

Browse files
dubeeVincent
authored andcommitted
Remove JSON.parseFull references
1 parent 8dbc273 commit dfdf854

2 files changed

Lines changed: 32 additions & 43 deletions

File tree

tests/src/test/scala/whisk/core/cli/test/ApiGwCliBasicTests.scala

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ import common.WhiskProperties
3636
import common.{TestUtils, WhiskProperties, WskProps}
3737

3838
import scala.concurrent.duration.DurationInt
39-
import scala.util.parsing.json.JSON
4039
import scala.util.matching.Regex
4140

4241
import org.apache.commons.io.FileUtils
4342

43+
import spray.json._
44+
import spray.json.DefaultJsonProtocol._
45+
4446
/**
4547
* Tests for testing the CLI "api" subcommand. Most of these tests require a deployed backend.
4648
*/
@@ -203,22 +205,21 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
203205
tmpFileName
204206
}
205207

206-
def getParametersFromJson(rr: RunResult, pathName: String): List[Map[String, Any]] = {
207-
val parsed = JSON.parseFull(rr.stdout).asInstanceOf[Option[Map[String, Map[String, Map[String, Map[String, List[Map[String, String]]]]]]]]
208-
parsed.get("paths").get(pathName).get("get").get("parameters").get
208+
def getParametersFromJson(json: JsObject, pathName: String): Vector[JsValue] = {
209+
json.fields("paths").asJsObject.fields(pathName).asJsObject.fields("get").asJsObject.fields("parameters").convertTo[JsArray].elements
209210
}
210211

211212
def getSslConfig(): RestAssuredConfig = {
212213
// force RestAssured to allow all hosts in SSL certificates
213214
new RestAssuredConfig().sslConfig(new SSLConfig().keystore("keystore", WhiskProperties.getSslCertificateChallenge).allowAllHostnames())
214215
}
215216

216-
def validateParameter(parameter: Map[String, Any], name: String, in: String, required: Boolean, pType: String, description: String): Unit = {
217-
parameter.get("name").get should be(name)
218-
parameter.get("in").get should be(in)
219-
parameter.get("required") should be(Some(required))
220-
parameter.get("type").get should be(pType)
221-
parameter.get("description").get should be(description)
217+
def validateParameter(parameter: JsObject, name: String, in: String, required: Boolean, pType: String, description: String): Unit = {
218+
parameter.fields("name") should be(name.toJson)
219+
parameter.fields("in") should be(in.toJson)
220+
parameter.fields("required") should be(required.toJson)
221+
parameter.fields("type") should be(pType.toJson)
222+
parameter.fields("description") should be(description.toJson)
222223
}
223224

224225
behavior of "Wsk api"
@@ -228,7 +229,6 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
228229
it should "create the API when swagger file contains path parameters" in withAssetCleaner(
229230
wskprops) { (wp, assetHelper) =>
230231
val actionName = "cli_apigwtest_path_param_swagger_action"
231-
var exception: Throwable = null
232232
val apiName = "/guest/v1"
233233
val reqPath = "\\$\\(request.path\\)"
234234
val testRelPath = "/api2/greeting2/{name}"
@@ -262,17 +262,17 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
262262
verifyApiDeleted(rr)
263263

264264
//Create the api using the swagger file.
265-
266265
rr = apiCreate(swagger = Some(swaggerFile.getAbsolutePath()), expectedExitCode = SUCCESS_EXIT)
267266
verifyApiCreated(rr)
268267
val swaggerApiUrl = getSwaggerUrl(rr).replace("{name}", testUrlName)
269268

270269
//Lets validate that the swagger we get from the create contains the correct info.
271270
rr = apiGet(basepathOrApiName = Some(apiName))
272271
rr.stdout should include regex (s"""target-url.*${actionName}.http${reqPath}""")
273-
val params = getParametersFromJson(rr, testRelPath)
272+
273+
val params = getParametersFromJson(rr.stdout.parseJson.asJsObject, testRelPath)
274274
params.size should be(1)
275-
validateParameter(params(0), "name", "path", true, "string", "Default description for 'name'")
275+
validateParameter(params(0).asJsObject, "name", "path", true, "string", "Default description for 'name'")
276276

277277
//Lets call the swagger url so we can make sure the response is valid and contains our path in the ow path
278278
val apiToInvoke = s"$swaggerApiUrl"
@@ -282,16 +282,12 @@ abstract class ApiGwCliBasicTests extends BaseApiGwTests {
282282
response.statusCode should be(200)
283283
response
284284
}, 6, Some(2.second))
285-
val jsonReponse = JSON.parseFull(response.asString()).asInstanceOf[Option[Map[String, String]]].get
286-
jsonReponse.get("__ow_path").get should not be ("")
287-
jsonReponse.get("__ow_path").get should include (testRelPathGet)
285+
val jsonResponse = response.body.asString.parseJson.asJsObject
288286

289-
} catch {
290-
case unknown: Throwable => exception = unknown
287+
jsonResponse.fields("__ow_path").toString should include (testRelPathGet)
291288
} finally {
292289
apiDelete(basepathOrApiName = apiName)
293290
}
294-
assert(exception == null)
295291
}
296292

297293
it should "reject an api commands with an invalid path parameter" in {

tests/src/test/scala/whisk/core/cli/test/ApiGwCliTests.scala

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import org.junit.runner.RunWith
2626
import org.scalatest.junit.JUnitRunner
2727

2828
import scala.concurrent.duration.DurationInt
29-
import scala.util.parsing.json.JSON
29+
30+
import spray.json._
3031

3132
/**
3233
* Tests for basic CLI usage. Some of these tests require a deployed backend.
@@ -84,7 +85,6 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
8485
val testUrlOp = "get"
8586
val testApiName = testName + " API Name"
8687
val actionName = testName + "_action"
87-
var exception: Throwable = null
8888
val reqPath = "\\$\\(request.path\\)"
8989

9090
// Create the action for the API. It must be a "web-action" action.
@@ -111,10 +111,10 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
111111
rr.stdout should include(s"${actionName}")
112112
rr.stdout should include regex (""""cors":\s*\{\s*\n\s*"enabled":\s*true""")
113113
rr.stdout should include regex (s"""target-url.*${actionName}.http${reqPath}""")
114-
val params = getParametersFromJson(rr, testRelPath)
114+
val params = getParametersFromJson(rr.stdout.parseJson.asJsObject, testRelPath)
115115
params.size should be(2)
116-
validateParameter(params(0), "with", "path", true, "string", "Default description for 'with'")
117-
validateParameter(params(1), "path", "path", true, "string", "Default description for 'path'")
116+
validateParameter(params(0).asJsObject, "with", "path", true, "string", "Default description for 'with'")
117+
validateParameter(params(1).asJsObject, "path", "path", true, "string", "Default description for 'path'")
118118

119119
//Lets call the swagger url so we can make sure the response is valid and contains our path in the ow path
120120
val apiToInvoke = s"$swaggerApiUrl"
@@ -124,15 +124,12 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
124124
response.statusCode should be(200)
125125
response
126126
}, 6, Some(2.second))
127-
val jsonReponse = JSON.parseFull(response.asString()).asInstanceOf[Option[Map[String, String]]].get
128-
jsonReponse.get("__ow_path").get should not be ("")
129-
jsonReponse.get("__ow_path").get should include (testRelPathGet)
130-
} catch {
131-
case unknown: Throwable => exception = unknown
127+
val jsonResponse = response.body.asString.parseJson.asJsObject
128+
129+
jsonResponse.fields("__ow_path").toString should include (testRelPathGet)
132130
} finally {
133131
apiDelete(basepathOrApiName = testBasePath)
134132
}
135-
assert(exception == null)
136133
}
137134

138135
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 {
148145
val testUrlOp = "get"
149146
val testApiName = testName + " API Name"
150147
val actionName = testName + "_action"
151-
var exception: Throwable = null
152148
val reqPath = "\\$\\(request.path\\)"
153149
// Create the action for the API. It must be a "web-action" action.
154150
val file = TestUtils.getTestActionFilename(s"echo-web-http.js")
@@ -175,14 +171,14 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
175171
rr.stdout should include(s"${actionName}")
176172
rr.stdout should include regex (""""cors":\s*\{\s*\n\s*"enabled":\s*true""")
177173
rr.stdout should include regex (s"""target-url.*${actionName}.http${reqPath}""")
178-
val params = getParametersFromJson(rr, testRelPath)
174+
val params = getParametersFromJson(rr.stdout.parseJson.asJsObject, testRelPath)
179175

180176
// should have 4, not 5 parameter definitions (i.e. don't define "extra" twice
181177
params.size should be(4)
182-
validateParameter(params(0), "with", "path", true, "string", "Default description for 'with'")
183-
validateParameter(params(1), "double", "path", true, "string", "Default description for 'double'")
184-
validateParameter(params(2), "extra", "path", true, "string", "Default description for 'extra'")
185-
validateParameter(params(3), "path", "path", true, "string", "Default description for 'path'")
178+
validateParameter(params(0).asJsObject, "with", "path", true, "string", "Default description for 'with'")
179+
validateParameter(params(1).asJsObject, "double", "path", true, "string", "Default description for 'double'")
180+
validateParameter(params(2).asJsObject, "extra", "path", true, "string", "Default description for 'extra'")
181+
validateParameter(params(3).asJsObject, "path", "path", true, "string", "Default description for 'path'")
186182

187183
//Lets call the swagger url so we can make sure the response is valid and contains our path in the ow path
188184
val apiToInvoke = s"$swaggerApiUrl"
@@ -192,14 +188,11 @@ class ApiGwCliTests extends ApiGwCliBasicTests {
192188
response.statusCode should be(200)
193189
response
194190
}, 6, Some(2.second))
195-
val jsonReponse = JSON.parseFull(response.asString()).asInstanceOf[Option[Map[String, String]]].get
196-
jsonReponse.get("__ow_path").get should not be ("")
197-
jsonReponse.get("__ow_path").get should include (testRelPathGet)
198-
} catch {
199-
case unknown: Throwable => exception = unknown
191+
val jsonResponse = response.body.asString.parseJson.asJsObject
192+
193+
jsonResponse.fields("__ow_path").toString should include (testRelPathGet)
200194
} finally {
201195
apiDelete(basepathOrApiName = testBasePath)
202196
}
203-
assert(exception == null)
204197
}
205198
}

0 commit comments

Comments
 (0)