play.api.libs.ws.WSResponse Scala Examples

The following examples show how to use play.api.libs.ws.WSResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: AthanorClient.scala    From tap   with Apache License 2.0 5 votes vote down vote up
package controllers.external.athanor

import io.heta.tap.data.results.StringListResult
import javax.inject.Inject
import io.heta.tap.util.AppConfig
import play.api.Logger
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._ // scalastyle:ignore




class AthanorClient @Inject()(wsClient: WSClient, config: AppConfig)(implicit ec: ExecutionContext) {

  val logger: Logger = Logger(this.getClass)

  val athanorURL= config.getAthanorURL

  def process(text:String,parameter:String,start:Long):Future[StringListResult] = {
    //logger.info(s"Analysing with athanor: $text")

    val url = athanorURL + parameter
    logger.info(s"Analysing with athanor at this url: $url")

    val request: WSRequest = wsClient.url(url)

    val athanorRequest: WSRequest = request
      .withHttpHeaders("Accept" -> "application/json")
      .withRequestTimeout(30000.millis)

    val futureResponse: Future[WSResponse] = athanorRequest.post(text)


    val result: Future[StringListResult] = {
      val decoded = futureResponse.map { response =>
        val res = decodeRepsonse(response)
        val queryTime = (System.currentTimeMillis() - start).toInt
        StringListResult(res,"ok",querytime = queryTime)
      }
      val errMsg = "There was a problem connecting to the Athanor server."
      futureResponse.recover {
        case e: Any => {
          val msg = s"$errMsg: $e"
          logger.error(msg)
          StringListResult(Vector(),msg)
        }
        //case _ => logger.error(errMsg)
      }
      decoded
    }
    result
  }

  case class AthanorMsg(message: String, results: Vector[Vector[String]])

  def decodeRepsonse(response:WSResponse): Vector[Vector[String]] = {
    val resBody = response.body
    if(resBody.nonEmpty && resBody.contains(":[[")) {
      logger.debug("Decoding response: "+ resBody)

      import play.api.libs.functional.syntax._ //scalastyle:ignore
      import play.api.libs.json._ //scalastyle:ignore

      implicit val AMWrites: Writes[AthanorMsg] = (
        (JsPath \ "message").write[String] and
          (JsPath \ "results").write[Vector[Vector[String]]]
        ) (unlift(AthanorMsg.unapply))

      implicit val AMReads: Reads[AthanorMsg] = (
        (JsPath \ "message").read[String] and
          (JsPath \ "results").read[Vector[Vector[String]]]
        ) (AthanorMsg.apply _)

      val athanorMsg:AthanorMsg = response.json.as[AthanorMsg]
      logger.debug("Athanor message: " + athanorMsg.message)
      logger.debug("Athanor results: " + athanorMsg.results)
      athanorMsg.results
    } else {
      logger.error("There was a problem: " + resBody) //TODO If we get to here, we need to return the message to the client!!
      Vector()
    }
  }
} 
Example 2
Source File: CommentTestHelper.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package articles.test_helpers

import articles.models._
import commons_test.test_helpers.WsScalaTestClientWithHost.TestWsClient
import commons_test.test_helpers.{ResponseTransformer, WsScalaTestClientWithHost}
import play.api.http.HeaderNames
import play.api.libs.json.Json
import play.api.libs.ws.WSResponse

import scala.concurrent.{ExecutionContext, Future}

class CommentTestHelper(executionContext: ExecutionContext) extends WsScalaTestClientWithHost {

  implicit private val ec: ExecutionContext = executionContext

  def delete(comment: CommentWithAuthor, article: ArticleWithTags, token: String)
            (implicit testWsClient: TestWsClient): Future[WSResponse] = {
    wsUrl(s"/articles/${article.slug}/comments/${comment.id.value}")
      .addHttpHeaders(HeaderNames.AUTHORIZATION -> s"Token $token")
      .delete()
  }

  def list[ReturnType](article: ArticleWithTags)
                      (implicit testWsClient: TestWsClient,
                       responseTransformer: ResponseTransformer[ReturnType]): Future[ReturnType] = {
    wsUrl(s"/articles/${article.slug}/comments")
      .get()
      .map(responseTransformer(_))
  }

  def create[ReturnType](article: ArticleWithTags, newComment: NewComment, token: String)
                        (implicit testWsClient: TestWsClient,
                         responseTransformer: ResponseTransformer[ReturnType]): Future[ReturnType] = {
    wsUrl(s"/articles/${article.slug}/comments")
      .addHttpHeaders(HeaderNames.AUTHORIZATION -> s"Token $token")
      .post(Json.toJson(NewCommentWrapper(newComment)))
      .map(responseTransformer(_))
  }

} 
Example 3
Source File: ArticleTestHelper.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package articles.test_helpers

import articles.models._
import commons_test.test_helpers.WsScalaTestClientWithHost.TestWsClient
import commons_test.test_helpers.{ResponseTransformer, WsScalaTestClientWithHost}
import play.api.http.HeaderNames
import play.api.libs.json.{JsObject, Json}
import play.api.libs.ws.{EmptyBody, WSResponse}

import scala.concurrent.{ExecutionContext, Future}

class ArticleTestHelper(executionContext: ExecutionContext) extends WsScalaTestClientWithHost {

  def update(article: ArticleWithTags, articleUpdate: ArticleUpdate, token: String)
            (implicit testWsClient: TestWsClient): Future[WSResponse] = {

    wsUrl(s"/articles/${article.slug}")
      .addHttpHeaders(HeaderNames.AUTHORIZATION -> s"Token $token")
      .put(JsObject(Map("article" -> Json.toJson(articleUpdate))))
  }


  implicit private val ex: ExecutionContext = executionContext

  private def getQueryParams(articlesPageRequest: ArticlesPageRequest) = {
    import articlesPageRequest._

    val requiredParams = Seq("limit" -> limit.toString, "offset" -> offset.toString)
    val maybeFilterParam = articlesPageRequest match {
      case pg: ArticlesByTag =>
        Some("tag" -> pg.tag)
      case pg: ArticlesByAuthor =>
        Some("author" -> pg.author.value)
      case pg: ArticlesByFavorited =>
        Some("favorited " -> pg.favoritedBy.value)
      case _: ArticlesAll =>
        None
    }

    maybeFilterParam.map(requiredParams.prepended)
      .getOrElse(requiredParams)
  }

  def findAll[ReturnType](mainFeedPageRequest: ArticlesPageRequest, maybeToken: Option[String] = None)
                         (implicit testWsClient: TestWsClient,
                          responseTransformer: ResponseTransformer[ReturnType]): Future[ReturnType] = {
    val queryParams = getQueryParams(mainFeedPageRequest)
    val request = wsUrl( s"/articles").addQueryStringParameters(queryParams: _*)

    maybeToken
      .map(token => request.addHttpHeaders(HeaderNames.AUTHORIZATION -> s"Token $token"))
      .getOrElse(request)
      .get()
      .map(responseTransformer(_))
  }

  def favorite[ReturnType](slug: String, token: String)
                          (implicit testWsClient: TestWsClient,
                           responseTransformer: ResponseTransformer[ReturnType]): Future[ReturnType] = {
    wsUrl(s"/articles/$slug/favorite")
      .addHttpHeaders(HeaderNames.AUTHORIZATION -> s"Token $token")
      .post(EmptyBody)
      .map(responseTransformer(_))
  }


  def getBySlug[ReturnType](slug: String)(implicit testWsClient: TestWsClient,
                                          responseTransformer: ResponseTransformer[ReturnType]): Future[ReturnType] = {
    wsUrl(s"/articles/$slug")
      .get()
      .map(responseTransformer(_))
  }


  def delete(articleWithTags: ArticleWithTags, token: String)(implicit testWsClient: TestWsClient): Future[WSResponse] = {
    wsUrl(s"/articles/${articleWithTags.slug}")
      .addHttpHeaders(HeaderNames.AUTHORIZATION -> s"Token $token")
      .delete()
  }

  def create[ReturnType](newArticle: NewArticle, token: String)
                        (implicit testWsClient: TestWsClient,
                         responseTransformer: ResponseTransformer[ReturnType]): Future[ReturnType] = {
    val body = Json.toJson(NewArticleWrapper(newArticle))
    wsUrl("/articles")
      .addHttpHeaders(HeaderNames.AUTHORIZATION -> s"Token $token")
      .post(body)
      .map(responseTransformer(_))
  }

} 
Example 4
Source File: CommentCreateTest.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package articles

import articles.models._
import articles.test_helpers.{Articles, Comments}
import commons_test.test_helpers.{RealWorldWithServerAndTestConfigBaseTest, WithArticleTestHelper, WithUserTestHelper}
import play.api.libs.ws.WSResponse
import users.models.UserDetailsWithToken
import users.test_helpers.UserRegistrations

