play.api.libs.json.JsString Scala Examples

The following examples show how to use play.api.libs.json.JsString. 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: ItemAdded.scala    From lagom   with Apache License 2.0 5 votes vote down vote up
package docs.home.scaladsl.serialization.v2c

import com.lightbend.lagom.scaladsl.playjson.JsonMigration
import com.lightbend.lagom.scaladsl.playjson.JsonMigrations
import com.lightbend.lagom.scaladsl.playjson.JsonSerializerRegistry
import play.api.libs.json.JsObject
import play.api.libs.json.JsPath
import play.api.libs.json.JsString

import scala.collection.immutable

//#rename
case class ItemAdded(shoppingCartId: String, itemId: String, quantity: Int)
//#rename

object ItemAddedMigration {
  class ShopSerializerRegistry1 extends JsonSerializerRegistry {
    override def serializers = Vector.empty

    //#imperative-migration
    private val itemAddedMigration = new JsonMigration(2) {
      override def transform(fromVersion: Int, json: JsObject): JsObject = {
        if (fromVersion < 2) {
          val productId = (JsPath \ "productId").read[JsString].reads(json).get
          json + ("itemId" -> productId) - "productId"
        } else {
          json
        }
      }
    }

    override def migrations = Map[String, JsonMigration](
      classOf[ItemAdded].getName -> itemAddedMigration
    )
    //#imperative-migration
  }

  class ShopSerializerRegistry2 extends JsonSerializerRegistry {
    override val serializers = Vector.empty

    //#transformer-migration
    val productIdToItemId =
      JsPath.json
        .update(
          (JsPath \ "itemId").json.copyFrom((JsPath \ "productId").json.pick)
        )
        .andThen((JsPath \ "productId").json.prune)

    override def migrations = Map[String, JsonMigration](
      JsonMigrations.transform[ItemAdded](
        immutable.SortedMap(
          1 -> productIdToItemId
        )
      )
    )
    //#transformer-migration
  }
} 
Example 2
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 3
Source File: JsonOpsSpec.scala    From reactive-nakadi   with MIT License 5 votes vote down vote up
package org.zalando.react.nakadi.client.models

import org.scalatest.{Matchers, WordSpec}
import play.api.libs.json.{JsObject, JsString, Json}


class JsonOpsSpec extends WordSpec with Matchers {

  import JsonOps._


  val businessEvent =
    """
      |{
      |  "metadata": {
      |    "eid": "7b7c7100-0559-11e6-a837-0800200c9a66",
      |    "occurred_at": "2016-04-01T13:42:16Z",
      |    "event_type": "order-created"
      |  },
      |  "id": "504c91de-17c9-46d8-81ee-55135084d696"
      |}
    """.stripMargin

  val dataChangeEvent =
    """
      |{
      |  "data_type": "order",
      |  "data_op": "C",
      |  "data": {"some": "payload"},
      |  "metadata": {
      |    "eid": "7b7c7100-0559-11e6-a837-0800200c9a66",
      |    "occurred_at": "2016-04-01T13:42:16Z",
      |    "event_type": "order-created"
      |  }
      |}
    """.stripMargin

  "Event JsonOps" must {
    "correctly determine business event" in {

      val json = Json.parse(businessEvent)
      val validated = json.asOpt[Event]
      validated match {
        case Some(BusinessEvent(_, payload)) =>
          payload shouldEqual JsObject(Seq("id" -> JsString("504c91de-17c9-46d8-81ee-55135084d696")))
        case _ =>
          fail("expected correctly parsed BusinessEvent")
      }
    }

    "correctly determine data change event" in {
      val json = Json.parse(dataChangeEvent)
      val validated = json.asOpt[Event]
      validated match {
        case Some(d :DataChangeEvent) =>
          d.data shouldEqual JsObject(Seq("some" -> JsString("payload")))
        case _ =>
          fail("expected correctly parsed DataChangeEvent")
      }
    }

    "successfully serialize and deserialize the same events" in {
      val dataEvent = Json.parse(dataChangeEvent).as[DataChangeEvent]
      val dataJson = Json.toJson(dataEvent)
      dataJson shouldEqual Json.parse(dataChangeEvent)

      val businessEvt = Json.parse(businessEvent).as[BusinessEvent]
      val businessJson = Json.toJson(businessEvt)
      businessJson shouldEqual Json.parse(businessEvent)
    }

  }

} 
Example 4
Source File: AboutController.scala    From cluster-broccoli   with Apache License 2.0 5 votes vote down vote up
package de.frosner.broccoli.controllers

import javax.inject.Inject

import de.frosner.broccoli.auth.Account
import de.frosner.broccoli.services._
import de.frosner.broccoli.conf
import de.frosner.broccoli.models.AboutInfo.aboutInfoWrites
import jp.t2v.lab.play2.auth.BroccoliSimpleAuthorization
import play.api.Environment
import play.api.cache.CacheApi
import play.api.libs.json.{JsBoolean, JsObject, JsString, Json}
import play.api.mvc.{Action, AnyContent, Controller}

case class AboutController @Inject()(
    aboutInfoService: AboutInfoService,
    override val securityService: SecurityService,
    override val cacheApi: CacheApi,
    override val playEnv: Environment
) extends Controller
    with BroccoliSimpleAuthorization {

  def about = StackAction(parse.empty) { implicit request =>
    Ok(Json.toJson(AboutController.about(aboutInfoService, loggedIn)))
  }

}

object AboutController {

  def about(aboutInfoService: AboutInfoService, loggedIn: Account) =
    aboutInfoService.aboutInfo(loggedIn)

} 
Example 5
Source File: AuthConfigImpl.scala    From cluster-broccoli   with Apache License 2.0 5 votes vote down vote up
package de.frosner.broccoli.controllers

import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import de.frosner.broccoli.auth.{Account, Role}
import de.frosner.broccoli.services.SecurityService
import jp.t2v.lab.play2.auth._
import play.api.{Environment, Mode}
import play.api.cache.CacheApi
import play.api.libs.json.JsString
import play.api.mvc.{RequestHeader, Result, Results}

import scala.concurrent.{ExecutionContext, Future}
import scala.reflect.ClassTag

trait AuthConfigImpl extends AuthConfig {

  val securityService: SecurityService

  val playEnv: Environment

  val cacheApi: CacheApi

  type Id = String

  type User = Account

  type Authority = Role

  val idTag: ClassTag[Id] = scala.reflect.classTag[Id]

  val sessionTimeoutInSeconds = securityService.sessionTimeoutInSeconds

  val cookieSecure = securityService.cookieSecure

  override lazy val idContainer: AsyncIdContainer[Id] = securityService.allowMultiLogin match {
    case true  => AsyncIdContainer(new MultiLoginCacheIdContainer[Id](cacheApi))
    case false => AsyncIdContainer(new CacheIdContainer[Id])
  }

  def resolveUser(id: Id)(implicit ctx: ExecutionContext): Future[Option[User]] =
    securityService.identityService.retrieve(LoginInfo(CredentialsProvider.ID, id))

  def loginSucceeded(request: RequestHeader)(implicit ctx: ExecutionContext): Future[Result] =
    Future.successful(Results.Ok(JsString("Login successful!"))) // the content is not used anyway as the controller replaces it

  def logoutSucceeded(request: RequestHeader)(implicit ctx: ExecutionContext): Future[Result] =
    Future.successful(Results.Ok(JsString("Logout successful!")))

  def authenticationFailed(request: RequestHeader)(implicit ctx: ExecutionContext): Future[Result] =
    Future.successful(Results.Forbidden("Authentication failed."))

  override def authorizationFailed(request: RequestHeader, user: User, authority: Option[Authority])(
      implicit context: ExecutionContext): Future[Result] =
    Future.successful(Results.Forbidden(
      s"Authorization failed: Your privileges (${user.role}) are not matching the required (${authority.getOrElse("None")})."))

  def authorize(user: User, authority: Authority)(implicit ctx: ExecutionContext): Future[Boolean] = Future.successful {
    user.role match {
      case Role.Administrator => true
      case Role.Operator      => authority == Role.Operator || authority == Role.User
      case Role.User          => authority == Role.User
    }
  }

  override lazy val tokenAccessor = new CookieTokenAccessor(
    cookieName = AuthConfigImpl.CookieName,
    cookieSecureOption = playEnv.mode == Mode.Prod && cookieSecure,
    cookieHttpOnlyOption = true,
    cookieDomainOption = None,
    cookiePathOption = "/",
    cookieMaxAge = Some(sessionTimeoutInSeconds)
  )

}

object AuthConfigImpl {

  val CookieName = "BROCCOLI_SESS_ID"

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

import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.{JsString, Json}


class HttpResponseSpec extends AnyWordSpec with Matchers {
  "unapply" should {
    "return matching object in" in {
      HttpResponse(status = 1, body = "test body", headers = Map("a" -> List("1", "2", "3"))) match {
        case HttpResponse(status, body, headers) =>
          status  shouldBe 1
          body    shouldBe "test body"
          headers shouldBe Map("a" -> List("1", "2", "3"))
      }
    }
  }
} 
Example 7
Source File: RestFormats.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.http.controllers

import org.joda.time.format.ISODateTimeFormat
import play.api.libs.json._
import play.api.libs.json.JsString
import org.joda.time.{DateTime, DateTimeZone, LocalDate, LocalDateTime}
import scala.util.Try

object RestFormats extends RestFormats

trait RestFormats {

  private val dateTimeFormat = ISODateTimeFormat.dateTime.withZoneUTC
  private val localDateRegex = """^(\d\d\d\d)-(\d\d)-(\d\d)$""".r

  implicit val localDateTimeRead: Reads[LocalDateTime] = new Reads[LocalDateTime] {
    override def reads(json: JsValue): JsResult[LocalDateTime] =
      json match {
        case JsString(s) =>
          Try {
            JsSuccess(new LocalDateTime(dateTimeFormat.parseDateTime(s), DateTimeZone.UTC))
          }.getOrElse {
            JsError(s"Could not parse $s as a DateTime with format ${dateTimeFormat.toString}")
          }
        case _ => JsError(s"Expected value to be a string, was actually $json")
      }
  }

  implicit val localDateTimeWrite: Writes[LocalDateTime] = new Writes[LocalDateTime] {
    def writes(dateTime: LocalDateTime): JsValue = JsString(dateTimeFormat.print(dateTime.toDateTime(DateTimeZone.UTC)))
  }

  implicit val dateTimeRead: Reads[DateTime] = new Reads[DateTime] {
    override def reads(json: JsValue): JsResult[DateTime] =
      json match {
        case JsString(s) =>
          Try {
            JsSuccess(dateTimeFormat.parseDateTime(s))
          }.getOrElse {
            JsError(s"Could not parse $s as a DateTime with format ${dateTimeFormat.toString}")
          }
        case _ => JsError(s"Expected value to be a string, was actually $json")
      }
  }

  implicit val dateTimeWrite: Writes[DateTime] = new Writes[DateTime] {
    def writes(dateTime: DateTime): JsValue = JsString(dateTimeFormat.print(dateTime))
  }

