play.api.libs.json.JsValue Scala Examples

The following examples show how to use play.api.libs.json.JsValue. 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: PhraseController.scala    From pujangga   with Apache License 2.0 5 votes vote down vote up
package controllers

import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsArray, JsValue, Json}
import play.api.mvc._
import IndonesianNLP._

@Singleton
class PhraseController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
  val phraseChunker = new IndonesianPhraseChunker

  def chunker: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      phraseChunker.doPhraseChunker(string)
    } match {
      case Success(chunked) =>

        val chunkListString = new ListBuffer[String]()
        val chunkListJs = new ListBuffer[JsValue]()

        for(chunk <- chunked.asScala) {
          for(phrase <- chunk) {
            chunkListString += phrase
            if(chunk.indexOf(phrase) % 2 == 1) {
              chunkListJs += Json.toJson(phrase)
            }
          }
        }
        val chunkMap = chunkListString.grouped(2).collect { case ListBuffer(k, v) => k -> v }.toMap

        Ok(Json.obj("status" -> "success", "data" -> Json.obj("map" -> Json.toJson(chunkMap), "list" -> JsArray(chunkListJs))))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }
} 
Example 2
Source File: KernelStatusSpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.content

import org.scalatest.{FunSpec, Matchers}
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, JsValue, Json}

class KernelStatusSpec extends FunSpec with Matchers {
  val kernelStatusJson: JsValue = Json.parse("""
  {
    "execution_state": "<STRING>"
  }
  """)

  val kernelStatus: KernelStatus = KernelStatus("<STRING>")

  describe("KernelStatus") {
    describe("#toTypeString") {
      it("should return correct type") {
        KernelStatus.toTypeString should be ("status")
      }
    }

    describe("implicit conversions") {
      it("should implicitly convert from valid json to a kernelStatus instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        kernelStatusJson.as[KernelStatus] should be (kernelStatus)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newKernelStatus = kernelStatusJson.asOpt[KernelStatus]

        newKernelStatus.get should be (kernelStatus)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val kernelStatusResults = kernelStatusJson.validate[KernelStatus]

        kernelStatusResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: KernelStatus) => valid
        ) should be (kernelStatus)
      }

      it("should implicitly convert from a kernelStatus instance to valid json") {
        Json.toJson(kernelStatus) should be (kernelStatusJson)
      }
    }
  }
} 
Example 3
Source File: CompleteReplySpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.content

import org.scalatest.{Matchers, FunSpec}
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, JsValue, Json}

class CompleteReplySpec extends FunSpec with Matchers {


  val completeReplyJson: JsValue = Json.parse("""
  {
    "matches": [],
    "cursor_start": 1,
    "cursor_end": 5,
    "metadata": {},
    "status": "<STRING>"
  }
  """)

  val completeReply: CompleteReply = CompleteReply(
    List(), 1, 5, Map(), "<STRING>", None, None, None
  )

  describe("CompleteReply") {
    describe("#toTypeString") {
      it("should return correct type") {
        CompleteReply.toTypeString should be ("complete_reply")
      }
    }

    describe("implicit conversions") {
      it("should implicitly convert from valid json to a CompleteReply instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        completeReplyJson.as[CompleteReply] should be (completeReply)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newCompleteReply = completeReplyJson.asOpt[CompleteReply]

        newCompleteReply.get should be (completeReply)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val CompleteReplyResults = completeReplyJson.validate[CompleteReply]

        CompleteReplyResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: CompleteReply) => valid
        ) should be (completeReply)
      }

      it("should implicitly convert from a CompleteReply instance to valid json") {
        Json.toJson(completeReply) should be (completeReplyJson)
      }
    }
  }

} 
Example 4
Source File: ConnectReplySpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.content

import org.scalatest.{FunSpec, Matchers}
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, JsValue, Json}

class ConnectReplySpec extends FunSpec with Matchers {


  val connectReplyJson: JsValue = Json.parse("""
  {
    "shell_port": 11111,
    "iopub_port": 22222,
    "stdin_port": 33333,
    "hb_port": 44444
  }
  """)

  val connectReply: ConnectReply = ConnectReply(11111,22222,33333,44444)

  describe("ConnectReply") {
    describe("#toTypeString") {
      it("should return correct type") {
        ConnectReply.toTypeString should be ("connect_reply")
      }
    }

    describe("implicit conversions") {
      it("should implicitly convert from valid json to a ConnectReply instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        connectReplyJson.as[ConnectReply] should be (connectReply)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newConnectReply = connectReplyJson.asOpt[ConnectReply]

        newConnectReply.get should be (connectReply)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val ConnectReplyResults = connectReplyJson.validate[ConnectReply]

        ConnectReplyResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: ConnectReply) => valid
        ) should be (connectReply)
      }

      it("should implicitly convert from a ConnectReply instance to valid json") {
        Json.toJson(connectReply) should be (connectReplyJson)
      }
    }
  }

} 
Example 5
Source File: HeaderSpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5

import org.scalatest.{Matchers, FunSpec}
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, Json, JsValue}

class HeaderSpec extends FunSpec with Matchers {
  val headerJson: JsValue = Json.parse("""
  {
    "msg_id": "<UUID>",
    "username": "<STRING>",
    "session": "<UUID>",
    "msg_type": "<STRING>",
    "version": "<FLOAT>"
  }
  """)

  val header: Header = Header(
    "<UUID>", "<STRING>", "<UUID>", "<STRING>", "<FLOAT>"
  )

  describe("Header") {
    describe("when null") {
      it("should implicitly convert to empty json") {
        val header: Header = null
        Json.toJson(header).toString() should be ("{}")
      }
    }

    describe("implicit conversions") {
      it("should implicitly convert from valid json to a header instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        headerJson.as[Header] should be (header)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newHeader = headerJson.asOpt[Header]

        newHeader.get should be (header)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val headerResults = headerJson.validate[Header]

        headerResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: Header) => valid
        ) should be (header)
      }

      it("should implicitly convert from a header instance to valid json") {
        Json.toJson(header) should be (headerJson)
      }
    }
  }
} 
Example 6
Source File: PlayJsonSupport.scala    From kafka-serde-scala   with Apache License 2.0 5 votes vote down vote up
package io.github.azhur.kafkaserdeplayjson

import java.nio.charset.StandardCharsets.UTF_8
import java.util

import io.github.azhur.kafkaserdeplayjson.PlayJsonSupport.PlayJsonError
import org.apache.kafka.common.errors.SerializationException
import org.apache.kafka.common.serialization.{ Deserializer, Serde, Serializer }
import play.api.libs.json.{ JsError, JsValue, Json, Reads, Writes }

import scala.language.implicitConversions
import scala.util.control.NonFatal

trait PlayJsonSupport {
  implicit def toSerializer[T <: AnyRef](
      implicit writes: Writes[T],
      printer: JsValue => String = Json.stringify
  ): Serializer[T] =
    new Serializer[T] {
      override def configure(configs: util.Map[String, _], isKey: Boolean): Unit = {}
      override def close(): Unit                                                 = {}
      override def serialize(topic: String, data: T): Array[Byte] =
        if (data == null) null
        else
          try printer(writes.writes(data)).getBytes(UTF_8)
          catch {
            case NonFatal(e) => throw new SerializationException(e)
          }
    }

  implicit def toDeserializer[T >: Null <: AnyRef: Manifest](
      implicit reads: Reads[T]
  ): Deserializer[T] =
    new Deserializer[T] {
      override def configure(configs: util.Map[String, _], isKey: Boolean): Unit = {}
      override def close(): Unit                                                 = {}
      override def deserialize(topic: String, data: Array[Byte]): T =
        if (data == null) null
        else
          reads
            .reads(Json.parse(new String(data, UTF_8)))
            .recoverTotal { e =>
              throw new SerializationException(PlayJsonError(e))
            }
    }

  implicit def toSerde[T >: Null <: AnyRef: Manifest](
      implicit writes: Writes[T],
      reads: Reads[T],
      printer: JsValue => String = Json.stringify
  ): Serde[T] =
    new Serde[T] {
      override def configure(configs: util.Map[String, _], isKey: Boolean): Unit = {}
      override def close(): Unit                                                 = {}
      override def serializer(): Serializer[T]                                   = toSerializer[T]
      override def deserializer(): Deserializer[T]                               = toDeserializer[T]
    }
}

object PlayJsonSupport extends PlayJsonSupport {
  final case class PlayJsonError(error: JsError) extends RuntimeException {
    override def getMessage: String =
      JsError.toJson(error).toString()
  }
} 
Example 7
Source File: Headers.scala    From jwt   with MIT License 5 votes vote down vote up
package io.igl.jwt

import play.api.libs.json.{JsString, JsValue}

trait HeaderValue extends JwtValue {
  val field: HeaderField
}

trait HeaderField extends JwtField {
  def attemptApply(value: JsValue): Option[HeaderValue]
}

case class Typ(value: String) extends HeaderValue {
  override val field: HeaderField = Typ
  override val jsValue: JsValue = JsString(value)
}

object Typ extends HeaderField {
  override def attemptApply(value: JsValue): Option[Typ] =
    value.asOpt[String].map(apply)

  override val name = "typ"
}

case class Alg(value: Algorithm) extends HeaderValue {
  override val field: HeaderField = Alg
  override val jsValue: JsValue = JsString(value.name)
}

object Alg extends HeaderField {
  override def attemptApply(value: JsValue): Option[Alg] =
    value.asOpt[String].flatMap(Algorithm.getAlgorithm).map(apply)

  override val name = "alg"
}

case object Cty extends HeaderField with HeaderValue {
  override def attemptApply(value: JsValue): Option[HeaderValue] =
    value.asOpt[String].map{case this.value => Cty}

  override val name = "cty"
  override val field: HeaderField = this
  override val value = "JWT"
  override val jsValue: JsValue = JsString(value)
} 
Example 8
Source File: DocumentActionsController.scala    From recogito2   with Apache License 2.0 5 votes vote down vote up
package controllers.my.directory.delete

import com.mohiva.play.silhouette.api.Silhouette
import controllers.{BaseController, HasPrettyPrintJSON, Security}
import javax.inject.{Inject, Singleton}
import play.api.Configuration
import play.api.libs.json.{Json, JsValue}
import play.api.mvc.ControllerComponents
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try
import services.RuntimeAccessLevel
import services.annotation.AnnotationService
import services.contribution.ContributionService
import services.document.DocumentService
import services.folder.FolderService
import services.generated.tables.records.DocumentRecord
import services.user.UserService

@Singleton
class DocumentActionsController @Inject() (
  val annotations: AnnotationService,
  val components: ControllerComponents,
  val contributions: ContributionService,
  val documents: DocumentService,
  val folders: FolderService,
  val silhouette: Silhouette[Security.Env],
  val users: UserService,
  val config: Configuration,
  implicit val ctx: ExecutionContext
) extends BaseController(components, config, users)
    with HasPrettyPrintJSON {

  
  def bulkUnshareDocuments() = silhouette.SecuredAction.async { implicit request => 
    val docIds = getDocIdPayload(request.body.asJson)
    val user = request.identity.username

    Future.sequence {
      docIds.map(documents.removeDocumentCollaborator(_, user))
    }.map { results =>
      val success = !results.exists(!_)
      if (success) Ok else BadRequest
    }
  }

} 
Example 9
Source File: OpenApiUtils.scala    From gospeak   with Apache License 2.0 5 votes vote down vote up
package gospeak.web.utils

import java.io.{File, FileNotFoundException}

import com.typesafe.config.{ConfigFactory, ConfigRenderOptions}
import play.api.libs.json.{JsValue, Json}
import gospeak.libs.scala.Extensions._

import scala.util.Try

object OpenApiUtils {
  val specPath = "app/gospeak/web/api/swagger/gospeak.openapi.conf"

  def loadSpec(): Try[JsValue] = {
    Try(new File(s"web/$specPath")).filterWith(_.exists(), f => new FileNotFoundException(f.getAbsolutePath))
      .orElse(Try(new File(specPath)).filterWith(_.exists(), f => new FileNotFoundException(f.getAbsolutePath)))
      .flatMap(loadSpec)
  }

  private def loadSpec(file: File): Try[JsValue] = {
    val spec = ConfigFactory.parseFile(file).resolve()
    val json = spec.root().render(ConfigRenderOptions.concise())
    Try(Json.parse(json))
  }
} 
Example 10
Source File: JSONBuilder.scala    From mimir   with Apache License 2.0 5 votes vote down vote up
package mimir.util;

import mimir.algebra.{PrimitiveValue,StringPrimitive,TypePrimitive};
import play.api.libs.json.JsString
import play.api.libs.json.JsArray
import play.api.libs.json.JsObject
import play.api.libs.json.JsValue
import play.api.libs.json.JsNumber
import play.api.libs.json.JsNull
import play.api.libs.json.JsBoolean
import mimir.algebra.NullPrimitive
import mimir.algebra.RowIdPrimitive
import mimir.algebra.IntPrimitive
import mimir.algebra.FloatPrimitive
import mimir.algebra.BoolPrimitive
import mimir.algebra.TimestampPrimitive
import mimir.algebra.DatePrimitive


object JSONBuilder {
	
	def list(content: Seq[Any]): String =
		listJs(content).toString()

  private def listJs(content: Seq[Any]): JsArray =
		JsArray(content.map { el => value(el) })
		
	def dict(content: Map[String,Any]): String =
		dictJs(content).toString()

	def dict(content: Seq[(String,Any)]): String =
		JsObject(content.map( (x) => x._1.toLowerCase() -> value(x._2))).toString()

  private def dictJs(content: Map[String,Any]): JsObject =
		JsObject(content.map { el => el._1 -> value(el._2) } )
		
	def string(content: String): String = {
		value(content).toString()
	}

	def int(content: Int): String = {
		value(content).toString()
	}

	def double(content: Double): String = {
		value(content).toString()
	}
	
	def boolean(content: Boolean): String = {
		value(content).toString()
	}

	def prim(content: PrimitiveValue) = {
		primJs(content).toString()
	}
	
	private def primJs(content: PrimitiveValue) = {
		content match {
			case StringPrimitive(s) => JsString(s)
			case TypePrimitive(t) => JsString(t.toString())
			case NullPrimitive() => JsNull
      case RowIdPrimitive(s) => JsString(s)
      case IntPrimitive(i) => JsNumber(i)
      case FloatPrimitive(f) => JsNumber(f)
      case BoolPrimitive(b) => JsBoolean(b)
      case TimestampPrimitive(_,_,_,_,_,_,_) => JsString(content.asString)
      case DatePrimitive(_,_,_) => JsString(content.asString)
      case _ =>  JsString(content.toString())
		}
	}
	
	private def value(content: Any): JsValue = {
		content match {
		  case s:String => {
		    try {
		      play.api.libs.json.Json.parse(s)
		    } catch {
		      case t: Throwable => JsString(s)
		    }
		  }
			case null => JsNull
      case i:Int => JsNumber(i)
      case f:Float => JsNumber(f)
      case l:Long => JsNumber(l)
      case d:Double => JsNumber(d)
      case b:Boolean => JsBoolean(b)
      case seq:Seq[Any] => listJs(seq)
      case map:Map[_,_] => dictJs(map.asInstanceOf[Map[String,Any]])
      case jsval:JsValue => jsval
      case prim:PrimitiveValue => primJs(prim)
			case _ =>  JsString(content.toString())
		}
	}
} 
Example 11
Source File: StemmerController.scala    From pujangga   with Apache License 2.0 5 votes vote down vote up
package controllers

import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import IndonesianNLP._

@Singleton
class StemmerController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
  val stem = new IndonesianStemmer

  def stemmer: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      stem.stemSentence(string)
    } match {
      case Success(stemmed) => Ok(Json.obj("status" -> "success", "data" -> stemmed))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }
} 
Example 12
Source File: NamedEntityController.scala    From pujangga   with Apache License 2.0 5 votes vote down vote up
package controllers

import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsArray, JsValue, Json}
import play.api.mvc._
import IndonesianNLP._

@Singleton
class NamedEntityController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
  val nETagger = new IndonesianNETagger

  def tagger: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      nETagger.extractNamedEntity(string)
    } match {
      case Success(extracted) =>

        val namedEntityList = new ListBuffer[JsValue]()

        for(tag <- extracted.asScala) {
          namedEntityList += Json.toJson(tag)
        }

        Ok(Json.obj("status" -> "success", "data" -> JsArray(namedEntityList)))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }
} 
Example 13
Source File: Word2VecController.scala    From pujangga   with Apache License 2.0 5 votes vote down vote up
package controllers

import scala.collection.mutable.ListBuffer
import scala.collection.JavaConverters._
import scala.util.{Failure, Success, Try}
import java.io.File
import javax.inject._
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import play.api.Configuration
import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer
import org.deeplearning4j.models.word2vec.Word2Vec

@Singleton
class Word2VecController @Inject() (cc: ControllerComponents, configuration: Configuration) extends AbstractController(cc) {
  val word2VecIdFile = new File(configuration.underlying.getString("models.word2vec"))
  val word2VecIdModel: Word2Vec = WordVectorSerializer.readWord2VecModel(word2VecIdFile)

  def nearestWords: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]
    val n = (request.body \ "n").as[Int]

    Try {
      word2VecIdModel.wordsNearest(string, n)
    } match {
      case Success(word2vec) =>
        val wordList = new ListBuffer[JsValue]()
        for(word <- word2vec.asScala) {
          wordList += Json.toJson(word)
        }
        Ok(Json.obj("status" -> "success", "data" -> wordList))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }

  def arithmetic: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string01 = (request.body \ "first_string").as[String]
    val string02 = (request.body \ "second_string").as[String]
    val string03 = (request.body \ "third_string").as[String]
    val n = (request.body \ "n").as[Int]

    Try {
      word2VecIdModel.wordsNearest(List(string01, string02).asJavaCollection,List(string03).asJavaCollection, n)
    } match {
      case Success(word2vec) =>
        val wordList = new ListBuffer[JsValue]()
        for(word <- word2vec.asScala) {
          wordList += Json.toJson(word)
        }
        Ok(Json.obj("status" -> "success", "data" -> wordList))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }

  def similarityWords: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string01 = (request.body \ "first_string").as[String]
    val string02 = (request.body \ "second_string").as[String]

    Try {
      word2VecIdModel.similarity(string01, string02)
    } match {
      case Success(word2vec) => Ok(Json.obj("status" -> "success", "data" -> word2vec))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }
} 
Example 14
Source File: package.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol

//import akka.zeromq.ZMQMessage
import org.apache.toree.kernel.protocol.v5._
import org.apache.toree.kernel.protocol.v5.content.{CompleteRequest, ExecuteRequest}
import play.api.libs.json.{JsValue, Json}

package object v5Test {
  //  The header for the message
  val MockHeader : Header = Header("<UUID>","<USER>","<SESSION>",
    MessageType.Outgoing.ClearOutput.toString, "<VERSION>")
  //  The parent header for the message
  val MockParenHeader: Header = Header("<PARENT-UUID>","<PARENT-USER>","<PARENT-SESSION>",
    MessageType.Outgoing.ClearOutput.toString, "<PARENT-VERSION>")
  //  The actual kernel message
  val MockKernelMessage : KernelMessage = KernelMessage(Seq("<ID>".getBytes), "<SIGNATURE>", MockHeader,
    MockParenHeader, Metadata(), "<CONTENT>")
  //  Use the implicit to convert the KernelMessage to ZMQMessage
  //val MockZMQMessage : ZMQMessage = MockKernelMessage

  val MockExecuteRequest: ExecuteRequest =
    ExecuteRequest("spark code", false, true, Map(), false)
  val MockExecuteRequestKernelMessage = MockKernelMessage.copy(
    contentString =  Json.toJson(MockExecuteRequest).toString
  )
  val MockKernelMessageWithBadExecuteRequest = new KernelMessage(
    Seq[Array[Byte]](), "test message", MockHeader, MockParenHeader, Metadata(),
    """
        {"code" : 124 }
    """
  )
  val MockCompleteRequest: CompleteRequest = CompleteRequest("", 0)
  val MockCompleteRequestKernelMessage: KernelMessage = MockKernelMessage.copy(contentString = Json.toJson(MockCompleteRequest).toString)
  val MockKernelMessageWithBadJSON: KernelMessage = MockKernelMessage.copy(contentString = "inval1d")
} 
Example 15
Source File: SentencesController.scala    From pujangga   with Apache License 2.0 5 votes vote down vote up
package controllers

import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import IndonesianNLP._

@Singleton
class SentencesController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
  val sentenceTokenizer = new IndonesianSentenceTokenizer
  val sentenceDetector = new IndonesianSentenceDetector
  val refResolution = new IndonesianReferenceResolution

  def tokenizer: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      sentenceTokenizer.tokenizeSentence(string)
    } match {
      case Success(tokenized) =>
        val tokenList = new ListBuffer[JsValue]()

        for(token <- tokenized.asScala) {
          tokenList += Json.toJson(token)
        }

        Ok(Json.obj("status" -> "success", "data" -> tokenList))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }

  def tokenizerComposite: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      sentenceTokenizer.tokenizeSentenceWithCompositeWords(string)
    } match {
      case Success(tokenized) =>
        val tokenList = new ListBuffer[JsValue]()

        for(token <- tokenized.asScala) {
          tokenList += Json.toJson(token)
        }

        Ok(Json.obj("status" -> "success", "data" -> tokenList))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }

  def splitter: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      sentenceDetector.splitSentence(string)
    } match {
      case Success(split) =>
        val sentenceList = new ListBuffer[JsValue]()

        for(sentence <- split.asScala) {
          sentenceList += Json.toJson(sentence)
        }

        Ok(Json.obj("status" -> "success", "data" -> sentenceList))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }
} 
Example 16
Source File: FormalizerController.scala    From pujangga   with Apache License 2.0 5 votes vote down vote up
package controllers

import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import IndonesianNLP._

@Singleton
class FormalizerController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {

  val formalize = new IndonesianSentenceFormalization

  def formalizer: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      formalize.formalizeSentence(string)
    } match {
      case Success(formalized) => Ok(Json.obj("status" -> "success", "data" -> formalized))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }
} 
Example 17
Source File: PartOfSpeechController.scala    From pujangga   with Apache License 2.0 5 votes vote down vote up
package controllers

import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsArray, JsValue, Json}
import play.api.mvc._
import IndonesianNLP._

@Singleton
class PartOfSpeechController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {

  def tagger: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      IndonesianPOSTagger.doPOSTag(string)
    } match {
      case Success(tagged) =>
        val tagListString = new ListBuffer[String]()
        val tagListJs = new ListBuffer[JsValue]()

        for(tag <- tagged.asScala) {
          for(word <- tag) {
            tagListString += word
            if(tag.indexOf(word) % 2 == 1) {
              tagListJs += Json.toJson(word)
            }
          }
        }
        val tagMap = tagListString.grouped(2).collect { case ListBuffer(k, v) => k -> v }.toMap

        Ok(Json.obj("status" -> "success", "data" -> Json.obj("map" -> Json.toJson(tagMap), "list" -> JsArray(tagListJs))))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }
} 
Example 18
Source File: StopwordsController.scala    From pujangga   with Apache License 2.0 5 votes vote down vote up
package controllers

import scala.util.{Failure, Success, Try}
import javax.inject._
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import IndonesianNLP._

@Singleton
class StopwordsController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {
  val formalizer = new IndonesianSentenceFormalization

  def remover: Action[JsValue] = Action(parse.json) { implicit request: Request[JsValue] =>
    val string = (request.body \ "string").as[String]

    Try {
      formalizer.initStopword()
      formalizer.deleteStopword(string)
    } match {
      case Success(removed) => Ok(Json.obj("status" -> "success", "data" -> removed))
      case Failure(failure) => InternalServerError(Json.obj("status" -> "error", "message" -> failure.toString))
    }
  }
} 
Example 19
Source File: DnsChangeNotices.scala    From vinyldns   with Apache License 2.0 5 votes vote down vote up
package models

import com.typesafe.config.Config
import models.DnsChangeNoticeType.DnsChangeNoticeType
import models.DnsChangeStatus.DnsChangeStatus
import play.api.libs.json.{JsValue, Json, Writes}
import play.api.ConfigLoader
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.EnumerationReader._

import scala.collection.JavaConverters._

case class DnsChangeNotices(notices: JsValue)

object DnsChangeNotices {
  implicit val dnsChangeNoticeWrites: Writes[DnsChangeNotice] = Json.writes[DnsChangeNotice]
  implicit val configLoader: ConfigLoader[DnsChangeNotices] =
    new ConfigLoader[DnsChangeNotices] {
      def load(config: Config, path: String): DnsChangeNotices = {
        val notices = config
          .getConfigList(path)
          .asScala
          .map(formatDnsChangeNotice)
        DnsChangeNotices(Json.toJson(notices))
      }
    }

  def formatDnsChangeNotice(config: Config): DnsChangeNotice = {
    val status = config.as[DnsChangeStatus]("status")
    val alertType = config.as[DnsChangeNoticeType]("alertType")
    val text = config.getString("text")
    val hrefText = config.getOrElse[String]("hrefText", "")
    val href = config.getOrElse[String]("href", "")
    DnsChangeNotice(status, alertType, text, hrefText, href)
  }
}
case class DnsChangeNotice(
    status: DnsChangeStatus,
    alertType: DnsChangeNoticeType,
    text: String,
    hrefText: String,
    href: String
)

object DnsChangeStatus extends Enumeration {
  type DnsChangeStatus = Value
  val Cancelled, Complete, Failed, PartialFailure, PendingProcessing, PendingReview, Rejected,
      Scheduled = Value
}

object DnsChangeNoticeType extends Enumeration {
  type DnsChangeNoticeType = Value
  val info, success, warning, danger = Value
} 
Example 20
Source File: JsonProducer.scala    From skafka   with MIT License 5 votes vote down vote up
package com.evolutiongaming.skafka.producer

import cats.Applicative
import com.evolutiongaming.catshelper.FromTry
import com.evolutiongaming.jsonitertool.PlayJsonJsoniter
import com.evolutiongaming.skafka.{ToBytes, Topic}
import play.api.libs.json.{JsValue, Json}

import scala.util.Try

trait JsonProducer[F[_]] {
  def apply(record: ProducerRecord[String, JsValue]): F[F[RecordMetadata]]
}

object JsonProducer {

  def empty[F[_] : Applicative : FromTry]: JsonProducer[F] = apply(Producer.Send.empty[F])

  implicit def jsValueToBytes[F[_] : FromTry]: ToBytes[F, JsValue] = {
    (value: JsValue, _: Topic) => FromTry[F].apply {
      Try(PlayJsonJsoniter.serialize(value)).orElse {
        Try(Json.toBytes(value))
      }
    }
  }

  def apply[F[_] : FromTry](send: Producer.Send[F]): JsonProducer[F] = (record: ProducerRecord[String, JsValue]) => send(record)
} 
Example 21
Source File: FitsValuesIgnoringArrayOrderSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.doc._104

import org.scalawebtest.integration.json.{FitsTypeMismatchBehavior, ScalaWebTestJsonBaseSpec}
import play.api.libs.json.{JsValue, Json}

class FitsValuesIgnoringArrayOrderSpec extends ScalaWebTestJsonBaseSpec with FitsTypeMismatchBehavior {
  config.useBaseUri("http://localhost:9090")
  path = "/dijkstra.json"

  def dijkstra: JsValue = Json.parse(webDriver.getPageSource)

  "Dijkstra" should "contain the correct firstName and lastName and the correct universities in any order" in {
    dijkstra fits valuesIgnoringArrayOrder of
      """
        |{
        | "name": "Dijkstra",
        | "firstName": "Edsger",
        | "universities":
        | [
        |   { "name": "Universität Leiden","begin": 1948, "end": 1956 },
        |   { "name": "University of Texas at Austin", "begin": 1984, "end": 1999 },
        |   { "name": "Technische Universiteit Eindhoven", "begin": 1962, "end": 1984 },
        |   { "name": "Mathematisch Centrum Amsterdam", "begin": 1951, "end": 1959 }
        | ]
        |}
      """.stripMargin
  }
} 
Example 22
Source File: PagePropertiesSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.aem

import org.scalawebtest.aem.PageProperties
import org.scalawebtest.core.gauge.HtmlGauge.fits
import org.scalawebtest.integration.extensions.aem.AemModuleScalaWebTestBaseSpec
import play.api.libs.json.{JsObject, JsValue}

class PagePropertiesSpec extends AemModuleScalaWebTestBaseSpec with PageProperties {
  path = "/aem/geometrixx-outdoors/en/company/our-story.html"

  case class ContentPage(pageProperties: JsValue) {
    def sidebarParsys = (pageProperties \ "jcr:content" \ "sidebar").as[JsObject]
    def jcrTitle =(pageProperties \ "jcr:content" \ "jcr:title").as[String]
  }

  "Content page" should "have the correct title (pageProperties example)" in {
    val contentPage = ContentPage(pageProperties)
    fits(<h1>{contentPage.jcrTitle}</h1>)
  }
  it should "have the correct title (jcrContent example" in {
    fits(<h1>{(pageProperties \ "jcr:content" \ "jcr:title").as[String]}</h1>)
  }
  it should "have the correct title (page model example" in {
    val individualPage = ContentPage(pageProperties)
    fits(<h1>{individualPage.jcrTitle}</h1>)
  }
} 
Example 23
Source File: FindByResourceTypeSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.aem

import org.scalawebtest.aem.PageProperties
import org.scalawebtest.core.gauge.HtmlGauge.fits
import org.scalawebtest.integration.extensions.aem.AemModuleScalaWebTestBaseSpec
import play.api.libs.json.{JsObject, JsValue}

class FindByResourceTypeSpec extends AemModuleScalaWebTestBaseSpec with PageProperties {
  path = "/aem/geometrixx-outdoors/en/company/our-story.html"

  case class ContentPage(pageProperties: JsValue) {
    def sidebarParsys: JsObject = (pageProperties \ "jcr:content" \ "sidebar").as[JsObject]
    def sidebarImageAltText: String = {
      val sidebarImages = sidebarParsys findByResourceType "foundation/components/image"
      (sidebarImages.head \ "alt").as[String]
    }
  }

  "Content page" should "have the correct alt text in the sidebar image" in {
    val contentPage = ContentPage(pageProperties)
    fits(<img alt={contentPage.sidebarImageAltText}></img>)
  }
} 
Example 24
Source File: JsonGaugeBehavior.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.browser.behaviors

import org.scalawebtest.core.IntegrationFlatSpec
import play.api.libs.json.{JsValue, Json}
import org.scalawebtest.json.JsonGauge._

trait JsonGaugeBehavior {
  self: IntegrationFlatSpec =>

  def aJsonGauge(): Unit = {
    def json: JsValue = {
      Json.parse(webDriver.getPageSource)
    }

    it should "be capable to handle JSON responses (some browsers wrap this in HTML and need special treatment)" in {
      navigateTo("/jsonResponse.json.jsp")
      json fits values of
        """
          |{
          |  "name": "Dijkstra",
          |  "firstName": "Edsger",
          |  "yearOfBirth": 1930,
          |  "theories": [
          |    "shortest path",
          |    "graph theory"
          |  ],
          |  "isTuringAwardWinner": true,
          |  "universities": [
          |    {
          |      "name": "Universität Leiden",
          |      "begin": 1948,
          |      "end": 1956
          |    },
          |    {
          |      "name": "Mathematisch Centrum Amsterdam",
          |      "begin": 1951,
          |      "end": 1959
          |    },
          |    {
          |      "name": "Technische Universiteit Eindhoven",
          |      "begin": 1962,
          |      "end": 1984
          |    },
          |    {
          |      "name": "University of Texas at Austin",
          |      "begin": 1984,
          |      "end": 1999
          |    }
          |  ],
          |  "falseTheories": null
          |}""".stripMargin
    }
  }
} 
Example 25
Source File: JsonGaugeObjectSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.json

import org.scalawebtest.integration.ScalaWebTestBaseSpec
import org.scalawebtest.json.JsonGauge._
import play.api.libs.json.{JsValue, Json}

class JsonGaugeObjectSpec extends ScalaWebTestBaseSpec {
  path = "/jsonResponse.json.jsp"
  def dijkstra: JsValue = Json.parse(webDriver.getPageSource)

  "Fits types" should "report success, when the json gauge contains the same values as the response it is tested against" in {
    dijkstra fits values of
      """{
        | "name": "Dijkstra",
        | "firstName": "Edsger",
        | "yearOfBirth": 1930,
        | "isTuringAwardWinner": true,
        | "theories": [
        |   "shortest path",
        |   "graph theory"
        | ]
        |}
      """.stripMargin
  }
} 
Example 26
Source File: ExampleApp.scala    From akka-http-json   with Apache License 2.0 5 votes vote down vote up
package de.heikoseeberger.akkahttpplayjson

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.server.Directives
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.scaladsl.Source
import play.api.libs.json.{ Format, JsValue, Json }

import scala.concurrent.Await
import scala.concurrent.duration._
import scala.io.StdIn

object ExampleApp {

  final object Foo {
    implicit val fooFormat: Format[Foo] = Json.format[Foo]
  }
  final case class Foo(bar: String)

  def main(args: Array[String]): Unit = {
    implicit val system = ActorSystem()

    Http().bindAndHandle(route, "127.0.0.1", 8000)

    StdIn.readLine("Hit ENTER to exit")
    Await.ready(system.terminate(), Duration.Inf)
  }

  def route(implicit sys: ActorSystem) = {
    import Directives._
    import PlayJsonSupport._

    implicit val prettyPrint: JsValue => String = Json.prettyPrint

    pathSingleSlash {
      post {
        entity(as[Foo]) { foo =>
          complete {
            foo
          }
        }
      }
    } ~ pathPrefix("stream") {
      post {
        entity(as[SourceOf[Foo]]) { fooSource: SourceOf[Foo] =>
          complete(fooSource.throttle(1, 2.seconds))
        }
      } ~ get {
        pathEndOrSingleSlash {
          complete(
            Source(0 to 5)
              .throttle(1, 1.seconds)
              .map(i => Foo(s"bar-$i"))
          )
        } ~ pathPrefix("remote") {
          onSuccess(Http().singleRequest(HttpRequest(uri = "http://localhost:8000/stream"))) {
            response => complete(Unmarshal(response).to[SourceOf[Foo]])
          }
        }
      }
    }
  }
} 
Example 27
Source File: ElasticJsonDataStoreTest.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package specs.elastic.store

import elastic.api.Elastic
import env.{DbDomainConfig, DbDomainConfigDetails, ElasticConfig}
import org.scalactic.source.Position
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll}
import play.api.libs.json.JsValue
import store.AbstractJsonDataStoreTest
import store.elastic._

class ElasticJsonDataStoreTest extends AbstractJsonDataStoreTest("Elastic") with BeforeAndAfter with BeforeAndAfterAll {


  private val config = ElasticConfig("localhost", 9210, "http", None, None, true)
  val elastic: Elastic[JsValue] = ElasticClient(config, system)

  override def dataStore(dataStore: String): ElasticJsonDataStore = ElasticJsonDataStore(
    elastic, config, DbDomainConfig(env.Elastic, DbDomainConfigDetails(dataStore, None), None)
  )

  override protected def before(fun: => Any)(implicit pos: Position): Unit = {
    super.before(fun)
    cleanUpElastic
  }

  private def cleanUpElastic = {
    import _root_.elastic.implicits._
    import _root_.elastic.codec.PlayJson._
    elastic.deleteIndex("*").futureValue
  }

  override protected def afterAll(): Unit = {
    super.afterAll()
    cleanUpElastic
  }
} 
Example 28
Source File: ExperimentVariantEventElasticServiceTest.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package specs.elastic.abtesting

import domains.abtesting.events.impl.ExperimentVariantEventElasticService
import domains.abtesting.AbstractExperimentServiceTest
import domains.abtesting.events.ExperimentVariantEventService
import elastic.api.Elastic
import env.{DbDomainConfig, DbDomainConfigDetails, ElasticConfig}
import org.scalactic.source.Position
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll}
import play.api.libs.json.JsValue
import store.elastic.ElasticClient

class ExperimentVariantEventElasticServiceTest
    extends AbstractExperimentServiceTest("Elastic")
    with BeforeAndAfter
    with BeforeAndAfterAll {

  private val config            = ElasticConfig("localhost", 9210, "http", None, None, true)
  val elastic: Elastic[JsValue] = ElasticClient(config, system)

  override def dataStore(name: String): ExperimentVariantEventService.Service = ExperimentVariantEventElasticService(
    elastic,
    config,
    DbDomainConfig(env.Elastic, DbDomainConfigDetails(name, None), None)
  )

  override protected def before(fun: => Any)(implicit pos: Position): Unit = {
    cleanUpElastic
    super.before(fun)
  }

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

  private def cleanUpElastic = {
    import _root_.elastic.codec.PlayJson._
    elastic.deleteIndex("*").futureValue
  }

} 
Example 29
Source File: ElasticClient.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package store.elastic

import akka.actor.ActorSystem
import elastic.api.Elastic
import elastic.client.{ElasticClient => AkkaClient}
import env.ElasticConfig
import libs.logs.IzanamiLogger
import play.api.libs.json.JsValue

object ElasticClient {

  def apply(elasticConfig: ElasticConfig, actorSystem: ActorSystem): Elastic[JsValue] = {
    IzanamiLogger.info(s"Creating elastic client $elasticConfig")
    (
      for {
        user     <- elasticConfig.user
        password <- elasticConfig.password
      } yield
        AkkaClient[JsValue](elasticConfig.host,
                            elasticConfig.port,
                            elasticConfig.scheme,
                            user = user,
                            password = password)(actorSystem)
    ) getOrElse AkkaClient[JsValue](elasticConfig.host, elasticConfig.port, elasticConfig.scheme)(actorSystem)
  }

} 
Example 30
Source File: HomeController.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package controllers

import controllers.actions.AuthContext
import domains.auth.AuthInfo
import domains.user.{User, UserNoPasswordInstances}
import env.{Env, Oauth2Config}
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.mvc._

class HomeController(_env: Env, AuthAction: ActionBuilder[AuthContext, AnyContent], cc: ControllerComponents)
    extends AbstractController(cc) {

  private val maybeOauth2Config: Option[Oauth2Config] = _env.izanamiConfig.oauth2.filter(_.enabled)

  private lazy val userManagementMode: String =
    (_env.izanamiConfig.filter, maybeOauth2Config) match {
      case (_, Some(c)) if c.izanamiManagedUser => "OAuth"
      case (_: env.Default, _)                  => "Izanami"
      case _                                    => "None"
    }
  private lazy val enabledApikeyManagement: Boolean = _env.izanamiConfig.filter match {
    case _: env.Default => true
    case _              => false
  }
  private lazy val baseURL: String             = _env.baseURL
  private lazy val confirmationDialog: Boolean = _env.izanamiConfig.confirmationDialog
  private lazy val logoutUrl: String = if (_env.izanamiConfig.logout.url.startsWith("http")) {
    _env.izanamiConfig.logout.url
  } else {
    s"$baseURL${_env.izanamiConfig.logout.url}"
  }

  private val p: Package      = getClass.getPackage
  private val version: String = p.getImplementationVersion

  def index() = AuthAction { ctx =>
    ctx.auth match {
      case Some(_) =>
        Ok(
          views.html
            .index(_env,
                   baseURL,
                   logoutUrl,
                   confirmationDialog,
                   userManagementMode,
                   enabledApikeyManagement,
                   toJson(ctx.auth),
                   version)
        )
      case None =>
        Redirect(s"$baseURL/login")
    }
  }

  def login() = AuthAction { ctx =>
    maybeOauth2Config match {
      case Some(_) =>
        Redirect(controllers.routes.OAuthController.appLoginPage())
      case _ =>
        Ok(
          views.html.index(_env,
                           baseURL,
                           logoutUrl,
                           confirmationDialog,
                           userManagementMode,
                           enabledApikeyManagement,
                           toJson(ctx.auth),
                           version)
        )
    }
  }

  def logout() = Action { _ =>
    maybeOauth2Config match {
      case Some(_) =>
        Redirect(controllers.routes.OAuthController.appLogout())
      case _ =>
        Redirect(s"${_env.baseURL}/login").withCookies(Cookie(name = _env.cookieName, value = "", maxAge = Some(0)))
    }
  }

  def otherRoutes(anyPath: String) = index()

  private def toJson(auth: Option[AuthInfo.Service]): JsValue = auth match {
    case Some(u: User) => UserNoPasswordInstances.format.writes(u).as[JsObject] - "id"
    case _             => Json.obj()
  }
} 
Example 31
Source File: EventsController.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package controllers

import akka.actor.ActorSystem
import controllers.actions.SecuredAuthContext
import domains.Domain.Domain
import domains.events.{EventStore, EventStoreContext}
import play.api.libs.EventSource
import play.api.libs.EventSource.{EventDataExtractor, EventIdExtractor, EventNameExtractor}
import play.api.libs.json.{JsString, Json}
import play.api.mvc.{AbstractController, ActionBuilder, AnyContent, ControllerComponents}
import libs.http.HttpContext
import akka.stream.scaladsl.Flow
import scala.util.Success
import scala.util.Failure
import libs.logs.IzanamiLogger
import java.time.LocalDateTime
import play.api.libs.json.JsValue
import scala.concurrent.duration.DurationDouble
import domains.auth.AuthInfo
import domains.Key

class EventsController(system: ActorSystem,
                       AuthAction: ActionBuilder[SecuredAuthContext, AnyContent],
                       cc: ControllerComponents)(implicit r: HttpContext[EventStoreContext])
    extends AbstractController(cc) {

  import libs.http._
  import domains.events.Events._
  import system.dispatcher

  private implicit val nameExtractor =
    EventNameExtractor[IzanamiEvent](_ => None) //Some(event.`type`))
  private implicit val idExtractor = EventIdExtractor[IzanamiEvent](event => Some(s"${event._id}")) //Some(event.key.key))
  private implicit val dataExtractor =
    EventDataExtractor[IzanamiEvent](event => Json.stringify(event.toJson))

  def allEvents(patterns: String, domains: String) =
    events(domains.split(",").toIndexedSeq, patterns)

  def eventsForADomain(domain: String, patterns: String) =
    events(domain.split(",").toIndexedSeq, patterns)

  val logEvent = Flow[IzanamiEvent].map { event =>
    event
  }

  case class KeepAliveEvent() extends IzanamiEvent {
    val _id: Long                          = 0
    val domain: Domain                     = domains.Domain.Unknown
    val authInfo: Option[AuthInfo.Service] = None
    val key: Key                           = Key("na")
    def timestamp: LocalDateTime           = LocalDateTime.now()
    val `type`: String                     = "KEEP_ALIVE"
    val payload: JsValue                   = Json.obj()
  }

  val keepAlive = Flow[IzanamiEvent].keepAlive(30.seconds, () => KeepAliveEvent())

  // TODO abilitations
  private def events[T <: IzanamiEvent](domains: Seq[String], patterns: String) =
    AuthAction.asyncTask[EventStoreContext] { ctx =>
      val allPatterns: Seq[String] = ctx.authorizedPatterns ++ patterns
        .split(",")
        .toList

      val lastEventId = ctx.request.headers.get("Last-Event-ID").map(_.toLong)
      val allDomains  = domains.map(JsString).flatMap(_.validate[Domain].asOpt)

      EventStore
        .events(allDomains, allPatterns, lastEventId)
        .map { source =>
          val eventSource = (source via keepAlive via logEvent via EventSource.flow).watchTermination() { (_, fDone) =>
            fDone.onComplete {
              case Success(_) =>
                IzanamiLogger.debug("SSE disconnected")
              case Failure(e) =>
                IzanamiLogger.error("Error during SSE ", e)
            }
            fDone
          }
          Ok.chunked(eventSource).as("text/event-stream")
        }
    }

} 
Example 32
Source File: AuthController.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package controllers

import com.auth0.jwt.algorithms.Algorithm
import domains.{AuthorizedPatterns, Key}
import domains.user.{IzanamiUser, User, UserContext, UserService}
import env.{DefaultFilter, Env}
import libs.crypto.Sha
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.mvc._
import store.Query
import zio.{Runtime, ZIO}
import libs.http.HttpContext

case class Auth(userId: String, password: String)

object Auth {
  implicit val format = Json.format[Auth]
}

class AuthController(_env: Env, cc: ControllerComponents)(implicit R: HttpContext[UserContext])
    extends AbstractController(cc) {

  import domains.user.UserNoPasswordInstances._
  import cats.implicits._
  import libs.http._

  lazy val _config: DefaultFilter = _env.izanamiConfig.filter match {
    case env.Default(config) => config
    case _                   => throw new RuntimeException("Wrong config")
  }
  lazy val algorithm: Algorithm = Algorithm.HMAC512(_config.sharedKey)

  def authenticate: Action[JsValue] = Action.asyncZio[UserContext](parse.json) { req =>
    val auth: Auth = req.body.as[Auth]

    UserService
      .getByIdWithoutPermissions(Key(auth.userId))
      .mapError(_ => InternalServerError(""))
      .flatMap {
        case Some(user: User) =>
          ZIO.succeed {
            user match {
              case IzanamiUser(_, _, _, Some(password), _, _) if password === Sha.hexSha512(auth.password) =>
                val token: String = User.buildToken(user, _config.issuer, algorithm)

                Ok(Json.toJson(user).as[JsObject] - "password")
                  .withCookies(Cookie(name = _env.cookieName, value = token))
              case _ =>
                Forbidden
            }
          }
        case None =>
          UserService
            .countWithoutPermissions(Query.oneOf("*"))
            .map {
              case count
                  if count === 0 && auth.userId === _env.izanamiConfig.user.initialize.userId && auth.password === _env.izanamiConfig.user.initialize.password => {
                val userId = _env.izanamiConfig.user.initialize.userId
                val user: User = IzanamiUser(id = userId,
                                             name = userId,
                                             email = s"[email protected]",
                                             password = None,
                                             admin = true,
                                             authorizedPatterns = AuthorizedPatterns.All)

                val token: String = User.buildToken(user, _config.issuer, algorithm)

                Ok(Json.toJson(user).as[JsObject] ++ Json.obj("changeme" -> true))
                  .withCookies(Cookie(name = _env.cookieName, value = token))
              }
              case _ =>
                Forbidden
            }
            .mapError(_ => InternalServerError(""))
      }
  }

} 
Example 33
Source File: ApiErrors.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package controllers.dto.error

import cats.kernel.Monoid
import play.api.libs.json.{JsValue, Json}
import play.api.mvc.{Result, Results}
import domains.errors.{
  DataShouldExists,
  DataShouldNotExists,
  ErrorMessage,
  IdMustBeTheSame,
  InvalidCopyKey,
  IzanamiError,
  IzanamiErrors,
  Unauthorized,
  ValidationError
}

case class ApiError(message: String, args: List[String])

object ApiError {
  implicit val format = Json.format[ApiError]
}

case class ApiErrors(errors: List[ApiError], fieldErrors: Map[String, List[ApiError]]) {
  def toJson: JsValue = Json.toJson(this)(ApiErrors.format)
}

object ApiErrors {
  implicit val format = Json.format[ApiErrors]

  import cats.implicits._

  implicit val monoid: Monoid[ApiErrors] = new Monoid[ApiErrors] {
    override def empty = ApiErrors(List.empty, Map.empty)
    override def combine(x: ApiErrors, y: ApiErrors): ApiErrors = {
      val errors      = x.errors ++ y.errors
      val fieldErrors = (x.fieldErrors, y.fieldErrors).combineAll
      ApiErrors(errors, fieldErrors)
    }
  }

  def fromErrors(errors: List[IzanamiError]): ApiErrors =
    errors.foldMap {
      case ValidationError(errors, fieldErrors) =>
        ApiErrors(
          errors.toList.map { case e: ErrorMessage => ApiError(e.message, e.args.toList) },
          fieldErrors.view
            .mapValues(_.map { case e: ErrorMessage => ApiError(e.message, e.args.toList) })
            .toMap
        )
      case InvalidCopyKey(id) => error("error.id.copy.invalid", id.key)
      case IdMustBeTheSame(fromObject, inParam) =>
        error("error.id.not.the.same", fromObject.key, inParam.key)
      case DataShouldExists(id)    => error("error.data.missing", id.key)
      case DataShouldNotExists(id) => error("error.data.exists", id.key)
      case Unauthorized(id)        => error("error.data.unauthorized", id.map(_.key).toSeq: _*)
    }

  def toHttpResult(errors: IzanamiErrors): Result = {
    val forbiddens: List[Unauthorized] = errors.toList.collect { case u: Unauthorized => u }
    if (forbiddens.isEmpty) {
      Results.BadRequest(Json.toJson(fromErrors(errors.toList)))
    } else {
      Results.Forbidden(Json.toJson(forbiddens.foldMap {
        case Unauthorized(id) => error("error.data.unauthorized", id.map(_.key).toSeq: _*)
      }))
    }
  }

  def error(message: String, args: String*): ApiErrors =
    ApiErrors(List(ApiError(message, args.toList)), Map.empty)

} 
Example 34
Source File: SearchController.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package controllers

import akka.actor.ActorSystem
import akka.stream.scaladsl.{GraphDSL, Interleave, Sink, Source}
import akka.stream.{ActorMaterializer, SourceShape}
import controllers.actions.SecuredAuthContext
import domains.abtesting.ExperimentService
import domains.config.ConfigService
import domains.feature.FeatureService
import domains.script.GlobalScriptService
import domains.configuration.GlobalContext
import play.api.libs.json.{JsArray, JsValue, Json}
import play.api.mvc.{AbstractController, ActionBuilder, AnyContent, ControllerComponents}
import store.Query
import zio.{Runtime, ZIO}
import libs.http.HttpContext

class SearchController(AuthAction: ActionBuilder[SecuredAuthContext, AnyContent], cc: ControllerComponents)(
    implicit system: ActorSystem,
    R: HttpContext[GlobalContext]
) extends AbstractController(cc) {

  import libs.http._

  def search(pattern: String, features: Boolean, configs: Boolean, experiments: Boolean, scripts: Boolean) =
    AuthAction.asyncTask[GlobalContext] { ctx =>
      val query: Query = Query.oneOf(ctx.authorizedPatterns).and(pattern.split(",").toList)

      for {
        featuresRes <- if (features)
                        FeatureService
                          .findByQuery(query, 1, 10)
                          .map(_.results.map(value => Json.obj("type" -> "features", "id" -> Json.toJson(value.id))))
                          .map(value => Source(value.toList))
                      else ZIO.succeed(Source.empty[JsValue])

        configsRes <- if (configs)
                       ConfigService
                         .findByQuery(query, 1, 10)
                         .map(
                           _.results.map(value => Json.obj("type" -> "configurations", "id" -> Json.toJson(value.id)))
                         )
                         .map(value => Source(value.toList))
                     else ZIO.succeed(Source.empty[JsValue])

        experimentsRes <- if (experiments)
                           ExperimentService
                             .findByQuery(query, 1, 10)
                             .map(
                               _.results.map(value => Json.obj("type" -> "experiments", "id" -> Json.toJson(value.id)))
                             )
                             .map(value => Source(value.toList))
                         else ZIO.succeed(Source.empty[JsValue])

        scriptsRes <- if (scripts)
                       GlobalScriptService
                         .findByQuery(query, 1, 10)
                         .map(_.results.map(value => Json.obj("type" -> "scripts", "id" -> Json.toJson(value.id))))
                         .map(value => Source(value.toList))
                     else ZIO.succeed(Source.empty[JsValue])

        res <- ZIO.fromFuture { implicit ec =>
                val all = Source.fromGraph(GraphDSL.create() { implicit builder =>
                  import GraphDSL.Implicits._

                  val interleave = builder.add(Interleave[JsValue](4, 1))
                  featuresRes ~> interleave.in(0)
                  configsRes ~> interleave.in(1)
                  experimentsRes ~> interleave.in(2)
                  scriptsRes ~> interleave.in(3)

                  SourceShape(interleave.out)
                })
                all.take(10).runWith(Sink.seq) map { jsons =>
                  Ok(JsArray(jsons))
                }
              }
      } yield res
    }

} 
Example 35
Source File: package.scala    From akka-cluster-manager   with MIT License 5 votes vote down vote up
package io.orkestra.cluster

import akka.actor.ActorRef
import akka.http.scaladsl.model.{StatusCodes, StatusCode}
import play.api.libs.json.{JsString, Format, Json, JsValue}

package object protocol {

  case class Register(member: ActorRef, role: String)
  case class RegisterInternal(member: ActorRef, role: String)

  sealed trait Response {
    def asJson: JsValue
    def httpStatusCode: StatusCode
  }

  object Response {

    trait Success extends Response {
      override val httpStatusCode: StatusCode = StatusCodes.OK
    }

    object Success {

      case class Router(name: String, routees: List[String]) extends Success {
        override val asJson = Json.toJson(this)
      }
      object Router {
        implicit val fmt: Format[Router] = Json.format[Router]
      }

      case class Routers(routers: Iterable[JsValue]) extends Success {
        override val asJson = Json.toJson(routers)
      }

      case class RouteeDeleted(role: String, path: String) extends Success {
        override val asJson = JsString(s"routee: $path with role: $role successfully deleted")
      }

    }

    trait Failure extends Response

    object Failure {
      case class RouterNotFound(role: String) extends Failure {
        override val httpStatusCode: StatusCode = StatusCodes.NotFound
        override val asJson: JsValue = Json.obj("error" -> s"router with role: $role not found")
      }
    }
  }

} 
Example 36
Source File: HttpReceptionist.scala    From sumobot   with Apache License 2.0 5 votes vote down vote up
package com.sumologic.sumobot.core

import java.time.Instant

import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import com.sumologic.sumobot.core.model.PublicChannel
import com.sumologic.sumobot.plugins.BotPlugin.{InitializePlugin, PluginAdded, PluginRemoved}
import play.api.libs.json.{JsObject, JsValue}
import slack.api.RtmStartState
import slack.models.{Channel, Group, Im, Team, User}
import slack.rtm.RtmState

object HttpReceptionist {
  private[core] val DefaultChannel = Channel("C0001SUMO", "sumobot", Instant.now().getEpochSecond(),
    Some("U0001SUMO"), Some(false), Some(true), Some(false), Some(false), Some(true), None, Some(false), Some(false), None, None, None, None, None, None, None, None)
  val DefaultSumoBotChannel = PublicChannel(DefaultChannel.id, DefaultChannel.name)

  val DefaultBotUser = User("U0001SUMO", "sumobot-bot", None, None, None, None, None, None, None, None, None, None, None, None, None, None)
  val DefaultClientUser = User("U0002SUMO", "sumobot-client", None, None, None, None, None, None, None, None, None, None, None, None, None, None)

  private[core] val StateUrl = ""
  private[core] val StateTeam = Team("T0001SUMO", "Sumo Bot", "sumobot", "sumologic.com", 30, false, new JsObject(Map.empty), "std")
  private[core] val StateUsers: Seq[User] = Array(DefaultBotUser, DefaultClientUser)
  private[core] val StateChannels: Seq[Channel] = Array(DefaultChannel)
  private[core] val StateGroups: Seq[Group] = Seq.empty
  private[core] val StateIms: Seq[Im] = Seq.empty
  private[core] val StateBots: Seq[JsValue] = Seq.empty

  private[core] val StartState = RtmStartState(StateUrl, DefaultBotUser, StateTeam, StateUsers, StateChannels, StateGroups, StateIms, StateBots)
  private[core] val State = new RtmState(StartState)
}

class HttpReceptionist(brain: ActorRef) extends Actor with ActorLogging {
  private val pluginRegistry = context.system.actorOf(Props(classOf[PluginRegistry]), "plugin-registry")

  override def receive: Receive = {
    case message@PluginAdded(plugin, _) =>
      plugin ! InitializePlugin(HttpReceptionist.State, brain, pluginRegistry)
      pluginRegistry ! message

    case message@PluginRemoved(_) =>
      pluginRegistry ! message
  }
} 
Example 37
Source File: NotificationSender.scala    From cave   with MIT License 5 votes vote down vote up
package worker.web

import java.util.concurrent.Executor

import com.cave.metrics.data.Check
import com.ning.http.client.AsyncHttpClient
import org.jboss.netty.handler.codec.http.HttpHeaders.Names._
import play.api.libs.json.{JsValue, Json}
import worker.converter.ConverterFactory

import scala.concurrent._

trait NotificationSender {
  def send(notification: Check)(implicit exec: Executor): Future[Boolean]
  def shutdown(): Unit
}

case class BadStatus(status: Int) extends RuntimeException

class AsyncNotificationSender(converterFactory: ConverterFactory) extends NotificationSender {

  private val client = new AsyncHttpClient

  override def send(notification: Check)(implicit exec: Executor): Future[Boolean] = {

    def sendJson(url: String, json: JsValue): Future[Boolean] = {
      val f = client.preparePost(url)
        .addHeader(CONTENT_TYPE, "application/json")
        .setBody(Json.stringify(json))
        .execute()

      val p = Promise[Boolean]()

      f.addListener(new Runnable {

        override def run(): Unit = {
          val response = f.get
          if (response.getStatusCode < 400) p.success(true)
          else p.failure(BadStatus(response.getStatusCode))
        }
      }, exec)
      p.future
    }

    val url = notification.schedule.notificationUrl
    converterFactory.getConverter(url).convert(notification) match {
      case Some(json) =>
        println("JSON: " + json)
        sendJson(url, json)
      case None =>
        println("Failed to convert notification to JSON. Entity was " + notification)
        Future.successful(false)
    }
  }

  override def shutdown(): Unit = client.close()
} 
Example 38
Source File: PagerDutyConverter.scala    From cave   with MIT License 5 votes vote down vote up
package worker.converter

import com.cave.metrics.data.Check
import play.api.libs.json.{Json, JsValue}

class PagerDutyConverter extends CheckConverter {
  override def matchesUrl(url: String): Boolean = url.contains("pagerduty.com")

  override def convert(check: Check): Option[JsValue] =
    check.schedule.alert.routing.getOrElse(Map.empty[String, String]).get("pagerduty_service_api_key") map { key =>
      val org = s"organizations/${check.schedule.orgName}"
      val team = check.schedule.teamName map { name => s"/teams/$name" } getOrElse ""

      Json.obj(
        "service_key" -> key,
        "incident_key" -> check.schedule.alert.id.get,
        "event_type" -> "trigger",
        "description" -> check.schedule.alert.description,
        "client" -> "CAVE",
        "client_url" -> s"https://www.cavellc.io/$org$team/alerts/${check.schedule.alert.id.get}",
        "details" -> Json.toJson(check)
      )
    }
} 
Example 39
Source File: FitsValuesSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.json

import org.scalatest.exceptions.TestFailedException
import play.api.libs.json.{JsValue, Json}

class FitsValuesSpec extends ScalaWebTestJsonBaseSpec with FitsTypeMismatchBehavior {
  path = "/jsonResponse.json.jsp"
  def dijkstra: JsValue = Json.parse(webDriver.getPageSource)

  "Fits types" should "report success, when the json gauge contains the same values as the response it is tested against" in {
    dijkstra fits values of
      """{
        | "name": "Dijkstra",
        | "firstName": "Edsger",
        | "yearOfBirth": 1930,
        | "isTuringAwardWinner": true,
        | "theories": [
        |   "shortest path",
        |   "graph theory"
        | ],
        | "falseTheories": null
        |}
      """.stripMargin
  }
  it should "provide the fit synonym" in {
    val universities = dijkstra \ "universities"
    universities fit values of
      """
        | [
        |   { "name": "Universität Leiden","begin": 1948, "end": 1956 },
        |   { "name": "Mathematisch Centrum Amsterdam", "begin": 1951, "end": 1959 },
        |   { "name": "Technische Universiteit Eindhoven", "begin": 1962, "end": 1984 },
        |   { "name": "University of Texas at Austin", "begin": 1984, "end": 1999 }
        | ]
      """.stripMargin
  }
  it should behave like jsonGaugeFitting(values)
  it should "fail when a String doesn't contain the correct value" in {
    assertThrows[TestFailedException]{
      dijkstra fits values of """{"name": "Turing"}"""
    }
  }
  it should "fail when an Int doesn't contain the correct value" in {
    assertThrows[TestFailedException]{
      dijkstra fits values of """{"yearOfBirth": 1995}"""
    }
  }
  it should "fail when a Boolean doesn't contain the correct value" in {
    assertThrows[TestFailedException]{
      dijkstra fits values of """{"isTuringAwardWinner": false}"""
    }
  }
  it should "fail when an array is not complete" in {
    assertThrows[TestFailedException]{
      dijkstra fits values of
        """{
          | "theories": ["shortest path"]
          |}""".stripMargin
    }
  }
  it should "fail when an array contains a wrong value" in {
    assertThrows[TestFailedException]{
      dijkstra fits values of
        """{
          | "theories": ["shortest path", "relational algebra"]
          |}""".stripMargin
    }
  }
} 
Example 40
Source File: play.scala    From caliban   with Apache License 2.0 5 votes vote down vote up
package caliban.interop.play

import caliban.introspection.adt.__Type
import caliban.schema.Step.QueryStep
import caliban.schema.Types.makeScalar
import caliban.schema.{ ArgBuilder, PureStep, Schema, Step }
import caliban.{ InputValue, ResponseValue }
import play.api.libs.json.{ JsPath, JsValue, Json, JsonValidationError, Reads, Writes }
import zio.ZIO
import zio.query.ZQuery


private[caliban] trait IsPlayJsonReads[F[_]]
private[caliban] object IsPlayJsonReads {
  implicit val isPlayJsonReads: IsPlayJsonReads[Reads] = null
}

object json {
  implicit val jsonSchema: Schema[Any, JsValue] = new Schema[Any, JsValue] {
    private def parse(value: JsValue) =
      implicitly[Reads[ResponseValue]]
        .reads(value)
        .asEither
        .left
        .map(parsingException)

    override def toType(isInput: Boolean, isSubscription: Boolean): __Type = makeScalar("Json")
    override def resolve(value: JsValue): Step[Any] =
      QueryStep(ZQuery.fromEffect(ZIO.fromEither(parse(value))).map(PureStep))
  }
  implicit val jsonArgBuilder: ArgBuilder[JsValue] = (input: InputValue) => Right(Json.toJson(input))

  private[caliban] def parsingException(
    errs: scala.collection.Seq[(JsPath, scala.collection.Seq[JsonValidationError])]
  ) =
    new Throwable(s"Couldn't decode json: $errs")
} 
Example 41
Source File: PlayJsonBackend.scala    From caliban   with Apache License 2.0 5 votes vote down vote up
package caliban.interop.play

import akka.http.scaladsl.unmarshalling.FromEntityUnmarshaller
import caliban._
import caliban.interop.play.json.parsingException
import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport
import play.api.libs.json.{ JsObject, JsValue, Json }
import scala.util.Try


final class PlayJsonBackend extends JsonBackend with PlayJsonSupport {

  private def parseJson(s: String): Try[JsValue] =
    Try(Json.parse(s))

  def parseHttpRequest(
    query: Option[String],
    op: Option[String],
    vars: Option[String],
    exts: Option[String]
  ): Either[Throwable, GraphQLRequest] = {
    val variablesJs  = vars.flatMap(parseJson(_).toOption)
    val extensionsJs = exts.flatMap(parseJson(_).toOption)
    Json
      .obj(
        "query"         -> query,
        "operationName" -> op,
        "variables"     -> variablesJs,
        "extensions"    -> extensionsJs
      )
      .validate[GraphQLRequest]
      .asEither
      .left
      .map(parsingException)
  }

  def encodeGraphQLResponse(r: GraphQLResponse[Any]): String = Json.toJson(r).toString()

  def parseWSMessage(text: String): Either[Throwable, WSMessage] =
    parseJson(text).toEither.map { json =>
      PlayWSMessage(
        (json \ "id").validate[String].getOrElse(""),
        (json \ "type").validate[String].getOrElse(""),
        (json \ "payload").validate[JsObject].asOpt
      )
    }

  def encodeWSResponse[E](id: String, data: ResponseValue, errors: List[E]): String =
    Json.stringify(
      Json
        .obj(
          "id"      -> id,
          "type"    -> "data",
          "payload" -> GraphQLResponse(data, errors)
        )
    )

  def encodeWSError(id: String, error: Throwable): String =
    Json.stringify(
      Json
        .obj(
          "id"      -> id,
          "type"    -> "complete",
          "payload" -> error.toString
        )
    )

  def reqUnmarshaller: FromEntityUnmarshaller[GraphQLRequest] = implicitly
} 
Example 42
Source File: Workout.scala    From quick-plan   with Apache License 2.0 5 votes vote down vote up
package com.github.mgifos.workouts.model

import play.api.libs.json.{JsValue, Json}
import Workout._

trait Workout {
  def json(): JsValue = Json.obj()
  def valid(): Boolean = true
}

case class WorkoutDef(sport: String, name: String, steps: Seq[Step] = Nil) extends Workout {
  def toRef: WorkoutRef = WorkoutRef(name)
  def withStep(step: Step): WorkoutDef = WorkoutDef(sport, name, steps :+ step)
  override def json(): JsValue =
    Json.obj(
      "sportType" -> Json.obj("sportTypeId" -> sportId(sport), "sportTypeKey" -> sportTypeKey(sport)),
      "workoutName" -> name,
      "workoutSegments" -> Json.arr(
        Json.obj(
          "segmentOrder" -> 1,
          "sportType" -> Json.obj("sportTypeId" -> sportId(sport), "sportTypeKey" -> sport),
          "workoutSteps" -> steps.zipWithIndex.map { case (s, i) => s.json(i + 1) }
        ))
    )
}

case class WorkoutDefFailure(`type`: String, original: String, cause: String) extends Workout {
  override def toString = s"""Possible workout definition that can't be parsed: "$original"\nCause: "$cause"\n-------------------------------------"""
  override def valid(): Boolean = false
}

case class WorkoutStepFailure(original: String, cause: String) extends Workout {
  override def toString = s"""Workout steps that can't be parsed: "$original"\nCause: "$cause"\n-------------------------------------"""
  override def valid(): Boolean = false
}

case class WorkoutRef(name: String) extends Workout

case class WorkoutNote(note: String) extends Workout

object Workout {

  private val WorkoutType = "(running|cycling|custom)"
  private val WorkoutHeader = raw"""^$WorkoutType?:\s*([\u0020-\u007F]+)(([\r\n]+\s*\-\s[a-z]+:.*)*)$$""".r
  private val NextStepRx = """^((-\s\w*:\s.*)(([\r\n]+\s{1,}-\s.*)*))(([\s].*)*)$""".r
  private val PossibleWorkoutHeader = raw"""^\s*$WorkoutType?\s*:\s*.*(([\r\n]+\s*.*)*)$$""".r

  def parse(text: String)(implicit msys: MeasurementSystems.MeasurementSystem): Workout = {
    def loop(w: WorkoutDef, steps: String): Workout = steps match {
      case NextStepRx(next, _, _, _, rest, _) =>
        try {
          val newWorkout = w.withStep(Step.parse(next.trim))
          if (rest.trim.isEmpty) newWorkout
          else loop(newWorkout, rest.trim)
        } catch {
          case ex: IllegalArgumentException => WorkoutStepFailure(text, ex.getMessage.trim)
        }
      case _ => WorkoutStepFailure(text, steps.trim)
    }
    text match {
      case WorkoutHeader(null, name, steps, _)  => loop(WorkoutDef(detectSport(steps), name), steps.trim)
      case WorkoutHeader(sport, name, steps, _) => loop(WorkoutDef(sport, name), steps.trim)
      case PossibleWorkoutHeader(t, _, cause)   => WorkoutDefFailure(`type` = t, text, if (cause == null) "" else cause.trim)
      case _                                    => WorkoutNote(text)
    }
  }

  def detectSport(steps: String): String = steps match {
    case x if x.contains("- run")  => "running"
    case x if x.contains("- bike") => "cycling"
    case _                         => "custom"
  }

  def sportId(sport: String) = sport match {
    case "running" => 1
    case "cycling" => 2
    case "custom"  => 3
    case _         => throw new IllegalArgumentException("Only running, cycling and 'custom' workouts are supported.")
  }

  def sportTypeKey(sport: String) = sport match {
    case "custom" => "other"
    case _        => sport
  }
} 
Example 43
Source File: DefaultBintrayRepoConnector.scala    From releaser   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.releaser.bintray

import java.net.{HttpURLConnection, URL}
import java.nio.file.Path

import play.api.libs.json.{JsValue, Json}
import uk.gov.hmrc.{FileDownloader, Logger, ServiceCredentials}

import scala.util.{Failure, Success, Try}

object BintrayRepoConnector extends Logger {
  def apply(bintrayCreds: ServiceCredentials, workDir : Path): BintrayRepoConnector =
    new DefaultBintrayRepoConnector(workDir, new BintrayHttp(bintrayCreds), new FileDownloader())

  def dryRun(bintrayCreds: ServiceCredentials, workDir : Path) = {
    log.info("Bintray : running in dry-run mode")
    val dryRunHttp = new BintrayHttp(bintrayCreds){
      override def emptyPost(url:String): Try[Unit] = { println("BintrayHttp emptyPost DRY_RUN");Success(Unit)}
      override def putFile(version: VersionDescriptor, file: Path, url: String): Try[Unit] = { println("BintrayHttp putFile DRY_RUN");Success(Unit) }
    }

    new DefaultBintrayRepoConnector(workDir, dryRunHttp, new FileDownloader())
  }
}

trait BintrayRepoConnector {
  def findJar(jarFileName: String, jarUrl: String, version: VersionDescriptor): Option[Path]
  def publish(version: VersionDescriptor): Try[Unit]
  def downloadFile(url: String, fileName: String): Try[Path]
  def uploadFile(version: VersionDescriptor, filePath: Path, url: String): Try[Unit]
  def verifyTargetDoesNotExist(version: VersionDescriptor): Try[Unit]
  def findFiles(version: VersionDescriptor): Try[List[String]]
  def getRepoMetaData(repoName:String, artefactName: String): Try[Unit]
}

class DefaultBintrayRepoConnector(workDir: Path, bintrayHttp: BintrayHttp, fileDownloader: FileDownloader)
  extends BintrayRepoConnector with Logger {

  def publish(version: VersionDescriptor):Try[Unit] = {
    val url = BintrayPaths.publishUrlFor(version)
    bintrayHttp.emptyPost(url)
  }

  def verifyTargetDoesNotExist(version: VersionDescriptor): Try[Unit] = {
    val url = BintrayPaths.fileListUrlFor(version)
    log.info(s"Bintray : checking to see if $url exists")

    val conn = new URL(url).openConnection().asInstanceOf[HttpURLConnection]
    conn.setRequestMethod("HEAD")
    conn.connect()

    conn.getResponseCode match {
      case 200 => Failure(new IllegalArgumentException(s"${version.artefactName} ${version.version} already exists"))
      case _ => Success()
    }
  }

  def findJar(jarFileName: String, jarUrl: String, version: VersionDescriptor): Option[Path] = {
    downloadFile(jarUrl, jarFileName) match {
      case Success(x) => Some(x)
      case Failure(y) => None
    }
  }

  def uploadFile(version: VersionDescriptor, filePath: Path, url: String): Try[Unit] = {
    bintrayHttp.putFile(version, filePath, url)
  }

  def downloadFile(url: String, fileName: String): Try[Path] = {
    val targetFile = workDir.resolve(fileName)
    fileDownloader.url2File(url, targetFile) map { unit => targetFile }
  }

  def findFiles(version: VersionDescriptor): Try[List[String]] = {
    val url = BintrayPaths.fileListUrlFor(version)
    bintrayHttp.get(url).map { st =>
      val fileNames: Seq[JsValue] = Json.parse(st) \\ "path"
      fileNames.map(_.as[String]).toList
    }
  }

  def getRepoMetaData(repoName:String, artefactName: String): Try[Unit] = {
    val url = BintrayPaths.metadata(repoName, artefactName)
    bintrayHttp.get(url).map { _ => Unit}
  }
} 
Example 44
Source File: GithubHttp.scala    From releaser   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.releaser.github

import java.util.concurrent.TimeUnit

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.json.JsValue
import play.api.libs.ws._
import play.api.libs.ws.ning.{NingAsyncHttpClientConfigBuilder, NingWSClient}
import uk.gov.hmrc.{Logger, ServiceCredentials}

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

class GithubHttp(cred: ServiceCredentials) extends Logger {

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

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

  def buildCall(method:String, url:String, body:Option[JsValue] = None): WSRequest ={
    log.debug(s"github client_id ${cred.user.takeRight(5)}")
    log.debug(s"github client_secret ${cred.pass.takeRight(5)}")

    val req = ws.url(url)
      .withMethod(method)
      .withAuth(cred.user, cred.pass, WSAuthScheme.BASIC)
      .withQueryString("client_id" -> cred.user, "client_secret" -> cred.pass)
      .withHeaders("content-type" -> "application/json")

    body.map { b =>
      req.withBody(b)
    }.getOrElse(req)
  }

  def callAndWait(req:WSRequest): WSResponse = {
    log.info(s"${req.method} with ${req.url}")
    val result: WSResponse = Await.result(req.execute(), Duration.apply(1, TimeUnit.MINUTES))
    log.info(s"${req.method} with ${req.url} result ${result.status} - ${result.statusText}")
    result
  }

  def get(url:String): Try[Unit] = {
    val result = callAndWait(buildCall("GET", url))
    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 Github. Got status ${result.status}: ${result.body}"))
    }
  }

  def post[A](responseBuilder:(WSResponse) => Try[A])(url:String, body:JsValue): Try[A] = {
    log.debug("github url: " + url)
    log.debug("github body: " + body)

    val result = callAndWait(buildCall("POST", url, Some(body)))
    result.status match {
      case s if s >= 200 && s < 300 => responseBuilder(result)
      case _@e => Failure(new scala.Exception(s"Didn't get expected status code when writing to Github. Got status ${result.status}: ${result.body}"))
    }
  }

  def postUnit(url:String, body:JsValue): Try[Unit] = {
    post[Unit](_ => Success(Unit))(url, body)
  }
} 
Example 45
Source File: AliasApiRoute.scala    From Waves   with MIT License 5 votes vote down vote up
package com.wavesplatform.api.http.alias

import akka.NotUsed
import akka.http.scaladsl.common.{EntityStreamingSupport, JsonEntityStreamingSupport}
import akka.http.scaladsl.server.Route
import akka.stream.scaladsl.Source
import cats.syntax.either._
import com.wavesplatform.account.Alias
import com.wavesplatform.api.common.CommonTransactionsApi
import com.wavesplatform.api.http._
import com.wavesplatform.api.http.requests.CreateAliasRequest
import com.wavesplatform.http.BroadcastRoute
import com.wavesplatform.network.UtxPoolSynchronizer
import com.wavesplatform.settings.RestAPISettings
import com.wavesplatform.state.Blockchain
import com.wavesplatform.transaction._
import com.wavesplatform.utils.Time
import com.wavesplatform.wallet.Wallet
import play.api.libs.json.{JsString, JsValue, Json}

case class AliasApiRoute(
    settings: RestAPISettings,
    commonApi: CommonTransactionsApi,
    wallet: Wallet,
    utxPoolSynchronizer: UtxPoolSynchronizer,
    time: Time,
    blockchain: Blockchain
) extends ApiRoute
    with BroadcastRoute
    with AuthRoute {

  override val route: Route = pathPrefix("alias") {
    addressOfAlias ~ aliasOfAddress ~ deprecatedRoute
  }

  private def deprecatedRoute: Route =
    path("broadcast" / "create") {
      broadcast[CreateAliasRequest](_.toTx)
    } ~ (path("create") & withAuth) {
      broadcast[CreateAliasRequest](TransactionFactory.createAlias(_, wallet, time))
    }

  def addressOfAlias: Route = (get & path("by-alias" / Segment)) { aliasName =>
    complete {
      Alias
        .create(aliasName)
        .flatMap { a =>
          blockchain.resolveAlias(a).bimap(_ => TxValidationError.AliasDoesNotExist(a), addr => Json.obj("address" -> addr.stringRepr))
        }
    }
  }

  private implicit val ess: JsonEntityStreamingSupport = EntityStreamingSupport.json()

  def aliasOfAddress: Route = (get & path("by-address" / AddrSegment)) { address =>
    extractScheduler { implicit s =>
      val value: Source[JsValue, NotUsed] =
        Source.fromPublisher(commonApi.aliasesOfAddress(address).map { case (_, tx) => JsString(tx.alias.stringRepr) }.toReactivePublisher)
      complete(value)
    }
  }
} 
Example 46
Source File: MassTransferTxSerializer.scala    From Waves   with MIT License 5 votes vote down vote up
package com.wavesplatform.transaction.serialization.impl

import java.nio.ByteBuffer

import com.google.common.primitives.{Bytes, Longs, Shorts}
import com.wavesplatform.account.{AddressOrAlias, AddressScheme}
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils._
import com.wavesplatform.serialization._
import com.wavesplatform.transaction.TxVersion
import com.wavesplatform.transaction.transfer.MassTransferTransaction
import com.wavesplatform.transaction.transfer.MassTransferTransaction.{ParsedTransfer, Transfer}
import com.wavesplatform.utils.byteStrFormat
import play.api.libs.json.{JsObject, JsValue, Json}

import scala.util.Try

object MassTransferTxSerializer {
  def transfersJson(transfers: Seq[ParsedTransfer]): JsValue =
    Json.toJson(transfers.map { case ParsedTransfer(address, amount) => Transfer(address.stringRepr, amount) })

  def toJson(tx: MassTransferTransaction): JsObject = {
    import tx._
    BaseTxJson.toJson(tx) ++ Json.obj(
      "assetId"       -> assetId.maybeBase58Repr,
      "attachment"    -> attachment,
      "transferCount" -> transfers.size,
      "totalAmount"   -> transfers.map(_.amount).sum,
      "transfers"     -> transfersJson(transfers)
    )
  }

  def bodyBytes(tx: MassTransferTransaction): Array[Byte] = {
    import tx._
    version match {
      case TxVersion.V1 =>
        val transferBytes = transfers.map { case ParsedTransfer(recipient, amount) => Bytes.concat(recipient.bytes, Longs.toByteArray(amount)) }

        Bytes.concat(
          Array(builder.typeId, version),
          sender.arr,
          assetId.byteRepr,
          Shorts.toByteArray(transfers.size.toShort),
          Bytes.concat(transferBytes: _*),
          Longs.toByteArray(timestamp),
          Longs.toByteArray(fee),
          Deser.serializeArrayWithLength(attachment.arr)
        )

      case _ =>
        PBTransactionSerializer.bodyBytes(tx)
    }
  }

  def toBytes(tx: MassTransferTransaction): Array[Byte] =
    if (tx.isProtobufVersion) PBTransactionSerializer.bytes(tx)
    else Bytes.concat(this.bodyBytes(tx), tx.proofs.bytes()) // No zero mark

  def parseBytes(bytes: Array[Byte]): Try[MassTransferTransaction] = Try {
    def parseTransfers(buf: ByteBuffer): Seq[MassTransferTransaction.ParsedTransfer] = {
      def readTransfer(buf: ByteBuffer): ParsedTransfer = {
        val addressOrAlias = AddressOrAlias.fromBytes(buf).explicitGet()
        val amount         = buf.getLong
        ParsedTransfer(addressOrAlias, amount)
      }

      val entryCount = buf.getShort
      require(entryCount >= 0 && buf.remaining() > entryCount, s"Broken array size ($entryCount entries while ${buf.remaining()} bytes available)")
      Vector.fill(entryCount)(readTransfer(buf))
    }

    val buf = ByteBuffer.wrap(bytes)
    require(buf.getByte == MassTransferTransaction.typeId && buf.getByte == TxVersion.V1, "transaction type mismatch")

    val sender     = buf.getPublicKey
    val assetId    = buf.getAsset
    val transfers  = parseTransfers(buf)
    val timestamp  = buf.getLong // Timestamp before fee
    val fee        = buf.getLong
    val attachment = Deser.parseArrayWithLength(buf)
    val proofs     = buf.getProofs
    MassTransferTransaction(TxVersion.V1, sender, assetId, transfers, fee, timestamp, ByteStr(attachment), proofs, AddressScheme.current.chainId)
  }
} 
Example 47
Source File: SwaggerSpecRunner.scala    From play-swagger   with Apache License 2.0 5 votes vote down vote up
package com.iheart.playSwagger

import java.nio.file.{ Files, Paths, StandardOpenOption }

import play.api.libs.json.{ JsValue, Json }

import scala.util.{ Failure, Success, Try }

object SwaggerSpecRunner extends App {
  implicit def cl: ClassLoader = getClass.getClassLoader

  val targetFile :: routesFile :: domainNameSpaceArgs :: outputTransformersArgs :: swaggerV3String :: apiVersion :: swaggerPrettyJson :: namingStrategy :: Nil = args.toList
  private def fileArg = Paths.get(targetFile)
  private def swaggerJson = {
    val swaggerV3 = java.lang.Boolean.parseBoolean(swaggerV3String)
    val domainModelQualifier = PrefixDomainModelQualifier(domainNameSpaceArgs.split(","): _*)
    val transformersStrs: Seq[String] = if (outputTransformersArgs.isEmpty) Seq() else outputTransformersArgs.split(",")
    val transformers = transformersStrs.map { clazz ⇒
      Try(cl.loadClass(clazz).asSubclass(classOf[OutputTransformer]).newInstance()) match {
        case Failure(ex: ClassCastException) ⇒
          throw new IllegalArgumentException("Transformer should be a subclass of com.iheart.playSwagger.OutputTransformer:" + clazz, ex)
        case Failure(ex) ⇒ throw new IllegalArgumentException("Could not create transformer", ex)
        case Success(el) ⇒ el
      }
    }

    val swaggerSpec: JsValue = SwaggerSpecGenerator(
      NamingStrategy.from(namingStrategy),
      domainModelQualifier,
      outputTransformers = transformers,
      swaggerV3 = swaggerV3,
      apiVersion = Some(apiVersion)).generate(routesFile).get

    if (swaggerPrettyJson.toBoolean) Json.prettyPrint(swaggerSpec)
    else swaggerSpec.toString
  }

  Files.write(fileArg, swaggerJson.getBytes, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)
} 
Example 48
Source File: OutputTransformer.scala    From play-swagger   with Apache License 2.0 5 votes vote down vote up
package com.iheart.playSwagger

import java.util.regex.Pattern

import com.iheart.playSwagger.OutputTransformer.SimpleOutputTransformer
import play.api.libs.json.{ JsArray, JsString, JsValue, JsObject }

import scala.util.matching.Regex
import scala.util.{ Success, Failure, Try }


  def >=>(b: JsObject ⇒ Try[JsObject]): OutputTransformer = SimpleOutputTransformer { value: JsObject ⇒
    this.apply(value).flatMap(b)
  }
}

object OutputTransformer {
  final case class SimpleOutputTransformer(run: (JsObject ⇒ Try[JsObject])) extends OutputTransformer {
    override def apply(value: JsObject): Try[JsObject] = run(value)
  }

  def traverseTransformer(vals: JsArray)(transformer: JsValue ⇒ Try[JsValue]): Try[JsArray] = {
    val tryElements = vals.value.map {
      case value: JsObject ⇒ traverseTransformer(value)(transformer)
      case value: JsArray  ⇒ traverseTransformer(value)(transformer)
      case value: JsValue  ⇒ transformer(value)
    }

    val failures: Seq[Failure[JsValue]] = tryElements.filter(_.isInstanceOf[Failure[_]]).asInstanceOf[Seq[Failure[JsValue]]]
    if (failures.nonEmpty) {
      Failure(failures.head.exception)
    } else {
      Success(JsArray(tryElements.asInstanceOf[Seq[Success[JsValue]]].map(_.value)))
    }
  }

  def traverseTransformer(obj: JsObject)(transformer: JsValue ⇒ Try[JsValue]): Try[JsObject] = {
    val tryFields = obj.fields.map {
      case (key, value: JsObject) ⇒ (key, traverseTransformer(value)(transformer))
      case (key, values: JsArray) ⇒ (key, traverseTransformer(values)(transformer))
      case (key, value: JsValue)  ⇒ (key, transformer(value))
    }
    val failures: Seq[(String, Failure[JsValue])] = tryFields
      .filter(_._2.isInstanceOf[Failure[_]])
      .asInstanceOf[Seq[(String, Failure[JsValue])]]
    if (failures.nonEmpty) {
      Failure(failures.head._2.exception)
    } else {
      Success(JsObject(tryFields.asInstanceOf[Seq[(String, Success[JsValue])]].map {
        case (key, Success(result)) ⇒ (key, result)
      }))
    }
  }
}

class PlaceholderVariablesTransformer(map: String ⇒ Option[String], pattern: Regex = "^\\$\\{(.*)\\}$".r) extends OutputTransformer {
  def apply(value: JsObject) = OutputTransformer.traverseTransformer(value) {
    case JsString(pattern(key)) ⇒ map(key) match {
      case Some(result) ⇒ Success(JsString(result))
      case None         ⇒ Failure(new IllegalStateException(s"Unable to find variable $key"))
    }
    case e: JsValue ⇒ Success(e)
  }
}

final case class MapVariablesTransformer(map: Map[String, String]) extends PlaceholderVariablesTransformer(map.get)
class EnvironmentVariablesTransformer extends PlaceholderVariablesTransformer((key: String) ⇒ Option(System.getenv(key))) 
Example 49
Source File: Domain.scala    From play-swagger   with Apache License 2.0 5 votes vote down vote up
package com.iheart.playSwagger

import play.api.libs.json.{ JsObject, JsPath, JsValue, Reads }
import play.twirl.api.TemplateMagic.Default

object Domain {
  type Path = String
  type Method = String

  final case class Definition(
    name:        String,
    properties:  Seq[SwaggerParameter],
    description: Option[String]        = None)

  sealed trait SwaggerParameter {
    def name: String
    def required: Boolean
    def default: Option[JsValue]

    def update(required: Boolean, default: Option[JsValue]): SwaggerParameter
  }

  final case class GenSwaggerParameter(
    name:          String,
    referenceType: Option[String]           = None,
    `type`:        Option[String]           = None,
    format:        Option[String]           = None,
    required:      Boolean                  = true,
    default:       Option[JsValue]          = None,
    example:       Option[JsValue]          = None,
    items:         Option[SwaggerParameter] = None,
    enum:          Option[Seq[String]]      = None) extends SwaggerParameter {
    def update(_required: Boolean, _default: Option[JsValue]) =
      copy(required = _required, default = _default)
  }

  final case class CustomSwaggerParameter(
    name:            String,
    specAsParameter: List[JsObject],
    specAsProperty:  Option[JsObject],
    required:        Boolean          = true,
    default:         Option[JsValue]  = None) extends SwaggerParameter {
    def update(_required: Boolean, _default: Option[JsValue]) =
      copy(required = _required, default = _default)
  }

  type CustomMappings = List[CustomTypeMapping]

  case class CustomTypeMapping(
    `type`:          String,
    specAsParameter: List[JsObject]   = Nil,
    specAsProperty:  Option[JsObject] = None,
    required:        Boolean          = true)

  object CustomTypeMapping {
    import play.api.libs.functional.syntax._
    implicit val csmFormat: Reads[CustomTypeMapping] = (
      (JsPath \ 'type).read[String] and
      (JsPath \ 'specAsParameter).read[List[JsObject]] and
      (JsPath \ 'specAsProperty).readNullable[JsObject] and
      ((JsPath \ 'required).read[Boolean] orElse Reads.pure(true)))(CustomTypeMapping.apply _)
  }
} 
Example 50
Source File: SocketConfigSpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.kernel.socket

import com.typesafe.config.ConfigFactory
import org.scalatest.{FunSpec, Matchers}
import org.slf4j.LoggerFactory
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, JsValue, Json}

class SocketConfigSpec extends FunSpec with Matchers {
  val logger = LoggerFactory.getLogger("jt4")
  //logger.error("WOOT!")

  private val jsonString: String =
    """
    {
      "stdin_port": 10000,
      "control_port": 10001,
      "hb_port": 10002,
      "shell_port": 10003,
      "iopub_port": 10004,
      "ip": "1.2.3.4",
      "transport": "tcp",
      "signature_scheme": "hmac-sha256",
      "key": ""
    }
    """.stripMargin

  val socketConfigJson: JsValue = Json.parse(jsonString)

  val socketConfigFromConfig = SocketConfig.fromConfig(ConfigFactory.parseString(jsonString))

  val socketConfig = SocketConfig(
    10000, 10001, 10002, 10003, 10004, "1.2.3.4", "tcp", "hmac-sha256", ""
  )

  describe("SocketConfig") {
    describe("implicit conversions") {
      it("should implicitly convert from valid json to a SocketConfig instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        socketConfigJson.as[SocketConfig] should be (socketConfig)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newCompleteRequest = socketConfigJson.asOpt[SocketConfig]

        newCompleteRequest.get should be (socketConfig)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val CompleteRequestResults = socketConfigJson.validate[SocketConfig]

        CompleteRequestResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: SocketConfig) => valid
        ) should be (socketConfig)
      }

      it("should implicitly convert from a SocketConfig instance to valid json") {
        Json.toJson(socketConfig) should be (socketConfigJson)
      }
    }
    describe("#toConfig") {
      it("should implicitly convert from valid json to a SocketConfig instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        socketConfigFromConfig should be (socketConfig)
      }
      
      it("should convert json file to SocketConfig object") {
        socketConfigFromConfig.stdin_port should be (10000)
      }
    }
  }
} 
Example 51
Source File: BuildInfoObj.scala    From eidos   with Apache License 2.0 5 votes vote down vote up
package org.clulab.wm.eidos.serialization.web

import org.clulab.wm.eidos.BuildInfo
import play.api.libs.json.JsValue
import play.api.libs.json.Json

object BuildInfoObj {

  val mkJson: JsValue = Json.obj(
    "name" -> BuildInfo.name,
    "version" -> BuildInfo.version,
    "scalaVersion" -> BuildInfo.scalaVersion,
    "sbtVersion" -> BuildInfo.sbtVersion,
    "libraryDependencies" -> BuildInfo.libraryDependencies,
    "scalacOptions" -> BuildInfo.scalacOptions,
    "gitCurrentBranch" -> BuildInfo.gitCurrentBranch,
    "gitHeadCommit" -> BuildInfo.gitHeadCommit,
    "gitHeadCommitDate" -> BuildInfo.gitHeadCommitDate,
    "gitUncommittedChanges" -> BuildInfo.gitUncommittedChanges 
  )
} 
Example 52
Source File: UberMappers.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.she.mappers

import java.util.UUID

import org.hatdex.hat.api.models.{ EndpointQuery, EndpointQueryFilter, FilterOperator, PropertyQuery }
import org.hatdex.hat.api.models.applications.{ DataFeedItem, DataFeedItemContent, DataFeedItemLocation, DataFeedItemTitle, LocationGeo }
import org.hatdex.hat.she.models.StaticDataValues
import org.joda.time.DateTime
import play.api.libs.json.{ JsError, JsSuccess, JsValue, Json }

import scala.util.Try

class UberRidesMapper extends DataEndpointMapper {
  override protected val dataDeduplicationField: Option[String] = Some("request_id")

  def dataQueries(fromDate: Option[DateTime], untilDate: Option[DateTime]): Seq[PropertyQuery] = {
    val unixDateFilter = fromDate.flatMap { _ =>
      Some(FilterOperator.Between(Json.toJson(fromDate.map(t => t.getMillis / 1000)), Json.toJson(untilDate.map(t => t.getMillis / 1000))))
    }

    Seq(PropertyQuery(
      List(EndpointQuery("uber/rides", None,
        unixDateFilter.map(f ⇒ Seq(EndpointQueryFilter("start_time", None, f))), None)), Some("start_time"), Some("descending"), None))
  }

  def mapDataRecord(recordId: UUID, content: JsValue, tailRecordId: Option[UUID] = None, tailContent: Option[JsValue] = None): Try[DataFeedItem] = {
    logger.debug(s"uber content: $content")
    for {
      distance <- Try((content \ "distance").asOpt[Double].getOrElse(0.doubleValue()).toString)
      startDate <- Try(new DateTime((content \ "start_time").as[Long] * 1000.longValue()))
      durationSeconds ← Try((content \ "end_time").asOpt[Int].getOrElse(0) - (content \ "start_time").asOpt[Int].getOrElse(0))
      duration <- Try {
        val m = (durationSeconds / 60) % 60
        val h = (durationSeconds / 60 / 60) % 24
        "%02d h %02d min".format(h, m)
      }
      title ← Try(DataFeedItemTitle(s"Your trip on ${startDate.toString("dd/MM/YYYY")}", None, None))
      itemContent ← Try(DataFeedItemContent(
        Some(
          s"""${(content \ "start_city" \ "display_name").asOpt[String].getOrElse("Unknown City")},
             |${BigDecimal.decimal(distance.toFloat).setScale(1, BigDecimal.RoundingMode.HALF_UP).toDouble} miles,
             |${duration}""".stripMargin),
        None,
        None,
        None))
      latitude <- Try((content \ "start_city" \ "latitude").asOpt[Double].getOrElse(0.doubleValue()))
      longitude <- Try((content \ "start_city" \ "longitude").asOpt[Double].getOrElse(0.doubleValue()))
      location <- Try(DataFeedItemLocation(Some(LocationGeo(longitude, latitude)), None, None))
    } yield DataFeedItem("uber", startDate, Seq(), Some(title), Some(itemContent), Some(location))
  }
}

class UberProfileStaticDataMapper extends StaticDataEndpointMapper {
  def dataQueries(): Seq[PropertyQuery] = {
    Seq(PropertyQuery(
      List(
        EndpointQuery("uber/profile", None, None, None)), Some("dateCreated"), Some("descending"), Some(1)))
  }

  def mapDataRecord(recordId: UUID, content: JsValue, endpoint: String): Seq[StaticDataValues] = {
    val eventualData = content.validate[Map[String, JsValue]]
    eventualData match {
      case JsSuccess(value, _) =>
        val lastPartOfEndpointString = endpoint.split("/").last

        Seq(StaticDataValues(lastPartOfEndpointString, value))
      case e: JsError =>
        logger.error(s"Couldn't validate static data JSON for $endpoint. $e")
        Seq()
    }
  }
} 
Example 53
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 54
Source File: StatefulStoreSupport.scala    From cloudstate   with Apache License 2.0 5 votes vote down vote up
package io.cloudstate.operator.stores

import akka.Done
import akka.actor.ActorSystem
import io.cloudstate.operator.OperatorConstants.StatefulStoreConditionType
import io.cloudstate.operator.{Condition, ImageConfig, StatefulStore, Validated}
import play.api.libs.json.JsValue
import skuber.EnvVar
import skuber.api.client.KubernetesClient

import scala.concurrent.Future

object StatefulStoreSupport {
  private val types: List[StatefulStoreSupport] =
    List(CassandraStoreSupport, InMemoryStoreSupport, PostgresStoreSupport)

  def get(storeType: String): Option[StatefulStoreSupport] = types.find(_.name == storeType)

  def get(store: StatefulStore.Resource): Validated[StatefulStoreSupport] =
    store.spec.`type` match {
      case Some(storeType) =>
        StatefulStoreSupport.get(storeType) match {
          case Some(storeSupport) => Validated(storeSupport)
          case None =>
            Validated.error(
              StatefulStoreConditionType,
              "UnknownStoreType",
              s"Unknown store type: $storeType, supported types are: ${StatefulStoreSupport.supportedTypes.mkString(", ")}"
            )
        }
      case None =>
        Validated.error(StatefulStoreConditionType,
                        "UnspecifiedStoreType",
                        s"StatefulStore ${store.name} does not specify a store type.")
    }

  def supportedTypes: List[String] = types.map(_.name)

  object noStoreUsageConfiguration extends StatefulStoreUsageConfiguration {
    override def successfulConditions: List[Condition] = List()
    override def proxyImage(config: ImageConfig): String = config.noStore
    override def proxyContainerEnvVars: List[EnvVar] = Nil
  }

}

trait StatefulStoreSupport {
  def name: String
  def validate(store: StatefulStore.Resource, client: KubernetesClient): Validated[ConfiguredStatefulStore]
  def reconcile(store: StatefulStore.Resource, client: KubernetesClient): Validated[ConfiguredStatefulStore]
  def delete(spec: StatefulStore.Spec, client: KubernetesClient): Future[Done] = Future.successful(Done)
}

trait ConfiguredStatefulStore {
  def successfulConditions: List[Condition]
  def validateInstance(config: Option[JsValue], client: KubernetesClient): Validated[StatefulStoreUsageConfiguration]
}

trait StatefulStoreUsageConfiguration {
  def successfulConditions: List[Condition]
  def proxyImage(config: ImageConfig): String
  def proxyContainerEnvVars: List[EnvVar]
} 
Example 55
Source File: InMemoryStoreSupport.scala    From cloudstate   with Apache License 2.0 5 votes vote down vote up
package io.cloudstate.operator.stores
import io.cloudstate.operator.StatefulStore.Resource
import io.cloudstate.operator.{Condition, ImageConfig, OperatorConstants, StatefulStore, Validated}
import play.api.libs.json.JsValue
import skuber.EnvVar
import skuber.api.client.KubernetesClient

object InMemoryStoreSupport
    extends StatefulStoreSupport
    with ConfiguredStatefulStore
    with StatefulStoreUsageConfiguration {
  override def name: String = OperatorConstants.InMemoryStatefulStoreType
  override def validate(store: StatefulStore.Resource, client: KubernetesClient): Validated[ConfiguredStatefulStore] =
    Validated(this)
  override def reconcile(store: Resource, client: KubernetesClient): Validated[ConfiguredStatefulStore] =
    Validated(this)
  override def successfulConditions: List[Condition] = List()
  override def validateInstance(config: Option[JsValue],
                                client: KubernetesClient): Validated[StatefulStoreUsageConfiguration] = Validated(this)
  override def proxyImage(config: ImageConfig): String = config.inMemory
  override def proxyContainerEnvVars: List[EnvVar] = Nil
} 
Example 56
Source File: CassandraStoreSupport.scala    From cloudstate   with Apache License 2.0 5 votes vote down vote up
package io.cloudstate.operator.stores
import io.cloudstate.operator.{Condition, ImageConfig, OperatorConstants, StatefulStore, Validated}
import skuber.api.client.KubernetesClient
import io.cloudstate.operator.OperatorConstants._
import io.cloudstate.operator.StatefulStore.Resource
import play.api.libs.json.JsValue
import skuber.EnvVar

object CassandraStoreSupport extends StatefulStoreSupport {

  override def name: String = OperatorConstants.CassandraStatefulStoreType

  override def validate(store: StatefulStore.Resource, client: KubernetesClient): Validated[ConfiguredStatefulStore] =
    store.spec.deployment match {
      case Some(`UnmanagedStatefulStoreDeployment`) =>
        store.spec.config.flatMap(c => (c \ "service").asOpt[String]) match {
          case Some(serviceName) =>
            Validated(new UnmanagedCassandra(serviceName))

          case None =>
            Validated.error(StatefulStoreConditionType,
                            "MissingServiceName",
                            "No service name declared in unmanaged Cassandra journal")
        }

      case Some(unknown) =>
        Validated.error(
          StatefulStoreConditionType,
          "UnknownDeploymentType",
          s"Unknown Cassandra deployment type: $unknown, supported types for Cassandra are: $UnmanagedStatefulStoreDeployment"
        )

      case None =>
        Validated.error(
          StatefulStoreConditionType,
          "UnspecifiedDeploymentType",
          s"Unspecified Cassandra deployment type, supported types for Cassandra are: $UnmanagedStatefulStoreDeployment"
        )

    }

  override def reconcile(store: Resource, client: KubernetesClient): Validated[ConfiguredStatefulStore] =
    validate(store, client)

  private class UnmanagedCassandra(service: String) extends ConfiguredStatefulStore {
    override def successfulConditions: List[Condition] = Nil

    override def validateInstance(config: Option[JsValue],
                                  client: KubernetesClient): Validated[StatefulStoreUsageConfiguration] =
      config.flatMap(config => (config \ "keyspace").asOpt[String]) match {
        case Some(keyspace) =>
          Validated(new CassandraUsage(service, keyspace))
        case None =>
          Validated.error(StatefulStoreConditionType,
                          "MissingKeyspace",
                          "No keyspace declared for unmanaged Cassandra journal")
      }
  }

  private class CassandraUsage(service: String, keyspace: String) extends StatefulStoreUsageConfiguration {
    override def successfulConditions: List[Condition] = Nil
    override def proxyImage(config: ImageConfig): String = config.cassandra
    override def proxyContainerEnvVars: List[EnvVar] = List(
      EnvVar("CASSANDRA_CONTACT_POINTS", service),
      EnvVar("CASSANDRA_KEYSPACE", keyspace)
    )
  }
} 
Example 57
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 58
Source File: HttpResponse.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http

import com.github.ghik.silencer.silent
import play.api.libs.json.{JsValue, Json}


// This trait will be replaced with a case class (Which will remove coupling to specific types, enable `.copy` etc (useful for testing))
// To not break clients, we will discourage use of extending HttpResponse, rather use the apply functions. We will be able to introduce
// the case class afterwards.
trait HttpResponse {
  def status: Int

  def body: String

  @deprecated("For reading use headers instead. If setting, use HttpResponse.apply instead. You should not extend HttpResponse, but create instances with HttpResponse.apply", "11.0.0")
  def allHeaders: Map[String, Seq[String]]

  // final to help migrate away from allHeaders (i.e. read only - set via HttpResponse.apply)
  @silent("deprecated")
  final def headers: Map[String, Seq[String]]
    = allHeaders

  def json: JsValue =
    Json.parse(body)

  def header(key: String): Option[String] =
    headers.get(key).flatMap(_.headOption)

  override def toString: String =
    s"HttpResponse status=$status"
}

object HttpResponse {
  @deprecated("Use alternative HttpResponse.apply functions instead", "11.0.0")
  def apply(
    responseStatus : Int,
    responseJson   : Option[JsValue]          = None,
    responseHeaders: Map[String, Seq[String]] = Map.empty,
    responseString : Option[String]           = None
  ) = new HttpResponse {
    override def status    : Int                      = responseStatus
    override def body      : String                   = responseString.orElse(responseJson.map(Json.prettyPrint)).orNull
    override def allHeaders: Map[String, Seq[String]] = responseHeaders
    override def json      : JsValue                  = responseJson.orNull
  }

  def apply(
    status : Int,
    body   : String
  ): HttpResponse =
    apply(
      status  = status,
      body    = body,
      headers = Map.empty
    )

  def apply(
    status : Int,
    body   : String,
    headers: Map[String, Seq[String]]
  ): HttpResponse = {
    val pStatus  = status
    val pBody    = body
    val pHeaders = headers
    new HttpResponse {
      override def status    : Int                      = pStatus
      override def body      : String                   = pBody
      override def allHeaders: Map[String, Seq[String]] = pHeaders
    }
  }

  def apply(
    status : Int,
    json   : JsValue,
    headers: Map[String, Seq[String]]
  ): HttpResponse = {
    val pStatus  = status
    val pJson    = json
    val pHeaders = headers
    new HttpResponse {
      override def status    : Int                      = pStatus
      override def body      : String                   = Json.prettyPrint(pJson)
      override def allHeaders: Map[String, Seq[String]] = pHeaders
      override def json      : JsValue                  = pJson
    }
  }

  def unapply(that: HttpResponse): Option[(Int, String, Map[String, Seq[String]])] =
    Some((that.status, that.body, that.headers))
} 
Example 59
Source File: HttpReadsLegacyInstances.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http

import com.github.ghik.silencer.silent
import play.api.libs.json
import play.api.libs.json.{JsNull, JsValue}


trait HttpReadsLegacyInstances extends HttpReadsLegacyOption with HttpReadsLegacyJson

trait HttpReadsLegacyRawReads extends HttpErrorFunctions {
  @deprecated("Use uk.gov.hmrc.http.HttpReads.Implicits instead. See README for differences.", "11.0.0")
  implicit val readRaw: HttpReads[HttpResponse] = new HttpReads[HttpResponse] {
    @silent("deprecated")
    def read(method: String, url: String, response: HttpResponse) = handleResponse(method, url)(response)
  }
}

object HttpReadsLegacyRawReads extends HttpReadsLegacyRawReads

trait HttpReadsLegacyOption extends HttpErrorFunctions {
  @deprecated("Use uk.gov.hmrc.http.HttpReads.Implicits instead. See README for differences.", "11.0.0")
  implicit def readOptionOf[P](implicit rds: HttpReads[P]): HttpReads[Option[P]] = new HttpReads[Option[P]] {
    def read(method: String, url: String, response: HttpResponse) = response.status match {
      case 204 | 404 => None
      case _         => Some(rds.read(method, url, response))
    }
  }
}

trait HttpReadsLegacyJson extends HttpErrorFunctions {
  @deprecated("Use uk.gov.hmrc.http.HttpReads.Implicits instead. See README for differences.", "11.0.0")
  implicit def readFromJson[O](implicit rds: json.Reads[O], mf: Manifest[O]): HttpReads[O] = new HttpReads[O] {
    def read(method: String, url: String, response: HttpResponse) =
      readJson(method, url, handleResponse(method, url)(response).json)
  }

  @deprecated("Use uk.gov.hmrc.http.HttpReads.Implicits instead. See README for differences.", "11.0.0")
  def readSeqFromJsonProperty[O](name: String)(implicit rds: json.Reads[O], mf: Manifest[O]) = new HttpReads[Seq[O]] {
    def read(method: String, url: String, response: HttpResponse) = response.status match {
      case 204 | 404 => Seq.empty
      case _ =>
        readJson[Seq[O]](method, url, (handleResponse(method, url)(response).json \ name).getOrElse(JsNull)) //Added JsNull here to force validate to fail - replicates existing behaviour
    }
  }

  private def readJson[A](method: String, url: String, jsValue: JsValue)(implicit rds: json.Reads[A], mf: Manifest[A]) =
    jsValue
      .validate[A]
      .fold(
        errs => throw new JsValidationException(method, url, mf.runtimeClass, errs.toString()),
        valid => valid
      )
} 
Example 60
Source File: GraphQlController.scala    From tap   with Apache License 2.0 5 votes vote down vote up
package controllers

import javax.inject.Inject
import models.GraphqlSchema
import models.graphql.GraphqlActions
import play.api.Logger
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.mvc.{Action, AnyContent, InjectedController, Result}
import sangria.ast.Document
import sangria.execution.{ErrorWithResolver, Executor, QueryAnalysisError}
import sangria.marshalling.playJson.{PlayJsonInputUnmarshallerJObject, PlayJsonResultMarshaller}
import sangria.parser.{QueryParser, SyntaxError}
import sangria.schema.Schema
import views.GraphiqlPage

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.{Failure, Success}


class GraphQlController @Inject() (assets: AssetsFinder, gqlSchema: GraphqlSchema, actions: GraphqlActions) extends InjectedController {

  val schema:Schema[GraphqlActions,Unit] = gqlSchema.create

  def graphiql:Action[AnyContent] = Action {
    request => Logger.info("Got Any content request from:" + request.remoteAddress)
    //Ok(views.html.graphiql(assets))
    Ok(GraphiqlPage.render("Explore TAP with GraphiQL"))
  }

  def graphql:Action[JsValue] = Action.async(parse.json) { request =>
    val query = (request.body \ "query").as[String]
    val operation = (request.body \ "operationName").asOpt[String]
    val variables = (request.body \ "variables").asOpt[JsObject].getOrElse(Json.obj())
    Logger.info(s"Query received from ${request.remoteAddress} >>> ${operation.getOrElse("No query")}")
    Logger.info(s"Variables: $variables")
    process(query,operation,variables)
  }

  def process(query:String,name:Option[String],variables:JsObject):Future[Result] = QueryParser.parse(query) match {
    case Success(queryAst) => executeGraphQLQuery(queryAst, name, variables)
    case Failure(error: SyntaxError) => Future.successful(BadRequest(error.getMessage))
    case _ => Future.successful(BadRequest("There was a problem with the request to TAP graphql."))
  }

  def executeGraphQLQuery(query: Document, name: Option[String], vars: JsObject):Future[Result] = {
     Executor.execute(schema, query, actions, operationName = name, variables = vars)
      .map(Ok(_))
      .recover {
        case error: QueryAnalysisError => BadRequest(error.resolveError)
        case error: ErrorWithResolver => InternalServerError(error.resolveError)
      }

  }
} 
Example 61
Source File: GenericHandler.scala    From tap   with Apache License 2.0 5 votes vote down vote up
package controllers.handlers


import io.heta.tap.data.results.StringResult
import play.api.Logger
import play.api.libs.json.{JsValue, Json}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.reflect.runtime.universe._
import scala.util.Try

trait GenericHandler {

  def queryTime(start:Long):Int = (System.currentTimeMillis() - start).toInt

  def validJson(parameters:Option[String]):Option[JsValue] = parameters.flatMap(p => Try(Json.parse(p)).toOption).map(_.result.get)

  def extractParameter[A:TypeTag](paramName:String,parameters:Option[String]):Option[Any] = {
    val jsParams = validJson(parameters)
    Logger.debug(s"JSON: $jsParams")
    jsParams.flatMap { jp =>
      val result = Try((jp \ paramName).toOption).toOption.flatten
      typeOf[A] match {
        case t if t =:= typeOf[String] => Try(result.map(_.as[String])).toOption.flatten // scalastyle:ignore
        case t if t =:= typeOf[Double] => Try(result.map(_.as[Double])).toOption.flatten // scalastyle:ignore
        case t if t =:= typeOf[Int] => Try(result.map(_.as[Int])).toOption.flatten       // scalastyle:ignore
        case _ => None
      }
    }
  }

  def dummyResult(text:String):Future[StringResult] = Future {
    StringResult("This features is not implemented yet")
  }


} 
Example 62
Source File: MyPostgresProfile.scala    From silhouette-vuejs-app   with Apache License 2.0 5 votes vote down vote up
package models.daos

import com.github.tminglei.slickpg._
import play.api.libs.json.{JsValue, Json}
import slick.basic.Capability
import slick.jdbc.JdbcCapabilities
import scala.language.higherKinds

trait MyPostgresProfile extends ExPostgresProfile
  with PgArraySupport
  with PgDate2Support
  with PgRangeSupport
  with PgHStoreSupport
  with PgPlayJsonSupport
  with PgSearchSupport
  with PgNetSupport
  with PgLTreeSupport {
  def pgjson = "jsonb" // jsonb support is in postgres 9.4.0 onward; for 9.3.x use "json"

  import slick.ast._
  import slick.ast.Library._
  import slick.lifted.FunctionSymbolExtensionMethods._

  // Add back `capabilities.insertOrUpdate` to enable native `upsert` support; for postgres 9.5+
  override protected def computeCapabilities: Set[Capability] =
    super.computeCapabilities + JdbcCapabilities.insertOrUpdate

  override val api = MyAPI

  object MyAPI extends API with ArrayImplicits
    with DateTimeImplicits
    with JsonImplicits
    with NetImplicits
    with LTreeImplicits
    with RangeImplicits
    with HStoreImplicits
    with SearchImplicits
    with SearchAssistants {
    implicit val strListTypeMapper = new SimpleArrayJdbcType[String]("text").to(_.toList)
    implicit val playJsonArrayTypeMapper =
      new AdvancedArrayJdbcType[JsValue](pgjson,
        s => utils.SimpleArrayUtils.fromString[JsValue](Json.parse(_))(s).orNull,
        v => utils.SimpleArrayUtils.mkString[JsValue](_.toString())(v)
      ).to(_.toList)

    // Declare the name of an aggregate function:
    val ArrayAgg = new SqlAggregateFunction("array_agg")

    // Implement the aggregate function as an extension method:
    implicit class ArrayAggColumnQueryExtensionMethods[P, C[_]](val q: Query[Rep[P], _, C]) {
      def arrayAgg[B](implicit tm: TypedType[List[B]]) = ArrayAgg.column[List[B]](q.toNode)
    }
  }
}

object MyPostgresProfile extends MyPostgresProfile 
Example 63
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 64
Source File: GoogleCalendarMappers.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.she.mappers

import java.util.UUID

import org.hatdex.hat.api.models.{ EndpointQuery, EndpointQueryFilter, FilterOperator, PropertyQuery }
import org.hatdex.hat.api.models.applications.{ DataFeedItem, DataFeedItemContent, DataFeedItemLocation, DataFeedItemTitle, LocationAddress }
import org.joda.time.{ DateTime, DateTimeZone }
import play.api.libs.json.{ JsValue, Json }

import scala.util.Try

class GoogleCalendarMapper extends DataEndpointMapper {
  override protected val dataDeduplicationField: Option[String] = Some("id")

  def dataQueries(fromDate: Option[DateTime], untilDate: Option[DateTime]): Seq[PropertyQuery] = {
    val eventDateTimePropertyQuery = PropertyQuery(
      List(
        EndpointQuery("calendar/google/events", None, Some(Seq(
          dateFilter(fromDate, untilDate).map(f ⇒ EndpointQueryFilter("start.dateTime", None, f))).flatten), None)),
      Some("start.dateTime"), Some("descending"), None)

    val dateOnlyFilter = if (fromDate.isDefined) {
      Some(FilterOperator.Between(Json.toJson(fromDate.map(_.toString("yyyy-MM-dd"))), Json.toJson(untilDate.map(_.toString("yyyy-MM-dd")))))
    }
    else {
      None
    }

    val eventDatePropertyQuery = PropertyQuery(
      List(
        EndpointQuery("calendar/google/events", None, Some(Seq(
          dateOnlyFilter.map(f ⇒ EndpointQueryFilter("start.date", None, f))).flatten), None)),
      Some("start.date"), Some("descending"), None)

    Seq(eventDateTimePropertyQuery, eventDatePropertyQuery)
  }

  def cleanHtmlTags(input: String): String = {
    input.replaceAll("<br/?>", "\n")
      .replaceAll("&nbsp;", " ")
      .replaceAll("<a [^>]*>([^<]*)</a>", "$1")
  }

  def mapDataRecord(recordId: UUID, content: JsValue, tailRecordId: Option[UUID] = None, tailContent: Option[JsValue] = None): Try[DataFeedItem] = {
    for {
      startDate ← Try((content \ "start" \ "dateTime").asOpt[DateTime]
        .getOrElse((content \ "start" \ "date").as[DateTime])
        .withZone((content \ "start" \ "timeZone").asOpt[String].flatMap(z ⇒ Try(DateTimeZone.forID(z)).toOption).getOrElse(DateTimeZone.getDefault)))
      endDate ← Try((content \ "end" \ "dateTime").asOpt[DateTime]
        .getOrElse((content \ "end" \ "date").as[DateTime])
        .withZone((content \ "end" \ "timeZone").asOpt[String].flatMap(z ⇒ Try(DateTimeZone.forID(z)).toOption).getOrElse(DateTimeZone.getDefault)))
      timeIntervalString ← Try(eventTimeIntervalString(startDate, Some(endDate)))
      itemContent ← Try(DataFeedItemContent(
        (content \ "description").asOpt[String].map(cleanHtmlTags), None, None, None))
      location ← Try(DataFeedItemLocation(
        geo = None,
        address = (content \ "location").asOpt[String].map(l ⇒ LocationAddress(None, None, Some(l), None, None)), // TODO integrate with geocoding API for full location information?
        tags = None))
    } yield {
      val title = DataFeedItemTitle(s"${(content \ "summary").as[String]}", Some(s"${timeIntervalString._1} ${timeIntervalString._2.getOrElse("")}"), Some("event"))
      val loc = Some(location).filter(l ⇒ l.address.isDefined || l.geo.isDefined || l.tags.isDefined)
      DataFeedItem("google", startDate, Seq("event"),
        Some(title), Some(itemContent), loc)
    }
  }
} 
Example 65
Source File: PropertyConnectorSpec.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.connectors


import mocks.MockHttp
import mocks.config.MockAppConfig
import mocks.httpParser.MockSelfAssessmentHttpParser
import play.api.http.Status
import play.api.libs.json.{JsValue, Json}
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import support.UnitSpec
import uk.gov.hmrc.http.HttpResponse

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

class PropertyConnectorSpec extends UnitSpec
  with MockHttp
  with MockAppConfig
  with MockSelfAssessmentHttpParser {

  class Setup {
    object TestConnector extends PropertyConnector(
      mockHttp,
      mockSelfAssessmentHttpParser,
      mockAppConfig
    )
    MockAppConfig.propertyUrl returns propertyUrl
  }

  lazy val propertyUrl = "test-sa-api-url"
  val path = "/2.0/test-path"

  "get" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.OK, Some(Json.obj()))

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.GET[SelfAssessmentOutcome](s"$propertyUrl$path").returns(Future.successful(Right(response)))
        await(TestConnector.get(path)(hc)) shouldBe Right(response)
      }
    }
  }

  "post" should {
    "return an HttpResponse" when {
      "a successful HttpResponse with no content is returned" in new Setup {
        val response = HttpResponse(Status.NO_CONTENT)
        val requestJson = Json.obj("test" -> "request json")

        MockHttp.POST[JsValue, SelfAssessmentOutcome](s"$propertyUrl$path", requestJson).returns(Future.successful(Right(response)))
        await(TestConnector.post(path, requestJson)(hc)) shouldBe Right(response)
      }
    }
  }
} 
Example 66
Source File: SelfEmploymentConnectorSpec.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.connectors

import mocks.MockHttp
import mocks.config.MockAppConfig
import mocks.httpParser.MockSelfAssessmentHttpParser
import play.api.http.Status
import play.api.libs.json.{JsValue, Json}
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import support.UnitSpec
import uk.gov.hmrc.http.HttpResponse

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

class SelfEmploymentConnectorSpec extends UnitSpec
  with MockHttp
  with MockAppConfig
  with MockSelfAssessmentHttpParser {

  class Setup {
    object TestConnector extends SelfEmploymentConnector(
      mockHttp,
      mockSelfAssessmentHttpParser,
      mockAppConfig
    )
    MockAppConfig.selfEmploymentUrl returns selfEmploymentUrl
  }

  lazy val selfEmploymentUrl = "test-sa-api-url"
  val path = "/2.0/test-path"

  "post" should {
    "return an HttpResponse" when {
      "a successful HttpResponse with no content is returned" in new Setup {
        val response  = HttpResponse(Status.NO_CONTENT)
        val requestJson = Json.obj("test" -> "request json")

        MockHttp.POST[JsValue, SelfAssessmentOutcome](s"$selfEmploymentUrl$path", requestJson).returns(Future.successful(Right(response)))
        await(TestConnector.post(path, requestJson)(hc)) shouldBe Right(response)
      }
    }
  }
} 
Example 67
Source File: SavingsAccountsConnectorSpec.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.connectors

import mocks.MockHttp
import mocks.config.MockAppConfig
import mocks.httpParser.MockSelfAssessmentHttpParser
import play.api.http.Status
import play.api.libs.json.{JsValue, Json}
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import support.UnitSpec
import uk.gov.hmrc.http.HttpResponse

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

class SavingsAccountsConnectorSpec extends UnitSpec
  with MockHttp
  with MockAppConfig
  with MockSelfAssessmentHttpParser {

  class Setup {
    object TestConnector extends SavingsAccountConnector(
      mockHttp,
      mockSelfAssessmentHttpParser,
      mockAppConfig
    )
    MockAppConfig.savingsAccountsApiUrl returns savingsAccountsApiUrl
  }

  lazy val savingsAccountsApiUrl = "test-di-api-url"
  val path = "/2.0/test-path"

  "create" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.CREATED, Some(Json.obj()))
        val requestJson = Json.obj("test" -> "request json")

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.POST[JsValue, SelfAssessmentOutcome](s"$savingsAccountsApiUrl$path", requestJson).returns(Future.successful(Right(response)))
        await(TestConnector.post(path, requestJson)(hc)) shouldBe Right(response)
      }
    }
  }

  "retrieve" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.OK, Some(Json.obj()))

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.GET[SelfAssessmentOutcome](s"$savingsAccountsApiUrl$path").returns(Future.successful(Right(response)))
        await(TestConnector.get(path)(hc)) shouldBe Right(response)
      }
    }
  }

  "amend" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.NO_CONTENT)
        val requestJson = Json.obj("test" -> "request json")

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.PUT[JsValue, SelfAssessmentOutcome](s"$savingsAccountsApiUrl$path", requestJson).returns(Future.successful(Right(response)))
        await(TestConnector.put(path, requestJson)(hc)) shouldBe Right(response)
      }
    }
  }

} 
Example 68
Source File: CharitableGivingConnectorSpec.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.connectors

import mocks.MockHttp
import mocks.config.MockAppConfig
import mocks.httpParser.MockSelfAssessmentHttpParser
import play.api.http.Status
import play.api.libs.json.{JsValue, Json}
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import support.UnitSpec
import uk.gov.hmrc.http.HttpResponse

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

class CharitableGivingConnectorSpec extends UnitSpec
  with MockHttp
  with MockAppConfig
  with MockSelfAssessmentHttpParser {

  class Setup {
    object TestConnector extends CharitableGivingConnector(
      mockHttp,
      mockSelfAssessmentHttpParser,
      mockAppConfig
    )
    MockAppConfig.cgApiUrl returns cgApiUrl
  }

  lazy val cgApiUrl = "test-cg-api-url"
  val path = "/2.0/test-path"

  "amend" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.OK, Some(Json.obj()))
        val requestJson = Json.obj("test" -> "request json")

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.PUT[JsValue, SelfAssessmentOutcome](s"$cgApiUrl$path", requestJson).returns(Future.successful(Right(response)))
        await(TestConnector.put(path, requestJson)(hc)) shouldBe Right(response)
      }
    }
  }

  "retrieve" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.OK, Some(Json.obj()))

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.GET[SelfAssessmentOutcome](s"$cgApiUrl$path").returns(Future.successful(Right(response)))
        await(TestConnector.get(path)(hc)) shouldBe Right(response)
      }
    }
  }
} 
Example 69
Source File: DividendsConnectorSpec.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.connectors

import mocks.MockHttp
import mocks.config.MockAppConfig
import mocks.httpParser.MockSelfAssessmentHttpParser
import play.api.http.Status
import play.api.libs.json.{JsValue, Json}
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import support.UnitSpec
import uk.gov.hmrc.http.HttpResponse

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

class DividendsConnectorSpec extends UnitSpec
  with MockHttp
  with MockAppConfig
  with MockSelfAssessmentHttpParser {

  class Setup {
    object TestConnector extends DividendsConnector(
      mockHttp,
      mockSelfAssessmentHttpParser,
      mockAppConfig
    )
    MockAppConfig.dividendsApiUrl returns dividendsApiUrl
  }

  lazy val dividendsApiUrl = "test-di-api-url"
  val path = "/2.0/test-path"

  "amend" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.OK, Some(Json.obj()))
        val requestJson = Json.obj("test" -> "request json")

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.PUT[JsValue, SelfAssessmentOutcome](s"$dividendsApiUrl$path", requestJson).returns(Future.successful(Right(response)))
        await(TestConnector.put(path, requestJson)(hc)) shouldBe Right(response)
      }
    }
  }

  "retrieve" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.OK, Some(Json.obj()))

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.GET[SelfAssessmentOutcome](s"$dividendsApiUrl$path").returns(Future.successful(Right(response)))
        await(TestConnector.get(path)(hc)) shouldBe Right(response)
      }
    }
  }
} 
Example 70
Source File: CrystallisationConnectorSpec.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.connectors


import mocks.MockHttp
import mocks.config.MockAppConfig
import mocks.httpParser.MockSelfAssessmentHttpParser
import play.api.http.Status
import play.api.libs.json.{JsValue, Json}
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import support.UnitSpec
import uk.gov.hmrc.http.HttpResponse

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

class CrystallisationConnectorSpec extends UnitSpec
  with MockHttp
  with MockAppConfig
  with MockSelfAssessmentHttpParser {


  class Setup {

    object TestConnector extends CrystallisationConnector(
      mockHttp,
      mockSelfAssessmentHttpParser,
      mockAppConfig
    )

    MockAppConfig.crystallisationApiUrl returns crystallisationApiUrl
  }

  lazy val crystallisationApiUrl = "test-di-api-url"
  val path = "/2.0/test-path"

  "post" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response = HttpResponse(Status.NO_CONTENT)
        val requestJson = Json.obj("test" -> "request json")

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.POST[JsValue, SelfAssessmentOutcome](s"$crystallisationApiUrl$path", requestJson)
          .returns(Future.successful(Right(response)))
        await(TestConnector.post(path, requestJson)(hc)) shouldBe Right(response)
      }
    }
  }

  "postEmpty" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response = HttpResponse(Status.NO_CONTENT)

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.POSTEmpty[SelfAssessmentOutcome](s"$crystallisationApiUrl$path")
          .returns(Future.successful(Right(response)))
        await(TestConnector.postEmpty(path)(hc)) shouldBe Right(response)
      }
    }
  }

  "retrieve" should {
    "return a HttpResponse" when {
      "a successful HttpResponse is returned" in new Setup {
        val response  = HttpResponse(Status.OK, Some(Json.obj()))

        MockSelfAssessmentHttpParser.read.returns(Right(response))
        MockHttp.GET[SelfAssessmentOutcome](s"$crystallisationApiUrl$path")
          .returns(Future.successful(Right(response)))
        await(TestConnector.get(path)(hc)) shouldBe Right(response)
      }
    }
  }
} 
Example 71
Source File: MockCrystallisationConnector.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package mocks.connectors

import mocks.Mock
import org.mockito.stubbing.OngoingStubbing
import org.scalatest.Suite
import play.api.libs.json.JsValue
import router.connectors.CrystallisationConnector
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockCrystallisationConnector extends Mock {_: Suite =>

  val mockCrystallisationConnector = mock[CrystallisationConnector]

  object MockCrystallisationConnector {
    def post(uri: String, body: JsValue): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockCrystallisationConnector.post(eqTo(uri), eqTo(body))(any[HeaderCarrier]()))
    }

    def postEmpty(uri: String): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockCrystallisationConnector.postEmpty(eqTo(uri))(any[HeaderCarrier]()))
    }

    def get(uri: String): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockCrystallisationConnector.get(eqTo(uri))(any[HeaderCarrier]()))
    }
  }

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    reset(mockCrystallisationConnector)
  }

} 
Example 72
Source File: MockPropertyConnector.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package mocks.connectors

import mocks.Mock
import org.mockito.stubbing.OngoingStubbing
import org.scalatest.Suite
import play.api.libs.json.JsValue
import router.connectors.PropertyConnector
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockPropertyConnector extends Mock { _: Suite =>

  val mockPropertyConnector = mock[PropertyConnector]

  object MockPropertyConnector {
    def get(uri: String): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockPropertyConnector.get(eqTo(uri))(any[HeaderCarrier]()))
    }
    def post(uri: String, body: JsValue): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockPropertyConnector.post(eqTo(uri), eqTo(body))(any[HeaderCarrier]()))
    }
  }

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    reset(mockPropertyConnector)
  }
} 
Example 73
Source File: MockSelfEmploymentConnector.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package mocks.connectors

import mocks.Mock
import org.mockito.stubbing.OngoingStubbing
import org.scalatest.Suite
import play.api.libs.json.JsValue
import router.connectors.SelfEmploymentConnector
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockSelfEmploymentConnector extends Mock { _: Suite =>

  val mockSelfEmploymentConnector = mock[SelfEmploymentConnector]

  object MockSelfEmploymentConnector {
    def get(uri: String): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSelfEmploymentConnector.get(eqTo(uri))(any[HeaderCarrier]()))
    }
    def post(uri: String, body: JsValue): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSelfEmploymentConnector.post(eqTo(uri), eqTo(body))(any[HeaderCarrier]()))
    }
  }

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    reset(mockSelfEmploymentConnector)
  }
} 
Example 74
Source File: MockSelfAssessmentConnector.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package mocks.connectors

import mocks.Mock
import org.mockito.stubbing.OngoingStubbing
import org.scalatest.Suite
import play.api.libs.json.JsValue
import router.connectors.SelfAssessmentConnector
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockSelfAssessmentConnector extends Mock { _: Suite =>

  val mockSelfAssessmentConnector = mock[SelfAssessmentConnector]

  object MockSelfAssessmentConnector {
    def get(uri: String): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSelfAssessmentConnector.get(eqTo(uri))(any[HeaderCarrier]()))
    }

    def post(uri: String, body: JsValue): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSelfAssessmentConnector.post(eqTo(uri), eqTo(body))(any[HeaderCarrier]()))
    }

    def postEmpty(uri: String): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSelfAssessmentConnector.postEmpty(eqTo(uri))(any[HeaderCarrier]()))
    }

    def put(uri: String, body: JsValue): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSelfAssessmentConnector.put(eqTo(uri), eqTo(body))(any[HeaderCarrier]()))
    }
  }

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    reset(mockSelfAssessmentConnector)
  }
} 
Example 75
Source File: MockSavingsAccountConnector.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package mocks.connectors

import mocks.Mock
import org.mockito.stubbing.OngoingStubbing
import org.scalatest.Suite
import play.api.libs.json.JsValue
import router.connectors.SavingsAccountConnector
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockSavingsAccountConnector extends Mock { _: Suite =>

  val mockSavingsAccountConnector = mock[SavingsAccountConnector]

  object MockSavingsAccountConnector {
    def post(uri: String, body: JsValue): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSavingsAccountConnector.post(eqTo(uri), eqTo(body))(any[HeaderCarrier]()))
    }

    def get(uri: String): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSavingsAccountConnector.get(eqTo(uri))(any[HeaderCarrier]()))
    }

    def put(uri: String, body: JsValue): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockSavingsAccountConnector.put(eqTo(uri), eqTo(body))(any[HeaderCarrier]()))
    }
  }

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    reset(mockSavingsAccountConnector)
  }
} 
Example 76
Source File: AggregationTests.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.it

import com.typesafe.scalalogging.LazyLogging
import org.scalatest.{AsyncFunSpec, Inspectors, Matchers}
import play.api.libs.json.{JsValue, _}

import scala.collection.mutable.ArrayBuffer
import scala.concurrent.duration._

class AggregationTests extends AsyncFunSpec with Matchers with Inspectors with Helpers with LazyLogging {

  describe("Agg API should") {
    val agg = scala.io.Source.fromURL(this.getClass.getResource("/agg/aggnames_293846.nq"))
    val ingestAgg = {
      Http.post(_in, agg.mkString, Some("text/nquads;charset=UTF-8"), List("format" -> "nquads"), tokenHeader)
    }.map { res =>
        withClue(res) {
          res.status should be(200)
          jsonSuccessPruner(Json.parse(res.payload)) shouldEqual jsonSuccess
        }
    }

    agg.close()


    val path = cmw / "test.agg.org" / "Test201903_05_1501_11" / "testStatsApiTerms"

    val aggForIntField = executeAfterCompletion(ingestAgg) {
      spinCheck(100.millis, true)(Http.get(
        uri = path,
        queryParams = List("op" -> "stats", "format" -> "json", "ap" -> "type:term,field::$http://qa.test.rfnt.com/v1.1/testns/num$,size:3")))
      { r =>
        (Json.parse(r.payload) \ "AggregationResponse" \\ "buckets": @unchecked) match {
          case n: collection.IndexedSeq[JsValue] => (r.status == 200) && n.forall(jsonval=> jsonval.as[JsArray].value.size == 3)
        }
      }.map { res =>
        withClue(res) {
          res.status should be(200)
          val total = (Json.parse(res.payload) \ "AggregationResponse" \\ "buckets").map(jsonval=> jsonval.as[JsArray].value.size)
          total should equal (ArrayBuffer(3))
        }
      }
    }


    val aggForExactTextField = executeAfterCompletion(ingestAgg) {
      spinCheck(100.millis, true)(Http.get(
        uri = path,
        queryParams = List("op" -> "stats", "format" -> "json", "ap" -> "type:term,field::$http://qa.test.rfnt.com/v1.1/testns/Test_Data$,size:2")))
      { r =>
        (Json.parse(r.payload) \ "AggregationResponse" \\ "buckets": @unchecked) match {
          case n: collection.IndexedSeq[JsValue] => (r.status == 200) && n.forall(jsonval=> jsonval.as[JsArray].value.size == 2)
        }
      }.map { res =>
        withClue(res) {
          res.status should be(200)
          val total = (Json.parse(res.payload) \ "AggregationResponse" \\ "buckets").map(jsonval=> jsonval.as[JsArray].value.size)
          total should equal (ArrayBuffer(2))
        }
      }
    }


    val badQueryNonExactTextMatch = executeAfterCompletion(ingestAgg) {
      spinCheck(100.millis, true)(Http.get(
        uri = path,
        queryParams = List("op" -> "stats", "format" -> "json", "ap" -> "type:term,field:$http://qa.test.rfnt.com/v1.1/testns/Test_Data$,size:2")))
      { r =>
        Json.parse(r.payload).toString()
          .contains("Stats API does not support non-exact value operator for text fields. Please use :: instead of :") && r.status == 400
      }.map { res =>
        withClue(res) {
          res.status should be(400)
          val result = (Json.parse(res.payload) \ "error").as[String]
          result should include ("Stats API does not support non-exact value operator for text fields. Please use :: instead of :")
        }
      }
    }



    it("ingest aggnames data successfully")(ingestAgg)
    it("get stats for int field")(aggForIntField)
    it("get exact stats for string field")(aggForExactTextField)
    it("get stats for non exact string field should be bad response")(badQueryNonExactTextMatch)

  }

} 
Example 77
Source File: FitsTypesAndArraySizesSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.json

import org.scalatest.exceptions.TestFailedException
import play.api.libs.json.{JsValue, Json}

class FitsTypesAndArraySizesSpec extends ScalaWebTestJsonBaseSpec with FitsTypeMismatchBehavior {
  path = "/jsonResponse.json.jsp"
  def dijkstra: JsValue = Json.parse(webDriver.getPageSource)

  "The json response representing Edsger Dijkstra" should "use the correct types" in {

    dijkstra fits typesAndArraySizes of
      """{
        | "name": "",
        | "firstName": "",
        | "isTuringAwardWinner": true,
        | "theories": ["", ""],
        | "universities": [{"name": "", "begin": 0, "end": 0}, {}, {}, {}]
        |}
      """.stripMargin
  }
  it should "not have only a single entry in universities" in {
    assertThrows[TestFailedException] {
      dijkstra fits typesAndArraySizes of
      """{
        | "universities": [{}]
        |}
      """.stripMargin
    }
  }
  it should behave like jsonGaugeFitting(typesAndArraySizes)
} 
Example 78
Source File: FitsTypesSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.json

import play.api.libs.json.{JsValue, Json}

class FitsTypesSpec extends ScalaWebTestJsonBaseSpec with FitsTypeMismatchBehavior {
  path = "/jsonResponse.json.jsp"
  def dijkstra: JsValue = Json.parse(webDriver.getPageSource)

  "Fits types" should "report success, when the json gauge contains the same types as the response it is tested against" in {
    dijkstra fits types of
      """{
        | "name": "",
        | "firstName": "",
        | "yearOfBirth": 0,
        | "isTuringAwardWinner": true,
        | "theories": [],
        | "universities": [{"name": "", "begin": 0, "end": 0}],
        | "falseTheories": null
        |}
      """.stripMargin
  }
  it should "provide the fit synonym" in {
    val universities = dijkstra \ "universities"
    universities fit types of
      """
        | [{
        |   "name": "",
        |   "begin": 0,
        |   "end": 0
        | }]
      """.stripMargin
  }
  it should behave like jsonGaugeFitting(types)

} 
Example 79
Source File: ContainsElementFittingSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.json

import play.api.libs.json.{JsLookupResult, JsValue, Json}

class ContainsElementFittingSpec extends ScalaWebTestJsonBaseSpec {
  path = "/jsonResponse.json.jsp"
  def dijkstra: JsValue = Json.parse(webDriver.getPageSource)
  def universities: JsLookupResult = { dijkstra \ "universities" }

  "The universities array" should "contain an element with the expected types" in {
    universities containsElementFitting types of
      """{
        | "name": "",
        | "begin": 0,
        | "end": 0
        | } """.stripMargin
  }
  it should "contain an element with the expected values" in {
    universities containsElementFitting values of
      """{
        | "name": "Technische Universiteit Eindhoven",
        | "begin": 1962,
        | "end": 1984
        | }""".stripMargin
  }
} 
Example 80
Source File: FitsValuesIgnoringArrayOrderSpec.scala    From ScalaWebTest   with Apache License 2.0 5 votes vote down vote up
package org.scalawebtest.integration.json

import org.scalatest.exceptions.TestFailedException
import play.api.libs.json.{JsValue, Json}

class FitsValuesIgnoringArrayOrderSpec extends ScalaWebTestJsonBaseSpec with FitsTypeMismatchBehavior {
  path = "/jsonResponse.json.jsp"
  def dijkstra: JsValue = Json.parse(webDriver.getPageSource)

  "Fits types" should "report success, when the json gauge contains the same valuesIgnoringArrayOrder as the response it is tested against" in {
    dijkstra fits valuesIgnoringArrayOrder of
      """{
        | "name": "Dijkstra",
        | "firstName": "Edsger",
        | "yearOfBirth": 1930,
        | "isTuringAwardWinner": true,
        | "theories": [
        |   "graph theory",
        |   "shortest path"
        | ],
        | "falseTheories": null
        |}
      """.stripMargin
  }
  it should "provide the fit synonym" in {
    val universities = dijkstra \ "universities"
    universities fit valuesIgnoringArrayOrder of
      """
        | [
        |   { "name": "Universität Leiden","begin": 1948, "end": 1956 },
        |   { "name": "University of Texas at Austin", "begin": 1984, "end": 1999 },
        |   { "name": "Technische Universiteit Eindhoven", "begin": 1962, "end": 1984 },
        |   { "name": "Mathematisch Centrum Amsterdam", "begin": 1951, "end": 1959 }
        | ]
      """.stripMargin
  }
  it should behave like jsonGaugeFitting(valuesIgnoringArrayOrder)
  it should "fail when a String doesn't contain the correct value" in {
    assertThrows[TestFailedException]{
      dijkstra fits valuesIgnoringArrayOrder of """{"name": "Turing"}"""
    }
  }
  it should "fail when an Int doesn't contain the correct value" in {
    assertThrows[TestFailedException]{
      dijkstra fits valuesIgnoringArrayOrder of """{"yearOfBirth": 1995}"""
    }
  }
  it should "fail when a Boolean doesn't contain the correct value" in {
    assertThrows[TestFailedException]{
      dijkstra fits valuesIgnoringArrayOrder of """{"isTuringAwardWinner": false}"""
    }
  }
  it should "fail when an array is not complete" in {
    assertThrows[TestFailedException]{
      dijkstra fits valuesIgnoringArrayOrder of
        """{
          | "theories": ["shortest path"]
          |}""".stripMargin
    }
  }
  it should "fail when an array contains a wrong value" in {
    assertThrows[TestFailedException]{
      dijkstra fits valuesIgnoringArrayOrder of
        """{
          | "theories": ["shortest path", "relational algebra"]
          |}""".stripMargin
    }
  }
} 
Example 81
Source File: Callback.scala    From auth0-scala-samples   with MIT License 5 votes vote down vote up
package controllers

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import javax.inject.Inject
import play.api.cache._
import play.api.http.HeaderNames
import play.api.http.MimeTypes
import play.api.libs.json.JsValue
import play.api.libs.json.Json
import play.api.libs.json.Json.toJsFieldJsValueWrapper
import play.api.libs.ws._
import play.api.mvc.{Action, AnyContent, Controller}
import helpers.Auth0Config
import play.api.Configuration

class Callback @Inject() (cache: CacheApi, ws: WSClient, configuration: Configuration) extends Controller {

  private val config = Auth0Config.get(configuration)

  def callback(codeOpt: Option[String] = None, stateOpt: Option[String] = None): Action[AnyContent] = Action.async { request =>
    val sessionId = request.session.get("id").get
    if (stateOpt == cache.get(sessionId + "state")) {
      (for {
        code <- codeOpt
      } yield {
        getToken(code, sessionId).flatMap { case (idToken, accessToken) =>
          getUser(accessToken).map { user =>
            cache.set(request.session.get("id").get + "profile", user)
            Redirect(routes.User.index())
          }

        }.recover {
          case ex: IllegalStateException => Unauthorized(ex.getMessage)
        }
      }).getOrElse(Future.successful(BadRequest("No parameters supplied")))
    } else {
      Future.successful(BadRequest("Invalid state parameter"))
    }
  }

  def getToken(code: String, sessionId: String): Future[(String, String)] = {
    var audience = config.audience
    if (config.audience == ""){
      audience = String.format("https://%s/userinfo",config.domain)
    }
    val tokenResponse = ws.url(String.format("https://%s/oauth/token", config.domain)).
      withHeaders(HeaderNames.ACCEPT -> MimeTypes.JSON).
      post(
        Json.obj(
          "client_id" -> config.clientId,
          "client_secret" -> config.secret,
          "redirect_uri" -> config.callbackURL,
          "code" -> code,
          "grant_type"-> "authorization_code",
          "audience" -> audience
        )
      )

    tokenResponse.flatMap { response =>
      (for {
        idToken <- (response.json \ "id_token").asOpt[String]
        accessToken <- (response.json \ "access_token").asOpt[String]
      } yield {
        cache.set(sessionId + "id_token", idToken)
        cache.set(sessionId + "access_token", accessToken)
        Future.successful((idToken, accessToken))
      }).getOrElse(Future.failed[(String, String)](new IllegalStateException("Tokens not sent")))
    }

  }

  def getUser(accessToken: String): Future[JsValue] = {
    val userResponse = ws.url(String.format("https://%s/userinfo", config.domain))
      .withQueryString("access_token" -> accessToken)
      .get()

    userResponse.flatMap(response => Future.successful(response.json))
  }
} 
Example 82
Source File: OpenIdInfoQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OpenIDInfo
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
import play.api.libs.json.{ JsValue, Json }

object OpenIdInfoQueries extends BaseQueries[OpenIDInfo] {
  override protected val tableName = "openid_info"
  override protected val columns = Seq("provider", "key", "id", "attributes", "created")
  override protected val idColumns = Seq("provider", "key")
  override protected val searchColumns = Seq("key")

  val getById = GetById
  val removeById = RemoveById

  case class CreateOpenIdInfo(l: LoginInfo, o: OpenIDInfo) extends Statement {
    override val sql = insertSql
    override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(o)
  }

  case class UpdateOpenIdInfo(l: LoginInfo, o: OpenIDInfo) extends Statement {
    override val sql = s"update $tableName set id = ?, attributes = ?, created = ? where provider = ? and key = ?"
    val attributes = Json.prettyPrint(Json.toJson(o.attributes))
    override val values = toDataSeq(o) ++ Seq(l.providerID, l.providerKey)
  }

  override protected def fromRow(row: Row) = {
    val id = row.as[String]("id")
    val attributesString = row.as[String]("attributes")
    val attributes = Json.parse(attributesString).as[Map[String, JsValue]].map(x => x._1 -> x._2.as[String])
    OpenIDInfo(id, attributes)
  }

  override protected def toDataSeq(o: OpenIDInfo) = {
    val attributes = Json.prettyPrint(Json.toJson(o.attributes))
    Seq(o.id, attributes, new LocalDateTime())
  }
} 
Example 83
Source File: OAuth2InfoQueries.scala    From play-silhouette-postgres-async-seed   with Apache License 2.0 5 votes vote down vote up
package models.queries

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.OAuth2Info
import jdub.async.{ Row, Statement }
import jdub.async.queries.BaseQueries
import org.joda.time.LocalDateTime
import play.api.libs.json.{ JsValue, Json }

object OAuth2InfoQueries extends BaseQueries[OAuth2Info] {
  override protected val tableName = "oauth2_info"
  override protected val columns = Seq("provider", "key", "access_token", "token_type", "expires_in", "refresh_token", "params", "created")
  override protected val idColumns = Seq("provider", "key")
  override protected val searchColumns = Seq("key", "access_token")

  val getById = GetById
  val removeById = RemoveById

  case class CreateOAuth2Info(l: LoginInfo, o: OAuth2Info) extends Statement {
    override val sql = insertSql
    override val values = Seq(l.providerID, l.providerKey) ++ toDataSeq(o)
  }

  case class UpdateOAuth2Info(l: LoginInfo, o: OAuth2Info) extends Statement {
    override val sql = {
      s"update $tableName set access_token = ?, token_type = ?, expires_in = ?, refresh_token = ?, params = ?, created = ? where provider = ? and key = ?"
    }
    override val values = toDataSeq(o) ++ Seq(l.providerID, l.providerKey)
  }

  override protected def fromRow(row: Row) = {
    val params = row.asOpt[String]("params").map { p =>
      Json.parse(p).as[Map[String, JsValue]].map(x => x._1 -> x._2.as[String])
    }
    OAuth2Info(
      accessToken = row.as[String]("access_token"),
      tokenType = row.asOpt[String]("token_type"),
      expiresIn = row.asOpt[Int]("expires_in"),
      refreshToken = row.asOpt[String]("refresh_token"),
      params = params
    )
  }

  override protected def toDataSeq(o: OAuth2Info) = {
    val params = o.params.map(p => Json.prettyPrint(Json.toJson(p)))
    Seq(o.accessToken, o.tokenType, o.expiresIn, o.refreshToken, params, new LocalDateTime())
  }
} 
Example 84
Source File: ApplicationController.scala    From monix-sample   with Apache License 2.0 5 votes vote down vote up
package controllers

import akka.actor.ActorSystem
import akka.stream.Materializer
import engine.{BackPressuredWebSocketActor, DataProducer, SimpleWebSocketActor}
import monix.execution.Scheduler.Implicits.global
import play.api.Environment
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import scala.concurrent.duration._

class ApplicationController()
  (implicit env: Environment, as: ActorSystem, m: Materializer)
  extends Controller with JSONFormats {

  def index = Action {
    Ok(views.html.index(env))
  }

  def backPressuredStream(periodMillis: Int, seed: Long) =
    WebSocket.accept[JsValue, JsValue] { request =>
      val obs = new DataProducer(periodMillis.millis, seed)
      ActorFlow.actorRef(out => BackPressuredWebSocketActor.props(obs, out))
    }

  def simpleStream(periodMillis: Int, seed: Long) =
    WebSocket.accept[JsValue, JsValue] { request =>
      val obs = new DataProducer(periodMillis.millis, seed)
      ActorFlow.actorRef(out => SimpleWebSocketActor.props(obs, out))
    }
} 
Example 85
Source File: SimpleWebSocketActor.scala    From monix-sample   with Apache License 2.0 5 votes vote down vote up
package engine

import akka.actor.{Actor, ActorRef, Props}
import engine.SimpleWebSocketActor.Next
import monix.execution.Scheduler
import monix.reactive.Observable
import monix.execution.Ack.Continue
import monix.execution.cancelables.CompositeCancelable
import org.joda.time.{DateTime, DateTimeZone}
import play.api.libs.json.{JsValue, Json, Writes}

import scala.concurrent.duration._
import engine.BackPressuredWebSocketActor._


class SimpleWebSocketActor[T: Writes]
  (producer: Observable[T], out: ActorRef)(implicit s: Scheduler)
  extends Actor {

  def receive: Receive = {
    case Next(jsValue) =>
      out ! jsValue
  }

  private[this] val subscription =
    CompositeCancelable()

  override def preStart(): Unit = {
    super.preStart()

    val source = {
      val initial = Observable.evalOnce(initMessage(now()))
      val obs = initial ++ producer.map(x => Json.toJson(x))
      val timeout = obs.debounce(3.seconds).map(_ => keepAliveMessage(now()))
      Observable.merge(obs, timeout)
    }

    subscription += source.subscribe { jsValue =>
      self ! Next(jsValue)
      Continue
    }
  }

  override def postStop(): Unit = {
    subscription.cancel()
    super.postStop()
  }

  def now(): Long =
    DateTime.now(DateTimeZone.UTC).getMillis
}

object SimpleWebSocketActor {
  
  case class Next(value: JsValue)
} 
Example 86
Source File: Helpers.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
import play.api.libs.json.{JsArray, JsValue}

import scala.language.{implicitConversions, postfixOps}
import scala.util.{Failure, Try}


trait Helpers {
  implicit class JsValueExtensions(v: JsValue) {
    def getValue =  withPrettyException({(v\"value").as[String]}, "getValue")
    def getQuad = withPrettyException({(v\"quad").as[String]}, "getQuad")
    def getArr(prop: String): collection.Seq[JsValue] =
      withPrettyException(_=>{((v \ prop).get: @unchecked) match { case JsArray(seq) => seq }},"getArr",prop)

    def getFirst(prop: String) = getArr(prop).head
    def getValueOfFirst(prop: String) = getFirst(prop).getValue

    private def withPrettyException(f: => String, desc: String):String = Try(f).getOrElse(throw new Exception(s"Operation $desc failed on $v"))
    private def withPrettyException(f: String => collection.Seq[JsValue], desc: String, prop: String):collection.Seq[JsValue] = {
      Try(f(prop)).recoverWith {
        case t: Throwable =>
          Failure(new Exception(s"""Operation $desc("$prop") failed on $v""",t))
      }.get
    }
  }
} 
Example 87
Source File: ElasticsearchChecker.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.ctrl.checkers

import cmwell.ctrl.config.Config
import cmwell.ctrl.utils.ProcUtil
import cmwell.util.http.{SimpleHttpClient => Http}
import com.typesafe.scalalogging.LazyLogging

import scala.concurrent.Future
import play.api.libs.json.{JsValue, Json}

import scala.concurrent.ExecutionContext.Implicits.global


object ElasticsearchChecker extends Checker with LazyLogging {
  override val storedStates: Int = 10
  override def check: Future[ComponentState] = {
    val url = s"http://${Config.pingIp}:9201/_cluster/health"
    val res = Http.get(url)
    val hasMaster = ProcUtil.checkIfProcessRun("es-master") > 0
    res
      .map { r =>
        if (r.status == 200) {
          val json: JsValue = Json.parse(r.payload)
          val status = json.\("status").as[String]
          val n = (json \ "number_of_nodes").as[Int]
          val d = (json \ "number_of_data_nodes").as[Int]
          val p = (json \ "active_primary_shards").as[Int]
          val s = (json \ "active_shards").as[Int](implicitly)
          status match {
            case "green"  => ElasticsearchGreen(n, d, p, s, hasMaster)
            case "yellow" => ElasticsearchYellow(n, d, p, s, hasMaster)
            case "red"    => ElasticsearchRed(n, d, p, s, hasMaster)
            case _        => throw new Exception("Bad status")
          }
        } else
          ElasticsearchBadCode(r.status, hasMaster)
      }
      .recover {
        case e: Throwable => {
          logger.error("ElasticsearchChecker check failed with an exception: ", e)
          ElasticsearchDown(hasMaster)
        }
      }
  }
} 
Example 88
Source File: MockDividendsConnector.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package mocks.connectors

import mocks.Mock
import org.mockito.stubbing.OngoingStubbing
import org.scalatest.Suite
import play.api.libs.json.JsValue
import router.connectors.DividendsConnector
import router.httpParsers.SelfAssessmentHttpParser.SelfAssessmentOutcome
import uk.gov.hmrc.http.HeaderCarrier

import scala.concurrent.Future

trait MockDividendsConnector extends Mock { _: Suite =>

  val mockDividendsConnector = mock[DividendsConnector]

  object MockDividendsConnector {
    def put(uri: String, body: JsValue): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockDividendsConnector.put(eqTo(uri), eqTo(body))(any[HeaderCarrier]()))
    }

    def get(uri: String): OngoingStubbing[Future[SelfAssessmentOutcome]] = {
      when(mockDividendsConnector.get(eqTo(uri))(any[HeaderCarrier]()))
    }
  }

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    reset(mockDividendsConnector)
  }
} 
Example 89
Source File: DatastreamHandlerUnitSpec.scala    From play-auditing   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.audit.handler

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import org.scalatest.Inspectors
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import play.api.inject.DefaultApplicationLifecycle
import play.api.libs.json.{JsString, JsValue}
import uk.gov.hmrc.audit.HandlerResult

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

class DatastreamHandlerUnitSpec extends AnyWordSpecLike with Inspectors with Matchers with ScalaFutures {

  val datastreamHandler = new DatastreamHandler(
    scheme         = "http",
    host           = "localhost",
    port           = 1234,
    path           = "/some/path",
    connectTimeout = 2000.millis,
    requestTimeout = 2000.millis,
    userAgent      = "the-micro-service-name",
    materializer   = ActorMaterializer()(ActorSystem()),
    lifecycle      = new DefaultApplicationLifecycle()
    ) {
    override def sendHttpRequest(event: JsValue)(implicit ec: ExecutionContext): Future[HttpResult] =
      Future.successful(HttpResult.Response(event.as[String].toInt))
  }

  "Any Datastream response" should {
    "Return Success for any response code of 204" in {
      val result = datastreamHandler.sendEvent(JsString("204")).futureValue
      result shouldBe HandlerResult.Success
    }

    "Return Failure for any response code of 3XX or 401-412 or 414-499 or 5XX" in {
      forAll((300 to 399) ++ (401 to 412) ++ (414 to 499) ++ (500 to 599)) { code =>
        val result = datastreamHandler.sendEvent(JsString(code.toString)).futureValue
        result shouldBe HandlerResult.Failure
      }
    }

    "Return Rejected for any response code of 400 or 413" in {
      forAll(Seq(400, 413)) { code =>
        val result = datastreamHandler.sendEvent(JsString(code.toString)).futureValue
        result shouldBe HandlerResult.Rejected
      }
    }
  }
} 
Example 90
Source File: HttpHandler.scala    From play-auditing   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.audit.handler

import java.io.IOException
import java.net.URL
import java.util.concurrent.TimeoutException

import akka.stream.Materializer
import org.slf4j.{Logger, LoggerFactory}
import play.api.inject.ApplicationLifecycle
import play.api.libs.json.JsValue

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


sealed trait HttpResult
object HttpResult {
  case class Response(statusCode: Int) extends HttpResult
  case object Malformed extends HttpResult
  case class Failure(msg: String, nested: Option[Throwable] = None) extends Exception(msg, nested.orNull) with HttpResult
}

abstract class HttpHandler(
  endpointUrl      : URL,
  userAgent        : String,
  connectTimeout   : Duration,
  requestTimeout   : Duration,
  materializer     : Materializer,
  lifecycle        : ApplicationLifecycle
) {
  private val logger: Logger = LoggerFactory.getLogger(getClass)

  val HTTP_STATUS_CONTINUE = 100

  val wsClient: WSClient = {
    implicit val m = materializer
    val wsClient = WSClient(connectTimeout, requestTimeout, userAgent)
    lifecycle.addStopHook { () =>
      logger.info("Closing play-auditing http connections...")
      wsClient.close()
      Future.successful(())
    }
    wsClient
  }

  def sendHttpRequest(event: JsValue)(implicit ec: ExecutionContext): Future[HttpResult] =
    try {
      logger.debug(s"Sending audit request to URL ${endpointUrl.toString}")

      wsClient.url(endpointUrl.toString)
        .post(event)
        .map { response =>
          val httpStatusCode = response.status
          logger.debug(s"Got status code : $httpStatusCode")
          response.body
          logger.debug("Response processed and closed")

          if (httpStatusCode >= HTTP_STATUS_CONTINUE) {
            logger.info(s"Got status code $httpStatusCode from HTTP server.")
            HttpResult.Response(httpStatusCode)
          } else {
            logger.warn(s"Malformed response (status $httpStatusCode) returned from server")
            HttpResult.Malformed
          }
        }.recover {
          case e: TimeoutException =>
            HttpResult.Failure("Error opening connection, or request timed out", Some(e))
          case e: IOException =>
            HttpResult.Failure("Error opening connection, or request timed out", Some(e))
        }
    } catch {
      case t: Throwable =>
        Future.successful(HttpResult.Failure("Error sending HTTP request", Some(t)))
    }
} 
Example 91
Source File: DatastreamHandler.scala    From play-auditing   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.audit.handler

import java.net.URL

import akka.stream.Materializer
import org.slf4j.{Logger, LoggerFactory}
import play.api.inject.ApplicationLifecycle
import play.api.libs.json.JsValue
import uk.gov.hmrc.audit.HandlerResult
import uk.gov.hmrc.audit.HandlerResult.{Failure, Rejected, Success}

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

class DatastreamHandler(
  scheme        : String,
  host          : String,
  port          : Integer,
  path          : String,
  connectTimeout: Duration,
  requestTimeout: Duration,
  userAgent     : String,
  materializer  : Materializer,
  lifecycle     : ApplicationLifecycle
) extends HttpHandler(
  endpointUrl    = new URL(s"$scheme://$host:$port$path"),
  userAgent      = userAgent,
  connectTimeout = connectTimeout,
  requestTimeout = requestTimeout,
  materializer   = materializer,
  lifecycle      = lifecycle
) with AuditHandler {

  private val logger: Logger = LoggerFactory.getLogger(getClass)

  override def sendEvent(event: JsValue)(implicit ec: ExecutionContext): Future[HandlerResult] =
    sendEvent(event, retryIfMalformed = true)

  private def sendEvent(event: JsValue, retryIfMalformed: Boolean)(implicit ec: ExecutionContext): Future[HandlerResult] =
    sendHttpRequest(event).flatMap {
      case HttpResult.Response(status) =>
        Future.successful(status match {
          case 204 => Success
          case 400 => logger.warn("Malformed request rejected by Datastream")
                      Rejected
          case 413 => logger.warn("Too large request rejected by Datastream")
                      Rejected
          case _   => logger.error(s"Unknown return value $status")
                      Failure
        })
      case HttpResult.Malformed =>
        if (retryIfMalformed) {
          logger.warn("Malformed response on first request, retrying")
          sendEvent(event, retryIfMalformed = false)
        } else {
          logger.warn("Malformed response on second request, failing")
          Future.successful(Failure)
        }
      case HttpResult.Failure(msg, exceptionOption) =>
        exceptionOption match {
          case None     => logger.error(msg)
          case Some(ex) => logger.error(msg, ex)
        }
        Future.successful(Failure)
    }
} 
Example 92
Source File: AuditSerialiser.scala    From play-auditing   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.audit.serialiser

import play.api.libs.json.{JsString, JsValue, Json, Writes}
import uk.gov.hmrc.play.audit.model.{DataCall, DataEvent, ExtendedDataEvent, MergedDataEvent}
import java.time.{Instant, ZoneId}
import java.time.format.DateTimeFormatter

object DateWriter {
  // Datastream does not support default X offset (i.e. `Z` must be `+0000`)
  implicit def instantWrites = new Writes[Instant] {
    private val dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

    def writes(instant: Instant): JsValue =
      JsString(dateFormat.withZone(ZoneId.of("UTC")).format(instant))
  }
}

trait AuditSerialiserLike {
  def serialise(event: DataEvent): JsValue
  def serialise(event: ExtendedDataEvent): JsValue
  def serialise(event: MergedDataEvent): JsValue
}

class AuditSerialiser extends AuditSerialiserLike {
  private implicit val dateWriter: Writes[Instant] = DateWriter.instantWrites
  private implicit val dataEventWriter: Writes[DataEvent] = Json.writes[DataEvent]
  private implicit val dataCallWriter: Writes[DataCall] = Json.writes[DataCall]
  private implicit val extendedDataEventWriter: Writes[ExtendedDataEvent] = Json.writes[ExtendedDataEvent]
  private implicit val mergedDataEventWriter: Writes[MergedDataEvent] = Json.writes[MergedDataEvent]

  override def serialise(event: DataEvent): JsValue =
    Json.toJson(event)

  override def serialise(event: ExtendedDataEvent): JsValue =
    Json.toJson(event)

  override def serialise(event: MergedDataEvent): JsValue =
    Json.toJson(event)
}

object AuditSerialiser extends AuditSerialiser 
Example 93
Source File: GraphQlClient.scala    From graphcool-framework   with Apache License 2.0 5 votes vote down vote up
package cool.graph.graphql

import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import cool.graph.akkautil.SingleThreadedActorSystem
import play.api.libs.json.{JsPath, JsValue, Json, Reads}

import scala.concurrent.Future
import scala.util.{Failure, Success, Try}

trait GraphQlClient {
  def sendQuery(query: String): Future[GraphQlResponse]
}

object GraphQlClient {
  private implicit lazy val actorSystem       = SingleThreadedActorSystem("graphql-client")
  private implicit lazy val actorMaterializer = ActorMaterializer()(actorSystem)
  private implicit lazy val akkaHttp          = Http()(actorSystem)

  def apply(uri: String, headers: Map[String, String] = Map.empty): GraphQlClient = {
    GraphQlClientImpl(uri, headers, akkaHttp)
  }
}

case class GraphQlResponse(status: Int, body: String) {
  def bodyAs[T](path: String)(implicit reads: Reads[T]): Try[T] = {
    def jsPathForElements(pathElements: Seq[String], current: JsPath = JsPath()): JsPath = {
      if (pathElements.isEmpty) {
        current
      } else {
        jsPathForElements(pathElements.tail, current \ pathElements.head)
      }
    }
    val jsPath      = jsPathForElements(path.split('.'))
    val actualReads = jsPath.read(reads)
    jsonBody.map(_.as(actualReads))
  }

  val is2xx: Boolean = status >= 200 && status <= 299
  val is200: Boolean = status == 200
  val is404: Boolean = status == 404

  def isSuccess: Boolean = deserializedBody match {
    case Success(x) => x.errors.isEmpty && is200
    case Failure(e) => false
  }

  def isFailure: Boolean       = !isSuccess
  def firstError: GraphQlError = deserializedBody.get.errors.head

  private lazy val deserializedBody: Try[GraphQlResponseJson] = {
    for {
      body     <- jsonBody
      response <- Try { body.as(JsonReaders.graphqlResponseReads) }
    } yield response
  }

  lazy val jsonBody: Try[JsValue] = Try(Json.parse(body))
}

case class GraphQlResponseJson(data: JsValue, errors: Seq[GraphQlError])
case class GraphQlError(message: String, code: Int)

object JsonReaders {
  import play.api.libs.functional.syntax._
  import play.api.libs.json._

  implicit lazy val graphqlErrorReads = Json.reads[GraphQlError]
  implicit lazy val graphqlResponseReads = (
    (JsPath \ "data").read[JsValue] and
      (JsPath \ "errors").readNullable[Seq[GraphQlError]].map(_.getOrElse(Seq.empty))
  )(GraphQlResponseJson.apply _)
} 
Example 94
Source File: UpdateGitRepo.scala    From graphcool-framework   with Apache License 2.0 5 votes vote down vote up
import play.api.libs.json.{JsSuccess, JsValue, Json}

import scalaj.http.{Base64, Http, HttpRequest}

object GithubClient {
  def apply(): GithubClient = GithubClient(Env.read("GITHUB_ACCESS_TOKEN"))
}

case class GithubClient(accessToken: String) {
  import JsonFormatting._

  val host       = "https://api.github.com"
  val authHeader = "Authorization" -> s"token $accessToken"

  def updateFile(owner: String, repo: String, filePath: String, newContent: String, branch: String): Unit = {
    getCurrentSha(owner, repo, filePath, branch) match {
      case Some(currentSha) =>
        updateContentsOfFile(owner, repo, filePath, currentSha, newContent, branch)
        println(s"Updated file $filePath in other repo successfully.")
      case None =>
        println(s"Branch $branch in other repo does not seem to exist. Won't update file.")
    }
  }

  def getCurrentSha(owner: String, repo: String, filePath: String, branch: String): Option[String] = {
    val request = baseRequest(urlPath(owner, repo, filePath, branch))
    request.asJson(200, 404).validate[GetContentResponse](getContentReads) match {
      case JsSuccess(parsed, _) => Some(parsed.sha)
      case _                    => None
    }
  }

  def updateContentsOfFile(owner: String, repo: String, filePath: String, sha: String, newContent: String, branch: String): JsValue = {
    val request = baseRequest(urlPath(owner, repo, filePath))
    val payload = UpdateContentsRequest(
      message = s"Updated by the SBT Task in the open source repo to: $newContent",
      content = Base64.encodeString(newContent),
      sha = sha,
      branch = branch
    )
    request.put(Json.toJson(payload)(updateContentsWrites).toString).asJson(200)
  }

  def urlPath(owner: String, repo: String, filePath: String, branch: String): String = urlPath(owner, repo, filePath) + s"?ref=$branch"
  def urlPath(owner: String, repo: String, filePath: String): String                 = s"/repos/$owner/$repo/contents/$filePath"
  def baseRequest(path: String)                                                      = Http(s"$host$path").headers(authHeader).header("content-type", "application/json")

  implicit class HttpRequestExtensions(httpRequest: HttpRequest) {
    def asJson(allowedStatusCodes: Int*): JsValue = {
      val response          = httpRequest.asString
      val isAllowedResponse = allowedStatusCodes.contains(response.code)
      require(isAllowedResponse, s"The request did not result in an expected status code. Allowed status are $allowedStatusCodes. The response was: $response")
      Json.parse(response.body)
    }
  }
}

object JsonFormatting {
  import play.api.libs.json._

  case class GetContentResponse(sha: String)
  case class UpdateContentsRequest(message: String, content: String, sha: String, branch: String)

  implicit val getContentReads      = Json.reads[GetContentResponse]
  implicit val updateContentsWrites = Json.writes[UpdateContentsRequest]
}

object Env {
  def read(name: String) = sys.env.getOrElse(name, sys.error(s"Env var $name must be set"))
} 
Example 95
Source File: TestSpec.scala    From study-category-theory   with Apache License 2.0 5 votes vote down vote up
package com.github.dnvriend

import akka.actor.ActorSystem
import akka.stream.Materializer
import akka.util.Timeout
import org.scalatest._
import org.scalatest.concurrent.{ Eventually, ScalaFutures }
import org.scalatestplus.play.guice.GuiceOneServerPerSuite
import play.api.inject.BindingKey
import play.api.libs.json.{ JsValue, Json, Writes }
import play.api.test.WsTestClient

import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }
import scala.reflect.ClassTag
import scala.util.Try

object Person {
  implicit val format = Json.format[Person]
  implicit class ValueObjectOps(val self: Person) {
    def toJson: JsValue = Json.toJson(self)
  }
  implicit class IterableOps(val self: Iterable[Person]) {
    def toJson: JsValue = Json.toJson(self)
  }
}
final case class Person(firstName: String, age: Int)

class TestSpec extends FlatSpec
    with Matchers
    with GivenWhenThen
    with OptionValues
    with TryValues
    with ScalaFutures
    with WsTestClient
    with BeforeAndAfterAll
    with BeforeAndAfterEach
    with Eventually
    with GuiceOneServerPerSuite {

  def getComponent[A: ClassTag] = app.injector.instanceOf[A]
  def getNamedComponent[A](name: String)(implicit ct: ClassTag[A]): A =
    app.injector.instanceOf[A](BindingKey(ct.runtimeClass.asInstanceOf[Class[A]]).qualifiedWith(name))

  // set the port number of the HTTP server
  override lazy val port: Int = getNamedComponent[Int]("test.port")
  implicit val timeout: Timeout = getComponent[Timeout]
  implicit val pc: PatienceConfig = PatienceConfig(timeout = 30.seconds, interval = 300.millis)
  implicit val system: ActorSystem = getComponent[ActorSystem]
  implicit val ec: ExecutionContext = getComponent[ExecutionContext]
  implicit val mat: Materializer = getComponent[Materializer]

  // ================================== Supporting Operations ====================================
  def id: String = java.util.UUID.randomUUID().toString

  implicit class PimpedFuture[T](self: Future[T]) {
    def toTry: Try[T] = Try(self.futureValue)
  }

  
  final val FirstName: String = "John"
  final val LastName: String = "Doe"

  override protected def beforeEach(): Unit = {
  }
} 
Example 96
Source File: HatDataStatsProcessorSpec.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.api.service.monitoring

import java.util.UUID

import akka.stream.Materializer
import com.google.inject.AbstractModule
import net.codingwell.scalaguice.ScalaModule
import org.hatdex.hat.api.models.{ EndpointData, Owner }
import org.hatdex.hat.api.service.applications.{ TestApplicationProvider, TrustedApplicationProvider }
import org.hatdex.hat.api.service.monitoring.HatDataEventBus.DataCreatedEvent
import org.hatdex.hat.authentication.models.HatUser
import org.hatdex.hat.dal.ModelTranslation
import org.hatdex.hat.resourceManagement.FakeHatConfiguration
import org.joda.time.DateTime
import org.specs2.mock.Mockito
import org.specs2.specification.Scope
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{ JsValue, Json }
import play.api.test.PlaySpecification
import play.api.{ Application, Logger }

class HatDataStatsProcessorSpec extends PlaySpecification with Mockito with HatDataStatsProcessorContext {

  val logger = Logger(this.getClass)

  sequential

  "The `computeInboundStats` method" should {
    "Correctly count numbers of values for simple objects" in {
      val service = application.injector.instanceOf[HatDataStatsProcessor]
      val stats = service.computeInboundStats(simpleDataCreatedEvent)

      import org.hatdex.hat.api.json.DataStatsFormat._
      logger.debug(s"Got back stats: ${Json.prettyPrint(Json.toJson(stats))}")

      stats.logEntry must be equalTo "test item"
      stats.statsType must be equalTo "inbound"
      stats.stats.length must be equalTo 1
      val endpointStats = stats.stats.head
      endpointStats.endpoint must be equalTo "testendpoint"

      endpointStats.propertyStats("field") must equalTo(1)
      endpointStats.propertyStats("date") must equalTo(1)
      endpointStats.propertyStats("date_iso") must equalTo(1)
      endpointStats.propertyStats("anotherField") must equalTo(1)
      endpointStats.propertyStats("object.objectField") must equalTo(1)
      endpointStats.propertyStats("object.objectFieldArray[]") must equalTo(3)
      endpointStats.propertyStats("object.objectFieldObjectArray[].subObjectName") must equalTo(2)
      endpointStats.propertyStats("object.objectFieldObjectArray[].subObjectName2") must equalTo(2)
    }
  }

}

trait HatDataStatsProcessorContext extends Scope {
  import scala.concurrent.ExecutionContext.Implicits.global
  // Setup default users for testing
  val owner = HatUser(UUID.randomUUID(), "hatuser", Some("pa55w0rd"), "hatuser", Seq(Owner()), enabled = true)

  class ExtrasModule extends AbstractModule with ScalaModule {
    override def configure(): Unit = {
      bind[TrustedApplicationProvider].toInstance(new TestApplicationProvider(Seq()))
    }
  }

  lazy val application: Application = new GuiceApplicationBuilder()
    .configure(FakeHatConfiguration.config)
    .overrides(new ExtrasModule)
    .build()

  implicit lazy val materializer: Materializer = application.materializer

  val simpleJson: JsValue = Json.parse(
    """
      | {
      |   "field": "value",
      |   "date": 1492699047,
      |   "date_iso": "2017-04-20T14:37:27+00:00",
      |   "anotherField": "anotherFieldValue",
      |   "object": {
      |     "objectField": "objectFieldValue",
      |     "objectFieldArray": ["objectFieldArray1", "objectFieldArray2", "objectFieldArray3"],
      |     "objectFieldObjectArray": [
      |       {"subObjectName": "subObject1", "subObjectName2": "subObject1-2"},
      |       {"subObjectName": "subObject2", "subObjectName2": "subObject2-2"}
      |     ]
      |   }
      | }
    """.stripMargin)

  val simpleDataCreatedEvent = DataCreatedEvent(
    "testhat.hubofallthings.net",
    ModelTranslation.fromInternalModel(owner).clean,
    DateTime.now(), "test item",
    Seq(
      EndpointData("testendpoint", Option(UUID.randomUUID()), None, None, simpleJson, None)))
} 
Example 97
Source File: EndpointSubscriberService.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.api.service.monitoring

import org.hatdex.hat.api.models._
import org.hatdex.hat.api.service.richData.JsonDataTransformer
import org.joda.time.DateTime
import play.api.Logger
import play.api.libs.json.Reads._
import play.api.libs.json.{ JsArray, JsValue, Json, _ }

case class EndpointQueryException(message: String = "", cause: Throwable = None.orNull)
  extends Exception(message, cause)

object EndpointSubscriberService {
  private val logger = Logger(this.getClass)

  def matchesBundle(data: EndpointData, bundle: EndpointDataBundle): Boolean = {
    val endpointQueries = bundle.flatEndpointQueries
      .filter(_.endpoint == data.endpoint)

    endpointQueries collectFirst {
      case q if q.filters.isEmpty                             => true
      case q if q.filters.exists(dataMatchesFilters(data, _)) => true
    } getOrElse {
      false
    }
  }

  private implicit val dateReads: Reads[DateTime] = JodaReads.jodaDateReads("yyyy-MM-dd'T'HH:mm:ssZ")

  private def dataMatchesFilters(data: EndpointData, filters: Seq[EndpointQueryFilter]): Boolean = {
    logger.debug("Checking if data matches provided filters")
    filters.exists { f =>
      data.data.transform(JsonDataTransformer.parseJsPath(f.field).json.pick).fold(
        invalid = {
          _ => false
        },
        valid = {
          fieldData =>
            val data = f.transformation map {
              case _: FieldTransformation.Identity =>
                fieldData
              case trans: FieldTransformation.DateTimeExtract =>
                Json.toJson(dateTimeExtractPart(fieldData.as[DateTime](dateReads), trans.part))
              case trans: FieldTransformation.TimestampExtract =>
                Json.toJson(dateTimeExtractPart(new DateTime(fieldData.as[Long] * 1000L), trans.part))
              case trans =>
                throw EndpointQueryException(s"Invalid field transformation `${trans.getClass.getName}` for ongoing tracking")
            } getOrElse {
              fieldData
            }
            f.operator match {
              case op: FilterOperator.In       => jsContains(op.value, data)
              case op: FilterOperator.Contains => jsContains(data, op.value)
              case op: FilterOperator.Between  => jsLessThanOrEqual(op.lower, data) && jsLessThanOrEqual(data, op.upper)
              case op                          => throw EndpointQueryException(s"Invalid match operator `${op.getClass.getName}` for ongoing tracking")
            }

        })
    }
  }

  private def dateTimeExtractPart(d: DateTime, part: String): Int = {
    part match {
      case "milliseconds" => d.getMillisOfSecond
      case "second"       => d.getSecondOfMinute
      case "minute"       => d.getMinuteOfDay
      case "hour"         => d.getHourOfDay
      case "day"          => d.getDayOfMonth
      case "week"         => d.getWeekOfWeekyear
      case "month"        => d.getMonthOfYear
      case "year"         => d.getYear
      case "decade"       => d.getYear / 10
      case "century"      => d.getCenturyOfEra
      case "dow"          => d.getDayOfWeek
      case "doy"          => d.getDayOfYear
      case "epoch"        => (d.getMillis / 1000).toInt
    }
  }

  private def jsContains(contains: JsValue, contained: JsValue): Boolean = {
    (contains, contained) match {
      case (a: JsObject, b: JsObject) => b.fieldSet.subsetOf(a.fieldSet)
      case (a: JsArray, b: JsArray)   => a.value.containsSlice(b.value)
      case (a: JsArray, b: JsValue)   => a.value.contains(b)
      case (a: JsValue, b: JsValue)   => a == b
      case _                          => false
    }
  }

  private def jsLessThanOrEqual(a: JsValue, b: JsValue): Boolean = {
    (a, b) match {
      case (aa: JsNumber, bb: JsNumber) => aa.value <= bb.value
      case (aa: JsString, bb: JsString) => aa.value <= bb.value
      case _                            => false
    }
  }
} 
Example 98
Source File: NotablesMappers.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.she.mappers

import java.util.UUID

import org.hatdex.hat.api.models.{ EndpointQuery, EndpointQueryFilter, PropertyQuery }
import org.hatdex.hat.api.models.applications.{ DataFeedItem, DataFeedItemContent, DataFeedItemLocation, DataFeedItemMedia, DataFeedItemTitle, LocationGeo }
import org.joda.time.DateTime
import play.api.libs.json.JsValue

import scala.util.Try

class NotablesFeedMapper extends DataEndpointMapper {
  def dataQueries(fromDate: Option[DateTime], untilDate: Option[DateTime]): Seq[PropertyQuery] = {
    Seq(PropertyQuery(
      List(EndpointQuery("rumpel/notablesv1", None,
        dateFilter(fromDate, untilDate).map(f ⇒ Seq(EndpointQueryFilter("created_time", None, f))), None)), Some("created_time"), Some("descending"), None))
  }

  def mapDataRecord(recordId: UUID, content: JsValue, tailRecordId: Option[UUID] = None, tailContent: Option[JsValue] = None): Try[DataFeedItem] = {
    for {
      title ← Try(if ((content \ "currently_shared").as[Boolean]) {
        DataFeedItemTitle("You posted", None, Some("public"))
      }
      else {
        DataFeedItemTitle("You posted", None, Some("private"))
      })
      itemContent ← Try(if ((content \ "photov1").isDefined && (content \ "photov1" \ "link").as[String].nonEmpty) {
        DataFeedItemContent(Some((content \ "message").as[String]), None, Some(Seq(
          DataFeedItemMedia(Some((content \ "photov1" \ "link").as[String]), Some((content \ "photov1" \ "link").as[String])))), None)
      }
      else {
        DataFeedItemContent(Some((content \ "message").as[String]), None, None, None)
      })
      location ← Try(if ((content \ "locationv1").isDefined) {
        Some(DataFeedItemLocation(Some(LocationGeo(
          (content \ "locationv1" \ "longitude").as[Double],
          (content \ "locationv1" \ "latitude").as[Double])), None, None))
      }
      else {
        None
      })
      date ← Try((content \ "created_time").as[DateTime])
      tags ← Try((content \ "shared_on").as[Seq[String]])
    } yield DataFeedItem("notables", date, tags, Some(title), Some(itemContent), location)
  }
} 
Example 99
Source File: SpotifyMappers.scala    From HAT2.0   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.hatdex.hat.she.mappers

import java.util.UUID

import org.hatdex.hat.api.models.{ EndpointQuery, EndpointQueryFilter, PropertyQuery }
import org.hatdex.hat.api.models.applications.{ DataFeedItem, DataFeedItemContent, DataFeedItemMedia, DataFeedItemTitle }
import org.hatdex.hat.she.models.StaticDataValues
import org.joda.time.DateTime
import play.api.libs.json.{ JsError, JsNumber, JsObject, JsResult, JsSuccess, JsValue, __ }

import scala.util.Try

class SpotifyFeedMapper extends DataEndpointMapper {
  def dataQueries(fromDate: Option[DateTime], untilDate: Option[DateTime]): Seq[PropertyQuery] = {
    Seq(PropertyQuery(
      List(EndpointQuery("spotify/feed", None,
        dateFilter(fromDate, untilDate).map(f ⇒ Seq(EndpointQueryFilter("played_at", None, f))), None)), Some("played_at"), Some("descending"), None))
  }

  def mapDataRecord(recordId: UUID, content: JsValue, tailRecordId: Option[UUID] = None, tailContent: Option[JsValue] = None): Try[DataFeedItem] = {
    for {
      durationSeconds ← Try((content \ "track" \ "duration_ms").as[Int] / 1000)
      title ← Try(
        DataFeedItemTitle("You listened", None, Some(s"${"%02d".format(durationSeconds / 60)}:${"%02d".format(durationSeconds % 60)}")))
      itemContent ← Try(DataFeedItemContent(
        Some(
          s"""${(content \ "track" \ "name").as[String]},
             |${(content \ "track" \ "artists").as[Seq[JsObject]].map(a ⇒ (a \ "name").as[String]).mkString(", ")},
             |${(content \ "track" \ "album" \ "name").as[String]}""".stripMargin),
        None,
        Some(
          Seq(DataFeedItemMedia((content \ "track" \ "album" \ "images" \ 0 \ "url").asOpt[String], (content \ "track" \ "album" \ "images" \ 0 \ "url").asOpt[String]))),
        None))
      date ← Try((content \ "played_at").as[DateTime])
    } yield DataFeedItem("spotify", date, Seq(), Some(title), Some(itemContent), None)
  }
}

class SpotifyProfileStaticDataMapper extends StaticDataEndpointMapper {
  def dataQueries(): Seq[PropertyQuery] = {
    Seq(PropertyQuery(
      List(
        EndpointQuery("spotify/profile", None, None, None)), Some("dateCreated"), Some("descending"), Some(1)))
  }

  def mapDataRecord(recordId: UUID, content: JsValue, endpoint: String): Seq[StaticDataValues] = {
    val eventualData = content.validate[JsObject]
    eventualData match {
      case JsSuccess(value, _) =>

        val lastPartOfEndpointString = endpoint.split("/").last
        val maybeTransformedData = transformData(value).flatMap(item => item.validate[Map[String, JsValue]])
        maybeTransformedData match {
          case JsSuccess(data, _) =>

            Seq(StaticDataValues(lastPartOfEndpointString, (data - "images" - "external_urls")))
          case e: JsError =>

            logger.error(s"Couldn't validate static data JSON for $endpoint. $e")
            Seq()
        }
      case e: JsError =>
        logger.error(s"Couldn't validate static data JSON for $endpoint. $e")
        Seq()
    }
  }

  private def transformData(rawData: JsObject): JsResult[JsValue] = {
    val transformation = __.json.update(
      __.read[JsObject].map(profile => {
        val followers = (profile \ "followers" \ "total").asOpt[JsNumber].getOrElse(JsNumber(0))

        profile ++ JsObject(Map(
          "followers" -> followers))
      }))

    rawData.transform(transformation)
  }
} 
Example 100
Source File: SelfAssessmentUserType.scala    From pertax-frontend   with Apache License 2.0 5 votes vote down vote up
package models

import play.api.libs.json.{JsDefined, JsError, JsResult, JsString, JsSuccess, JsValue, Json, Reads, Writes}
import uk.gov.hmrc.domain.SaUtr

sealed trait SelfAssessmentUserType

sealed trait SelfAssessmentUser extends SelfAssessmentUserType {
  def saUtr: SaUtr
}

object SelfAssessmentUserType {
  val cacheId = "SelfAssessmentUser"

  val activatedSa = ActivatedOnlineFilerSelfAssessmentUser.toString
  val notActivatedSa = NotYetActivatedOnlineFilerSelfAssessmentUser.toString
  val wrongCredsSa = WrongCredentialsSelfAssessmentUser.toString
  val notEnrolledSa = NotEnrolledSelfAssessmentUser.toString
  val nonFilerSa = NonFilerSelfAssessmentUser.toString

  implicit val writes = new Writes[SelfAssessmentUserType] {
    override def writes(o: SelfAssessmentUserType): JsValue = o match {
      case ActivatedOnlineFilerSelfAssessmentUser(utr) =>
        Json.obj("_type" -> JsString(activatedSa), "utr" -> JsString(utr.toString))
      case NotYetActivatedOnlineFilerSelfAssessmentUser(utr) =>
        Json.obj("_type" -> JsString(notActivatedSa), "utr" -> JsString(utr.toString))
      case WrongCredentialsSelfAssessmentUser(utr) =>
        Json.obj("_type" -> JsString(wrongCredsSa), "utr" -> JsString(utr.toString))
      case NotEnrolledSelfAssessmentUser(utr) =>
        Json.obj("_type" -> JsString(notEnrolledSa), "utr" -> JsString(utr.toString))
      case NonFilerSelfAssessmentUser =>
        Json.obj("_type" -> JsString(nonFilerSa))
    }
  }

  implicit val reads = new Reads[SelfAssessmentUserType] {
    override def reads(json: JsValue): JsResult[SelfAssessmentUserType] =
      (json \ "_type", json \ "utr") match {

        case (JsDefined(JsString(`activatedSa`)), JsDefined(JsString(utr))) =>
          JsSuccess(ActivatedOnlineFilerSelfAssessmentUser(SaUtr(utr)))
        case (JsDefined(JsString(`notActivatedSa`)), JsDefined(JsString(utr))) =>
          JsSuccess(NotYetActivatedOnlineFilerSelfAssessmentUser(SaUtr(utr)))
        case (JsDefined(JsString(`wrongCredsSa`)), JsDefined(JsString(utr))) =>
          JsSuccess(WrongCredentialsSelfAssessmentUser(SaUtr(utr)))
        case (JsDefined(JsString(`notEnrolledSa`)), JsDefined(JsString(utr))) =>
          JsSuccess(NotEnrolledSelfAssessmentUser(SaUtr(utr)))
        case (JsDefined(JsString(`nonFilerSa`)), _) =>
          JsSuccess(NonFilerSelfAssessmentUser)
        case _ => JsError("Could not read SelfAssessmentUserType")
      }
  }
}

case class ActivatedOnlineFilerSelfAssessmentUser(saUtr: SaUtr) extends SelfAssessmentUser
case class NotYetActivatedOnlineFilerSelfAssessmentUser(saUtr: SaUtr) extends SelfAssessmentUser
case class WrongCredentialsSelfAssessmentUser(saUtr: SaUtr) extends SelfAssessmentUser
case class NotEnrolledSelfAssessmentUser(saUtr: SaUtr) extends SelfAssessmentUser
case object NonFilerSelfAssessmentUser extends SelfAssessmentUserType 
Example 101
Source File: JobDescription.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.s2jobs

import play.api.libs.json.{JsValue, Json}
import org.apache.s2graph.s2jobs.task._
import org.apache.s2graph.s2jobs.udfs.UdfOption

case class JobDescription(
                           name:String,
                           udfs: Seq[UdfOption],
                           sources:Seq[Source],
                           processes:Seq[task.Process],
                           sinks:Seq[Sink]
                         )

object JobDescription extends Logger {
  val dummy = JobDescription("dummy", Nil, Nil, Nil, Nil)

  def apply(jsVal:JsValue):JobDescription = {
    implicit val TaskConfReader = Json.reads[TaskConf]
    implicit val UdfOptionReader = Json.reads[UdfOption]

    logger.debug(s"JobDescription: ${jsVal}")

    val jobName = (jsVal \ "name").as[String]
    val udfs = (jsVal \ "udfs").asOpt[Seq[UdfOption]].getOrElse(Nil)
    val sources = (jsVal \ "source").asOpt[Seq[TaskConf]].getOrElse(Nil).map(conf => getSource(conf))
    val processes = (jsVal \ "process").asOpt[Seq[TaskConf]].getOrElse(Nil).map(conf => getProcess(conf))
    val sinks = (jsVal \ "sink").asOpt[Seq[TaskConf]].getOrElse(Nil).map(conf => getSink(jobName, conf))

    JobDescription(jobName, udfs, sources, processes, sinks)
  }

  def getSource(conf:TaskConf):Source = {
    conf.`type` match {
      case "kafka" => new KafkaSource(conf)
      case "file"  => new FileSource(conf)
      case "hive" => new HiveSource(conf)
      case "jdbc" => new JdbcSource(conf)
      case "s2graph" => new S2GraphSource(conf)
      case _ => throw new IllegalArgumentException(s"unsupported source type : ${conf.`type`}")
    }
  }

  def getProcess(conf:TaskConf): task.Process = {
    conf.`type` match {
      case "sql" => new SqlProcess(conf)
      case "custom" =>
        val customClassOpt = conf.options.get("class")
        customClassOpt match {
          case Some(customClass:String) =>
            logger.debug(s"custom class init.. $customClass")

            Class.forName(customClass)
              .getConstructor(classOf[TaskConf])
              .newInstance(conf)
              .asInstanceOf[task.Process]

          case None => throw new IllegalArgumentException(s"custom class name is not exist.. ${conf}")
        }

      case _ => throw new IllegalArgumentException(s"unsupported process type : ${conf.`type`}")
    }
  }

  def getSink(jobName:String, conf:TaskConf):Sink = {
    conf.`type` match {
      case "kafka" => new KafkaSink(jobName, conf)
      case "file" => new FileSink(jobName, conf)
      case "es" => new ESSink(jobName, conf)
      case "s2graph" => new S2GraphSink(jobName, conf)
      case "jdbc" => new JdbcSink(jobName, conf)
      case "custom" =>
        val customClassOpt = conf.options.get("class")
        customClassOpt match {
          case Some(customClass:String) =>
            logger.debug(s"custom class for sink init.. $customClass")

            Class.forName(customClass)
              .getConstructor(classOf[String], classOf[TaskConf])
              .newInstance(jobName, conf)
              .asInstanceOf[task.Sink]

          case None => throw new IllegalArgumentException(s"sink custom class name is not exist.. ${conf}")
        }
      case _ => throw new IllegalArgumentException(s"unsupported sink type : ${conf.`type`}")
    }

  }
} 
Example 102
Source File: Grok.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.s2jobs.udfs

import org.apache.s2graph.s2jobs.utils.GrokHelper
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types.{DataType, StructType}
import play.api.libs.json.{JsValue, Json}

class Grok extends Udf {
  import org.apache.spark.sql.functions.udf

  def register(ss: SparkSession, name:String, options:Map[String, String]) = {
    // grok
    val patternDir = options.getOrElse("patternDir", "/tmp")
    val patternFiles = options.getOrElse("patternFiles", "").split(",").toSeq
    val patterns = Json.parse(options.getOrElse("patterns", "{}")).asOpt[Map[String, String]].getOrElse(Map.empty)
    val compilePattern = options("compilePattern")
    val schemaOpt = options.get("schema")

    patternFiles.foreach { patternFile =>
      ss.sparkContext.addFile(s"${patternDir}/${patternFile}")
    }

    implicit val grok = GrokHelper.getGrok(name, patternFiles, patterns, compilePattern)

    val f = if(schemaOpt.isDefined) {
      val schema = DataType.fromJson(schemaOpt.get)
      implicit val keys:Array[String] = schema.asInstanceOf[StructType].fieldNames
      udf(GrokHelper.grokMatchWithSchema _, schema)
    } else {
      udf(GrokHelper.grokMatch _)
    }


    ss.udf.register(name, f)
  }
} 
Example 103
Source File: JobLauncher.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.s2jobs

import org.apache.s2graph.s2jobs.udfs.Udf
import org.apache.spark.sql.SparkSession
import play.api.libs.json.{JsValue, Json}

import scala.io.Source

case class JobOption(
                      name:String = "S2BatchJob",
                      confType:String = "db",
                      jobId:Int = -1,
                      confFile:String = ""
                    )

object JobLauncher extends Logger {

  def parseArguments(args: Array[String]): JobOption = {
    val parser = new scopt.OptionParser[JobOption]("run") {
      opt[String]('n', "name").required().action((x, c) =>
        c.copy(name = x)).text("job display name")

      cmd("file").action((_, c) => c.copy(confType = "file"))
        .text("get config from file")
        .children(
          opt[String]('f', "confFile").required().valueName("<file>").action((x, c) =>
            c.copy(confFile = x)).text("configuration file")
        )

      cmd("db").action((_, c) => c.copy(confType = "db"))
        .text("get config from db")
        .children(
          opt[String]('i', "jobId").required().valueName("<jobId>").action((x, c) =>
            c.copy(jobId = x.toInt)).text("configuration file")
        )
    }

    parser.parse(args, JobOption()) match {
      case Some(o) => o
      case None =>
        parser.showUsage()
        throw new IllegalArgumentException(s"failed to parse options... (${args.mkString(",")}")
    }
  }

  def getConfig(options: JobOption):JsValue = options.confType match {
    case "file" =>
      Json.parse(Source.fromFile(options.confFile).mkString)
    case "db" =>
      throw new IllegalArgumentException(s"'db' option that read config file from database is not supported yet.. ")
  }

  def main(args: Array[String]): Unit = {

    val options = parseArguments(args)
    logger.info(s"Job Options : ${options}")

    val jobDescription = JobDescription(getConfig(options))

    val ss = SparkSession
      .builder()
      .appName(s"${jobDescription.name}")
      .config("spark.driver.maxResultSize", "20g")
      .enableHiveSupport()
      .getOrCreate()

    // register udfs
    jobDescription.udfs.foreach{ udfOption =>
      val udf = Class.forName(udfOption.`class`).newInstance().asInstanceOf[Udf]
      logger.info((s"[udf register] ${udfOption}"))
      udf.register(ss, udfOption.name, udfOption.params.getOrElse(Map.empty))
    }

    val job = new Job(ss, jobDescription)
    job.run()
  }
} 
Example 104
Source File: OrderingUtilBenchmarkSpec.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.core.benchmark

import org.apache.s2graph.core.OrderingUtil._
import org.apache.s2graph.core.{OrderingUtil, SeqMultiOrdering}
import play.api.libs.json.{JsNumber, JsValue}

import scala.util.Random

class OrderingUtilBenchmarkSpec extends BenchmarkCommon {
  "OrderingUtilBenchmarkSpec" should {

    "performance MultiOrdering any" >> {
      val tupLs = (0 until 10) map { i =>
        Random.nextDouble() -> Random.nextLong()
      }

      val seqLs = tupLs.map { tup =>
        Seq(tup._1, tup._2)
      }

      val sorted1 = duration("TupleOrdering double,long") {
        (0 until 1000) foreach { _ =>
          tupLs.sortBy { case (x, y) =>
            -x -> -y
          }
        }
        tupLs.sortBy { case (x, y) =>
          -x -> -y
        }
      }.map { x => x._1 }

      val sorted2 = duration("MultiOrdering double,long") {
        (0 until 1000) foreach { _ =>
          seqLs.sorted(new SeqMultiOrdering[Any](Seq(false, false)))
        }
        seqLs.sorted(new SeqMultiOrdering[Any](Seq(false, false)))
      }.map { x => x.head }

      sorted1.toString() must_== sorted2.toString()
    }

    "performance MultiOrdering double" >> {
      val tupLs = (0 until 50) map { i =>
        Random.nextDouble() -> Random.nextDouble()
      }

      val seqLs = tupLs.map { tup =>
        Seq(tup._1, tup._2)
      }

      duration("MultiOrdering double") {
        (0 until 1000) foreach { _ =>
          seqLs.sorted(new SeqMultiOrdering[Double](Seq(false, false)))
        }
      }

      duration("TupleOrdering double") {
        (0 until 1000) foreach { _ =>
          tupLs.sortBy { case (x, y) =>
            -x -> -y
          }
        }
      }

      1 must_== 1
    }

    "performance MultiOrdering jsvalue" >> {
      val tupLs = (0 until 50) map { i =>
        Random.nextDouble() -> Random.nextLong()
      }

      val seqLs = tupLs.map { tup =>
        Seq(JsNumber(tup._1), JsNumber(tup._2))
      }

      val sorted1 = duration("TupleOrdering double,long") {
        (0 until 1000) foreach { _ =>
          tupLs.sortBy { case (x, y) =>
            -x -> -y
          }
        }
        tupLs.sortBy { case (x, y) =>
          -x -> -y
        }
      }

      val sorted2 = duration("MultiOrdering jsvalue") {
        (0 until 1000) foreach { _ =>
          seqLs.sorted(new SeqMultiOrdering[JsValue](Seq(false, false)))
        }
        seqLs.sorted(new SeqMultiOrdering[JsValue](Seq(false, false)))
      }

      1 must_== 1
    }
  }
} 
Example 105
Source File: GraphOperation.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.counter.core.v2

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import com.typesafe.config.Config
import org.apache.http.HttpStatus
import org.apache.s2graph.counter.config.S2CounterConfig
import org.apache.s2graph.counter.core.v2.ExactStorageGraph._
import org.asynchttpclient.DefaultAsyncHttpClientConfig
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsObject, JsValue, Json}
import scala.concurrent.Await
import scala.concurrent.duration._

class GraphOperation(config: Config) {
  // using play-ws without play app
  implicit val materializer = ActorMaterializer.create(ActorSystem(getClass.getSimpleName))
  private val builder = new DefaultAsyncHttpClientConfig.Builder()
  private val wsClient = new play.api.libs.ws.ning.NingWSClient(builder.build)
  private val s2config = new S2CounterConfig(config)
  val s2graphUrl = s2config.GRAPH_URL
  private[counter] val log = LoggerFactory.getLogger(this.getClass)

  import scala.concurrent.ExecutionContext.Implicits.global

  def createLabel(json: JsValue): Boolean = {
    // fix counter label's schemaVersion
    val newJson = json.as[JsObject] ++ Json.obj("schemaVersion" -> "v2")
    val future = wsClient.url(s"$s2graphUrl/graphs/createLabel").post(newJson).map { resp =>
      resp.status match {
        case HttpStatus.SC_OK =>
          true
        case _ =>
          throw new RuntimeException(s"failed createLabel. errCode: ${resp.status} body: ${resp.body} query: $json")
      }
    }

    Await.result(future, 10 second)
  }

  def deleteLabel(label: String): Boolean = {
    val future = wsClient.url(s"$s2graphUrl/graphs/deleteLabel/$label").put("").map { resp =>
      resp.status match {
        case HttpStatus.SC_OK =>
          true
        case _ =>
          throw new RuntimeException(s"failed deleteLabel. errCode: ${resp.status} body: ${resp.body}")
      }
    }

    Await.result(future, 10 second)
  }
} 
Example 106
Source File: JsonBodyParser.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.rest.play.controllers

import akka.util.ByteString
import org.apache.s2graph.core.utils.logger
import play.api.Play
import play.api.libs.iteratee.Iteratee
import play.api.libs.json.{JsValue, Json}
import play.api.libs.streams.Streams
import play.api.mvc._

import scala.concurrent.Future
import scala.util.control.NonFatal

object s2parse extends BodyParsers {

  import parse._

  val defaultMaxTextLength = 1024 * 512
  val defaultMaxJsonLength = 1024 * 512

  def json: BodyParser[JsValue] = json(defaultMaxTextLength)

  
  def jsonText: BodyParser[String] = when(
    _.contentType.exists(m => m.equalsIgnoreCase("text/json") || m.equalsIgnoreCase("application/json")),
    jsonText(defaultMaxTextLength),
    createBadResult("Expecting text/json or application/json body")
  )

  private def jsonText(maxLength: Int): BodyParser[String] = BodyParser("json, maxLength=" + maxLength) { request =>
    import play.api.libs.iteratee.Execution.Implicits.trampoline
    import play.api.libs.iteratee.Traversable

    val iteratee = Traversable.takeUpTo[ByteString](maxLength)
      .transform(Iteratee.consume[ByteString]().map(_.utf8String))
      .flatMap(Iteratee.eofOrElse(Results.EntityTooLarge))

    Streams.iterateeToAccumulator(iteratee)
  }

  def json(maxLength: Int): BodyParser[JsValue] = when(
    _.contentType.exists(m => m.equalsIgnoreCase("text/json") || m.equalsIgnoreCase("application/json")),
    tolerantJson(maxLength),
    createBadResult("Expecting text/json or application/json body")
  )

  def tolerantJson(maxLength: Int): BodyParser[JsValue] =
    tolerantBodyParser[JsValue]("json", maxLength, "Invalid Json") { (request, bytes) =>
      // Encoding notes: RFC 4627 requires that JSON be encoded in Unicode, and states that whether that's
      // UTF-8, UTF-16 or UTF-32 can be auto detected by reading the first two bytes. So we ignore the declared
      // charset and don't decode, we passing the byte array as is because Jackson supports auto detection.
      Json.parse(bytes)
    }

  private def tolerantBodyParser[A](name: String, maxLength: Int, errorMessage: String)(parser: (RequestHeader, Array[Byte]) => A): BodyParser[A] =
    BodyParser(name + ", maxLength=" + maxLength) { request =>
      import play.api.libs.iteratee.Execution.Implicits.trampoline
      import play.api.libs.iteratee.Traversable

      import scala.util.control.Exception._

      val bodyParser: Iteratee[ByteString, Either[Result, Either[Future[Result], A]]] =
        Traversable.takeUpTo[ByteString](maxLength).transform(
          Iteratee.consume[ByteString]().map { bytes =>
            allCatch[A].either {
              parser(request, bytes.toByteBuffer.array())
            }.left.map {
              case NonFatal(e) =>
                val txt = bytes.utf8String
                logger.error(s"$errorMessage: $txt", e)
                createBadResult(s"$errorMessage: $e")(request)
              case t => throw t
            }
          }
        ).flatMap(Iteratee.eofOrElse(Results.EntityTooLarge))

      Streams.iterateeToAccumulator(bodyParser).mapFuture {
        case Left(tooLarge) => Future.successful(Left(tooLarge))
        case Right(Left(badResult)) => badResult.map(Left.apply)
        case Right(Right(body)) => Future.successful(Right(body))
      }
    }

  private def createBadResult(msg: String): RequestHeader => Future[Result] = { request =>
    Play.maybeApplication.map(_.global.onBadRequest(request, msg))
      .getOrElse(Future.successful(Results.BadRequest))
  }
} 
Example 107
Source File: VertexController.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.rest.play.controllers

import org.apache.s2graph.core.rest.RequestParser
import org.apache.s2graph.core.storage.MutateResponse
import org.apache.s2graph.core.utils.logger
import org.apache.s2graph.core.{ExceptionHandler, GraphExceptions, S2Graph}
import org.apache.s2graph.rest.play.actors.QueueActor
import org.apache.s2graph.rest.play.config.Config
import play.api.libs.json.{JsValue, Json}
import play.api.mvc.{Controller, Result}

import scala.concurrent.Future

object VertexController extends Controller {
  private val s2: S2Graph = org.apache.s2graph.rest.play.Global.s2graph
  private val requestParser: RequestParser = org.apache.s2graph.rest.play.Global.s2parser
  private val walLogHandler: ExceptionHandler = org.apache.s2graph.rest.play.Global.wallLogHandler

  import ApplicationController._
  import ExceptionHandler._
  import play.api.libs.concurrent.Execution.Implicits._

  def tryMutates(jsValue: JsValue, operation: String, serviceNameOpt: Option[String] = None, columnNameOpt: Option[String] = None, withWait: Boolean = false): Future[Result] = {
    if (!Config.IS_WRITE_SERVER) Future.successful(Unauthorized)
    else {
      try {
        val vertices = requestParser.toVertices(jsValue, operation, serviceNameOpt, columnNameOpt)

        for (vertex <- vertices) {
          val kafkaTopic = toKafkaTopic(vertex.isAsync)
          walLogHandler.enqueue(toKafkaMessage(kafkaTopic, vertex, None))
        }

        //FIXME:
        val verticesToStore = vertices.filterNot(v => skipElement(v.isAsync))
        if (verticesToStore.isEmpty) Future.successful(jsonResponse(Json.toJson(Seq.empty[Boolean])))
        else {
          if (withWait) {
            val rets = s2.mutateVertices(verticesToStore, withWait = true).map(_.map(_.isSuccess))
            rets.map(Json.toJson(_)).map(jsonResponse(_))
          } else {
            val rets = verticesToStore.map { vertex => QueueActor.router ! vertex; true }
            Future.successful(jsonResponse(Json.toJson(rets)))
          }
        }
      } catch {
        case e: GraphExceptions.JsonParseException => Future.successful(BadRequest(s"e"))
        case e: Exception =>
          logger.error(s"[Failed] tryMutates", e)
          Future.successful(InternalServerError(s"${e.getStackTrace}"))
      }
    }
  }

  def inserts() = withHeaderAsync(jsonParser) { request =>
    tryMutates(request.body, "insert")
  }

  def insertsWithWait() = withHeaderAsync(jsonParser) { request =>
    tryMutates(request.body, "insert", withWait = true)
  }

  def insertsSimple(serviceName: String, columnName: String) = withHeaderAsync(jsonParser) { request =>
    tryMutates(request.body, "insert", Some(serviceName), Some(columnName))
  }

  def deletes() = withHeaderAsync(jsonParser) { request =>
    tryMutates(request.body, "delete")
  }

  def deletesWithWait() = withHeaderAsync(jsonParser) { request =>
    tryMutates(request.body, "delete", withWait = true)
  }

  def deletesSimple(serviceName: String, columnName: String) = withHeaderAsync(jsonParser) { request =>
    tryMutates(request.body, "delete", Some(serviceName), Some(columnName))
  }

  def deletesAll() = withHeaderAsync(jsonParser) { request =>
    tryMutates(request.body, "deleteAll")
  }

  def deletesAllSimple(serviceName: String, columnName: String) = withHeaderAsync(jsonParser) { request =>
    tryMutates(request.body, "deleteAll", Some(serviceName), Some(columnName))
  }

} 
Example 108
Source File: S2GraphMutateRoute.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.http

import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse, StatusCodes}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{ExceptionHandler, Route}
import com.fasterxml.jackson.core.JsonParseException
import org.apache.s2graph.core.rest.RequestParser
import org.apache.s2graph.core.storage.MutateResponse
import org.apache.s2graph.core.{GraphElement, S2Graph}
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsValue, Json}

import scala.concurrent.{ExecutionContext, Future}

trait S2GraphMutateRoute extends PlayJsonSupport {

  val s2graph: S2Graph
  val logger = LoggerFactory.getLogger(this.getClass)

  lazy val parser = new RequestParser(s2graph)

  lazy val exceptionHandler = ExceptionHandler {
    case ex: JsonParseException => complete(StatusCodes.BadRequest -> ex.getMessage)
    case ex: java.lang.IllegalArgumentException => complete(StatusCodes.BadRequest -> ex.getMessage)
  }

  lazy val mutateVertex = path("vertex" / Segments) { params =>
    implicit val ec = s2graph.ec

    val (operation, serviceNameOpt, columnNameOpt) = params match {
      case operation :: serviceName :: columnName :: Nil => (operation, Option(serviceName), Option(columnName))
      case operation :: Nil => (operation, None, None)
      case _ => throw new RuntimeException("invalid params")
    }

    entity(as[JsValue]) { payload =>
      val future = vertexMutate(payload, operation, serviceNameOpt, columnNameOpt).map(Json.toJson(_))

      complete(future)
    }
  }

  lazy val mutateEdge = path("edge" / Segment) { operation =>
    implicit val ec = s2graph.ec

    entity(as[JsValue]) { payload =>
      val future = edgeMutate(payload, operation, withWait = true).map(Json.toJson(_))

      complete(future)
    }
  }

  def vertexMutate(jsValue: JsValue,
                   operation: String,
                   serviceNameOpt: Option[String] = None,
                   columnNameOpt: Option[String] = None,
                   withWait: Boolean = true)(implicit ec: ExecutionContext): Future[Seq[Boolean]] = {
    val vertices = parser.toVertices(jsValue, operation, serviceNameOpt, columnNameOpt)

    val verticesToStore = vertices.filterNot(_.isAsync)

    s2graph.mutateVertices(verticesToStore, withWait).map(_.map(_.isSuccess))
  }

  def edgeMutate(elementsWithTsv: Seq[(GraphElement, String)], withWait: Boolean)(implicit ec: ExecutionContext): Future[Seq[Boolean]] = {
    val elementWithIdxs = elementsWithTsv.zipWithIndex
    val (elementSync, elementAsync) = elementWithIdxs.partition { case ((element, tsv), idx) => !element.isAsync }

    val retToSkip = elementAsync.map(_._2 -> MutateResponse.Success)
    val (elementsToStore, _) = elementSync.map(_._1).unzip
    val elementsIdxToStore = elementSync.map(_._2)

    s2graph.mutateElements(elementsToStore, withWait).map { mutateResponses =>
      elementsIdxToStore.zip(mutateResponses) ++ retToSkip
    }.map(_.sortBy(_._1).map(_._2.isSuccess))
  }

  def edgeMutate(jsValue: JsValue, operation: String, withWait: Boolean)(implicit ec: ExecutionContext): Future[Seq[Boolean]] = {
    val edgesWithTsv = parser.parseJsonFormat(jsValue, operation)
    edgeMutate(edgesWithTsv, withWait)
  }

  // expose routes
  lazy val mutateRoute: Route =
    post {
      concat(
        handleExceptions(exceptionHandler) {
          mutateVertex
        },
        handleExceptions(exceptionHandler) {
          mutateEdge
        }
      )
    }

} 
Example 109
Source File: MutateRouteSpec.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.http

import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model._
import akka.http.scaladsl.testkit.ScalatestRouteTest
import com.typesafe.config.ConfigFactory
import org.apache.s2graph.core.Management.JsonModel.Prop
import org.apache.s2graph.core.S2Graph
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec}
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsValue, Json}

class MutateRouteSpec extends WordSpec with Matchers with PlayJsonSupport with ScalaFutures with ScalatestRouteTest with S2GraphMutateRoute with BeforeAndAfterAll {

  import scala.collection.JavaConverters._

  val dbUrl = "jdbc:h2:file:./var/metastore_mutate_route;MODE=MYSQL;AUTO_SERVER=true"
  val config =
    ConfigFactory.parseMap(Map("db.default.url" -> dbUrl).asJava)
  lazy val s2graph = new S2Graph(config.withFallback(ConfigFactory.load()))
  override val logger = LoggerFactory.getLogger(this.getClass)

  override def afterAll(): Unit = {
    s2graph.shutdown(true)
  }

  lazy val routes = mutateRoute

  val serviceName = "kakaoFavorites"
  val columnName = "userName"

  "MutateRoute" should {

    "be able to insert vertex (POST /mutate/vertex/insert)" in {
      s2graph.management.createService(serviceName, "localhost", s"${serviceName}-dev", 1, None)
      s2graph.management.createServiceColumn(serviceName, columnName, "string", Seq(Prop("age", "0", "integer")))

      // {"timestamp": 10, "serviceName": "s2graph", "columnName": "user", "id": 1, "props": {}}
      val param = Json.obj(
        "timestamp" -> 10,
        "serviceName" -> serviceName,
        "columnName" -> columnName,
        "id" -> "user_a",
        "props" -> Json.obj(
          "age" -> 20
        )
      )

      val entity = Marshal(param).to[MessageEntity].futureValue
      val request = Post("/vertex/insert").withEntity(entity)

      request ~> routes ~> check {
        status should ===(StatusCodes.OK)
        contentType should ===(ContentTypes.`application/json`)

        val response = entityAs[JsValue]
        response should ===(Json.toJson(Seq(true)))
      }
    }
  }
} 
Example 110
Source File: AdminRouteSpec.scala    From incubator-s2graph   with Apache License 2.0 5 votes vote down vote up
package org.apache.s2graph.http

import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model._
import akka.http.scaladsl.testkit.ScalatestRouteTest
import com.typesafe.config.ConfigFactory
import org.apache.s2graph.core.Management.JsonModel.Prop
import org.apache.s2graph.core.S2Graph
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpec}
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsString, JsValue, Json}

class AdminRoutesSpec extends WordSpec with Matchers with ScalaFutures with ScalatestRouteTest with S2GraphAdminRoute with BeforeAndAfterAll {
  import scala.collection.JavaConverters._

  val dbUrl = "jdbc:h2:file:./var/metastore_admin_route;MODE=MYSQL;AUTO_SERVER=true"
  val config =
    ConfigFactory.parseMap(Map("db.default.url" -> dbUrl).asJava)
  lazy val s2graph = new S2Graph(config.withFallback(ConfigFactory.load()))
  override val logger = LoggerFactory.getLogger(this.getClass)

  override def afterAll(): Unit = {
    s2graph.shutdown(true)
  }

  lazy val routes = adminRoute

  val serviceName = "kakaoFavorites"
  val columnName = "userName"

  "AdminRoute" should {
    "be able to create service (POST /createService)" in {
      val serviceParam = Json.obj(
        "serviceName" -> serviceName,
        "compressionAlgorithm" -> "gz"
      )

      val serviceEntity = Marshal(serviceParam).to[MessageEntity].futureValue
      val request = Post("/createService").withEntity(serviceEntity)

      request ~> routes ~> check {
        status should ===(StatusCodes.Created)
        contentType should ===(ContentTypes.`application/json`)

        val response = entityAs[JsValue]

        (response \\ "name").head should ===(JsString("kakaoFavorites"))
        (response \\ "status").head should ===(JsString("ok"))
      }
    }

    "return service if present (GET /getService/{serviceName})" in {
      val request = HttpRequest(uri = s"/getService/$serviceName")

      request ~> routes ~> check {
        status should ===(StatusCodes.OK)
        contentType should ===(ContentTypes.`application/json`)

        val response = entityAs[JsValue]

        (response \\ "name").head should ===(JsString("kakaoFavorites"))
      }
    }

    "be able to create serviceColumn (POST /createServiceColumn)" in {
      val serviceColumnParam = Json.obj(
        "serviceName" -> serviceName,
        "columnName" -> columnName,
        "columnType" -> "string",
        "props" -> Json.toJson(
          Seq(
            Json.obj("name" -> "age", "defaultValue" -> "-1", "dataType" -> "integer")
          )
        )
      )

      val serviceColumnEntity = Marshal(serviceColumnParam).to[MessageEntity].futureValue
      val request = Post("/createServiceColumn").withEntity(serviceColumnEntity)

      request ~> routes ~> check {
        status should ===(StatusCodes.Created)
        contentType should ===(ContentTypes.`application/json`)

        val response = entityAs[JsValue]

        (response \\ "serviceName").head should ===(JsString("kakaoFavorites"))
        (response \\ "columnName").head should ===(JsString("userName"))
        (response \\ "status").head should ===(JsString("ok"))
      }
    }
  }
} 
Example 111
Source File: DexRestConnector.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.tool.connectors

import cats.syntax.option._
import com.google.common.primitives.Longs
import com.wavesplatform.dex.api.http.protocol.HttpCancelOrder
import com.wavesplatform.dex.cli.ErrorOr
import com.wavesplatform.dex.domain.account.KeyPair
import com.wavesplatform.dex.domain.asset.AssetPair
import com.wavesplatform.dex.domain.bytes.codec.Base58
import com.wavesplatform.dex.domain.crypto
import com.wavesplatform.dex.domain.order.Order
import com.wavesplatform.dex.tool.connectors.RestConnector.ErrorOrJsonResponse
import play.api.libs.json.{JsValue, Json}
import sttp.client._
import sttp.model.MediaType
import sttp.model.Uri.QuerySegment

case class DexRestConnector(target: String) extends RestConnector {

  private val apiUri = s"$targetUri/matcher"

  private def mkCancelRequest(orderId: Order.Id, owner: KeyPair): HttpCancelOrder = {
    val cancelRequest = HttpCancelOrder(owner, orderId.some, None, Array.emptyByteArray)
    val signature     = crypto.sign(owner, cancelRequest.toSign)
    cancelRequest.copy(signature = signature)
  }

  private def cancelOrdersByRequest(cancelRequest: HttpCancelOrder, assetPair: AssetPair): ErrorOrJsonResponse = mkResponse {
    _.post(uri"$apiUri/orderbook/${assetPair.amountAsset}/${assetPair.priceAsset}/cancel")
      .body(Json.stringify(Json toJson cancelRequest))
      .contentType(MediaType.ApplicationJson)
  }

  private def timestampAndSignatureHeaders(owner: KeyPair, timestamp: Long): Map[String, String] = Map(
    "Timestamp" -> timestamp.toString,
    "Signature" -> Base58.encode(crypto.sign(owner, owner.publicKey ++ Longs.toByteArray(timestamp)))
  )

  def placeOrder(order: Order): ErrorOrJsonResponse = mkResponse {
    _.post(uri"$apiUri/orderbook").body(order.jsonStr).contentType(MediaType.ApplicationJson)
  }

  def cancelOrder(orderId: Order.Id, assetPair: AssetPair, owner: KeyPair): ErrorOrJsonResponse =
    cancelOrdersByRequest(mkCancelRequest(orderId, owner), assetPair)

  def cancelOrder(order: Order, owner: KeyPair): ErrorOrJsonResponse = cancelOrder(order.id(), order.assetPair, owner)

  def getOrderStatus(orderId: Order.Id, assetPair: AssetPair): ErrorOrJsonResponse = mkResponse {
    _.get(uri"$apiUri/orderbook/${assetPair.amountAsset}/${assetPair.priceAsset}/$orderId")
  }

  def getTxsByOrderId(id: Order.Id): ErrorOr[Seq[JsValue]] = mkResponse { _.get(uri"$apiUri/transactions/$id") } map { _.as[Seq[JsValue]] }

  def waitForOrderStatus(orderId: Order.Id, assetPair: AssetPair, expectedStatusName: String): ErrorOrJsonResponse =
    repeatRequest { getOrderStatus(orderId, assetPair) } {
      _.map(json => (json \ "status").get.asOpt[String] contains expectedStatusName).getOrElse(false)
    }

  def waitForOrderStatus(order: Order, expectedStatusName: String): ErrorOrJsonResponse =
    waitForOrderStatus(order.id(), order.assetPair, expectedStatusName)

  def getActiveOrdersByPair(keyPair: KeyPair, assetPair: AssetPair): ErrorOr[Seq[JsValue]] = {
    val uri =
      uri"$apiUri/orderbook/${assetPair.amountAsset}/${assetPair.priceAsset}/publicKey/${keyPair.publicKey.toString}"
        .copy(querySegments = List(QuerySegment.KeyValue("activeOnly", "true")))
    mkResponse { _.get(uri).headers(timestampAndSignatureHeaders(keyPair, System.currentTimeMillis)) }.map(_.as[Seq[JsValue]])
  }

  def getMatcherSettings: ErrorOr[JsValue] = mkResponse { _.get(uri"$apiUri/settings") }
} 
Example 112
Source File: NodeRestConnector.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.tool.connectors

import cats.syntax.either._
import com.wavesplatform.dex.cli.ErrorOr
import com.wavesplatform.dex.tool.connectors.Connector.RepeatRequestOptions
import com.wavesplatform.dex.tool.connectors.RestConnector.ErrorOrJsonResponse
import com.wavesplatform.wavesj.Transaction
import com.wavesplatform.wavesj.json.WavesJsonMapper
import play.api.libs.json.jackson.PlayJsonModule
import play.api.libs.json.{JsValue, JsonParserSettings}
import sttp.client._
import sttp.model.MediaType

import scala.annotation.tailrec
import scala.concurrent.duration._

case class NodeRestConnector(target: String, chainId: Byte) extends RestConnector {

  override implicit val repeatRequestOptions: RepeatRequestOptions = {
    val blocksCount = 5
    (
      for {
        currentHeight <- getCurrentHeight
        blocksTs <- getBlockHeadersAtHeightRange((currentHeight - blocksCount).max(0), currentHeight)
          .map(_.map(json => (json \ "timestamp").as[Long]))
          .ensure("0 or 1 blocks have been forged at the moment, try again later")(_.size > 1)
      } yield {
        val timeBetweenBlocks = ((blocksTs.last - blocksTs.head) / blocksCount * 1.5 / 1000).toInt
        RepeatRequestOptions(timeBetweenBlocks, 1.second)
      }
    ).fold(ex => throw new RuntimeException(s"Could not construct repeat request options: $ex"), identity)
  }

  private val mapper: WavesJsonMapper = new WavesJsonMapper(chainId); mapper.registerModule(new PlayJsonModule(JsonParserSettings()))

  def broadcastTx(tx: Transaction): ErrorOrJsonResponse = mkResponse {
    _.post(uri"$targetUri/transactions/broadcast").body(mapper writeValueAsString tx).contentType(MediaType.ApplicationJson)
  }

  def getTxInfo(txId: String): ErrorOrJsonResponse    = mkResponse { _.get(uri"$targetUri/transactions/info/$txId") }
  def getTxInfo(tx: JsValue): ErrorOrJsonResponse     = getTxInfo { (tx \ "id").as[String] }
  def getTxInfo(tx: Transaction): ErrorOrJsonResponse = getTxInfo(tx.getId.toString)

  def getCurrentHeight: ErrorOr[Long] = mkResponse { _.get(uri"$targetUri/blocks/height") }.map(json => (json \ "height").as[Long])

  def getBlockHeadersAtHeightRange(from: Long, to: Long): ErrorOr[Seq[JsValue]] =
    mkResponse { _.get(uri"$targetUri/blocks/headers/seq/$from/$to") }.map(_.as[Seq[JsValue]])

  @tailrec
  final def waitForHeightArise(): ErrorOr[Long] = getCurrentHeight match {
    case Right(origHeight) => repeatRequest(getCurrentHeight) { _.exists(_ > origHeight) }
    case Left(_)           => waitForHeightArise()
  }
} 
Example 113
Source File: package.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.api.http

import com.wavesplatform.dex.domain.account.PublicKey
import com.wavesplatform.dex.domain.asset.{Asset, AssetPair}
import com.wavesplatform.dex.json.{assetDoubleMapFormat, assetMapFormat, assetPairMapFormat}
import com.wavesplatform.dex.queue.QueueEventWithMeta
import play.api.libs.json.{Format, JsValue, Writes}

package object entities {

  type HttpMatcherPublicKey = PublicKey
  type HttpRates            = Map[Asset, Double]
  type HttpOffset           = QueueEventWithMeta.Offset
  type HttpSnapshotOffsets  = Map[AssetPair, HttpOffset]
  type HttpBalance          = Map[Asset, Long]

  implicit val httpMatcherPublicKeyFormat: Format[PublicKey]          = PublicKey.publicKeyJsonFormat
  implicit val httpRatesFormat: Format[HttpRates]                     = assetDoubleMapFormat
  implicit val httpSnapshotOffsetsFormat: Format[HttpSnapshotOffsets] = assetPairMapFormat[Long]
  implicit val httpBalanceFormat: Format[HttpBalance]                 = assetMapFormat[Long]

  implicit class HttpOps[A](private val self: A)(implicit writes: Writes[A]) {
    def toJson: JsValue = writes.writes(self)
  }
} 
Example 114
Source File: OntonetHubEndpointsTest.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package clients

import scala.concurrent.Await

import org.junit.After
import org.junit.Before
import org.junit.Test

import play.api.libs.json.JsLookupResult
import play.api.libs.json.JsValue
import scala.concurrent.duration.Duration

import scala.concurrent.ExecutionContext.Implicits._
import utilities.JSONHelper
import org.junit.Assert
import semantic_manager.yaml.OntonetHubProperty
import clients.HTTPClient

class OntonetHubEndpointsTest {

  var http = HTTPClient
  var hub: OntonetHubClient = null

  @Before
  def before() {
    http.start()
    hub = new OntonetHubClient(http.ws, OntonetHubClient.DEFAULT_CONFIG)
  }

  @After
  def after() {
    http.stop()
  }

  //  @Test
  def testing_hub_find {

    val (host, port) = ("localhost", 8000)
    val (query, lang, limit) = ("nome", "it", 4)

    val http = HTTPClient
    http.start()

    val ws = http.ws

    val future = ws.url(s"http://${host}:${port}/stanbol/ontonethub/ontologies/find")
      .withHeaders(("accept", "application/json"))
      .withHeaders(("content-type", "application/x-www-form-urlencoded"))
      .withFollowRedirects(true)
      .post(s"name=${query}&lang=${lang}&limit=${limit}")
      .map { item =>
        val json = JSONHelper.pretty(item.body)
        println("\n\n")
        println(json)
        item
      }

    val results = Await.result(future, Duration.Inf)
    Assert.assertTrue(results.status == 200)

    http.stop()

  }

  @Test
  def testing_find_property {

    val (query, lang, limit) = ("nome", "it", 2)

    val future = hub.find_property(query, lang, limit)

    // CHECK for de-coupling from swagger
    //      .map(_.map(item => OntonetHubProperty.tupled(OntonetHubClient.models.FindResult.unapply(item).get)))

    println("\n\n############################################ RESULTS")
    val results = Await.result(future, Duration.Inf)
    println(results.mkString("\n\n"))

  }

} 
Example 115
Source File: QueryExecution.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers

import akka.stream.scaladsl.Source
import cats.syntax.show.toShow
import daf.dataset._
import daf.dataset.query.jdbc.{ JdbcResult, QueryFragmentWriterSyntax, Writers }
import daf.dataset.query.Query
import daf.web._
import daf.filesystem._
import daf.instances.FileSystemInstance
import it.gov.daf.common.utils._
import org.apache.hadoop.fs.Path
import play.api.libs.json.JsValue

import scala.concurrent.Future
import scala.util.{ Failure, Success, Try }

trait QueryExecution { this: DatasetController with DatasetExport with FileSystemInstance =>

  private def extractDatabaseName(parent: String, params: FileDatasetParams) = parent.toLowerCase match {
    case "opendata" => params.extraParams.get("theme").map { s => s"opendata__${s.toLowerCase}" } getOrElse "opendata" // append __{theme} for opendata
    case other      => other // use the parent dir for other data
  }

  private def extractTableName(path: Path, params: FileDatasetParams): Try[String] = Try {
    s"${extractDatabaseName(path.getParent.getName, params)}.${path.getName.toLowerCase}"
  }

  private def extractTableName(params: DatasetParams, userId: String): Try[String] = params match {
    case kudu: KuduDatasetParams => (proxyUser as userId) { downloadService.tableInfo(kudu.table) } map { _ => kudu.table }
    case file: FileDatasetParams => (proxyUser as userId) { extractTableName(file.path.asHadoop.resolve, file) }
  }

  private def prepareQuery(params: DatasetParams, query: Query, userId: String) = for {
    tableName <- extractTableName(params, userId)
    fragment  <- Writers.sql(query, tableName).write
  } yield fragment.query[Unit].sql

  private def analyzeQuery(params: DatasetParams, query: Query, userId: String) = for {
    tableName <- extractTableName(params, userId)
    analysis  <- queryService.explain(query, tableName, userId)
  } yield analysis

  private def transform(jdbcResult: JdbcResult, targetFormat: FileDataFormat) = targetFormat match {
    case CsvFileFormat  => Try {
      Source[String](jdbcResult.toCsv).map { csv => s"$csv${System.lineSeparator}" }
    }
    case JsonFileFormat => Try {
      wrapJson {
        Source[JsValue](jdbcResult.toJson).map { _.toString }
      }
    }
    case _              => Failure { new IllegalArgumentException(s"Invalid target format [$targetFormat]; must be [csv | json]") }
  }

  // Web
  // Failure

  private def failQuickExec(params: DatasetParams, targetFormat: FileDataFormat) = Future.successful {
    TemporaryRedirect {
      s"${controllers.routes.DatasetController.queryDataset(params.catalogUri, targetFormat.show, "batch").url}"
    }
  }

  // Executions

  private def doBatchExec(params: DatasetParams, query: Query, targetFormat: FileDataFormat, userId: String) = prepareQuery(params, query, userId) match {
    case Success(sql)   => prepareQueryExport(sql, targetFormat).map { formatExport(_, targetFormat) }
    case Failure(error) => Future.failed { error }
  }

  private def doQuickExec(params: DatasetParams, query: Query, targetFormat: FileDataFormat, userId: String) = for {
    tableName  <- extractTableName(params, userId)
    jdbcResult <- queryService.exec(query, tableName, userId)
    data       <- transform(jdbcResult, targetFormat)
  } yield data

  // API

  protected def quickExec(params: DatasetParams, query: Query, targetFormat: FileDataFormat, userId: String) = analyzeQuery(params, query, userId) match {
    case Success(analysis) if analysis.memoryEstimation <= impalaConfig.memoryEstimationLimit => doQuickExec(params, query, targetFormat, userId).~>[Future].map { respond(_, params.name, targetFormat) }
    case Success(_)                                                                           => failQuickExec(params, targetFormat)
    case Failure(error)                                                                       => Future.failed { error }
  }

  protected def batchExec(params: DatasetParams, query: Query, targetFormat: FileDataFormat, userId: String) =
    doBatchExec(params, query, targetFormat, userId).map { respond(_, params.name, targetFormat) }

  protected def exec(params: DatasetParams, query: Query, userId: String, targetFormat: FileDataFormat, method: DownloadMethod) = method match {
    case QuickDownloadMethod => quickExec(params, query, targetFormat, userId)
    case BatchDownloadMethod => batchExec(params, query, targetFormat, userId)
  }

} 
Example 116
Source File: KyloApiClient.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.securitymanager.service

import com.google.inject.Inject
import it.gov.daf.securitymanager.service.utilities.ConfigReader
import play.api.Logger
import play.api.libs.json.{JsUndefined, JsValue, Json}
import play.api.libs.ws.{WSAuthScheme, WSClient}
import security_manager.yaml.{Error, Success}

import scala.concurrent.Future

class KyloApiClient @Inject()(wSClient: WSClient){

  import play.api.libs.concurrent.Execution.Implicits._

  def createCategory(name: String):Future[Either[Error,Success]]= {

    val jsonRequest: JsValue = Json.parse(
                                        s"""{
                                           "id": null,
                                           "name": "$name",
                                           "description": null,
                                           "icon": null,
                                           "iconColor": null,
                                           "userFields": [],
                                           "userProperties": [],
                                           "relatedFeedSummaries": [],
                                           "securityGroups": [],
                                           "roleMemberships": [],
                                           "feedRoleMemberships": [],
                                           "owner": null,
                                           "systemName": "$name"
                                    }""")

    Logger.logger.debug("createCategory: "+ jsonRequest.toString())

    val response = wSClient.url(ConfigReader.kyloUrl + "/proxy/v1/feedmgr/categories")
                    .withHeaders("Accept" -> "application/json")
                    .withAuth(ConfigReader.kyloUser,ConfigReader.kyloUserPwd,WSAuthScheme.BASIC)
                    .post(jsonRequest)

    response.map{response =>

      if( response.status != 200 )
        Left( Error(Option(0),Some("Error in during kylo category creation: bad http code"+response.status),None) )
      else{
        Logger.logger.debug("RESPONSE:"+response.json)
        val result = response.json \ "id"
        if( result.isInstanceOf[JsUndefined] )
          Left( Error(Option(0),Some("Error in during kylo category creation"),None) )
        else
          Right( Success(Some("Category created"), Some("ok")) )
      }

    }

  }


} 
Example 117
Source File: catalog_manager.yaml.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package catalog_manager.yaml

import de.zalando.play.controllers.ResponseWriters

import scala.concurrent.Future
import play.api.mvc._
import de.zalando.play.controllers.SwaggerSecurityExtractors._
import catalog_manager.yaml
import play.api.libs.json.JsValue

import scala.math.BigInt



object SecurityExtractorsExecutionContext {
    // this ExecutionContext might be overridden if default configuration is not suitable for some reason
    implicit val ec = de.zalando.play.controllers.Contexts.tokenChecking
}

trait SecurityExtractors {
    def basicAuth_Extractor[User >: Any](): RequestHeader => Future[Option[User]] =
        header => basicAuth(header) { (username: String, password: String) =>
            userFromToken("abracadabra")
    }


    implicit val unauthorizedContentWriter = ResponseWriters.choose[String]("application/json")
    def unauthorizedContent(req: RequestHeader) = Results.Unauthorized("Unauthorized")

     def userFromToken[User >: Any](token: String): User = {
         Token(Some(token))
         // Gettest200(token)
         //controllers_base. Gettest200(Token(Some(token)))
    }
} 
Example 118
Source File: CkanRepositoryComponent.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.catalogmanager.repository.ckan

import catalog_manager.yaml.{AutocompRes, Credentials, User}
import play.api.libs.json.JsValue

import scala.concurrent.Future


trait CkanRepository {

  def getMongoUser(name:String,callingUserid :MetadataCat): JsResult[User]
  def verifyCredentials(credentials: Credentials):Boolean
  def updateOrganization(orgId: String, jsonOrg: JsValue,callingUserid :MetadataCat): Future[String]
  def patchOrganization(orgId: String, jsonOrg: JsValue,callingUserid :MetadataCat): Future[String]
  def createUser(jsonUser: JsValue,callingUserid :MetadataCat): Future[String]
  def getUserOrganizations(userName :String,callingUserid :MetadataCat) : Future[JsResult[Seq[Organization]]]
  def createDataset(jsonDataset: JsValue,callingUserid :MetadataCat): Future[String]
  def createOrganization(jsonDataset: JsValue,callingUserid :MetadataCat): Future[String]
  def dataset(datasetId: String,callingUserid :MetadataCat): JsValue
  def getOrganization(orgId :String,callingUserid :MetadataCat) : Future[JsResult[Organization]]
  def getOrganizations(callingUserid :MetadataCat) : Future[JsValue]
  def getDatasets(callingUserid :MetadataCat) : Future[JsValue]
  def searchDatasets( input: (MetadataCat, MetadataCat, ResourceSize, ResourceSize), callingUserid :MetadataCat ) : Future[JsResult[Seq[Dataset]]]
  def autocompleteDatasets( input: (MetadataCat, ResourceSize), callingUserid :MetadataCat) : Future[JsResult[Seq[AutocompRes]]]
  def getDatasetsWithRes( input: (ResourceSize, ResourceSize), callingUserid :MetadataCat ) : Future[JsResult[Seq[Dataset]]]
  def testDataset(datasetId :String, callingUserid :MetadataCat) : Future[JsResult[Dataset]]

}

object CkanRepository {
  def apply(config: String): CkanRepository = config match {
    case "dev" => new CkanRepositoryDev
    case "prod" => new CkanRepositoryProd
  }
}

trait CkanRepositoryComponent {
  val ckanRepository :CkanRepository
} 
Example 119
Source File: TestJson.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.catalogmanager.repository.voc

import java.io.FileInputStream

import catalog_manager.yaml.KeyValue
import play.api.libs.json.{JsArray, JsValue}

object TestJson extends App {
  import play.api.libs.json.{JsError, JsResult, JsSuccess, Json}

  val stream = new FileInputStream("data/voc/cv_theme-subtheme_bk.json")
  val json = try { (Json.parse(stream) \ "voc").asOpt[JsArray]} finally {stream.close()}
  val dcatapitThemeId = "AGRI"
  val subthemeId = "policy"
  print {
    json match {
      case Some(s) => s.value.map(x => ((x \ "theme_code").as[String],
        (x \ "subthemes").as[List[JsValue]])).map{ x=>

        x._2.map{ y=> //subthemes

          (x._1, (y \ "subthemes_ita").as[List[List[String]]])
        }
      }.flatten
        .filter(x=>x._1.equals(dcatapitThemeId))
        .map{x=>
          println(x)
          x._2.map{y=>
            println(y)
            println(y.length)
            //println(y(0))
            //println(y(1))
            KeyValue(y(0), y(1))
          }
        }.flatMap(x=>x)
      case None =>
        println("VocRepositoryFile - Error occurred with Json")
        Seq()
    }
  }

} 
Example 120
Source File: CatalogServiceComponent.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.catalogmanager.service


import catalog_manager.yaml.{Dataset, Error, MetaCatalog, MetadataCat, Success}
import it.gov.daf.catalogmanager.repository.catalog.CatalogRepositoryComponent
import play.api.libs.json.JsValue

import scala.concurrent.Future
import play.api.libs.ws._
import play.api.libs.ws.ahc.AhcWSComponents


trait CatalogServiceComponent {
  this: CatalogRepositoryComponent  =>
  val catalogService: CatalogService

  class CatalogService {


    def listCatalogs(page :Option[Int], limit :Option[Int]) :Seq[MetaCatalog] = {
       catalogRepository.listCatalogs(page, limit)

    }
    def catalog(catalogId :String): Option[MetaCatalog] = {
      catalogRepository.catalog(catalogId)
    }

    def catalogByName(name :String, groups: List[String]): Option[MetaCatalog] = {
      catalogRepository.catalogByName(name, groups)
    }

    def publicCatalogByName(name :String): Option[MetaCatalog] = {
      catalogRepository.publicCatalogByName(name)
    }

    def createCatalog(metaCatalog: MetaCatalog, callingUserid :MetadataCat, ws :WSClient) :Success = {
      println("Service : " +  callingUserid)
      catalogRepository.createCatalog(metaCatalog, callingUserid, ws)
    }

    def createCatalogExtOpenData(metaCatalog: MetaCatalog, callingUserid :MetadataCat, ws :WSClient) :Success = {
      println("Service : " +  callingUserid)
      catalogRepository.createCatalogExtOpenData(metaCatalog, callingUserid, ws)
    }

    def isPresentOnCatalog(name :String) :Option[Boolean] = {
      catalogRepository.isDatasetOnCatalog(name)
    }

    def deleteCatalogByName(nameCatalog: String, user: String, isAdmin: Boolean): Either[Error, Success] = {
      catalogRepository.deleteCatalogByName(nameCatalog, user, isAdmin)
    }

  }
} 
Example 121
Source File: CkanService.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package it.gov.daf.catalogmanager.service



trait CkanServiceComponent {
  this: CkanRepositoryComponent =>
  val ckanService: CkanService

  class CkanService {

    def getMongoUser(name:String, callingUserid :MetadataCat ): JsResult[User]  = {
      ckanRepository.getMongoUser(name, callingUserid)
    }

    def verifyCredentials(credentials: Credentials):Boolean = {
      ckanRepository.verifyCredentials(credentials: Credentials)
    }
    def updateOrganization(orgId: String, jsonOrg: JsValue, callingUserid :MetadataCat ): Future[String] = {
      ckanRepository.updateOrganization(orgId,jsonOrg, callingUserid)
    }
    def patchOrganization(orgId: String, jsonOrg: JsValue, callingUserid :MetadataCat ): Future[String] = {
      ckanRepository.patchOrganization(orgId,jsonOrg, callingUserid)
    }

    def createUser(jsonUser: JsValue, callingUserid :MetadataCat): Future[String] = {
      ckanRepository.createUser(jsonUser, callingUserid)
    }
    def getUserOrganizations(userName :String, callingUserid :MetadataCat) : Future[JsResult[Seq[Organization]]] = {
      ckanRepository.getUserOrganizations(userName, callingUserid)
    }

    def createDataset(jsonDataset: JsValue, callingUserid :MetadataCat): Future[String] = {
      ckanRepository.createDataset(jsonDataset,callingUserid)
    }
    def createOrganization(jsonDataset: JsValue, callingUserid :MetadataCat): Future[String] = {
      ckanRepository.createOrganization(jsonDataset,callingUserid)
    }
    def dataset(datasetId: String, callingUserid :MetadataCat): JsValue = {
      ckanRepository.dataset(datasetId,callingUserid)
    }

    def getOrganization(orgId :String, callingUserid :MetadataCat) : Future[JsResult[Organization]] = {
      ckanRepository.getOrganization(orgId,callingUserid)
    }

    def getOrganizations(callingUserid :MetadataCat) : Future[JsValue] = {
      ckanRepository.getOrganizations(callingUserid)
    }

    def getDatasets(callingUserid :MetadataCat) : Future[JsValue] = {
      ckanRepository.getDatasets(callingUserid)
    }

    def searchDatasets( input: (MetadataCat, MetadataCat, ResourceSize, ResourceSize), callingUserid :MetadataCat) : Future[JsResult[Seq[Dataset]]] = {
      ckanRepository.searchDatasets(input, callingUserid)
    }
    def autocompleteDatasets( input: (MetadataCat, ResourceSize), callingUserid :MetadataCat) : Future[JsResult[Seq[AutocompRes]]] = {
      ckanRepository.autocompleteDatasets(input, callingUserid)
    }

    def getDatasetsWithRes( input: (ResourceSize, ResourceSize),callingUserid :MetadataCat ) : Future[JsResult[Seq[Dataset]]] = {
      ckanRepository.getDatasetsWithRes(input, callingUserid)
    }

    def testDataset(datasetId :String, callingUserid :MetadataCat) : Future[JsResult[Dataset]] = {
      ckanRepository.testDataset(datasetId, callingUserid)
    }

  }
}


object CkanRegistry extends
  CkanServiceComponent with
  CkanRepositoryComponent {
  val conf = Configuration.load(Environment.simple())
  val app: String = conf.getString("app.type").getOrElse("dev")
  val ckanRepository =  CkanRepository(app)
  val ckanService = new CkanService
} 
Example 122
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 123
Source File: StudyClientActor.scala    From lila-ws   with GNU Affero General Public License v3.0 5 votes vote down vote up
package lila.ws

import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ Behavior, PostStop }
import play.api.libs.json.JsValue

import ipc._

object StudyClientActor {

  import ClientActor._

  case class State(
      room: RoomActor.State,
      site: ClientActor.State = ClientActor.State()
  )

  def start(roomState: RoomActor.State, fromVersion: Option[SocketVersion])(
      deps: Deps
  ): Behavior[ClientMsg] =
    Behaviors.setup { ctx =>
      RoomActor.onStart(roomState, fromVersion, deps, ctx)
      apply(State(roomState), deps)
    }

  private def apply(state: State, deps: Deps): Behavior[ClientMsg] =
    Behaviors
      .receive[ClientMsg] { (ctx, msg) =>
        import deps._

        def forward(payload: JsValue): Unit =
          lilaIn.study(
            LilaIn.TellRoomSri(state.room.id, LilaIn.TellSri(req.sri, req.user.map(_.id), payload))
          )

        def receive: PartialFunction[ClientMsg, Behavior[ClientMsg]] = {

          case in: ClientIn =>
            clientInReceive(state.site, deps, in) match {
              case None    => Behaviors.same
              case Some(s) => apply(state.copy(site = s), deps)
            }

          case ClientCtrl.Broom(oldSeconds) =>
            if (state.site.lastPing < oldSeconds) Behaviors.stopped
            else {
              keepAlive study state.room.id
              Behaviors.same
            }

          case ctrl: ClientCtrl => socketControl(state.site, deps, ctrl)

          case ClientOut.StudyForward(payload) =>
            forward(payload)
            Behaviors.same

          case anaMove: ClientOut.AnaMove =>
            clientIn(Chess(anaMove))
            forward(anaMove.payload)
            Behaviors.same

          case anaDrop: ClientOut.AnaDrop =>
            clientIn(Chess(anaDrop))
            forward(anaDrop.payload)
            Behaviors.same

          case ClientOut.PalantirPing =>
            deps.req.user map { Palantir.respondToPing(state.room.id, _) } foreach clientIn
            Behaviors.same

          // default receive (site)
          case msg: ClientOutSite =>
            val siteState = globalReceive(state.site, deps, ctx, msg)
            if (siteState == state.site) Behaviors.same
            else apply(state.copy(site = siteState), deps)

          case _ =>
            Monitor.clientOutUnhandled("study").increment()
            Behaviors.same
        }

        RoomActor.receive(state.room, deps).lift(msg).fold(receive(msg)) {
          case (newState, emit) =>
            emit foreach lilaIn.study
            newState.fold(Behaviors.same[ClientMsg]) { roomState =>
              apply(state.copy(room = roomState), deps)
            }
        }

      }
      .receiveSignal {
        case (ctx, PostStop) =>
          onStop(state.site, deps, ctx)
          RoomActor.onStop(state.room, deps, ctx)
          Behaviors.same
      }
} 
Example 124
Source File: LobbyClientActor.scala    From lila-ws   with GNU Affero General Public License v3.0 5 votes vote down vote up
package lila.ws

import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ Behavior, PostStop }
import play.api.libs.json.JsValue

import ipc._

object LobbyClientActor {

  import ClientActor._

  case class State(
      idle: Boolean = false,
      site: ClientActor.State = ClientActor.State()
  )

  def start(deps: Deps): Behavior[ClientMsg] =
    Behaviors.setup { ctx =>
      import deps._
      onStart(deps, ctx)
      req.user foreach { users.connect(_, ctx.self, silently = true) }
      services.lobby.connect(req.sri -> req.user.map(_.id))
      Bus.subscribe(Bus.channel.lobby, ctx.self)
      apply(State(), deps)
    }

  private def apply(state: State, deps: Deps): Behavior[ClientMsg] =
    Behaviors
      .receive[ClientMsg] { (ctx, msg) =>
        import deps._

        def forward(payload: JsValue): Unit =
          lilaIn.lobby(LilaIn.TellSri(req.sri, req.user.map(_.id), payload))

        msg match {

          case ctrl: ClientCtrl => socketControl(state.site, deps, ctrl)

          case ClientIn.LobbyNonIdle(payload) =>
            if (!state.idle) clientIn(payload)
            Behaviors.same

          case ClientIn.OnlyFor(endpoint, payload) =>
            if (endpoint == ClientIn.OnlyFor.Lobby) clientIn(payload)
            Behaviors.same

          case in: ClientIn =>
            clientInReceive(state.site, deps, in) match {
              case None    => Behaviors.same
              case Some(s) => apply(state.copy(site = s), deps)
            }

          case msg: ClientOut.Ping =>
            clientIn(services.lobby.pong.get)
            apply(state.copy(site = sitePing(state.site, deps, msg)), deps)

          case ClientOut.LobbyForward(payload) =>
            forward(payload)
            Behaviors.same

          case ClientOut.Idle(value, payload) =>
            forward(payload)
            apply(state.copy(idle = value), deps)

          // default receive (site)
          case msg: ClientOutSite =>
            val siteState = globalReceive(state.site, deps, ctx, msg)
            if (siteState == state.site) Behaviors.same
            else apply(state.copy(site = siteState), deps)

          case _ =>
            Monitor.clientOutUnhandled("lobby").increment()
            Behaviors.same
        }

      }
      .receiveSignal {
        case (ctx, PostStop) =>
          onStop(state.site, deps, ctx)
          Bus.unsubscribe(Bus.channel.lobby, ctx.self)
          deps.services.lobby.disconnect(deps.req.sri)
          Behaviors.same
      }
} 
Example 125
Source File: JsonReceiverActor.scala    From incubator-retired-iota   with Apache License 2.0 5 votes vote down vote up
package org.apache.iota.fey

import java.nio.file.Paths
import java.io.File

import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import play.api.libs.json.{JsValue, Json}

class JsonReceiverActor extends Actor with ActorLogging {

  import JsonReceiverActor._

  val monitoring_actor = FEY_MONITOR.actorRef
  var watchFileTask: WatchServiceReceiver = _
  var watchThread: Thread = _

  override def preStart() {
    prepareDynamicJarRepo()
    processCheckpointFiles()

    watchFileTask = new WatchServiceReceiver(self)
    watchThread = new Thread(watchFileTask, GLOBAL_DEFINITIONS.WATCH_SERVICE_THREAD)

    monitoring_actor  ! Monitor.START(Utils.getTimestamp)
    watchThread.setDaemon(true)
    watchThread.start()

    watchFileTask.watch(Paths.get(CONFIG.JSON_REPOSITORY))
  }

  private def prepareDynamicJarRepo() = {
    val jarDir = new File(CONFIG.DYNAMIC_JAR_REPO)
    if (!jarDir.exists()){
      jarDir.mkdir()
    }else if(CONFIG.DYNAMIC_JAR_FORCE_PULL){
      jarDir.listFiles().foreach(_.delete())
    }
  }


  private def processCheckpointFiles() = {
    if (CONFIG.CHEKPOINT_ENABLED) {
      val checkpoint = new CheckpointProcessor(self)
      checkpoint.run()
    }
  }

  override def postStop() {
    monitoring_actor  ! Monitor.STOP(Utils.getTimestamp)
    watchThread.interrupt()
    watchThread.join()
  }

  override def postRestart(reason: Throwable): Unit = {
    monitoring_actor  ! Monitor.RESTART(reason, Utils.getTimestamp)
    preStart()
  }

  override def receive: Receive = {
    case JSON_RECEIVED(json, file) =>
      log.info(s"JSON RECEIVED => ${Json.stringify(json)}")
      context.parent ! FeyCore.ORCHESTRATION_RECEIVED(json, Some(file))

    case _ =>
  }

}

object JsonReceiverActor {

  case class JSON_RECEIVED(json: JsValue, file: File)

} 
Example 126
Source File: CheckpointProcessor.scala    From incubator-retired-iota   with Apache License 2.0 5 votes vote down vote up
package org.apache.iota.fey

import java.io.File

import akka.actor.ActorRef
import org.apache.iota.fey.JsonReceiverActor.JSON_RECEIVED
import play.api.libs.json.{JsValue, Json}

import scala.io.Source


class CheckpointProcessor(receiverActor: ActorRef) extends JsonReceiver{

  override def run(): Unit = {
    processCheckpointFiles()
  }

  def getJsonObject(params: String): Option[JsValue] = {
    try{
      val stringJson = Source.fromFile(params).getLines.mkString
      Option(Json.parse(stringJson))
    }catch{
      case e: Exception =>
        log.error("Could not parse JSON", e)
        None
    }
  }

  private def processJson(path: String, file: File) = {
    try{
      getJsonObject(path) match {
        case Some(orchestrationJSON) =>
          val valid = validJson(orchestrationJSON)
          if(valid && (orchestrationJSON \ JSON_PATH.COMMAND).as[String].toUpperCase != "DELETE"){
            checkForLocation(orchestrationJSON)
          }
          if(valid) {
            receiverActor ! JSON_RECEIVED(orchestrationJSON, file)
          }else{
            log.warn(s"File $path not processed. Incorrect JSON schema")
          }
          file.delete()
        case None =>
      }
    } catch {
      case e: Exception =>
        log.error(s"File $path will not be processed", e)
    }
  }

  private def processCheckpointFiles() = {
    Utils.getFilesInDirectory(CONFIG.CHECKPOINT_DIR)
      .filter(file => file.getName.endsWith(CONFIG.JSON_EXTENSION))
      .foreach(file => {
        processJson(file.getAbsolutePath, file)
      })
  }

  override def execute(): Unit = {}
  override def exceptionOnRun(e: Exception): Unit = {}
} 
Example 127
Source File: JsonRequestSpec.scala    From play-ws   with Apache License 2.0 5 votes vote down vote up
package play.api.libs.ws.ahc

import java.nio.charset.StandardCharsets

import akka.actor.ActorSystem
import akka.stream.Materializer
import akka.util.ByteString
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.when
import org.specs2.mock.Mockito

import org.specs2.mutable.Specification
import org.specs2.specification.AfterAll
import play.api.libs.json.JsString
import play.api.libs.json.JsValue
import play.api.libs.json.Json
import play.api.libs.ws.JsonBodyReadables
import play.api.libs.ws.JsonBodyWritables
import play.libs.ws.DefaultObjectMapper
import play.shaded.ahc.org.asynchttpclient.Response

import scala.io.Codec


class JsonRequestSpec extends Specification with Mockito with AfterAll with JsonBodyWritables {
  sequential

  implicit val system       = ActorSystem()
  implicit val materializer = Materializer.matFromSystem

  override def afterAll: Unit = {
    system.terminate()
  }

  "set a json node" in {
    val jsValue = Json.obj("k1" -> JsString("v1"))
    val client  = mock[StandaloneAhcWSClient]
    val req = new StandaloneAhcWSRequest(client, "http://playframework.com/", null)
      .withBody(jsValue)
      .asInstanceOf[StandaloneAhcWSRequest]
      .buildRequest()

    req.getHeaders.get("Content-Type") must be_==("application/json")
    ByteString.fromArray(req.getByteData).utf8String must be_==("""{"k1":"v1"}""")
  }

  "set a json node using the default object mapper" in {
    val objectMapper = DefaultObjectMapper.instance

    implicit val jsonReadable = body(objectMapper)
    val jsonNode              = objectMapper.readTree("""{"k1":"v1"}""")
    val client                = mock[StandaloneAhcWSClient]
    val req = new StandaloneAhcWSRequest(client, "http://playframework.com/", null)
      .withBody(jsonNode)
      .asInstanceOf[StandaloneAhcWSRequest]
      .buildRequest()

    req.getHeaders.get("Content-Type") must be_==("application/json")
    ByteString.fromArray(req.getByteData).utf8String must be_==("""{"k1":"v1"}""")
  }

  "read an encoding of UTF-8" in {
    val json = io.Source.fromResource("test.json")(Codec.ISO8859).getLines.mkString

    val ahcResponse = mock[Response]
    val response    = new StandaloneAhcWSResponse(ahcResponse)

    when(ahcResponse.getResponseBody(StandardCharsets.UTF_8)).thenReturn(json)
    when(ahcResponse.getContentType).thenReturn("application/json")

    val value: JsValue = JsonBodyReadables.readableAsJson.transform(response)
    verify(ahcResponse, times(1)).getResponseBody(StandardCharsets.UTF_8)
    verify(ahcResponse, times(1)).getContentType
    value.toString must beEqualTo(json)
  }

  "read an encoding of ISO-8859-1" in {
    val json = io.Source.fromResource("test.json")(Codec.ISO8859).getLines.mkString

    val ahcResponse = mock[Response]
    val response    = new StandaloneAhcWSResponse(ahcResponse)

    when(ahcResponse.getResponseBody(StandardCharsets.ISO_8859_1)).thenReturn(json)
    when(ahcResponse.getContentType).thenReturn("application/json;charset=iso-8859-1")

    val value: JsValue = JsonBodyReadables.readableAsJson.transform(response)
    verify(ahcResponse, times(1)).getResponseBody(StandardCharsets.ISO_8859_1)
    verify(ahcResponse, times(1)).getContentType
    value.toString must beEqualTo(json)
  }
} 
Example 128
Source File: JsonBodyWritables.scala    From play-ws   with Apache License 2.0 5 votes vote down vote up
package play.api.libs.ws

import akka.util.ByteString
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import play.api.libs.json.JsValue
import play.api.libs.json.Json

trait JsonBodyWritables {

  
  implicit val writeableOf_JsValue: BodyWritable[JsValue] = {
    BodyWritable(a => InMemoryBody(ByteString.fromArrayUnsafe(Json.toBytes(a))), "application/json")
  }

  def body(objectMapper: ObjectMapper): BodyWritable[JsonNode] =
    BodyWritable(
      json => InMemoryBody(ByteString.fromArrayUnsafe(objectMapper.writer.writeValueAsBytes(json))),
      "application/json"
    )
}

object JsonBodyWritables extends JsonBodyWritables 
Example 129
Source File: JsonBodyReadablesSpec.scala    From play-ws   with Apache License 2.0 5 votes vote down vote up
package play.api.libs.ws

import java.nio.charset.Charset
import java.nio.charset.StandardCharsets._

import akka.stream.scaladsl.Source
import akka.util.ByteString
import org.specs2.matcher.MustMatchers
import org.specs2.mutable.Specification
import play.api.libs.json.JsSuccess
import play.api.libs.json.JsValue

class JsonBodyReadablesSpec extends Specification with MustMatchers {

  class StubResponse(byteArray: Array[Byte], charset: Charset = UTF_8) extends StandaloneWSResponse {
    override def uri: java.net.URI = ???

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

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

    override def status: Int = ???

    override def statusText: String = ???

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

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

    override def body: String = new String(byteArray, charset)

    override def bodyAsBytes: ByteString = ByteString.fromArray(byteArray)

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

  "decode encodings correctly" should {

    "read an encoding of UTF-32BE" in {
      val readables   = new JsonBodyReadables() {}
      val json        = """{"menu": {"id": "file", "value": "File"} }"""
      val charsetName = "UTF-32BE"
      val value: JsValue =
        readables.readableAsJson.transform(new StubResponse(json.getBytes(charsetName), Charset.forName(charsetName)))
      (value \ "menu" \ "id").validate[String] must beEqualTo(JsSuccess("file"))
    }

    "read an encoding of UTF-32LE" in {
      val readables   = new JsonBodyReadables() {}
      val json        = """{"menu": {"id": "file", "value": "File"} }"""
      val charsetName = "UTF-32LE"
      val value: JsValue =
        readables.readableAsJson.transform(new StubResponse(json.getBytes(charsetName), Charset.forName(charsetName)))
      (value \ "menu" \ "id").validate[String] must beEqualTo(JsSuccess("file"))
    }

    "read an encoding of UTF-16BE" in {
      val readables      = new JsonBodyReadables() {}
      val json           = """{"menu": {"id": "file", "value": "File"} }"""
      val charset        = UTF_16BE
      val value: JsValue = readables.readableAsJson.transform(new StubResponse(json.getBytes(charset), charset))
      (value \ "menu" \ "id").validate[String] must beEqualTo(JsSuccess("file"))
    }

    "read an encoding of UTF-16LE" in {
      val readables      = new JsonBodyReadables() {}
      val json           = """{"menu": {"id": "file", "value": "File"} }"""
      val charset        = UTF_16LE
      val value: JsValue = readables.readableAsJson.transform(new StubResponse(json.getBytes(charset), charset))
      (value \ "menu" \ "id").validate[String] must beEqualTo(JsSuccess("file"))
    }

    "read an encoding of UTF-8" in {
      val readables      = new JsonBodyReadables() {}
      val json           = """{"menu": {"id": "file", "value": "File"} }"""
      val value: JsValue = readables.readableAsJson.transform(new StubResponse(json.getBytes(UTF_8)))
      (value \ "menu" \ "id").validate[String] must beEqualTo(JsSuccess("file"))
    }

    "read an encoding of UTF-8 with empty object" in {
      val readables      = new JsonBodyReadables() {}
      val json           = "{}"
      val value: JsValue = readables.readableAsJson.transform(new StubResponse(json.getBytes(UTF_8)))
      value.toString() must beEqualTo("{}")
    }

    "read an encoding of UTF-8 with empty array" in {
      val readables      = new JsonBodyReadables() {}
      val json           = "[]"
      val value: JsValue = readables.readableAsJson.transform(new StubResponse(json.getBytes(UTF_8)))
      value.toString() must beEqualTo("[]")
    }

  }

} 
Example 130
Source File: Metadata.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.models.nrs.request

import org.joda.time.DateTime
import play.api.libs.json.{Format, JsValue, Json, OFormat}
import utils.DateUtils

case class Metadata(businessId: String,
                    notableEvent: String,
                    payloadContentType: String,
                    payloadSha256Checksum: Option[String],
                    userSubmissionTimestamp: DateTime,
                    identityData: Option[IdentityData],
                    userAuthToken: String,
                    headerData: JsValue,
                    searchKeys: SearchKeys)

object Metadata {
  implicit val idformat: OFormat[IdentityData] = IdentityData.format
  implicit val dateFormats: Format[DateTime] = DateUtils.isoInstantDateFormat
  implicit val format: OFormat[Metadata] = Json.format[Metadata]
} 
Example 131
Source File: ErrorWrapper.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.models.errors

import play.api.libs.json.{JsObject, JsValue, Json, Writes}
import v1.models.audit.AuditError

case class ErrorWrapper(correlationId: Option[String], error: MtdError, errors: Option[Seq[MtdError]] = None) {

  private def allErrors: Seq[MtdError] = errors match {
    case Some(seq) => seq
    case None      => Seq(error)
  }

  def auditErrors: Seq[AuditError] =
    allErrors.map(error => AuditError(error.code))
}

object ErrorWrapper {

  val allErrors: Seq[MtdError] => Seq[JsValue] = {
    case mtdError :: Nil => mtdErrors(mtdError)
    case mtdError :: rest => mtdErrors(mtdError) ++ allErrors(rest)
  }

  private val mtdErrors : MtdError => Seq[JsValue] = {
    case MtdError(_, _, Some(customJson)) =>
      customJson.asOpt[MtdErrorWrapper] match {
        case Some(e) => mtdErrorWrapper(e)
        case _ => Seq(customJson)
      }
    case _@o => Seq(Json.toJson(o))
  }

  private val mtdErrorWrapper: MtdErrorWrapper => Seq[JsValue]= wrapper => wrapper.errors match {
    case Some(errors) if errors.nonEmpty => errors.map(error => Json.toJson(error))
    case _ => Seq(Json.toJson(wrapper))
  }

  implicit val writes: Writes[ErrorWrapper] = (errorResponse: ErrorWrapper) => {

    val singleJson: JsObject = Json.toJson(errorResponse.error).as[JsObject]

    errorResponse.errors match {
      case Some(errors) if errors.nonEmpty => singleJson + ("errors" -> Json.toJson(allErrors(errors)))
      case _ => singleJson
    }
  }
} 
Example 132
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 133
Source File: FinancialDataReadsUtils.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package utils

import java.time.LocalDate

import play.api.libs.json.{JsValue, Json, Reads}
import v1.models.response.common.TaxPeriod

trait FinancialDataReadsUtils {

  def filterNotArrayReads[T](filterName: String, notMatching: Seq[String])
                            (implicit rds: Reads[Seq[T]]): Reads[Seq[T]] = (json: JsValue) => {
    json
      .validate[Seq[JsValue]]
      .flatMap(
        readJson =>
          Json
            .toJson(readJson.filterNot {
              element =>
                (element \ filterName).asOpt[String].exists(item => notMatching.contains(item.toLowerCase()))
            })
            .validate[Seq[T]])
  }

  def dateCheck(taxPeriod: Option[TaxPeriod], requestToDate: String): Boolean = {
    val toDate = taxPeriod.fold(None: Option[LocalDate]) { l => Some(LocalDate.parse(l.to)) }
    toDate.fold(true) { desTo => desTo.compareTo(LocalDate.parse(requestToDate)) <= 0
    }
  }
} 
Example 134
Source File: FinancialDataResponse.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.resources.wrappers

import play.api.http.Status
import play.api.libs.json.{JsError, JsSuccess, JsValue}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http.HttpResponse
import uk.gov.hmrc.vatapi.models.des.DesErrorCode._
import uk.gov.hmrc.vatapi.models.{DesTransformError, DesTransformValidator, Errors, Liabilities, Payments, des}
import uk.gov.hmrc.vatapi.resources.VatResult

case class FinancialDataResponse(underlying: HttpResponse) extends Response {

  def getLiabilities(vrn: Vrn): Either[DesTransformError, Liabilities] = {

    def deserialise(js: JsValue) = js.validate[des.FinancialData] match {
      case JsError(errors) => Left(ParseError(s"[FinancialDataResponse][getLiabilities - deserialise] Json format from DES doesn't match the FinancialData model:  $errors"))
      case JsSuccess(financialData, _) =>
        DesTransformValidator[des.FinancialData, Liabilities].from(financialData)
    }

    jsonOrError match {
      case Right(js) =>
        logger.debug(s"[FinancialDataResponse][getLiabilities - jsonOrError] Json response body from DES : ${js}")
        deserialise(js)
      case Left(e) =>
        logger.error(s"[FinancialDataResponse][getLiabilities - jsonOrError] Non json response from DES : ${e.getMessage}")
        Left(ParseError(s"Unable to parse the response from DES as Json: $e"))
    }
  }

  def getPayments(vrn: Vrn): Either[DesTransformError, Payments] = {
    def deserialise(js: JsValue) = js.validate[des.FinancialData] match {
      case JsError(errors) => Left(ParseError(s"[FinancialDataResponse][getPayments - deserialise] Json format from DES doesn't match the FinancialData model: $errors"))
      case JsSuccess(financialData, _) => DesTransformValidator[des.FinancialData, Payments].from(financialData)
    }

    jsonOrError match {
      case Right(js) =>
        logger.debug(s"[FinancialDataResponse][getPayments - jsonOrError] Json response body from DES : ${js}")
        deserialise(js)
      case Left(e) =>
        logger.error(s"[FinancialDataResponse][getPayments - jsonOrError] Non json response from DES : ${e.getMessage}")
        Left(ParseError(s"Unable to parse the response from DES as Json: $e"))
    }
  }

  override def errorMappings: PartialFunction[Int, VatResult] = {
    case 400 if errorCodeIsOneOf(INVALID_IDNUMBER) => VatResult.Failure(Status.BAD_REQUEST, Errors.VrnInvalid)
    case 400 if errorCodeIsOneOf(INVALID_DATEFROM) => VatResult.Failure(Status.BAD_REQUEST, Errors.InvalidDateFrom)
    case 400 if errorCodeIsOneOf(INVALID_DATETO) => VatResult.Failure(Status.BAD_REQUEST,Errors.InvalidDateTo)
    case 400 if errorCodeIsOneOf(NOT_FOUND) => VatResult.Failure(Status.NOT_FOUND, Errors.NotFound)
    case 400 if errorCodeIsOneOf(INVALID_IDTYPE, INVALID_ONLYOPENITEMS, INVALID_REGIMETYPE,
      INVALID_INCLUDELOCKS, INVALID_CALCULATEACCRUEDINTEREST, INVALID_CUSTOMERPAYMENTINFORMATION
    ) => VatResult.Failure(Status.INTERNAL_SERVER_ERROR, Errors.InternalServerError)
    case 404 if errorCodeIsOneOf(NOT_FOUND) => VatResult.Failure(Status.NOT_FOUND,Errors.NotFound)
    case 422 if errorCodeIsOneOf(INVALID_DATA) => VatResult.Failure(Status.BAD_REQUEST,Errors.InvalidData)
  }

} 
Example 135
Source File: VatReturnResponse.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.resources.wrappers

import play.api.http.Status
import play.api.libs.json.{JsError, JsSuccess, JsValue}
import uk.gov.hmrc.http.HttpResponse
import uk.gov.hmrc.vatapi.httpparsers.NRSData
import uk.gov.hmrc.vatapi.models.des.DesErrorCode._
import uk.gov.hmrc.vatapi.models.{DesTransformError, DesTransformValidator, Errors, VatReturn, des}
import uk.gov.hmrc.vatapi.resources.VatResult

case class VatReturnResponse(underlying: HttpResponse) extends Response {

  var nrsData: NRSData = _

  def vatReturnOrError: Either[DesTransformError, VatReturn] = {

    def deserialise(js: JsValue) = js.validate[des.VatReturn] match {
      case JsError(errors) => Left(ParseError(s"Json format from DES doesn't match the VatReturn model: $errors"))
      case JsSuccess(vatReturn, _) => DesTransformValidator[des.VatReturn, VatReturn].from(vatReturn)
    }

    jsonOrError match {
      case Left(e) =>
        logger.error(s"[VatReturnResponse][vatReturnOrError] Non json response from DES : $e")
        Left(ParseError(s"Unable to parse the response from DES as Json: $e"))
      case Right(js) =>
        deserialise(js)
    }
  }

  def vatSubmissionReturnOrError: Either[DesTransformError, JsValue] = {
    jsonOrError match {
      case Left(e) =>
        logger.error(s"[VatReturnResponse][vatReturnOrError] Non json response from DES : $e")
        Left(ParseError(s"Unable to parse the response from DES as Json: $e"))
      case Right(js) => Right(js)
    }
  }

  def withNrsData(data: NRSData): VatReturnResponse = {
    nrsData = data;
    this
  }

  override def errorMappings: PartialFunction[Int, VatResult] = {
    case 400 if errorCodeIsOneOf(INVALID_VRN) => VatResult.Failure(Status.BAD_REQUEST, Errors.VrnInvalid)
    case 400 if errorCodeIsOneOf(INVALID_ARN) => VatResult.Failure(Status.INTERNAL_SERVER_ERROR, Errors.InternalServerError)
    case 400 if errorCodeIsOneOf(INVALID_ORIGINATOR_ID) => VatResult.Failure(Status.INTERNAL_SERVER_ERROR, Errors.InternalServerError)
    case 400 if errorCodeIsOneOf(INVALID_PAYLOAD) => VatResult.Failure(Status.BAD_REQUEST, Errors.InvalidRequest)
    case 400 if errorCodeIsOneOf(INVALID_PERIODKEY) => VatResult.Failure(Status.BAD_REQUEST, Errors.InvalidPeriodKey)
    case 400 if errorCodeIsOneOf(INVALID_SUBMISSION) =>
      logger.info(s"[VatReturnResponse][errorMappings] Des returned error with status 400 and errorCode INVALID_SUBMISSION")
      VatResult.Failure(Status.INTERNAL_SERVER_ERROR, Errors.InternalServerError)
    case 403 if errorCodeIsOneOf(DATE_RANGE_TOO_LARGE) => VatResult.Failure(Status.FORBIDDEN, Errors.businessError(Errors.DateRangeTooLarge))
    case 403 if errorCodeIsOneOf(VRN_NOT_FOUND, NOT_FOUND_VRN) => VatResult.Failure(Status.INTERNAL_SERVER_ERROR, Errors.InternalServerError)
    case 403 if errorCodeIsOneOf(INVALID_IDENTIFIER) => VatResult.Failure(Status.NOT_FOUND, Errors.InvalidPeriodKey)
    case 403 if errorCodeIsOneOf(INVALID_INPUTDATA) => VatResult.Failure(Status.FORBIDDEN, Errors.InvalidRequest)
    case 403 if errorCodeIsOneOf(TAX_PERIOD_NOT_ENDED) => VatResult.Failure(Status.FORBIDDEN, Errors.TaxPeriodNotEnded)
    case 409 if errorCodeIsOneOf(DUPLICATE_SUBMISSION) => VatResult.Failure(Status.FORBIDDEN, Errors.businessError(Errors.DuplicateVatSubmission))

  }
}

case class ParseError(msg: String) extends DesTransformError 
Example 136
Source File: Response.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.resources.wrappers

import play.api.Logger
import play.api.http.Status
import play.api.libs.json.JsValue
import uk.gov.hmrc.http.HttpResponse
import uk.gov.hmrc.vatapi.models.Errors
import uk.gov.hmrc.vatapi.models.des.DesError
import uk.gov.hmrc.vatapi.models.des.DesErrorCode.{DesErrorCode, _}
import uk.gov.hmrc.vatapi.resources.{AuthRequest, VatResult}
import uk.gov.hmrc.vatapi.utils.pagerDutyLogging.{Endpoint, PagerDutyLogging}

import scala.PartialFunction.{apply => _, _}
import scala.util.{Failure, Success, Try}

object Response {
  val defaultCorrelationId = "No Correlation ID"

  def getCorrelationId(httpResponse: HttpResponse): String =
    httpResponse.header("CorrelationId").getOrElse(defaultCorrelationId)
}

trait Response {

  val logger: Logger = Logger(this.getClass)
  val status: Int = underlying.status

  def underlying: HttpResponse

  def filter[A](pf: PartialFunction[Int, VatResult])(implicit endpoint: Endpoint, request: AuthRequest[A]): VatResult = {
    val statusPrefix: Int = status / 100
    statusPrefix match {
      case 4 | 5 =>
        val message = s"DES error occurred. User type: ${request.authContext.affinityGroup}\n" +
          s"Status code: ${underlying.status}\nBody: ${underlying.body}"

        PagerDutyLogging.logError(endpoint.toLoggerMessage, message, statusPrefix, logger.error(_))

        (pf orElse errorMappings orElse standardErrorMapping) (status)
      case _ => (pf andThen addCorrelationHeader) (status)
    }
  }

  private def addCorrelationHeader(result: VatResult) =
    underlying
      .header("CorrelationId")
      .fold(result)(correlationId => result.withHeaders("X-CorrelationId" -> correlationId))

  def errorMappings: PartialFunction[Int, VatResult] = empty

  private def standardErrorMapping: PartialFunction[Int, VatResult] = {
    case 404 => VatResult.FailureEmptyBody(Status.NOT_FOUND, Errors.NotFound)
    case 500 if errorCodeIsOneOf(SERVER_ERROR) => VatResult.Failure(Status.INTERNAL_SERVER_ERROR, Errors.InternalServerError)
    case 503 if errorCodeIsOneOf(SERVICE_UNAVAILABLE) => VatResult.Failure(Status.INTERNAL_SERVER_ERROR, Errors.InternalServerError)
    case _ => VatResult.Failure(Status.INTERNAL_SERVER_ERROR, Errors.InternalServerError)
  }

  def errorCodeIsOneOf(errorCodes: DesErrorCode*): Boolean = jsonOrError match {
    case Right(json) => json.asOpt[DesError].exists(errorCode => errorCodes.contains(errorCode.code))
    case Left(_) => false
  }

  def jsonOrError: Either[Throwable, JsValue] = {
    Try(underlying.json) match {
      case Success(null) => Left(new RuntimeException)
      case Success(json) => Right(json)
      case Failure(e) => Left(e)
    }
  }

  def getCorrelationId: String = Response.getCorrelationId(underlying)
} 
Example 137
Source File: NRSConnector.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 javax.inject.Inject
import play.api.Logger
import play.api.libs.json.{JsValue, Json, Writes}
import play.api.libs.ws.WSClient
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http._
import uk.gov.hmrc.play.bootstrap.http.DefaultHttpClient
import uk.gov.hmrc.vatapi.config.AppContext
import uk.gov.hmrc.vatapi.httpparsers.EmptyNrsData
import uk.gov.hmrc.vatapi.httpparsers.NrsSubmissionHttpParser.{NrsSubmissionOutcome, NrsSubmissionOutcomeReads}
import uk.gov.hmrc.vatapi.models.NRSSubmission

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Success, Try}


class NRSConnector @Inject()(
                              override val http: DefaultHttpClient,
                              override val appContext: AppContext,
                              ws: WSClient
                            ) extends BaseConnector {

  val logger: Logger = Logger(this.getClass)
  val nrsSubmissionUrl: String => String = vrn => s"${appContext.nrsServiceUrl}/submission"
  val nrsMaxTimeout: Duration = appContext.nrsMaxTimeoutMillis.milliseconds

  private val xApiKeyHeader = "X-API-Key"

  def submit(vrn: Vrn, nrsSubmission: NRSSubmission)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[NrsSubmissionOutcome] = {

    logger.debug(s"[NRSConnector][submit] - Submission to NRS for 9 box vat return for VRN: $vrn")

    val nrsResponse = {
      val submitUrl = nrsSubmissionUrl(vrn.toString)
      val headers = hc.withExtraHeaders(xApiKeyHeader -> s"${appContext.xApiKey}", "User-Agent" -> appContext.appName).headers

      implicit val nrsWrites = implicitly[Writes[NRSSubmission]]

      ws.url(submitUrl)
        .withHttpHeaders(headers: _*)
        .withRequestTimeout(nrsMaxTimeout)
        .post(Json.toJson(nrsSubmission))
    }

    nrsResponse.map { res =>

      val resJson = Try(res.json) match {
        case Success(json: JsValue) => Some(json)
        case _ => None
      }

      val httpResponse = HttpResponse(
        res.status,
        resJson,
        res.headers,
        None
      )

      Logger.debug(s"[NRSConnector][submit] - NRS Call succeeded")

      NrsSubmissionOutcomeReads.read("", "", httpResponse)

    }.recover {
      case e: TimeoutException => {
        logger.warn(s"[NRSConnector][submit] - NRS Call timed out for VRN: $vrn - $e")
        Right(EmptyNrsData)
      }
    }
  }
} 
Example 138
Source File: RetrieveLiabilitiesFixture.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.fixtures

import play.api.libs.json.{JsValue, Json}
import v1.models.response.common.TaxPeriod
import v1.models.response.liabilities.{LiabilitiesResponse, Liability}

trait RetrieveLiabilitiesFixture {

  val desJson: JsValue = Json.parse(
    """
      |{
      |    "idType": "VRN",
      |    "idNumber": "123456789",
      |    "regimeType": "VATC",
      |    "processingDate": "2017-03-07T09:30:00.000Z",
      |    "financialTransactions": [{
      |            "chargeType": "VAT",
      |            "mainType": "2100",
      |            "periodKey": "13RL",
      |            "periodKeyDescription": "abcde",
      |            "taxPeriodFrom": "2017-02-01",
      |            "taxPeriodTo": "2017-11-01",
      |            "businessPartner": "6622334455",
      |            "contractAccountCategory": "02",
      |            "contractAccount": "D",
      |            "contractObjectType": "ABCD",
      |            "contractObject": "00000003000000002757",
      |            "sapDocumentNumber": "1040000872",
      |            "sapDocumentNumberItem": "XM00",
      |            "chargeReference": "XM002610011594",
      |            "mainTransaction": "1234",
      |            "subTransaction": "5678",
      |            "originalAmount": 463872,
      |            "outstandingAmount": 463872,
      |            "accruedInterest": 10000,
      |            "items": [{
      |                "subItem": "001",
      |                "dueDate": "2017-11-11",
      |                "amount": 463872
      |            }]
      |        }
      |    ]
      |}
    """.stripMargin
  )

  val mtdJson: JsValue = Json.parse(
    s"""
       |{
       |	"liabilities": [{
       |		"taxPeriod": {
       |			"from": "2017-02-01",
       |			"to": "2017-11-01"
       |		},
       |		"type": "VAT",
       |		"originalAmount": 463872,
       |		"outstandingAmount": 463872,
       |		"due": "2017-11-11"
       |	}]
       |}
    """.stripMargin
  )

  val liabilityResponse: LiabilitiesResponse =
    LiabilitiesResponse(
      Seq(
        Liability(
          Some(TaxPeriod("2017-01-01", "2017-12-01")),
          "VAT",
          1.0,
          Some(1.0),
          Some("2017-11-11")
        )
      )
    )
} 
Example 139
Source File: ViewReturnFixture.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.fixtures

import play.api.libs.json.{JsValue, Json}
import v1.models.response.viewReturn.ViewReturnResponse

trait ViewReturnFixture {

  val viewReturnDesJson: JsValue = Json.parse(
    """
      |{
      |    "periodKey": "A001",
      |    "vatDueSales": 1234567890123.23,
      |    "vatDueAcquisitions": -9876543210912.87,
      |    "vatDueTotal": 1234567890112.23,
      |    "vatReclaimedCurrPeriod": -1234567890122.23,
      |    "vatDueNet": 2345678901.12,
      |    "totalValueSalesExVAT": 1234567890123.00,
      |    "totalValuePurchasesExVAT": 1234567890123.00,
      |    "totalValueGoodsSuppliedExVAT": 1234567890123.00,
      |    "totalAllAcquisitionsExVAT": -1234567890123.00
      |}
    """.stripMargin
  )

  val viewReturnMtdJson: JsValue = Json.parse(
    """
      |{
      |    "periodKey": "A001",
      |    "vatDueSales": 1234567890123.23,
      |    "vatDueAcquisitions": -9876543210912.87,
      |    "totalVatDue": 1234567890112.23,
      |    "vatReclaimedCurrPeriod": -1234567890122.23,
      |    "netVatDue": 2345678901.12,
      |    "totalValueSalesExVAT": 1234567890123.00,
      |    "totalValuePurchasesExVAT": 1234567890123.00,
      |    "totalValueGoodsSuppliedExVAT": 1234567890123.00,
      |    "totalAcquisitionsExVAT": -1234567890123.00
      |}
    """.stripMargin
  )

  val viewReturnResponse: ViewReturnResponse =
    ViewReturnResponse(
      periodKey = "A001",
      vatDueSales = 1234567890123.23,
      vatDueAcquisitions = -9876543210912.87,
      totalVatDue = 1234567890112.23,
      vatReclaimedCurrPeriod = -1234567890122.23,
      netVatDue = 2345678901.12,
      totalValueSalesExVAT = 1234567890123.00,
      totalValuePurchasesExVAT = 1234567890123.00,
      totalValueGoodsSuppliedExVAT = 1234567890123.00,
      totalAcquisitionsExVAT = -1234567890123.00
    )
} 
Example 140
Source File: ObligationsFixture.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.fixtures

import play.api.libs.json.{JsValue, Json}
import v1.models.response.obligations.{Obligation, ObligationsResponse}

trait ObligationsFixture {

  val obligationsDesJson: JsValue = Json.parse(
    s"""
       |{
       |   "obligations":[
       |      {
       |         "identification":{
       |            "referenceNumber":"123456789",
       |            "referenceType":"VRN"
       |         },
       |         "obligationDetails":[
       |            {
       |               "status":"F",
       |               "inboundCorrespondenceFromDate":"2017-01-01",
       |               "inboundCorrespondenceToDate":"2017-03-31",
       |               "inboundCorrespondenceDateReceived":"2017-05-06",
       |               "inboundCorrespondenceDueDate":"2017-05-07",
       |               "periodKey":"18A1"
       |            },
       |            {
       |               "status":"O",
       |               "inboundCorrespondenceFromDate":"2017-04-01",
       |               "inboundCorrespondenceToDate":"2017-06-30",
       |               "inboundCorrespondenceDueDate":"2017-08-07",
       |               "periodKey":"18A2"
       |            }
       |         ]
       |      }
       |   ]
       |}
       |""".stripMargin
  )
  val obligationsMtdJson: JsValue = Json.parse(
    s"""
       |{
       |  "obligations": [
       |    {
       |      "start": "2017-01-01",
       |      "end": "2017-03-31",
       |      "due": "2017-05-07",
       |      "status": "F",
       |      "periodKey": "18A1",
       |      "received": "2017-05-06"
       |    },
       |    {
       |      "start": "2017-04-01",
       |      "end": "2017-06-30",
       |      "due": "2017-08-07",
       |      "status": "O",
       |      "periodKey": "18A2"
       |    }
       |  ]
       |}
       |""".stripMargin
  )

  val obligationsResponse: ObligationsResponse =
    ObligationsResponse(Seq(
      Obligation(
        start = "2017-01-01",
        end = "2017-03-31",
        due = "2017-05-07",
        status = "F",
        periodKey = "18A1",
        received = Some("2017-05-06")
      ),
      Obligation(
        start = "2017-04-01",
        end =  "2017-06-30",
        due = "2017-08-07",
        status = "O",
        periodKey = "18A2",
        received = None
      )
    )
    )
} 
Example 141
Source File: NrsStub.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.stubs

import com.github.tomakehurst.wiremock.stubbing.StubMapping
import play.api.libs.json.JsValue
import support.WireMockMethods

object NrsStub extends WireMockMethods {

  def onSuccess(method: HTTPMethod, uri: String, status: Int, body: JsValue): StubMapping = {
    when(method = method, uri = uri)
      .thenReturnWithHeaders(status = status, headers, body)
  }

  def onError(method: HTTPMethod, uri: String, errorStatus: Int, errorBody: String): StubMapping = {
    when(method = method, uri = uri)
      .thenReturn(status = errorStatus, errorBody)
  }

  private val headers = Map("Content-Type" -> "application/json", "Receipt-Id" -> "de1249ad-c242-4f22-9fe6-f357b1bfcccf",
    "Receipt-Signature" -> "757b1365-d89e-4dac-8317-ba87efca6c21",
    "Receipt-Timestamp" -> "2018-03-27T15:10:44.798Z")
} 
Example 142
Source File: DesStub.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.stubs

import com.github.tomakehurst.wiremock.stubbing.StubMapping
import play.api.http.Status.OK
import play.api.libs.json.JsValue
import support.WireMockMethods
import v1.fixtures.ViewReturnFixture

object DesStub extends WireMockMethods with ViewReturnFixture {

  def onSuccess(method: HTTPMethod, uri: String, status: Int, body: JsValue): StubMapping = {
    when(method = method, uri = uri)
      .thenReturn(status = status, body)
  }

  def onSuccess(method: HTTPMethod, uri: String, queryParams: Map[String, String], status: Int, body: JsValue): StubMapping = {
    when(method = method, uri = uri, queryParams = queryParams)
      .thenReturn(status = status, body)
  }

  def onError(method: HTTPMethod, uri: String, errorStatus: Int, errorBody: String): StubMapping = {
    when(method = method, uri = uri)
      .thenReturn(status = errorStatus, errorBody)
  }

  def onError(method: HTTPMethod, uri: String, queryParams: Map[String, String], errorStatus: Int, errorBody: String): StubMapping = {
    when(method = method, uri = uri, queryParams)
      .thenReturn(status = errorStatus, errorBody)
  }

  private def url(vrn: String): String =
    s"/vat/returns/vrn/$vrn"

  def serviceSuccess(vrn: String, periodKey: String): StubMapping = {
    when(method = GET, uri = url(vrn), queryParams = Map("period-key" -> periodKey))
      .thenReturn(status = OK, viewReturnDesJson)
  }
} 
Example 143
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 144
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 145
Source File: DesStub.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.stubs

import com.github.tomakehurst.wiremock.stubbing.StubMapping
import play.api.libs.json.JsValue
import uk.gov.hmrc.support.WireMockMethods

object DesStub extends WireMockMethods {

  def onSuccess(method: HTTPMethod, uri: String, status: Int, body: JsValue): StubMapping = {
    when(method = method, uri = uri)
      .thenReturn(status = status, body)
  }

  def onSuccess(method: HTTPMethod, uri: String, queryParams: Map[String, String], status: Int, body: String): StubMapping = {
    when(method = method, uri = uri)
      .thenReturn(status = status, body)
  }

  def onSuccess(method: HTTPMethod, uri: String, queryParams: Map[String, String], status: Int, body: JsValue): StubMapping = {
    when(method = method, uri = uri, queryParams = queryParams)
      .thenReturn(status = status, body)
  }

  def onError(method: HTTPMethod, uri: String, errorStatus: Int, errorBody: String): StubMapping = {
    when(method = method, uri = uri)
      .thenReturn(status = errorStatus, errorBody)
  }

  def onError(method: HTTPMethod, uri: String, queryParams: Map[String, String], errorStatus: Int, errorBody: String): StubMapping = {
    when(method = method, uri = uri, queryParams)
      .thenReturn(status = errorStatus, errorBody)
  }

  def onError(method: HTTPMethod, uri: String, queryParams: Map[String, String], errorStatus: Int, errorBody: JsValue): StubMapping = {
    when(method = method, uri = uri, queryParams)
      .thenReturn(status = errorStatus, errorBody)
  }
} 
Example 146
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 147
Source File: NRS.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.assets.nrs

import play.api.libs.json.{JsValue, Json}

  object NRS {
    def success(): JsValue =
      Json.parse(
        s"""
           |{
           |  "nrSubmissionId":"2dd537bc-4244-4ebf-bac9-96321be13cdc",
           |  "cadesTSignature":"30820b4f06092a864886f70111111111c0445c464",
           |  "timestamp":"2018-02-14T09:32:15Z"
           |}
         """.stripMargin)
  } 
Example 148
Source File: SubmitRequestBodySpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.models.request

import play.api.libs.json.{JsValue, Json}
import support.UnitSpec
import v1.models.request.submit.SubmitRequestBody

class SubmitRequestBodySpec extends UnitSpec {

  val vendorRequest: JsValue = Json.parse(
    """
      |{
      |   "periodKey": "AB12",
      |   "vatDueSales": 	1000.00,
      |   "vatDueAcquisitions": 	2000.00,
      |   "totalVatDue": 	3000.00,
      |   "vatReclaimedCurrPeriod": 	99999999999.99,
      |   "netVatDue": 	99999999999.99,
      |   "totalValueSalesExVAT": 	9999999999999,
      |   "totalValuePurchasesExVAT": 	9999999999999,
      |   "totalValueGoodsSuppliedExVAT": 	9999999999999,
      |   "totalAcquisitionsExVAT": 	9999999999999,
      |   "finalised": true
      |}
    """.stripMargin)

  val toDesJson: JsValue = Json.parse(
    """
      |{
      |   "vatDueAcquisitions":2000,
      |   "vatDueSales":1000,
      |   "totalValuePurchasesExVAT":9999999999999,
      |   "agentReference":"LARN0085901",
      |   "totalAllAcquisitionsExVAT":9999999999999,
      |   "periodKey":"AB12",
      |   "vatDueNet":99999999999.99,
      |   "totalValueSalesExVAT":9999999999999,
      |   "receivedAt":"2020-05-05T12:00:00Z",
      |   "vatReclaimedCurrPeriod":99999999999.99,
      |   "vatDueTotal":3000,
      |   "totalValueGoodsSuppliedExVAT":9999999999999
      |}
    """.stripMargin)

  val submitRequestBody: SubmitRequestBody = SubmitRequestBody(Some("AB12"), Some(BigDecimal(1000.00)),
    Some(BigDecimal(2000.00)), Some(BigDecimal(3000.00)), Some(BigDecimal(99999999999.99)),
    Some(BigDecimal(99999999999.99)), Some(BigDecimal(9999999999999.0)), Some(BigDecimal(9999999999999.0)),
    Some(BigDecimal(9999999999999.0)), Some(BigDecimal(9999999999999.0)), Some(true), None, None)

  val submitRequestToDesBody: SubmitRequestBody = SubmitRequestBody(Some("AB12"), Some(BigDecimal(1000.00)),
    Some(BigDecimal(2000.00)), Some(BigDecimal(3000.00)), Some(BigDecimal(99999999999.99)),
    Some(BigDecimal(99999999999.99)), Some(BigDecimal(9999999999999.0)), Some(BigDecimal(9999999999999.0)),
    Some(BigDecimal(9999999999999.0)), Some(BigDecimal(9999999999999.0)), Some(true), Some("2020-05-05T12:00:00Z"), Some("LARN0085901"))

  "Submit request body" should {
    "return a SubmitRequestBody model" when {
      "valid json is provided" in {

        vendorRequest.as[SubmitRequestBody] shouldBe submitRequestBody
      }
    }

    "write valid Json" when {
      "a valid model is provided" in {

        Json.toJson(submitRequestToDesBody) shouldBe toDesJson
      }
    }
  }
} 
Example 149
Source File: PaymentItemSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.models.response.payments

import play.api.libs.json.{JsError, JsObject, JsValue, Json}
import support.UnitSpec

class PaymentItemSpec extends UnitSpec {

  val desJson: JsValue = Json.parse(
    """
      |{
      |  "paymentAmount" : 100.2,
      |  "clearingDate" : "2017-01-01"
      |}
    """.stripMargin
  )

  val invalidDesJson: JsValue = Json.parse(
    """
      |{
      |  "paymentAmount" : 100.2,
      |  "clearingDate" : false
      |}
    """.stripMargin
  )

  val mtdJson: JsValue = Json.parse(
    """
      |{
      |  "amount" : 100.2,
      |  "received" : "2017-01-01"
      |}
    """.stripMargin
  )

  val paymentItemModel: PaymentItem =
    PaymentItem(amount = Some(100.2), received = Some("2017-01-01"))

  "PaymentItem" when {
    "read from valid JSON" should {
      "produce the expected PaymentItem object" in {
        desJson.as[PaymentItem] shouldBe paymentItemModel
      }

      "handle missing optional fields" in {
        JsObject.empty.as[PaymentItem] shouldBe PaymentItem.empty
      }

      "error on invalid json" in {
        invalidDesJson.validate[PaymentItem] shouldBe a[JsError]
      }
    }

    "written to JSON" should {
      "produce the expected Js Object" in {
        Json.toJson(paymentItemModel) shouldBe mtdJson
      }

      "not write empty fields" in {

        val emptyPaymentItemModel: PaymentItem =
          PaymentItem(amount = None, received = None)

        Json.toJson(emptyPaymentItemModel) shouldBe JsObject.empty
      }
    }
  }
} 
Example 150
Source File: ViewReturnResponseSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.models.response

import play.api.libs.json.{JsValue, Json}
import support.UnitSpec
import v1.models.response.viewReturn.ViewReturnResponse

class ViewReturnResponseSpec extends UnitSpec {

  val desJson: JsValue = Json.parse(
    """
      |{
      |    "periodKey": "A001",
      |    "vatDueSales": 1234567890123.23,
      |    "vatDueAcquisitions": -9876543210912.87,
      |    "vatDueTotal": 1234567890112.23,
      |    "vatReclaimedCurrPeriod": -1234567890122.23,
      |    "vatDueNet": 2345678901.12,
      |    "totalValueSalesExVAT": 1234567890123.00,
      |    "totalValuePurchasesExVAT": 1234567890123.00,
      |    "totalValueGoodsSuppliedExVAT": 1234567890123.00,
      |    "totalAllAcquisitionsExVAT": -1234567890123.00
      |}
    """.stripMargin
  )

  val mtdJson: JsValue = Json.parse(
    """
      |{
      |    "periodKey": "A001",
      |    "vatDueSales": 1234567890123.23,
      |    "vatDueAcquisitions": -9876543210912.87,
      |    "totalVatDue": 1234567890112.23,
      |    "vatReclaimedCurrPeriod": -1234567890122.23,
      |    "netVatDue": 2345678901.12,
      |    "totalValueSalesExVAT": 1234567890123.00,
      |    "totalValuePurchasesExVAT": 1234567890123.00,
      |    "totalValueGoodsSuppliedExVAT": 1234567890123.00,
      |    "totalAcquisitionsExVAT": -1234567890123.00
      |}
    """.stripMargin
  )

  val viewReturnResponse: ViewReturnResponse =
    ViewReturnResponse(
      periodKey = "A001",
      vatDueSales = 1234567890123.23,
      vatDueAcquisitions = -9876543210912.87,
      totalVatDue = 1234567890112.23,
      vatReclaimedCurrPeriod = -1234567890122.23,
      netVatDue = 2345678901.12,
      totalValueSalesExVAT = 1234567890123.00,
      totalValuePurchasesExVAT = 1234567890123.00,
      totalValueGoodsSuppliedExVAT = 1234567890123.00,
      totalAcquisitionsExVAT = -1234567890123.00
    )

  "ViewReturnResponse" when {
    "read from valid JSON" should {
      "produce the expected ViewReturnResponse object for a return" in {
        desJson.as[ViewReturnResponse] shouldBe viewReturnResponse
      }
    }

    "written to JSON" should {
      "produce the expected JsObject" in {
        Json.toJson(viewReturnResponse) shouldBe mtdJson
      }
    }
  }
} 
Example 151
Source File: NrsAuditDetailSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.models.audit

import play.api.libs.json.{JsValue, Json}
import support.UnitSpec

class NrsAuditDetailSpec extends UnitSpec {

  val modelSuccess = NrsAuditDetail("1234567", "Bearer test", Some("1234"), None, "")

  val modelError = NrsAuditDetail("1234567", "Bearer test", None, Some(
    Json.parse("""{
      |"test":"test"
      |}""".stripMargin)), "")

  val jsonSuccess: JsValue = Json.parse(
    """{
      |"vrn":"1234567",
      | "authorization":"Bearer test",
      | "nrSubmissionID":"1234",
      | "correlationId":""
      |}""".stripMargin)

  val jsonError: JsValue = Json.parse(
    """
      |{"vrn":"1234567","authorization":"Bearer test","request":{"test":"test"},"correlationId":""}
      |""".stripMargin)
  
  "NrsAuditDetail" when {
    "written to JSON (success)" should {
      "produce the expected JsObject" in {
        Json.toJson(modelSuccess) shouldBe jsonSuccess
      }
    }

    "written to JSON (error)" should {
      "produce the expected JsObject" in {
        Json.toJson(modelError) shouldBe jsonError
      }
    }
  }
} 
Example 152
Source File: AuditErrorSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.models.audit

import play.api.libs.json.{JsValue, Json}
import support.UnitSpec

class AuditErrorSpec extends UnitSpec {

  private val auditErrorModel: AuditError = AuditError(errorCode = "FORMAT_NINO")

  private val auditErrorJson: JsValue = Json.parse(
    """
      |{
      |  "errorCode": "FORMAT_NINO"
      |}
    """.stripMargin
  )

  "AuditError" when {
    "read from valid JSON" should {
      "produce the expected AuditError object" in {
        auditErrorJson.as[AuditError] shouldBe auditErrorModel
      }
    }

    "written to JSON" should {
      "produce the expected JsObject" in {
        Json.toJson(auditErrorModel) shouldBe auditErrorJson
      }
    }
  }
} 
Example 153
Source File: MtdErrorSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.models.errors

import play.api.libs.json.{JsValue, Json}
import support.UnitSpec

class MtdErrorSpec extends UnitSpec {

  "MtdError" when {
    "written to JSON" should {
      "generate the correct JSON" in {
        Json.toJson(MtdError("CODE", "some message")) shouldBe Json.parse(
          """
            |{
            |   "code": "CODE",
            |   "message": "some message"
            |}
        """.stripMargin
        )
      }
    }
  }

  "written to JSON with custom JSON" should {
    "generate the correct JSON" in {

      val customJson: JsValue = Json.parse(
        """
          |{
          |  "statusCode": "STATUS_CODE",
          |  "errorMessage": "ERROR_MESSAGE"
          |}
        """.stripMargin
      )

      Json.toJson(MtdError("CODE", "some message", Some(customJson))) shouldBe customJson
    }
  }
} 
Example 154
Source File: AuditResponseFixture.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.fixtures.audit

import play.api.http.Status.{BAD_REQUEST, OK}
import play.api.libs.json.{JsValue, Json}
import v1.models.audit.{AuditError, AuditResponse}

object AuditResponseFixture {

  val auditErrors: Seq[AuditError] = Seq(AuditError(errorCode = "FORMAT_NINO"), AuditError(errorCode = "FORMAT_TAX_YEAR"))

  val auditResponseModelWithoutBody: AuditResponse =
    AuditResponse(
      httpStatus = OK,
      payload = Right(None)
    )

  val auditResponseModelWithBody: AuditResponse =
    AuditResponse(
      httpStatus = OK,
      payload = Right(Some(Json.parse(
        """
          |{
          | "response":"success"
          |}
          |""".stripMargin)))
    )

  val auditResponseModelWithErrors: AuditResponse =
    AuditResponse(
      httpStatus = BAD_REQUEST,
      payload = Left(auditErrors)
    )

  val successAuditResponse: JsValue = Json.parse(
    s"""
       |{
       |  "httpStatus": $OK
       |}
    """.stripMargin
  )

  val successAuditResponseWithBody: JsValue = Json.parse(
    s"""
       |{
       |  "httpStatus": $OK,
       |  "payload":{
       |    "response": "success"
       |  }
       |}
    """.stripMargin
  )

  val auditResponseJsonWithErrors: JsValue = Json.parse(
    s"""
       |{
       |  "httpStatus": $BAD_REQUEST,
       |  "errors" : [
       |    {
       |      "errorCode" : "FORMAT_NINO"
       |    },
       |    {
       |      "errorCode" : "FORMAT_TAX_YEAR"
       |    }
       |  ]
       |}
    """.stripMargin
  )
} 
Example 155
Source File: AuditDetailFixture.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.fixtures.audit

import play.api.libs.json.{JsValue, Json}
import v1.fixtures.audit.AuditResponseFixture._
import v1.models.audit.AuditDetail
import v1.models.auth.UserDetails

object AuditDetailFixture {

  val userType: String = "Agent"
  val agentReferenceNumber: Option[String] = Some("012345678")
  val correlationId = "a1e8057e-fbbc-47a8-a8b478d9f015c253"
  val userDetails: UserDetails = UserDetails("Agent", agentReferenceNumber, "id")

  val auditDetailModelSuccess: AuditDetail =
    AuditDetail(
      userType = userType,
      agentReferenceNumber = agentReferenceNumber,
      response = auditResponseModelWithoutBody,
      `X-CorrelationId` = correlationId,
      clientId = "id"
    )

  val auditDetailModelError: AuditDetail =
    auditDetailModelSuccess.copy(
      response = auditResponseModelWithErrors
    )

  val auditDetailJsonSuccess: JsValue = Json.parse(
    s"""
       |{
       |   "userType" : "$userType",
       |   "agentReferenceNumber" : "${agentReferenceNumber.get}",
       |   "response":{
       |     "httpStatus": ${auditResponseModelWithoutBody.httpStatus}
       |   },
       |   "X-CorrelationId": "$correlationId",
       |   "clientId": "id"
       |}
    """.stripMargin
  )

  val auditDetailJsonError: JsValue = Json.parse(
    s"""
       |{
       |   "userType" : "$userType",
       |   "agentReferenceNumber" : "${agentReferenceNumber.get}",
       |   "response": $auditResponseJsonWithErrors,
       |   "X-CorrelationId": "$correlationId",
       |   "clientId": "id"
       |}
     """.stripMargin
  )
} 
Example 156
Source File: MandatoryFieldValidationSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.controllers.requestParsers.validators.validations

import play.api.libs.json.{JsValue, Json}
import support.UnitSpec
import v1.models.errors.MandatoryFieldRuleError

class MandatoryFieldValidationSpec extends UnitSpec {
  "validate" should {
    "return no errors" when {
      "no fields are missing" in {
        val testJson: JsValue =  Json.parse(
          """
            |{
            |   "test" : "test"
            |}
            |""".stripMargin)

        MandatoryFieldValidation.validate(testJson \ "test", "test") shouldBe List()

      }

      "return errors" when {
        "when a field is missing" in {
          val testJson: JsValue =  Json.parse(
            """
              |{
              |
              |}
              |""".stripMargin)

          MandatoryFieldValidation.validate(testJson \ "test", "test") shouldBe List(MandatoryFieldRuleError.withFieldName("test"))
        }
      }
    }
  }

} 
Example 157
Source File: AuthorisedControllerSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package v1.controllers

import play.api.libs.json.{JsValue, Json}
import play.api.mvc._
import uk.gov.hmrc.auth.core.Enrolment
import uk.gov.hmrc.http.HeaderCarrier
import v1.mocks.services.MockEnrolmentsAuthService
import v1.models.errors.{DownstreamError, ForbiddenDownstreamError, LegacyUnauthorisedError, MtdError}
import v1.services.EnrolmentsAuthService

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

class AuthorisedControllerSpec extends ControllerBaseSpec {

  trait Test extends MockEnrolmentsAuthService {

    val hc: HeaderCarrier = HeaderCarrier()
    val authorisedController: TestController = new TestController()

    class TestController extends AuthorisedController(cc) {
      override val authService: EnrolmentsAuthService = mockEnrolmentsAuthService

      def action(vrn: String): Action[AnyContent] = authorisedAction(vrn).async {
        Future.successful(Ok(Json.obj()))
      }
    }
  }

  val vrn: String = "123456789"

  "calling an action" when {
    "a user is properly authorised" should {
      "return a 200 success response" in new Test {
        MockEnrolmentsAuthService.authoriseUser()
        private val result = authorisedController.action(vrn)(fakeGetRequest)
        status(result) shouldBe OK
      }
    }

    "the enrolments auth service returns an error" must {
      "map to the correct result" when {

        val predicate: Enrolment =
          Enrolment("HMRC-MTD-VAT")
            .withIdentifier("VRN", vrn)
            .withDelegatedAuthRule("mtd-vat-auth")

        def serviceErrors(mtdError: MtdError, expectedStatus: Int, expectedBody: JsValue): Unit = {
          s"a ${mtdError.code} error is returned from the enrolments auth service" in new Test {

            MockEnrolmentsAuthService.authorised(predicate)
              .returns(Future.successful(Left(mtdError)))

            private val actualResult = authorisedController.action(vrn)(fakeGetRequest)
            status(actualResult) shouldBe expectedStatus
            contentAsJson(actualResult) shouldBe expectedBody
          }
        }

        object unexpectedError extends MtdError(code = "UNEXPECTED_ERROR", message = "This is an unexpected error")

        val authServiceErrors =
          Seq(
            (LegacyUnauthorisedError, FORBIDDEN, Json.toJson(LegacyUnauthorisedError)),
            (ForbiddenDownstreamError, FORBIDDEN, Json.toJson(DownstreamError)),
            (unexpectedError, INTERNAL_SERVER_ERROR, Json.toJson(DownstreamError))
          )

        authServiceErrors.foreach(args => (serviceErrors _).tupled(args))
      }
    }
  }
} 
Example 158
Source File: FinancialDataResponseSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.resources.wrappers

import play.api.libs.json.{JsValue, Json}
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http.HttpResponse
import uk.gov.hmrc.vatapi.UnitSpec
import uk.gov.hmrc.vatapi.resources.Jsons

class FinancialDataResponseSpec extends UnitSpec {
  val vrn = Vrn("123456789")

  val emptyJson: JsValue = Json.parse("""{}""")

  "FinancialDataResponse for liabilities" should {
    "wrap empty response" in {
      val response = FinancialDataResponse(HttpResponse(200, Some(emptyJson)))

      val liabilities = response.getLiabilities(vrn)
      liabilities.left.get.msg contains  "Json format from DES doesn't match the FinancialData model"
    }


    "wrap invalid json response" in {
      val response = FinancialDataResponse(HttpResponse(200, Some(Jsons.FinancialData.oneLiability)))

      val liabilities = response.getLiabilities(vrn)
      liabilities.left.get.msg contains  "Json format from DES doesn't match the FinancialData model"
    }

    "wrap valid response" in {
      val response = FinancialDataResponse(HttpResponse(200, Some(Jsons.FinancialData.singleLiabilityDesResponse)))

      val liabilities = response.getLiabilities(vrn)
      liabilities.right.get.liabilities.head.`type` shouldBe "VAT"
    }
  }

  "FinancialDataResponse for payments" should {
    "wrap empty response" in {
      val response = FinancialDataResponse(HttpResponse(200, Some(emptyJson)))

      val payments = response.getPayments(vrn)
      payments.left.get.msg contains  "Json format from DES doesn't match the FinancialData model"
    }


    "wrap invalid json response" in {
      val response = FinancialDataResponse(HttpResponse(200, Some(Jsons.FinancialData.onePayment)))

      val payments = response.getPayments(vrn)
      payments.left.get.msg contains  "Json format from DES doesn't match the FinancialData model"
    }

    "wrap valid response" in {
      val response = FinancialDataResponse(HttpResponse(200, Some(Jsons.FinancialData.singlePaymentDesResponse)))

      val payments = response.getPayments(vrn)
      payments.right.get.payments.head.amount shouldBe 1534.65
    }
  }
} 
Example 159
Source File: ObligationsConnectorSpec.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.vatapi.connectors

import org.scalatestplus.play.guice.GuiceOneAppPerSuite
import play.api.libs.json.JsValue
import play.mvc.Http.MimeTypes
import uk.gov.hmrc.domain.Vrn
import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse}
import uk.gov.hmrc.vatapi.UnitSpec
import uk.gov.hmrc.vatapi.mocks.MockHttp
import uk.gov.hmrc.vatapi.mocks.config.MockAppContext
import uk.gov.hmrc.vatapi.models.ObligationsQueryParams
import uk.gov.hmrc.vatapi.resources.Jsons
import uk.gov.hmrc.vatapi.resources.wrappers.ObligationsResponse

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

class ObligationsConnectorSpec extends UnitSpec with GuiceOneAppPerSuite
  with MockHttp
  with MockAppContext {

  class Setup {
    val testObligationsConnector = new ObligationsConnector(mockHttp, mockAppContext)
    MockAppContext.desUrl returns desBaseUrl
    MockAppContext.desToken returns desToken
    MockAppContext.desEnv returns desEnvironment
  }

  lazy val desToken = "test-token"
  lazy val desEnvironment = "test-env"
  lazy val desBaseUrl = "des-base-url"

  val vrn: Vrn = generateVrn
  val queryParams = ObligationsQueryParams(Some(now.minusDays(7)), Some(now), Some("O"))
  val queryString = queryParams.queryString
  val desUrl = s"$desBaseUrl/enterprise/obligation-data/vrn/$vrn/VATC?$queryString"
  val desObligationsJson: JsValue = Jsons.Obligations.desResponse(vrn)

  val desHttpResponse = HttpResponse(200, Some(desObligationsJson))

  implicit val hc = HeaderCarrier()

  "get" should {
    "have the DES headers in the request" in new Setup {
      MockHttp.GET[HttpResponse](desUrl)
        .returns(Future.successful(desHttpResponse))

      await(testObligationsConnector.get(vrn, queryParams))

      val headers = MockHttp.fetchHeaderCarrier.headers.toMap
      headers("Accept") shouldBe MimeTypes.JSON
      headers("Environment") shouldBe desEnvironment
      headers("Authorization") shouldBe s"Bearer $desToken"
    }

    "return an ObligationsResponse" when {
      "DES returns a 200 response" in new Setup {
        MockHttp.GET[HttpResponse](desUrl)
          .returns(Future.successful(desHttpResponse))

        val response = await(testObligationsConnector.get(vrn, queryParams))
        response shouldBe ObligationsResponse(desHttpResponse)
      }
    }
  }
} 
Example 160
Source File: Configuration.scala    From codacy-analysis-cli   with GNU Affero General Public License v3.0 5 votes vote down vote up
package com.codacy.analysis.core.model

import play.api.libs.json.JsValue

sealed trait Configuration {
  val baseSubDir: Option[String]
  val extraValues: Option[Map[String, JsValue]]
}

final case class Parameter(name: String, value: String)

final case class Pattern(id: String, parameters: Set[Parameter])

final case class CodacyCfg(patterns: Set[Pattern],
                           baseSubDir: Option[String] = Option.empty[String],
                           extraValues: Option[Map[String, JsValue]] = Option.empty[Map[String, JsValue]])
    extends Configuration

final case class FileCfg(baseSubDir: Option[String] = Option.empty[String],
                         extraValues: Option[Map[String, JsValue]] = Option.empty[Map[String, JsValue]])
    extends Configuration 
Example 161
Source File: CodacyConfigurationFile.scala    From codacy-analysis-cli   with GNU Affero General Public License v3.0 5 votes vote down vote up
package com.codacy.analysis.core.configuration

import better.files.File
import cats.syntax.show._
import com.codacy.analysis.core.files.Glob
import com.codacy.plugins.api.languages.{Language, Languages}
import io.circe.generic.auto._
import io.circe.yaml.parser
import io.circe.{Decoder, Json, _}
import play.api.libs.json.JsValue

import scala.util.{Properties, Try}

final case class LanguageConfiguration(extensions: Option[Set[String]])

final case class EngineConfiguration(excludePaths: Option[Set[Glob]],
                                     baseSubDir: Option[String],
                                     extraValues: Option[Map[String, JsValue]])

final case class CodacyConfigurationFile(engines: Option[Map[String, EngineConfiguration]],
                                         excludePaths: Option[Set[Glob]],
                                         languages: Option[Map[Language, LanguageConfiguration]]) {

  lazy val languageCustomExtensions: Map[Language, Set[String]] =
    languages.fold(Map.empty[Language, Set[String]])(_.map {
      case (lang, config) => (lang, config.extensions.getOrElse(Set.empty[String]))
    })
}

class CodacyConfigurationFileLoader {

  val filenames: Set[String] = Set(".codacy.yaml", ".codacy.yml")

  def load(directory: File): Either[String, CodacyConfigurationFile] = {
    search(directory).flatMap(configDir => parse(configDir.contentAsString))
  }

  def search(root: File): Either[String, File] = {
    filenames
      .map(root / _)
      .find(f => f.exists && f.isRegularFile)
      .fold[Either[String, File]](
        Left(s"Could not find Codacy configuration file. Make sure you have a file named like one of ${filenames
          .mkString(", ")}."))(Right(_))
  }

  def parse(yamlString: String): Either[String, CodacyConfigurationFile] = {
    for {
      json <- parser.parse(yamlString).left.map(_.show)
      cursor = HCursor.fromJson(json)
      configurationEither = Decoder[CodacyConfigurationFile].decodeAccumulating(cursor).toEither
      configuration <- configurationEither.left.map(_.toList.map(_.show).mkString(Properties.lineSeparator))
    } yield configuration
  }

}

object CodacyConfigurationFile {

  implicit val globDecoder: Decoder[Glob] = (c: HCursor) => c.as[String].map(Glob)

  implicit val languageKeyDecoder: KeyDecoder[Language] = (languageStr: String) => Languages.fromName(languageStr)

  implicit val decodeEngineConfiguration: Decoder[EngineConfiguration] =
    new Decoder[EngineConfiguration] {
      val engineConfigurationKeys = Set("enabled", "exclude_paths", "base_sub_dir")

      def apply(c: HCursor): Decoder.Result[EngineConfiguration] = {
        val extraKeys =
          c.keys.fold(List.empty[String])(_.to[List]).filter(key => !engineConfigurationKeys.contains(key))
        for {
          excludePaths <- c.downField("exclude_paths").as[Option[Set[Glob]]]
          baseSubDir <- c.downField("base_sub_dir").as[Option[String]]
        } yield {
          val extraToolConfigurations: Map[String, JsValue] = extraKeys.flatMap { extraKey =>
            c.downField(extraKey)
              .as[Json]
              .fold[Option[JsValue]](
                { _ =>
                  Option.empty
                },
                { json =>
                  Try(play.api.libs.json.Json.parse(json.noSpaces)).toOption
                })
              .map(value => (extraKey, value))
          }(collection.breakOut)

          EngineConfiguration(excludePaths, baseSubDir, Option(extraToolConfigurations).filter(_.nonEmpty))
        }
      }
    }

  implicit val decodeCodacyConfigurationFile: Decoder[CodacyConfigurationFile] =
    Decoder.forProduct3("engines", "exclude_paths", "languages")(CodacyConfigurationFile.apply)

} 
Example 162
Source File: LaboratoryController.scala    From Aton   with GNU General Public License v3.0 5 votes vote down vote up
package controllers.api

import com.fasterxml.jackson.annotation.JsonValue
import com.google.inject.Inject
import dao.{LaboratoryDAO, UserDAO}
import jp.t2v.lab.play2.auth.OptionalAuthElement
import model._
import model.json.LoginJson
import play.Logger
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json.{JsValue, Json, Writes}
import play.api.mvc.{Action, AnyContent, Controller}
import services.LaboratoryService
import views.html._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ExecutionContext, Future}


class LaboratoryController @Inject()(userDAO: UserDAO, laboratoryService: LaboratoryService, val messagesApi: MessagesApi) extends Controller with I18nSupport {



  //override def resolveUser(id: LoginFormData)(implicit context: ExecutionContext): Future[Option[User]] = userDAO.get(id)

  def convertToJson(laboratoryObject: Laboratory, roomsWithComputers: Map[Room, Seq[(Computer, Option[(ComputerState, Seq[ConnectedUser])])]]): JsValue = {
    val roomsConverted = roomsWithComputers.toSeq
    val grouped = roomsConverted.groupBy(_._1)
    val resultRooms = grouped.map(filtered=>(filtered._1,filtered._2.map(_._2).head)).toSeq
    Json.toJson((laboratoryObject,resultRooms))
  }

  def get(id: Long) = Action.async { implicit request =>
    Logger.debug("Petición de listar el laboratory " + id + " [API] respondida.")
    implicit val username = Some("")
    laboratoryService.get(id).map {
      case Some((laboratoryObject, roomsWithComputers)) => Ok(convertToJson(laboratoryObject, roomsWithComputers))
      case _ => NotFound(Json.parse(
        """
          |{
          |  "answer"->"no encontrado"
          |}
        """))
    }
  }


} 
Example 163
Source File: ThingType.scala    From swagger-check   with MIT License 5 votes vote down vote up
package models

import play.api.libs.json.{Format, JsResult, JsString, JsValue}
import play.api.mvc.QueryStringBindable.Parsing

object ThingType extends Enumeration {
  type Type = Value
  val Primary, Secondary, Other = Value

  implicit val jsonFormat = new Format[Type] {
    override def reads(json: JsValue): JsResult[Type] =
      json.validate[String].map(ThingType.withName)

    override def writes(o: Type): JsValue = JsString(o.toString)
  }

  implicit val queryBinder = new Parsing[Type](
    parse = ThingType.withName,
    serialize = v => v.toString,
    error = (key: String, e: Exception) => "Cannot parse parameter %s as ThingType: %s".format(key, e.getMessage)
  )
} 
Example 164
Source File: Migration.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.models

import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Success

import play.api.Logger
import play.api.libs.json.{JsNull, JsNumber, JsString, JsValue, Json}

import org.thp.cortex.services.{OrganizationSrv, UserSrv, WorkerSrv}

import org.elastic4play.controllers.Fields
import org.elastic4play.services.Operation._
import org.elastic4play.services.{DatabaseState, IndexType, MigrationOperations, Operation}
import org.elastic4play.utils.Hasher

@Singleton
class Migration @Inject()(userSrv: UserSrv, organizationSrv: OrganizationSrv, workerSrv: WorkerSrv, implicit val ec: ExecutionContext)
    extends MigrationOperations {

  lazy val logger                                = Logger(getClass)
  def beginMigration(version: Int): Future[Unit] = Future.successful(())

  def endMigration(version: Int): Future[Unit] =
    userSrv.inInitAuthContext { implicit authContext ⇒
      organizationSrv
        .create(Fields(Json.obj("name" → "cortex", "description" → "Default organization", "status" → "Active")))
        .transform(_ ⇒ Success(())) // ignore errors (already exist)
    }

  override def indexType(version: Int): IndexType.Value = if (version > 3) IndexType.indexWithoutMappingTypes else IndexType.indexWithMappingTypes

  val operations: PartialFunction[DatabaseState, Seq[Operation]] = {
    case DatabaseState(1) ⇒
      val hasher = Hasher("MD5")
      Seq(
        // add type to analyzer
        addAttribute("analyzer", "type" → JsString("analyzer")),
        renameAttribute("job", "workerDefinitionId", "analyzerDefinitionId"),
        renameAttribute("job", "workerId", "analyzerId"),
        renameAttribute("job", "workerName", "analyzerName"),
        addAttribute("job", "type"          → JsString(WorkerType.analyzer.toString)),
        addAttribute("report", "operations" → JsString("[]")),
        renameEntity("analyzer", "worker"),
        renameAttribute("worker", "workerDefinitionId", "analyzerDefinitionId"),
        addAttribute("worker", "type" → JsString(WorkerType.analyzer.toString)),
        mapEntity("worker") { worker ⇒
          val id = for {
            organizationId ← (worker \ "_parent").asOpt[String]
            name           ← (worker \ "name").asOpt[String]
            tpe            ← (worker \ "type").asOpt[String]
          } yield hasher.fromString(s"${organizationId}_${name}_$tpe").head.toString
          worker + ("_id" → JsString(id.getOrElse("<null>")))
        },
        renameEntity("analyzerConfig", "workerConfig"),
        addAttribute("workerConfig", "type" → JsString(WorkerType.analyzer.toString))
      )

    case DatabaseState(2) ⇒
      Seq(mapEntity("worker") { worker ⇒
        val definitionId = (worker \ "workerDefinitionId").asOpt[String]
        definitionId
          .flatMap(workerSrv.getDefinition(_).toOption)
          .fold {
            logger.warn(s"no definition found for worker ${definitionId.getOrElse(worker)}. You should probably have to disable and re-enable it")
            worker
          } { definition ⇒
            worker +
              ("version"     → JsString(definition.version)) +
              ("author"      → JsString(definition.author)) +
              ("url"         → JsString(definition.url)) +
              ("license"     → JsString(definition.license)) +
              ("command"     → definition.command.fold[JsValue](JsNull)(c ⇒ JsString(c.toString))) +
              ("dockerImage" → definition.dockerImage.fold[JsValue](JsNull)(JsString.apply)) +
              ("baseConfig"  → definition.baseConfiguration.fold[JsValue](JsNull)(JsString.apply))
          }
      })

    case DatabaseState(3) ⇒
      Seq(
        mapEntity("sequence") { seq ⇒
          val oldId   = (seq \ "_id").as[String]
          val counter = (seq \ "counter").as[JsNumber]
          seq - "counter" - "_routing" +
            ("_id"             → JsString("sequence_" + oldId)) +
            ("sequenceCounter" → counter)
        }
      )
  }
} 
Example 165
Source File: Roles.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.models

import play.api.libs.json.{JsString, JsValue}

import com.sksamuel.elastic4s.http.ElasticDsl.keywordField
import com.sksamuel.elastic4s.mappings.KeywordField
import org.scalactic.{Every, Good, One, Or}

import org.elastic4play.{AttributeError, InvalidFormatAttributeError}
import org.elastic4play.controllers.{InputValue, JsonInputValue, StringInputValue}
import org.elastic4play.models.AttributeFormat
import org.elastic4play.services.Role

import org.thp.cortex.models.JsonFormat.roleFormat

object Roles {
  object read       extends Role("read")
  object analyze    extends Role("analyze")
  object orgAdmin   extends Role("orgadmin")
  object superAdmin extends Role("superadmin")
  val roles: List[Role] = read :: analyze :: orgAdmin :: superAdmin :: Nil

  val roleNames: List[String]            = roles.map(_.name)
  def isValid(roleName: String): Boolean = roleNames.contains(roleName.toLowerCase())

  def withName(roleName: String): Option[Role] = {
    val lowerCaseRole = roleName.toLowerCase()
    roles.find(_.name == lowerCaseRole)
  }
}

object RoleAttributeFormat extends AttributeFormat[Role]("role") {

  override def checkJson(subNames: Seq[String], value: JsValue): Or[JsValue, One[InvalidFormatAttributeError]] = value match {
    case JsString(v) if subNames.isEmpty && Roles.isValid(v) ⇒ Good(value)
    case _                                                   ⇒ formatError(JsonInputValue(value))
  }

  override def fromInputValue(subNames: Seq[String], value: InputValue): Role Or Every[AttributeError] =
    if (subNames.nonEmpty)
      formatError(value)
    else
      (value match {
        case StringInputValue(Seq(v))    ⇒ Good(v)
        case JsonInputValue(JsString(v)) ⇒ Good(v)
        case _                           ⇒ formatError(value)
      }).flatMap(v ⇒ Roles.withName(v).fold[Role Or Every[AttributeError]](formatError(value))(role ⇒ Good(role)))

  override def elasticType(attributeName: String): KeywordField = keywordField(attributeName)
} 
Example 166
Source File: TlpAttributeFormat.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.models

import play.api.libs.json.{JsNumber, JsValue}

import org.scalactic.{Every, Good, One, Or}

import org.elastic4play.{AttributeError, InvalidFormatAttributeError}
import org.elastic4play.controllers.{InputValue, JsonInputValue, StringInputValue}
import org.elastic4play.models.{Attribute, AttributeDefinition, NumberAttributeFormat}
import org.elastic4play.services.DBLists

object TlpAttributeFormat extends NumberAttributeFormat {

  def isValidValue(value: Long): Boolean = 0 <= value && value <= 3

  override def definition(dblists: DBLists, attribute: Attribute[Long]): Seq[AttributeDefinition] =
    Seq(
      AttributeDefinition(
        attribute.attributeName,
        name,
        attribute.description,
        Seq(JsNumber(0), JsNumber(1), JsNumber(2), JsNumber(3)),
        Seq("white", "green", "amber", "red")
      )
    )

  override def checkJson(subNames: Seq[String], value: JsValue): Or[JsValue, One[InvalidFormatAttributeError]] = value match {
    case JsNumber(v) if subNames.isEmpty && isValidValue(v.toLong) ⇒ Good(value)
    case _                                                         ⇒ formatError(JsonInputValue(value))
  }

  override def fromInputValue(subNames: Seq[String], value: InputValue): Long Or Every[AttributeError] =
    value match {
      case StringInputValue(Seq(v)) if subNames.isEmpty ⇒
        try {
          val longValue = v.toLong
          if (isValidValue(longValue)) Good(longValue)
          else formatError(value)
        } catch {
          case _: Throwable ⇒ formatError(value)
        }
      case JsonInputValue(JsNumber(v)) ⇒ Good(v.longValue)
      case _                           ⇒ formatError(value)
    }
} 
Example 167
Source File: MispCtrl.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.controllers

import javax.inject.Inject
import org.elastic4play.controllers.{Authenticated, Fields, FieldsBodyParser, Renderer}
import org.thp.cortex.models.Roles
import org.thp.cortex.services.{MispSrv, WorkerSrv}
import play.api.Logger
import play.api.libs.json.{JsObject, JsValue}
import play.api.mvc._

import scala.concurrent.{ExecutionContext, Future}

class MispCtrl @Inject()(
    mispSrv: MispSrv,
    analyzerSrv: WorkerSrv,
    authenticated: Authenticated,
    fieldsBodyParser: FieldsBodyParser,
    renderer: Renderer,
    components: ControllerComponents,
    implicit val ec: ExecutionContext
) extends AbstractController(components) {

  private[MispCtrl] lazy val logger = Logger(getClass)

  def modules: Action[Fields] = authenticated(Roles.read).async(fieldsBodyParser) { implicit request ⇒
    val (analyzers, analyzerCount) = mispSrv.moduleList
    renderer.toOutput(OK, analyzers, analyzerCount)
  }

  def query: Action[JsValue] = authenticated(Roles.analyze)(parse.json).async { implicit request ⇒
    (request.body \ "module")
      .asOpt[String]
      .fold(Future.successful(BadRequest("Module parameter is not present in request"))) { module ⇒
        request
          .body
          .as[JsObject]
          .fields
          .collectFirst {
            case kv @ (k, _) if k != "module" ⇒ kv
          }
          .fold(Future.successful(BadRequest("Request doesn't contain data to analyze"))) {
            case (mispType, dataJson) ⇒
              dataJson.asOpt[String].fold(Future.successful(BadRequest("Data has invalid type (expected string)"))) { data ⇒
                mispSrv
                  .query(module, mispType, data)
                  .map(Ok(_))
              }
          }
      }
  }
} 
Example 168
Source File: DBListCtrl.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.controllers

import javax.inject.{Inject, Singleton}

import scala.concurrent.{ExecutionContext, Future}

import play.api.libs.json.{JsValue, Json}
import play.api.mvc._

import org.thp.cortex.models.Roles

import org.elastic4play.controllers.{Authenticated, Fields, FieldsBodyParser, Renderer}
import org.elastic4play.services.DBLists
import org.elastic4play.MissingAttributeError

@Singleton
class DBListCtrl @Inject()(
    dblists: DBLists,
    authenticated: Authenticated,
    renderer: Renderer,
    components: ControllerComponents,
    fieldsBodyParser: FieldsBodyParser,
    implicit val ec: ExecutionContext
) extends AbstractController(components) {

  def list: Action[AnyContent] = authenticated(Roles.read).async { _ ⇒
    dblists.listAll.map { listNames ⇒
      renderer.toOutput(OK, listNames)
    }
  }

  def listItems(listName: String): Action[AnyContent] = authenticated(Roles.read) { _ ⇒
    val (src, _) = dblists(listName).getItems[JsValue]
    val items = src
      .map { case (id, value) ⇒ s""""$id":$value""" }
      .intersperse("{", ",", "}")
    Ok.chunked(items).as("application/json")
  }

  def addItem(listName: String): Action[Fields] = authenticated(Roles.superAdmin).async(fieldsBodyParser) { implicit request ⇒
    request.body.getValue("value").fold(Future.successful(NoContent)) { value ⇒
      dblists(listName).addItem(value).map { item ⇒
        renderer.toOutput(OK, item.id)
      }
    }
  }

  def deleteItem(itemId: String): Action[AnyContent] = authenticated(Roles.superAdmin).async { implicit request ⇒
    dblists.deleteItem(itemId).map { _ ⇒
      NoContent
    }
  }

  def updateItem(itemId: String): Action[Fields] = authenticated(Roles.superAdmin).async(fieldsBodyParser) { implicit request ⇒
    request
      .body
      .getValue("value")
      .map { value ⇒
        for {
          item    ← dblists.getItem(itemId)
          _       ← dblists.deleteItem(item)
          newItem ← dblists(item.dblist).addItem(value)
        } yield renderer.toOutput(OK, newItem.id)
      }
      .getOrElse(Future.failed(MissingAttributeError("value")))
  }

  def itemExists(listName: String): Action[Fields] = authenticated(Roles.read).async(fieldsBodyParser) { implicit request ⇒
    val itemKey   = request.body.getString("key").getOrElse(throw MissingAttributeError("Parameter key is missing"))
    val itemValue = request.body.getValue("value").getOrElse(throw MissingAttributeError("Parameter value is missing"))
    dblists(listName).exists(itemKey, itemValue).map(r ⇒ Ok(Json.obj("found" → r)))
  }
} 
Example 169
Source File: response.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime

import com.linkedin.data.DataList
import org.coursera.common.stringkey.StringKeyFormat
import org.coursera.naptime.model.KeyFormat
import org.coursera.naptime.model.Keyed
import org.coursera.naptime.actions.NaptimeSerializer
import org.coursera.naptime.actions.RestActionCategoryEngine2
import play.api.libs.json.JsValue
import play.api.libs.json.OFormat
import play.api.mvc.Result
import play.api.mvc.Results

sealed abstract class RestResponse[+T] {
  def isOk: Boolean
  def isError: Boolean
  def isRedirect: Boolean

  def map[U](fn: T => U): RestResponse[U]
}

final case class Ok[+T](
    content: T,
    related: Map[ResourceName, Ok.Related[_, _]] = Map.empty,
    pagination: Option[ResponsePagination] = None,
    
  case class Related[K, A](
      resourceName: ResourceName,
      objects: Seq[Keyed[K, A]],
      jsonFormat: OFormat[A],
      keyFormat: KeyFormat[K],
      fields: ResourceFields[A]) {
    def toJson(requestFields: RequestFields): Seq[JsValue] = {
      val finalFields = requestFields
        .forResource(resourceName)
        .getOrElse(RequestFields.empty)
        .mergeWithDefaults(fields.defaultFields)
      JsonUtilities.outputSeq(objects, finalFields)(jsonFormat, keyFormat)
    }

    def toPegasus(requestFields: RequestFields, dataList: DataList): RequestFields = {
      RestActionCategoryEngine2.serializeCollection(
        dataList,
        objects,
        keyFormat,
        NaptimeSerializer.playJsonFormats(jsonFormat),
        requestFields,
        fields)
    }
  }

}

final case class RestError(error: NaptimeActionException) extends RestResponse[Nothing] {
  override val isOk = false
  override val isError = true
  override val isRedirect = false

  override def map[U](fn: Nothing => U): RestResponse[U] = this
}

final case class Redirect(url: String, isTemporary: Boolean) extends RestResponse[Nothing] {
  override val isOk = false
  override val isError = false
  override val isRedirect = true

  override def map[U](fn: Nothing => U): RestResponse[U] = this

  def result: Result = {
    if (isTemporary) {
      Results.TemporaryRedirect(url)
    } else {
      Results.MovedPermanently(url)
    }
  }
} 
Example 170
Source File: DummyAuthenticatorService.scala    From playsonify   with MIT License 5 votes vote down vote up
package com.alexitc.example

import com.alexitc.playsonify.core.FutureApplicationResult
import com.alexitc.playsonify.play.AbstractAuthenticatorService
import org.scalactic.{One, Or}
import play.api.http.HeaderNames
import play.api.libs.json.JsValue
import play.api.mvc.Request

import scala.concurrent.Future
import scala.util.Try

class DummyAuthenticatorService extends AbstractAuthenticatorService[Int] {

  override def authenticate(request: Request[JsValue]): FutureApplicationResult[Int] = {
    val userIdMaybe = request
      .headers
      .get(HeaderNames.AUTHORIZATION)
      .flatMap { header => Try(header.toInt).toOption }

    val result = Or.from(userIdMaybe, One(SimpleAuthError.InvalidAuthorizationHeader))
    Future.successful(result)
  }
} 
Example 171
Source File: CustomAuthenticator.scala    From playsonify   with MIT License 5 votes vote down vote up
package com.alexitc.playsonify.play.common

import com.alexitc.playsonify.core.FutureApplicationResult
import com.alexitc.playsonify.play.AbstractAuthenticatorService
import org.scalactic.{One, Or}
import play.api.libs.json.JsValue
import play.api.mvc.Request
import play.api.test.Helpers.AUTHORIZATION

import scala.concurrent.Future

class CustomAuthenticator extends AbstractAuthenticatorService[CustomUser] {

  override def authenticate(request: Request[JsValue]): FutureApplicationResult[CustomUser] = {
    val header = request
        .headers
        .get(AUTHORIZATION)
        .map(CustomUser.apply)

    val result = Or.from(header, One(CustomError.FailedAuthError))
    Future.successful(result)
  }
} 
Example 172
Source File: PublicErrorRenderer.scala    From playsonify   with MIT License 5 votes vote down vote up
package com.alexitc.playsonify.akka

import com.alexitc.playsonify.models._
import play.api.libs.json.{JsValue, Json}

// TODO: common with play module
class PublicErrorRenderer {

  import PublicErrorRenderer._

  
  def renderPublicError(publicError: PublicError): JsValue = publicError match {
    case e: GenericPublicError =>
      val obj = Json.obj(
        "type" -> GenericErrorType,
        "message" -> e.message
      )
      Json.toJson(obj)

    case e: FieldValidationError =>
      val obj = Json.obj(
        "type" -> FieldValidationErrorType,
        "field" -> e.field,
        "message" -> e.message
      )
      Json.toJson(obj)

    case e: HeaderValidationError =>
      val obj = Json.obj(
        "type" -> HeaderValidationErrorType,
        "header" -> e.header,
        "message" -> e.message
      )
      Json.toJson(obj)

    case e: InternalError =>
      val obj = Json.obj(
        "type" -> ServerErrorType,
        "errorId" -> e.id.string,
        "message" -> e.message
      )
      Json.toJson(obj)
  }
}

object PublicErrorRenderer {
  val GenericErrorType = "generic-error"
  val FieldValidationErrorType = "field-validation-error"
  val HeaderValidationErrorType = "header-validation-error"
  val ServerErrorType = "server-error"
} 
Example 173
Source File: PlayDefinitions.scala    From circe-benchmarks   with Apache License 2.0 5 votes vote down vote up
package io.circe.benchmarks

import org.openjdk.jmh.annotations._
import play.api.libs.functional.syntax._
import play.api.libs.json.{ Format, JsPath, JsValue, Json, Writes }

trait PlayFooInstances {
  implicit val playFormatFoo: Format[Foo] = (
    (JsPath \ "s")
      .format[String]
      .and((JsPath \ "d").format[Double])
      .and((JsPath \ "i").format[Int])
      .and((JsPath \ "l").format[Long])
      .and((JsPath \ "bs").format[List[Boolean]])
    )(Foo.apply, unlift(Foo.unapply))
}

trait PlayData { self: ExampleData =>
  @inline def encodeP[A](a: A)(implicit encode: Writes[A]): JsValue = encode.writes(a)

  val foosP: JsValue = encodeP(foos)
  val intsP: JsValue = encodeP(ints)
}

trait PlayWriting { self: ExampleData =>
  @Benchmark
  def writeFoosPlay: String = Json.stringify(encodeP(foos))

  @Benchmark
  def writeIntsPlay: String = Json.stringify(encodeP(ints))
}

trait PlayReading { self: ExampleData =>
  @Benchmark
  def readFoosPlay: Map[String, Foo] = Json.parse(foosJson).as[Map[String, Foo]]

  @Benchmark
  def readIntsPlay: List[Int] = Json.parse(intsJson).as[List[Int]]
}

trait PlayEncoding { self: ExampleData =>
  @Benchmark
  def encodeFoosPlay: JsValue = encodeP(foos)

  @Benchmark
  def encodeIntsPlay: JsValue = encodeP(ints)
}

trait PlayDecoding { self: ExampleData =>
  @Benchmark
  def decodeFoosPlay: Map[String, Foo] = foosP.as[Map[String, Foo]]

  @Benchmark
  def decodeIntsPlay: List[Int] = intsP.as[List[Int]]
}

trait PlayPrinting { self: ExampleData =>
  @Benchmark
  def printFoosPlay: String = Json.stringify(foosP)

  @Benchmark
  def printIntsPlay: String = Json.stringify(intsP)
}

trait PlayParsing { self: ExampleData =>
  @Benchmark
  def parseFoosPlay: JsValue = Json.parse(foosJson)

  @Benchmark
  def parseIntsPlay: JsValue = Json.parse(intsJson)
} 
Example 174
Source File: StringConstraints4.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.draft4.constraints

import com.eclipsesource.schema.internal.Keywords
import com.eclipsesource.schema.internal.constraints.Constraints.{AnyConstraints, HasAnyConstraint, StringConstraints}
import com.eclipsesource.schema.internal.validation.VA
import com.eclipsesource.schema.{SchemaResolutionContext, SchemaType, SchemaValue}
import com.osinka.i18n.Lang
import play.api.libs.json.{JsNumber, JsString, JsValue}

case class StringConstraints4(minLength: Option[Int] = None,
                              maxLength: Option[Int] = None,
                              pattern: Option[String] = None,
                              format: Option[String] = None,
                              any: AnyConstraints
                             ) extends HasAnyConstraint with StringConstraints {

  import com.eclipsesource.schema.internal.validators.StringValidators._

  override def subSchemas: Set[SchemaType] = any.subSchemas

  override def resolvePath(path: String): Option[SchemaType] = path match {
    case Keywords.String.MinLength => minLength.map(min => SchemaValue(JsNumber(min)))
    case Keywords.String.MaxLength => maxLength.map(max => SchemaValue(JsNumber(max)))
    case Keywords.String.Pattern => pattern.map(p => SchemaValue(JsString(p)))
    case Keywords.String.Format => format.map(f => SchemaValue(JsString(f)))
    case other => any.resolvePath(other)
  }

  def validate(schema: SchemaType, json: JsValue, context: SchemaResolutionContext)
              (implicit lang: Lang): VA[JsValue] = {
    val reader = for {
      minLength <- validateMinLength(minLength)
      maxLength <- validateMaxLength(maxLength)
      pattern <- validatePattern(pattern)
      format <- validateFormat(format)
    } yield minLength |+| maxLength |+| pattern |+| format
    reader.run(context)
      .repath(_.compose(context.instancePath))
      .validate(json)
  }
} 
Example 175
Source File: ArrayConstraints4.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.draft4.constraints

import com.eclipsesource.schema.internal.Keywords
import com.eclipsesource.schema.{SchemaArray, SchemaResolutionContext, SchemaTuple, SchemaType, SchemaValue}
import com.eclipsesource.schema.internal.constraints.Constraints.{AnyConstraints, ArrayConstraints, Constraint}
import com.eclipsesource.schema.internal.validation.VA
import com.eclipsesource.schema.internal.validators.TupleValidators
import com.osinka.i18n.Lang
import play.api.libs.json.{JsBoolean, JsNumber, JsValue}
import scalaz.Success

case class ArrayConstraints4(maxItems: Option[Int] = None,
                             minItems: Option[Int] = None,
                             additionalItems: Option[SchemaType] = None,
                             unique: Option[Boolean] = None,
                             any: AnyConstraints = AnyConstraints4()
                            ) extends ArrayConstraints with Constraint {

  import com.eclipsesource.schema.internal.validators.ArrayConstraintValidators._

  override def subSchemas: Set[SchemaType] =
    additionalItems.map(Set(_)).getOrElse(Set.empty) ++ any.subSchemas

  override def validate(schema: SchemaType, json: JsValue, resolutionContext: SchemaResolutionContext)
                       (implicit lang: Lang): VA[JsValue] = {

    val reader = for {
      minItemsRule <- validateMinItems(minItems)
      maxItemsRule <- validateMaxItems(maxItems)
      uniqueRule <- validateUniqueness(unique)
    } yield {
      minItemsRule |+| maxItemsRule |+| uniqueRule
    }

    schema match {
      case t: SchemaTuple =>
        TupleValidators
          .validateTuple(additionalItems, t)
          .flatMap(x => reader.map(f => f |+| x))
          .run(resolutionContext)
          .repath(_.compose(resolutionContext.instancePath))
          .validate(json)
      case _: SchemaArray =>
        reader.run(resolutionContext)
          .repath(_.compose(resolutionContext.instancePath))
          .validate(json)
      case _ => Success(json)
    }
  }

  def resolvePath(path: String): Option[SchemaType] = path match {
    case Keywords.Array.MinItems => minItems.map(min => SchemaValue(JsNumber(min)))
    case Keywords.Array.MaxItems => maxItems.map(max => SchemaValue(JsNumber(max)))
    case Keywords.Array.AdditionalItems => additionalItems
    case Keywords.Array.UniqueItems => unique.map(u => SchemaValue(JsBoolean(u)))
    case other => any.resolvePath(other)
  }
} 
Example 176
Source File: ObjectConstraints4.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.draft4.constraints

import com.eclipsesource.schema.{SchemaMap, SchemaObject, SchemaProp, SchemaResolutionContext, SchemaType, SchemaValue}
import com.eclipsesource.schema.internal.constraints.Constraints._
import com.eclipsesource.schema.internal.validation.VA
import com.osinka.i18n.Lang
import play.api.libs.json.{JsNumber, JsObject, JsValue}
import scalaz.std.option._
import scalaz.std.set._
import scalaz.syntax.semigroup._
import scalaz.Success
import com.eclipsesource.schema.internal._

case class ObjectConstraints4(additionalProps: Option[SchemaType] = None,
                              dependencies: Option[Map[String, SchemaType]] = None,
                              patternProps: Option[Map[String, SchemaType]] = None,
                              required: Option[Seq[String]] = None,
                              minProperties: Option[Int] = None,
                              maxProperties: Option[Int] = None,
                              any: AnyConstraints = AnyConstraints4()
                             ) extends HasAnyConstraint with ObjectConstraints {

  type A = ObjectConstraints4

  import com.eclipsesource.schema.internal.validators.ObjectValidators._

  override def subSchemas: Set[SchemaType] =
    (additionalProps.map(Set(_)) |+| dependencies.map(_.values.toSet) |+| patternProps.map(_.values.toSet))
      .getOrElse(Set.empty[SchemaType]) ++ any.subSchemas

  override def resolvePath(path: String): Option[SchemaType] = path match {
    case Keywords.Object.AdditionalProperties => additionalProps
    case Keywords.Object.Dependencies => dependencies.map(entries =>
      SchemaMap(Keywords.Object.Dependencies, entries.toSeq.map(e => SchemaProp(e._1, e._2)))
    )
    case Keywords.Object.PatternProperties => patternProps.map(patternProps => SchemaMap(
      Keywords.Object.PatternProperties,
      patternProps.toSeq.map(e => SchemaProp(e._1, e._2)))
    )
    case Keywords.Object.MinProperties => minProperties.map(min => SchemaValue(JsNumber(min)))
    case Keywords.Object.MaxProperties => maxProperties.map(max => SchemaValue(JsNumber(max)))
    case other => any.resolvePath(other)
  }

  override def validate(schema: SchemaType, json: JsValue, context: SchemaResolutionContext)
                       (implicit lang: Lang): VA[JsValue] =
    (schema, json) match {
      case (obj@SchemaObject(_, _, _), jsObject@JsObject(_)) =>
        val validation = for {
          _ <- validateDependencies(schema, dependencies, jsObject)
          remaining <- validateProps(obj.properties, required, jsObject)
          unmatched <- validatePatternProps(patternProps, jsObject.fields.toSeq)
          _ <- validateAdditionalProps(additionalProps, unmatched.intersect(remaining), json)
          _ <- validateMinProperties(minProperties, jsObject)
          _ <- validateMaxProperties(maxProperties, jsObject)
        } yield schema

        val (_, _, result) = validation.run(context, Success(json))
        result
      case _ => Success(json)
    }
}

object ObjectConstraints4 {
  def emptyObject: SchemaType = SchemaObject(Seq.empty, ObjectConstraints4())
} 
Example 177
Source File: AnyConstraints4.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.draft4.constraints

import com.eclipsesource.schema.{SchemaMap, SchemaProp, SchemaResolutionContext, SchemaSeq, SchemaType, SchemaValue}
import com.eclipsesource.schema.internal.Keywords
import com.eclipsesource.schema.internal.constraints.Constraints.AnyConstraints
import com.eclipsesource.schema.internal.validation.{Rule, VA}
import com.osinka.i18n.Lang
import play.api.libs.json.{JsArray, JsString, JsValue}
import scalaz.std.option._
import scalaz.std.set._
import scalaz.syntax.semigroup._

case class AnyConstraints4(schemaType: Option[String] = None,
                           allOf: Option[Seq[SchemaType]] = None,
                           anyOf: Option[Seq[SchemaType]] = None,
                           oneOf: Option[Seq[SchemaType]] = None,
                           definitions: Option[Map[String, SchemaType]] = None,
                           enum: Option[Seq[JsValue]] = None,
                           not: Option[SchemaType] = None,
                           description: Option[String] = None,
                           id: Option[String] = None
                          )
  extends AnyConstraints {

  override def subSchemas: Set[SchemaType] =
    (definitions.map(_.values.toSet) |+| allOf.map(_.toSet) |+| anyOf.map(_.toSet) |+| oneOf.map(_.toSet))
      .getOrElse(Set.empty[SchemaType])

  override def resolvePath(path: String): Option[SchemaType] = path match {
    case Keywords.Any.Type => schemaType.map(t => SchemaValue(JsString(t)))
    case Keywords.Any.AllOf => allOf.map(types => SchemaSeq(types))
    case Keywords.Any.AnyOf => anyOf.map(types => SchemaSeq(types))
    case Keywords.Any.OneOf => oneOf.map(types => SchemaSeq(types))
    case Keywords.Any.Definitions => definitions.map(entries =>
      SchemaMap(
        Keywords.Any.Definitions,
        entries.toSeq.map { case (name, schema) => SchemaProp(name, schema) })
    )
    case Keywords.Any.Enum => enum.map(e => SchemaValue(JsArray(e)))
    case Keywords.Any.Not => not
    case "id" => id.map(id => SchemaValue(JsString(id)))
    case _ => None
  }

  import com.eclipsesource.schema.internal.validators.AnyConstraintValidators._

  override def validate(schema: SchemaType, json: JsValue, context: SchemaResolutionContext)(implicit lang: Lang): VA[JsValue] = {
    val reader: scalaz.Reader[SchemaResolutionContext, Rule[JsValue, JsValue]] = for {
      allOfRule <- validateAllOf(schema, allOf)
      anyOfRule <- validateAnyOf(schema, anyOf)
      oneOfRule <- validateOneOf(schema, oneOf)
      enumRule <- validateEnum(enum)
      notRule <- validateNot(not)
    } yield allOfRule |+| anyOfRule |+| oneOfRule |+| enumRule |+| notRule
    reader
      .run(context)
      .repath(_.compose(context.instancePath))
      .validate(json)
  }

} 
Example 178
Source File: NumberConstraints4.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.draft4.constraints

import com.eclipsesource.schema.{SchemaInteger, SchemaNumber, SchemaResolutionContext, SchemaType, SchemaValue}
import com.eclipsesource.schema.internal.{Keywords, SchemaUtil, ValidatorMessages}
import com.eclipsesource.schema.internal.constraints.Constraints._
import com.eclipsesource.schema.internal.validation.{Rule, VA}
import com.osinka.i18n.Lang
import play.api.libs.json.{JsNumber, JsString, JsValue}
import scalaz.Success

case class NumberConstraints4(min: Option[Minimum] = None,
                              max: Option[Maximum] = None,
                              multipleOf: Option[BigDecimal] = None,
                              format: Option[String] = None,
                              any: AnyConstraints = AnyConstraints4()
                             ) extends HasAnyConstraint with NumberConstraints {

  import com.eclipsesource.schema.internal.validators.NumberValidators._

  override def subSchemas: Set[SchemaType] = any.subSchemas

  override def resolvePath(path: String): Option[SchemaType] = path match {
    case Keywords.Number.Min => min.map(m => SchemaValue(JsNumber(m.min)))
    case Keywords.Number.Max => max.map(m => SchemaValue(JsNumber(m.max)))
    case Keywords.Number.MultipleOf => multipleOf.map(m => SchemaValue(JsNumber(m)))
    case Keywords.String.Format => format.map(f => SchemaValue(JsString(f)))
    case other => any.resolvePath(other)
  }

  def isInt(implicit lang: Lang): scalaz.Reader[SchemaResolutionContext, Rule[JsValue, JsValue]] =
    scalaz.Reader { context =>
      Rule.fromMapping {
        case json@JsNumber(number) if number.isWhole => Success(json)
        case other =>
          SchemaUtil.failure(
            Keywords.Any.Type,
            ValidatorMessages("err.expected.type", "integer", SchemaUtil.typeOfAsString(other)),
            context.schemaPath,
            context.instancePath,
            other
          )
      }
    }

  override def validate(schema: SchemaType, json: JsValue, context: SchemaResolutionContext)
                       (implicit lang: Lang): VA[JsValue] = {

    val reader = for {
      maxRule <- validateMax(max)
      minRule <- validateMin(min)
      multipleOfRule <- validateMultipleOf(multipleOf)
      format <- validateFormat(format)
    } yield maxRule |+| minRule |+| multipleOfRule |+| format

    schema match {
      case SchemaInteger(_) =>
        isInt.flatMap(x => reader.map(y => x |+| y))
          .run(context)
          .repath(_.compose(context.instancePath))
          .validate(json)
      case SchemaNumber(_) =>
        reader
          .run(context)
          .repath(_.compose(context.instancePath))
          .validate(json)
      case _ => Success(json)
    }
  }
} 
Example 179
Source File: CompoundValidator.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.validators
import com.eclipsesource.schema.internal.validation.VA
import com.eclipsesource.schema.internal.{Keywords, Results, ValidatorMessages}
import com.eclipsesource.schema.{CompoundSchemaType, _}
import com.osinka.i18n.Lang
import play.api.libs.json.JsValue

object CompoundValidator extends SchemaTypeValidator[CompoundSchemaType] {
  override def validate(schema: CompoundSchemaType, json: => JsValue, context: SchemaResolutionContext)
                       (implicit lang: Lang): VA[JsValue] = {
    val result: Option[VA[JsValue]] = schema.alternatives
      .map(_.validate(json, context))
      .find(_.isSuccess)

    result.getOrElse(
        Results.failureWithPath(
          Keywords.Any.Type,
          ValidatorMessages("comp.no.schema"),
          context,
          json
        )
      )
  }
} 
Example 180
Source File: ArrayValidator.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.validators

import com.eclipsesource.schema.internal.validation.VA
import com.eclipsesource.schema.internal.{Keywords, Results, SchemaUtil, ValidatorMessages}
import com.eclipsesource.schema.{SchemaArray, SchemaResolutionContext}
import com.osinka.i18n.Lang
import play.api.libs.json.{JsArray, JsValue}
import scalaz.{Failure, Success}


object ArrayValidator extends SchemaTypeValidator[SchemaArray] {

  override def validate(schema: SchemaArray, json: => JsValue, context: SchemaResolutionContext)
                       (implicit lang: Lang): VA[JsValue] = {
    json match {
      case JsArray(values) =>
        val elements: Seq[VA[JsValue]] = values.toSeq.zipWithIndex.map { case (jsValue, idx) =>
          schema.item.validate(
            jsValue,
            context.updateScope(
              _.copy(instancePath = context.instancePath \ idx.toString)
            )
          )
        }
        if (elements.exists(_.isFailure)) {
          Failure(elements.collect { case Failure(err) => err }.reduceLeft(_ ++ _))
        } else {
          val updatedArr = JsArray(elements.collect { case Success(js) => js })
          schema.constraints.validate(schema, updatedArr, context)
        }
      case _ =>
        Results.failureWithPath(
          Keywords.Any.Type,
          ValidatorMessages("err.expected.type", SchemaUtil.typeOfAsString(json)),
          context,
          json
        )
    }
  }

} 
Example 181
Source File: StringConstraints7.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.draft7.constraints

import com.eclipsesource.schema.{SchemaResolutionContext, SchemaType, SchemaValue}
import com.eclipsesource.schema.internal.Keywords
import com.eclipsesource.schema.internal.constraints.Constraints.{AnyConstraints, HasAnyConstraint, StringConstraints}
import com.eclipsesource.schema.internal.validation.VA
import com.osinka.i18n.Lang
import play.api.libs.json.{JsNumber, JsString, JsValue}

case class StringConstraints7(minLength: Option[Int] = None,
                             maxLength: Option[Int] = None,
                             pattern: Option[String] = None,
                             format: Option[String] = None,
                             any: AnyConstraints = AnyConstraints7()
                            ) extends HasAnyConstraint with StringConstraints {

  import com.eclipsesource.schema.internal.validators.StringValidators._

  override def subSchemas: Set[SchemaType] = any.subSchemas

  override def resolvePath(path: String): Option[SchemaType] = path match {
    case Keywords.String.MinLength => minLength.map(min => SchemaValue(JsNumber(min)))
    case Keywords.String.MaxLength => maxLength.map(max => SchemaValue(JsNumber(max)))
    case Keywords.String.Pattern => pattern.map(p => SchemaValue(JsString(p)))
    case Keywords.String.Format => format.map(f => SchemaValue(JsString(f)))
    case other => any.resolvePath(other)
  }

  def validate(schema: SchemaType, json: JsValue, context: SchemaResolutionContext)
              (implicit lang: Lang): VA[JsValue] = {
    val reader = for {
      minLength <- validateMinLength(minLength)
      maxLength <- validateMaxLength(maxLength)
      pattern <- validatePattern(pattern)
      format <- validateFormat(format)
    } yield minLength |+| maxLength |+| pattern |+| format
    reader.run(context)
      .repath(_.compose(context.instancePath))
      .validate(json)
  }
} 
Example 182
Source File: ObjectConstraints7.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.draft7.constraints

import com.eclipsesource.schema.{SchemaMap, SchemaObject, SchemaProp, SchemaResolutionContext, SchemaType, SchemaValue}
import com.eclipsesource.schema.internal.Keywords
import com.eclipsesource.schema.internal.constraints.Constraints.{AnyConstraints, Constraint, HasAnyConstraint, ObjectConstraints}
import com.eclipsesource.schema.internal.validation.VA
import com.osinka.i18n.Lang
import play.api.libs.json.{JsNumber, JsObject, JsValue}
import scalaz.Success
import scalaz.std.option._
import scalaz.std.set._
import scalaz.syntax.semigroup._
import com.eclipsesource.schema.internal._

case class ObjectConstraints7(additionalProps: Option[SchemaType] = None,
                              dependencies: Option[Map[String, SchemaType]] = None,
                              patternProps: Option[Map[String, SchemaType]] = None,
                              required: Option[Seq[String]] = None,
                              minProperties: Option[Int] = None,
                              maxProperties: Option[Int] = None,
                              propertyNames: Option[SchemaType] = None,
                              any: AnyConstraints = AnyConstraints7()
                             ) extends HasAnyConstraint with ObjectConstraints {

  import com.eclipsesource.schema.internal.validators.ObjectValidators._

   override def subSchemas: Set[SchemaType] =
    (additionalProps.map(Set(_)) |+| dependencies.map(_.values.toSet) |+| patternProps.map(_.values.toSet))
      .getOrElse(Set.empty[SchemaType]) ++ any.subSchemas

  override def resolvePath(path: String): Option[SchemaType] = path match {
    case Keywords.Object.AdditionalProperties => additionalProps
    case Keywords.Object.Dependencies => dependencies.map(entries =>
      SchemaMap(Keywords.Object.Dependencies, entries.toSeq.map(e => SchemaProp(e._1, e._2)))
    )
    case Keywords.Object.PatternProperties => patternProps.map(patternProps =>
      SchemaMap(Keywords.Object.PatternProperties, patternProps.toSeq.map(e => SchemaProp(e._1, e._2)))
    )
    case Keywords.Object.MinProperties => minProperties.map(min => SchemaValue(JsNumber(min)))
    case Keywords.Object.MaxProperties => maxProperties.map(max => SchemaValue(JsNumber(max)))
    case "propertyNames" => propertyNames
    case other => any.resolvePath(other)
  }

  override def validate(schema: SchemaType, json: JsValue, context: SchemaResolutionContext)
                       (implicit lang: Lang): VA[JsValue] =
    (schema, json) match {
      case (obj@SchemaObject(_, _, _), jsObject@JsObject(_)) =>
        val validation = for {
          _ <- validateDependencies(schema, dependencies, jsObject)
          remaining <- validateProps(obj.properties, required, jsObject)
          unmatched <- validatePatternProps(patternProps, jsObject.fields.toSeq)
          _ <- validateAdditionalProps(additionalProps, unmatched.intersect(remaining), json)
          _ <- validateMinProperties(minProperties, jsObject)
          _ <- validateMaxProperties(maxProperties, jsObject)
          _ <- validatePropertyNames(propertyNames, jsObject)
        } yield schema

        val (_, _, result) = validation.run(context, Success(json))
        result
      case _ => Success(json)
    }
} 
Example 183
Source File: NumberConstraints7.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.draft7.constraints

import com.eclipsesource.schema.{SchemaInteger, SchemaNumber, SchemaResolutionContext, SchemaType, SchemaValue}
import com.eclipsesource.schema.internal.{Keywords, SchemaUtil, ValidatorMessages}
import com.eclipsesource.schema.internal.constraints.Constraints._
import com.eclipsesource.schema.internal.validation.{Rule, VA}
import com.osinka.i18n.Lang
import play.api.libs.json.{JsNumber, JsString, JsValue}
import scalaz.Success

case class NumberConstraints7(min: Option[Minimum] = None,
                             max: Option[Maximum] = None,
                             multipleOf: Option[BigDecimal] = None,
                             format: Option[String] = None,
                             any: AnyConstraints = AnyConstraints7()
                            ) extends HasAnyConstraint with NumberConstraints {

  import com.eclipsesource.schema.internal.validators.NumberValidators._

  override def subSchemas: Set[SchemaType] = any.subSchemas

  override def resolvePath(path: String): Option[SchemaType] = path match {
    case Keywords.Number.Min => min.map(m => SchemaValue(JsNumber(m.min)))
    case Keywords.Number.Max => max.map(m => SchemaValue(JsNumber(m.max)))
    case Keywords.Number.MultipleOf => multipleOf.map(m => SchemaValue(JsNumber(m)))
    case Keywords.String.Format => format.map(f => SchemaValue(JsString(f)))
    case other => any.resolvePath(other)
  }

  def isInt(implicit lang: Lang): scalaz.Reader[SchemaResolutionContext, Rule[JsValue, JsValue]] =
    scalaz.Reader { context =>
      Rule.fromMapping {
        case json@JsNumber(number) if number.isWhole => Success(json)
        case other =>
          SchemaUtil.failure(
            Keywords.Any.Type,
            ValidatorMessages("err.expected.type", "integer", SchemaUtil.typeOfAsString(other)),
            context.schemaPath,
            context.instancePath,
            other
          )
      }
    }

  override def validate(schema: SchemaType, json: JsValue, context: SchemaResolutionContext)
                       (implicit lang: Lang): VA[JsValue] = {

    val reader = for {
      maxRule <- validateMax(max)
      minRule <- validateMin(min)
      multipleOfRule <- validateMultipleOf(multipleOf)
      format <- validateFormat(format)
    } yield maxRule |+| minRule |+| multipleOfRule |+| format

    schema match {
      case SchemaInteger(_) =>
        isInt.flatMap(x => reader.map(y => x |+| y))
          .run(context)
          .repath(_.compose(context.instancePath))
          .validate(json)
      case SchemaNumber(_) =>
        reader
          .run(context)
          .repath(_.compose(context.instancePath))
          .validate(json)
      case _ => Success(json)
    }
  }
} 
Example 184
Source File: SimplePerformanceSpec.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema

import java.net.URL

import com.eclipsesource.schema.drafts.Version4
import org.specs2.mutable.Specification
import play.api.libs.json.{JsValue, Json}

class SimplePerformanceSpec extends Specification {

  import Version4._

  def timed(name: String)(body: => Unit) {
    val start = System.currentTimeMillis()
    body
    println(name + ": " + (System.currentTimeMillis() - start) + " ms")
  }

  val validator = SchemaValidator(Some(Version4))
  val schemaUrl: URL = getClass.getResource("/issue-99-1.json")
  val schema: SchemaType = JsonSource.schemaFromUrl(schemaUrl).get

  val instance: JsValue = Json.parse("""{ "mything": "the thing" }""".stripMargin)

  timed("preloaded") {
    for (_ <- 1 to 1000) validator.validate(schema, instance)
  }
  timed("url based") {
    for (_ <- 1 to 1000) validator.validate(schemaUrl)(instance)
  }

} 
Example 185
Source File: ResolveObjectConstraintsSpec.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.refs

import com.eclipsesource.schema.{JsonSource, SchemaString, SchemaType}
import com.eclipsesource.schema.drafts.{Version4, Version7}
import com.eclipsesource.schema.internal.draft7.constraints.StringConstraints7
import org.specs2.mutable.Specification
import play.api.libs.json.{JsBoolean, JsObject, JsString, JsValue}

class ResolveObjectConstraintsSpec extends Specification {

  "draft v4" should {

    import Version4._
    val resolver = SchemaRefResolver(Version4)

    "resolve dependencies constraint" in {
      val schema = JsonSource.schemaFromString(
        """{
          |"dependencies": {
          |    "a": "b",
          |    "c": ["d", "e"]
          |  }
          |}""".stripMargin).get

      val resolved = resolver.resolveFromRoot("#/dependencies/c/1", SchemaResolutionScope(schema))
      resolved.right.map(_.toJson) must beRight[JsValue](JsString("e"))
    }

    "resolve patternProperties constraint" in {
      val schema = JsonSource.schemaFromString(
        """{
          |  "patternProperties": {
          |        "^(/[^/]+)+$": {}
          |  }
          |}""".stripMargin).get
      val result = resolver.resolveFromRoot("#/patternProperties", SchemaResolutionScope(schema))
      result.map(_.toJson).right.get must beAnInstanceOf[JsObject]
    }

    "should resolve additionalProperties constraint" in {
      val schema = JsonSource.schemaFromString(
        """{
          |  "additionalProperties": false
          |}""".stripMargin).get

      val result = resolver.resolveFromRoot("#/additionalProperties", SchemaResolutionScope(schema))
      result.map(_.toJson) must beRight[JsValue](JsBoolean(false))
    }
  }

  "draft v7" should {

    import Version7._
    val resolver = SchemaRefResolver(Version7)

    "resolve dependencies constraint" in {
      val schema = JsonSource.schemaFromString(
        """{
          |"dependencies": {
          |    "a": "b",
          |    "c": ["d", "e"]
          |  }
          |}""".stripMargin).get

      val resolved = resolver.resolveFromRoot("#/dependencies/c/1", SchemaResolutionScope(schema))
      resolved.right.map(_.toJson) must beRight[JsValue](JsString("e"))
    }

    "resolve patternProperties constraint" in {
      val schema = JsonSource.schemaFromString(
        """{
          |  "patternProperties": {
          |        "^(/[^/]+)+$": {}
          |  }
          |}""".stripMargin).get
      val result = resolver.resolveFromRoot("#/patternProperties", SchemaResolutionScope(schema))
      result.map(_.toJson).right.get must beAnInstanceOf[JsObject]
    }

    "should resolve additionalProperties constraint" in {
      val schema = JsonSource.schemaFromString(
        """{
          |  "additionalProperties": false
          |}""".stripMargin).get

      val result = resolver.resolveFromRoot("#/additionalProperties", SchemaResolutionScope(schema))
      result.map(_.toJson) must beRight[JsValue](JsBoolean(false))
    }

    "should resolve additionalProperties constraint" in {
      val schema = JsonSource.schemaFromString(
        """{
          |  "propertyNames": {"maxLength": 3}
          |}""".stripMargin).get

      val result = resolver.resolveFromRoot("#/propertyNames", SchemaResolutionScope(schema))
      result.map(_.resolved) must beRight[SchemaType](SchemaString(StringConstraints7().copy(maxLength = Some(3))))
    }
  }
} 
Example 186
Source File: package.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal

import com.eclipsesource.schema.SchemaType
import com.osinka.i18n.Lang
import play.api.libs.json.{JsValue, Json, JsonValidationError, Writes}

package object refs {

  implicit class ResolveResultExtensionOps(resolvedResult: ResolvedResult) {
    def toJson(implicit writes: Writes[SchemaType]): JsValue =
      Json.toJson(resolvedResult.resolved)
  }

  implicit class SchemaRefResolverExtensionOps(resolver: SchemaRefResolver) {
    def resolveFromRoot(ref: String, scope: SchemaResolutionScope)
                       (implicit lang: Lang = Lang.Default): Either[JsonValidationError, ResolvedResult] = {
      resolver.resolve(scope.documentRoot, Ref(ref), scope).toEither
    }
  }
} 
Example 187
Source File: TinCanSpec.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema

import org.specs2.mutable.Specification
import play.api.libs.json.{JsValue, Json}

import scala.util.Try

class TinCanSpec extends Specification { self =>

  import com.eclipsesource.schema.drafts.Version4

  val instance = JsonSource.fromString(
    """
      |{
      |  "actor": {
      |    "name": "Sally Glider",
      |    "mbox": "mailto:[email protected]"
      |  },
      |  "verb": {
      |    "id": "http://adlnet.gov/expapi/verbs/experienced",
      |    "display": { "en-US": "experienced" }
      |  },
      |  "object": {
      |    "id": "http://example.com/activities/solo-hang-gliding",
      |    "definition": {
      |      "name": { "en-US": "Solo Hang Gliding" }
      |    }
      |  }
      |}
    """.stripMargin).get


  "Tin Can Spec" should {

    import Version4._

    def readSchema(filename: String) =
      JsonSource.schemaFromStream(self.getClass.getResourceAsStream(s"/tincan/$filename.json")).get

    "validate basic statement object" in {

      val validator = SchemaValidator(Some(Version4))
        .addSchema("#agent", readSchema("agent"))
        .addSchema("#group", readSchema("group"))
        .addSchema("#inversefunctional", readSchema("inversefunctional"))
        .addSchema("#mbox", readSchema("mbox"))
        .addSchema("#statement_base", readSchema("statement_base"))
        .addSchema("#statement_object", readSchema("statement_object"))
        .addSchema("#verb", readSchema("verb"))
        .addSchema("#languagemap", readSchema("languagemap"))
        .addSchema("#activity", readSchema("activity"))
        .addSchema("#activity_definition", readSchema("activity_definition"))
        .addSchema("#activityid", readSchema("activityid"))

      val result = validator.validate(readSchema("statement_base"), instance)
      result.isSuccess must beTrue
    }
  }
} 
Example 188
Source File: HeaderSpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5

import org.scalatest.{Matchers, FunSpec}
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, Json, JsValue}

class HeaderSpec extends FunSpec with Matchers {
  val headerJson: JsValue = Json.parse("""
  {
    "msg_id": "<UUID>",
    "username": "<STRING>",
    "session": "<UUID>",
    "msg_type": "<STRING>",
    "version": "<FLOAT>"
  }
  """)

  val header: Header = Header(
    "<UUID>", "<STRING>", "<UUID>", "<STRING>", "<FLOAT>"
  )

  describe("Header") {
    describe("when null") {
      it("should implicitly convert to empty json") {
        val header: Header = null
        Json.toJson(header).toString() should be ("{}")
      }
    }

    describe("implicit conversions") {
      it("should implicitly convert from valid json to a header instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        headerJson.as[Header] should be (header)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newHeader = headerJson.asOpt[Header]

        newHeader.get should be (header)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val headerResults = headerJson.validate[Header]

        headerResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: Header) => valid
        ) should be (header)
      }

      it("should implicitly convert from a header instance to valid json") {
        Json.toJson(header) should be (headerJson)
      }
    }
  }
} 
Example 189
Source File: ConnectReplySpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.content

import org.scalatest.{FunSpec, Matchers}
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, JsValue, Json}

class ConnectReplySpec extends FunSpec with Matchers {


  val connectReplyJson: JsValue = Json.parse("""
  {
    "shell_port": 11111,
    "iopub_port": 22222,
    "stdin_port": 33333,
    "hb_port": 44444
  }
  """)

  val connectReply: ConnectReply = ConnectReply(11111,22222,33333,44444)

  describe("ConnectReply") {
    describe("#toTypeString") {
      it("should return correct type") {
        ConnectReply.toTypeString should be ("connect_reply")
      }
    }

    describe("implicit conversions") {
      it("should implicitly convert from valid json to a ConnectReply instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        connectReplyJson.as[ConnectReply] should be (connectReply)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newConnectReply = connectReplyJson.asOpt[ConnectReply]

        newConnectReply.get should be (connectReply)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val ConnectReplyResults = connectReplyJson.validate[ConnectReply]

        ConnectReplyResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: ConnectReply) => valid
        ) should be (connectReply)
      }

      it("should implicitly convert from a ConnectReply instance to valid json") {
        Json.toJson(connectReply) should be (connectReplyJson)
      }
    }
  }

} 
Example 190
Source File: CompleteReplySpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.content

import org.scalatest.{Matchers, FunSpec}
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, JsValue, Json}

class CompleteReplySpec extends FunSpec with Matchers {


  val completeReplyJson: JsValue = Json.parse("""
  {
    "matches": [],
    "cursor_start": 1,
    "cursor_end": 5,
    "metadata": {},
    "status": "<STRING>"
  }
  """)

  val completeReply: CompleteReply = CompleteReply(
    List(), 1, 5, Map(), "<STRING>", None, None, None
  )

  describe("CompleteReply") {
    describe("#toTypeString") {
      it("should return correct type") {
        CompleteReply.toTypeString should be ("complete_reply")
      }
    }

    describe("implicit conversions") {
      it("should implicitly convert from valid json to a CompleteReply instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        completeReplyJson.as[CompleteReply] should be (completeReply)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newCompleteReply = completeReplyJson.asOpt[CompleteReply]

        newCompleteReply.get should be (completeReply)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val CompleteReplyResults = completeReplyJson.validate[CompleteReply]

        CompleteReplyResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: CompleteReply) => valid
        ) should be (completeReply)
      }

      it("should implicitly convert from a CompleteReply instance to valid json") {
        Json.toJson(completeReply) should be (completeReplyJson)
      }
    }
  }

} 
Example 191
Source File: KernelStatusSpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.content

import org.scalatest.{FunSpec, Matchers}
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, JsValue, Json}

class KernelStatusSpec extends FunSpec with Matchers {
  val kernelStatusJson: JsValue = Json.parse("""
  {
    "execution_state": "<STRING>"
  }
  """)

  val kernelStatus: KernelStatus = KernelStatus("<STRING>")

  describe("KernelStatus") {
    describe("#toTypeString") {
      it("should return correct type") {
        KernelStatus.toTypeString should be ("status")
      }
    }

    describe("implicit conversions") {
      it("should implicitly convert from valid json to a kernelStatus instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        kernelStatusJson.as[KernelStatus] should be (kernelStatus)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newKernelStatus = kernelStatusJson.asOpt[KernelStatus]

        newKernelStatus.get should be (kernelStatus)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val kernelStatusResults = kernelStatusJson.validate[KernelStatus]

        kernelStatusResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: KernelStatus) => valid
        ) should be (kernelStatus)
      }

      it("should implicitly convert from a kernelStatus instance to valid json") {
        Json.toJson(kernelStatus) should be (kernelStatusJson)
      }
    }
  }
} 
Example 192
Source File: package.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol

//import akka.zeromq.ZMQMessage
import org.apache.toree.kernel.protocol.v5._
import org.apache.toree.kernel.protocol.v5.content.{CompleteRequest, ExecuteRequest}
import play.api.libs.json.{JsValue, Json}

package object v5Test {
  //  The header for the message
  val MockHeader : Header = Header("<UUID>","<USER>","<SESSION>",
    MessageType.Outgoing.ClearOutput.toString, "<VERSION>")
  //  The parent header for the message
  val MockParenHeader: Header = Header("<PARENT-UUID>","<PARENT-USER>","<PARENT-SESSION>",
    MessageType.Outgoing.ClearOutput.toString, "<PARENT-VERSION>")
  //  The actual kernel message
  val MockKernelMessage : KernelMessage = KernelMessage(Seq("<ID>".getBytes), "<SIGNATURE>", MockHeader,
    MockParenHeader, Metadata(), "<CONTENT>")
  //  Use the implicit to convert the KernelMessage to ZMQMessage
  //val MockZMQMessage : ZMQMessage = MockKernelMessage

  val MockExecuteRequest: ExecuteRequest =
    ExecuteRequest("spark code", false, true, Map(), false)
  val MockExecuteRequestKernelMessage = MockKernelMessage.copy(
    contentString =  Json.toJson(MockExecuteRequest).toString
  )
  val MockKernelMessageWithBadExecuteRequest = new KernelMessage(
    Seq[Array[Byte]](), "test message", MockHeader, MockParenHeader, Metadata(),
    """
        {"code" : 124 }
    """
  )
  val MockCompleteRequest: CompleteRequest = CompleteRequest("", 0)
  val MockCompleteRequestKernelMessage: KernelMessage = MockKernelMessage.copy(contentString = Json.toJson(MockCompleteRequest).toString)
  val MockKernelMessageWithBadJSON: KernelMessage = MockKernelMessage.copy(contentString = "inval1d")
} 
Example 193
Source File: SocketConfigSpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.kernel.protocol.v5.kernel.socket

import com.typesafe.config.ConfigFactory
import org.scalatest.{FunSpec, Matchers}
import org.slf4j.LoggerFactory
import play.api.data.validation.ValidationError
import play.api.libs.json.{JsPath, JsValue, Json}

class SocketConfigSpec extends FunSpec with Matchers {
  val logger = LoggerFactory.getLogger("jt4")
  //logger.error("WOOT!")

  private val jsonString: String =
    """
    {
      "stdin_port": 10000,
      "control_port": 10001,
      "hb_port": 10002,
      "shell_port": 10003,
      "iopub_port": 10004,
      "ip": "1.2.3.4",
      "transport": "tcp",
      "signature_scheme": "hmac-sha256",
      "key": ""
    }
    """.stripMargin

  val socketConfigJson: JsValue = Json.parse(jsonString)

  val socketConfigFromConfig = SocketConfig.fromConfig(ConfigFactory.parseString(jsonString))

  val socketConfig = SocketConfig(
    10000, 10001, 10002, 10003, 10004, "1.2.3.4", "tcp", "hmac-sha256", ""
  )

  describe("SocketConfig") {
    describe("implicit conversions") {
      it("should implicitly convert from valid json to a SocketConfig instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        socketConfigJson.as[SocketConfig] should be (socketConfig)
      }

      it("should also work with asOpt") {
        // This is safer, but we lose the error information as it returns
        // None if the conversion fails
        val newCompleteRequest = socketConfigJson.asOpt[SocketConfig]

        newCompleteRequest.get should be (socketConfig)
      }

      it("should also work with validate") {
        // This is the safest as it collects all error information (not just first error) and reports it
        val CompleteRequestResults = socketConfigJson.validate[SocketConfig]

        CompleteRequestResults.fold(
          (invalid: Seq[(JsPath, Seq[ValidationError])]) => println("Failed!"),
          (valid: SocketConfig) => valid
        ) should be (socketConfig)
      }

      it("should implicitly convert from a SocketConfig instance to valid json") {
        Json.toJson(socketConfig) should be (socketConfigJson)
      }
    }
    describe("#toConfig") {
      it("should implicitly convert from valid json to a SocketConfig instance") {
        // This is the least safe way to convert as an error is thrown if it fails
        socketConfigFromConfig should be (socketConfig)
      }
      
      it("should convert json file to SocketConfig object") {
        socketConfigFromConfig.stdin_port should be (10000)
      }
    }
  }
} 
Example 194
Source File: Codecs.scala    From kafka-journal   with MIT License 5 votes vote down vote up
package com.evolutiongaming.kafka.journal.circe

import cats.implicits._
import com.evolutiongaming.kafka.journal.PayloadAndType._
import com.evolutiongaming.kafka.journal.PayloadType.TextOrJson
import com.evolutiongaming.kafka.journal._
import io.circe._
import io.circe.generic.semiauto._
import play.api.libs.json.JsValue

import scala.concurrent.duration._

object Codecs {

  implicit val finiteDurationEncoder: Encoder[FiniteDuration] = Encoder.encodeString.contramap(_.toString)
  implicit val finiteDurationDecoder: Decoder[FiniteDuration] = Decoder.decodeString.emap { str =>
    Either.catchNonFatal(Duration(str).asInstanceOf[FiniteDuration])
      .leftMap(_ => s"cannot parse FiniteDuration from $str")
  }

  implicit val expireAfterEncoder: Encoder[ExpireAfter] = finiteDurationEncoder.contramap(_.value)
  implicit val expireAfterDecoder: Decoder[ExpireAfter] = finiteDurationDecoder.map(ExpireAfter(_))

  implicit val jsValueEncoder: Encoder[JsValue] = Encoder.instance(convertPlayToCirce)
  implicit val jsValueDecoder: Decoder[JsValue] = Decoder.decodeJson.emap(convertCirceToPlay)

  implicit val payloadMetadataCodec: Codec[PayloadMetadata] = deriveCodec

  implicit val seqNrEncoder: Encoder[SeqNr] = Encoder.encodeLong.contramap(_.value)
  implicit val seqNrDecoder: Decoder[SeqNr] = Decoder.decodeLong.emap(SeqNr.of[Either[String, *]](_))

  implicit val payloadTypeEncoder: Encoder[PayloadType.TextOrJson] = Encoder.encodeString.contramap(_.name)
  implicit val payloadTypeDecoder: Decoder[PayloadType.TextOrJson] = Decoder.decodeString.emap { str =>
    PayloadType(str)
      .flatMap {
        case v: TextOrJson => v.some
        case _             => none
      }
      .toRight(s"No PayloadType.TextOrJson found by $str")
  }

  implicit def eventJsonEncoder[A : Encoder]: Encoder[EventJson[A]] = deriveEncoder
  implicit def eventJsonDecoder[A : Decoder]: Decoder[EventJson[A]] = deriveDecoder

  implicit def payloadJsonEncoder[A : Encoder]: Encoder[PayloadJson[A]] = deriveEncoder
  implicit def payloadJsonDecoder[A : Decoder]: Decoder[PayloadJson[A]] = deriveDecoder

} 
Example 195
Source File: JsonSerializationFailed.scala    From lagom   with Apache License 2.0 5 votes vote down vote up
package com.lightbend.lagom.scaladsl.playjson

import scala.collection.Seq

import play.api.libs.json.JsPath
import play.api.libs.json.JsValue
import play.api.libs.json.Json
import play.api.libs.json.JsonValidationError

class JsonSerializationFailed private[lagom] (
    message: String,
    errors: Seq[(JsPath, Seq[JsonValidationError])],
    json: JsValue
) extends RuntimeException {
  override def getMessage: String =
    s"$message\nerrors:\n${errors.map(errorToString).mkString("\t", "\n\t", "\n")}}\n${Json.prettyPrint(json)}"

  private def errorToString(t: (JsPath, Seq[JsonValidationError])) = t match {
    case (path, pathErrors) => s"$path: " + pathErrors.mkString(", ")
  }
} 
Example 196
Source File: SelfAssessmentResource.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.resources

import javax.inject.Inject
import play.api.libs.json.JsValue
import play.api.mvc.{Action, AnyContent, ControllerComponents}
import router.services.SelfAssessmentService
import uk.gov.hmrc.auth.core.AuthConnector

import scala.concurrent.ExecutionContext

class SelfAssessmentResource @Inject()(service: SelfAssessmentService,
                                       val cc: ControllerComponents,
                                       val authConnector: AuthConnector)
                                      (implicit ec: ExecutionContext) extends BaseResource(cc, authConnector) {

  def get(route: Any*): Action[AnyContent] = AuthAction.async {
    implicit request =>
      service.get().map{
        case Left(error) => buildErrorResponse(error)
        case Right(apiResponse) => buildResponse(apiResponse)
      }
  }

  def post(route: Any*): Action[JsValue] = AuthAction.async(parse.json) {
    implicit request =>
      withJsonBody[JsValue]{
        service.post(_).map {
          case Left(error) => buildErrorResponse(error)
          case Right(apiResponse) => buildResponse(apiResponse)
        }
      }
  }

  def put(route: Any*): Action[JsValue] = AuthAction.async(parse.json) {
    implicit request =>
      withJsonBody[JsValue]{
        service.put(_).map {
          case Left(error) => buildErrorResponse(error)
          case Right(apiResponse) => buildResponse(apiResponse)
        }
      }
  }
} 
Example 197
Source File: CrystallisationResource.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.resources

import javax.inject.Inject
import play.api.libs.json.{JsNull, JsValue}
import play.api.mvc.{Action, AnyContent, BodyParser, ControllerComponents}
import router.constants.Versions
import router.constants.Versions._
import router.services.{CrystallisationService, Service}
import uk.gov.hmrc.auth.core.AuthConnector
import scala.concurrent.ExecutionContext.Implicits.global

class CrystallisationResource @Inject()(service: CrystallisationService,
                                        val cc: ControllerComponents,
                                        val authConnector: AuthConnector) extends BaseResource(cc, authConnector) with Service {

  def post(param: Any*): Action[JsValue] = AuthAction.async(parse.json) {
    implicit request =>
      withJsonBody[JsValue] {
        service.post(_).map {
          case Left(error) => buildErrorResponse(error)
          case Right(apiResponse) => buildResponse(apiResponse)
        }
      }
  }

  // Note that intent V1 requires empty JSON (i.e. {}) whereas V2 requires completely empty body. So we need to parse
  // accordingly these with the empty body parsed to JsNull
  private val jsonOrEmptyParser: BodyParser[JsValue] = parse.using { request =>

    if (Versions.getFromRequest(request).contains(VERSION_1))
      parse.json
    else
      parse.empty.map(_ => JsNull)
  }

  def intent(param: Any*): Action[JsValue] = AuthAction.async(jsonOrEmptyParser) { implicit request =>
    withJsonBody[JsValue] { body =>
      val serviceOutcome = body match {
        case JsNull => service.postEmpty
        case json => service.post(json)
      }

      serviceOutcome.map {
        case Left(error) => buildErrorResponse(error)
        case Right(apiResponse) => buildResponse(apiResponse)
      }
    }
  }

  def get(param: Any*): Action[AnyContent] = {
    AuthAction.async {
      implicit request =>
        service.get().map{
          case Left(error) => buildErrorResponse(error)
          case Right(apiResponse) => buildResponse(apiResponse)
        }
    }
  }
} 
Example 198
Source File: SelfEmploymentEopsDeclarationResource.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.resources

import javax.inject.Inject
import play.api.libs.json.JsValue
import play.api.mvc.{Action, ControllerComponents}
import router.services.SelfEmploymentEopsDeclarationService
import uk.gov.hmrc.auth.core.AuthConnector

import scala.concurrent.ExecutionContext

class SelfEmploymentEopsDeclarationResource @Inject()(service: SelfEmploymentEopsDeclarationService,
                                                      val cc: ControllerComponents,
                                                      val authConnector: AuthConnector)
                                                     (implicit ec: ExecutionContext) extends BaseResource(cc, authConnector) {

  def post(seId: String, nino: String, from: String, to: String): Action[JsValue] = AuthAction.async(parse.json) {
    implicit request =>
      withJsonBody[JsValue] {
        service.post(_).map {
          case Left(error) => buildErrorResponse(error)
          case Right(apiResponse) => buildResponse(apiResponse)
        }
      }
  }
} 
Example 199
Source File: PropertyEopsDeclarationResource.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.resources

import javax.inject.Inject
import play.api.libs.json.JsValue
import play.api.mvc.{Action, ControllerComponents}
import router.services.PropertyEopsDeclarationService
import uk.gov.hmrc.auth.core.AuthConnector

import scala.concurrent.ExecutionContext

class PropertyEopsDeclarationResource @Inject()(service: PropertyEopsDeclarationService,
                                                val cc: ControllerComponents,
                                                val authConnector: AuthConnector)
                                               (implicit ec: ExecutionContext) extends BaseResource(cc, authConnector) {

  def post(nino: String, from: String, to: String): Action[JsValue] = AuthAction.async(parse.json) {
    implicit request =>
      withJsonBody[JsValue]{
        service.post(_).map {
          case Left(error) => buildErrorResponse(error)
          case Right(apiResponse) => buildResponse(apiResponse)
        }
      }

  }
} 
Example 200
Source File: CharitableGivingResource.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
package router.resources

import javax.inject.Inject
import play.api.libs.json.JsValue
import play.api.mvc.{Action, AnyContent, ControllerComponents}
import router.services.CharitableGivingService
import uk.gov.hmrc.auth.core.AuthConnector

import scala.concurrent.ExecutionContext

class CharitableGivingResource @Inject()(service: CharitableGivingService,
                                         val cc: ControllerComponents,
                                         val authConnector: AuthConnector)
                                        (implicit ec: ExecutionContext) extends BaseResource(cc, authConnector) {


  def put(param:Any*): Action[JsValue] = AuthAction.async(parse.json) {
    implicit request =>
      withJsonBody[JsValue]{
        service.put(_).map {
          case Left(error) => buildErrorResponse(error)
          case Right(apiResponse) => buildResponse(apiResponse)
        }
      }
  }

  def get(param:Any*): Action[AnyContent] =
    AuthAction.async {
      implicit request =>
        service.get().map{
          case Left(error) => buildErrorResponse(error)
          case Right(apiResponse) => buildResponse(apiResponse)
        }
    }
}