Skip to content

Commit c315cc8

Browse files
committed
Using JsValue logic and redesigning ServerTests for instances/{id}/label
1 parent ae1d6fb commit c315cc8

File tree

2 files changed

+17
-32
lines changed
  • src
    • main/scala/de/upb/cs/swt/delphi/instanceregistry/connection
    • test/scala/de/upb/cs/swt/delphi/instanceregistry/connection

2 files changed

+17
-32
lines changed

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/connection/Server.scala

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Server(handler: RequestHandler) extends HttpApp
109109
entity(as[String]) { jsonString => assignInstance(Id, jsonString) }
110110
} ~
111111
path("label") {
112-
entity(as[String]) { jsonString => addLabel(Id, jsonString) }
112+
entity(as[JsValue]) { json => addLabel(Id, json.asJsObject.fields("Label").toString()) }
113113
}
114114
}
115115
} ~
@@ -854,11 +854,10 @@ class Server(handler: RequestHandler) extends HttpApp
854854
*
855855
* @return Server route that either maps to 200 OK or the respective error codes.
856856
*/
857-
def addLabel(id: Long, addLabelStr: String): server.Route = {
857+
def addLabel(id: Long, label: String): server.Route = {
858858
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Admin)) { token =>
859859

860-
try {
861-
val label: String = addLabelStr.parseJson.convertTo[String]
860+
862861
post {
863862
log.debug(s"POST /instances/$id/label with parameter label=$label has been called.")
864863
handler.handleAddLabel(id, label) match {
@@ -878,18 +877,6 @@ class Server(handler: RequestHandler) extends HttpApp
878877
complete("Successfully added label")
879878
}
880879
}
881-
}
882-
catch {
883-
case dx: DeserializationException =>
884-
log.error(dx, "Deserialization exception")
885-
complete(HttpResponse(StatusCodes.BadRequest, entity = s"Could not deserialize parameter instance with message ${dx.getMessage}."))
886-
case px: ParsingException =>
887-
log.error(px, "Failed to parse JSON while adding label")
888-
complete(HttpResponse(StatusCodes.BadRequest, entity = s"Failed to parse JSON entity with message ${px.getMessage}"))
889-
case x: Exception =>
890-
log.error(x, "Uncaught exception while deserializing.")
891-
complete(HttpResponse(StatusCodes.InternalServerError, entity = "An internal server error occurred."))
892-
}
893880
}
894881
}
895882

src/test/scala/de/upb/cs/swt/delphi/instanceregistry/connection/ServerTest.scala

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -602,47 +602,45 @@ class ServerTest
602602
}
603603
}
604604

605-
//Valid POST /addLabel
606-
/* "add a generic label to an instance is label and id are valid" in {
607-
Post("/addLabel?Id=0&Label=ElasticSearchDefaultLabel") ~> addAuthorization("Admin") ~> server.routes ~> check {
605+
//Valid POST /instances/{id}/label
606+
"add a generic label to an instance is label and id are valid" in {
607+
Post("/instances/0/label", HttpEntity(ContentTypes.`application/json`, """{ "Label": "Private"}""")) ~> addAuthorization("Admin") ~> server.routes ~> check {
608608
assert(status === StatusCodes.OK)
609609
responseAs[String] shouldEqual "Successfully added label"
610610
}
611611
}
612-
613-
//Invalid POST /addLabel
614-
"fail to add label if id is invalid or label too long" in{
612+
//Invalid POST /instances/{id}/label
613+
"fail to add label if id is invalid or label too long" in {
615614
//Unknown id - expect 404
616-
Post("/addLabel?Id=45&Label=Private") ~> addAuthorization("Admin") ~> server.routes ~> check {
615+
Post("/instances/45/label",HttpEntity(ContentTypes.`application/json`, """{ "Label": "Private"}""")) ~> addAuthorization("Admin") ~> server.routes ~> check {
617616
assert(status === StatusCodes.NOT_FOUND)
618617
responseAs[String] shouldEqual "Cannot add label, id 45 not found."
619618
}
620-
621-
val tooLongLabel = "VeryVeryExtraLongLabelThatDoesNotWorkWhileAddingLabel"
622619
//Label out of bounds - expect 400
623-
Post(s"/addLabel?Id=0&Label=$tooLongLabel") ~> addAuthorization("Admin") ~> server.routes ~> check {
620+
val tooLongLabel = "VeryVeryExtraLongLabelThatDoesNotWorkWhileAddingLabel"
621+
val jsonStr = tooLongLabel.toJson
622+
Post("/instances/0/label",HttpEntity(ContentTypes.`application/json`, s"""{ "Label": $jsonStr}""")) ~> addAuthorization("Admin") ~> server.routes ~> check {
624623
assert(status === StatusCodes.BAD_REQUEST)
625624
responseAs[String].toLowerCase should include ("exceeds character limit")
626625
}
627-
628626
//Wrong user type
629-
Post("/addLabel?Id=0&Label=Private") ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
627+
Post("/instances/0/label",HttpEntity(ContentTypes.`application/json`, """{ "Label": "Private"}""")) ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
630628
assert(status === StatusCodes.UNAUTHORIZED)
631629
responseAs[String] shouldEqual "The supplied authentication is invalid"
632630
}
633631

634632
//Wrong user type
635-
Post("/addLabel?Id=0&Label=Private") ~> addAuthorization("User") ~> Route.seal(server.routes) ~> check {
633+
Post("/instances/0/label",HttpEntity(ContentTypes.`application/json`, """{ "Label": "Private"}""")) ~> addAuthorization("User") ~> Route.seal(server.routes) ~> check {
636634
assert(status === StatusCodes.UNAUTHORIZED)
637635
responseAs[String] shouldEqual "The supplied authentication is invalid"
638636
}
639-
640637
//No authorization
641-
Post("/addLabel?Id=0&Label=Private") ~> Route.seal(server.routes) ~> check {
638+
Post("/instances/0/label",HttpEntity(ContentTypes.`application/json`, """{ "Label": "Private"}"""))~> Route.seal(server.routes) ~> check {
642639
assert(status === StatusCodes.UNAUTHORIZED)
643640
responseAs[String].toLowerCase should include ("not supplied with the request")
644641
}
645-
} */
642+
643+
}
646644

647645
/**Minimal tests for docker operations**/
648646

0 commit comments

Comments
 (0)