  implicit val localDateRead: Reads[LocalDate] = new Reads[LocalDate] {
    override def reads(json: JsValue): JsResult[LocalDate] =
      json match {
        case JsString(s @ localDateRegex(y, m, d)) =>
          Try {
            JsSuccess(new LocalDate(y.toInt, m.toInt, d.toInt))
          }.getOrElse {
            JsError(s"$s is not a valid date")
          }
        case JsString(s) => JsError(s"Cannot parse $s as a LocalDate")
        case _           => JsError(s"Expected value to be a string, was actually $json")
      }
  }

  implicit val localDateWrite: Writes[LocalDate] = new Writes[LocalDate] {
    def writes(date: LocalDate): JsValue =
      JsString("%04d-%02d-%02d".format(date.getYear, date.getMonthOfYear, date.getDayOfMonth))
  }

  implicit val dateTimeFormats      = Format(dateTimeRead, dateTimeWrite)
  implicit val localDateTimeFormats = Format(localDateTimeRead, localDateTimeWrite)
  implicit val localDateFormats     = Format(localDateRead, localDateWrite)

} 
Example 8
Source File: NonBlankStringSpec.scala    From play-ui   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.binders

import org.scalatest.{Matchers, WordSpecLike}
import play.api.libs.json.{JsError, JsString, JsSuccess, Json}

class NonBlankStringSpec extends WordSpecLike with Matchers {

  "Creating a NonBlankString" should {
    "throw an exception for a blank string" in {
      an[IllegalArgumentException] should be thrownBy NonBlankString("")
    }

    "give an error for a null string" in {
      an[IllegalArgumentException] should be thrownBy NonBlankString(null)
    }

    "give a success for a non-blank string" in {
      NonBlankString("x")
    }
  }

  "Reading a NonBlankString" should {
    "give an error for a blank string" in {
      validating("") shouldBe a[JsError]
    }

    "give an error for a null string" in {
      validating(null) shouldBe a[JsError]
    }

    "give a success for a non-blank string" in {
      validating("x") should be(JsSuccess(NonBlankString("x")))
    }

    def validating(s: String) = JsString(s).validate[NonBlankString]
  }

  "Writing a NonBlankString" should {
    "just include the value" in {
      Json.toJson(NonBlankString("x")) should be(JsString("x"))
    }
  }

  "Binding from a query string" should {
    "give an error for a blank string" in {
      binding("") shouldBe Some(Left("String was blank"))
    }

    "give an error for a null string" in {
      binding(null) shouldBe Some(Left("String was blank"))
    }

    "give a success for a non-blank string" in {
      binding("x") shouldBe Some(Right(NonBlankString("x")))
    }
    def binding(s: String) = NonBlankString.stringToNonBlankString.bind("v", Map("v" -> Seq(s)))
  }

  "Unbinding to a query string" should {
    "extract the value" in {
      unbinding(NonBlankString("something")) shouldBe "something"
    }
    def unbinding(n: NonBlankString) = NonBlankString.stringToNonBlankString.unbind("v", n)
  }

} 
Example 9
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 10
Source File: AuditSerialiserSpec.scala    From play-auditing   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.audit.serialiser

import java.time.Instant

import play.api.libs.json.JsString
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
import uk.gov.hmrc.play.audit.model.{DataCall, DataEvent, ExtendedDataEvent, MergedDataEvent}

class AuditSerialiserSpec extends AnyWordSpecLike with Matchers {

  "AuditSerialiser" should {
    "serialise DataEvent" in {
      AuditSerialiser.serialise(DataEvent(
        auditSource = "myapp",
        auditType   = "RequestReceived",
        eventId     = "cb5ebe82-cf3c-4f15-bd92-39a6baa1f929",
        tags        = Map("tagkey" -> "tagval"),
        detail      = Map("detailkey" -> "detailval"),
        generatedAt = Instant.parse("2007-12-03T10:15:30.123Z")
      )).toString shouldBe """{"auditSource":"myapp","auditType":"RequestReceived","eventId":"cb5ebe82-cf3c-4f15-bd92-39a6baa1f929","tags":{"tagkey":"tagval"},"detail":{"detailkey":"detailval"},"generatedAt":"2007-12-03T10:15:30.123+0000"}"""
    }

    "serialise ExtendedDataEvent" in {
      AuditSerialiser.serialise(ExtendedDataEvent(
        auditSource = "myapp",
        auditType   = "RequestReceived",
        eventId     = "cb5ebe82-cf3c-4f15-bd92-39a6baa1f929",
        tags        = Map("tagkey" -> "tagval"),
        detail      = JsString("detail"),
        generatedAt = Instant.parse("2007-12-03T10:15:30.123Z")
      )).toString shouldBe """{"auditSource":"myapp","auditType":"RequestReceived","eventId":"cb5ebe82-cf3c-4f15-bd92-39a6baa1f929","tags":{"tagkey":"tagval"},"detail":"detail","generatedAt":"2007-12-03T10:15:30.123+0000"}"""
    }

    "serialise MergedDataEvent" in {
      AuditSerialiser.serialise(MergedDataEvent(
        auditSource = "myapp",
        auditType   = "RequestReceived",
        eventId     = "cb5ebe82-cf3c-4f15-bd92-39a6baa1f929",
        request     = DataCall(
                        tags   = Map("requesttagkey" -> "requesttagval"),
                        detail = Map("requestdetailkey" -> "requestdetailval"),
                        generatedAt = Instant.parse("2007-12-03T10:15:30.123Z")
                      ),
        response    = DataCall(
                        tags   = Map("responsetagkey" -> "responsetagval"),
                        detail = Map("responsedetailkey" -> "responsedetailval"),
                        generatedAt = Instant.parse("2007-12-03T10:16:31.123Z")
                      )
      )).toString shouldBe """{"auditSource":"myapp","auditType":"RequestReceived","eventId":"cb5ebe82-cf3c-4f15-bd92-39a6baa1f929","request":{"tags":{"requesttagkey":"requesttagval"},"detail":{"requestdetailkey":"requestdetailval"},"generatedAt":"2007-12-03T10:15:30.123+0000"},"response":{"tags":{"responsetagkey":"responsetagval"},"detail":{"responsedetailkey":"responsedetailval"},"generatedAt":"2007-12-03T10:16:31.123+0000"}}"""
    }
  }
} 
Example 11
Source File: EventSerializerSpec.scala    From kafka-journal   with MIT License 5 votes vote down vote up
package akka.persistence.kafka.journal

import java.io.FileOutputStream

import akka.persistence.PersistentRepr
import akka.persistence.serialization.Snapshot
import cats.effect.{IO, Sync}
import com.evolutiongaming.kafka.journal.FromBytes.implicits._
import com.evolutiongaming.kafka.journal.IOSuite._
import com.evolutiongaming.kafka.journal._
import com.evolutiongaming.kafka.journal.util.CatsHelper._
import org.scalatest.funsuite.AsyncFunSuite
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.JsString
import scodec.bits.ByteVector
import TestJsonCodec.instance
import cats.implicits._

import scala.util.Try

class EventSerializerSpec extends AsyncFunSuite with ActorSuite with Matchers {

  for {
    (name, payloadType, payload) <- List(
      ("PersistentRepr.bin",       PayloadType.Binary, Snapshot("binary")),
      ("PersistentRepr.text.json", PayloadType.Json, "text"),
      ("PersistentRepr.json",      PayloadType.Json, JsString("json")))
  } {

    test(s"toEvent & toPersistentRepr, payload: $payload") {
      val persistenceId = "persistenceId"
      val persistentRepr = PersistentRepr(
        payload = payload,
        sequenceNr = 1,
        persistenceId = persistenceId,
        manifest = "manifest",
        writerUuid = "writerUuid")

      val fa = for {
        serializer <- EventSerializer.of[IO](actorSystem)
        event      <- serializer.toEvent(persistentRepr)
        actual     <- serializer.toPersistentRepr(persistenceId, event)
        _          <- Sync[IO].delay { actual shouldEqual persistentRepr }
        payload    <- event.payload.getOrError[IO]("Event.payload is not defined")
        _           = payload.payloadType shouldEqual payloadType
        
        bytes      <- ByteVectorOf[IO](getClass, name)
      } yield {
        payload match {
          case payload: Payload.Binary => payload.value shouldEqual bytes
          case payload: Payload.Text   => payload.value shouldEqual bytes.fromBytes[Try, String].get
          case payload: Payload.Json   => payload.value shouldEqual JsonCodec.summon[Try].decode.fromBytes(bytes).get
        }
      }

      fa.run()
    }
  }

  def writeToFile[F[_] : Sync](bytes: ByteVector, path: String): F[Unit] = {
    Sync[F].delay {
      val os = new FileOutputStream(path)
      os.write(bytes.toArray)
      os.close()
    }
  }
} 
Example 12
Source File: PayloadTypeSpec.scala    From kafka-journal   with MIT License 5 votes vote down vote up
package com.evolutiongaming.kafka.journal

import cats.implicits._
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.JsString

class PayloadTypeSpec extends AnyFunSuite with Matchers {

  for {
    (ext, payloadType) <- List(
      ("json", PayloadType.Json),
      ("txt" , PayloadType.Text),
      ("bin" , PayloadType.Binary))
  } {
    test(s"$payloadType.ext") {
      payloadType.ext shouldEqual ext
    }
  }

  for {
    (json, expected) <- List(
      ("json"  , PayloadType.Json.some),
      ("text"  , PayloadType.Text.some),
      ("binary", PayloadType.Binary.some),
      ("none"  , none))
  } {
    test(s"reads & writes $json") {
      JsString(json).validate[PayloadType].asOpt shouldEqual expected
    }
  }

  for {
    (json, expected) <- List(
      ("json"  , PayloadType.Json.some),
      ("text"  , PayloadType.Text.some),
      ("binary", none))
  } {
    test(s"TextOrJson reads & writes $json") {
      JsString(json).validate[PayloadType.TextOrJson].asOpt shouldEqual expected
    }
  }

  for {
    (json, expected) <- List(
      ("json"  , PayloadType.Json.some),
      ("text"  , none),
      ("binary", PayloadType.Binary.some))
  } {
    test(s"BinaryOrJson reads & writes $json") {
      JsString(json).validate[PayloadType.BinaryOrJson].asOpt shouldEqual expected
    }
  }
} 
Example 13
Source File: PayloadSpec.scala    From kafka-journal   with MIT License 5 votes vote down vote up
package com.evolutiongaming.kafka.journal

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.JsString
import scodec.bits.ByteVector

class PayloadSpec extends AnyFunSuite with Matchers {

  test("apply text") {
    Payload("text") shouldEqual Payload.text("text")
  }

  test("apply binary") {
    Payload(ByteVector.empty) shouldEqual Payload.binary(ByteVector.empty)
  }

  test("apply json") {
    Payload(JsString("json")) shouldEqual Payload.json(JsString("json"))
  }
} 
Example 14
Source File: JsonCodecTest.scala    From kafka-journal   with MIT License 5 votes vote down vote up
package com.evolutiongaming.kafka.journal

import cats.implicits._
import org.scalatest.FunSuite
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.{JsString, Json}
import scodec.bits.ByteVector

import scala.util.{Failure, Try}

class JsonCodecTest extends FunSuite with Matchers {

  private val malformed = ByteVector.view(Json.toBytes(JsString("\ud83d\ude18\ud83d")))

  test("JsonCodec.jsoniter") {

    JsonCodec.jsoniter[Try].decode.fromBytes(malformed) should matchPattern {
      case Failure(_: JournalError) =>
    }
  }