class CommentCreateTest extends RealWorldWithServerAndTestConfigBaseTest with WithArticleTestHelper with WithUserTestHelper {

  "Create comment" should "create comment for authenticated user" in await {
    val newArticle = Articles.hotToTrainYourDragon
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      persistedArticle <- articleTestHelper.create[ArticleWithTags](newArticle, userDetailsWithToken.token)
      newComment = Comments.yummy

      response <- commentTestHelper.create[WSResponse](persistedArticle, newComment, userDetailsWithToken.token)
    } yield {
      response.status.mustBe(OK)
      val comment = response.json.as[CommentWrapper].comment
      comment.author.username.mustBe(userDetailsWithToken.username)
      comment.body.mustBe(newComment.body)
    }
  }

} 
Example 5
Source File: CommentListTest.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package articles

import articles.models.{ArticleWithTags, CommentList, CommentWithAuthor}
import articles.test_helpers.{Articles, Comments}
import commons_test.test_helpers.{RealWorldWithServerAndTestConfigBaseTest, WithArticleTestHelper, WithUserTestHelper}
import play.api.libs.ws.WSResponse
import users.models.UserDetailsWithToken
import users.test_helpers.UserRegistrations

class CommentListTest extends RealWorldWithServerAndTestConfigBaseTest with WithArticleTestHelper with WithUserTestHelper {

  "Comment list" should "return empty array if article does not have any comments" in await {
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      articleWithTags <- articleTestHelper.create[ArticleWithTags](Articles.hotToTrainYourDragon, userDetailsWithToken.token)

      response <- commentTestHelper.list[WSResponse](articleWithTags)
    } yield {
      response.status.mustBe(OK)
      val comments = response.json.as[CommentList].comments
      comments.isEmpty.mustBe(true)
    }
  }

  it should "return given article's comment " in await {
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      articleWithTags <- articleTestHelper.create[ArticleWithTags](Articles.hotToTrainYourDragon, userDetailsWithToken.token)
      comment <- commentTestHelper.create[CommentWithAuthor](articleWithTags, Comments.yummy, userDetailsWithToken.token)

      response <- commentTestHelper.list[WSResponse](articleWithTags)
    } yield {
      response.status.mustBe(OK)
      val comments = response.json.as[CommentList].comments
      comments.size.mustBe(1)
      comments.head.id.mustBe(comment.id)
    }
  }

  it should "return two comments, newer a the top" in await {
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      articleWithTags <- articleTestHelper.create[ArticleWithTags](Articles.hotToTrainYourDragon, userDetailsWithToken.token)
      _ <- commentTestHelper.create[CommentWithAuthor](articleWithTags, Comments.yummy, userDetailsWithToken.token)
      newerComment <- commentTestHelper.create[CommentWithAuthor](articleWithTags, Comments.yummy, userDetailsWithToken.token)

      response <- commentTestHelper.list[WSResponse](articleWithTags)
    } yield {
      response.status.mustBe(OK)
      val comments = response.json.as[CommentList].comments
      comments.size.mustBe(2)
      comments.head.id.mustBe(newerComment.id)
    }
  }

} 
Example 6
Source File: UserTestHelper.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package users.test_helpers

import authentication.models.PlainTextPassword
import commons.models.Email
import commons_test.test_helpers.{ResponseTransformer, WsScalaTestClientWithHost}
import commons_test.test_helpers.WsScalaTestClientWithHost.TestWsClient
import play.api.http.HeaderNames
import play.api.libs.json.{JsObject, JsString, JsValue, Json}
import play.api.libs.ws.WSResponse
import users.models._

import scala.concurrent.{ExecutionContext, Future}

class UserTestHelper(executionContext: ExecutionContext) extends WsScalaTestClientWithHost {

  def update(updateUser: UserUpdate, token: String)(implicit testWsClient: TestWsClient): Future[WSResponse] = {
    val registrationRequestBody = Json.toJson(UpdateUserWrapper(updateUser))

    wsUrl(s"/user")
      .addHttpHeaders(HeaderNames.AUTHORIZATION -> s"Token $token")
      .put(registrationRequestBody)
  }

  def login(email: Email, password: PlainTextPassword)(implicit testWsClient: TestWsClient): Future[WSResponse] = {
    val requestBody: JsValue = buildEmailAndPasswordRequestBody(email, password)

    wsUrl("/users/login")
      .post(requestBody)
  }

  private def buildEmailAndPasswordRequestBody(email: Email, password: PlainTextPassword) = {
    val rawEmail = email.value
    val rawPassword = password.value
    val userJsonObj = JsObject(Map("email" -> JsString(rawEmail), "password" -> JsString(rawPassword)))
    JsObject(Map("user" -> userJsonObj))
  }


  implicit private val ex: ExecutionContext = executionContext

  def register[ReturnType](userRegistration: UserRegistration)
              (implicit testWsClient: TestWsClient,
               responseTransformer: ResponseTransformer[ReturnType]): Future[ReturnType] = {
    require(userRegistration != null)

    wsUrl("/users")
      .post(Json.toJson(UserRegistrationWrapper(userRegistration)))
      .map(responseTransformer(_))
  }

} 
Example 7
Source File: UserRegistrationTest.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package users.controllers

import authentication.models.PlainTextPassword
import commons.models.ValidationResultWrapper
import commons.validations.constraints.{EmailAlreadyTakenViolation, MinLengthViolation, UsernameAlreadyTakenViolation}
import commons_test.test_helpers.RealWorldWithServerAndTestConfigBaseTest
import play.api.libs.ws.WSResponse
import users.test_helpers.{UserRegistrations, UserTestHelper}

class UserRegistrationTest extends RealWorldWithServerAndTestConfigBaseTest {

  def userTestHelper: UserTestHelper = new UserTestHelper(executionContext)

  "User registration" should "success when registration data is valid" in await {
    for {
      response <- userTestHelper.register[WSResponse](UserRegistrations.petycjaRegistration)
    } yield {
      response.status.mustBe(OK)
    }
  }

  it should "fail because given password was too short" in await {
    val userRegistration = UserRegistrations.petycjaRegistration.copy(password = PlainTextPassword("short"))
    for {
      response <- userTestHelper.register[WSResponse](userRegistration)
    } yield {
      response.status.mustBe(UNPROCESSABLE_ENTITY)
      val validationResultWrapper = response.json.as[ValidationResultWrapper]
      validationResultWrapper.errors.size.mustBe(1)
      validationResultWrapper.errors("password").head.mustBe(MinLengthViolation(8).message)
    }
  }

  it should "fail because login has already been taken" in await {
    val userRegistration = UserRegistrations.petycjaRegistration
    for {
      _ <- userTestHelper.register[WSResponse](userRegistration)
      response <- userTestHelper.register[WSResponse](userRegistration)
    } yield {
      response.status.mustBe(UNPROCESSABLE_ENTITY)
      val validationResultWrapper = response.json.as[ValidationResultWrapper]
      validationResultWrapper.errors.size.mustBe(>=(1))
      validationResultWrapper.errors("username").head.mustBe(UsernameAlreadyTakenViolation(userRegistration.username).message)
    }
  }

  it should "fail because email has already been taken" in await {
    val userRegistration = UserRegistrations.petycjaRegistration
    for {
      _ <- userTestHelper.register[WSResponse](userRegistration)
      response <- userTestHelper.register[WSResponse](userRegistration)
    } yield {
      response.status.mustBe(UNPROCESSABLE_ENTITY)
      val validationResultWrapper = response.json.as[ValidationResultWrapper]
      validationResultWrapper.errors.size.mustBe(>=(1))
      validationResultWrapper.errors("email").head.mustBe(EmailAlreadyTakenViolation(userRegistration.email).message)
    }
  }

} 
Example 8
Source File: ResponseTransformer.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package commons_test.test_helpers

import play.api.libs.json.Reads
import play.api.libs.ws.WSResponse

trait ResponseTransformer[T] {
  def apply(rawResponse: WSResponse): T
}

object ResponseTransformer {
  implicit val identityResponseTransformer: ResponseTransformer[WSResponse] = IdentityResponseTransformer
}

object FromJsonToModelResponseTransformerFactory {
  def apply[WrapperType : Reads, ResultType](unwrap: WrapperType => ResultType): ResponseTransformer[ResultType] = {
    rawResponse: WSResponse => {
      unwrap(rawResponse.json.as[WrapperType])
    }
  }
}

object IdentityResponseTransformer extends ResponseTransformer[WSResponse] {
  override def apply(rawResponse: WSResponse): WSResponse = {
    rawResponse
  }
} 
Example 9
Source File: TagListTest.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package core.tags

import articles.models.{ArticleWithTags, NewTag, TagListWrapper}
import articles.test_helpers.{Articles, Tags}
import commons_test.test_helpers.{RealWorldWithServerAndTestConfigBaseTest, WithArticleTestHelper, WithUserTestHelper}
import play.api.libs.ws.WSResponse
import users.models.UserDetailsWithToken
import users.test_helpers.UserRegistrations

