Skip to content

Commit fb26f52

Browse files
committed
Redesigning API and ServerTests for instances/{id}/matchingResult and formatting codes
1 parent 9c89675 commit fb26f52

File tree

2 files changed

+31
-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

+31
-32
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@ import de.upb.cs.swt.delphi.instanceregistry.authorization.AccessTokenEnums.User
1111
import de.upb.cs.swt.delphi.instanceregistry.authorization.{AccessToken, AuthProvider}
1212
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType
1313
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.{EventJsonSupport, Instance, InstanceJsonSupport, InstanceLinkJsonSupport}
14+
import de.upb.cs.swt.delphi.instanceregistry.requestLimiter.{IpLogActor, RequestLimitScheduler}
1415
import de.upb.cs.swt.delphi.instanceregistry.{AppLogging, Registry, RequestHandler}
1516
import spray.json.JsonParser.ParsingException
1617
import spray.json._
1718

18-
import scala.concurrent.{ExecutionContext, Future}
19+
import scala.concurrent.ExecutionContext
1920
import scala.util.{Failure, Success}
20-
import de.upb.cs.swt.delphi.instanceregistry.requestLimiter.{IpLogActor, RequestLimitScheduler}
21-
import sun.font.Decoration.Label
22-
23-
import scala.collection.immutable.Range
2421