  test("JsonCodec.playJson") {
    JsonCodec.playJson[Try].decode.fromBytes(malformed).isSuccess shouldEqual true
  }

  test("JsonCodec.default") {
    JsonCodec.default[Try].decode.fromBytes(malformed).isSuccess shouldEqual true
  }
} 
Example 15
Source File: P2PRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.common

import java.net.URI

import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.{
  AddNodeArgument,
  SetBanCommand
}
import org.bitcoins.commons.jsonmodels.bitcoind._
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.core.protocol.blockchain.Block
import play.api.libs.json.{JsBoolean, JsNumber, JsString}

import scala.concurrent.Future


trait P2PRpc { self: Client =>

  def addNode(address: URI, command: AddNodeArgument): Future[Unit] = {
    bitcoindCall[Unit](
      "addnode",
      List(JsString(address.getAuthority), JsString(command.toString)))
  }

  def clearBanned(): Future[Unit] = {
    bitcoindCall[Unit]("clearbanned")
  }

  def disconnectNode(address: URI): Future[Unit] = {
    bitcoindCall[Unit]("disconnectnode", List(JsString(address.getAuthority)))
  }

  def getAddedNodeInfo: Future[Vector[Node]] = getAddedNodeInfo(None)

  private def getAddedNodeInfo(node: Option[URI]): Future[Vector[Node]] = {
    val params =
      if (node.isEmpty) {
        List.empty
      } else {
        List(JsString(node.get.getAuthority))
      }
    bitcoindCall[Vector[Node]]("getaddednodeinfo", params)
  }

  def getAddedNodeInfo(node: URI): Future[Vector[Node]] =
    getAddedNodeInfo(Some(node))

  def getConnectionCount: Future[Int] = {
    bitcoindCall[Int]("getconnectioncount")
  }

  def getNetTotals: Future[GetNetTotalsResult] = {
    bitcoindCall[GetNetTotalsResult]("getnettotals")
  }

  def getNetworkInfo: Future[GetNetworkInfoResult] = {
    bitcoindCall[GetNetworkInfoResult]("getnetworkinfo")
  }

  def getPeerInfo: Future[Vector[Peer]] = {
    bitcoindCall[Vector[Peer]]("getpeerinfo")
  }

  def listBanned: Future[Vector[NodeBan]] = {
    bitcoindCall[Vector[NodeBan]]("listbanned")
  }

  def setBan(
      address: URI,
      command: SetBanCommand,
      banTime: Int = 86400,
      absolute: Boolean = false): Future[Unit] = {
    bitcoindCall[Unit]("setban",
                       List(JsString(address.getAuthority),
                            JsString(command.toString),
                            JsNumber(banTime),
                            JsBoolean(absolute)))
  }

  def setNetworkActive(activate: Boolean): Future[Unit] = {
    bitcoindCall[Unit]("setnetworkactive", List(JsBoolean(activate)))
  }

  def submitBlock(block: Block): Future[Unit] = {
    bitcoindCall[Unit]("submitblock", List(JsString(block.hex)))

  }
} 
Example 16
Source File: MultisigRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.common

import org.bitcoins.commons.jsonmodels.bitcoind.MultiSigResult
import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.AddressType
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.commons.serializers.JsonWriters._
import org.bitcoins.core.protocol.P2PKHAddress
import org.bitcoins.crypto.ECPublicKey
import play.api.libs.json.{JsArray, JsNumber, JsString, Json}

import scala.concurrent.Future


trait MultisigRpc { self: Client =>

  private def addMultiSigAddress(
      minSignatures: Int,
      keys: Vector[Either[ECPublicKey, P2PKHAddress]],
      account: String = "",
      addressType: Option[AddressType]): Future[MultiSigResult] = {
    def keyToString(key: Either[ECPublicKey, P2PKHAddress]): JsString =
      key match {
        case Right(k) => JsString(k.value)
        case Left(k)  => JsString(k.hex)
      }

    val params =
      List(JsNumber(minSignatures),
           JsArray(keys.map(keyToString)),
           JsString(account)) ++ addressType.map(Json.toJson(_)).toList

    bitcoindCall[MultiSigResult]("addmultisigaddress", params)
  }

  def addMultiSigAddress(
      minSignatures: Int,
      keys: Vector[Either[ECPublicKey, P2PKHAddress]]): Future[MultiSigResult] =
    addMultiSigAddress(minSignatures, keys, addressType = None)

  def addMultiSigAddress(
      minSignatures: Int,
      keys: Vector[Either[ECPublicKey, P2PKHAddress]],
      account: String): Future[MultiSigResult] =
    addMultiSigAddress(minSignatures, keys, account, None)

  def addMultiSigAddress(
      minSignatures: Int,
      keys: Vector[Either[ECPublicKey, P2PKHAddress]],
      addressType: AddressType): Future[MultiSigResult] =
    addMultiSigAddress(minSignatures, keys, addressType = Some(addressType))

  def addMultiSigAddress(
      minSignatures: Int,
      keys: Vector[Either[ECPublicKey, P2PKHAddress]],
      account: String,
      addressType: AddressType): Future[MultiSigResult] =
    addMultiSigAddress(minSignatures, keys, account, Some(addressType))

  def createMultiSig(
      minSignatures: Int,
      keys: Vector[ECPublicKey]): Future[MultiSigResult] = {
    bitcoindCall[MultiSigResult](
      "createmultisig",
      List(JsNumber(minSignatures), Json.toJson(keys.map(_.hex))))
  }

} 
Example 17
Source File: DescriptorRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.common

import org.bitcoins.commons.jsonmodels.bitcoind.{
  DeriveAddressesResult,
  GetDescriptorInfoResult
}
import org.bitcoins.commons.serializers.JsonSerializers._
import play.api.libs.json.{JsString, Json}

import scala.concurrent.Future


trait DescriptorRpc {
  self: Client =>

  def deriveAddresses(
      descriptor: String,
      range: Option[Vector[Double]]): Future[DeriveAddressesResult] = {
    val params =
      if (range.isDefined) List(JsString(descriptor), Json.toJson(range))
      else List(JsString(descriptor))
    bitcoindCall[DeriveAddressesResult]("deriveaddresses", params)
  }

  def getDescriptorInfo(descriptor: String): Future[GetDescriptorInfoResult] = {
    bitcoindCall[GetDescriptorInfoResult]("getdescriptorinfo",
                                          List(JsString(descriptor)))
  }
} 
Example 18
Source File: MiningRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.common

import org.bitcoins.commons.jsonmodels.bitcoind.{
  GetBlockTemplateResult,
  GetMiningInfoResult,
  RpcOpts
}
import org.bitcoins.commons.serializers.JsonReaders._
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.core.currency.Satoshis
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.crypto.{DoubleSha256Digest, DoubleSha256DigestBE}
import play.api.libs.json.{JsNumber, JsString, Json}

import scala.concurrent.Future


trait MiningRpc { self: Client =>

  @deprecated("use generateToAddress instead", since = "0.18.0")
  def generate(
      blocks: Int,
      maxTries: Int = 1000000): Future[Vector[DoubleSha256DigestBE]] = {
    bitcoindCall[Vector[DoubleSha256DigestBE]](
      "generate",
      List(JsNumber(blocks), JsNumber(maxTries)))
  }

  def generateToAddress(
      blocks: Int,
      address: BitcoinAddress,
      maxTries: Int = 1000000): Future[Vector[DoubleSha256DigestBE]] = {
    bitcoindCall[Vector[DoubleSha256DigestBE]](
      "generatetoaddress",
      List(JsNumber(blocks), JsString(address.toString), JsNumber(maxTries)))
  }

  def getBlockTemplate(request: Option[RpcOpts.BlockTemplateRequest] =
    None): Future[GetBlockTemplateResult] = {
    val params =
      if (request.isEmpty) {
        List.empty
      } else {
        List(Json.toJson(request.get))
      }
    bitcoindCall[GetBlockTemplateResult]("getblocktemplate", params)
  }

  def getNetworkHashPS(
      blocks: Int = 120,
      height: Int = -1): Future[BigDecimal] = {
    bitcoindCall[BigDecimal]("getnetworkhashps",
                             List(JsNumber(blocks), JsNumber(height)))
  }

  def getMiningInfo: Future[GetMiningInfoResult] = {
    bitcoindCall[GetMiningInfoResult]("getmininginfo")
  }

  def prioritiseTransaction(
      txid: DoubleSha256DigestBE,
      feeDelta: Satoshis): Future[Boolean] = {
    bitcoindCall[Boolean](
      "prioritisetransaction",
      List(JsString(txid.hex), JsNumber(0), JsNumber(feeDelta.toLong)))
  }

  def prioritiseTransaction(
      txid: DoubleSha256Digest,
      feeDelta: Satoshis): Future[Boolean] = {
    prioritiseTransaction(txid.flip, feeDelta)
  }
} 
Example 19
Source File: LoggingHandlerSpec.scala    From play-auditing   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.audit.handler

import org.mockito.Mockito._
import org.scalatest.wordspec.AnyWordSpecLike
import org.scalatestplus.mockito.MockitoSugar
import org.slf4j.Logger
import play.api.libs.json.JsString

import scala.concurrent.ExecutionContext.Implicits.global

class LoggingHandlerSpec extends AnyWordSpecLike with MockitoSugar {

  val mockLog: Logger = mock[Logger]
  val loggingHandler = new LoggingHandler(mockLog)

  "LoggingHandler" should {
    "log the event" in {
      val expectedLogContent = """DS_EventMissed_AuditRequestFailure : audit item : "FAILED_EVENT""""

      loggingHandler.sendEvent(JsString("FAILED_EVENT"))

      verify(mockLog).warn(expectedLogContent)
    }
  }
} 
Example 20
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 21
Source File: DDPCAlgorithmJsonParser.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.dc.stream.algo

import java.net.URL

import cmwell.dc.LazyLogging
import cmwell.dc.stream.MessagesTypesAndExceptions.AlgoData
import play.api.libs.json.{JsArray, JsDefined, JsLookupResult, JsObject, JsString}

object DDPCAlgorithmJsonParser extends LazyLogging {


  def extractAlgoInfo(f: JsLookupResult):AlgoData = {
  val algoClass = f \ "algoClass" match {
      case JsDefined(JsArray(seq))
        if seq.length == 1 && seq.head.isInstanceOf[JsString] =>
        seq.head.as[String]
    }

    val algoJarUrl = f \ "algoJarUrl" match {
      case JsDefined(JsArray(seq))
        if seq.length == 1 && seq.head.isInstanceOf[JsString] =>
        seq.head.as[String]
    }

    val params = f \ "algoParams" match {
      case JsDefined(JsArray(seq)) =>
        seq.collect {
          case JsString(rule) => rule.split("->") match {
            case Array(source, target) => (source, target)
          }
        }.toMap
      case _ => Map.empty[String, String]
    }
    val url = new URL(algoJarUrl)
    if(url.getHost != "localhost" || !url.getPath.startsWith("/meta"))
      throw new IllegalArgumentException(s"Host is not localhost or url doesn't in /meta, url=$algoJarUrl")
    AlgoData(algoClass, algoJarUrl, params)
  }
} 
Example 22
Source File: JsonProducerSpec.scala    From skafka   with MIT License 5 votes vote down vote up
package com.evolutiongaming.skafka.producer

import java.nio.charset.StandardCharsets.UTF_8

import com.evolutiongaming.skafka.{Bytes, Partition, ToBytes, TopicPartition}
import play.api.libs.json.{JsString, Json}
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

class JsonProducerSpec extends AnyFunSuite with Matchers {