class TagListTest extends RealWorldWithServerAndTestConfigBaseTest with WithArticleTestHelper with WithUserTestHelper {

  "Tags list" should "return empty array within wrapper when there are not any tags" in await {
    for {
      response <- tagTestHelper.findAll[WSResponse]
    } yield {
      response.status.mustBe(OK)
      response.json.as[TagListWrapper].tags.isEmpty.mustBe(true)
    }
  }

  private def createTag(newTag: NewTag) = {
    for {
      user <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      newArticle = Articles.hotToTrainYourDragon.copy(tagList = Seq(newTag.name))
      article <- articleTestHelper.create[ArticleWithTags](newArticle, user.token)
    } yield article
  }

  it should "return array with one tag within wrapper" in await {
    val newTag = Tags.dragons
    for {
      _ <- createTag(newTag)
      response <- tagTestHelper.findAll[WSResponse]
    } yield {
      response.status.mustBe(OK)
      val tags = response.json.as[TagListWrapper].tags
      tags.isEmpty.mustBe(false)
      tags.head.mustBe(newTag.name)
    }
  }

} 
Example 10
Source File: ArticleFavoriteTest.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package articles

import articles.models.{ArticleWithTags, ArticleWrapper}
import articles.test_helpers.Articles
import commons_test.test_helpers.{RealWorldWithServerAndTestConfigBaseTest, WithArticleTestHelper, WithUserTestHelper}
import play.api.libs.ws.WSResponse
import users.models.UserDetailsWithToken
import users.test_helpers.{UserRegistrations, UserTestHelper}

class ArticleFavoriteTest extends RealWorldWithServerAndTestConfigBaseTest with WithArticleTestHelper with WithUserTestHelper {

  "Favorite article" should "mark article as favorited for current user" in await {
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      newArticle = Articles.hotToTrainYourDragon
      articleWithTags <- articleTestHelper.create[ArticleWithTags](newArticle, userDetailsWithToken.token)
      response <- articleTestHelper.favorite[WSResponse](articleWithTags.slug, userDetailsWithToken.token)
    } yield {
      response.status.mustBe(OK)
      val article = response.json.as[ArticleWrapper].article
      article.title.mustBe(newArticle.title)
      article.favorited.mustBe(true)
      article.favoritesCount.mustBe(1)
    }
  }

} 
Example 11
Source File: WSHttpResponse.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.http.ws

import com.github.ghik.silencer.silent
import play.api.libs.json.JsValue
import play.api.libs.ws.WSResponse
import uk.gov.hmrc.http.HttpResponse

@deprecated("Use WsHttpResponse.apply and HttpResponse instead", "11.0.0")
class WSHttpResponse(wsResponse: WSResponse) extends HttpResponse {

  @silent("deprecated") // allHeaders is required for Play 2.5
  override def allHeaders: Map[String, Seq[String]] = wsResponse.allHeaders

  override def status: Int = wsResponse.status

  override def json: JsValue = wsResponse.json

  override def body: String = wsResponse.body
}

object WSHttpResponse {
  @silent("deprecated") // allHeaders is required for Play 2.5
  def apply(wsResponse: WSResponse): HttpResponse =
    // Note that HttpResponse defines `def json` as `Json.parse(body)` - this may be different from wsResponse.json depending on version.
    // https://github.com/playframework/play-ws/commits/master/play-ws-standalone-json/src/main/scala/play/api/libs/ws/JsonBodyReadables.scala shows that is was redefined
    // to handle an encoding issue, but subsequently reverted.
    HttpResponse(
      status  = wsResponse.status,
      body    = wsResponse.body,
      headers = wsResponse.allHeaders
    )
} 
Example 12
Source File: ResponseMatchers.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.http.test

import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
import org.scalatest.matchers.{HavePropertyMatchResult, HavePropertyMatcher}
import play.api.libs.json._
import play.api.libs.ws.WSResponse

import scala.concurrent.Future

trait ResponseMatchers extends ScalaFutures with IntegrationPatience {

  
  def jsonProperty(path: JsPath) = new HavePropertyMatcher[Future[WSResponse], JsValue] {
    def apply(response: Future[WSResponse]) = HavePropertyMatchResult(
      matches = response.futureValue.json.validate(path.readNullable[JsValue]).get.isDefined,
      propertyName = "Response JSON at path " + path,
      expectedValue = JsString("defined"),
      actualValue = response.futureValue.json.validate(path.readNullable[JsValue]).get.getOrElse(JsNull)
    )
  }
}

object ResponseMatchers extends ResponseMatchers 
Example 13
Source File: DownloadParentPoms.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index
package data
package maven

import java.nio.file.{Files, Path}

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import ch.epfl.scala.index.data.download.PlayWsDownloader
import org.slf4j.LoggerFactory
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}

import scala.util.Failure

class DownloadParentPoms(repository: LocalPomRepository,
                         paths: DataPaths,
                         tmp: Option[Path] = None)(
    implicit val system: ActorSystem,
    implicit val materializer: ActorMaterializer
) extends PlayWsDownloader {

  private val log = LoggerFactory.getLogger(getClass)

  assert(
    repository == LocalPomRepository.MavenCentral || repository == LocalPomRepository.Bintray
  )

  val parentPomsPath = paths.parentPoms(repository)
  val pomReader =
    tmp match {
      case Some(path) => PomsReader.tmp(paths, path)
      case None       => PomsReader(repository, paths)
    }

  
    val parentPomsToDownload: Set[Dependency] =
      pomReader
        .load()
        .collect {
          case Failure(m: MissingParentPom) => m.dep
        }
        .toSet

    log.debug(s"to download: ${parentPomsToDownload.size}")
    log.debug(s"last failed: $lastFailedToDownload")

    if (parentPomsToDownload.size > lastFailedToDownload) {

      val downloaded =
        download[Dependency, Int]("Download parent POMs",
                                  parentPomsToDownload,
                                  downloadRequest,
                                  processResponse,
                                  parallelism = 32)
      val failedDownloads = downloaded.sum

      log.warn(s"failed downloads: $failedDownloads")

      if (0 < failedDownloads && parentPomsToDownload.size != failedDownloads) {

        run(failedDownloads) // grand-parent poms, etc
      }
    }
  }
} 
Example 14
Source File: BintrayDownloadPoms.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index.data
package bintray

import download.PlayWsDownloader
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path}

import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import play.api.libs.ws.ahc.AhcWSClient
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import org.slf4j.LoggerFactory

class BintrayDownloadPoms(paths: DataPaths)(
    implicit val system: ActorSystem,
    implicit val materializer: ActorMaterializer
) extends PlayWsDownloader {

  private val log = LoggerFactory.getLogger(getClass)

  private val bintrayPomBase = paths.poms(LocalPomRepository.Bintray)

  
  def run(): Unit = {

    download[BintraySearch, Unit]("Downloading POMs",
                                  searchesBySha1,
                                  downloadRequest,
                                  processPomDownload,
                                  parallelism = 32)
    ()
  }
} 
Example 15
Source File: BintrayClientTests.scala    From scaladex   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ch.epfl.scala.index.bintray

import akka.stream.scaladsl.Source
import akka.util.ByteString
import ch.epfl.scala.index.data.bintray.BintrayClient
import org.scalatest._
import play.api.libs.json.JsValue
import play.api.libs.ws.{WSCookie, WSResponse}

import scala.xml.Elem

class BintrayClientTests extends FlatSpec with Matchers {
  "BintrayClient" should "calculate pagination properly" in {
    getPagination(startPos = 50, endPos = 59, total = 100) should contain theSameElementsAs List(
      60,
      70,
      80,
      90
    )
    getPagination(startPos = 0, endPos = 49, total = 100) should contain theSameElementsAs List(
      50
    )
    getPagination(startPos = 50, endPos = 99, total = 100) should contain theSameElementsAs Nil
    getPagination(startPos = 0, endPos = 49, total = 50) should contain theSameElementsAs Nil
    getPagination(startPos = 0, endPos = 49, total = 51) should contain theSameElementsAs List(
      50
    )
    getPagination(startPos = 0, endPos = 0, total = 10) should contain theSameElementsAs List(
        1, 2, 3, 4, 5, 6, 7, 8, 9)
  }

  def getPagination(startPos: Int, endPos: Int, total: Int): Seq[Int] = {
    val wsResponse = wsResponseWithHeaders(
      Map("X-RangeLimit-Total" -> Seq(total.toString),
          "X-RangeLimit-StartPos" -> Seq(startPos.toString),
          "X-RangeLimit-EndPos" -> Seq(endPos.toString))
    )

    BintrayClient.remainingPages(wsResponse)
  }