2522
/**
2623
* Web server configuration for Instance Registry API.
@@ -75,7 +72,9 @@ class Server(handler: RequestHandler) extends HttpApp
7572
matchingInstance(Id)
7673
} ~
7774
path("matchingResult") {
78-
entity(as[JsValue]) { json => matchInstance(Id, json.asJsObject.fields("MatchingSuccessful").asInstanceOf[Boolean], json.asJsObject.fields("SenderId").asInstanceOf[Long]) }
75+
entity(as[JsValue]) {
76+
json => matchInstance(Id, json.asJsObject.fields("MatchingSuccessful").toString(), json.asJsObject.fields("SenderId").toString())
77+
}
7978
} ~
8079
path("eventList") {
8180
eventList(Id)
@@ -341,10 +340,13 @@ class Server(handler: RequestHandler) extends HttpApp
341340
*
342341
* @return Server route that either maps to 200 OK or to the respective error codes
343342
*/
344-
def matchInstance(callerId: Long, matchingResult: Boolean, matchedInstanceId: Long): server.Route = {
343+
def matchInstance(callerId: Long, matchingResultStr: String, matchedInstanceIdStr: String): server.Route = {
345344
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Component)) { token =>
345+
346+
val matchingResult: Boolean = matchingResultStr.toBoolean
347+
val matchedInstanceId: Long = matchedInstanceIdStr.toLong
346348
post {
347-
log.debug(s"POST /matchingResult?callerId=$callerId&matchedInstanceId=$matchedInstanceId&MatchingSuccessful=$matchingResult has been called")
349+
log.debug(s"POST /instances/$callerId/matchingResult has been called with parameters : matchedInstanceId=$matchedInstanceId&MatchingSuccessful=$matchingResult")
348350

349351
handler.handleMatchingResult(callerId, matchedInstanceId, matchingResult) match {
350352
case handler.OperationResult.IdUnknown =>
@@ -395,9 +397,9 @@ class Server(handler: RequestHandler) extends HttpApp
395397
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.Admin)) { token =>
396398
post {
397399
if (name.isEmpty) {
398-
log.debug(s"POST /deploy?ComponentType=$compTypeString has been called")
400+
log.debug(s"POST /instances/deploy?ComponentType=$compTypeString has been called")
399401
} else {
400-
log.debug(s"POST /deploy?ComponentType=$compTypeString&name=${name.get} has been called")
402+
log.debug(s"POST /instances/deploy?ComponentType=$compTypeString&name=${name.get} has been called")
401403
}
402404
val compType: ComponentType = ComponentType.values.find(v => v.toString == compTypeString).orNull
403405

@@ -832,7 +834,7 @@ class Server(handler: RequestHandler) extends HttpApp
832834
def network(): server.Route = {
833835
authenticateOAuth2[AccessToken]("Secure Site", AuthProvider.authenticateOAuthRequire(_, userType = UserType.User)) { token =>
834836
get {
835-
log.debug(s"GET /network has been called.")
837+
log.debug(s"GET /instances/network has been called.")
836838
complete {
837839
handler.handleGetNetwork().toJson
838840
}

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -392,52 +392,49 @@ class ServerTest
392392
val id2 = assertValidRegister(ComponentType.WebApi)
393393

394394

395-
/* Post(s"/instances/$id1/matchingResult", HttpEntity(ContentTypes.`application/json`, s"""{ "MatchingSuccessful": "true", "SenderId" : $id2}""")) ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
396-
assert(status === StatusCodes.OK)
397-
responseAs[String] shouldEqual "Matching result true processed."
398-
}
399-
400-
Post(s"/matchingResult?CallerId=$id1&MatchedInstanceId=$id2&MatchingSuccessful=1") ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
401-
assert(status === StatusCodes.OK)
402-
responseAs[String] shouldEqual "Matching result true processed."
403-
}
404-
*/
395+
Post(s"/instances/$id1/matchingResult", HttpEntity(ContentTypes.`application/json`, s"""{ "MatchingSuccessful": true, "SenderId" : $id2}""")) ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
396+
assert(status === StatusCodes.OK)
397+
responseAs[String] shouldEqual "Matching result true processed."
398+
}
405399
//Remove Instances
406400
assertValidDeregister(id1)
407401
assertValidDeregister(id2)
408402
}
409403

410-
/*
404+
411405
//Invalid POST /matchingResult
412406
"not process matching result if method or parameters are invalid" in {
413407
//Wrong method
414-
Get("/matchingResult?CallerId=0&MatchedInstanceId=0&MatchingSuccessful=1") ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
408+
409+
Get(s"/instances/0/matchingResult", HttpEntity(ContentTypes.`application/json`, s"""{ "MatchingSuccessful": true, "SenderId" : 0}""")) ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
415410
assert(status === StatusCodes.METHOD_NOT_ALLOWED)
416411
responseAs[String] shouldEqual "HTTP method not allowed, supported methods: POST"
417412
}
418413

414+
419415
//Invalid IDs - expect 404
420-
Post("/matchingResult?CallerId=1&MatchedInstanceId=2&MatchingSuccessful=0") ~> addAuthorization("Component") ~> server.routes ~> check {
416+
Post(s"/instances/1/matchingResult", HttpEntity(ContentTypes.`application/json`, s"""{ "MatchingSuccessful": false, "SenderId" : 2}""")) ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
421417
assert(status === StatusCodes.NOT_FOUND)
422418
}
423419

424-
//Wrong parameters, caller is same as callee - expect bad request
425-
Post("/matchingResult?CallerId=0&MatchedInstanceId=0&MatchingSuccessful=O") ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
426-
assert(status === StatusCodes.BAD_REQUEST)
427-
}
420+
/* //Wrong parameters, caller is same as callee - expect bad request
421+
// Post("/matchingResult?CallerId=0&MatchedInstanceId=0&MatchingSuccessful=O") ~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
422+
Post(s"/instances/1/matchingResult", HttpEntity(ContentTypes.`application/json`, s"""{ "MatchingSuccessful": false, "SenderId" : 1}"""))~> addAuthorization("Component") ~> Route.seal(server.routes) ~> check {
423+
assert(status === StatusCodes.BAD_REQUEST)
424+
}*/
428425

429426
//Wrong user type
430-
Post("/matchingResult?CallerId=1&MatchedInstanceId=2&MatchingSuccessful=0") ~> addAuthorization("User") ~> Route.seal(server.routes) ~> check {
427+
Post(s"/instances/1/matchingResult", HttpEntity(ContentTypes.`application/json`, s"""{ "MatchingSuccessful": false, "SenderId" : 2}""")) ~> addAuthorization("User") ~> Route.seal(server.routes) ~> check {
431428
assert(status === StatusCodes.UNAUTHORIZED)
432429
responseAs[String] shouldEqual "The supplied authentication is invalid"
433430
}
434431

435432
//No authorization
436-
Post("/matchingResult?CallerId=1&MatchedInstanceId=2&MatchingSuccessful=0") ~> Route.seal(server.routes) ~> check {
433+
Post(s"/instances/1/matchingResult", HttpEntity(ContentTypes.`application/json`, s"""{ "MatchingSuccessful": false, "SenderId" : 2}""")) ~> Route.seal(server.routes) ~> check {
437434
assert(status === StatusCodes.UNAUTHORIZED)
438-
responseAs[String].toLowerCase should include ("not supplied with the request")
435+
responseAs[String].toLowerCase should include("not supplied with the request")
439436
}
440-
}*/
437+
}
441438

442439
//Valid GET /eventList
443440
"returns registry events that are associated to the instance if id is valid" in {

0 commit comments

Comments
 (0)