  test("apply") {
    val metadata = RecordMetadata(TopicPartition("topic", Partition.min))
    var actual = Option.empty[(Option[Bytes], Option[Bytes])]
    type Id[A] = A
    val send = new Producer.Send[Id] {
      def apply[K, V](
        record: ProducerRecord[K, V])(implicit
        toBytesK: ToBytes[Id, K],
        toBytesV: ToBytes[Id, V]
      ) = {
        val topic = record.topic
        val value = record.value.map(toBytesV(_, topic))
        val key = record.key.map(toBytesK(_, topic))
        actual = Some((key, value))
        metadata
      }
    }
    val producer = JsonProducer(send)

    val value = JsString("value")
    val key = "key"
    val record = ProducerRecord("topic", value, key)
    producer(record) shouldEqual metadata
    val (Some(keyBytes), valueBytes) = actual.get
    new String(keyBytes, UTF_8) shouldEqual key
    valueBytes.map(Json.parse) shouldEqual Some(value)
  }
} 
Example 23
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 24
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 25
Source File: DataFrameConverterSpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.utils

import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{DataFrame, Row}
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, FunSpec, Matchers}
import play.api.libs.json.{JsArray, JsString, Json}
import test.utils.SparkContextProvider

import scala.collection.mutable

class DataFrameConverterSpec extends FunSpec with MockitoSugar with Matchers with BeforeAndAfterAll {

  lazy val spark = SparkContextProvider.sparkContext

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

  val dataFrameConverter: DataFrameConverter = new DataFrameConverter
  val mockDataFrame = mock[DataFrame]
  val mockRdd = spark.parallelize(Seq(Row(new mutable.WrappedArray.ofRef(Array("test1", "test2")), 2, null)))
  val mockStruct = mock[StructType]
  val columns = Seq("foo", "bar").toArray

  doReturn(mockStruct).when(mockDataFrame).schema
  doReturn(columns).when(mockStruct).fieldNames
  doReturn(mockRdd).when(mockDataFrame).rdd

  describe("DataFrameConverter") {
    describe("#convert") {
      it("should convert to a valid JSON object") {
        val someJson = dataFrameConverter.convert(mockDataFrame, "json")
        val jsValue = Json.parse(someJson.get)
        jsValue \ "columns" should be (JsArray(Seq(JsString("foo"), JsString("bar"))))
        jsValue \ "rows" should be (JsArray(Seq(
          JsArray(Seq(JsString("[test1, test2]"), JsString("2"), JsString("null")))
        )))
      }
      it("should convert to csv") {
        val csv = dataFrameConverter.convert(mockDataFrame, "csv").get
        val values = csv.split("\n")
        values(0) shouldBe "foo,bar"
        values(1) shouldBe "[test1, test2],2,null"
      }
      it("should convert to html") {
        val html = dataFrameConverter.convert(mockDataFrame, "html").get
        html.contains("<th>foo</th>") should be(true)
        html.contains("<th>bar</th>") should be(true)
        html.contains("<td>[test1, test2]</td>") should be(true)
        html.contains("<td>2</td>") should be(true)
        html.contains("<td>null</td>") should be(true)
      }
      it("should convert limit the selection") {
        val someLimited = dataFrameConverter.convert(mockDataFrame, "csv", 1)
        val limitedLines = someLimited.get.split("\n")
        limitedLines.length should be(2)
      }
      it("should return a Failure for invalid types") {
        val result = dataFrameConverter.convert(mockDataFrame, "Invalid Type")
        result.isFailure should be(true)
      }
    }
  }
} 
Example 26
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 27
Source File: InvokeScriptTxSerializer.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}
import com.wavesplatform.account.AddressScheme
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils._
import com.wavesplatform.lang.v1.Serde
import com.wavesplatform.lang.v1.compiler.Terms
import com.wavesplatform.lang.v1.compiler.Terms.{EXPR, FUNCTION_CALL}
import com.wavesplatform.serialization._
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.smart.InvokeScriptTransaction
import com.wavesplatform.transaction.smart.InvokeScriptTransaction.Payment
import com.wavesplatform.transaction.{Asset, TxVersion}
import play.api.libs.json.{JsArray, JsObject, JsString, Json}

import scala.util.Try

object InvokeScriptTxSerializer {
  def functionCallToJson(fc: Terms.FUNCTION_CALL): JsObject = {
    Json.obj(
      "function" -> JsString(fc.function.asInstanceOf[com.wavesplatform.lang.v1.FunctionHeader.User].internalName),
      "args" -> JsArray(
        fc.args.map {
          case Terms.ARR(elements) => Json.obj("type" -> "list", "value" -> elements.map(mapSingleArg))
          case other               => mapSingleArg(other)
        }
      )
    )
  }

  private def mapSingleArg(arg: EXPR) =
    arg match {
      case Terms.CONST_LONG(num)      => Json.obj("type" -> "integer", "value" -> num)
      case Terms.CONST_BOOLEAN(bool)  => Json.obj("type" -> "boolean", "value" -> bool)
      case Terms.CONST_BYTESTR(bytes) => Json.obj("type" -> "binary", "value" -> bytes.base64)
      case Terms.CONST_STRING(str)    => Json.obj("type" -> "string", "value" -> str)
      case arg                        => throw new NotImplementedError(s"Not supported: $arg")
    }

  def toJson(tx: InvokeScriptTransaction): JsObject = {
    import tx._
    BaseTxJson.toJson(tx) ++ Json.obj(
      "dApp"    -> dAppAddressOrAlias.stringRepr,
      "payment" -> payments
    ) ++ (funcCallOpt match {
      case Some(fc) => Json.obj("call" -> this.functionCallToJson(fc))
      case None     => JsObject.empty
    })
  }