  private def wsResponseWithHeaders(providedHeaders: Map[String, Seq[String]]) =
    new WSResponse {
      override def status: Int = ???

      override def statusText: String = ???

      override def underlying[T]: T = ???

      override def cookies: Seq[WSCookie] = ???

      override def cookie(name: String): Option[WSCookie] = ???

      override def body: String = ???

      override def bodyAsBytes: ByteString = ???

      override def bodyAsSource: Source[ByteString, _] = ???

      override def allHeaders: Map[String, Seq[String]] = ???

      override def xml: Elem = ???

      override def json: JsValue = ???

      override def headers: Map[String, Seq[String]] = providedHeaders
    }
} 
Example 16
Source File: MillinerHatSignup.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.resourceManagement

import org.hatdex.hat.resourceManagement.models.HatSignup
import play.api.cache.AsyncCacheApi
import play.api.http.Status._
import play.api.libs.json.{ JsError, JsSuccess }
import play.api.libs.ws.{ WSClient, WSRequest, WSResponse }
import play.api.{ Configuration, Logger }

import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }

trait MillinerHatSignup {
  val logger: Logger
  val ws: WSClient
  val configuration: Configuration
  val schema: String = configuration.get[String]("resourceManagement.millinerAddress") match {
    case address if address.startsWith("https") => "https://"
    case address if address.startsWith("http")  => "http://"
    case _                                      => "https://"
  }

  val millinerAddress: String = configuration.get[String]("resourceManagement.millinerAddress")
    .stripPrefix("http://")
    .stripPrefix("https://")
  val hatSharedSecret: String = configuration.get[String]("resourceManagement.hatSharedSecret")

  val cache: AsyncCacheApi

  def getHatSignup(hatAddress: String)(implicit ec: ExecutionContext): Future[HatSignup] = {
    // Cache the signup information for subsequent calls (For private/public key and database details)
    cache.getOrElseUpdate[HatSignup](s"configuration:$hatAddress") {
      val request: WSRequest = ws.url(s"$schema$millinerAddress/api/manage/configuration/$hatAddress")
        .withVirtualHost(millinerAddress)
        .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> hatSharedSecret)

      val futureResponse: Future[WSResponse] = request.get()
      futureResponse.map { response =>
        response.status match {
          case OK =>
            response.json.validate[HatSignup] match {
              case signup: JsSuccess[HatSignup] =>
                logger.debug(s"Got back configuration: ${signup.value}")
                cache.set(s"configuration:$hatAddress", signup.value, 1.minute)
                signup.value
              case e: JsError =>
                logger.error(s"Parsing HAT configuration failed: $e")
                throw new HatServerDiscoveryException("Fetching HAT configuration failed")
            }
          case _ =>
            logger.error(s"Fetching HAT configuration failed: ${response.body}")
            throw new HatServerDiscoveryException("Fetching HAT configuration failed")
        }
      }
    }
  }

} 
Example 17
Source File: BintrayHttp.scala    From releaser   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.releaser.bintray

import java.net.URL
import java.nio.file.Path
import java.util.concurrent.TimeUnit

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws.ning.{NingAsyncHttpClientConfigBuilder, NingWSClient, NingWSClientConfig}
import play.api.libs.ws.{WSAuthScheme, WSClientConfig, WSResponse}
import play.api.mvc.Results
import uk.gov.hmrc.{Logger, ServiceCredentials}

import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success, Try}
import scala.concurrent.duration._

class BintrayHttp(creds:ServiceCredentials) extends Logger {

  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()

  private def getTimeoutPropertyOptional(key: String) = Option(System.getProperty(key)).map(_.toLong milliseconds)

  def wsClientConfig = NingWSClientConfig(
    wsClientConfig = WSClientConfig(
    connectionTimeout = getTimeoutPropertyOptional("wsclient.timeout.connection").getOrElse(2 seconds),
    idleTimeout = getTimeoutPropertyOptional("wsclient.timeout.idle").getOrElse(2 seconds),
    requestTimeout = getTimeoutPropertyOptional("wsclient.timeout.request").getOrElse(2 seconds)
    )
  )

  val ws = new NingWSClient(new NingAsyncHttpClientConfigBuilder(wsClientConfig).build())

  def apiWs(url:String) = ws.url(url)
    .withAuth(
      creds.user, creds.pass, WSAuthScheme.BASIC)
    .withHeaders("content-type" -> "application/json")

  def emptyPost(url:String): Try[Unit] = {
    log.info(s"posting file to $url")

    val call = apiWs(url).post(Results.EmptyContent())
    val result: WSResponse = Await.result(call, Duration.apply(5, TimeUnit.MINUTES))

    result.status match {
      case s if s >= 200 && s < 300 => Success(new URL(url))
      case _@e => Failure(new scala.Exception(s"Didn't get expected status code when writing to Bintray. Got status ${result.status}: ${result.body}"))
    }
  }

  def get[A](url:String): Try[String] ={
    log.info(s"getting file from $url")

    val call = apiWs(url).get()
    val result: WSResponse = Await.result(call, Duration.apply(5, TimeUnit.MINUTES))

    result.status match {
      case s if s >= 200 && s < 300 => Success(result.body)
      case _@e => Failure(new scala.Exception(s"Didn't get expected status code when writing to Bintray. Got status ${result.status}: ${result.body}"))
    }
  }

  def putFile(version: VersionDescriptor, file: Path, url: String): Try[Unit] = {
    log.info(s"version $version")
    log.info(s"putting file to $url")

    val call = apiWs(url)
      .withHeaders(
        "X-Bintray-Package" -> version.artefactName,
        "X-Bintray-Version" -> version.version)
      .put(file.toFile)

    val result: WSResponse = Await.result(call, Duration.apply(6, TimeUnit.MINUTES))

    result.status match {
      case s if s >= 200 && s < 300 => Success(Unit)
      case _@e => Failure(new scala.Exception(s"Didn't get expected status code when writing to Bintray. Got status ${result.status}: ${result.body}"))
    }
  }
} 
Example 18
Source File: ConfigControllerSpec.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package controllers

import org.scalatest.concurrent.IntegrationPatience
import org.scalatestplus.play._
import play.api.Configuration
import play.api.libs.json.Json
import test.{IzanamiMatchers, OneServerPerSuiteWithMyComponents}
import play.api.libs.ws.JsonBodyWritables._
import play.api.libs.ws.WSResponse
import scala.util.Random
import org.scalatest.BeforeAndAfterAll

abstract class ConfigControllerSpec(name: String, configurationSpec: Configuration)
    extends PlaySpec
    with IzanamiMatchers
    with OneServerPerSuiteWithMyComponents
    with IntegrationPatience {

  override def getConfiguration(configuration: Configuration) =
    configurationSpec withFallback configuration

  private lazy val ws       = izanamiComponents.wsClient
  private lazy val rootPath = s"http://localhost:$port"

  s"$name ConfigController" should {

    "create read update delete" in {
      val key = "my:path"
      
      val getByIdUpdated =
        ws.url(s"$rootPath/api/configs/$key2").get().futureValue
      getByIdUpdated must beAResponse(200, configUpdated)

      ws.url(s"$rootPath/api/configs/$key").get().futureValue must beAStatus(404)
    }
  }

} 
Example 19
Source File: IntegrationBaseSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package support

import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{JsValue, Json}
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import play.api.{Application, Environment, Mode}

trait IntegrationBaseSpec extends UnitSpec with WireMockHelper with GuiceOneServerPerSuite
  with BeforeAndAfterEach with BeforeAndAfterAll {

  val mockHost: String = WireMockHelper.host
  val mockPort: String = WireMockHelper.wireMockPort.toString

  lazy val client: WSClient = app.injector.instanceOf[WSClient]

  def servicesConfig: Map[String, Any] = Map(
    "microservice.services.des.host" -> mockHost,
    "microservice.services.des.port" -> mockPort,
    "microservice.services.auth.host" -> mockHost,
    "microservice.services.auth.port" -> mockPort,
    "auditing.consumer.baseUri.port" -> mockPort,
    "microservice.services.non-repudiation.host" -> mockHost,
    "microservice.services.non-repudiation.port" -> mockPort,
    "feature-switch.refactor.enabled" -> true,
    "feature-switch.refactor.prod.enabled" -> false,
    "microservice.services.non-repudiation.maxTimeout" -> 5000
  )

  override implicit lazy val app: Application = new GuiceApplicationBuilder()
    .in(Environment.simple(mode = Mode.Dev))
    .configure(servicesConfig)
    .build()

  override def beforeAll(): Unit = {
    super.beforeAll()
    startWireMock()
  }

  override def afterAll(): Unit = {
    stopWireMock()
    super.afterAll()
  }

  def buildRequest(path: String): WSRequest = client.url(s"http://localhost:$port$path").withFollowRedirects(false)

  def document(response: WSResponse): JsValue = Json.parse(response.body)
} 
Example 20
Source File: SemanticRepositorySpecs.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package specs

import org.junit.runner.RunWith

import scala.concurrent.{ Await, Future }
import scala.concurrent.duration.Duration

