In Scala I have a call to service in controller which is returning me Future[WSResponse]. I want to make sure service is returning valid result so send Ok(..) otherwise send BadRequest(...). I don't think I can use map. Any other suggestion?
def someWork = Action.async(parse.xml) { request =>
val result:Future[WSResponse] = someService.processData(request.body.toString())
//Need to send back Ok or BadRequest Message
}
EDIT
Solution from #alextsc is working fine. Now moving to test my existing test is failing. It is getting 400 instead of 200.
test("should post something") {
val requestBody = <value>{UUID.randomUUID}</value>
val mockResponse = mock[WSResponse]
val expectedResponse: Future[WSResponse] = Future.successful(mockResponse)
val request = FakeRequest(Helpers.POST, "/posthere").withXmlBody(requestBody)
when(mockResponse.body).thenReturn("SOME_RESPONSE")
when(someService.processData(any[String])).thenReturn(expectedResponse)
val response: Future[Result] = call(controller.someWork , request)
whenReady(response) { response =>
assert(response.header.status == 200)
}
}
You're on the right track and yes, you can use map.
Since you're using Action.async already and your service returns a future as it stands all you need to do is map that future to a Future[Result] so Play can handle it:
def someWork = Action.async(parse.xml) { request =>
someService.processData(request.body.toString()).map {
// Assuming status 200 (OK) is a valid result for you.
case resp : WSResponse if resp.getStatus == 200 => Ok(...)
case _ => BadRequest(...)
}
}
(I note that your service returns WSResponse (from the play ws java library) and not play.api.libs.ws.Response (the scala version of it), hence getStatus and not just status)
Related
I am using Akka HTTP cache to cache my result. But i am facing issue to test it.
class GoogleAnalyticsController #Inject()(cache: Cache[String, HttpResponse],
googleAnalyticsApi: GoogleAnalyticsTrait,
googleAnalyticsHelper: GoogleAnalyticsHelper)
(implicit system: ActorSystem, materializer: ActorMaterializer) {
def routes: Route =
post {
pathPrefix("pageviews") {
path("clients" / Segment) { accountsClientId =>
entity(as[GoogleAnalyticsMetricsRequest]) { googleAnalyticsMetricsRequest =>
val googleAnalyticsMetricsKey = "key"
complete(
cache.getOrLoad(googleAnalyticsMetricsKey, _ => getGoogleAnalyticsMetricsData(accountsClientId, googleAnalyticsMetricsRequest))
)
}
}
}
}
private def getGoogleAnalyticsMetricsData(accountsClientId: String,
request: GoogleAnalyticsMetricsRequest) = {
val payload = generate(request)
val response = googleAnalyticsApi.googleAnalyticsMetricResponseHandler(accountsClientId, payload) // response from another microservice
googleAnalyticsHelper.googleAnalyticsMetricResponseHandler(
googleAnalyticsMetricsRequest.metricName, response)
}
}
class GoogleAnalyticsHelper extends LoggingHelper {
def googleAnalyticsMetricResponseHandler(metricName: String, response: Either[Throwable, Long]): Future[HttpResponse] =
response.fold({ error =>
logger.error(s"An exception has occurred while getting $metricName from behavior service and error is ${error.getMessage}")
Marshal(FailureResponse(error.getMessage)).to[HttpResponse].map(httpResponse => httpResponse.copy(status = StatusCodes.InternalServerError))
}, value =>
Marshal(MetricResponse(metricName, value)).to[HttpResponse].map(httpResponse => httpResponse.copy(status = StatusCodes.OK))
)
}
Test case: Sharing only the relevant part
"get success metric response for " + pageviews + " metric of given accounts client id" in { fixture =>
import fixture._
val metricResponse = MetricResponse(pageviews, 1)
val eventualHttpResponse = Marshal(metricResponse).to[HttpResponse].map(httpResponse => httpResponse.copy(status = StatusCodes.OK))
when(cache.getOrLoad(anyString, any[String => Future[HttpResponse]].apply)).thenReturn(eventualHttpResponse)
when(googleAnalyticsApi.getDataFromGoogleAnalytics(accountsClientId, generate(GoogleAnalyticsRequest(startDate, endDate, pageviews))))
.thenReturn(ApiResult[Long](Some("1"), None))
when(googleAnalyticsHelper.googleAnalyticsMetricResponseHandler(pageviews, Right(1))).thenReturn(eventualHttpResponse)
Post(s"/pageviews/clients/$accountsClientId").withEntity(requestEntity) ~>
googleAnalyticsController.routes ~> check {
status shouldEqual StatusCodes.OK
responseAs[String] shouldEqual generate(metricResponse)
}
}
By doing this, I am best to test if the cache has the key but not able to test if cache misses the hit. In code coverage, it misses following highlighted part
cache.getOrLoad(googleAnalyticsMetricsKey, _ =>
getGoogleAnalyticsMetricsData(accountsClientId,
googleAnalyticsMetricsRequest))
If there is a design issue, please feel free to guide me on how can I make my design testable.
Thanks in advance.
I think you don't need to mock the cache. You should create an actual object for cache instead of mocked one.
What you have done is, you have mocked the cache, in this case, the highlighted part will be not called as you are providing the mocked value. In the following stubbing, whenever cache.getOrLoad is found, eventualHttpResponse is returned:
when(cache.getOrLoad(anyString, any[String => Future[HttpResponse]].apply)).thenReturn(eventualHttpResponse)
and hence the function getGoogleAnalyticsMetricsData(accountsClientId, googleAnalyticsMetricsRequest) is never called.
In Services, i m calling the endpoint service and got response as Future[] Result. I need to send the Future[] Result to Controllers. In Controllers i should respond the result via Ok(response).
//In Controllers i have to receive the response from Services and have to
send the success or failure response.
def postLead = Action.async { lead =>
val isSuccess = service.postToFreedom(lead).map{ response =>
Ok(response)
}
}
//In Services,i am hitting the end point service and got the response as Future[]
private def postToFreedom(postXML:Request): Future[WSResponse] = {
val leadXml = FreedomMortgageXML(postXML).toXml
val response = ws.url(serviceEndpoint).withHeaders("Content-Type" -> "application/xml").post(leadXml)
response.map { response =>
response
}
You need to convert your ws.WSResponse into a mvc.Result:
def postLead = Action.async { lead =>
service.postToFreedom(lead).map{ wsResponse =>
val headers = wsResponse.allHeaders map {
h => (h._1, h._2.head)
}
Result(ResponseHeader(wsResponse.status, headers), Strict(wsResponse.bodyAsBytes, None))
}
}
In this method I would like to see the actual response (result.toJson.toString or StatusCodes.InternalServerError.toString) returned instead of empty string. How can I do that?
def process(msgIn : WebSocketMessageIn, service : ActorRef) : String = {
import model.Registration
import model.RegistrationJsonProtocol._
implicit val timeout = Timeout(10 seconds)
msgIn.method.toUpperCase match {
case "POST" =>
log.debug(s"Handing POST message with body ${msgIn.body}")
val registration = msgIn.body.convertTo[Registration]
val future = (service ? PostRegistrationMessage(registration)).mapTo[Registration]
var response = ""
future onComplete {
case Success(result) =>
response = result.toJson.toString
case Failure(e) =>
log.error(s"Error: ${e.toString}")
response = StatusCodes.InternalServerError.toString
}
response
case "PUT" =>
s"Handing PUT message ${msgIn.body}"
}
}
Here is the code snippet that calls the method and sends the response to websocket
case Message(ws, msg, service) =>
log.debug("url {} received msg '{}'", ws.getResourceDescriptor, msg)
val wsMessageIn = msg.parseJson.convertTo[WebSocketMessageIn]
val response = process(wsMessageIn, service)
ws.send(response);
UPDATE 1: Updated to use Await.result(future, 5000 millis) instead of 'future onComplete { ... }'. Here is code snippet that changed. Works now, but just wondering how we will handle failures.
msgIn.method.toUpperCase match {
case "POST" =>
log.debug(s"Handing POST message with body ${msgIn.body}")
val registration = msgIn.body.convertTo[ADSRegistration]
val future = (service ? PostADSRegistrationMessage(registration)).mapTo[ADSRegistration]
val response = Await.result(future, 5000 millis)
response.toJson.toString
You can use Await.result which is blocking. Something like this:
import scala.concurrent.duration._
val result = Await.result(future, atMost = 10.second)
val response = //result processing
Just the same, you could pass the future back and perform the send in the onComplete which would be much more reactive
Looking to achieve this:
HTTP request to REST API -> parse -> make async call to another API -> respond to http req with the result of the async call.
Currently, the code looks like:
def getItems(param: String): LiftResponse = {
#volatile var resp: LiftResponse = new BadResponse
param.toLowerCase match {
case "something" =>
val req = Async call returning a Future
req onSuccess {
case items =>
resp = new JsonResponse(items map (decompose(_)), S.getResponseHeaders(Nil), S.responseCookies, 200)
}
req onFailure {
case fail => resp = new BadResponse
}
resp
case _ => new OkResponse
}
}
But it looks like poor implementation.
What is the idiomatic Scala way to write the above?
Your code will probably not do what you think it should since it depends on scheduling whether it returns null or something else. Is LiftResponse a strict value or can it be deferred? If strict then you will have to return a Future[LiftResponse] obtained by mapping your req: Future.
Look into using Lift's RestHelper in conjunction with RestContinuation.async. It supports using continuations to suspend a request until data is available for it. There's some sample code at http://demo.liftweb.net/async_rest . After the async continuation is invoked and before the reply function is called with the result, the request thread will be released to the thread pool. Once the reply function is called, the request will be put back on a thread and the response sent to the client.
I think you can try to inline #volatile var resp by:
def getItems(param: String): LiftResponse = {
param.toLowerCase match {
case "something" =>
val req = Async call returning a Future
req.onComplete {
case Success(items) => new JsonResponse(items map (decompose(_)), S.getResponseHeaders(Nil), S.responseCookies, 200)
case Failure(t) => new BadResponse
}
case _ => new OkResponse
}
}
--edit--
sorry, onComplete returns Unit, how about using Await to get the result of future:
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._
def getItems(param: String): LiftResponse = {
param.toLowerCase match {
case "something" =>
val req = Async call returning a Future
val response = req map { items =>
new JsonResponse
} recover {
case t:Throwable => new BadResponse
}
Await.result(response, Duration(100, MILLISECONDS))
case _ => new OkResponse
}
}
Note (3 years later): with the recent (Nov. 2016) release of Lift3, you can use net.liftweb.http._, as described in "Request and Session Access in Lift Futures", from Piotr Dyraga.
As an example, say that you want to lazily render a list of users.
First, you execute database query asynchronously and get Future[Seq[User]] as a result of this operation.
If you use Lift 2, incorporate FutureBinds that I described in one my previous posts or if you use Lift3, import net.liftweb.http._ and do:
val allUsers: Future[Seq[User]] = ... // retrieve users from DB
".user-list-async-container" #> allUsers.map { users =>
".user-list" #> users.map { user =>
".user" #> userBindings(user)
}
}
I am trying to make a post request to Pusher api, but I am having trouble returning the right type, I a type mismatch; found : scala.concurrent.Future[play.api.libs.ws.Response] required: play.api.libs.ws.Response
def trigger(channel:String, event:String, message:String): ws.Response = {
val domain = "api.pusherapp.com"
val url = "/apps/"+appId+"/channels/"+channel+"/events";
val body = message
val params = List(
("auth_key", key),
("auth_timestamp", (new Date().getTime()/1000) toInt ),
("auth_version", "1.0"),
("name", event),
("body_md5", md5(body))
).sortWith((a,b) => a._1 < b._1 ).map( o => o._1+"="+URLEncoder.encode(o._2.toString)).mkString("&");
val signature = sha256(List("POST", url, params).mkString("\n"), secret.get);
val signatureEncoded = URLEncoder.encode(signature, "UTF-8");
implicit val timeout = Timeout(5 seconds)
WS.url("http://"+domain+url+"?"+params+"&auth_signature="+signatureEncoded).post(body
}
The request you are making with post is asynchronous. That call returns immediately, but does not return a Response object. Instead, it returns a Future[Response] object, which will contain the Response object once the http request is completed asynchronously.
If you want to block execution until the request is completed, do:
val f = Ws.url(...).post(...)
Await.result(f)
See more about futures here.
Just append a map:
WS.url("http://"+domain+url+"?"+params+"&auth_signature="+signatureEncoded).post(body).map(_)
Assuming you don't want to create a blocking app, your method should also return a Future[ws.Response]. Let your futures bubble up to the Controller where you return an AsyncResult using Async { ... } and let Play handle the rest.
Controller
def webServiceResult = Action { implicit request =>
Async {
// ... your logic
trigger(channel, event, message).map { response =>
// Do something with the response, e.g. convert to Json
}
}
}