  def bodyBytes(tx: InvokeScriptTransaction): Array[Byte] = {
    import tx._
    version match {
      case TxVersion.V1 =>
        Bytes.concat(
          Array(builder.typeId, version, chainId),
          sender.arr,
          dAppAddressOrAlias.bytes,
          Deser.serializeOption(funcCallOpt)(Serde.serialize(_)),
          Deser.serializeArrays(payments.map(pmt => Longs.toByteArray(pmt.amount) ++ pmt.assetId.byteRepr)),
          Longs.toByteArray(fee),
          feeAssetId.byteRepr,
          Longs.toByteArray(timestamp)
        )

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

  def toBytes(tx: InvokeScriptTransaction): Array[Byte] =
    if (tx.isProtobufVersion) PBTransactionSerializer.bytes(tx)
    else Bytes.concat(Array(0: Byte), this.bodyBytes(tx), tx.proofs.bytes())

  def parseBytes(bytes: Array[Byte]): Try[InvokeScriptTransaction] = Try {
    def parsePayment(arr: Array[Byte]): Payment = {
      val amt               = Longs.fromByteArray(arr.take(8))
      val (maybeAssetId, _) = Deser.parseOption(arr, 8, 32)(ByteStr.apply)
      val asset             = maybeAssetId.fold[Asset](Waves)(IssuedAsset)
      Payment(amt, asset)
    }

    val buf = ByteBuffer.wrap(bytes)
    require(buf.getByte == 0 && buf.getByte == InvokeScriptTransaction.typeId && buf.getByte == 1, "transaction type mismatch")
    val chainId = buf.getByte
    require(chainId == AddressScheme.current.chainId, "chainId mismatch")

    val sender       = buf.getPublicKey
    val dApp         = buf.getAddressOrAlias
    val functionCall = Deser.parseOption(buf)(Serde.deserialize(_).explicitGet().asInstanceOf[FUNCTION_CALL])
    val payments     = Deser.parseArrays(buf).map(parsePayment)
    val fee          = buf.getLong
    val feeAssetId   = buf.getAsset
    val timestamp    = buf.getLong
    InvokeScriptTransaction(TxVersion.V1, sender, dApp, functionCall, payments, fee, feeAssetId, timestamp, buf.getProofs, chainId)
  }
} 
Example 28
Source File: BaseTxJson.scala    From Waves   with MIT License 5 votes vote down vote up
package com.wavesplatform.transaction.serialization.impl

import com.wavesplatform.transaction.{LegacyPBSwitch, ProvenTransaction, SigProofsSwitch, VersionedTransaction}
import play.api.libs.json.{JsArray, JsObject, JsString, Json}

object BaseTxJson {
  def toJson(tx: ProvenTransaction): JsObject = {
    import tx._
    Json.obj(
      "type"            -> typeId,
      "id"              -> id().toString,
      "sender"          -> sender.toAddress,
      "senderPublicKey" -> sender,
      "fee"             -> assetFee._2,
      "feeAssetId"      -> assetFee._1.maybeBase58Repr,
      "timestamp"       -> timestamp,
      "proofs"          -> JsArray(proofs.proofs.map(p => JsString(p.toString)))
    ) ++ (tx match {
      // Compatibility
      case s: SigProofsSwitch if s.usesLegacySignature => Json.obj("signature" -> tx.signature.toString)
      case _                                           => Json.obj()
    }) ++ (tx match {
      case v: VersionedTransaction => Json.obj("version" -> v.version)
      case _                       => Json.obj()
    }) ++ (tx match {
      case pbs: LegacyPBSwitch if pbs.isProtobufVersion => Json.obj("chainId" -> tx.chainId)
      case _                                            => Json.obj()
    })
  }
} 
Example 29
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 30
Source File: JsonFormatsTest.scala    From courscala   with Apache License 2.0 5 votes vote down vote up
package org.coursera.common.jsonformat

import org.coursera.common.collection.Enum
import org.coursera.common.collection.EnumSymbol
import org.coursera.common.stringkey.StringKey
import org.coursera.common.stringkey.StringKeyFormat
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.Duration
import org.joda.time.Instant
import org.junit.Test
import org.scalatest.junit.AssertionsForJUnit
import play.api.libs.json.Format
import play.api.libs.json.JsNumber
import play.api.libs.json.JsString
import play.api.libs.json.JsSuccess
import play.api.libs.json.Json

class JsonFormatsTest extends AssertionsForJUnit {

  import JsonFormatsTest._

  @Test
  def stringKey(): Unit = {
    val id = TestId(2, "test")
    val idString = StringKey.stringify(id)
    assert(JsString(idString) === Json.toJson(id))
    assert(JsSuccess(id) === Json.fromJson[TestId](JsString(idString)))
    assert(JsString(s"invalid stuff $idString").validate[TestId].isError)
  }

  @Test
  def enums(): Unit = {
    assertResult(Color.Amber)(JsString("Amber").as[Color])

    assertResult(JsString("Green"))(Json.toJson(Color.Green))
  }

  @Test
  def instant(): Unit = {
    import JsonFormats.Implicits.instantFormat
    val testInstant = new Instant(137)
    assertResult(JsNumber(137))(Json.toJson(testInstant))
    assertResult(Some(testInstant))(Json.parse("137").asOpt[Instant])
  }


  @Test
  def duration(): Unit = {
    import JsonFormats.Implicits.durationFormat
    val testDuration = Duration.millis(137L)
    assertResult(JsNumber(137))(Json.toJson(testDuration))
    assertResult(Some(testDuration))(Json.parse("137").asOpt[Duration])
  }


  @Test
  def dateTime(): Unit = {
    import JsonFormats.Implicits.dateTimeFormat
    val testDatetime = new DateTime(2010, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC)
    assertResult(JsNumber(1262304000000L))(Json.toJson(testDatetime))
    assertResult(Some(testDatetime))(Json.parse("1262304000000").asOpt[DateTime].map(_.withZone(DateTimeZone.UTC)))
  }
}

object JsonFormatsTest {

  case class TestId(part1: Int, part2: String)

  object TestId {
    implicit val stringKeyFormat: StringKeyFormat[TestId] =
      StringKeyFormat.caseClassFormat((apply _).tupled, unapply)
    implicit val format: Format[TestId] = JsonFormats.stringKeyFormat[TestId]
  }

  sealed trait Color extends EnumSymbol

  object Color extends Enum[Color] {
    case object Red extends Color
    case object Amber extends Color
    case object Green extends Color

    implicit val format: Format[Color] = JsonFormats.enumFormat(Color)
  }

} 
Example 31
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 32
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 33
Source File: KeyTest.scala    From izanami   with Apache License 2.0 5 votes vote down vote up
package domains

import play.api.libs.json.{JsError, JsString, JsSuccess, Json}
import store.{EmptyPattern, StringPattern}
import test.IzanamiSpec


class KeyTest extends IzanamiSpec {

  "Key deserialization" must {

    "should be valid" in {
      val result = JsString("ragnar:lodbrok:730").validate[Key]
      result mustBe an[JsSuccess[_]]
      result.get must be(Key("ragnar:lodbrok:730"))
    }

    "should be invalid" in {
      val result = JsString("björn:ironside").validate[Key]
      result mustBe an[JsError]
    }

  }

  "Key serialization" must {

    "should be valid" in {
      Json.toJson(Key("ragnar:lodbrok:730")) must be(JsString("ragnar:lodbrok:730"))
    }
  }

  "Test patterns" must {

    "match" in {
      Key("test2:ab:scenario:B:8:displayed").matchPattern("test2:ab:scenario:*") must be(true)
    }

    "not match" in {
      Key("test1:ab:scenario:B:8:displayed").matchPattern("test2:ab:scenario:*") must be(false)
    }

    "match all is true" in {
      Key("test2:ab:scenario:B:8:displayed").matchAllPatterns("test2:ab:scenario:*", "test2:ab:*") must be(true)
    }

    "match all is false" in {
      Key("test1:ab:scenario:B:8:displayed").matchAllPatterns("test2:ab:scenario:*", "test1:ab:*") must be(false)
    }

    "match one str is true" in {
      Key("test1:ab:scenario:B:8:displayed").matchOneStrPatterns("test2:ab:scenario:*", "test1:ab:*") must be(true)
    }

    "match one is false" in {
      Key("test1:ab:scenario:B:8:displayed").matchOneStrPatterns("test2:ab:scenario:*", "test2:ab:*") must be(false)
    }

    "match one pattern is true" in {
      Key("test1:ab:scenario:B:8:displayed").matchOnePatterns(StringPattern("test2:ab:scenario:*"),
                                                              StringPattern("test1:ab:*")) must be(true)
    }

    "match one pattern with empty is false" in {
      Key("test1:ab:scenario:B:8:displayed")
        .matchOnePatterns(StringPattern("test2:ab:scenario:*"), EmptyPattern) must be(false)
    }

  }
} 
Example 34
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 35
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 36
Source File: HttpMarketStatusSpec.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.api.http.entities

import cats.syntax.option._
import com.wavesplatform.dex.domain.order.OrderType
import com.wavesplatform.dex.test.matchers.DiffMatcherWithImplicits
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
import play.api.libs.json.{JsBoolean, JsString, Json}

class HttpMarketStatusSpec extends AnyFreeSpec with Matchers with DiffMatcherWithImplicits {

  private val json =
    """{
      |  "lastPrice" : 1000,
      |  "lastAmount" : 2000,
      |  "lastSide" : "sell",
      |  "bid" : 2222,
      |  "bidAmount" : 1111,
      |  "ask" : 4444,
      |  "askAmount" : 3333,
      |  "success" : true,
      |  "status" : "SimpleResponse"
      |}""".stripMargin

  private val marketStatus =
    HttpMarketStatus(
      lastPrice = 1000L.some,
      lastAmount = 2000L.some,
      lastSide = OrderType.SELL.some,
      bid = 2222L.some,
      bidAmount = 1111L.some,
      ask = 4444L.some,
      askAmount = 3333L.some
    )

  "backward JSON compatibility" - {
    "deserialization" in {
      Json.parse(json).as[HttpMarketStatus] should matchTo(marketStatus)
    }

    "serialization" in {
      val marketStatusJson = Json.toJsObject(marketStatus) + ("success" -> JsBoolean(true)) + ("status" -> JsString("SimpleResponse"))
      Json.prettyPrint(marketStatusJson) should matchTo(json)
    }
  }
} 
Example 37
Source File: SPExclusionSpec.scala    From nisp-frontend   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.nisp.models.enums

import Exclusion.Exclusion
import play.api.libs.json.{JsString, Json}
import uk.gov.hmrc.play.test.UnitSpec

class SPExclusionSpec extends UnitSpec {

  "SP Exclusion" when {
    "serialised into JSON" should {
      "output Dead when Dead is formatted" in {
        Json.toJson(Exclusion.Dead) shouldBe JsString("Dead")
      }

      "parse Dead when Dead is read" in {
        Json.fromJson[Exclusion](JsString("Dead")).get shouldBe Exclusion.Dead
      }
    }
  }
} 
Example 38
Source File: NpsDateSpec.scala    From nisp-frontend   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.nisp.models

import org.joda.time.LocalDate
import play.api.libs.json.{JsNull, JsString, Json}
import uk.gov.hmrc.nisp.utils.Constants
import uk.gov.hmrc.play.test.UnitSpec

class NpsDateSpec extends UnitSpec {
  "NpsDate" when {
    "JSON parsing" should {
      "return a JSError for null date" in {
        JsNull.validate[NpsDate].isError shouldBe true
      }
    }

    "JSON serialisation" should {
      "return JSString in correct format" in {
        Json.toJson(NpsDate(new LocalDate(2015,1,1))) shouldBe JsString("01/01/2015")
      }
      "deserialise works" in {
        Json.fromJson[NpsDate](JsString("01/01/2015")).get shouldBe NpsDate(new LocalDate(2015,1,1))
      }
    }

    "taxYearEndDate" should {
      "return tax year end date" in {
        NpsDate.taxYearEndDate(2015) shouldBe NpsDate(2016, Constants.taxYearsStartEndMonth, Constants.taxYearEndDay)
      }
    }

    "taxYearStartDate" should {
      "return tax year start date" in {
        NpsDate.taxYearStartDate(2015) shouldBe NpsDate(2015, Constants.taxYearsStartEndMonth, Constants.taxYearStartDay)
      }
    }
  }
} 
Example 39
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 40
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 41
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 42
Source File: Worker.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 org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, BaseEntity, ChildModelDef, EntityDef, HiveEnumeration, AttributeFormat ⇒ F, AttributeOption ⇒ O}
import org.elastic4play.utils.Hasher
import org.thp.cortex.models.JsonFormat.workerTypeFormat
import play.api.libs.json.{JsObject, JsString, Json}

import scala.concurrent.Future
import scala.util.Try

object RateUnit extends Enumeration with HiveEnumeration {
  type Type = Value
  val Second         = Value(1)
  val Minute         = Value(60)
  val Hour           = Value(60 * 60)
  val Day            = Value(60 * 60 * 24)
  val Month          = Value(60 * 60 * 24 * 30)
  implicit val reads = enumFormat(this)
}

object WorkerType extends Enumeration with HiveEnumeration {
  type Type = Value
  val analyzer, responder = Value
}

trait WorkerAttributes { _: AttributeDef ⇒
  val workerId           = attribute("_id", F.stringFmt, "Worker id", O.model)
  val name               = attribute("name", F.stringFmt, "Worker name")
  val vers               = attribute("version", F.stringFmt, "Worker version", O.readonly)
  val workerDefinitionId = attribute("workerDefinitionId", F.stringFmt, "Worker definition id", O.readonly)
  val description        = attribute("description", F.textFmt, "Worker description", O.readonly)
  val author             = attribute("author", F.textFmt, "Worker author", O.readonly)
  val url                = attribute("url", F.textFmt, "Worker url", O.readonly)
  val license            = attribute("license", F.textFmt, "Worker license", O.readonly)
  val command            = optionalAttribute("command", F.textFmt, "Worker command", O.readonly)
  val dockerImage        = optionalAttribute("dockerImage", F.textFmt, "Worker docker image", O.readonly)
  val dataTypeList       = multiAttribute("dataTypeList", F.stringFmt, "List of data type this worker can manage")
  val configuration      = attribute("configuration", F.rawFmt, "Configuration of the worker", O.sensitive)
  val baseConfig         = attribute("baseConfig", F.stringFmt, "Base configuration key", O.readonly)
  val rate               = optionalAttribute("rate", F.numberFmt, "Number ")
  val rateUnit           = optionalAttribute("rateUnit", F.enumFmt(RateUnit), "")
  val jobCache           = optionalAttribute("jobCache", F.numberFmt, "")
  val jobTimeout         = optionalAttribute("jobTimeout", F.numberFmt, "")
  val tpe                = attribute("type", F.enumFmt(WorkerType), "", O.readonly)
}

@Singleton
class WorkerModel @Inject()(organizationModel: OrganizationModel)
    extends ChildModelDef[WorkerModel, Worker, OrganizationModel, Organization](organizationModel, "worker", "Worker", "/worker")
    with WorkerAttributes
    with AuditedModel {
  override def creationHook(parent: Option[BaseEntity], attrs: JsObject): Future[JsObject] = {
    val hasher = Hasher("md5")
    val id = for {
      organizationId ← parent.map(_.id)
      name           ← (attrs \ "name").asOpt[String]
      tpe            ← (attrs \ "type").asOpt[String]
    } yield hasher.fromString(s"${organizationId}_${name}_$tpe").head.toString
    Future.successful(attrs + ("_id" → JsString(id.getOrElse("<null>"))))
  }
}

class Worker(model: WorkerModel, attributes: JsObject) extends EntityDef[WorkerModel, Worker](model, attributes) with WorkerAttributes {
  def config: JsObject = Try(Json.parse(configuration()).as[JsObject]).getOrElse(JsObject.empty)
  def organization     = parentId.get
} 
Example 43
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 44
Source File: User.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.models

import scala.concurrent.Future

import play.api.libs.json.{JsArray, JsBoolean, JsObject, JsString}

import org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, BaseEntity, EntityDef, HiveEnumeration, ModelDef, AttributeFormat ⇒ F, AttributeOption ⇒ O}
import org.elastic4play.services.{User ⇒ EUser}

object UserStatus extends Enumeration with HiveEnumeration {
  type Type = Value
  val Ok, Locked     = Value
  implicit val reads = enumFormat(this)
}