import play.api.test._
import play.api.http.Status
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSResponse
import play.api.libs.ws.ahc.AhcWSClient
import org.specs2.runner.JUnitRunner
import org.specs2.mutable.Specification
import play.api.libs.json.Json
//import it.almawave.linkeddata.kb.utils.ConfigHelper

import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
import play.twirl.api.Content
import play.api.test.Helpers._
import play.api.libs.json.JsObject
import java.io.File
import play.api.http.Writeable
import akka.stream.scaladsl.Source
import play.api.mvc.MultipartFormData
import play.api.libs.Files.TemporaryFile
import java.nio.file.Files
import org.asynchttpclient.AsyncHttpClient
import play.api.libs.ws.WS
import akka.util.ByteString
import play.api.mvc.MultipartFormData.DataPart
import play.api.mvc.MultipartFormData.FilePart
import akka.stream.scaladsl.FileIO
import play.api.libs.ws.WSClient

/*
 * TODO: REWRITE
 */
@RunWith(classOf[JUnitRunner])
class SemanticRepositorySpecs extends Specification {

  def application: Application = GuiceApplicationBuilder().build()

  "The semantic repository" should {

    "call kb/v1/contexts to obtain a list of contexts" in {
      new WithServer(app = application, port = 9999) {
        WsTestClient.withClient { implicit client =>

          val response: WSResponse = Await.result[WSResponse](
            client.url(s"http://localhost:${port}/kb/v1/contexts").execute,
            Duration.Inf)

          response.status must be equalTo Status.OK
          response.json.as[Seq[JsObject]].size must be equals 0
          // response.json.as[Seq[JsObject]].size must be greaterThan 0 // if pre-loaded ontologies!

        }
      }
    }

    "call kb/v1/contexts ensuring all contexts have triples" in {
      new WithServer(app = application, port = 9999) {
        WsTestClient.withClient { implicit client =>

          val response: WSResponse = Await.result[WSResponse](
            client.url(s"http://localhost:${port}/kb/v1/contexts").execute,
            Duration.Inf)

          val json_list = response.json.as[Seq[JsObject]]
          forall(json_list)((_) must not beNull)
          forall(json_list)(_.keys must contain("context", "triples"))
          forall(json_list)(item => (item \ "triples").get.as[Int] > 0)

        }
      }
    }

  }

} 
Example 21
Source File: NifiProcessorSpec.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.ingestion.nifi


import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.typesafe.config.Config
import it.gov.daf.catalogmanager.MetaCatalog
import it.gov.daf.catalogmanager.json._
import it.gov.daf.ingestion.metacatalog.MetaCatalogProcessor
import org.scalatest.{AsyncFlatSpec, Matchers}
import play.api.libs.json._
import play.api.libs.ws.WSResponse
import play.api.libs.ws.ahc.AhcWSClient

import scala.concurrent.Future
import scala.io.Source

class NifiProcessorSpec extends AsyncFlatSpec with Matchers {

  "A Nifi Processor " should "create a nifi pipeline for a correct meta catalog entry" in {

    val in = this.getClass.getResourceAsStream("/data_test.json")
    val sMetaCatalog = Source.fromInputStream(in).getLines().mkString(" ")
    in.close()

    val parsed = Json.parse(sMetaCatalog)
    val metaCatalog: JsResult[MetaCatalog] = Json.fromJson[MetaCatalog](parsed)

    metaCatalog.isSuccess shouldBe true

    implicit val system: ActorSystem = ActorSystem()
    implicit val materializer: ActorMaterializer = ActorMaterializer()
    implicit val wsClient: AhcWSClient = AhcWSClient()

    implicit val config: Config = com.typesafe.config.ConfigFactory.load()
    implicit val ec = system.dispatcher

    def closeAll(): Unit = {
      system.terminate()
      materializer.shutdown()
      wsClient.close()
    }

    val fResult = NifiProcessor(metaCatalog.get).createDataFlow()

    fResult.map { response =>
      println(response)
      closeAll()
      true shouldBe true
    }
  }


} 
Example 22
Source File: ServiceSpec.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
import java.io.{IOException, File => JFile}
import java.net.ServerSocket
import java.util.Base64

import it.gov.daf.securitymanager.client.Security_managerClient
import org.specs2.mutable.Specification
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.ahc.AhcWSClient
import play.api.libs.ws.{WSAuthScheme, WSResponse}
import play.api.test.{WithServer, WsTestClient}

import scala.concurrent.Await
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.duration.Duration

//@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements", "org.wartremover.warts.Throw"))
class ServiceSpec extends Specification {

  def getAvailablePort: Int = {
    try {
      val socket = new ServerSocket(0)
      try {
        socket.getLocalPort
      } finally {
        socket.close()
      }
    } catch {
      case e: IOException =>
        throw new IllegalStateException(s"Cannot find available port: ${e.getMessage}", e)
    }
  }

  def application: Application = GuiceApplicationBuilder().
    configure("pac4j.authenticator" -> "test").
    build()

  "The security_manager" should {
    "manage user tokens correctly" in new WithServer(app = application, port = getAvailablePort) {

      private val token = WsTestClient.withClient { implicit client =>
        val response: WSResponse = Await.result[WSResponse](client.
          url(s"http://localhost:$port/security-manager/v1/token").
          withAuth("david", "david", WSAuthScheme.BASIC).
          execute, Duration.Inf)
        response.body
      }

      val ws: AhcWSClient = AhcWSClient()

      val plainCreds = "david:david"
      val plainCredsBytes = plainCreds.getBytes
      val base64CredsBytes = Base64.getEncoder.encode(plainCredsBytes)
      val base64Creds = new String(base64CredsBytes)

      val client = new Security_managerClient(ws)(s"http://localhost:$port")

      val token2 = Await.result(client.token(s"Basic $base64Creds"), Duration.Inf)

      s""""$token2"""" must be equalTo token

      Await.result(client.token(s"Bearer $token2").map(token => s""""$token""""), Duration.Inf) must be equalTo token
    }
  }

} 
Example 23
Source File: Iot_managerSpec.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
import java.io.{IOException, File => JFile}
import java.net.ServerSocket
import java.util.Base64

import better.files._
import it.gov.daf.iotmanager.client.Iot_managerClient
import org.specs2.mutable.Specification
import org.specs2.specification.BeforeAfterAll
import play.api.Application
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.ahc.AhcWSClient
import play.api.libs.ws.{WSAuthScheme, WSResponse}
import play.api.test.{WithServer, WsTestClient}

import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.util.{Failure, Try}

import org.apache.solr.client.solrj.embedded.JettyConfig
import org.apache.solr.client.solrj.embedded.JettySolrRunner
import org.eclipse.jetty.servlet.ServletHolder


@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements", "org.wartremover.warts.Throw"))
class Iot_managerSpec extends Specification with BeforeAfterAll {

  import Iot_managerSpec._

  def getAvailablePort: Int = {
    try {
      val socket = new ServerSocket(0)
      try {
        socket.getLocalPort
      } finally {
        socket.close()
      }
    } catch {
      case e: IOException =>
        throw new IllegalStateException(s"Cannot find available port: ${e.getMessage}", e)
    }
  }

  def application: Application = GuiceApplicationBuilder().
    configure("hadoop_conf_dir" -> s"${ServiceSpec.confPath.pathAsString}").
    configure("pac4j.authenticator" -> "test").
    build()

  "The security_manager" should {
    "manage user tokens correctly" in new WithServer(app = application, port = getAvailablePort) {
      print("ciao ciao")


    }
  }

  override def beforeAll(): Unit = {
    val solrXml = new Nothing("/solr/home/solr.xml")
    val solrHomeDir = solrXml.getParentFile

    val port = 8080
    val context = "/solr"
    // use org.apache.solr.client.solrj.embedded.JettySolrRunner
    val jettySolr = new Nothing(solrHomeDir.getAbsolutePath, context, port)

    val waitUntilTheSolrWebAppHasStarted = true
    jettySolr.start(waitUntilTheSolrWebAppHasStarted)
  }

  override def afterAll(): Unit = {
    jettySolr.stop()
  }
}

@SuppressWarnings(Array("org.wartremover.warts.Var", "org.wartremover.warts.Null"))
object Iot_managerSpec {



} 
Example 24
Source File: CatalogControllersSpec.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
import java.io.IOException
import java.net.ServerSocket

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import catalog_manager.yaml.MetaCatalog
import org.specs2.mutable.Specification
import play.api.Application
import play.api.http.Status
import play.api.routing.Router
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{JsArray, JsValue, Json}
import play.api.libs.ws.WSResponse
import play.api.libs.ws.ahc.AhcWSClient
import play.api.test._
import it.gov.daf.catalogmanager
import it.gov.daf.catalogmanager.client.Catalog_managerClient

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}


class CatalogControllersSpec extends Specification  {

  def application: Application = GuiceApplicationBuilder().build()

  import catalog_manager.yaml.BodyReads.MetaCatalogReads

  "The catalog-manager" should {
    "Call catalog-manager/v1/dataset-catalogs return ok status" in
      new WithServer(app = application, port = 9000) {
        WsTestClient.withClient { implicit client =>
          val response: WSResponse = Await.result[WSResponse](client.
            url(s"http://localhost:9001/catalog-manager/v1/dataset-catalogs").
            execute, Duration.Inf)
          println(response.status)
          response.status must be equalTo Status.OK
        }
      }

    "Call catalog-manager/v1/dataset-catalogs return a non empty list if" +
      "you have error maybe is necessaty to add data to db" in
      new WithServer(app = application, port = 9000) {
        WsTestClient.withClient { implicit client =>
          val response: WSResponse = Await.result[WSResponse](client.
            url(s"http://localhost:9001/catalog-manager/v1/dataset-catalogs").
            execute, Duration.Inf)
          println(response.status)
          println("ALE")
          println(response.body)
          val json: JsValue = Json.parse(response.body)
          json.as[JsArray].value.size must be greaterThan (0)
        }
      }


    "The catalog-manager" should {
      "Call catalog-manager/v1/dataset-catalogs/{logical_uri} return ok status" in
        new WithServer(app = application, port = 9000) {
          val logicalUri = "daf://dataset/std/standard/standard/uri_cultura/standard"
          val url = s"http://localhost:9001/catalog-manager/v1/dataset-catalogs/$logicalUri"
          println(url)
          WsTestClient.withClient { implicit client =>
            val response: WSResponse = Await.result[WSResponse](client.
              url(url).
              execute, Duration.Inf)
            println(response.status)
            response.status must be equalTo Status.OK
          }
        }
    }

    "The catalog-manager" should {
      "Call catalog-manager/v1/dataset-catalogs/{anything} return 401" in
        new WithServer(app = application, port = 9000) {
          val logicalUri = "anything"
          val url = s"http://localhost:9001/catalog-manager/v1/dataset-catalogs/$logicalUri"
          println(url)
          WsTestClient.withClient { implicit client =>
            val response: WSResponse = Await.result[WSResponse](client.
              url(url).
              execute, Duration.Inf)
            println(response.status)
            response.status must be equalTo 401
          }
        }
    }
  }
} 
Example 25
Source File: StandardNrsWsParser.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.connectors.httpparsers

import play.api.Logger
import play.api.http.Status._
import play.api.libs.json.Reads
import play.api.libs.ws.WSResponse
import v1.connectors.NrsOutcome
import v1.models.nrs.response.NrsError

trait WsReads[A] {
  def wsRead(response: WSResponse, defaultResult: A): A
}

object StandardNrsWsParser extends WsParser {

  val logger = Logger(getClass)

  case class SuccessCode(status: Int) extends AnyVal

  implicit def nrsReads[A: Reads](implicit successCode: SuccessCode = SuccessCode(ACCEPTED)): WsReads[NrsOutcome[A]] =
    (response: WSResponse, defaultResult: NrsOutcome[A]) => doRead(response, defaultResult) { response =>
      response.validateJson[A] match {
        case Some(ref) => Right(ref)
        case None => Left(NrsError)
      }
    }

  private def doRead[A](response: WSResponse, defaultResult: NrsOutcome[A])(successOutcomeFactory: WSResponse => NrsOutcome[A])(
    implicit successCode: SuccessCode): NrsOutcome[A] = {

    if (response.status!=successCode.status){
      logger.info("[StandardNrsWsParser][read] - Error response received from NRS " +
        s"with status: ${response.status} and body\n ${response.body}")
    }
    response.status match {
      case successCode.status =>
        logger.info("[StandardNrsWsParser][read] - Success response received from NRS")
        successOutcomeFactory(response)
      case BAD_REQUEST => Left(NrsError)
      case _ => defaultResult
    }
  }
} 
Example 26
Source File: WsParser.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.connectors.httpparsers

import play.api.Logger
import play.api.libs.json.{JsError, JsSuccess, JsValue, Reads}
import play.api.libs.ws.WSResponse

import scala.util.{Success, Try}

trait WsParser {

  implicit class KnownJsonResponse(response: WSResponse) {

    def validateJson[T](implicit reads: Reads[T]): Option[T] = {
      Try(response.json) match {
        case Success(json: JsValue) => parseResult(json)
        case _ =>
          Logger.warn("[KnownJsonResponse][validateJson - NRS] No JSON was returned")
          None
      }
    }

    def parseResult[T](json: JsValue)(implicit reads: Reads[T]): Option[T] = json.validate[T] match {

      case JsSuccess(value, _) => Some(value)
      case JsError(error) =>
        Logger.warn(s"[KnownJsonResponse][validateJson - NRS] Unable to parse JSON: $error")
        None
    }
  }
} 
Example 27
Source File: AuthISpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.endpoints

import com.github.tomakehurst.wiremock.stubbing.StubMapping
import play.api.http.HeaderNames.ACCEPT
import play.api.http.Status._
import play.api.libs.ws.{WSRequest, WSResponse}
import support.IntegrationBaseSpec
import v1.stubs.{AuditStub, AuthStub, DesStub}

class AuthISpec extends IntegrationBaseSpec {

  private trait Test {
    val vrn = "123456789"
    val periodKey = "AB19"
    val correlationId = "X-123"

    def setupStubs(): StubMapping

    def request(): WSRequest = {
      setupStubs()
      buildRequest(s"/$vrn/returns/$periodKey")
        .withHttpHeaders((ACCEPT, "application/vnd.hmrc.1.0+json"))
    }
  }

  "Calling the View VAT Return endpoint" when {
    "the user is authorised" should {
      "return 200" in new Test {

        override def setupStubs(): StubMapping = {
          AuditStub.audit()
          AuthStub.authorised()
          DesStub.serviceSuccess(vrn, periodKey)
        }

        val response: WSResponse = await(request().get())
        response.status shouldBe OK
      }
    }

    "the user is belongs to an unsupported affinity group" should {
      "return 403" in new Test {

        override def setupStubs(): StubMapping = {
          AuditStub.audit()
          AuthStub.unauthorisedUnsupportedAffinity()
          DesStub.serviceSuccess(vrn, periodKey)
        }

        val response: WSResponse = await(request().get())
        response.status shouldBe FORBIDDEN
      }
    }

    "the user is not logged in" should {
      "return 500" in new Test {

        override def setupStubs(): StubMapping = {
          AuditStub.audit()
          AuthStub.unauthorisedNotLoggedIn()
        }

        val response: WSResponse = await(request().get())
        response.status shouldBe INTERNAL_SERVER_ERROR
      }
    }

    "the user is NOT authorised" should {
      "return 500" in new Test {

        override def setupStubs(): StubMapping = {
          AuditStub.audit()
          AuthStub.unauthorisedOther()
        }

        val response: WSResponse = await(request().get())
        response.status shouldBe INTERNAL_SERVER_ERROR
      }
    }
  }
} 
Example 28
Source File: PdfGeneratorConnector.scala    From pertax-frontend   with Apache License 2.0 5 votes vote down vote up
package connectors

import com.google.inject.{ImplementedBy, Inject, Singleton}
import play.api.Mode
import play.api.libs.ws.{WSClient, WSResponse}
import play.api.{Configuration, Environment}
import uk.gov.hmrc.play.bootstrap.config.ServicesConfig

import scala.concurrent.Future

@ImplementedBy(classOf[FrontendPdfGeneratorConnector])
trait PdfGeneratorConnector {
  val serviceURL: String
  def getWsClient: WSClient

  def generatePdf(html: String): Future[WSResponse] =
    getWsClient.url(serviceURL).post(Map("html" -> Seq(html)))
}

@Singleton
class FrontendPdfGeneratorConnector @Inject()(
  environment: Environment,
  runModeConfiguration: Configuration,
  wsClient: WSClient,
  servicesConfig: ServicesConfig)
    extends PdfGeneratorConnector {
  val mode: Mode = environment.mode
  val pdfServiceUrl: String = servicesConfig.baseUrl("pdf-generator-service")
  val serviceURL = pdfServiceUrl + "/pdf-generator-service/generate"

  override def getWsClient: WSClient = wsClient
} 
Example 29
Source File: DocumentationControllerISpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package config


import play.api.http.Status
import play.api.libs.json.{JsValue, Json}
import play.api.libs.ws.WSResponse
import support.IntegrationBaseSpec

class DocumentationControllerISpec extends IntegrationBaseSpec {

  val apiDefinitionJson: JsValue = Json.parse(
    """
      |{
      |  "scopes":[
      |    {
      |      "key":"read:vat",
      |      "name":"View your VAT information",
      |      "description":"Allow read access to VAT data"
      |    },
      |    {
      |      "key":"write:vat",
      |      "name":"Change your VAT information",
      |      "description":"Allow write access to VAT data"
      |    }
      |  ],
      |  "api":{
      |    "name":"VAT (MTD)",
      |    "description":"An API for providing VAT data",
      |    "context":"organisations/vat",
      |    "categories":["VAT_MTD"],
      |    "versions":[
      |      {
      |        "version":"1.0",
      |        "status":"BETA",
      |        "endpointsEnabled":true
      |      }
      |    ]
      |  }
      |}
    """.stripMargin)