trait UserAttributes { _: AttributeDef ⇒
  val login        = attribute("login", F.userFmt, "Login of the user", O.form)
  val userId       = attribute("_id", F.stringFmt, "User id (login)", O.model)
  val key          = optionalAttribute("key", F.stringFmt, "API key", O.sensitive, O.unaudited)
  val userName     = attribute("name", F.stringFmt, "Full name (Firstname Lastname)")
  val roles        = multiAttribute("roles", RoleAttributeFormat, "Comma separated role list (READ, WRITE and ADMIN)")
  val status       = attribute("status", F.enumFmt(UserStatus), "Status of the user", UserStatus.Ok)
  val password     = optionalAttribute("password", F.stringFmt, "Password", O.sensitive, O.unaudited)
  val avatar       = optionalAttribute("avatar", F.rawFmt, "Base64 representation of user avatar image", O.unaudited)
  val preferences  = attribute("preferences", F.rawFmt, "User preferences", "{}", O.sensitive, O.unaudited)
  val organization = attribute("organization", F.stringFmt, "User organization")
}

class UserModel extends ModelDef[UserModel, User]("user", "User", "/user") with UserAttributes with AuditedModel {

  private def setUserId(attrs: JsObject) = (attrs \ "login").asOpt[JsString].fold(attrs) { login ⇒
    attrs - "login" + ("_id" → login)
  }

  override def creationHook(parent: Option[BaseEntity], attrs: JsObject): Future[JsObject] = Future.successful(setUserId(attrs))
}

class User(model: UserModel, attributes: JsObject) extends EntityDef[UserModel, User](model, attributes) with UserAttributes with EUser {
  override def getUserName = userName()
  override def getRoles    = roles()

  override def toJson: JsObject =
    super.toJson +
      ("roles"       → JsArray(roles().map(r ⇒ JsString(r.name.toLowerCase())))) +
      ("hasKey"      → JsBoolean(key().isDefined)) +
      ("hasPassword" → JsBoolean(password().isDefined))
} 
Example 45
Source File: Organization.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, Provider, Singleton}

import scala.concurrent.{ExecutionContext, Future}

import play.api.Logger
import play.api.libs.json.{JsNumber, JsObject, JsString, Json}

import org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, BaseEntity, EntityDef, HiveEnumeration, ModelDef, AttributeFormat ⇒ F, AttributeOption ⇒ O}
import org.elastic4play.services.FindSrv

object OrganizationStatus extends Enumeration with HiveEnumeration {
  type Type = Value
  val Active, Locked = Value
  implicit val reads = enumFormat(this)
}

trait OrganizationAttributes { _: AttributeDef ⇒
  val name        = attribute("name", F.stringFmt, "Organization name", O.form)
  val _id         = attribute("_id", F.stringFmt, "Organization name", O.model)
  val description = attribute("description", F.textFmt, "Organization description")
  val status      = attribute("status", F.enumFmt(OrganizationStatus), "Status of the organization", OrganizationStatus.Active)
}

@Singleton
class OrganizationModel @Inject()(
    findSrv: FindSrv,
    userModelProvider: Provider[UserModel],
    workerModelProvider: Provider[WorkerModel],
    implicit val ec: ExecutionContext
) extends ModelDef[OrganizationModel, Organization]("organization", "Organization", "/organization")
    with OrganizationAttributes
    with AuditedModel {

  private lazy val logger      = Logger(getClass)
  lazy val userModel           = userModelProvider.get
  lazy val workerModel         = workerModelProvider.get
  override def removeAttribute = Json.obj("status" → "Locked")

  override def creationHook(parent: Option[BaseEntity], attrs: JsObject): Future[JsObject] =
    Future.successful {
      (attrs \ "name").asOpt[JsString].fold(attrs) { orgName ⇒
        attrs - "name" + ("_id" → orgName)
      }
    }

  private def buildUserStats(organization: Organization): Future[JsObject] = {
    import org.elastic4play.services.QueryDSL._
    findSrv(userModel, "organization" ~= organization.id, groupByField("status", selectCount))
      .map { userStatsJson ⇒
        val (userCount, userStats) = userStatsJson.value.foldLeft((0L, JsObject.empty)) {
          case ((total, s), (key, value)) ⇒
            val count = (value \ "count").as[Long]
            (total + count, s + (key → JsNumber(count)))
        }
        Json.obj("users" → (userStats + ("total" → JsNumber(userCount))))
      }
  }

  private def buildWorkerStats(organization: Organization): Future[JsObject] = {
    import org.elastic4play.services.QueryDSL._
    findSrv(workerModel, withParent(organization), groupByField("status", selectCount))
      .map { workerStatsJson ⇒
        val (workerCount, workerStats) = workerStatsJson.value.foldLeft((0L, JsObject.empty)) {
          case ((total, s), (key, value)) ⇒
            val count = (value \ "count").as[Long]
            (total + count, s + (key → JsNumber(count)))
        }
        Json.obj("workers" → (workerStats + ("total" → JsNumber(workerCount))))
      }
  }

  override def getStats(entity: BaseEntity): Future[JsObject] =
    entity match {
      case organization: Organization ⇒
        for {
          userStats   ← buildUserStats(organization)
          workerStats ← buildWorkerStats(organization)
        } yield userStats ++ workerStats
      case other ⇒
        logger.warn(s"Request caseStats from a non-case entity ?! ${other.getClass}:$other")
        Future.successful(Json.obj())
    }

}

class Organization(model: OrganizationModel, attributes: JsObject)
    extends EntityDef[OrganizationModel, Organization](model, attributes)
    with OrganizationAttributes {
  override def toJson: JsObject = super.toJson + ("name" → JsString(id))
} 
Example 46
Source File: Job.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.models

import scala.util.Try

import play.api.libs.json.{JsObject, JsString, Json}

import javax.inject.{Inject, Singleton}
import org.thp.cortex.models.JsonFormat.workerTypeFormat

import org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, EntityDef, HiveEnumeration, ModelDef, AttributeFormat ⇒ F, AttributeOption ⇒ O}

object JobStatus extends Enumeration with HiveEnumeration {
  type Type = Value
  val Waiting, InProgress, Success, Failure, Deleted = Value
  implicit val reads                                 = enumFormat(this)
}

trait JobAttributes {
  _: AttributeDef ⇒
  val workerDefinitionId = attribute("workerDefinitionId", F.stringFmt, "Worker definition id", O.readonly)
  val workerId           = attribute("workerId", F.stringFmt, "Worker id", O.readonly)
  val workerName         = attribute("workerName", F.stringFmt, "Worker name", O.readonly)
  val organization       = attribute("organization", F.stringFmt, "Organization ID", O.readonly)
  val status             = attribute("status", F.enumFmt(JobStatus), "Status of the job")
  val startDate          = optionalAttribute("startDate", F.dateFmt, "Analysis start date")
  val endDate            = optionalAttribute("endDate", F.dateFmt, "Analysis end date")
  val dataType           = attribute("dataType", F.stringFmt, "Type of the artifact", O.readonly)
  val data               = optionalAttribute("data", F.rawFmt, "Content of the artifact", O.readonly)
  val attachment         = optionalAttribute("attachment", F.attachmentFmt, "Artifact file content", O.readonly)
  val tlp                = attribute("tlp", TlpAttributeFormat, "TLP level", 2L)
  val pap                = attribute("pap", TlpAttributeFormat, "PAP level", 2L)
  val message            = optionalAttribute("message", F.textFmt, "Message associated to the analysis")
  val errorMessage       = optionalAttribute("errorMessage", F.textFmt, "Message returned by the worker when it fails")
  val parameters         = attribute("parameters", F.rawFmt, "Parameters for this job", "{}")
  val input              = optionalAttribute("input", F.rawFmt, "Data sent to worker")
  val fromCache          = optionalAttribute("fromCache", F.booleanFmt, "Indicates if cache is used", O.form)
  val tpe                = attribute("type", F.enumFmt(WorkerType), "", O.readonly)
  val lbel               = optionalAttribute("label", F.stringFmt, "Label of the job")
  val cacheTag           = optionalAttribute("cacheTag", F.stringFmt, "hash of job discriminant, used for cache", O.readonly)
}

@Singleton
class JobModel @Inject()() extends ModelDef[JobModel, Job]("job", "Job", "/job") with JobAttributes with AuditedModel {

  override val removeAttribute: JsObject = Json.obj("status" → JobStatus.Deleted)

  override def defaultSortBy: Seq[String] = Seq("-createdAt")
}

class Job(model: JobModel, attributes: JsObject) extends EntityDef[JobModel, Job](model, attributes) with JobAttributes {
  val params: JsObject = Try(Json.parse(parameters()).as[JsObject]).getOrElse(JsObject.empty)

  override def toJson: JsObject = {
    val output = input().fold(super.toJson)(
      i ⇒
        super.toJson +
          ("input" → Json.parse(i))
    ) +
      ("parameters"           → params) +
      ("analyzerId"           → JsString(workerId())) +
      ("analyzerName"         → JsString(workerName())) +
      ("analyzerDefinitionId" → JsString(workerDefinitionId())) +
      ("date"                 → Json.toJson(createdAt))
    data() match {
      case Some(d) if tpe() == WorkerType.responder ⇒ output + ("data" → Json.parse(d))
      case _                                        ⇒ output
    }
  }
} 
Example 47
Source File: StatusCtrl.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.controllers

import scala.concurrent.ExecutionContext

import play.api.Configuration
import play.api.http.Status
import play.api.libs.json.Json.toJsFieldJsValueWrapper
import play.api.libs.json.{JsBoolean, JsString, Json}
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}

import com.sksamuel.elastic4s.http.ElasticDsl
import javax.inject.{Inject, Singleton}
import org.elasticsearch.client.Node
import org.thp.cortex.models.Worker

import org.elastic4play.database.DBIndex
import org.elastic4play.services.AuthSrv
import org.elastic4play.services.auth.MultiAuthSrv

@Singleton
class StatusCtrl @Inject()(
    configuration: Configuration,
    authSrv: AuthSrv,
    dbIndex: DBIndex,
    components: ControllerComponents,
    implicit val ec: ExecutionContext
) extends AbstractController(components)
    with Status {

  private[controllers] def getVersion(c: Class[_]) = Option(c.getPackage.getImplementationVersion).getOrElse("SNAPSHOT")

  def get: Action[AnyContent] = Action {
    Ok(
      Json.obj(
        "versions" → Json.obj(
          "Cortex"               → getVersion(classOf[Worker]),
          "Elastic4Play"         → getVersion(classOf[AuthSrv]),
          "Play"                 → getVersion(classOf[AbstractController]),
          "Elastic4s"            → getVersion(classOf[ElasticDsl]),
          "ElasticSearch client" → getVersion(classOf[Node])
        ),
        "config" → Json.obj(
          "protectDownloadsWith" → configuration.get[String]("datastore.attachment.password"),
          "authType" → (authSrv match {
            case multiAuthSrv: MultiAuthSrv ⇒
              multiAuthSrv.authProviders.map { a ⇒
                JsString(a.name)
              }
            case _ ⇒ JsString(authSrv.name)
          }),
          "capabilities" → authSrv.capabilities.map(c ⇒ JsString(c.toString)),
          "ssoAutoLogin" → JsBoolean(configuration.getOptional[Boolean]("auth.sso.autologin").getOrElse(false))
        )
      )
    )
  }

  def health: Action[AnyContent] = TODO
} 
Example 48
Source File: AnalyzerCtrl.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.controllers

import scala.concurrent.{ExecutionContext, Future}

import play.api.libs.json.{JsObject, JsString, Json}
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}