  "GET /api/definition" should {
    "return a 200 with the correct response body" in {
      val response: WSResponse = await(buildRequest("/api/definition").get())
      response.status shouldBe Status.OK
      Json.parse(response.body) shouldBe apiDefinitionJson
    }
  }

  "a documentation request" must {
    "return the documentation" in {
      val response: WSResponse = await(buildRequest("/api/conf/1.0/application.raml").get())
      response.status shouldBe Status.OK
      response.body[String] should startWith("#%RAML 1.0")
    }
  }

} 
Example 30
Source File: Http.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.support

import play.api.libs.json.{JsValue, Json, Writes}
import play.api.libs.ws.{EmptyBody, WSRequest, WSResponse}
import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse}
import uk.gov.hmrc.play.http.ws.WSHttpResponse

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

object Http extends BaseFunctionalSpec {

  def get(url: String)(implicit hc: HeaderCarrier, timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.get()
  }

  def post[A](url: String, body: A, headers: Seq[(String, String)] = Seq.empty)(
      implicit writes: Writes[A],
      hc: HeaderCarrier,
      timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.post(Json.toJson(body))
  }

  def postString(url: String, body: String, headers: Seq[(String, String)] = Seq.empty)(
    implicit hc: HeaderCarrier,
    timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.withHttpHeaders("Content-Type" -> "application/json").post[String](body)
  }

  def postJson(url: String, body: JsValue, headers: Seq[(String, String)] = Seq.empty)(
      implicit hc: HeaderCarrier,
      timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.post(body)
  }

  def putJson(url: String, body: JsValue, headers: Seq[(String, String)] = Seq.empty)(
      implicit hc: HeaderCarrier,
      timeout: FiniteDuration): HttpResponse = perform(url) { request =>
    request.put(body)
  }

  def postEmpty(url: String)(implicit hc: HeaderCarrier, timeout: FiniteDuration): HttpResponse = perform(url) {
    request =>
      request.post(EmptyBody)
  }

  def delete(url: String)(implicit hc: HeaderCarrier, timeout: FiniteDuration): HttpResponse = perform(url) {
    request =>
      request.delete()
  }

  def perform(url: String)(fun: WSRequest => Future[WSResponse])(implicit hc: HeaderCarrier,
                                                                         timeout: FiniteDuration): WSHttpResponse =
    await(fun(client.url(url).addHttpHeaders(hc.headers: _*).withRequestTimeout(timeout)).map(new WSHttpResponse(_)))

  override def await[A](future: Future[A])(implicit timeout: FiniteDuration) = Await.result(future, timeout)

} 
Example 31
Source File: MockWsClient.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.mocks
import org.scalamock.handlers.CallHandler
import org.scalamock.scalatest.MockFactory
import play.api.libs.ws.{BodyWritable, WSClient, WSRequest, WSResponse}

import scala.concurrent.Future
import scala.concurrent.duration.Duration

trait MockWsClient extends MockFactory {

  val mockWsClient: WSClient = mock[WSClient]
  val mockWsRequest: WSRequest = mock[WSRequest]

  object MockWsClient {

    def url(url: String): CallHandler[WSRequest] = {
      (mockWsClient.url(_: String))
        .expects(url)
    }
  }

  object MockWsRequest {

    def withHttpHeaders(headers: Seq[(String, String)]): CallHandler[WSRequest] = {
      (mockWsRequest.withHttpHeaders _ ).expects(*)
    }

    def withRequestTimeout(timeout: Duration): CallHandler[WSRequest] = {
      (mockWsRequest.withRequestTimeout(_: Duration))
        .expects(timeout)
    }

    def post[I: BodyWritable](body: I): CallHandler[Future[WSResponse]] = {
      (mockWsRequest.post(_: I)(_: BodyWritable[I]))
        .expects(body, *)
    }
  }

} 
Example 32
Source File: StandardNrsWsParserSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.connectors.httpparsers

import play.api.http.Status._
import play.api.libs.json.Json
import play.api.libs.ws.WSResponse
import play.api.libs.ws.ahc.AhcWSResponse
import play.api.libs.ws.ahc.cache.{CacheableHttpResponseBodyPart, CacheableHttpResponseStatus}
import play.shaded.ahc.org.asynchttpclient.Response
import play.shaded.ahc.org.asynchttpclient.uri.Uri
import support.UnitSpec
import v1.connectors.NrsOutcome
import v1.models.nrs.response.NrsError

class StandardNrsWsParserSpec extends UnitSpec {

  val method = "POST"
  val url = "test-url"

  import v1.connectors.httpparsers.StandardNrsWsParser._

  def wsReads: WsReads[NrsOutcome[SomeModel]] = implicitly

  val data = "someData"
  val nrsExpectedJson: Array[Byte] = Json.obj("data" -> data).toString().getBytes()

  val nrsResponse = SomeModel(data)

  "A generic WS parser" when {
    "no status code is specified" must {
      "return the expected success response for an ACCEPTED status" in {

        val wsResponse: WSResponse = new AhcWSResponse(new Response.ResponseBuilder()
          .accumulate(new CacheableHttpResponseStatus(Uri.create("http://uri"), ACCEPTED, "status text", "protocols!"))
          .accumulate(new CacheableHttpResponseBodyPart(nrsExpectedJson, true))
          .build());

        wsReads.wsRead(wsResponse, Right(SomeModel(""))) shouldBe Right(nrsResponse)
      }

      "return an error response for a BAD_REQUEST status" in {

        val wsResponse: WSResponse = new AhcWSResponse(new Response.ResponseBuilder()
          .accumulate(new CacheableHttpResponseStatus(Uri.create("http://uri"), BAD_REQUEST, "status text", "protocols!"))
          .accumulate(new CacheableHttpResponseBodyPart("".getBytes(), true))
          .build());

        wsReads.wsRead(wsResponse, Right(SomeModel(""))) shouldBe Left(NrsError)
      }

      "return the default response for an unexpected status" in {

        val wsResponse: WSResponse = new AhcWSResponse(new Response.ResponseBuilder()
          .accumulate(new CacheableHttpResponseStatus(Uri.create("http://uri"), IM_A_TEAPOT, "status text", "protocols!"))
          .accumulate(new CacheableHttpResponseBodyPart(nrsExpectedJson, true))
          .build());

        wsReads.wsRead(wsResponse, Right(SomeModel(""))) shouldBe Right(SomeModel(""))
      }
    }

    "a status code is specified" must {
      "return the expected success response for that status" in {

        implicit val successCode: SuccessCode = SuccessCode(OK)
        def wsReads: WsReads[NrsOutcome[SomeModel]] = implicitly

        val wsResponse: WSResponse = new AhcWSResponse(new Response.ResponseBuilder()
          .accumulate(new CacheableHttpResponseStatus(Uri.create("http://uri"), OK, "status text", "protocols!"))
          .accumulate(new CacheableHttpResponseBodyPart(nrsExpectedJson, true))
          .build());

        wsReads.wsRead(wsResponse, Right(SomeModel(""))) shouldBe Right(nrsResponse)
      }
    }

    "a response contains JSON which cannot be parsed as the expected response type" must {
      "return an error response" in {

        val wsResponse: WSResponse = new AhcWSResponse(new Response.ResponseBuilder()
          .accumulate(new CacheableHttpResponseStatus(Uri.create("http://uri"), ACCEPTED, "status text", "protocols!"))
          .accumulate(new CacheableHttpResponseBodyPart("".getBytes(), true))
          .build());

        wsReads.wsRead(wsResponse, Right(SomeModel(""))) shouldBe Left(NrsError)
      }
    }
  }
} 
Example 33
Source File: NRSConnectorSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.connectors

import java.util.concurrent.TimeoutException