import akka.stream.Materializer
import javax.inject.{Inject, Singleton}
import org.thp.cortex.models.{Roles, Worker}
import org.thp.cortex.services.{UserSrv, WorkerSrv}

import org.elastic4play.controllers.{Authenticated, Fields, FieldsBodyParser, Renderer}
import org.elastic4play.services.JsonFormat.queryReads
import org.elastic4play.services.{QueryDSL, QueryDef}

@Singleton
class AnalyzerCtrl @Inject()(
    workerSrv: WorkerSrv,
    userSrv: UserSrv,
    authenticated: Authenticated,
    fieldsBodyParser: FieldsBodyParser,
    renderer: Renderer,
    components: ControllerComponents,
    implicit val ec: ExecutionContext,
    implicit val mat: Materializer
) extends AbstractController(components) {

  def find: Action[Fields] = authenticated(Roles.read).async(fieldsBodyParser) { request ⇒
    val query                      = request.body.getValue("query").fold[QueryDef](QueryDSL.any)(_.as[QueryDef])
    val range                      = request.body.getString("range")
    val sort                       = request.body.getStrings("sort").getOrElse(Nil)
    val isAdmin                    = request.roles.contains(Roles.orgAdmin)
    val (analyzers, analyzerTotal) = workerSrv.findAnalyzersForUser(request.userId, query, range, sort)
    renderer.toOutput(OK, analyzers.map(analyzerJson(isAdmin)), analyzerTotal)
  }

  def get(analyzerId: String): Action[AnyContent] = authenticated(Roles.read).async { request ⇒
    val isAdmin = request.roles.contains(Roles.orgAdmin)
    workerSrv
      .getForUser(request.userId, analyzerId)
      .map(a ⇒ renderer.toOutput(OK, analyzerJson(isAdmin)(a)))
  }

  private def analyzerJson(isAdmin: Boolean)(analyzer: Worker): JsObject =
    if (isAdmin)
      analyzer.toJson + ("configuration" → Json.parse(analyzer.configuration())) + ("analyzerDefinitionId" → JsString(analyzer.workerDefinitionId()))
    else
      analyzer.toJson + ("analyzerDefinitionId" → JsString(analyzer.workerDefinitionId()))

  def listForType(dataType: String): Action[AnyContent] = authenticated(Roles.read).async { request ⇒
    import org.elastic4play.services.QueryDSL._
    val (responderList, responderCount) = workerSrv.findAnalyzersForUser(request.userId, "dataTypeList" ~= dataType, Some("all"), Nil)
    renderer.toOutput(OK, responderList.map(analyzerJson(isAdmin = false)), responderCount)
  }

  def create(analyzerDefinitionId: String): Action[Fields] = authenticated(Roles.orgAdmin).async(fieldsBodyParser) { implicit request ⇒
    for {
      organizationId   ← userSrv.getOrganizationId(request.userId)
      workerDefinition ← Future.fromTry(workerSrv.getDefinition(analyzerDefinitionId))
      analyzer         ← workerSrv.create(organizationId, workerDefinition, request.body)
    } yield renderer.toOutput(CREATED, analyzerJson(isAdmin = false)(analyzer))
  }

  def listDefinitions: Action[AnyContent] = authenticated(Roles.orgAdmin, Roles.superAdmin).async { _ ⇒
    val (analyzers, analyzerTotal) = workerSrv.listAnalyzerDefinitions
    renderer.toOutput(OK, analyzers, analyzerTotal)
  }

  def scan: Action[AnyContent] = authenticated(Roles.orgAdmin, Roles.superAdmin) { _ ⇒
    workerSrv.rescan()
    NoContent
  }

  def delete(analyzerId: String): Action[AnyContent] = authenticated(Roles.orgAdmin, Roles.superAdmin).async { implicit request ⇒
    for {
      analyzer ← workerSrv.getForUser(request.userId, analyzerId)
      _        ← workerSrv.delete(analyzer)
    } yield NoContent
  }

  def update(analyzerId: String): Action[Fields] = authenticated(Roles.orgAdmin).async(fieldsBodyParser) { implicit request ⇒
    for {
      analyzer        ← workerSrv.getForUser(request.userId, analyzerId)
      updatedAnalyzer ← workerSrv.update(analyzer, request.body)
    } yield renderer.toOutput(OK, analyzerJson(isAdmin = true)(updatedAnalyzer))
  }
} 
Example 49
Source File: KeyFormatTest.scala    From naptime   with Apache License 2.0 5 votes vote down vote up
package org.coursera.naptime.model

import org.coursera.common.jsonformat.JsonFormats
import org.coursera.naptime.model.KeyFormatTest.MembershipId
import org.junit.Test
import org.scalatest.junit.AssertionsForJUnit
import play.api.libs.json.JsString
import play.api.libs.json.JsSuccess
import play.api.libs.json.Json
import play.api.libs.json.Reads
import org.coursera.common.jsonformat.OrFormats.OrReads
import org.coursera.common.stringkey.StringKeyFormat

object KeyFormatTest {
  case class MembershipId(userId: Long, courseId: String)
  object MembershipId {

    implicit val stringKeyFormat: StringKeyFormat[MembershipId] = {
      StringKeyFormat.caseClassFormat((apply _).tupled, unapply)
    }

    val reads: Reads[MembershipId] =
      Json.reads[MembershipId].orReads(JsonFormats.stringKeyFormat[MembershipId])

    val keyFormat =
      KeyFormat.withFallbackReads(reads)(KeyFormat.idAsStringWithFields(Json.format[MembershipId]))
  }
}

class KeyFormatTest extends AssertionsForJUnit {

  @Test
  def testWithComplexReads(): Unit = {
    val oldSerialization = Json.obj("userId" -> 12345L, "courseId" -> "machine-learning")

    val newSerialization = JsString("12345~machine-learning")

    val expected = JsSuccess(MembershipId(12345L, "machine-learning"))

    assert(expected === MembershipId.keyFormat.reads(oldSerialization))
    assert(expected === MembershipId.keyFormat.reads(newSerialization))
  }
} 
Example 50
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 51
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 52
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 53
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 54
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 55
Source File: SchemaReadsSpec.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema.internal.serialization

import com.eclipsesource.schema._
import com.eclipsesource.schema.{JsonSource, SchemaType, SchemaValue}
import com.eclipsesource.schema.drafts.{Version4, Version7}
import com.eclipsesource.schema.internal.draft7.constraints.{AnyConstraints7, NumberConstraints7, StringConstraints7}
import org.specs2.mutable.Specification
import play.api.libs.json.{JsArray, JsBoolean, JsString, Json}

class SchemaReadsSpec extends Specification {


  "Schema Reads for draft 4" should {

    import Version4._

    "not fail with match error (#104)" in {
      val schema = JsonSource.schemaFromString("""
        |{
        |  "someProp": {"type": "sting"}
        |}""".stripMargin)

      schema.isSuccess must beTrue
    }

    "not be able to read boolean schema" in {
      Json.fromJson[SchemaType](JsBoolean(true)).isError must beTrue
    }

    "fail if exclusiveMinimum is not a boolean" in {
      val result = JsonSource.schemaFromString(
        """{ "exclusiveMinimum": 3 }""".stripMargin
      )
      result.asEither must beLeft.like { case error => (error.toJson(0) \ "msgs").get.as[JsArray].value.head ==
        JsString("error.expected.jsboolean")
      }
    }
  }

  "Schema Reads for draft 7" should {

    import Version7._

    "read boolean schema" in {
      val booleanSchema = Json.fromJson[SchemaType](JsBoolean(true)).get
      booleanSchema must beEqualTo(SchemaValue(JsBoolean(true)))
    }

    "read compound type with error" in {
      val schema = JsonSource.schemaFromString("""
                                                 |{
                                                 |  "type": ["number", "string"]
                                                 |}""".stripMargin)

      schema.isSuccess must beTrue
      schema.asOpt must beSome.which(
        _ == CompoundSchemaType(
          Seq(
            SchemaNumber(NumberConstraints7().copy(any = AnyConstraints7().copy(schemaType = Some("number")))),
            SchemaString(StringConstraints7().copy(any = AnyConstraints7().copy(schemaType = Some("string"))))
          )
        )
      )
    }
  }
} 
Example 56
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 57
Source File: ResolveStringConstraintsSpec.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.drafts.{Version4, Version7}
import com.eclipsesource.schema.{JsonSource, SchemaType, SchemaValue}
import org.specs2.mutable.Specification
import play.api.libs.json.{JsNumber, JsString}

class ResolveStringConstraintsSpec extends Specification {

  "draft v4" should {

    import Version4._
    val resolver = SchemaRefResolver(Version4)

    "resolve string constraints" in {
      val schema =
        JsonSource.schemaFromString(
          """{
            |  "type": "string",
            |  "minLength": 1,
            |  "maxLength": 10,
            |  "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"
            |}""".stripMargin).get

      val scope = SchemaResolutionScope(schema)
      resolver.resolveFromRoot("#/minLength", scope)
        .map(_.resolved) must beRight[SchemaType](SchemaValue(JsNumber(1)))
      resolver.resolveFromRoot("#/maxLength", scope)
        .map(_.resolved) must beRight[SchemaType](SchemaValue(JsNumber(10)))
      resolver.resolveFromRoot("#/pattern", scope)
        .map(_.resolved) must beRight[SchemaType](SchemaValue(JsString("^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$")))
    }
  }

  "draft v7" should {

    import Version7._
    val resolver = SchemaRefResolver(Version7)

    "resolve string constraints" in {
      val schema =
        JsonSource.schemaFromString(
          """{
            |  "type": "string",
            |  "minLength": 1,
            |  "maxLength": 10,
            |  "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$"
            |}""".stripMargin).get

      val scope = SchemaResolutionScope(schema)
      resolver.resolveFromRoot("#/minLength", scope)
        .map(_.resolved) must beRight[SchemaType](SchemaValue(JsNumber(1)))
      resolver.resolveFromRoot("#/maxLength", scope)
        .map(_.resolved) must beRight[SchemaType](SchemaValue(JsNumber(10)))
      resolver.resolveFromRoot("#/pattern", scope)
        .map(_.resolved) must beRight[SchemaType](SchemaValue(JsString("^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$")))
    }
  }
} 
Example 58
Source File: MinLengthSpec.scala    From play-json-schema-validator   with Apache License 2.0 5 votes vote down vote up
package com.eclipsesource.schema

import com.eclipsesource.schema.drafts.{Version4, Version7}
import com.eclipsesource.schema.test.JsonSpec
import org.specs2.mutable.Specification
import play.api.libs.json.JsString

class MinLengthSpec extends Specification with JsonSpec {

  "validate draft4" in {
    import Version4._
    implicit val validator: SchemaValidator = SchemaValidator(Some(Version4))
    validate("minLength", "draft4")
  }

  "validate draft7" in {
    import Version7._
    implicit val validator: SchemaValidator = SchemaValidator(Some(Version7))
    validate("minLength", "draft7")
  }

  "MinLength" should {

    import Version4._
    implicit val validator: SchemaValidator = SchemaValidator(Some(Version4))

    "validate against numeric strings that are long enough" in {
      val schema = JsonSource.schemaFromString(
        """{
          |"minLength": 3
        }""".stripMargin).get

      validator.validate(schema)(JsString("123")).isSuccess must beTrue
    }

    "not validate against numeric strings that are too short" in {
      val schema = JsonSource.schemaFromString(
        """{
          |"minLength": 3
        }""".stripMargin).get

      validator.validate(schema)(JsString("12")).isError must beTrue
    }

  }
} 
Example 59
Source File: DataFrameConverterSpec.scala    From incubator-toree   with Apache License 2.0 5 votes vote down vote up
package org.apache.toree.utils

import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{DataFrame, Row}
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.{BeforeAndAfterAll, FunSpec, Matchers}
import play.api.libs.json.{JsArray, JsString, Json}
import test.utils.SparkContextProvider

import scala.collection.mutable

class DataFrameConverterSpec extends FunSpec with MockitoSugar with Matchers with BeforeAndAfterAll {

  lazy val spark = SparkContextProvider.sparkContext

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

  val dataFrameConverter: DataFrameConverter = new DataFrameConverter
  val mockDataFrame = mock[DataFrame]
  val mockRdd = spark.parallelize(Seq(Row(new mutable.WrappedArray.ofRef(Array("test1", "test2")), 2, null)))
  val mockStruct = mock[StructType]
  val columns = Seq("foo", "bar").toArray

  doReturn(mockStruct).when(mockDataFrame).schema
  doReturn(columns).when(mockStruct).fieldNames
  doReturn(mockRdd).when(mockDataFrame).rdd

  describe("DataFrameConverter") {
    describe("#convert") {
      it("should convert to a valid JSON object") {
        val someJson = dataFrameConverter.convert(mockDataFrame, "json")
        val jsValue = Json.parse(someJson.get)
        jsValue \ "columns" should be (JsArray(Seq(JsString("foo"), JsString("bar"))))
        jsValue \ "rows" should be (JsArray(Seq(
          JsArray(Seq(JsString("[test1, test2]"), JsString("2"), JsString("null")))
        )))
      }
      it("should convert to csv") {
        val csv = dataFrameConverter.convert(mockDataFrame, "csv").get
        val values = csv.split("\n")
        values(0) shouldBe "foo,bar"
        values(1) shouldBe "[test1, test2],2,null"
      }
      it("should convert to html") {
        val html = dataFrameConverter.convert(mockDataFrame, "html").get
        html.contains("<th>foo</th>") should be(true)
        html.contains("<th>bar</th>") should be(true)
        html.contains("<td>[test1, test2]</td>") should be(true)
        html.contains("<td>2</td>") should be(true)
        html.contains("<td>null</td>") should be(true)
      }
      it("should convert limit the selection") {
        val someLimited = dataFrameConverter.convert(mockDataFrame, "csv", 1)
        val limitedLines = someLimited.get.split("\n")
        limitedLines.length should be(2)
      }
      it("should return a Failure for invalid types") {
        val result = dataFrameConverter.convert(mockDataFrame, "Invalid Type")
        result.isFailure should be(true)
      }
    }
  }
} 
Example 60
Source File: V16AccountRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.v16

import org.bitcoins.commons.jsonmodels.bitcoind.ReceivedAccount
import org.bitcoins.commons.serializers.JsonReaders._
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.core.currency.Bitcoins
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.rpc.client.common.Client
import play.api.libs.json.{JsBoolean, JsNumber, JsString}

import scala.concurrent.Future


trait V16AccountRpc { self: Client =>

  def getAccountAddress(account: String): Future[BitcoinAddress] = {
    bitcoindCall[BitcoinAddress]("getaccountaddress", List(JsString(account)))
  }

  def getReceivedByAccount(
      account: String,
      confirmations: Int = 1): Future[Bitcoins] = {
    bitcoindCall[Bitcoins]("getreceivedbyaccount",
                           List(JsString(account), JsNumber(confirmations)))
  }

  def getAccount(address: BitcoinAddress): Future[String] = {
    bitcoindCall[String]("getaccount", List(JsString(address.value)))
  }

  def getAddressesByAccount(account: String): Future[Vector[BitcoinAddress]] = {
    bitcoindCall[Vector[BitcoinAddress]]("getaddressesbyaccount",
                                         List(JsString(account)))
  }

  def listAccounts(
      confirmations: Int = 1,
      includeWatchOnly: Boolean = false): Future[Map[String, Bitcoins]] = {
    bitcoindCall[Map[String, Bitcoins]](
      "listaccounts",
      List(JsNumber(confirmations), JsBoolean(includeWatchOnly)))
  }

  def setAccount(address: BitcoinAddress, account: String): Future[Unit] = {
    bitcoindCall[Unit]("setaccount",
                       List(JsString(address.value), JsString(account)))
  }

  def listReceivedByAccount(
      confirmations: Int = 1,
      includeEmpty: Boolean = false,
      includeWatchOnly: Boolean = false): Future[Vector[ReceivedAccount]] = {
    bitcoindCall[Vector[ReceivedAccount]]("listreceivedbyaccount",
                                          List(JsNumber(confirmations),
                                               JsBoolean(includeEmpty),
                                               JsBoolean(includeWatchOnly)))
  }
} 
Example 61
Source File: V16SendRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.v16

import org.bitcoins.commons.serializers.JsonReaders._
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.core.currency.{Bitcoins, CurrencyUnit}
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.crypto.DoubleSha256DigestBE
import org.bitcoins.rpc.client.common.Client
import play.api.libs.json.{JsNumber, JsString, Json}

import scala.concurrent.Future


trait V16SendRpc { self: Client =>

  def move(
      fromAccount: String,
      toAccount: String,
      amount: CurrencyUnit,
      comment: String = ""): Future[Boolean] = {
    bitcoindCall[Boolean]("move",
                          List(JsString(fromAccount),
                               JsString(toAccount),
                               Json.toJson(Bitcoins(amount.satoshis)),
                               JsNumber(6),
                               JsString(comment)))
  }

  def sendFrom(
      fromAccount: String,
      toAddress: BitcoinAddress,
      amount: CurrencyUnit,
      confirmations: Int = 1,
      comment: String = "",
      toComment: String = ""): Future[DoubleSha256DigestBE] = {
    bitcoindCall[DoubleSha256DigestBE](
      "sendfrom",
      List(JsString(fromAccount),
           Json.toJson(toAddress),
           Json.toJson(Bitcoins(amount.satoshis)),
           JsNumber(confirmations),
           JsString(comment),
           JsString(toComment))
    )
  }
} 
Example 62
Source File: V18AssortedRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.v18

import org.bitcoins.commons.jsonmodels.bitcoind.{
  GetNodeAddressesResult,
  GetRpcInfoResult,
  ListWalletDirResult
}
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.core.protocol.blockchain.BlockHeader
import org.bitcoins.rpc.client.common.Client
import play.api.libs.json.{JsString, Json}

import scala.concurrent.Future


trait V18AssortedRpc {
  self: Client =>

  private def getNodeAddresses(
      count: Option[Int]): Future[Vector[GetNodeAddressesResult]] = {
    bitcoindCall[Vector[GetNodeAddressesResult]]("getnodeaddresses",
                                                 List(Json.toJson(count)))
  }

  def getNodeAddresses(count: Int): Future[Vector[GetNodeAddressesResult]] =
    getNodeAddresses(Some(count))

  def getNodeAddresses(): Future[Vector[GetNodeAddressesResult]] =
    getNodeAddresses(None)

  def listWalletDir(): Future[ListWalletDirResult] = {
    bitcoindCall[ListWalletDirResult]("listwalletdir")
  }

  def getRpcInfo(): Future[GetRpcInfoResult] = {
    bitcoindCall[GetRpcInfoResult]("getrpcinfo")
  }

  def submitHeader(header: BlockHeader): Future[Unit] = {
    bitcoindCall[Unit]("submitheader", List(JsString(header.hex)))
  }

} 
Example 63
Source File: V17LabelRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.v17

import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.LabelPurpose
import org.bitcoins.commons.jsonmodels.bitcoind.{LabelResult, ReceivedLabel}
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.core.currency.Bitcoins
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.rpc.client.common.Client
import play.api.libs.json.{JsBoolean, JsNumber, JsString}

import scala.concurrent.Future


trait V17LabelRpc { self: Client =>

  def getAddressesByLabel(
      label: String): Future[Map[BitcoinAddress, LabelResult]] = {
    bitcoindCall[Map[BitcoinAddress, LabelResult]]("getaddressesbylabel",
                                                   List(JsString(label)))
  }

  def getReceivedByLabel(
      account: String,
      confirmations: Int = 1): Future[Bitcoins] = {
    bitcoindCall[Bitcoins]("getreceivedbylabel",
                           List(JsString(account), JsNumber(confirmations)))
  }

  def setLabel(address: BitcoinAddress, label: String): Future[Unit] = {
    bitcoindCall[Unit]("setlabel",
                       List(JsString(address.value), JsString(label)))
  }

  def listLabels(
      purpose: Option[LabelPurpose] = None): Future[Vector[String]] = {
    bitcoindCall[Vector[String]]("listlabels",
                                 List(JsString(purpose.getOrElse("").toString)))
  }

  def listReceivedByLabel(
      confirmations: Int = 1,
      includeEmpty: Boolean = false,
      includeWatchOnly: Boolean = false): Future[Vector[ReceivedLabel]] = {
    bitcoindCall[Vector[ReceivedLabel]]("listreceivedbylabel",
                                        List(JsNumber(confirmations),
                                             JsBoolean(includeEmpty),
                                             JsBoolean(includeWatchOnly)))
  }
} 
Example 64
Source File: UtilRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.common

import org.bitcoins.commons.jsonmodels.bitcoind.{
  DecodeScriptResult,
  ValidateAddressResult,
  ValidateAddressResultImpl
}
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.core.protocol.script.ScriptPubKey
import play.api.libs.json.{JsString, Json}

import scala.concurrent.Future


trait UtilRpc { self: Client =>

  def validateAddress(
      address: BitcoinAddress): Future[ValidateAddressResult] = {
    bitcoindCall[ValidateAddressResultImpl]("validateaddress",
                                            List(JsString(address.toString)))
  }

  def decodeScript(script: ScriptPubKey): Future[DecodeScriptResult] = {
    bitcoindCall[DecodeScriptResult]("decodescript", List(Json.toJson(script)))
  }
} 
Example 65
Source File: MessageRpc.scala    From bitcoin-s   with MIT License 5 votes vote down vote up
package org.bitcoins.rpc.client.common

import org.bitcoins.core.crypto.ECPrivateKeyUtil
import org.bitcoins.core.protocol.P2PKHAddress
import org.bitcoins.crypto.ECPrivateKey
import play.api.libs.json.JsString

import scala.concurrent.Future


trait MessageRpc { self: Client =>

  def signMessage(address: P2PKHAddress, message: String): Future[String] = {
    bitcoindCall[String]("signmessage",
                         List(JsString(address.value), JsString(message)))
  }

  def signMessageWithPrivKey(
      key: ECPrivateKey,
      message: String): Future[String] = {
    bitcoindCall[String](
      "signmessagewithprivkey",
      List(JsString(ECPrivateKeyUtil.toWIF(key, network)), JsString(message)))
  }

  def verifyMessage(
      address: P2PKHAddress,
      signature: String,
      message: String): Future[Boolean] = {
    bitcoindCall[Boolean](
      "verifymessage",
      List(JsString(address.value), JsString(signature), JsString(message)))
  }
}