import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.libs.json.{JsResultException, Json, Writes}
import play.api.libs.ws.{WSClient, WSRequest, WSResponse}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.vatapi.UnitSpec
import uk.gov.hmrc.vatapi.assets.TestConstants.NRSResponse._
import uk.gov.hmrc.vatapi.httpparsers.NrsSubmissionHttpParser.NrsSubmissionOutcome
import uk.gov.hmrc.vatapi.httpparsers.{EmptyNrsData, NRSData}
import uk.gov.hmrc.vatapi.mocks.MockHttp
import uk.gov.hmrc.vatapi.mocks.config.MockAppContext
import uk.gov.hmrc.vatapi.models.NRSSubmission

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class NRSConnectorSpec extends UnitSpec with GuiceOneAppPerSuite
  with MockHttp
  with MockAppContext {

  class Setup {
    val wsClient = mock[WSClient]
    val testNrsConnector = new NRSConnector(mockHttp, mockAppContext, wsClient)

    MockAppContext.nrsMaxTimeoutMilliseconds returns 5000

    val testUrl: String = testNrsConnector.nrsSubmissionUrl(testVrn.vrn)
    def result(requestBody: NRSSubmission): Future[NrsSubmissionOutcome] = testNrsConnector.submit(testVrn, requestBody)
  }

  implicit val hc: HeaderCarrier = HeaderCarrier(nsStamp = 2L)

  val testVrn = Vrn("123456789")

  "NRSConnector.submit" should {

    "successful responses are returned from the connector" should {
      "return the correctly formatted NRS Data model" in new Setup {

        val request = mock[WSRequest]
        val response = mock[WSResponse]
        val expectedResponse = NRSData("2dd537bc-4244-4ebf-bac9-96321be13cdc","This has been deprecated - DO NOT USE","")


        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]
        val meJson = Json.toJson(nrsSubmission)

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(meJson))(any())).thenReturn(Future.successful(response))
        when(response.json).thenReturn(nrsResponseJson)
        when(response.status).thenReturn(202)

        await(result(nrsSubmission)) shouldBe Right(expectedResponse)

      }

    }

    "return EmptyNrsData" when {
      "the connection times out" in new Setup {

        val request = mock[WSRequest]

        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(Json.toJson(nrsSubmission)))(any())).thenReturn(Future.failed(new TimeoutException("Expected Error")))

        await(result(nrsSubmission)) shouldBe Right(EmptyNrsData)

      }

      "the response JSON cannot be parsed" in new Setup {

        val request = mock[WSRequest]
        val response = mock[WSResponse]

        implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

        when(wsClient.url(testUrl)).thenReturn(request)
        when(request.withHttpHeaders(any())).thenReturn(request)
        when(request.withRequestTimeout(testNrsConnector.nrsMaxTimeout)).thenReturn(request)
        when(request.post(eqTo(Json.toJson(nrsSubmission)))(any())).thenReturn(Future.successful(response))
        when(response.json).thenThrow(JsResultException(Seq()))
        await(result(nrsSubmission)) shouldBe Right(EmptyNrsData)

      }
    }
  }
} 
Example 34
Source File: HttpRequest.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package support.functional

import play.api.libs.ws.{EmptyBody, WSRequest, WSResponse}
import support.WSClient

import scala.concurrent.Future

trait HttpRequest extends WSClient {

  private val GET = "GET"
  private val POST = "POST"
  private val PUT = "PUT"

  case class HttpRequest(verb: String, wsRequest: WSRequest, body: Option[String] = None) {
    def execute: Future[WSResponse] = body.fold(executeNoBody)(executeWithBody)

    private def executeNoBody: Future[WSResponse] = verb match {
      case GET  => wsRequest.get()
      case POST => wsRequest.post(EmptyBody)
      case PUT => wsRequest.put(EmptyBody)
    }

    private def executeWithBody(body: String): Future[WSResponse] = verb match {
      case GET  => wsRequest.get()
      case POST => wsRequest.post(body)
      case PUT => wsRequest.put(body)
    }
  }

  def httpGet(url: String): HttpRequest = {
    HttpRequest(GET, buildRequest(url))
  }

  def httpPost(url: String): HttpRequest = {
    HttpRequest(POST, buildRequest(url))
  }

  def httpPut(url: String): HttpRequest = {
    HttpRequest(PUT, buildRequest(url))
  }

  def requestWithHeaders(request: HttpRequest, headers: Seq[(String, String)]): HttpRequest = {
    request.copy(wsRequest = request.wsRequest.withHttpHeaders(headers: _*))
  }

  def requestWithBody(request: HttpRequest, body: String): HttpRequest = {
    request.copy(body = Some(body))
  }
} 
Example 35
Source File: ArticleDeleteTest.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package articles

import articles.models.ArticleWithTags
import articles.test_helpers.Articles
import commons_test.test_helpers.{RealWorldWithServerAndTestConfigBaseTest, WithArticleTestHelper, WithUserTestHelper}
import core.tags.test_helpers.TagTestHelper
import play.api.libs.ws.WSResponse
import users.models.UserDetailsWithToken
import users.test_helpers.UserRegistrations

class ArticleDeleteTest extends RealWorldWithServerAndTestConfigBaseTest with WithArticleTestHelper with WithUserTestHelper {

  def tagsTestHelper: TagTestHelper = new TagTestHelper(executionContext)

  "Delete article" should "delete article" in await {
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      token = userDetailsWithToken.token
      articleWithTags <- articleTestHelper.create[ArticleWithTags](Articles.hotToTrainYourDragon, token)
      response <- articleTestHelper.delete(articleWithTags, token)
      getBySlugResponse: WSResponse <- articleTestHelper.getBySlug[WSResponse](articleWithTags.slug)
    } yield {
      response.status.mustBe(OK)
      getBySlugResponse.status.mustBe(NOT_FOUND)
    }
  }

} 
Example 36
Source File: ArticleCreateTest.scala    From scala-play-realworld-example-app   with MIT License 5 votes vote down vote up
package articles

import java.time.Instant

import articles.models.ArticleWrapper
import articles.test_helpers.Articles
import commons.repositories.DateTimeProvider
import commons_test.test_helpers.RealWorldWithServerAndTestConfigBaseTest.RealWorldWithTestConfig
import commons_test.test_helpers.{FixedDateTimeProvider, RealWorldWithServerAndTestConfigBaseTest, WithArticleTestHelper, WithUserTestHelper}
import play.api.ApplicationLoader.Context
import play.api.libs.ws.WSResponse
import users.models.UserDetailsWithToken
import users.test_helpers.UserRegistrations

class ArticleCreateTest extends RealWorldWithServerAndTestConfigBaseTest with WithArticleTestHelper with WithUserTestHelper {

  val dateTime: Instant = Instant.now

  "Create article" should "create valid article without tags" in await {
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      newArticle = Articles.hotToTrainYourDragon.copy(tagList = Nil)
      response <- articleTestHelper.create[WSResponse](newArticle, userDetailsWithToken.token)
    } yield {
      response.status.mustBe(OK)
      val article = response.json.as[ArticleWrapper].article
      article.title.mustBe(newArticle.title)
      article.updatedAt.mustBe(dateTime)
      article.tagList.isEmpty.mustBe(true)
    }
  }

  it should "create valid article with dragons tag" in await {
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      newArticle = Articles.hotToTrainYourDragon
      response <- articleTestHelper.create[WSResponse](newArticle, userDetailsWithToken.token)
    } yield {
      response.status.mustBe(OK)
      val article = response.json.as[ArticleWrapper].article
      article.title.mustBe(newArticle.title)
      article.updatedAt.mustBe(dateTime)
      article.tagList.size.mustBe(1L)
    }
  }


  it should "create article and associate it with existing dragons tag" in await {
    def createFirstArticleToCreateDragonsTag(userDetailsWithToken: UserDetailsWithToken) = {
      articleTestHelper.create[WSResponse](Articles.hotToTrainYourDragon, userDetailsWithToken.token)
    }

    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      _ <- createFirstArticleToCreateDragonsTag(userDetailsWithToken)
      response <- articleTestHelper.create[WSResponse](Articles.hotToTrainYourDragon, userDetailsWithToken.token)
    } yield {
      response.status.mustBe(OK)
      val article = response.json.as[ArticleWrapper].article
      article.tagList.size.mustBe(1L)
    }
  }

  it should "create article and set author" in await {
    val registration = UserRegistrations.petycjaRegistration
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](registration)
      response <- articleTestHelper.create[WSResponse](Articles.hotToTrainYourDragon, userDetailsWithToken.token)
    } yield {
      response.status.mustBe(OK)
      val article = response.json.as[ArticleWrapper].article
      article.author.username.mustBe(registration.username)
    }
  }

  it should "generate slug based on title" in await {
    for {
      userDetailsWithToken <- userTestHelper.register[UserDetailsWithToken](UserRegistrations.petycjaRegistration)
      titlePart1 = "the"
      titlePart2 = "title"
      response <- articleTestHelper.create[WSResponse](Articles.hotToTrainYourDragon, userDetailsWithToken.token)
    } yield {
      response.status.mustBe(OK)
      val slug = response.json.as[ArticleWrapper].article.slug
      slug.contains(include(titlePart1).and(include(titlePart2)))
    }
  }

  override def createComponents: RealWorldWithTestConfig = {
    new RealWorldWithTestConfigWithFixedDateTimeProvider(new FixedDateTimeProvider(dateTime), context)
  }

}

class RealWorldWithTestConfigWithFixedDateTimeProvider(dtProvider: DateTimeProvider, context: Context)
  extends RealWorldWithTestConfig(context) {

  override lazy val dateTimeProvider: DateTimeProvider = dtProvider
}