org.scalatest.prop.PropertyChecks Scala Examples

The following examples show how to use org.scalatest.prop.PropertyChecks. 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: AirlinesHttpEndpointSpec.scala    From core   with Apache License 2.0 5 votes vote down vote up
package com.smartbackpackerapp.http

import cats.effect.IO
import com.smartbackpackerapp.common.IOAssertion
import com.smartbackpackerapp.http.Http4sUtils._
import com.smartbackpackerapp.model._
import com.smartbackpackerapp.repository.algebra.AirlineRepository
import com.smartbackpackerapp.service.AirlineService
import org.http4s.{HttpService, Query, Request, Status, Uri}
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpecLike, Matchers}

class AirlinesHttpEndpointSpec extends FlatSpecLike with Matchers with AirlinesHttpEndpointFixture {

  forAll(examples) { (airline, expectedStatus, expectedBody) =>
    it should s"find the airline $airline" in IOAssertion {
      val request = Request[IO](uri = Uri(path = s"/$ApiVersion/airlines", query = Query(("name", Some(airline)))))

      httpService(request).value.map { task =>
        task.fold(fail("Empty response")){ response =>
          response.status should be (expectedStatus)
          assert(response.body.asString.contains(expectedBody))
        }
      }
    }
  }

}

trait AirlinesHttpEndpointFixture extends PropertyChecks {

  private val airlines: List[Airline] = List(
    Airline(AirlineName("Aer Lingus"), BaggagePolicy(
      allowance = List(
        BaggageAllowance(CabinBag, Some(10), BaggageSize(55, 40, 24)),
        BaggageAllowance(SmallBag, None, BaggageSize(25, 33, 20))
      ),
      extra = None,
      website = Some("https://www.aerlingus.com/travel-information/baggage-information/cabin-baggage/"))
    ),
    Airline(AirlineName("Transavia"), BaggagePolicy(
      allowance = List(
        BaggageAllowance(CabinBag, None, BaggageSize(55, 40, 25))
      ),
      extra = None,
      website = Some("https://www.transavia.com/en-EU/service/hand-luggage/"))
    )
  )

  private val testAirlineRepo = new AirlineRepository[IO] {
    override def findAirline(airlineName: AirlineName): IO[Option[Airline]] = IO {
      airlines.find(_.name.value == airlineName.value)
    }
  }

  private implicit val errorHandler = new HttpErrorHandler[IO]

  val httpService: HttpService[IO] =
    ioMiddleware(
      new AirlinesHttpEndpoint(
        new AirlineService[IO](testAirlineRepo)
      ).service
    )

  val examples = Table(
    ("airline", "expectedStatus", "expectedBody"),
    ("Aer Lingus", Status.Ok, "baggagePolicy"),
    ("Transavia", Status.Ok, "baggagePolicy"),
    ("Ryan Air", Status.NotFound, """{"code":"100","error":"Airline not found Ryan Air"}""")
  )

} 
Example 2
Source File: ConversionsSpec.scala    From core   with Apache License 2.0 5 votes vote down vote up
package com.smartbackpackerapp.scraper.sql

import com.smartbackpackerapp.model._
import com.smartbackpackerapp.scraper.model.VisaRequirementsFor
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck._
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class ConversionsSpec extends FunSuite with ConversionsArbitraries with PropertyChecks {

  forAll { (vr: VisaRequirementsFor) =>
    test(s"convert a $vr into a VisaRequirementsDTO") {
      val dto = (vr.from.value, vr.to.value, vr.visaCategory.toString, vr.description)
      assert(vr.toVisaRequirementsDTO == dto)
    }
  }

  forAll { (v: Vaccine) =>
    test(s"convert a $v into a VaccineDTO # ${v.hashCode()}") {
      val dto = (v.disease.value, v.description, v.diseaseCategories.map(_.toString).mkString(","))
      assert(v.toVaccineDTO == dto)
    }
  }

}

trait ConversionsArbitraries {

  implicit val visaCategory: Arbitrary[VisaCategory] = Arbitrary[VisaCategory] {
    val list = List(
      VisaNotRequired, VisaWaiverProgram, AdmissionRefused, TravelBanned,
      VisaRequired, VisaDeFactoRequired, ElectronicVisa, ElectronicVisitor,
      ElectronicTravelAuthority, FreeVisaOnArrival, VisaOnArrival,
      ElectronicVisaPlusVisaOnArrival, OnlineReciprocityFee,
      MainlandTravelPermit, HomeReturnPermitOnly, UnknownVisaCategory
    )
    Gen.oneOf(list)
  }

  implicit val visaRequirementsFor: Arbitrary[VisaRequirementsFor] = Arbitrary[VisaRequirementsFor] {
    for {
      f <- Gen.alphaUpperStr
      t <- Gen.alphaUpperStr
      c <- arbitrary[VisaCategory]
      d <- Gen.alphaStr
    } yield VisaRequirementsFor(CountryCode(f), CountryCode(t), c, d)
  }

  implicit val diseaseCategory: Arbitrary[DiseaseCategory] = Arbitrary[DiseaseCategory] {
    val list = List(
      AvoidNonSterileEquipment, TakeAntimalarialMeds, GetVaccinated,
      AvoidSharingBodyFluids, ReduceExposureToGerms, EatAndDrinkSafely,
      PreventBugBites, KeepAwayFromAnimals, UnknownDiseaseCategory
    )
    Gen.oneOf(list)
  }

  implicit val vaccine: Arbitrary[Vaccine] = Arbitrary[Vaccine] {
    for {
      d <- Gen.alphaStr
      x <- Gen.alphaStr
      c <- Gen.listOf(arbitrary[DiseaseCategory])
    } yield Vaccine(Disease(d), x, c)
  }

} 
Example 3
Source File: ToxExceptionTest.scala    From jvm-toxcore-c   with GNU General Public License v3.0 5 votes vote down vote up
package im.tox.tox4j.exceptions

import im.tox.tox4j.core.exceptions.ToxBootstrapException
import org.scalatest.FlatSpec
import org.scalatest.prop.PropertyChecks

@SuppressWarnings(Array("org.wartremover.warts.Equals"))
final class ToxExceptionTest extends FlatSpec with PropertyChecks {

  "getMessage" should "contain the error code name" in {
    ToxBootstrapException.Code.values().foreach { code =>
      val exn = new ToxBootstrapException(code)
      assert(exn.getMessage.contains(code.name()))
    }
  }

  it should "contain the exception message" in {
    forAll { (message: String) =>
      val exn = new ToxBootstrapException(ToxBootstrapException.Code.NULL, message)
      assert(exn.getMessage.contains(message))
    }
  }

  "code" should "be the passed code" in {
    ToxBootstrapException.Code.values().foreach { code =>
      val exn = new ToxBootstrapException(code)
      assert(exn.code == code)
    }
  }

} 
Example 4
Source File: BadInstanceNumberTest.scala    From jvm-toxcore-c   with GNU General Public License v3.0 5 votes vote down vote up
package im.tox.tox4j.impl.jni

import im.tox.tox4j.core.options.ToxOptions
import im.tox.tox4j.exceptions.ToxKilledException
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks


@SuppressWarnings(Array("org.wartremover.warts.Equals"))
final class BadInstanceNumberTest extends FunSuite with PropertyChecks {

  private def callWithInstanceNumber(instanceNumber: Int): Unit = {
    val tox = new ToxCoreImpl(ToxOptions())

    val field = tox.getClass.getDeclaredField("instanceNumber")
    field.setAccessible(true)
    val oldInstanceNumber = field.get(tox).asInstanceOf[Int]
    field.set(tox, instanceNumber)

    val exception =
      try {
        tox.iterationInterval
        null
      } catch {
        case e: Throwable => e
      }

    // Set it back to the good one, so close() works.
    field.set(tox, oldInstanceNumber)
    tox.close()

    if (exception != null) {
      throw exception
    }
  }

  test("negative or zero instance numbers") {
    forAll(Gen.choose(Int.MinValue, 0)) { instanceNumber =>
      intercept[IllegalStateException] {
        callWithInstanceNumber(instanceNumber)
      }
    }
  }

  test("very large instance numbers") {
    forAll(Gen.choose(0xffff, Int.MaxValue)) { instanceNumber =>
      intercept[IllegalStateException] {
        callWithInstanceNumber(instanceNumber)
      }
    }
  }

  test("any invalid instance numbers") {
    // This could be fine if there is another Tox instance lingering around, but we assume there isn't.
    // So, it's either killed (ToxKilledException) or never existed (IllegalStateException).
    System.gc() // After this, there should be no lingering instances.

    forAll { (instanceNumber: Int) =>
      whenever(instanceNumber != 1) {
        try {
          callWithInstanceNumber(instanceNumber)
          fail("No exception thrown. Expected IllegalStateException or ToxKilledException.")
        } catch {
          case _: IllegalStateException =>
          case _: ToxKilledException    => // Both fine.
        }
      }
    }
  }

} 
Example 5
Source File: ToxJniLogTest.scala    From jvm-toxcore-c   with GNU General Public License v3.0 5 votes vote down vote up
package im.tox.tox4j.impl.jni

import im.tox.tox4j.core.data.ToxFriendNumber
import im.tox.tox4j.impl.jni.proto.JniLog
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

@SuppressWarnings(Array("org.wartremover.warts.Equals"))
final class ToxJniLogTest extends FunSuite with PropertyChecks {

  private val TestMaxSize = 100

  private val friendNumber = ToxFriendNumber.fromInt(0).get

  test("constructing and destroying a Tox instance with logging enabled should result in a non-empty log") {
    ToxJniLog() // clear

    ToxJniLog.maxSize = TestMaxSize
    assert(ToxJniLog.maxSize == TestMaxSize)
    assert(ToxJniLog().entries.isEmpty)
    // Construct and destroy a Tox instance to cause something (tox_new) to be logged and the log
    // will be non-empty.
    ToxCoreImplFactory.withToxUnit { tox => }
    assert(ToxJniLog().entries.nonEmpty)
  }

  test("constructing and destroying a Tox instance with logging disabled should result in an empty log") {
    ToxJniLog() // clear

    ToxJniLog.maxSize = 0
    assert(ToxJniLog.maxSize == 0)
    assert(ToxJniLog().entries.isEmpty)
    ToxCoreImplFactory.withToxUnit { tox => }
    assert(ToxJniLog().entries.isEmpty)
  }

  test("one log entry per native call") {
    ToxJniLog() // clear

    ToxJniLog.maxSize = TestMaxSize
    assert(ToxJniLog().entries.isEmpty)

    ToxCoreImplFactory.withToxUnit { tox => }
    val count1 = ToxJniLog().entries.size

    ToxCoreImplFactory.withToxUnit { tox => tox.friendExists(friendNumber) }
    val count2 = ToxJniLog().entries.size

    assert(count2 == count1 + 1)
  }

  test("null protobufs are ignored") {
    assert(ToxJniLog.fromBytes(null) == JniLog.defaultInstance)
  }

  test("invalid protobufs are ignored") {
    forAll { (bytes: Array[Byte]) =>
      assert(ToxJniLog.fromBytes(bytes) == JniLog.defaultInstance)
    }
  }

  test("concurrent logging works") {
    ToxJniLog() // clear
    ToxJniLog.maxSize = 10000

    forAll(Gen.choose(1, 99), Gen.choose(1, 100)) { (threadCount, iterations) =>
      val threads = for (_ <- 1 to threadCount) yield {
        new Thread {
          override def run(): Unit = {
            ToxCoreImplFactory.withToxUnit { tox =>
              for (_ <- 0 until iterations) {
                tox.friendExists(friendNumber)
              }
            }
          }
        }
      }

      threads.foreach(_.start())
      threads.foreach(_.join())

      val log = ToxJniLog()
      assert(log.entries.size < 10000)
      assert(log.entries.size == threadCount + threadCount * iterations)
      assert(ToxJniLog.toString(log).count(_ == '\n') == log.entries.size)
    }

    assert(ToxJniLog().entries.isEmpty)
    ToxJniLog.maxSize = 0
  }

} 
Example 6
Source File: ToxCoreTest.scala    From jvm-toxcore-c   with GNU General Public License v3.0 5 votes vote down vote up
package im.tox.tox4j.core

import im.tox.tox4j.core.SmallNat._
import im.tox.tox4j.core.callbacks.ToxCoreEventListener
import im.tox.tox4j.core.data.{ ToxFriendNumber, ToxFriendRequestMessage }
import im.tox.tox4j.core.enums.ToxConnection
import im.tox.tox4j.impl.jni.ToxCoreImpl
import im.tox.tox4j.impl.jni.ToxCoreImplFactory.withToxUnit
import im.tox.tox4j.testing.GetDisjunction._
import org.scalatest.FlatSpec
import org.scalatest.prop.PropertyChecks

@SuppressWarnings(Array("org.wartremover.warts.Equals"))
final class ToxCoreTest extends FlatSpec with PropertyChecks {

  "addFriend" should "return increasing friend numbers and increment the friend list size" in {
    forAll { (count: SmallNat, message: Array[Byte]) =>
      whenever(message.length >= 1 && message.length <= ToxCoreConstants.MaxFriendRequestLength) {
        withToxUnit { tox =>
          (0 until count).map(ToxFriendNumber.fromInt(_).get) foreach { i =>
            withToxUnit { friend =>
              val friendNumber = tox.addFriend(
                friend.getAddress,
                ToxFriendRequestMessage.fromValue(message).toOption.get
              )
              assert(friendNumber == i)
            }
          }
          assert(tox.getFriendList.length == count.self)
        }
      }
    }
  }

  "onClose callbacks" should "have been called after close" in {
    var called = false
    withToxUnit { tox =>
      tox.asInstanceOf[ToxCoreImpl].addOnCloseCallback { () =>
        called = true
      }
    }
    assert(called)
  }

  they should "not be called before close" in {
    var called = false
    withToxUnit { tox =>
      tox.asInstanceOf[ToxCoreImpl].addOnCloseCallback { () =>
        called = true
      }
      assert(!called)
    }
  }

  they should "not be called if they were unregistered" in {
    var called = false
    withToxUnit { tox =>
      val toxImpl = tox.asInstanceOf[ToxCoreImpl]
      val id = toxImpl.addOnCloseCallback { () =>
        called = true
      }
      toxImpl.removeOnCloseCallback(id)
    }
    assert(!called)
  }

} 
Example 7
Source File: RandomCoreTest.scala    From jvm-toxcore-c   with GNU General Public License v3.0 5 votes vote down vote up
package im.tox.core.random

import org.scalacheck.Gen
import org.scalatest.WordSpec
import org.scalatest.prop.PropertyChecks

@SuppressWarnings(Array("org.wartremover.warts.Equals"))
final class RandomCoreTest extends WordSpec with PropertyChecks {

  "entropy" should {
    "be 0 for the empty sequence" in {
      assert(RandomCore.entropy(Nil) == 0)
    }

    "be 0 for sequences of all the same element" in {
      forAll { (bytes: Seq[Byte], filler: Byte) =>
        assert(RandomCore.entropy(bytes.map(_ => filler)) == 0)
      }
    }

    "be near 1 for long sequences" in {
      assert(RandomCore.entropy((0 to 1000).map(_.toByte)) > 0.999)
    }

    "be 1 for sequences containing every byte equally often (maximum)" in {
      forAll(Gen.choose(1, 10)) { count =>
        val bytes = (1 to count).flatMap(_ => (0 to 255).map(_.toByte))
        assert(RandomCore.entropy(bytes) == 1)
      }
    }

    "be sorting-insensitive (symmetry)" in {
      forAll { (bytes: Seq[Byte]) =>
        assert(RandomCore.entropy(bytes) == RandomCore.entropy(bytes.sorted))
        assert(RandomCore.entropy(bytes) == RandomCore.entropy(bytes.reverse))
      }
    }
  }

} 
Example 8
Source File: VectorConnectionPropertiesTest.scala    From spark-vector   with Apache License 2.0 5 votes vote down vote up
package com.actian.spark_vector.vector

import org.scalatest.{FunSuite, Matchers}
import org.scalatest.prop.PropertyChecks

class VectorConnectionPropertiesTest extends FunSuite with Matchers with PropertyChecks {
  val validCombos = Table(
    ("host", "instance", "database", "user", "password", "expectedURL"),
    ("host.com", "VH", "db", Some("user"), Some("pw"), "jdbc:ingres://host.com:VH7/db"),
    ("some.host.com", "VW", "db", None, None, "jdbc:ingres://some.host.com:VW7/db"),
    ("justhost", "99", "mydatabase", None, None, "jdbc:ingres://justhost:997/mydatabase"))

  val invalidCombos = Table(
    ("host", "instance", "database"),
    (null, "VW", "database"),
    ("", "VW", "database"),
    ("host.com", null, "db"),
    ("host.com", "", "db"),
    ("host.com", "VW", null),
    ("host.com", "VW", ""))

  test("valid URL and values") {
    forAll(validCombos) { (host: String, instance: String, database: String, user: Option[String], password: Option[String], expectedURL: String) =>
      // With user & password
      val props = VectorConnectionProperties(host, instance, database, user, password)
      validate(props, host, instance, database, user, password, expectedURL)

      // Without user & password
      val props2 = VectorConnectionProperties(host, instance, database)
      validate(props2, host, instance, database, None, None, expectedURL)
    }
  }

  test("invalid vector properties") {
    forAll(invalidCombos) { (host: String, instance: String, database: String) =>
      a[IllegalArgumentException] should be thrownBy {
        VectorConnectionProperties(host, instance, database)
      }
    }
  }

  private def validate(props: VectorConnectionProperties, host: String, instance: String, database: String, user: Option[String], password: Option[String], expectedURL: String): Unit = {
    props.toJdbcUrl should be(expectedURL)
    props.host should be(host)
    props.instance should be(instance)
    props.database should be(database)
    props.user should be(user)
    props.password should be(password)
  }
} 
Example 9
Source File: TableSchemaGeneratorTest.scala    From spark-vector   with Apache License 2.0 5 votes vote down vote up
package com.actian.spark_vector.vector

import org.apache.spark.sql.types._
import org.scalacheck.Gen.identifier
import org.scalacheck.Shrink
import org.scalatest.{ FunSuite, Inspectors, Matchers }
import org.scalatest.prop.PropertyChecks

import com.actian.spark_vector.vector.VectorJDBC.withJDBC;
import com.actian.spark_vector.DataTypeGens.schemaGen
import com.actian.spark_vector.test.IntegrationTest
import com.actian.spark_vector.test.tags.RandomizedTest

@IntegrationTest
class TableSchemaGeneratorTest extends FunSuite with Matchers with PropertyChecks with VectorFixture {
  import com.actian.spark_vector.DataTypeGens._
  import com.actian.spark_vector.vector.TableSchemaGenerator._
  import org.scalacheck.Gen._

  val defaultFields: Seq[StructField] = Seq(
    StructField("a", BooleanType, true),
    StructField("b", ByteType, false),
    StructField("c", ShortType, true),
    StructField("d", IntegerType, false),
    StructField("e", LongType, true),
    StructField("f", FloatType, false),
    StructField("g", DoubleType, true),
    StructField("h", DecimalType(10, 2), false),
    StructField("i", DateType, true),
    StructField("j", TimestampType, false),
    StructField("k", StringType, true))

  val defaultSchema = StructType(defaultFields)

  test("table schema") {
    withJDBC(connectionProps)(cxn => {
      cxn.autoCommit(false)
      assertSchemaGeneration(cxn, "testtable", defaultSchema)
    })
  }

  test("table schema/gen", RandomizedTest) {
    withJDBC(connectionProps)(cxn => {
      cxn.autoCommit(false)
      forAll(identifier, schemaGen)((name, schema) => {
        assertSchemaGeneration(cxn, name, schema)
      })(PropertyCheckConfig(minSuccessful = 5), Shrink.shrinkAny[String], Shrink.shrinkAny[StructType])
    })
  }

  private def assertSchemaGeneration(cxn: VectorJDBC, name: String, schema: StructType): Unit = {
    val sql = generateTableSQL(name, schema)
    try {
      cxn.executeStatement(sql)
      val columnsAsFields = cxn.columnMetadata(name).map(_.structField)
      columnsAsFields.size should be(schema.fields.length)
      Inspectors.forAll(columnsAsFields.zip(schema.fields)) {
        case (columnField, origField) => {
          columnField.name should be(origField.name.toLowerCase)
          columnField.dataType should be(origField.dataType)
          columnField.nullable should be(origField.nullable)
          // TODO ensure field metadata consistency
        }
      }
      cxn.dropTable(name)
    } finally {
      cxn.rollback()
    }
  }
} 
Example 10
Source File: GroupContiguousSpec.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.store.dao.events

import akka.stream.scaladsl.{Sink, Source}
import com.daml.ledger.api.testing.utils.AkkaBeforeAndAfterAll
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.prop.PropertyChecks
import org.scalatest.{AsyncFlatSpec, Matchers}

final class GroupContiguousSpec
    extends AsyncFlatSpec
    with Matchers
    with PropertyChecks
    with ScalaFutures
    with AkkaBeforeAndAfterAll {

  behavior of "groupContiguous"

  override def spanScaleFactor: Double = 10 // Give some extra slack on CI

  it should "be equivalent to grouping on inputs with an ordered key" in forAll {
    pairs: List[(Int, String)] =>
      val sortedPairs = pairs.sortBy(_._1)
      val grouped = groupContiguous(Source(sortedPairs))(by = _._1)
      whenReady(grouped.runWith(Sink.seq[Vector[(Int, String)]])) {
        _ should contain theSameElementsAs pairs.groupBy(_._1).values
      }
  }

  it should "be equivalent to grouping on inputs with a contiguous key" in {
    val pairsWithContiguousKeys = List(1 -> "baz", 0 -> "foo", 0 -> "bar", 0 -> "quux")
    val grouped = groupContiguous(Source(pairsWithContiguousKeys))(by = _._1)
    whenReady(grouped.runWith(Sink.seq[Vector[(Int, String)]])) {
      _.map(_.toSet) should contain theSameElementsAs pairsWithContiguousKeys
        .groupBy(_._1)
        .map(_._2.toSet)
    }
  }

  it should "behave as expected when grouping inputs without a contiguous key" in {
    val pairs = List(0 -> "foo", 0 -> "bar", 1 -> "baz", 0 -> "quux")
    val grouped = groupContiguous(Source(pairs))(by = _._1)
    whenReady(grouped.runWith(Sink.seq[Vector[(Int, String)]])) {
      _.map(_.toSet) should contain theSameElementsAs Vector(
        Set(0 -> "foo", 0 -> "bar"),
        Set(1 -> "baz"),
        Set(0 -> "quux"),
      )
    }
  }

} 
Example 11
Source File: RelationTest.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.lf.data

import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

final class RelationTest extends PropSpec with Matchers with PropertyChecks {

  import Relation.Relation._

  // an empty map and a map with exclusively empty values represent
  // the same relationship but the underlying structure is different
  private val nonEmptyRelations: Gen[Map[Int, Set[Char]]] =
    arbitrary[Map[Int, Set[Char]]].suchThat(_.values.forall(_.nonEmpty))

  property("invert andThen invert == identity for non empty relations") {
    forAll(nonEmptyRelations) { nonEmpty: Map[Int, Set[Char]] =>
      nonEmpty shouldEqual invert(invert(nonEmpty))
    }
  }

  property("union commutative") {
    forAll { (m1: Map[Int, Set[Char]], m2: Map[Int, Set[Char]]) =>
      union(m1, m2) shouldEqual union(m2, m1)
    }
  }

  property("union associative") {
    forAll { (m1: Map[Int, Set[Char]], m2: Map[Int, Set[Char]], m3: Map[Int, Set[Char]]) =>
      union(union(m1, m2), m3) shouldEqual union(m1, union(m2, m3))
    }
  }

  property("union has unit") {
    forAll { m: Map[Int, Set[Char]] =>
      union(m, Map.empty[Int, Set[Char]]) shouldEqual m
      union(Map.empty[Int, Set[Char]], m) shouldEqual m
    }
  }

  property("flattening is the inverse of grouping for non empty relations") {
    forAll(nonEmptyRelations) { nonEmpty =>
      flatten(nonEmpty).toSeq.groupBy(_._1).mapValues(_.map(_._2).toSet) shouldEqual nonEmpty
    }
  }

  property("diff is idempotent") {
    forAll { (m1: Map[Int, Set[Char]], m2: Map[Int, Set[Char]]) =>
      diff(m1, m2) shouldEqual diff(diff(m1, m2), m2)
    }
  }

  property("diff by empty doesn't affect non-empty relations") {
    forAll(nonEmptyRelations) { m =>
      diff(m, Map.empty[Int, Set[Char]]) shouldEqual m
    }
  }

  property("diff: no item in the right operand appears in the result") {
    forAll { (m1: Map[Int, Set[Char]], m2: Map[Int, Set[Char]]) =>
      val result = flatten(diff(m1, m2)).toList
      val right = flatten(m2).toList
      result should contain noElementsOf right
    }
  }

  property("diff: items in the result should be a subset of the ones in the left operand") {
    forAll { (m1: Map[Int, Set[Char]], m2: Map[Int, Set[Char]]) =>
      val result = flatten(diff(m1, m2)).toSet
      val left = flatten(m1).toSet
      assert(result.subsetOf(left))
    }
  }

  property("diff is equivalent to flatten-and-diff") {
    forAll { (m1: Map[Int, Set[Char]], m2: Map[Int, Set[Char]]) =>
      flatten(diff(m1, m2)).toSet shouldEqual flatten(m1).toSet.diff(flatten(m2).toSet)
    }
  }
} 
Example 12
Source File: CommonSpec.scala    From iotchain   with MIT License 5 votes vote down vote up
package jbok.common

import cats.effect.{IO, Resource}
import cats.implicits._
import jbok.common.log.{Level, Logger}
import jbok.common.thread.ThreadUtil
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest._
import org.scalatest.concurrent.TimeLimitedTests
import org.scalatest.prop.PropertyChecks
import org.scalatest.time.Span

import scala.concurrent.ExecutionContext
import scala.concurrent.duration._

trait CommonSpec
    extends WordSpecLike
    with Matchers
    with PropertyChecks
    with BeforeAndAfterAll
    with BeforeAndAfterEach
    with TimeLimitedTests
    with CancelAfterFailure
    with CommonArb {

  implicit val cs = IO.contextShift(ExecutionContext.global)

  implicit val timer = IO.timer(ExecutionContext.global)

  implicit val ce = IO.ioConcurrentEffect(cs)

  implicit val acg = ThreadUtil.acgGlobal

  override def timeLimit: Span = 60.seconds

  Logger.setRootHandlers[IO](Logger.consoleHandler(minimumLevel = Some(Level.Info))).unsafeRunSync()

  def withResource[A](res: Resource[IO, A])(f: A => IO[Unit]): Unit =
    res.use(a => f(a)).unsafeRunSync()

  def withIO[A](ioa: IO[A]): Unit =
    ioa.void.unsafeRunSync()

  def random[A](implicit arb: Arbitrary[A]): A =
    arb.arbitrary.sample.get

  def random[A](gen: Gen[A]): A =
    gen.sample.get
}

object CommonSpec extends CommonSpec 
Example 13
Source File: GenericCodecTest.scala    From Fixed-Length   with MIT License 5 votes vote down vote up
package com.github.atais.fixedlength

import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}

class GenericCodecTest extends FlatSpec with Matchers with PropertyChecks {

  implicit val generatorDrivenConf = PropertyCheckConfiguration(minSuccessful = 5000)

  case class Example(a: Char, b: Option[Char], c: Char)

  object Example {

    import Codec._
    import cats.implicits._
    import com.github.atais.util.Read._
    import com.github.atais.util.Write._

    implicit val example1: Codec[Example] = {
      fixed[Char](0, 1) <<:
        fixed[Option[Char]](1, 2) <<:
        fixed[Char](2, 3)
    }.as[Example]
  }

  "Any example class" should "be parsed properly" in {
    forAll { (a: Char, b: Option[Char], c: Char) => {
      if (!(a == ' ' || b == Some(' ') || c == ' ')) {
        val m = Example(a, b, c)
        import Example._

        val stringed = m.a.toString + m.b.getOrElse(" ").toString + m.c.toString
        val encoded = Parser.encode[Example](m)

        encoded shouldEqual stringed

        val decoded = Parser.decode[Example](encoded)
        decoded.right.get shouldEqual m
      }
    }
    }
  }

} 
Example 14
Source File: ReadersTest.scala    From Fixed-Length   with MIT License 5 votes vote down vote up
package com.github.atais.read

import com.github.atais.util.Read._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}

class ReadersTest extends FlatSpec with Matchers with PropertyChecks {

  implicit val generatorDrivenConf = PropertyCheckConfiguration(minSuccessful = 5000)

  "Any short" should "be parsed properly" in {
    forAll { m: Short => test(shortRead.read(m.toString), m) }
  }

  "Any int" should "be parsed properly" in {
    forAll { m: Int => test(intRead.read(m.toString), m) }
  }

  "Any long" should "be parsed properly" in {
    forAll { m: Long => test(longRead.read(m.toString), m) }
  }

  "Any float" should "be parsed properly" in {
    forAll { m: Float => test(floatRead.read(m.toString), m) }
  }

  "Any double" should "be parsed properly" in {
    forAll { m: Double => test(doubleRead.read(m.toString), m) }
  }

  "Any boolean" should "be parsed properly" in {
    forAll { m: Boolean => test(booleanRead.read(m.toString), m) }
  }

  "Any char" should "be parsed properly" in {
    forAll { m: Char => test(charRead.read(m.toString), m) }
  }

  "Any string" should "be parsed properly" in {
    forAll { m: String => test(stringRead.read(m.toString), m) }
  }

  private def test[A](returned: Either[Throwable, A], checkValue: A) = {
    returned match {
      case Right(v) => v shouldEqual checkValue
      case Left(_) => fail()
    }
  }

} 
Example 15
Source File: RLPSpeedSuite.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.rlp

import akka.util.ByteString
import io.iohk.ethereum.ObjectGenerators
import io.iohk.ethereum.domain.Block._
import io.iohk.ethereum.domain._
import io.iohk.ethereum.network.p2p.messages.CommonMessages.SignedTransactions._
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import io.iohk.ethereum.utils.Logger
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.{GeneratorDrivenPropertyChecks, PropertyChecks}
import org.spongycastle.util.encoders.Hex


class RLPSpeedSuite extends FunSuite
    with PropertyChecks
    with GeneratorDrivenPropertyChecks
    with ObjectGenerators
    with Logger {

  val rounds = 10000

  test("Main") {
    val startBlockSerialization: Long = System.currentTimeMillis
    val block = blockGen.sample.get
    val serializedBlock = doTestSerialize[Block](block, (b: Block) => b.toBytes, rounds)
    val elapsedBlockSerialization = (System.currentTimeMillis() - startBlockSerialization) / 1000f
    log.info(s"Block serializations / sec: (${rounds.toFloat / elapsedBlockSerialization})")

    val blockDeserializationStart: Long = System.currentTimeMillis
    val deserializedBlock: Block = doTestDeserialize(serializedBlock, (b: Array[Byte]) => b.toBlock, rounds)
    val elapsedBlockDeserialization = (System.currentTimeMillis() - blockDeserializationStart) / 1000f
    log.info(s"Block deserializations / sec: (${rounds.toFloat / elapsedBlockDeserialization})")

    val serializationTxStart: Long = System.currentTimeMillis
    val tx = validTransaction
    val serializedTx = doTestSerialize(tx, (stx: SignedTransaction) => stx.toBytes, rounds)
    val elapsedTxSerialization = (System.currentTimeMillis() - serializationTxStart) / 1000f
    log.info(s"TX serializations / sec: (${rounds.toFloat / elapsedTxSerialization})")

    val txDeserializationStart: Long = System.currentTimeMillis
    val deserializedTx: SignedTransaction = doTestDeserialize(serializedTx, (b: Array[Byte]) => b.toSignedTransaction, rounds)
    val elapsedTxDeserialization = (System.currentTimeMillis() - txDeserializationStart) / 1000f
    log.info(s"TX deserializations / sec: (${rounds.toFloat / elapsedTxDeserialization})")
  }

  test("Performance decode") {
    val blockRaw: String = "f8cbf8c7a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a02f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817da00000000000000000000000000000000000000000000000000000000000000000834000008080830f4240808080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829c0c0"
    val payload: Array[Byte] = Hex.decode(blockRaw)
    val ITERATIONS: Int = 10000000
    log.info("Starting " + ITERATIONS + " decoding iterations...")
    val start1: Long = System.currentTimeMillis
    (1 to ITERATIONS).foreach { _ => RLP.rawDecode(payload); Unit }
    val end1: Long = System.currentTimeMillis
    log.info("Result decode()\t: " + (end1 - start1) + "ms")
  }

  def doTestSerialize[T](toSerialize: T, encode: T => Array[Byte], rounds: Int): Array[Byte] = {
    (1 until rounds).foreach(_ => {
      encode(toSerialize)
    })
    encode(toSerialize)
  }

  def doTestDeserialize[T](serialized: Array[Byte], decode: Array[Byte] => T, rounds: Int): T = {
    (1 until rounds).foreach(_ => {
      decode(serialized)
    })
    decode(serialized)
  }

  val validTransaction = SignedTransaction(
    Transaction(
      nonce = 172320,
      gasPrice = BigInt("50000000000"),
      gasLimit = 90000,
      receivingAddress = Address(Hex.decode("1c51bf013add0857c5d9cf2f71a7f15ca93d4816")),
      value = BigInt("1049756850000000000"),
      payload = ByteString.empty),
    pointSign = 28,
    signatureRandom = ByteString(Hex.decode("cfe3ad31d6612f8d787c45f115cc5b43fb22bcc210b62ae71dc7cbf0a6bea8df")),
    signature = ByteString(Hex.decode("57db8998114fae3c337e99dbd8573d4085691880f4576c6c1f6c5bbfe67d6cf0")),
    chainId = 0x3d.toByte
  ).get

  lazy val blockGen: Gen[Block] = for {
    header <- blockHeaderGen
    uncles <- blockHeaderGen
  } yield Block(header = header, BlockBody(transactionList = List.fill(10)(validTransaction), uncleNodesList = Seq(uncles)))
} 
Example 16
Source File: MerklePatriciaTreeSpeedSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.mpt

import io.iohk.ethereum.{ObjectGenerators, crypto}
import io.iohk.ethereum.db.dataSource.EphemDataSource
import io.iohk.ethereum.db.storage.{ArchiveNodeStorage, NodeStorage}
import io.iohk.ethereum.mpt.MerklePatriciaTrie.defaultByteArraySerializable
import io.iohk.ethereum.utils.Logger
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks
import org.spongycastle.util.encoders.Hex

class MerklePatriciaTreeSpeedSpec extends FunSuite
  with PropertyChecks
  with ObjectGenerators
  with Logger
  with PersistentStorage {

  test("Performance test (From: https://github.com/ethereum/wiki/wiki/Benchmarks)") {
    val Rounds = 1000
    val Symmetric = true

    val start: Long = System.currentTimeMillis
    val emptyTrie = MerklePatriciaTrie[Array[Byte], Array[Byte]](new ArchiveNodeStorage(new NodeStorage(EphemDataSource())))
    var seed: Array[Byte] = Array.fill(32)(0.toByte)

    val trieResult = (0 until Rounds).foldLeft(emptyTrie) { case (recTrie, i) =>
      seed = Node.hashFn(seed)
      if (!Symmetric) recTrie.put(seed, seed)
      else {
        val mykey = seed
        seed = Node.hashFn(seed)
        val myval = if ((seed(0) & 0xFF) % 2 == 1) Array[Byte](seed.last) else seed
        recTrie.put(mykey, myval)
      }
    }
    val rootHash = Hex.toHexString(trieResult.getRootHash)

    log.debug("Time taken(ms): " + (System.currentTimeMillis - start))
    log.debug("Root hash obtained: " + rootHash)

    if (Symmetric) assert(rootHash.take(4) == "36f6" && rootHash.drop(rootHash.length - 4) == "93a3")
    else assert(rootHash.take(4) == "da8a" && rootHash.drop(rootHash.length - 4) == "0ca4")
  }

  test("MPT benchmark") {
    withNodeStorage { ns =>
      val hashFn = crypto.kec256(_: Array[Byte])

      val defaultByteArraySer = MerklePatriciaTrie.defaultByteArraySerializable
      val EmptyTrie = MerklePatriciaTrie[Array[Byte], Array[Byte]](ns)(defaultByteArraySer, defaultByteArraySer)

      var t = System.currentTimeMillis()
      (1 to 20000000).foldLeft(EmptyTrie){case (trie, i) =>
        val k = hashFn(("hello" + i).getBytes)
        val v = hashFn(("world" + i).getBytes)

        if (i % 100000 == 0) {
          val newT = System.currentTimeMillis()
          val delta = (newT - t) / 1000.0
          t = newT
          log.debug(s"=== $i elements put, time for batch is: $delta sec")
        }
        trie.put(k, v)
      }
    }
  }
} 
Example 17
Source File: CallOpcodesSpecPostEip161.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.vm

import io.iohk.ethereum.domain.UInt256
import io.iohk.ethereum.vm.MockWorldState._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, WordSpec}

// scalastyle:off object.name
class CallOpcodesSpecPostEip161 extends WordSpec with Matchers with PropertyChecks {

  val config = EvmConfig.PostEIP161ConfigBuilder(None)
  val startState = MockWorldState(touchedAccounts = Set.empty, noEmptyAccountsCond = true)
  import config.feeSchedule._

  val fxt = new CallOpFixture(config, startState)

  "CALL" when {

    "call depth limit is reached" should {
      val context: PC = fxt.context.copy(env = fxt.env.copy(callDepth = EvmConfig.MaxCallDepth))
      val call = fxt.CallResult(op = CALL, context = context)

      "not modify world state" in {
        call.world shouldEqual fxt.worldWithExtAccount
      }
    }

    "call value is greater than balance" should {

      val call = fxt.CallResult(op = CALL, value = fxt.initialBalance + 1)

      "not modify world state" in {
        call.world shouldEqual fxt.worldWithExtAccount
      }
    }

    "external contract terminates abnormally" should {

      val context: PC = fxt.context.copy(world = fxt.worldWithInvalidProgram)
      val call = fxt.CallResult(op = CALL, context)

      "modify only touched accounts in world state" in {
        call.world shouldEqual fxt.worldWithInvalidProgram.touchAccounts(fxt.ownerAddr, fxt.extAddr)
      }
    }

    "calling an empty" should {

      val contextEmptyAccount: PC = fxt.context.copy(world = fxt.worldWithExtEmptyAccount)
      val callEmptyAccount = fxt.CallResult(op = CALL, contextEmptyAccount)
      val callZeroTransfer = fxt.CallResult(op = CALL, contextEmptyAccount, value = UInt256.Zero)

      "consume correct gas (refund call gas, add new account modifier) when transferring value to Empty Account" in {
        val expectedGas = G_call + G_callvalue + G_newaccount - G_callstipend + fxt.expectedMemCost
        callEmptyAccount.stateOut.gasUsed shouldEqual expectedGas
        callEmptyAccount.world.touchedAccounts.size shouldEqual 2
      }

      "consume correct gas when transferring no value to Empty Account" in {
        val expectedGas = G_call + fxt.expectedMemCost
        callZeroTransfer.stateOut.gasUsed shouldEqual expectedGas
        callZeroTransfer.world.touchedAccounts.size shouldEqual 2
      }
    }
  }
} 
Example 18
Source File: ProgramSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.vm

import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}
import Generators._
import akka.util.ByteString
import org.scalacheck.Gen

class ProgramSpec extends FlatSpec with Matchers with PropertyChecks {

  val CodeSize = Byte.MaxValue
  val PositionsSize = 10

  val nonPushOp: Byte = JUMP.code
  val invalidOpCode: Byte = 0xef.toByte

  def positionsSetGen: Gen[Set[Int]] = getListGen(minSize = 0, maxSize = PositionsSize, genT = intGen(0, CodeSize)).map(_.toSet)

  it should "detect all jump destinations if there are no push op" in {
    forAll(positionsSetGen) { jumpDestLocations =>
      val code = ByteString((0 to CodeSize).map{ i =>
        if(jumpDestLocations.contains(i)) JUMPDEST.code
        else nonPushOp
      }.toArray)
      val program = Program(code)
      program.validJumpDestinations shouldBe jumpDestLocations
    }
  }

  it should "detect all jump destinations if there are push op" in {
    forAll(positionsSetGen, positionsSetGen) { (jumpDestLocations, pushOpLocations) =>
      val code = ByteString((0 to CodeSize).map{ i =>
        if(jumpDestLocations.contains(i)) JUMPDEST.code
        else if (pushOpLocations.contains(i)) PUSH1.code
        else nonPushOp
      }.toArray)
      val program = Program(code)

      //Removing the PUSH1 that would be used as a parameter of another PUSH1
      //  Example: In "PUSH1 PUSH1 JUMPDEST", the JUMPDEST is a valid jump destination
      val pushOpLocationsNotParameters = (pushOpLocations diff jumpDestLocations).toList.sorted
        .foldLeft(List.empty[Int]){case (recPushOpLocations, i) =>
          if(recPushOpLocations.lastOption.contains(i - 1)) recPushOpLocations else recPushOpLocations :+ i
        }

      val jumpDestLocationsWithoutPushBefore = jumpDestLocations
        .filterNot(i => pushOpLocationsNotParameters.contains(i - 1))
        .filter(i => 0 <= i && i <= CodeSize)
      program.validJumpDestinations shouldBe jumpDestLocationsWithoutPushBefore
    }
  }

  it should "detect all jump destinations if there are invalid ops" in {
    forAll(positionsSetGen, positionsSetGen) { (jumpDestLocations, invalidOpLocations) =>
      val code = ByteString((0 to CodeSize).map{ i =>
        if(jumpDestLocations.contains(i)) JUMPDEST.code
        else if (invalidOpLocations.contains(i)) invalidOpCode
        else nonPushOp
      }.toArray)
      val program = Program(code)
      program.validJumpDestinations shouldBe jumpDestLocations
    }
  }

  it should "detect all instructions as jump destinations if they are" in {
    val code = ByteString((0 to CodeSize).map(_ => JUMPDEST.code).toArray)
    val program = Program(code)
    program.validJumpDestinations shouldBe (0 to CodeSize).toSet
  }
} 
Example 19
Source File: StackSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.vm

import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FunSuite, Matchers}
import io.iohk.ethereum.domain.UInt256

class StackSpec extends FunSuite with Matchers with PropertyChecks {

  val maxStackSize = 32
  val stackGen = Generators.getStackGen(maxSize = maxStackSize)
  val intGen = Gen.choose(0, maxStackSize).filter(_ >= 0)
  val uint256Gen = Generators.getUInt256Gen()
  val uint256ListGen = Generators.getListGen(0, 16, uint256Gen)

  test("pop single element") {
    forAll(stackGen) { stack =>
      val (v, stack1) = stack.pop
      if (stack.size > 0) {
        v shouldEqual stack.toSeq.head
        stack1.toSeq shouldEqual stack.toSeq.tail
      } else {
        v shouldEqual 0
        stack1 shouldEqual stack
      }
    }
  }

  test("pop multiple elements") {
    forAll(stackGen, intGen) { (stack, i) =>
      val (vs, stack1) = stack.pop(i)
      if (stack.size >= i) {
        vs shouldEqual stack.toSeq.take(i)
        stack1.toSeq shouldEqual stack.toSeq.drop(i)
      } else {
        vs shouldEqual Seq.fill(i)(UInt256.Zero)
        stack1 shouldEqual stack
      }
    }
  }

  test("push single element") {
    forAll(stackGen, uint256Gen) { (stack, v) =>
      val stack1 = stack.push(v)

      if (stack.size < stack.maxSize) {
        stack1.toSeq shouldEqual (v +: stack.toSeq)
      } else {
        stack1 shouldEqual stack
      }
    }
  }

  test("push multiple elements") {
    forAll(stackGen, uint256ListGen) { (stack, vs) =>
      val stack1 = stack.push(vs)

      if (stack.size + vs.size <= stack.maxSize) {
        stack1.toSeq shouldEqual (vs.reverse ++ stack.toSeq)
      } else {
        stack1 shouldEqual stack
      }
    }
  }

  test("duplicate element") {
    forAll(stackGen, intGen) { (stack, i) =>
      val stack1 = stack.dup(i)

      if (i < stack.size && stack.size < stack.maxSize) {
        val x = stack.toSeq(i)
        stack1.toSeq shouldEqual (x +: stack.toSeq)
      } else {
        stack1 shouldEqual stack
      }
    }
  }

  test("swap elements") {
    forAll(stackGen, intGen) { (stack, i) =>
      val stack1 = stack.swap(i)

      if (i < stack.size) {
        val x = stack.toSeq.head
        val y = stack.toSeq(i)
        stack1.toSeq shouldEqual stack.toSeq.updated(0, y).updated(i, x)
      } else {
        stack1 shouldEqual stack
      }
    }
  }

} 
Example 20
Source File: OpCodeGasSpecPostEip161.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.vm

import io.iohk.ethereum.domain.UInt256._
import io.iohk.ethereum.domain.{Account, Address}
import io.iohk.ethereum.vm.Generators._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FunSuite, Matchers}

class OpCodeGasSpecPostEip161 extends FunSuite with OpCodeTesting with Matchers with PropertyChecks {

  override val config = EvmConfig.PostEIP161ConfigBuilder(None)

  import config.feeSchedule._

  test(SELFDESTRUCT) { op =>
    val stateGen = getProgramStateGen(
      stackGen = getStackGen(elems = 1),
      evmConfig = config
    )

    // Sending refund to a non-existent account
    forAll(stateGen) { stateIn =>
      val (refund, _) = stateIn.stack.pop
      whenever(stateIn.world.getAccount(Address(refund)).isEmpty && stateIn.ownBalance > 0) {
        val stateOut = op.execute(stateIn)
        stateOut.gasRefund shouldEqual R_selfdestruct
        verifyGas(G_selfdestruct + G_newaccount, stateIn, stateOut)
      }
    }

    // Sending refund to an already existing account not dead account
    forAll(stateGen) { (stateIn) =>
      val (refund, _) = stateIn.stack.pop
      val world = stateIn.world.saveAccount(
        Address(refund),
        Account.empty().increaseNonce())
      val updatedStateIn = stateIn.withWorld(world)
      val stateOut = op.execute(updatedStateIn)
      verifyGas(G_selfdestruct, updatedStateIn, stateOut)
      stateOut.gasRefund shouldEqual R_selfdestruct
    }

    // Owner account was already selfdestructed
    forAll(stateGen) { stateIn =>
      val (refund, _) = stateIn.stack.pop
      whenever(stateIn.world.getAccount(Address(refund)).isEmpty && stateIn.ownBalance > 0) {
        val updatedStateIn = stateIn.withAddressToDelete(stateIn.context.env.ownerAddr)
        val stateOut = op.execute(updatedStateIn)
        verifyGas(G_selfdestruct + G_newaccount, updatedStateIn, stateOut)
        stateOut.gasRefund shouldEqual 0
      }
    }
  }
} 
Example 21
Source File: NodeParserSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.network

import io.iohk.ethereum.network.discovery.NodeParser
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}

class NodeParserSpec extends FlatSpec with Matchers with PropertyChecks {

  it should "correctly parse IPv4 nodes" in {
    val testVectors = Table[String, Boolean](
      ("nodes", "isValid"),

      ("enode://fba5a07e283d517a2680bcfc7aeb498ac2d246d756556a2ebd5edeb39496491c47a6d27e27f82833b7d7d12defc8de994de04bb58beb72472649f9a323006820@41.135.121.6:30303",
        true),
      (" enode://09c4a2fec7daed400e5e28564e23693b23b2cc5a019b612505631bbe7b9ccf709c1796d2a3d29ef2b045f210caf51e3c4f5b6d3587d43ad5d6397526fa6179@174.112.32.157:30303",
        false), //Has invalid ' ' character
      ("http://6e538e7c1280f0a31ff08b382db5302480f775480b8e68f8febca0ceff81e4b19153c6f8bf60313b93bef2cc34d34e1df41317de0ce613a201d1660a788a03e2@52.206.67.235:30303",
        false), //Has invalid scheme
      ("enode://5fbfb426fbb46f8b8c1bd3dd140f5b511da558cd37d60844b525909ab82e13a25ee722293c829e52cb65c2305b1637fa9a2ea4d6634a224d5f400bfe244ac0de@162-243-55-45:30303",
        false), //Has invalid IP format
      ("enode://a5a07e283d517a2680bcfc7aeb498ac2d246d756556a2ebd5edeb39496491c47a6d27e27f82833b7d7d12defc8de994de04bb58beb72472649f9a323006820@41.135.121.6:30303",
        false), //Has invalid node id size
      ("enode://zba5a07e283d517a2680bcfc7aeb498ac2d246d756556a2ebd5edeb39496491c47a6d27e27f82833b7d7d12defc8de994de04bb58beb72472649f9a323006820@41.135.121.6:30303",
        false), //Node id has invalid 'z' character
      ("enode://@41.135.121.6:30303",
        false), //Has no node id
      ("enode://41.135.121.6:30303", false), //Has no node id
      ("enode://fba5a07e283d517a2680bcfc7aeb498ac2d246d756556a2ebd5edeb39496491c47a6d27e27f82833b7d7d12defc8de994de04bb58beb72472649f9a323006820@:30303",
        false), //Has no IP
      ("enode://fba5a07e283d517a2680bcfc7aeb498ac2d246d756556a2ebd5edeb39496491c47a6d27e27f82833b7d7d12defc8de994de04bb58beb72472649f9a323006820@41.135.121.6:",
        false), //Has no port
      ("", false) //Empty node string
    )

    forAll(testVectors) { case (nodeString, valid) =>
        val node = NodeParser.parseNode(nodeString)
        node.isRight shouldEqual valid
        if(valid)
          node.toOption.get.toUri.toString shouldBe nodeString
    }
  }

  it should "correctly parse IPv6 nodes" in {
    val testVectors = Table[String, Option[String]](
      ("nodes", "expectedOutput"),

      ("enode://c94b6f71c2f3d84ed5587ff936172138cfd4af4951e4ca784b9ea5330f76ed8d77d23a7178b18716947a17a8ef59f18519bc0064e7f3f12e0c1c5934cac147a0@[ce90:c2c:7000:0:10:0:0:0]:30303",
        Some("enode://c94b6f71c2f3d84ed5587ff936172138cfd4af4951e4ca784b9ea5330f76ed8d77d23a7178b18716947a17a8ef59f18519bc0064e7f3f12e0c1c5934cac147a0@[ce90:c2c:7000:0:10:0:0:0]:30303")
      ), //Has full IPv6 address
      ("enode://c94b6f71c2f3d84ed5587ff936172138cfd4af4951e4ca784b9ea5330f76ed8d77d23a7178b18716947a17a8ef59f18519bc0064e7f3f12e0c1c5934cac147a0@[ce90:c2c:7000:0:10::]:30303",
        Some("enode://c94b6f71c2f3d84ed5587ff936172138cfd4af4951e4ca784b9ea5330f76ed8d77d23a7178b18716947a17a8ef59f18519bc0064e7f3f12e0c1c5934cac147a0@[ce90:c2c:7000:0:10:0:0:0]:30303")
      ), //Has partial IPv6 address
      ("enode://c94b6f71c2f3d84ed5587ff936172138cfd4af4951e4ca784b9ea5330f76ed8d77d23a7178b18716947a17a8ef59f18519bc0064e7f3f12e0c1c5934cac147a0@[::]:30303",
        Some("enode://c94b6f71c2f3d84ed5587ff936172138cfd4af4951e4ca784b9ea5330f76ed8d77d23a7178b18716947a17a8ef59f18519bc0064e7f3f12e0c1c5934cac147a0@[0:0:0:0:0:0:0:0]:30303")
      ), //Has partial localhost IPv6 address
      ("enode://c94b6f71c2f3d84ed5587ff936172138cfd4af4951e4ca784b9ea5330f76ed8d77d23a7178b18716947a17a8ef59f18519bc0064e7f3f12e0c1c5934cac147a0@[0:0:0]:30303",
        None
      ) //Has short localhost IPv6 address
    )

    forAll(testVectors) { case (nodeString, maybeExpectedOutput) =>
      val node = NodeParser.parseNode(nodeString)
      node.isRight shouldEqual maybeExpectedOutput.nonEmpty
      if(maybeExpectedOutput.nonEmpty)
        node.toOption.get.toUri.toString shouldBe maybeExpectedOutput.get
    }
  }

} 
Example 22
Source File: NewBlockSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.network.p2p.messages

import akka.util.ByteString
import io.iohk.ethereum.ObjectGenerators
import io.iohk.ethereum.domain.{Block, BlockHeader}
import io.iohk.ethereum.network.p2p.messages.CommonMessages.NewBlock
import io.iohk.ethereum.network.p2p.messages.PV62.BlockBody
import org.scalatest.prop.PropertyChecks
import org.scalatest.FunSuite
import org.spongycastle.util.encoders.Hex
import NewBlock._
import io.iohk.ethereum.nodebuilder.SecureRandomBuilder

class NewBlockSpec extends FunSuite with PropertyChecks  with ObjectGenerators with SecureRandomBuilder {

  val chainId = Hex.decode("3d").head

  test("NewBlock messages are encoded and decoded properly") {
    forAll(newBlockGen(secureRandom, Some(chainId))) { newBlock =>
      val encoded: Array[Byte] = newBlock.toBytes
      val decoded: NewBlock = encoded.toNewBlock
      assert(decoded == newBlock)
    }
  }

  //Expected encoded NewBlock obtained from EthereumJ
  test("NewBlock messages are properly encoded") {
    val obtainEncoded = Hex.toHexString(newBlock.toBytes)
    val expectedEncoded = "f90200f901f9f901f4a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347943333333333333333333333333333333333333333a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830f0000808408000000808000a0000000000000000000000000000000000000000000000000000000000000000088deadbeefdeadbeefc0c0830f0000"
    assert(obtainEncoded == expectedEncoded)

    val obtainEncoded2 = Hex.toHexString(newBlock2.toBytes)
    val expectedEncoded2 = "f9021cf90215f90210a098352d9c1300bd82334cb3e5034c3ec622d437963f55cf5a00a49642806c2f32a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942cad6e80c7c0b58845fcd71ecad6867c3bd4de20a09b56d589ad6a12373e212fdb6cb4f64d1d7745aea551c7116c665a81a31f9492a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830f0b3f0a8407ec167f808458a6eb7098d783010507846765746887676f312e372e33856c696e7578a0ea0dec34a635401af44f5245a77b2cd838345615c555c322a3001df4dd0505fe8860d53a11c10d46fbc0c0830f0b3f"
    assert(obtainEncoded2 == expectedEncoded2)
  }

  val newBlock = NewBlock(
    Block(
      BlockHeader(
        parentHash = ByteString(Hex.decode("0000000000000000000000000000000000000000000000000000000000000000")),
        ommersHash = ByteString(Hex.decode("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")),
        beneficiary = ByteString(Hex.decode("3333333333333333333333333333333333333333")),
        stateRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
        transactionsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
        receiptsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
        logsBloom = ByteString(Hex.decode("00" * 256)),
        difficulty = BigInt("983040"),
        number = 0,
        gasLimit = 134217728,
        gasUsed = 0,
        unixTimestamp = 0,
        extraData = ByteString(Hex.decode("00")),
        mixHash = ByteString(Hex.decode("00" * 32)),
        nonce = ByteString(Hex.decode("deadbeefdeadbeef"))
      ),
      BlockBody(Seq(), Seq())
    ),
    BigInt("983040")
  )

  val newBlock2 = NewBlock(
    Block(
      BlockHeader(
        parentHash = ByteString(Hex.decode("98352d9c1300bd82334cb3e5034c3ec622d437963f55cf5a00a49642806c2f32")),
        ommersHash = ByteString(Hex.decode("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")),
        beneficiary = ByteString(Hex.decode("2cad6e80c7c0b58845fcd71ecad6867c3bd4de20")),
        stateRoot = ByteString(Hex.decode("9b56d589ad6a12373e212fdb6cb4f64d1d7745aea551c7116c665a81a31f9492")),
        transactionsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
        receiptsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
        logsBloom = ByteString(Hex.decode("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")),
        difficulty = BigInt("985919"),
        number = 10,
        gasLimit = 132912767,
        gasUsed = 0,
        unixTimestamp = 1487334256,
        extraData = ByteString(Hex.decode("d783010507846765746887676f312e372e33856c696e7578")),
        mixHash = ByteString(Hex.decode("ea0dec34a635401af44f5245a77b2cd838345615c555c322a3001df4dd0505fe")),
        nonce = ByteString(Hex.decode("60d53a11c10d46fb"))
      ),
      BlockBody(Seq(), Seq())
    ), BigInt("985919")
  )
} 
Example 23
Source File: HexPrefixSuite.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.mpt

import io.iohk.ethereum.ObjectGenerators
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class HexPrefixSuite extends FunSuite
  with PropertyChecks
  with ObjectGenerators {

  test("HexPrefix encoding"){
    forAll(hexPrefixDecodeParametersGen()) { pair: (Array[Byte], Boolean) =>
      val (bytes, t) = pair
      val nibbles = HexPrefix.bytesToNibbles(bytes = bytes)
      val packed = HexPrefix.encode(nibbles, t)
      val (unpacked, b) = HexPrefix.decode(packed)
      val bytesObtained = HexPrefix.nibblesToBytes(unpacked)
      assert(bytes sameElements bytesObtained)
      assert(b==t)
    }
  }

  test("testCompactEncodeOddCompact"){
    val test: Array[Byte] = Array[Byte](1, 2, 3, 4, 5)
    val expectedData: Array[Byte] = Array[Byte](0x11, 0x23, 0x45)
    assert(expectedData sameElements HexPrefix.encode(test, isLeaf = false))
  }

  test("testCompactEncodeEvenCompact"){
    val test: Array[Byte] = Array[Byte](0, 1, 2, 3, 4, 5)
    val expectedData: Array[Byte] = Array[Byte](0x00, 0x01, 0x23, 0x45)
    assert(expectedData sameElements HexPrefix.encode(test, isLeaf = false))
  }

  test("testCompactEncodeEvenTerminated"){
    val test: Array[Byte] = Array[Byte](0, 15, 1, 12, 11, 8)
    val expectedData: Array[Byte] = Array[Byte](0x20, 0x0f, 0x1c, 0xb8.toByte)
    assert(expectedData sameElements HexPrefix.encode(test, isLeaf = true))
  }

  test("testCompactEncodeOddTerminated"){
    val test: Array[Byte] = Array[Byte](15, 1, 12, 11, 8)
    val expectedData: Array[Byte] = Array[Byte](0x3f, 0x1c, 0xb8.toByte)
    assert(expectedData sameElements HexPrefix.encode(test, isLeaf = true))
  }

  test("testCompactDecodeOddCompact"){
    val test: Array[Byte] = Array[Byte](0x11, 0x23, 0x45)
    val expected: Array[Byte] = Array[Byte](1, 2, 3, 4, 5)
    val (obtained, t) = HexPrefix.decode(test)
    assert(expected sameElements obtained)
    assert(!t)
  }

  test("testCompactDecodeEvenCompact"){
    val test: Array[Byte] = Array[Byte](0x00, 0x01, 0x23, 0x45)
    val expected: Array[Byte] = Array[Byte](0, 1, 2, 3, 4, 5)
    val (obtained, t) = HexPrefix.decode(test)
    assert(expected sameElements obtained)
    assert(!t)
  }

  test("testCompactDecodeEvenTerminated"){
    val test: Array[Byte] = Array[Byte](0x20, 0x0f, 0x1c, 0xb8.toByte)
    val expected: Array[Byte] = Array[Byte](0, 15, 1, 12, 11, 8)
    val (obtained, t) = HexPrefix.decode(test)
    assert(expected sameElements obtained)
    assert(t)
  }

  test("testCompactDecodeOddTerminated"){
    val test: Array[Byte] = Array[Byte](0x3f, 0x1c, 0xb8.toByte)
    val expected: Array[Byte] = Array[Byte](15, 1, 12, 11, 8)
    val (obtained, t) = HexPrefix.decode(test)
    assert(expected sameElements obtained)
    assert(t)
  }

  test("testCompactHexEncode_1"){
    val test: Array[Byte] = "stallion".getBytes
    val result: Array[Byte] = Array[Byte](7, 3, 7, 4, 6, 1, 6, 12, 6, 12, 6, 9, 6, 15, 6, 14)
    assert(result sameElements HexPrefix.bytesToNibbles(bytes = test))
  }

  test("testCompactHexEncode_2"){
    val test: Array[Byte] = "verb".getBytes
    val result: Array[Byte] = Array[Byte](7, 6, 6, 5, 7, 2, 6, 2)
    assert(result sameElements HexPrefix.bytesToNibbles(bytes = test))
  }

  test("testCompactHexEncode_3"){
    val test: Array[Byte] = "puppy".getBytes
    val result: Array[Byte] = Array[Byte](7, 0, 7, 5, 7, 0, 7, 0, 7, 9)
    assert(result sameElements HexPrefix.bytesToNibbles(bytes = test))
  }
} 
Example 24
Source File: Pbkdf2HMacSha256Spec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.crypto

import java.nio.charset.StandardCharsets

import akka.util.ByteString
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}
import org.spongycastle.util.encoders.Hex

class Pbkdf2HMacSha256Spec extends FlatSpec with Matchers with PropertyChecks {

  "pbkdf2HMacSha256" should "correctly evaluate for the test vectors" in {

    // https://stackoverflow.com/a/5136918
    val testVectors = Table[String, String, Int, Int, String](
      ("passphrase", "salt", "c", "dklen", "derivedKey"),

      ("password", "salt", 1, 32,
        "120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b"),

      ("password", "salt", 2, 32,
        "ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43"),

      ("password", "salt", 4096, 32,
        "c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a"),

      // takes a bit too long to be run on the CI
      // leaving this around as it's a valid test if we want to examine this function in the future
      // ("password", "salt", 16777216, 32,
      //  "cf81c66fe8cfc04d1f31ecb65dab4089f7f179e89b3b0bcb17ad10e3ac6eba46"),

      ("passwordPASSWORDpassword", "saltSALTsaltSALTsaltSALTsaltSALTsalt", 4096, 40,
        "348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9"),

      ("pass\u0000word", "sa\u0000lt", 4096, 16,
        "89b69d0516f829893c696226650a8687")
    )

    forAll(testVectors) { (pass, s, c, dklen, dk) =>
      val salt = ByteString(s.getBytes(StandardCharsets.US_ASCII))
      val derivedKey = ByteString(Hex.decode(dk))

      pbkdf2HMacSha256(pass, salt, c, dklen) shouldEqual derivedKey
    }
  }
} 
Example 25
Source File: AesCbcSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.crypto

import java.nio.charset.StandardCharsets

import akka.util.ByteString
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.prop.PropertyChecks
import org.spongycastle.util.encoders.Hex

class AesCbcSpec extends FlatSpec with Matchers with PropertyChecks {

  "AES_CBC" should "correctly evaluate for the test vectors" in {

    // https://tools.ietf.org/html/rfc3602#section-4
    val testVectors = Table[String, String, ByteString, String](
      ("key", "iv", "plaintext", "ciphertext"),

      ("06a9214036b8a15b512e03d534120006",
        "3dafba429d9eb430b422da802c9fac41",
        ByteString("Single block msg".getBytes(StandardCharsets.US_ASCII)),
        "e353779c1079aeb82708942dbe77181a"),

      ("c286696d887c9aa0611bbb3e2025a45a",
        "562e17996d093d28ddb3ba695a2e6f58",
        ByteString(Hex.decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")),
        "d296cd94c2cccf8a3a863028b5e1dc0a7586602d253cfff91b8266bea6d61ab1"),

      ("6c3ea0477630ce21a2ce334aa746c2cd",
        "c782dc4c098c66cbd9cd27d825682c81",
        ByteString("This is a 48-byte message (exactly 3 AES blocks)".getBytes(StandardCharsets.US_ASCII)),
        "d0a02b3836451753d493665d33f0e8862dea54cdb293abc7506939276772f8d5021c19216bad525c8579695d83ba2684"),

      ("56e47a38c5598974bc46903dba290349",
        "8ce82eefbea0da3c44699ed7db51b7d9",
        ByteString(Hex.decode("a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf")),
        "c30e32ffedc0774e6aff6af0869f71aa0f3af07a9a31a9c684db207eb0ef8e4e35907aa632c3ffdf868bb7b29d3d46ad83ce9f9a102ee99d49a53e87f4c3da55")
    )

    forAll(testVectors) { (k, i, plaintext, c) =>
      val key = ByteString(Hex.decode(k))
      val iv = ByteString(Hex.decode(i))
      val ciphertext = ByteString(Hex.decode(c))

      val encrypted = AES_CBC.encrypt(key, iv, plaintext)

      // all the encrypted data in our test vectors is aligned to the block size so for each plaintext message
      // a padding of size equal to block size will be added (and needs to be removed for comparison)
      encrypted.dropRight(key.length) shouldEqual ciphertext

      AES_CBC.decrypt(key, iv, encrypted) shouldEqual Some(plaintext)
    }
  }

  it should "decrypt encrypted random values" in {
    val keyGen = Generators.getByteStringGen(16, 16)
    val ivGen = Generators.getByteStringGen(16, 16)
    val plaintextGen = Generators.getByteStringGen(1, 256)

    forAll(keyGen, ivGen, plaintextGen) { (key, iv, plaintext) =>
      val encrypted = AES_CBC.encrypt(key, iv, plaintext)
      val decrypted = AES_CBC.decrypt(key, iv, encrypted)
      decrypted shouldEqual Some(plaintext)
    }
  }
} 
Example 26
Source File: ScryptSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.crypto

import java.nio.charset.StandardCharsets

import akka.util.ByteString
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.prop.PropertyChecks
import org.spongycastle.util.encoders.Hex

class ScryptSpec extends FlatSpec with Matchers with PropertyChecks {

  "scrypt" should "correctly evaluate for the test vectors" in {

    // https://datatracker.ietf.org/doc/rfc7914/?include_text=1
    val testVectors = Table[String, String, Int, Int, Int, Int, String](
      ("passphrase", "salt", "n", "r", "p", "dklen", "derivedKey"),
      ("", "", 16, 1, 1, 64,
        "77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906"),

      ("password", "NaCl", 1024, 8, 16, 64,
        "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640"),

      // takes a bit too long to be run on the CI
      // leaving this around as it's a valid test if we want to examine this function in the future
      // ("pleaseletmein", "SodiumChloride", 1048576, 8, 1, 64,
      //   "2101cb9b6a511aaeaddbbe09cf70f881ec568d574a2ffd4dabe5ee9820adaa478e56fd8f4ba5d09ffa1c6d927c40f4c337304049e8a952fbcbf45c6fa77a41a4"),

      ("pleaseletmein", "SodiumChloride", 16384, 8, 1, 64,
        "7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887")
    )

    forAll(testVectors) { (pass, s, n, r, p, dklen, dk) =>
      val salt = ByteString(s.getBytes(StandardCharsets.US_ASCII))
      val derivedKey = ByteString(Hex.decode(dk))

      scrypt(pass, salt, n, r, p, dklen) shouldEqual derivedKey
    }
  }
} 
Example 27
Source File: AesCtrSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.crypto

import akka.util.ByteString
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.prop.PropertyChecks
import org.spongycastle.util.encoders.Hex

class AesCtrSpec extends FlatSpec with Matchers with PropertyChecks {

  "AES_CTR" should "correctly evaluate for the test vectors" in {


    // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf Appendix F.5
    val testVectors = Table[String, String, String, String](
      ("key", "iv", "plaintext", "ciphertext"),

      ("2b7e151628aed2a6abf7158809cf4f3c",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
        "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"),

      ("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
        "1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e941e36b26bd1ebc670d1bd1d665620abf74f78a7f6d29809585a97daec58c6b050"),

      ("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710",
        "601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c52b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6")
    )

    forAll(testVectors) { (k, i, p, c) =>
      val key = ByteString(Hex.decode(k))
      val iv = ByteString(Hex.decode(i))
      val plaintext = ByteString(Hex.decode(p))
      val ciphertext = ByteString(Hex.decode(c))

      AES_CTR.encrypt(key, iv, plaintext) shouldEqual ciphertext
      AES_CTR.decrypt(key, iv, ciphertext) shouldEqual Some(plaintext)
    }
  }

  it should "decrypt encrypted random values" in {
    val keyGen = Generators.getByteStringGen(16, 16)
    val ivGen = Generators.getByteStringGen(16, 16)
    val plaintextGen = Generators.getByteStringGen(1, 256)

    forAll(keyGen, ivGen, plaintextGen) { (key, iv, plaintext) =>
      val encrypted = AES_CTR.encrypt(key, iv, plaintext)
      val decrypted = AES_CTR.decrypt(key, iv, encrypted)
      decrypted shouldEqual Some(plaintext)
    }
  }
} 
Example 28
Source File: Ripemd160Spec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.crypto

import java.nio.charset.StandardCharsets

import org.scalatest.{FunSuite, Matchers}
import org.scalatest.prop.PropertyChecks
import org.spongycastle.util.encoders.Hex

class Ripemd160Spec extends FunSuite with PropertyChecks with Matchers {

  // these examples were taken from http://homes.esat.kuleuven.be/~bosselae/ripemd160.html#Outline
  val examples = Table[String, String](("input", "result"),
    ("", "9c1185a5c5e9fc54612808977ee8f548b2258d31"),
    ("a", "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"),
    ("abc", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"),
    ("message digest", "5d0689ef49d2fae572b881b123a85ffa21595f36"),
    ("abcdefghijklmnopqrstuvwxyz", "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"),
    ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "12a053384a9c0c88e405a06c27dcf49ada62eb2b"),
    ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "b0e20b6e3116640286ed3a87a5713079b21f5189"),
    ("1234567890" * 8, "9b752e45573d4b39f4dbd3323cab82bf63326bfb"),
    ("a" * 1000000, "52783243c1697bdbe16d37f97f68f08325dc1528")
  )

  test("RIPEMD-160") {
    forAll(examples) { (input, result) =>
      val inBytes = input.getBytes(StandardCharsets.US_ASCII)
      val hash = ripemd160(inBytes)
      val encodedHash = Hex.toHexString(hash)

      encodedHash shouldEqual result
    }
  }

} 
Example 29
Source File: ECDSASignatureSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.crypto

import akka.util.ByteString
import io.iohk.ethereum.nodebuilder.SecureRandomBuilder
import org.scalacheck.Arbitrary
import org.scalacheck.Arbitrary.arbitrary
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}
import org.spongycastle.crypto.params.ECPublicKeyParameters
import org.spongycastle.util.encoders.Hex

class ECDSASignatureSpec extends FlatSpec with Matchers with PropertyChecks with SecureRandomBuilder {
  "ECDSASignature" should "recover public key correctly for go ethereum transaction" in {
    val bytesToSign = Hex.decode("5a1465f4683bf2c18fc72c0789239c0f52b3ceac666ca9551cf265a11abe912c")
    val signatureRandom = ByteString(Hex.decode("f3af65a23fbf207b933d3c962381aa50e0ac19649c59c1af1655e592a8d95401"))
    val signature = ByteString(Hex.decode("53629a403579f5ce57bcbefba2616b1c6156d308ddcd37372c94943fdabeda97"))
    val pointSign = 28

    val sig = ECDSASignature(BigInt(1, signatureRandom.toArray[Byte]), BigInt(1, signature.toArray[Byte]), pointSign.toByte)

    sig.publicKey(bytesToSign).isEmpty shouldBe false
  }

  it should "fail on case from transaction 74c45d0cf2332cc021bebdfee6b1c1da0b58e8f4154537adb79b025f722920a4" in {
    val bytesToSign = Hex.decode("2bb3925f178aa22c11435c61899e134fb7b1227016274b5f7b9d85c4469130ba")
    val signatureRandom = ByteString(Hex.decode("fbe3df0cf030655d817a89936850d1cc00c07c35d3b21be73cfe9a730ea8b753"))
    val signature = ByteString(Hex.decode("62d73b6a92ac23ff514315fad795bbac6d485481d356329d71467e93c87dfa42"))
    val pointSign = 0x1f

    val sig = ECDSASignature(BigInt(1, signatureRandom.toArray[Byte]), BigInt(1, signature.toArray[Byte]), pointSign.toByte)

    sig.publicKey(bytesToSign).isEmpty shouldBe true
  }

  it should "sign message and recover public key" in {
    forAll(arbitrary[Array[Byte]], Arbitrary.arbitrary[Unit].map(_ => generateKeyPair(secureRandom))) {
      (message, keys) =>

        val pubKey = keys.getPublic.asInstanceOf[ECPublicKeyParameters].getQ
        val msg = kec256(message)

        val signature = ECDSASignature.sign(msg, keys)
        val recPubKey = signature.publicKey(msg)

        val result = recPubKey.map(a => ECDSASignature.uncompressedIndicator +: a).map(curve.getCurve.decodePoint).map(_.getEncoded(true)).map(ByteString(_))
        val expected = Some(pubKey.getEncoded(true)).map(ByteString(_))

        result shouldBe expected
    }
  }
} 
Example 30
Source File: EphemDataSourceSuite.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.db.dataSource

import akka.util.ByteString
import io.iohk.ethereum.ObjectGenerators
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class EphemDataSourceSuite extends FunSuite
  with PropertyChecks
  with ObjectGenerators {

  val KeySize: Int = 32
  val KeyNumberLimit: Int = 40
  val OtherNamespace: IndexedSeq[Byte] = IndexedSeq[Byte]('e'.toByte)
  def putMultiple(dataSource: DataSource, toInsert: Seq[(ByteString, ByteString)]): DataSource = {
    toInsert.foldLeft(dataSource){ case (recDB, keyValuePair) =>
      recDB.update(OtherNamespace, Seq(), Seq(keyValuePair))
    }
  }

  def removeMultiple(dataSource: DataSource, toDelete: Seq[ByteString]): DataSource = {
    toDelete.foldLeft(dataSource){ case (recDB, key) =>
      recDB.update(OtherNamespace, Seq(key), Seq())
    }
  }

  test("EphemDataSource insert"){
    forAll(seqByteStringOfNItemsGen(KeySize)) { unFilteredKeyList: Seq[ByteString] =>
      val keyList = unFilteredKeyList.filter(_.length == KeySize)
      val db = putMultiple(dataSource = EphemDataSource(), toInsert = keyList.zip(keyList))
      keyList.foreach { key =>
        val obtained = db.get(OtherNamespace, key)
        assert(obtained.isDefined)
        assert(obtained.get sameElements key)
      }
    }
  }

  test("EphemDataSource delete"){
    forAll(seqByteStringOfNItemsGen(KeySize)) { keyList: Seq[ByteString] =>
      val (keysToDelete, keyValueLeft) = keyList.splitAt(Gen.choose(0, keyList.size).sample.get)

      val dbAfterInsert = putMultiple(dataSource = EphemDataSource(), toInsert = keyList.zip(keyList))
      val db = removeMultiple(dataSource = dbAfterInsert, toDelete = keysToDelete)
      keyValueLeft.foreach { key =>
        val obtained = db.get(OtherNamespace, key)
        assert(obtained.isDefined)
        assert(obtained.get sameElements key)
      }
      keysToDelete.foreach { key => assert(db.get(OtherNamespace, key).isEmpty) }
    }
  }

  test("EphemDataSource clear") {
    forAll(seqByteStringOfNItemsGen(KeySize)) { keyList: Seq[ByteString] =>
      val db = EphemDataSource()
        .update(OtherNamespace, toRemove = Seq(), toUpsert = keyList.zip(keyList))
        .clear

      keyList.foreach { key => assert(db.get(OtherNamespace, key).isEmpty) }
    }
  }
} 
Example 31
Source File: TotalDifficultyStorageSuite.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.db.storage

import io.iohk.ethereum.ObjectGenerators
import io.iohk.ethereum.db.dataSource.EphemDataSource
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class TotalDifficultyStorageSuite extends FunSuite with PropertyChecks with ObjectGenerators {
  test("TotalDifficultyStorage insert") {
    forAll(Gen.listOf(byteStringOfLengthNGen(32))){ blockByteArrayHashes =>
      val blockHashes = blockByteArrayHashes.distinct
      val tdList = Gen.listOf(bigIntGen).sample.get
      val blockHashesTdPair = tdList.zip(blockHashes)

      val initialTotalDifficultyStorage = new TotalDifficultyStorage(EphemDataSource())
      val totalDifficultyStorage = blockHashesTdPair.foldLeft(initialTotalDifficultyStorage){
        case (recTotalDifficultyStorage, (td, blockHash)) =>
          recTotalDifficultyStorage.put(blockHash, td)
      }

      blockHashesTdPair.foreach{case (td, blockHash) => assert(totalDifficultyStorage.get(blockHash).contains(td)) }
    }
  }

  test("TotalDifficultyStorage delete") {
    forAll(Gen.listOf(byteStringOfLengthNGen(32))){ blockByteArrayHashes =>
      val blockHashes = blockByteArrayHashes.distinct
      val tdList = Gen.listOf(bigIntGen).sample.get
      val blockHashesTdPair = tdList.zip(blockHashes)

      //Total difficulty of blocks is inserted
      val initialTotalDifficultyStorage = new TotalDifficultyStorage(EphemDataSource())
      val totalDifficultyStorage = blockHashesTdPair.foldLeft(initialTotalDifficultyStorage){
        case (recTotalDifficultyStorage, (td, blockHash)) =>
          recTotalDifficultyStorage.put(blockHash, td)
      }

      //Total difficulty of blocks is deleted
      val (toDelete, toLeave) = blockHashesTdPair.splitAt(Gen.choose(0, blockHashesTdPair.size).sample.get)
      val totalDifficultyStorageAfterDelete = toDelete.foldLeft(totalDifficultyStorage){
        case (recTotalDifficultyStorage, (_, blockHash)) =>
          recTotalDifficultyStorage.remove(blockHash)
      }

      toLeave.foreach{case (td, blockHeader) => assert(totalDifficultyStorageAfterDelete.get(blockHeader).contains(td)) }
      toDelete.foreach{ case (_, bh) => assert(totalDifficultyStorageAfterDelete.get(bh).isEmpty) }
    }
  }
} 
Example 32
Source File: TransactionMappingStorageSuite.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.db.storage

import io.iohk.ethereum.ObjectGenerators
import io.iohk.ethereum.db.dataSource.EphemDataSource
import io.iohk.ethereum.db.storage.TransactionMappingStorage.TransactionLocation
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class TransactionMappingStorageSuite extends FunSuite with PropertyChecks with ObjectGenerators {
  test("TransactionMappingStorage insert") {
    forAll(Gen.listOf(byteStringOfLengthNGen(32))){ txByteArrayHashes =>
      val txHashes = txByteArrayHashes.distinct
      val blockHashesList = Gen.listOfN(txByteArrayHashes.length, byteStringOfLengthNGen(32)).sample.get
      val txIndexList = Gen.listOfN(txByteArrayHashes.length, intGen).sample.get
      val txLocationList = blockHashesList.zip(txIndexList).map{ case (blockHash, txIndex) =>
        TransactionLocation(blockHash, txIndex) }

      val initialTxMappingStorage = new TransactionMappingStorage(EphemDataSource())
      val txMappingStorage = txHashes.zip(txLocationList).foldLeft(initialTxMappingStorage){
        case (recTxMappingStorage, (txHash, txLocation)) =>
          recTxMappingStorage.put(txHash, txLocation)
      }

      txHashes.zip(txLocationList).foreach{case (txHash, txLocation) => assert(txMappingStorage.get(txHash).contains(txLocation)) }
    }
  }

  test("TransactionMappingStorage delete") {
    forAll(Gen.listOf(byteStringOfLengthNGen(32))){ txByteArrayHashes =>
      val txHashes = txByteArrayHashes.distinct
      val blockHashesList = Gen.listOfN(txByteArrayHashes.length, byteStringOfLengthNGen(32)).sample.get
      val txIndexList = Gen.listOfN(txByteArrayHashes.length, intGen).sample.get
      val txLocationList = blockHashesList.zip(txIndexList).map{ case (blockHash, txIndex) =>
        TransactionLocation(blockHash, txIndex) }
      val txHashAndLocationPair = txHashes.zip(txLocationList)

      //Mapping of tx to blocks is inserted
      val initialTxMappingStorage = new TransactionMappingStorage(EphemDataSource())
      val txMappingStorage = txHashAndLocationPair.foldLeft(initialTxMappingStorage){
        case (recTxMappingStorage, (txHash, txLocation)) =>
          recTxMappingStorage.put(txHash, txLocation)
      }

      //Mapping of tx to blocks is deleted
      val (toDelete, toLeave) = txHashAndLocationPair.splitAt(Gen.choose(0, txHashAndLocationPair.size).sample.get)
      val txMappingStorageAfterDelete = toDelete.foldLeft(txMappingStorage){
        case (recTxMappingStorage, (txHash, _)) =>
          recTxMappingStorage.remove(txHash)
      }

      toLeave.foreach{case (txHash, txLocation) => assert(txMappingStorageAfterDelete.get(txHash).contains(txLocation)) }
      toDelete.foreach{ case (txHash, _) => assert(txMappingStorageAfterDelete.get(txHash).isEmpty) }
    }
  }
} 
Example 33
Source File: CodeStorageSuite.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.db.storage

import akka.util.ByteString
import io.iohk.ethereum.ObjectGenerators
import io.iohk.ethereum.db.dataSource.EphemDataSource
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class CodeStorageSuite extends FunSuite with PropertyChecks with ObjectGenerators {
  val LimitCodeSize = 100

  test("CodeStorage insert") {
    forAll(Gen.listOf(byteStringOfLengthNGen(32))){ unfilteredCodeHashes =>
      val codeHashes = unfilteredCodeHashes.distinct
      val codes = Gen.listOfN(codeHashes.length, randomSizeByteArrayGen(0, LimitCodeSize)).sample.get.map(ByteString(_))
      val initialCodeStorage = new EvmCodeStorage(EphemDataSource())
      val codeStorage = codeHashes.zip(codes).foldLeft(initialCodeStorage){
        case (recCodeStorage, (codeHash, code)) =>
          recCodeStorage.put(codeHash, code)
      }

      codeHashes.zip(codes).foreach{ case (codeHash, code) =>
        assert(codeStorage.get(codeHash).contains(code))
      }
    }
  }

  test("CodeStorage delete") {
    forAll(Gen.listOf(byteStringOfLengthNGen(32))){ unfilteredCodeHashes =>
      val codeHashes = unfilteredCodeHashes.distinct
      val codes = Gen.listOfN(codeHashes.length, randomSizeByteArrayGen(0, LimitCodeSize)).sample.get.map(ByteString(_))

      //EVM codes are inserted
      val initialCodeStorage = new EvmCodeStorage(EphemDataSource())
      val codeStorage = codeHashes.zip(codes).foldLeft(initialCodeStorage){
        case (recCodeStorage, (codeHash, code)) =>
          recCodeStorage.put(codeHash, code)
      }

      //EVM codes are deleted
      val (toDelete, toLeave) = codeHashes.zip(codes)
        .splitAt(Gen.choose(0, codeHashes.size).sample.get)
      val codeStorageAfterDelete = toDelete.foldLeft(codeStorage){
        case (recCodeStorage, (codeHash, _)) =>
          recCodeStorage.remove(codeHash)
      }

      toLeave.foreach{ case (codeHash, code) => assert(codeStorageAfterDelete.get(codeHash).contains(code)) }
      toDelete.foreach{ case (codeHash, _) => assert(codeStorageAfterDelete.get(codeHash).isEmpty) }
    }
  }
} 
Example 34
Source File: NodeStorageSuite.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.db.storage

import akka.util.ByteString
import io.iohk.ethereum.ObjectGenerators
import io.iohk.ethereum.db.dataSource.EphemDataSource
import io.iohk.ethereum.network.p2p.messages.PV63.MptNodeEncoders._
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class NodeStorageSuite extends FunSuite with PropertyChecks with ObjectGenerators {
  test("NodeStorage insert") {
    forAll(Gen.listOf(nodeGen)){ unfilteredMptNodes =>
      val mptNodes = unfilteredMptNodes.distinct
      val initialNodeStorage: NodeStorage = new NodeStorage(EphemDataSource())
      val nodeStorage = mptNodes.foldLeft(initialNodeStorage){
        case (recNodeStorage, node) =>
          recNodeStorage.update(Nil, Seq(ByteString(node.hash) -> node.toBytes))
      }

      mptNodes.foreach{ node =>
        val obtainedNode = nodeStorage.get(ByteString(node.hash)).map(_.toMptNode)
        assert(obtainedNode.contains(node))
      }
    }
  }

  test("NodeStorage delete") {
    forAll(Gen.listOf(nodeGen)){ unfilteredMptNodes =>
      val mptNodes = unfilteredMptNodes.distinct

      //Nodes are inserted
      val initialNodeStorage: NodeStorage = new NodeStorage(EphemDataSource())
      val nodeStorage = mptNodes.foldLeft(initialNodeStorage){
        case (recNodeStorage, node) =>
          recNodeStorage.update(Nil, Seq(ByteString(node.hash) -> node.toBytes))
      }

      //Nodes are deleted
      val (toDelete, toLeave) = mptNodes.splitAt(Gen.choose(0, mptNodes.size).sample.get)
      val nodeStorageAfterDelete = toDelete.foldLeft(nodeStorage){
        case (recNodeStorage, node) =>
          recNodeStorage.update(Seq(ByteString(node.hash)), Nil)
      }

      toLeave.foreach{ node =>
        val obtainedNode = nodeStorageAfterDelete.get(ByteString(node.hash)).map(_.toMptNode)
        assert(obtainedNode.contains(node)) }
      toDelete.foreach{ node => assert(nodeStorageAfterDelete.get(ByteString(node.hash)).isEmpty) }
    }
  }
} 
Example 35
Source File: ReceiptStorageSuite.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.db.storage

import io.iohk.ethereum.ObjectGenerators
import io.iohk.ethereum.db.dataSource.EphemDataSource
import io.iohk.ethereum.domain.Receipt
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class ReceiptStorageSuite extends FunSuite with PropertyChecks with ObjectGenerators {

  test("ReceiptStorage insert") {
    forAll(Gen.listOf(byteStringOfLengthNGen(32))){ blockByteArrayHashes =>
      val blockHashes = blockByteArrayHashes.distinct
      val receipts = receiptsGen(blockHashes.length).sample.get
      val blockHashesReceiptsPair = receipts.zip(blockHashes)

      val initialReceiptStorage = new ReceiptStorage(EphemDataSource())
      val receiptStorage = blockHashesReceiptsPair.foldLeft(initialReceiptStorage){
        case (recReceiptStorage, (receiptList, blockHash)) =>
          recReceiptStorage.put(blockHash, receiptList)
      }

      blockHashesReceiptsPair.foreach{case (rs, bh) =>
        val obtainedReceipts: Option[Seq[Receipt]] = receiptStorage.get(bh)
        assert(obtainedReceipts.contains(rs))
      }
    }
  }

  test("ReceiptStorage delete") {
    forAll(Gen.listOf(byteStringOfLengthNGen(32))){ blockByteArrayHashes =>
      val blockHashes = blockByteArrayHashes.distinct
      val receipts = receiptsGen(blockHashes.length).sample.get
      val blockHashesReceiptsPair = receipts.zip(blockHashes)

      //Receipts are inserted
      val initialReceiptStorage = new ReceiptStorage(EphemDataSource())
      val receiptStorage = blockHashesReceiptsPair.foldLeft(initialReceiptStorage){
        case (recReceiptStorage, (receiptList, blockHash)) =>
          recReceiptStorage.put(blockHash, receiptList)
      }

      //Receipts are deleted
      val (toDelete, toLeave) = blockHashesReceiptsPair.splitAt(Gen.choose(0, blockHashesReceiptsPair.size).sample.get)
      val receiptStorageAfterDelete = toDelete.foldLeft(receiptStorage){
        case (recReceiptStorage, (_, blockHash)) =>
          recReceiptStorage.remove(blockHash)
      }

      toLeave.foreach{case (rs, bh) =>
        val obtainedReceipts = receiptStorageAfterDelete.get(bh)
        assert(obtainedReceipts.contains(rs))
      }
      toDelete.foreach{ case (_, bh) => assert(receiptStorageAfterDelete.get(bh).isEmpty) }
    }
  }
} 
Example 36
Source File: EthashSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.consensus

import akka.util.ByteString
import io.iohk.ethereum.crypto.kec256
import org.scalacheck.Arbitrary
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}
import org.spongycastle.util.encoders.Hex

class EthashSpec extends FlatSpec with Matchers with PropertyChecks {

  import Ethash._

  "Ethash" should "generate correct hash" in {
    forAll(Arbitrary.arbitrary[Long].filter(_ < 15000000)) { blockNumber =>
      seed(epoch(blockNumber)) shouldBe seedForBlockReference(blockNumber)
    }
  }

  it should "calculate cache size" in {
    val cacheSizes = Seq(16776896, 16907456, 17039296, 17170112, 17301056, 17432512, 17563072)
    cacheSizes.zipWithIndex.foreach { case (referenceSize, epoch) =>
      cacheSize(epoch) shouldBe referenceSize
    }
  }

  it should "compute proof of work using cache" in {
    val hash = Array(0xf5, 0x7e, 0x6f, 0x3a, 0xcf, 0xc0, 0xdd, 0x4b, 0x5b, 0xf2, 0xbe, 0xe4, 0x0a, 0xb3, 0x35, 0x8a, 0xa6, 0x87, 0x73, 0xa8, 0xd0, 0x9f, 0x5e, 0x59, 0x5e, 0xab, 0x55, 0x94, 0x05, 0x52, 0x7d, 0x72).map(_.toByte)
    val nonce = Array(0xd7, 0xb3, 0xac, 0x70, 0xa3, 0x01, 0xa2, 0x49).map(_.toByte)

    val mixHash = Array(0x1f, 0xff, 0x04, 0xce, 0xc9, 0x41, 0x73, 0xfd, 0x59, 0x1e, 0x3d, 0x89, 0x60, 0xce, 0x6b, 0xdf, 0x8b, 0x19, 0x71, 0x04, 0x8c, 0x71, 0xff, 0x93, 0x7b, 0xb2, 0xd3, 0x2a, 0x64, 0x31, 0xab, 0x6d).map(_.toByte)
    val boundary = Array(0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x3e, 0x9b, 0x6c, 0x69, 0xbc, 0x2c, 0xe2, 0xa2, 0x4a, 0x8e, 0x95, 0x69, 0xef, 0xc7, 0xd7, 0x1b, 0x33, 0x35, 0xdf, 0x36, 0x8c, 0x9a, 0xe9, 0x7e, 0x53, 0x84).map(_.toByte)

    val blockNumber = 486382
    val cache = makeCache(epoch(blockNumber))
    val proofOfWork = hashimotoLight(hash, nonce, dagSize(epoch(blockNumber)), cache)

    proofOfWork.mixHash shouldBe ByteString(mixHash)
    proofOfWork.difficultyBoundary shouldBe ByteString(boundary)

    val table = Table(
      ("blockNumber", "hashWithoutNonce", "nonce", "mixHash"),
      (3521,"269d13f7ca546dced28ee26071dcb61085b7c54dfc5f93808b94885e136cd616","534ab630b9aa1f68","c6913517d1dc7544febde9f17e65d6ae4fa380d4a2a2d31305b0043caf95e717"),
      (5021,"7bd6c3c49a0627712c51f1abf0a7828bb25ebb8679d2584385a191db955667da","413dc4ec1a0df7c4","35890608f8867402052b2ce55a694b86a44ce87e7fb5412a77a025b184f25883"),
      (5091,"5b27820bfa3a059274ce17db0beea90ba0b6fbe6b49d2a23cbf972e8cde79319","59225875d18ad606","46f72f8b269461078e9d1cf4edf1b608f9d101e0f335ea59568c3436f291d01b"),
      (3091,"c37d980124cf83a4de4d9600f5bb6d3883797b84b7ec472feff6ca855c01d245","745609efa9c4eef3","c647fec06481b9f3f74cd771968d6d630aa11bf75ebd9e3c55ccfbae0fbad4da"),
      (1091,"c1c1efb8fdd4241a55db39e092fedae3df6d4abc13133778810027ade6557bc6","4d9ddadaea6c20b2","53624a7faac2ec82208348f7a11e3b38c880a2fec76dd8b47e434fe641eeacde"),
      (109,"aa234d4bcee14e93d127275dcc83504b6e730a14e9110bd09b68e1964f0daad3","388e6b37c22147b7","df14701b1ad6d3d5639956e463250960de3189a726cb38d71a6f6042f45dea72"),
      (1009,"3259779f9d2c477d29e18ead0ccc829bf2146723563c3e81e5e4886673d93bfb","5faa044b70ccdf6b","a1f1af0c2ca3e1d8e69da59fefbfeb4d0d172ec96bdbdac71b2cde49ddb3a828"),
      (1001,"028cc9a70d6db52c2a2606f04392e9a323d0370291d6c6d78bc8ce54acf1d761","a54b5b31ce3de766","819c26573f1a9cd6c4b9a399b72fbfb0084a104b25b62083533e114ee98a4831"),
      (1000,"15c5729eb017a703c13d00752338f6b55e2d2551b380706f0486f2ccca57ae1e","eb610e766452a801","a369e2fd5c4e357cf9f60ba063ae0baf32075b0d7ed80cd78134bc401db8f1bf"),
      (100,"41944a94a42695180b1ca231720a87825f17d36475112b659c23dea1542e0977","37129c7f29a9364b","5bb43c0772e58084b221c8e0c859a45950c103c712c5b8f11d9566ee078a4501"))

    forAll(table) { (blockNumber, hashWithoutNonce, nonce, mixHash) =>
      val cache = makeCache(epoch(blockNumber))
      val proofOfWork = hashimotoLight(Hex.decode(hashWithoutNonce), Hex.decode(nonce), dagSize(epoch(blockNumber)), cache)
      proofOfWork.mixHash shouldBe ByteString(Hex.decode(mixHash))
    }
  }

  def seedForBlockReference(blockNumber: BigInt): ByteString = {
    if (blockNumber < EPOCH_LENGTH) {
      //wrong version from YP:
      //ByteString(kec256(Hex.decode("00" * 32)))
      //working version:
      ByteString(Hex.decode("00" * 32))
    } else {
      kec256(seedForBlockReference(blockNumber - EPOCH_LENGTH))
    }
  }

} 
Example 37
Source File: SignedTransactionSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.domain

import io.iohk.ethereum.crypto
import io.iohk.ethereum.crypto.generateKeyPair
import io.iohk.ethereum.domain.SignedTransaction.FirstByteOfAddress
import io.iohk.ethereum.nodebuilder.SecureRandomBuilder
import io.iohk.ethereum.vm.Generators
import org.scalacheck.Arbitrary
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}
import org.spongycastle.crypto.params.ECPublicKeyParameters

class SignedTransactionSpec extends FlatSpec with Matchers with PropertyChecks with SecureRandomBuilder {
  "SignedTransaction" should "correctly set pointSign for chainId with chain specific signing schema" in {
    forAll(Generators.transactionGen(), Arbitrary.arbitrary[Unit].map(_ => generateKeyPair(secureRandom))) {
      (tx, key) =>
        val chainId: Byte = 0x3d
        val allowedPointSigns = Set((chainId * 2 + 35).toByte, (chainId * 2 + 36).toByte)
        //byte 0 of encoded ECC point indicates that it is uncompressed point, it is part of spongycastle encoding
        val address = Address(crypto.kec256(key.getPublic.asInstanceOf[ECPublicKeyParameters].getQ.getEncoded(false).tail).drop(FirstByteOfAddress))
        val result = SignedTransaction.sign(tx, key, Some(chainId))

        allowedPointSigns should contain(result.signature.v)
        address shouldEqual result.senderAddress
    }
  }
} 
Example 38
Source File: BlockHeaderSpec.scala    From mantis   with Apache License 2.0 5 votes vote down vote up
package io.iohk.ethereum.domain

import akka.util.ByteString
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks
import org.spongycastle.util.encoders.Hex

class BlockHeaderSpec extends FunSuite with PropertyChecks  {
  test("Encoding of block header without nonce") {
    //Expected values obtained using EthereumJ
    val obtainedEncodedWithoutNonce = Hex.toHexString(BlockHeader.getEncodedWithoutNonce(validBlockHeader))
    val expectedEncodedWithoutNonce = "f901e6a0d882d5c210bab4cb7ef0b9f3dc2130cb680959afcd9a8f9bf83ee6f13e2f9da3a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479495f484419881c6e9b6de7fb3f8ad03763bd49a89a0634a2b20c9e02afdda7157afe384306c5acc4fb9c09b45dc0203c0fbb2fed0e6a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830f1a4c148407d85e8f8084589e0ab998d783010507846765746887676f312e372e33856c696e7578"
    assert(obtainedEncodedWithoutNonce == expectedEncodedWithoutNonce)

    val obtainedParentEncodedWithoutNonce = Hex.toHexString(BlockHeader.getEncodedWithoutNonce(validBlockParent))
    val expectedParentEncodedWithoutNonce = "f901e6a0677a5fb51d52321b03552e3c667f602cc489d15fc1d7824445aee6d94a9db2e7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d493479495f484419881c6e9b6de7fb3f8ad03763bd49a89a0cddeeb071e2f69ad765406fb7c96c0cd42ddfc6ec54535822b564906f9e38e44a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830f1869138407da55238084589e0ab898d783010507846765746887676f312e372e33856c696e7578"
    assert(obtainedParentEncodedWithoutNonce == expectedParentEncodedWithoutNonce)
  }

  val validBlockHeader = BlockHeader(
    parentHash = ByteString(Hex.decode("d882d5c210bab4cb7ef0b9f3dc2130cb680959afcd9a8f9bf83ee6f13e2f9da3")),
    ommersHash = ByteString(Hex.decode("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")),
    beneficiary = ByteString(Hex.decode("95f484419881c6e9b6de7fb3f8ad03763bd49a89")),
    stateRoot = ByteString(Hex.decode("634a2b20c9e02afdda7157afe384306c5acc4fb9c09b45dc0203c0fbb2fed0e6")),
    transactionsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
    receiptsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
    logsBloom = ByteString(Hex.decode("00" * 256)),
    difficulty = BigInt("989772"),
    number = 20,
    gasLimit = 131620495,
    gasUsed = 0,
    unixTimestamp = 1486752441,
    extraData = ByteString(Hex.decode("d783010507846765746887676f312e372e33856c696e7578")),
    mixHash = ByteString(Hex.decode("6bc729364c9b682cfa923ba9480367ebdfa2a9bca2a652fe975e8d5958f696dd")),
    nonce = ByteString(Hex.decode("797a8f3a494f937b"))
  )

  val validBlockParent = BlockHeader(
    parentHash = ByteString(Hex.decode("677a5fb51d52321b03552e3c667f602cc489d15fc1d7824445aee6d94a9db2e7")),
    ommersHash = ByteString(Hex.decode("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")),
    beneficiary = ByteString(Hex.decode("95f484419881c6e9b6de7fb3f8ad03763bd49a89")),
    stateRoot = ByteString(Hex.decode("cddeeb071e2f69ad765406fb7c96c0cd42ddfc6ec54535822b564906f9e38e44")),
    transactionsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
    receiptsRoot = ByteString(Hex.decode("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")),
    logsBloom = ByteString(Hex.decode("00" * 256)),
    difficulty = BigInt("989289"),
    number = 19,
    gasLimit = 131749155,
    gasUsed = 0,
    unixTimestamp = 1486752440,
    extraData = ByteString(Hex.decode("d783010507846765746887676f312e372e33856c696e7578")),
    mixHash = ByteString(Hex.decode("7f9ac1ddeafff0f926ed9887b8cf7d50c3f919d902e618b957022c46c8b404a6")),
    nonce = ByteString(Hex.decode("3fc7bc671f7cee70"))
  )
} 
Example 39
Source File: TestSpec.scala    From akka-serialization-test   with Apache License 2.0 5 votes vote down vote up
package com.github.dnvriend

import akka.actor.{ ActorRef, ActorSystem, PoisonPill }
import akka.event.{ Logging, LoggingAdapter }
import akka.serialization.SerializationExtension
import akka.stream.{ ActorMaterializer, Materializer }
import akka.testkit.TestProbe
import akka.util.Timeout
import org.scalatest.concurrent.{ Eventually, ScalaFutures }
import org.scalatest.prop.PropertyChecks
import org.scalatest.{ BeforeAndAfterAll, FlatSpec, GivenWhenThen, Matchers }

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

trait TestSpec extends FlatSpec
    with Matchers
    with GivenWhenThen
    with ScalaFutures
    with BeforeAndAfterAll
    with Eventually
    with PropertyChecks
    with AkkaPersistenceQueries
    with AkkaStreamUtils
    with InMemoryCleanup {

  implicit val timeout: Timeout = Timeout(10.seconds)
  implicit val system: ActorSystem = ActorSystem()
  implicit val ec: ExecutionContext = system.dispatcher
  implicit val mat: Materializer = ActorMaterializer()
  implicit val log: LoggingAdapter = Logging(system, this.getClass)
  implicit val pc: PatienceConfig = PatienceConfig(timeout = 50.seconds)
  val serialization = SerializationExtension(system)

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

  def killActors(actors: ActorRef*): Unit = {
    val probe = TestProbe()
    actors.foreach { actor ⇒
      probe watch actor
      actor ! PoisonPill
      probe expectTerminated actor
    }
  }

  override protected def afterAll(): Unit = {
    system.terminate()
    system.whenTerminated.toTry should be a 'success
  }
} 
Example 40
Source File: TreeSpec.scala    From bonsai   with MIT License 5 votes vote down vote up
package com.stripe.bonsai

import org.scalatest.{ WordSpec, Matchers }
import org.scalatest.prop.{ Checkers, PropertyChecks }

class TreeSpec extends WordSpec with Matchers with Checkers with PropertyChecks {
  val ops = TreeOps[Tree[Int], Int]

  def sumTree(g: Tree[Int]): Long =
    ops.reduce[Long](g)(_ + _.sum).getOrElse(0L)

  def treeElems(g: Tree[Int]): Set[Int] =
    ops.reduce[Set[Int]](g) { (n, as) =>
      as.foldLeft(Set(n))(_ | _)
    } getOrElse(Set.empty)

  def treeMin(g: Tree[Int]): Option[Int] =
    ops.reduce[Int](g) { (n, as) =>
      if (as.isEmpty) n else n min as.min
    }

  "Tree.apply" should {
    "copy structure of tree" in {
      forAll { (tree: GenericTree[Int]) =>
        GenericTree.fromTree(Tree(tree)) shouldBe Some(tree)
      }
    }

    "sum with tree ops" in {
      forAll { (x: Int, gtrees: List[GenericTree[Int]]) =>
        val trees = gtrees.map(Tree(_))
        val tree = Tree(GenericTree(x, gtrees))
        val expected = trees.foldLeft(x.toLong) { (t, n) => t + sumTree(n) }
        sumTree(tree) shouldBe expected
      }
    }

    "elems with tree ops" in {
      forAll { (x: Int, gtrees: List[GenericTree[Int]]) =>
        val trees = gtrees.map(Tree(_))
        val tree = Tree(GenericTree(x, gtrees))

        val elems = trees.foldLeft(Set(x))((t, n) => t | treeElems(n))
        treeElems(tree) shouldBe elems
        treeMin(tree) shouldBe Some(elems.min)
      }
    }
  }
} 
Example 41
Source File: ZeroCopyBinaryTest.scala    From filo   with Apache License 2.0 5 votes vote down vote up
package org.velvia.filo

import org.scalatest.{FunSpec, Matchers}
import org.scalatest.prop.PropertyChecks

class ZeroCopyBinaryTest extends FunSpec with Matchers with PropertyChecks {
  describe("ZeroCopyUTF8String") {
    it("should convert back and forth between regular strings") {
      ZeroCopyUTF8String("sheep").asNewString should equal ("sheep")
    }

    import ZeroCopyUTF8String._
    import Ordered._

    it("should compare two strings properly") {
      // Unequal lengths, equal prefix
      ZeroCopyUTF8String("boobeebob") should be > (ZeroCopyUTF8String("boobee"))

      // Equal lengths, different content
      // First comparison fights against int comparisons without proper byte ordering
      ZeroCopyUTF8String("aaab") should be < (ZeroCopyUTF8String("baaa"))
      "bobcat".utf8 should equal ("bobcat".utf8)

      // Strings longer than 8 chars (in case comparison uses long compare)
      "dictionary".utf8 should be < ("pictionar".utf8)
      "dictionary".utf8 should be > ("dictionaries".utf8)

      // Calling equals to some other type should return false
      ZeroCopyUTF8String("dictionary") should not equal ("dictionary")
    }

    it("should compare random strings properly") {
      import java.lang.Integer.signum
      forAll { (strs: (String, String)) =>
        val nativeCmp = signum(strs._1.compare(strs._2))
        signum(ZeroCopyUTF8String(strs._1).compare(ZeroCopyUTF8String(strs._2))) should equal (nativeCmp)
      }
    }

    it("should get bytes back and convert back to instance, and compare equally") {
      val origUTF8Str = ZeroCopyUTF8String("dictionary")
      ZeroCopyUTF8String(origUTF8Str.bytes) should equal (origUTF8Str)
    }

    it("should generate same hashcode for same content") {
      "bobcat".utf8.hashCode should equal ("bobcat".utf8.hashCode)
      "bobcat".utf8.hashCode should not equal (ZeroCopyUTF8String("bob").hashCode)

      "bobcat".utf8.cachedHash64 should equal ("bobcat".utf8.cachedHash64)
      "bobcat".utf8.cachedHash64 should not equal (ZeroCopyUTF8String("bob").cachedHash64)
    }

    val str1 = ZeroCopyUTF8String("1234")
    val str2 = ZeroCopyUTF8String("一2三4")
    val str3 = ZeroCopyUTF8String("一二34")

    it("should get substring correctly") {
      str1.substring(3, 2) should equal (ZeroCopyUTF8String(""))
      str2.substring(0, 2) should equal (ZeroCopyUTF8String("一2"))
      str2.substring(1, 5) should equal (ZeroCopyUTF8String("2三4"))
      str3.substring(0, 3) should equal (ZeroCopyUTF8String("一二3"))
      str2.substring(1, 3) should equal (ZeroCopyUTF8String("2三"))
    }

    it("should startsWith and endsWith correctly") {
      str2.startsWith(ZeroCopyUTF8String("一2")) should equal (true)
      str2.startsWith(ZeroCopyUTF8String("2三")) should equal (false)
      str2.startsWith(str1) should equal (false)

      str2.endsWith(str3) should equal (false)
      str2.endsWith(ZeroCopyUTF8String("4")) should equal (true)
    }

    it("should check contains correctly") {
      str2.contains(ZeroCopyUTF8String("2三")) should equal (true)
      str2.contains(str1) should equal (false)
    }
  }
} 
Example 42
Source File: PipeLineSpec.scala    From gearpump-examples   with Apache License 2.0 5 votes vote down vote up
package io.gearpump.examples.kafka_hdfs_pipeline

import akka.actor.ActorSystem
import io.gearpump._
import io.gearpump.cluster.UserConfig
import io.gearpump.streaming.task.{StartTime, Task, TaskContext}
import io.gearpump.streaming.transaction.api.TimeReplayableSource
import io.gearpump.util.LogUtil
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfterAll, Matchers, PropSpec}
import org.slf4j.Logger

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

class SpaceShuttleReplayableSource extends TimeReplayableSource {
  val data = Array[String](
    """
      |{"id":"2a329674-12ad-49f7-b40d-6485aae0aae8","on":"2015-04-02T18:52:02.680178753Z","body":"[-0.414141,-0.0246564,-0.125,0.0140301,-0.474359,0.0256049,-0.0980392,0.463884,0.40836]"}
    """
      .stripMargin,
    """
      |{"id":"043ade58-2fbc-4fe2-8253-84ab181b8cfa","on":"2015-04-02T18:52:02.680078434Z","body": "[-0.414141,-0.0246564,-0.125,0.0140301,-0.474359,0.0256049,-0.0980392,0.463884,0.40836]"}
    """.stripMargin,
    """
      |{"id":"043ade58-2fbc-4fe2-8253-84ab181b8cfa","on":"2015-04-02T18:52:02.680078434Z","body": "[-0.414141,-0.0246564,-0.125,0.0140301,-0.474359,0.0256049,-0.0980392,0.463884,0.40836]"}
    """.stripMargin
  )

  override def open(context: TaskContext, startTime: Option[TimeStamp]): Unit = {}

  override def read(num: Int): List[Message] = List(Message(data(0)), Message(data(1)), Message(data(2)))

  override def close(): Unit = {}
}

class SpaceShuttleProducer(taskContext : TaskContext, conf: UserConfig)
  extends Task(taskContext, conf) {

  import taskContext.{output, parallelism}

  private val batchSize = 3

  val taskParallelism = parallelism

  private val source: TimeReplayableSource = new SpaceShuttleReplayableSource()
  private var startTime: TimeStamp = 0L

  override def onStart(newStartTime: StartTime): Unit = {
    startTime = newStartTime.startTime
    LOG.info(s"start time $startTime")
    source.open(taskContext, Some(startTime))
    self ! Message("start", System.currentTimeMillis())
  }

  override def onNext(msg: Message): Unit = {
    Try({

      source.read(batchSize).foreach(msg => {
        output(msg)
      })
    }) match {
      case Success(ok) =>
      case Failure(throwable) =>
        LOG.error(s"failed ${throwable.getMessage}")
    }
    self ! Message("continue", System.currentTimeMillis())
  }

  override def onStop(): Unit = {
    LOG.info("closing kafka source...")
    source.close()
  }
}

class PipeLineSpec extends PropSpec with PropertyChecks with Matchers with BeforeAndAfterAll {
  val LOG: Logger = LogUtil.getLogger(getClass)
  implicit var system: ActorSystem = null

  override def beforeAll(): Unit = {
    system = ActorSystem("PipeLineSpec")
  }

  override def afterAll(): Unit = {
    system.shutdown()
  }

  property("PipeLineSpec should be able to create a DataSource") {
    Option(new SpaceShuttleReplayableSource) match {
      case Some(replayableSource) =>
      case None =>
        assert(false)
    }
  }
} 
Example 43
Source File: ParquetWriterTaskSpec.scala    From gearpump-examples   with Apache License 2.0 5 votes vote down vote up
package io.gearpump.examples.kafka_hdfs_pipeline

import akka.actor.ActorSystem
import org.apache.avro.Schema
import io.gearpump.Message
import io.gearpump.cluster.UserConfig
import io.gearpump.streaming.MockUtil
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.yarn.conf.YarnConfiguration
import org.apache.parquet.avro.{AvroParquetReader, AvroParquetWriter}
import org.apache.parquet.hadoop.ParquetReader
import org.apache.parquet.hadoop.api.ReadSupport
import org.mockito.Mockito
import org.mockito.Mockito._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfterAll, Matchers, PropSpec}


class ParquetWriterTaskSpec extends PropSpec with PropertyChecks with Matchers with BeforeAndAfterAll {

  implicit var system: ActorSystem = ActorSystem("PipeLineSpec")
  val context = MockUtil.mockTaskContext
  val appName = "KafkaHdfsPipeLine"
  when(context.appName).thenReturn(appName)
  val fs = FileSystem.get(new YarnConfiguration)
  val homeDir = fs.getHomeDirectory.toUri.getPath
  val parquetDir = new Path(homeDir, "gearpump")  + "/parquet/"
  val parquetPath = parquetDir + appName + ".parquet"
  val parquetCrc = parquetDir + "." + appName + ".parquet.crc"
  val parquetWriter = Mockito.mock(classOf[AvroParquetWriter[SpaceShuttleRecord]])

  val anomaly = 0.252
  val now = System.currentTimeMillis

  val userConfig = UserConfig.empty.withString(ParquetWriterTask.PARQUET_OUTPUT_DIRECTORY, "/parquet")

  override def afterAll(): Unit = {
    List(parquetPath, parquetCrc, parquetDir).foreach(new java.io.File(_).delete)
    system.shutdown()
  }

  property("ParquetWriterTask should initialize with local parquet file opened for writing") {
    val parquetWriterTask = new ParquetWriterTask(context, userConfig)
    val path = parquetWriterTask.absolutePath.stripPrefix("file:")
    assert(parquetPath.equals(path))
    parquetWriterTask.onStop
  }

  property("ParquetWriterTask should write records to a parquet file") {
    val message = Message(SpaceShuttleRecord(now, anomaly), now)
    val parquetWriterTask = new ParquetWriterTask(context, userConfig)
    parquetWriterTask.parquetWriter = parquetWriter
    parquetWriterTask.onNext(message)
    verify(parquetWriterTask.parquetWriter).write(message.msg.asInstanceOf[SpaceShuttleRecord])
    parquetWriterTask.onStop
  }

  property("ParquetWriterTask should have verifiable written record") {
    val message = Message(SpaceShuttleRecord(now, anomaly), now)
    val parquetWriterTask = new ParquetWriterTask(context, userConfig)
    parquetWriterTask.onNext(message)
    parquetWriterTask.onStop
    val reader = new AvroParquetReader[SpaceShuttleRecord](new Path(parquetPath))
    val record = reader.read()
    assert(message.msg.asInstanceOf[SpaceShuttleRecord].anomaly == record.anomaly)
    assert(message.msg.asInstanceOf[SpaceShuttleRecord].ts == record.ts)
  }
} 
Example 44
Source File: DistributedShellSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.examples.distributedshell

import scala.concurrent.Future
import scala.util.Success

import com.typesafe.config.Config
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}

class DistributedShellSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter with MasterHarness {

  before {
    startActorSystem()
  }

  after {
    shutdownActorSystem()
  }

  override def config: Config = TestUtil.DEFAULT_CONFIG

  property("DistributedShell should succeed to submit application with required arguments") {
    val requiredArgs = Array.empty[String]

    val masterReceiver = createMockMaster()

    Future {
      DistributedShell.main(masterConfig, requiredArgs)
    }

    masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
    masterReceiver.reply(SubmitApplicationResult(Success(0)))
  }
} 
Example 45
Source File: SeqFileStreamProcessorSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.fsio

import java.io.File
import java.time.Instant
import scala.collection.mutable.ArrayBuffer

import akka.actor.ActorSystem
import akka.testkit.TestProbe
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.io.SequenceFile.Reader
import org.apache.hadoop.io.{SequenceFile, Text}
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.{TestUtil, UserConfig}
import org.apache.gearpump.streaming.task.TaskId
import org.apache.gearpump.streaming.{MockUtil, Processor}
class SeqFileStreamProcessorSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter {

  val kvPairs = new ArrayBuffer[(String, String)]
  val outputDirectory = "SeqFileStreamProcessor_Test"
  val sequenceFilePath = new Path(outputDirectory + File.separator + TaskId(0, 0))
  val hadoopConf = new Configuration()
  val fs = FileSystem.get(hadoopConf)
  val textClass = new Text().getClass
  val _key = new Text()
  val _value = new Text()

  val kvGenerator = for {
    key <- Gen.alphaStr
    value <- Gen.alphaStr
  } yield (key, value)

  before {
    implicit val system1 = ActorSystem("SeqFileStreamProcessor", TestUtil.DEFAULT_CONFIG)
    val system2 = ActorSystem("Reporter", TestUtil.DEFAULT_CONFIG)
    val watcher = TestProbe()(system1)
    val conf = HadoopConfig(UserConfig.empty.withString(SeqFileStreamProcessor.OUTPUT_PATH,
      outputDirectory)).withHadoopConf(new Configuration())
    val context = MockUtil.mockTaskContext

    val processorDescription =
      Processor.ProcessorToProcessorDescription(id = 0, Processor[SeqFileStreamProcessor](1))

    val taskId = TaskId(0, 0)
    when(context.taskId).thenReturn(taskId)

    val processor = new SeqFileStreamProcessor(context, conf)
    processor.onStart(Instant.EPOCH)

    forAll(kvGenerator) { kv =>
      val (key, value) = kv
      kvPairs.append((key, value))
      processor.onNext(Message(key + "++" + value))
    }
    processor.onStop()
  }

  property("SeqFileStreamProcessor should write the key-value pairs to a sequence file") {
    val reader = new SequenceFile.Reader(hadoopConf, Reader.file(sequenceFilePath))
    kvPairs.foreach { kv =>
      val (key, value) = kv
      if (value.length > 0 && reader.next(_key, _value)) {
        assert(_key.toString == key && _value.toString == value)
      }
    }
    reader.close()
  }

  after {
    fs.deleteOnExit(new Path(outputDirectory))
  }
} 
Example 46
Source File: SeqFileStreamProducerSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.fsio

import java.time.Instant

import scala.collection.mutable.ArrayBuffer

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.hadoop.io.SequenceFile.Writer
import org.apache.hadoop.io.{SequenceFile, Text}
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.MockUtil._

class SeqFileStreamProducerSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter {

  val kvPairs = new ArrayBuffer[(String, String)]
  val inputFile = "SeqFileStreamProducer_Test"
  val sequenceFilePath = new Path(inputFile)
  val hadoopConf = new Configuration()
  val fs = FileSystem.get(hadoopConf)
  val textClass = new Text().getClass
  val _key = new Text()
  val _value = new Text()

  val kvGenerator = for {
    key <- Gen.alphaStr
    value <- Gen.alphaStr
  } yield (key, value)

  before {
    fs.deleteOnExit(sequenceFilePath)
    val writer = SequenceFile.createWriter(hadoopConf, Writer.file(sequenceFilePath),
      Writer.keyClass(textClass), Writer.valueClass(textClass))
    forAll(kvGenerator) { kv =>
      _key.set(kv._1)
      _value.set(kv._2)
      kvPairs.append((kv._1, kv._2))
      writer.append(_key, _value)
    }
    writer.close()
  }

  property("SeqFileStreamProducer should read the key-value pairs from " +
    "a sequence file and deliver them") {

    val conf = HadoopConfig(UserConfig.empty.withString(SeqFileStreamProducer.INPUT_PATH,
      inputFile)).withHadoopConf(new Configuration())

    val context = MockUtil.mockTaskContext

    val producer = new SeqFileStreamProducer(context, conf)
    producer.onStart(Instant.EPOCH)
    producer.onNext(Message("start"))

    val expected = kvPairs.map(kv => kv._1 + "++" + kv._2).toSet
    verify(context).output(argMatch[Message](msg =>
      expected.contains(msg.value.asInstanceOf[String])))
  }

  after {
    fs.deleteOnExit(sequenceFilePath)
  }
} 
Example 47
Source File: SequenceFileIOSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.fsio

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

import com.typesafe.config.Config
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfterAll, Matchers, PropSpec}

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}

class SequenceFileIOSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfterAll with MasterHarness {

  override def beforeAll {
    startActorSystem()
  }

  override def afterAll {
    shutdownActorSystem()
  }

  override def config: Config = TestUtil.DEFAULT_CONFIG

  property("SequenceFileIO should succeed to submit application with required arguments") {
    val requiredArgs = Array(
      "-input", "/tmp/input",
      "-output", "/tmp/output"
    )
    val optionalArgs = Array(
      "-source", "1",
      "-sink", "1"
    )
    val validArgs = {
      Table(
        ("requiredArgs", "optionalArgs"),
        (requiredArgs, optionalArgs)
      )
    }
    val masterReceiver = createMockMaster()
    forAll(validArgs) { (requiredArgs: Array[String], optionalArgs: Array[String]) =>
      val args = requiredArgs ++ optionalArgs

      Future {
        SequenceFileIO.main(masterConfig, args)
      }
      masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
      masterReceiver.reply(SubmitApplicationResult(Success(0)))
    }

    val invalidArgs = {
      Table(
        ("requiredArgs", "optionalArgs"),
        (requiredArgs.take(0), optionalArgs),
        (requiredArgs.take(2), optionalArgs)
      )
    }
    forAll(invalidArgs) { (requiredArgs: Array[String], optionalArgs: Array[String]) =>
      val args = optionalArgs
      assert(Try(SequenceFileIO.main(args)).isFailure, "missing required arguments, print usage")
    }
  }
} 
Example 48
Source File: SOLSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.sol

import scala.concurrent.Future
import scala.util.Success

import com.typesafe.config.Config
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfterAll, Matchers, PropSpec}

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}

class SOLSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfterAll with MasterHarness {
  override def beforeAll {
    startActorSystem()
  }

  override def afterAll {
    shutdownActorSystem()
  }

  override def config: Config = TestUtil.DEFAULT_CONFIG

  property("SOL should succeed to submit application with required arguments") {
    val requiredArgs = Array.empty[String]
    val optionalArgs = Array(
      "-streamProducer", "1",
      "-streamProcessor", "1",
      "-bytesPerMessage", "100",
      "-stages", "10")

    val args = {
      Table(
        ("requiredArgs", "optionalArgs"),
        (requiredArgs, optionalArgs)
      )
    }
    val masterReceiver = createMockMaster()
    forAll(args) { (requiredArgs: Array[String], optionalArgs: Array[String]) =>
      val args = requiredArgs ++ optionalArgs

      Future {
        SOL.main(masterConfig, args)
      }

      masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
      masterReceiver.reply(SubmitApplicationResult(Success(0)))
    }
  }
} 
Example 49
Source File: DagSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.complexdag

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}
import org.scalatest._
import org.scalatest.prop.PropertyChecks

import scala.concurrent.Future
import scala.util.Success

class DagSpec extends PropSpec with PropertyChecks
  with Matchers with BeforeAndAfterAll with MasterHarness {

  override def beforeAll {
    startActorSystem()
  }

  override def afterAll {
    shutdownActorSystem()
  }

  protected override def config = TestUtil.DEFAULT_CONFIG

  property("Dag should succeed to submit application with required arguments") {
    val requiredArgs = Array.empty[String]

    val masterReceiver = createMockMaster()
    val args = requiredArgs

    Future {
      Dag.main(masterConfig, args)
    }
    masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
    masterReceiver.reply(SubmitApplicationResult(Success(0)))
  }
} 
Example 50
Source File: NodeSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.complexdag

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.MockUtil._
import org.mockito.Mockito._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

class NodeSpec extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter {

  val context = MockUtil.mockTaskContext

  val node = new Node(context, UserConfig.empty)

  property("Node should send a Vector[String](classOf[Node].getCanonicalName, " +
    "classOf[Node].getCanonicalName") {
    val list = Vector(classOf[Node].getCanonicalName)
    val expected = Vector(classOf[Node].getCanonicalName, classOf[Node].getCanonicalName)
    node.onNext(Message(list))
    verify(context).output(argMatch[Message](_.value == expected))
  }
} 
Example 51
Source File: SinkSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.complexdag

import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil

class SinkSpec extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter {

  val context = MockUtil.mockTaskContext

  val sink = new Sink(context, UserConfig.empty)

  property("Sink should send a Vector[String](classOf[Sink].getCanonicalName, " +
    "classOf[Sink].getCanonicalName") {
    val list = Vector(classOf[Sink].getCanonicalName)
    val expected = Vector(classOf[Sink].getCanonicalName, classOf[Sink].getCanonicalName)
    sink.onNext(Message(list))

    (0 until sink.list.size).map(i => {
      assert(sink.list(i).equals(expected(i)))
    })
  }
} 
Example 52
Source File: WindowAverageAppSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.state

import scala.concurrent.Future
import scala.util.Success

import com.typesafe.config.Config
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}

class WindowAverageAppSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter with MasterHarness {

  before {
    startActorSystem()
  }

  after {
    shutdownActorSystem()
  }

  override def config: Config = TestUtil.DEFAULT_CONFIG

  property("WindowAverage should succeed to submit application with required arguments") {
    val requiredArgs = Array.empty[String]
    val optionalArgs = Array(
      "-gen", "2",
      "-window", "2",
      "-window_size", "5000",
      "-window_step", "5000"
    )

    val args = {
      Table(
        ("requiredArgs", "optionalArgs"),
        (requiredArgs, optionalArgs.take(0)),
        (requiredArgs, optionalArgs.take(2)),
        (requiredArgs, optionalArgs.take(4)),
        (requiredArgs, optionalArgs.take(6)),
        (requiredArgs, optionalArgs)
      )
    }
    val masterReceiver = createMockMaster()
    forAll(args) { (requiredArgs: Array[String], optionalArgs: Array[String]) =>
      val args = requiredArgs ++ optionalArgs

      Future {
        WindowAverageApp.main(masterConfig, args)
      }

      masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
      masterReceiver.reply(SubmitApplicationResult(Success(0)))
    }
  }
} 
Example 53
Source File: WindowAverageProcessorSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.state.processor

import java.time.Instant

import scala.concurrent.Await
import scala.concurrent.duration._

import akka.actor.ActorSystem
import akka.testkit.TestProbe
import com.twitter.algebird.AveragedValue
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.state.impl.{InMemoryCheckpointStoreFactory, PersistentStateConfig, WindowConfig}
import org.apache.gearpump.streaming.task.UpdateCheckpointClock
import org.apache.gearpump.streaming.transaction.api.CheckpointStoreFactory

class WindowAverageProcessorSpec extends PropSpec with PropertyChecks with Matchers {
  property("WindowAverageProcessor should update state") {

    implicit val system = ActorSystem("test")
    val longGen = Gen.chooseNum[Long](1, 1000)
    forAll(longGen, longGen) {
      (data: Long, num: Long) =>
        val taskContext = MockUtil.mockTaskContext

        val windowSize = num
        val windowStep = num

        val conf = UserConfig.empty
          .withBoolean(PersistentStateConfig.STATE_CHECKPOINT_ENABLE, true)
          .withLong(PersistentStateConfig.STATE_CHECKPOINT_INTERVAL_MS, num)
          .withValue[CheckpointStoreFactory](PersistentStateConfig.STATE_CHECKPOINT_STORE_FACTORY,
            new InMemoryCheckpointStoreFactory)
          .withValue(WindowConfig.NAME, WindowConfig(windowSize, windowStep))

        val windowAverage = new WindowAverageProcessor(taskContext, conf)

        val appMaster = TestProbe()(system)
        when(taskContext.appMaster).thenReturn(appMaster.ref)

        windowAverage.onStart(Instant.EPOCH)
        appMaster.expectMsg(UpdateCheckpointClock(taskContext.taskId, 0L))

        for (i <- 0L until num) {
          windowAverage.onNext(Message("" + data, i))
          windowAverage.getState.get shouldBe Some(AveragedValue(i + 1, data))
        }

        // Time to checkpoint
        windowAverage.onWatermarkProgress(Instant.ofEpochMilli(num))
        appMaster.expectMsg(UpdateCheckpointClock(taskContext.taskId, num))
    }

    system.terminate()
    Await.result(system.whenTerminated, Duration.Inf)
  }
} 
Example 54
Source File: CountProcessorSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.state.processor

import java.time.Instant

import scala.concurrent.Await
import scala.concurrent.duration._

import akka.actor.ActorSystem
import akka.testkit.TestProbe
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.state.impl.{InMemoryCheckpointStoreFactory, PersistentStateConfig}
import org.apache.gearpump.streaming.task.UpdateCheckpointClock
import org.apache.gearpump.streaming.transaction.api.CheckpointStoreFactory

class CountProcessorSpec extends PropSpec with PropertyChecks with Matchers {

  property("CountProcessor should update state") {

    val taskContext = MockUtil.mockTaskContext

    implicit val system = ActorSystem("test")

    val longGen = Gen.chooseNum[Long](1, 1000)
    forAll(longGen) {
      (num: Long) =>

        val conf = UserConfig.empty
          .withBoolean(PersistentStateConfig.STATE_CHECKPOINT_ENABLE, true)
          .withLong(PersistentStateConfig.STATE_CHECKPOINT_INTERVAL_MS, num)
          .withValue[CheckpointStoreFactory](PersistentStateConfig.STATE_CHECKPOINT_STORE_FACTORY,
            new InMemoryCheckpointStoreFactory)

        val count = new CountProcessor(taskContext, conf)

        val appMaster = TestProbe()(system)
        when(taskContext.appMaster).thenReturn(appMaster.ref)

        count.onStart(Instant.EPOCH)
        appMaster.expectMsg(UpdateCheckpointClock(taskContext.taskId, 0L))

        for (i <- 0L to num) {
          count.onNext(Message("", i))
          count.getState.get shouldBe Some(i + 1)
        }

        // Time to checkpoint
        count.onWatermarkProgress(Instant.ofEpochMilli(num))
        // Only the state before checkpoint time is checkpointed
        appMaster.expectMsg(UpdateCheckpointClock(taskContext.taskId, num))
    }

    system.terminate()
    Await.result(system.whenTerminated, Duration.Inf)
  }
} 
Example 55
Source File: DefaultMessageCountAppSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.state

import scala.concurrent.Future
import scala.util.Success

import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}
import org.apache.gearpump.streaming.examples.state.MessageCountApp._

class DefaultMessageCountAppSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter with MasterHarness {

  before {
    startActorSystem()
  }

  after {
    shutdownActorSystem()
  }

  protected override def config = TestUtil.DEFAULT_CONFIG

  property("MessageCount should succeed to submit application with required arguments") {
    val requiredArgs = Array(
      s"-$SOURCE_TOPIC", "source",
      s"-$SINK_TOPIC", "sink",
      s"-$ZOOKEEPER_CONNECT", "localhost:2181",
      s"-$BROKER_LIST", "localhost:9092",
      s"-$DEFAULT_FS", "hdfs://localhost:9000"
    )
    val optionalArgs = Array(
      s"-$SOURCE_TASK", "2",
      s"-$COUNT_TASK", "2",
      s"-$SINK_TASK", "2"
    )

    val args = {
      Table(
        ("requiredArgs", "optionalArgs"),
        (requiredArgs, optionalArgs.take(0)),
        (requiredArgs, optionalArgs.take(2)),
        (requiredArgs, optionalArgs.take(4)),
        (requiredArgs, optionalArgs)
      )
    }

    val masterReceiver = createMockMaster()
    forAll(args) { (requiredArgs: Array[String], optionalArgs: Array[String]) =>
      val args = requiredArgs ++ optionalArgs
      Future {
        MessageCountApp.main(masterConfig, args)
      }
      masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
      masterReceiver.reply(SubmitApplicationResult(Success(0)))
    }
  }
} 
Example 56
Source File: WordCountSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.wordcount

import scala.concurrent.Future
import scala.util.Success

import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}

class WordCountSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter with MasterHarness {

  before {
    startActorSystem()
  }

  after {
    shutdownActorSystem()
  }

  protected override def config = TestUtil.DEFAULT_CONFIG

  property("WordCount should succeed to submit application with required arguments") {
    val requiredArgs = Array.empty[String]
    val optionalArgs = Array(
      "-split", "1",
      "-sum", "1")

    val args = {
      Table(
        ("requiredArgs", "optionalArgs"),
        (requiredArgs, optionalArgs)
      )
    }
    val masterReceiver = createMockMaster()
    forAll(args) { (requiredArgs: Array[String], optionalArgs: Array[String]) =>

      val args = requiredArgs ++ optionalArgs

      Future {
        WordCount.main(masterConfig, args)
      }

      masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
      masterReceiver.reply(SubmitApplicationResult(Success(0)))
    }
  }
} 
Example 57
Source File: WordCountSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.wordcountjava

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}
import org.apache.gearpump.streaming.examples.wordcountjava.dsl.WordCount
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import scala.concurrent.Future
import scala.util.Success

class WordCountSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter with MasterHarness {

  before {
    startActorSystem()
  }

  after {
    shutdownActorSystem()
  }

  protected override def config = TestUtil.DEFAULT_CONFIG

  property("WordCount should succeed to submit application with required arguments") {
    val requiredArgs = Array.empty[String]

    val masterReceiver = createMockMaster()

    val args = requiredArgs

    Future {
      WordCount.main(masterConfig, args)
    }

    masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
    masterReceiver.reply(SubmitApplicationResult(Success(0)))
  }
} 
Example 58
Source File: KafkaWordCountSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.examples.kafka.wordcount

import scala.concurrent.Future
import scala.util.Success

import com.typesafe.config.Config
import org.scalatest.prop.PropertyChecks
import org.scalatest.{BeforeAndAfter, Matchers, PropSpec}

import org.apache.gearpump.cluster.ClientToMaster.SubmitApplication
import org.apache.gearpump.cluster.MasterToClient.SubmitApplicationResult
import org.apache.gearpump.cluster.{MasterHarness, TestUtil}

class KafkaWordCountSpec
  extends PropSpec with PropertyChecks with Matchers with BeforeAndAfter with MasterHarness {

  before {
    startActorSystem()
  }

  after {
    shutdownActorSystem()
  }

  override def config: Config = TestUtil.DEFAULT_CONFIG

  property("KafkaWordCount should succeed to submit application with required arguments") {
    val requiredArgs = Array.empty[String]
    val optionalArgs = Array(
      "-source", "1",
      "-split", "1",
      "-sum", "1",
      "-sink", "1")

    val args = {
      Table(
        ("requiredArgs", "optionalArgs"),
        (requiredArgs, optionalArgs)
      )
    }
    val masterReceiver = createMockMaster()
    forAll(args) { (requiredArgs: Array[String], optionalArgs: Array[String]) =>
      val args = requiredArgs ++ optionalArgs

      Future {
        KafkaWordCount.main(masterConfig, args)
      }

      masterReceiver.expectMsgType[SubmitApplication](PROCESS_BOOT_TIME)
      masterReceiver.reply(SubmitApplicationResult(Success(0)))
    }
  }
} 
Example 59
Source File: TwitterSourceSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.twitter

import java.time.Instant

import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.twitter.TwitterSource.{Factory, MessageListener}
import org.mockito.Mockito._
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.mock.MockitoSugar
import org.scalatest.{Matchers, PropSpec}
import org.scalatest.prop.PropertyChecks
import twitter4j.{FilterQuery, TwitterStream}

class TwitterSourceSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  implicit val arbQuery: Arbitrary[Option[FilterQuery]] = Arbitrary {
    Gen.oneOf(None, Some(new FilterQuery()))
  }

  property("TwitterSource should properly setup, poll message and teardown") {
    forAll {
      (query: Option[FilterQuery], startTime: Long) =>
        val factory = mock[Factory]
        val stream = mock[TwitterStream]
        val listener = mock[MessageListener]

        when(factory.getTwitterStream).thenReturn(stream)
        val twitterSource = new TwitterSource(factory, query, listener)

        twitterSource.open(MockUtil.mockTaskContext, Instant.ofEpochMilli(startTime))

        verify(stream).addListener(listener)
        query match {
          case Some(q) =>
            verify(stream).filter(q)
          case None =>
            verify(stream).sample()
        }

        twitterSource.read()
        verify(listener).poll()

        twitterSource.close()
        verify(stream).shutdown()
    }
  }
} 
Example 60
Source File: HadoopCheckpointStoreIntegrationSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.hadoop

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.hadoop.lib.HadoopUtil
import org.apache.gearpump.streaming.hadoop.lib.rotation.FileSizeRotation
import org.apache.gearpump.streaming.task.TaskId

class HadoopCheckpointStoreIntegrationSpec
  extends PropSpec with PropertyChecks with MockitoSugar with Matchers {

  property("HadoopCheckpointStore should persist and recover checkpoints") {
    val fileSizeGen = Gen.chooseNum[Int](100, 1000)
    forAll(fileSizeGen) { (fileSize: Int) =>
      val userConfig = UserConfig.empty
      val taskContext = MockUtil.mockTaskContext
      val hadoopConfig = new Configuration()

      when(taskContext.appId).thenReturn(0)
      when(taskContext.taskId).thenReturn(TaskId(0, 0))

      val rootDirName = "test"
      val rootDir = new Path(rootDirName + Path.SEPARATOR +
        s"v${HadoopCheckpointStoreFactory.VERSION}")
      val subDirName = "app0-task0_0"
      val subDir = new Path(rootDir, subDirName)

      val fs = HadoopUtil.getFileSystemForPath(rootDir, hadoopConfig)
      fs.delete(rootDir, true)
      fs.exists(rootDir) shouldBe false

      val checkpointStoreFactory = new HadoopCheckpointStoreFactory(
        rootDirName, hadoopConfig, new FileSizeRotation(fileSize))
      val checkpointStore = checkpointStoreFactory.getCheckpointStore(subDirName)

      checkpointStore.persist(0L, Array(0.toByte))

      val tempFile = new Path(subDir, "checkpoints-0.store")
      fs.exists(tempFile) shouldBe true

      checkpointStore.persist(1L, Array.fill(fileSize)(0.toByte))
      fs.exists(tempFile) shouldBe false
      fs.exists(new Path(subDir, "checkpoints-0-1.store")) shouldBe true

      checkpointStore.persist(2L, Array(0.toByte))
      val newTempFile = new Path(subDir, "checkpoints-2.store")
      fs.exists(newTempFile) shouldBe true

      for (i <- 0 to 2) {
        val optCp = checkpointStore.recover(i)
        optCp should not be empty
      }
      fs.exists(newTempFile) shouldBe false
      fs.exists(new Path(subDir, "checkpoints-2-2.store")) shouldBe true

      checkpointStore.close()
      fs.delete(rootDir, true)
      fs.close()
    }
  }
} 
Example 61
Source File: FileSizeRotationSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.hadoop.lib.rotation

import java.time.Instant

import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}
import org.apache.gearpump.Time.MilliSeconds

class FileSizeRotationSpec extends PropSpec with PropertyChecks with Matchers {

  val timestampGen = Gen.chooseNum[Long](0L, 1000L)
  val fileSizeGen = Gen.chooseNum[Long](1, Long.MaxValue)

  property("FileSize rotation rotates on file size") {
    forAll(timestampGen, fileSizeGen) { (timestamp: MilliSeconds, fileSize: Long) =>
      val rotation = new FileSizeRotation(fileSize)
      rotation.shouldRotate shouldBe false
      rotation.mark(Instant.ofEpochMilli(timestamp), rotation.maxBytes / 2)
      rotation.shouldRotate shouldBe false
      rotation.mark(Instant.ofEpochMilli(timestamp), rotation.maxBytes)
      rotation.shouldRotate shouldBe true
      rotation.rotate
      rotation.shouldRotate shouldBe false
    }
  }
} 
Example 62
Source File: HBaseSinkSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.external.hbase

import akka.actor.ActorSystem
import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.external.hbase.HBaseSink.{HBaseWriter, HBaseWriterFactory}
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.task.TaskContext
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.TableName
import org.apache.hadoop.hbase.client._
import org.apache.hadoop.hbase.util.Bytes
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class HBaseSinkSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {


  property("HBaseSink should invoke HBaseWriter for writing message to HBase") {

    val hbaseWriter = mock[HBaseWriter]
    val hbaseWriterFactory = mock[HBaseWriterFactory]

    implicit val system: ActorSystem = MockUtil.system

    val userConfig = UserConfig.empty
    val tableName = "hbase"

    when(hbaseWriterFactory.getHBaseWriter(userConfig, tableName))
      .thenReturn(hbaseWriter)

    val hbaseSink = new HBaseSink(userConfig, tableName, hbaseWriterFactory)

    hbaseSink.open(MockUtil.mockTaskContext)

    forAll(Gen.alphaStr) { (value: String) =>
      val message = Message(value)
      hbaseSink.write(message)
      verify(hbaseWriter, atLeastOnce()).put(value)
    }

    hbaseSink.close()
    verify(hbaseWriter).close()
  }

  property("HBaseWriter should insert a row successfully") {

    val table = mock[Table]
    val config = mock[Configuration]
    val connection = mock[Connection]
    val taskContext = mock[TaskContext]

    val map = Map[String, String]("HBASESINK" -> "hbasesink", "TABLE_NAME" -> "hbase.table.name",
      "COLUMN_FAMILY" -> "hbase.table.column.family", "COLUMN_NAME" -> "hbase.table.column.name",
      "HBASE_USER" -> "hbase.user", "GEARPUMP_KERBEROS_PRINCIPAL" -> "gearpump.kerberos.principal",
      "GEARPUMP_KEYTAB_FILE" -> "gearpump.keytab.file"
    )
    val userConfig = new UserConfig(map)
    val tableName = "hbase"
    val row = "row"
    val group = "group"
    val name = "name"
    val value = "3.0"

    when(connection.getTable(TableName.valueOf(tableName))).thenReturn(table)

    val put = new Put(Bytes.toBytes(row))
    put.addColumn(Bytes.toBytes(group), Bytes.toBytes(name), Bytes.toBytes(value))

    val hbaseWriter = new HBaseWriter(connection, tableName)
    hbaseWriter.insert(Bytes.toBytes(row), Bytes.toBytes(group), Bytes.toBytes(name),
      Bytes.toBytes(value))

    verify(table).put(MockUtil.argMatch[Put](_.getRow sameElements Bytes.toBytes(row)))
  }
} 
Example 63
Source File: KuduSinkSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.external.kudu

import akka.actor.ActorSystem
import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.external.kudu.KuduSink.{KuduWriter, KuduWriterFactory}
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.task.TaskContext
import org.apache.kudu.client._
import org.mockito.Mockito._
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class KuduSinkSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {


  property("KuduSink should invoke KuduWriter for writing message to Kudu") {

    val kuduWriter = mock[KuduWriter]
    val kuduWriterFactory = mock[KuduWriterFactory]

    implicit val system: ActorSystem = MockUtil.system

    val userConfig = UserConfig.empty
    val tableName = "kudu"

    when(kuduWriterFactory.getKuduWriter(userConfig, tableName))
      .thenReturn(kuduWriter)

    val kuduSink = new KuduSink(userConfig, tableName, kuduWriterFactory)

    kuduSink.open(MockUtil.mockTaskContext)

    val value = ("key", "value")
    val message = Message(value)
    kuduSink.write(message)
    verify(kuduWriter, atLeastOnce()).put(message.value)

    kuduSink.close()
    verify(kuduWriter).close()
  }

  property("KuduWriter should insert a row successfully") {

    val table = mock[KuduTable]
    val kuduClient = mock[KuduClient]
    val taskContext = mock[TaskContext]

    val map = Map[String, String]("KUDUSINK" -> "kudusink", "TABLE_NAME" -> "kudu.table.name",
      "COLUMN_FAMILY" -> "kudu.table.column.family", "COLUMN_NAME" -> "kudu.table.column.name",
      "KUDU_USER" -> "kudu.user", "GEARPUMP_KERBEROS_PRINCIPAL" -> "gearpump.kerberos.principal",
      "GEARPUMP_KEYTAB_FILE" -> "gearpump.keytab.file"
    )
    val userConfig = new UserConfig(map)
    val tableName = "kudu"
    val key = "key"
    val value = "value"

    when(kuduClient.openTable(tableName)).thenReturn(table)
  }
} 
Example 64
Source File: KafkaSinkSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.kafka

import java.util.Properties

import com.twitter.bijection.Injection
import org.apache.gearpump.streaming.kafka.lib.sink.AbstractKafkaSink.KafkaProducerFactory
import org.apache.gearpump.streaming.kafka.util.KafkaConfig
import org.apache.gearpump.streaming.kafka.util.KafkaConfig.KafkaConfigFactory
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import org.apache.gearpump.Message
import org.apache.gearpump.streaming.MockUtil

class KafkaSinkSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  val dataGen = for {
    topic <- Gen.alphaStr
    key <- Gen.alphaStr
    msg <- Gen.alphaStr
  } yield (topic, Injection[String, Array[Byte]](key), Injection[String, Array[Byte]](msg))

  property("KafkaSink write should send producer record") {
    forAll(dataGen) {
      (data: (String, Array[Byte], Array[Byte])) =>
        val props = mock[Properties]
        val producer = mock[KafkaProducer[Array[Byte], Array[Byte]]]
        val producerFactory = mock[KafkaProducerFactory]
        val configFactory = mock[KafkaConfigFactory]
        val config = mock[KafkaConfig]

        when(configFactory.getKafkaConfig(props)).thenReturn(config)
        when(producerFactory.getKafkaProducer(config)).thenReturn(producer)

        val (topic, key, msg) = data
        val kafkaSink = new KafkaSink(topic, props, configFactory, producerFactory)
        kafkaSink.write(Message((key, msg)))
        verify(producer).send(MockUtil.argMatch[ProducerRecord[Array[Byte], Array[Byte]]](
          r => r.topic == topic && (r.key sameElements key) && (r.value sameElements msg)))
        kafkaSink.write(Message(msg))
        verify(producer).send(MockUtil.argMatch[ProducerRecord[Array[Byte], Array[Byte]]](
          r => r.topic() == topic && (r.key == null) && (r.value() sameElements msg)
        ))
        kafkaSink.close()
    }
  }

  property("KafkaSink close should close kafka producer") {
    val props = mock[Properties]
    val producer = mock[KafkaProducer[Array[Byte], Array[Byte]]]
    val producerFactory = mock[KafkaProducerFactory]
    val configFactory = mock[KafkaConfigFactory]
    val config = mock[KafkaConfig]

    when(configFactory.getKafkaConfig(props)).thenReturn(config)
    when(producerFactory.getKafkaProducer(config)).thenReturn(producer)

    val kafkaSink = new KafkaSink("topic", props, configFactory, producerFactory)
    kafkaSink.close()
    verify(producer).close()
  }
} 
Example 65
Source File: KafkaConsumerSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.kafka.lib.source.consumer

import com.twitter.bijection.Injection
import kafka.api.OffsetRequest
import kafka.common.TopicAndPartition
import kafka.consumer.SimpleConsumer
import kafka.message.{Message, MessageAndOffset}
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class KafkaConsumerSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {
  val messageGen = Gen.alphaStr map (msg => new Message(Injection[String, Array[Byte]](msg)))
  val messageNumGen = Gen.choose[Int](0, 1000)
  val topicAndPartitionGen = for {
    topic <- Gen.alphaStr
    partition <- Gen.choose[Int](0, Int.MaxValue)
  } yield (topic, partition)

  property("KafkaConsumer should iterate MessageAndOffset calling hasNext and next") {
    forAll(messageGen, messageNumGen, topicAndPartitionGen) {
      (message: Message, num: Int, topicAndPartition: (String, Int)) =>
        val (topic, partition) = topicAndPartition
        val consumer = mock[SimpleConsumer]
        when(consumer.earliestOrLatestOffset(TopicAndPartition(topic, partition),
          OffsetRequest.EarliestTime, -1)).thenReturn(0)
        val iterator = 0.until(num).map(index => MessageAndOffset(message, index.toLong)).iterator
        val getIterator = (offset: Long) => iterator
        val kafkaConsumer = new KafkaConsumer(consumer, topic, partition, getIterator)
        0.until(num).foreach { i =>
          kafkaConsumer.hasNext shouldBe true
          val kafkaMessage = kafkaConsumer.next
          kafkaMessage.offset shouldBe i.toLong
          kafkaMessage.key shouldBe None
        }
        kafkaConsumer.hasNext shouldBe false
    }
  }

  val startOffsetGen = Gen.choose[Long](1L, 1000L)
  property("KafkaConsumer setStartOffset should reset internal iterator") {
    forAll(topicAndPartitionGen, startOffsetGen) {
      (topicAndPartition: (String, Int), startOffset: Long) =>
        val (topic, partition) = topicAndPartition
        val consumer = mock[SimpleConsumer]
        val getIterator = mock[Long => Iterator[MessageAndOffset]]
        when(consumer.earliestOrLatestOffset(TopicAndPartition(topic, partition),
          OffsetRequest.EarliestTime, -1)).thenReturn(0)
        val kafkaConsumer = new KafkaConsumer(consumer, topic, partition, getIterator)
        kafkaConsumer.setStartOffset(startOffset)
        verify(getIterator).apply(startOffset)
    }
  }

  property("KafkaConsumer close should close SimpleConsumer") {
    forAll(topicAndPartitionGen) {
      (topicAndPartition: (String, Int)) =>
        val (topic, partition) = topicAndPartition
        val consumer = mock[SimpleConsumer]
        when(consumer.earliestOrLatestOffset(TopicAndPartition(topic, partition),
          OffsetRequest.EarliestTime, -1)).thenReturn(0)
        val getIterator = mock[Long => Iterator[MessageAndOffset]]
        val kafkaConsumer = new KafkaConsumer(consumer, topic, partition, getIterator)
        kafkaConsumer.close()
        verify(consumer).close()
    }
  }
} 
Example 66
Source File: DefaultKafkaMessageDecoderSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.kafka.lib.source

import com.twitter.bijection.Injection
import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class DefaultKafkaMessageDecoderSpec extends PropSpec with PropertyChecks with Matchers {
  property("DefaultMessageDecoder should keep the original bytes data in Message") {
    val decoder = new DefaultKafkaMessageDecoder()
    forAll(Gen.chooseNum[Int](0, 100), Gen.alphaStr) { (k: Int, v: String) =>
      val kbytes = Injection[Int, Array[Byte]](k)
      val vbytes = Injection[String, Array[Byte]](v)
      val msgAndWmk = decoder.fromBytes(kbytes, vbytes)
      val message = msgAndWmk.message
      val watermark = msgAndWmk.watermark
      message.value shouldBe vbytes
      // processing time as message timestamp and watermark
      message.timestamp shouldBe watermark
    }
  }
} 
Example 67
Source File: DefaultPartitionGrouperSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.kafka.lib.source.grouper

import kafka.common.TopicAndPartition
import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class DefaultPartitionGrouperSpec extends PropSpec with PropertyChecks with Matchers {
  property("KafkaDefaultGrouper should group TopicAndPartitions in a round-robin way") {
    forAll(Gen.posNum[Int], Gen.posNum[Int], Gen.posNum[Int]) {
      (topicNum: Int, partitionNum: Int, taskNum: Int) => {
        val topicAndPartitions = for {
          t <- 0.until(topicNum)
          p <- 0.until(partitionNum)
        } yield TopicAndPartition("topic" + t, p)
        0.until(taskNum).foreach { taskIndex =>
          val grouper = new DefaultPartitionGrouper
          grouper.group(taskNum, taskIndex, topicAndPartitions.toArray).forall(
            tp => topicAndPartitions.indexOf(tp) % taskNum == taskIndex)
        }
      }
    }
  }
} 
Example 68
Source File: StormSpoutOutputCollectorSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.storm.producer

import backtype.storm.spout.ISpout
import backtype.storm.utils.Utils
import org.apache.gearpump.experiments.storm.util.StormOutputCollector
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import scala.collection.JavaConverters._

class StormSpoutOutputCollectorSpec
  extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  property("StormSpoutOutputCollector should call StormOutputCollector") {
    val valGen = Gen.oneOf(Gen.alphaStr, Gen.alphaChar, Gen.chooseNum[Int](0, 1000))
    val valuesGen = Gen.listOf[AnyRef](valGen)

    forAll(valuesGen) { (values: List[AnyRef]) =>
      val collector = mock[StormOutputCollector]
      val spout = mock[ISpout]
      val streamId = Utils.DEFAULT_STREAM_ID
      val spoutCollector = new StormSpoutOutputCollector(collector, spout, false)
      spoutCollector.emit(streamId, values.asJava, null)
      verify(collector).emit(streamId, values.asJava)
    }
  }
} 
Example 69
Source File: StormBoltOutputCollectorSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.storm.processor

import backtype.storm.tuple.Tuple
import backtype.storm.utils.Utils
import org.apache.gearpump.experiments.storm.util.StormOutputCollector
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import scala.collection.JavaConverters._

class StormBoltOutputCollectorSpec
  extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  property("StormBoltOutputCollector should call StormOutputCollector") {
    val valGen = Gen.oneOf(Gen.alphaStr, Gen.alphaChar, Gen.chooseNum[Int](0, 1000))
    val valuesGen = Gen.listOf[AnyRef](valGen)

    forAll(valuesGen) { (values: List[AnyRef]) =>
      val collector = mock[StormOutputCollector]
      val boltCollector = new StormBoltOutputCollector(collector)
      val streamId = Utils.DEFAULT_STREAM_ID
      boltCollector.emit(streamId, null, values.asJava)
      verify(collector).emit(streamId, values.asJava)
    }
  }

  property("StormBoltOutputCollector should throw on fail") {
    val collector = mock[StormOutputCollector]
    val tuple = mock[Tuple]
    val boltCollector = new StormBoltOutputCollector(collector)
    an[Exception] should be thrownBy boltCollector.fail(tuple)
  }
} 
Example 70
Source File: StormPartitionerSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.storm.partitioner

import java.util.{List => JList}
import scala.collection.JavaConverters._

import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import org.apache.gearpump.Message
import org.apache.gearpump.experiments.storm.topology.GearpumpTuple
import org.apache.gearpump.streaming.partitioner.Partitioner

class StormPartitionerSpec extends PropSpec with PropertyChecks with Matchers {

  property("StormPartitioner should get partitions directed by message and target") {
    val idGen = Gen.chooseNum[Int](0, Int.MaxValue)
    val componentsGen = Gen.listOf[String](Gen.alphaStr).map(_.distinct).suchThat(_.size > 1)
    val partitionsGen = Gen.listOf[Int](idGen).suchThat(_.nonEmpty).map(_.distinct.sorted.toArray)
    val tupleFactoryGen = for {
      values <- Gen.listOf[String](Gen.alphaStr).map(_.asJava.asInstanceOf[JList[AnyRef]])
      sourceTaskId <- idGen
      sourceStreamId <- Gen.alphaStr
    } yield (targetPartitions: Map[String, Array[Int]]) => {
      new GearpumpTuple(values, new Integer(sourceTaskId), sourceStreamId, targetPartitions)
    }

    forAll(tupleFactoryGen, idGen, componentsGen, partitionsGen) {
      (tupleFactory: Map[String, Array[Int]] => GearpumpTuple, id: Int,
        components: List[String], partitions: Array[Int]) => {
        val currentPartitionId = id
        val targetPartitions = components.init.map(c => (c, partitions)).toMap
        val tuple = tupleFactory(targetPartitions)
        targetPartitions.foreach {
          case (target, ps) => {
            val partitioner = new StormPartitioner(target)
            ps shouldBe partitioner.getPartitions(Message(tuple), ps.last + 1,
              currentPartitionId)
          }
        }
        val partitionNum = id
        val nonTarget = components.last
        val partitioner = new StormPartitioner(nonTarget)

        partitioner.getPartitions(Message(tuple), partitionNum,
          currentPartitionId) shouldBe List(Partitioner.UNKNOWN_PARTITION_ID)
      }
    }
  }
} 
Example 71
Source File: StormSerializerPoolSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.storm.util

import java.util.{HashMap => JHashMap, List => JList, Map => JMap}
import scala.collection.JavaConverters._

import akka.actor.ExtendedActorSystem
import backtype.storm.utils.Utils
import com.esotericsoftware.kryo.Kryo
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.experiments.storm.topology.GearpumpTuple
import org.apache.gearpump.experiments.storm.util.StormConstants._
import org.apache.gearpump.streaming.MockUtil

class StormSerializerPoolSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  property("StormSerializerPool should create and manage StormSerializer") {
    val taskContext = MockUtil.mockTaskContext
    val serializerPool = new StormSerializationFramework
    val system = taskContext.system.asInstanceOf[ExtendedActorSystem]
    implicit val actorSystem = system
    val stormConfig = Utils.readDefaultConfig.asInstanceOf[JMap[AnyRef, AnyRef]]
    val config = UserConfig.empty.withValue[JMap[AnyRef, AnyRef]](STORM_CONFIG, stormConfig)
    serializerPool.init(system, config)
    serializerPool.get shouldBe a[StormSerializer]
  }

  property("StormSerializer should serialize and deserialize GearpumpTuple") {
    val tupleGen = for {
      values <- Gen.listOf[String](Gen.alphaStr).map(_.asJava.asInstanceOf[JList[AnyRef]])
      sourceTaskId <- Gen.chooseNum[Int](0, Int.MaxValue)
      sourceStreamId <- Gen.alphaStr
    } yield new GearpumpTuple(values, new Integer(sourceTaskId), sourceStreamId, null)

    val kryo = new Kryo
    forAll(tupleGen) { (tuple: GearpumpTuple) =>
      val serializer = new StormSerializer(kryo)
      serializer.deserialize(serializer.serialize(tuple)) shouldBe tuple
    }
  }
} 
Example 72
Source File: StormOutputCollectorSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.storm.util

import java.util.{List => JList, Map => JMap}

import scala.collection.JavaConverters._
import backtype.storm.generated.Grouping
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}
import org.apache.gearpump.{Message, Time}
import org.apache.gearpump.Time.MilliSeconds
import org.apache.gearpump.experiments.storm.topology.GearpumpTuple
import org.apache.gearpump.streaming.MockUtil

class StormOutputCollectorSpec
  extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  private val stormTaskId = 0
  private val streamIdGen = Gen.alphaStr
  private val valuesGen = Gen.listOf[String](Gen.alphaStr).map(_.asJava.asInstanceOf[JList[AnyRef]])
  private val timestampGen = Gen.chooseNum[Long](0L, 1000L)

  property("StormOutputCollector emits tuple values into a stream") {
    forAll(timestampGen, streamIdGen, valuesGen) {
      (timestamp: MilliSeconds, streamId: String, values: JList[AnyRef]) =>
        val targets = mock[JMap[String, JMap[String, Grouping]]]
        val taskToComponent = mock[JMap[Integer, String]]
        val getTargetPartitionsFn = mock[(String, JList[AnyRef]) =>
          (Map[String, Array[Int]], JList[Integer])]
        val targetPartitions = mock[Map[String, Array[Int]]]
        val targetStormTaskIds = mock[JList[Integer]]
        when(getTargetPartitionsFn(streamId, values)).thenReturn((targetPartitions,
          targetStormTaskIds))
        val taskContext = MockUtil.mockTaskContext
        val stormOutputCollector = new StormOutputCollector(stormTaskId, taskToComponent,
          targets, getTargetPartitionsFn, taskContext, Time.MIN_TIME_MILLIS)

        when(targets.containsKey(streamId)).thenReturn(false)
        stormOutputCollector.emit(streamId, values) shouldBe StormOutputCollector.EMPTY_LIST
        verify(taskContext, times(0)).output(anyObject[Message])

        when(targets.containsKey(streamId)).thenReturn(true)
        stormOutputCollector.setTimestamp(timestamp)
        stormOutputCollector.emit(streamId, values) shouldBe targetStormTaskIds
        verify(taskContext, times(1)).output(MockUtil.argMatch[Message]({
          message: Message =>
            val expected = new GearpumpTuple(values, stormTaskId, streamId, targetPartitions)
            message.value == expected && message.timestamp.toEpochMilli == timestamp
        }))
    }
  }

  property("StormOutputCollector emit direct to a task") {
    val idGen = Gen.chooseNum[Int](0, 1000)
    val targetGen = Gen.alphaStr
    forAll(idGen, targetGen, timestampGen, streamIdGen, valuesGen) {
      (id: Int, target: String, timestamp: Long, streamId: String, values: JList[AnyRef]) =>
        val targets = mock[JMap[String, JMap[String, Grouping]]]
        val taskToComponent = mock[JMap[Integer, String]]
        when(taskToComponent.get(id)).thenReturn(target)
        val getTargetPartitionsFn = mock[(String, JList[AnyRef]) =>
          (Map[String, Array[Int]], JList[Integer])]
        val targetPartitions = mock[Map[String, Array[Int]]]
        val targetStormTaskIds = mock[JList[Integer]]
        when(getTargetPartitionsFn(streamId, values)).thenReturn((targetPartitions,
          targetStormTaskIds))
        val taskContext = MockUtil.mockTaskContext
        val stormOutputCollector = new StormOutputCollector(stormTaskId, taskToComponent,
          targets, getTargetPartitionsFn, taskContext, Time.MIN_TIME_MILLIS)

        when(targets.containsKey(streamId)).thenReturn(false)
        verify(taskContext, times(0)).output(anyObject[Message])

        when(targets.containsKey(streamId)).thenReturn(true)
        stormOutputCollector.setTimestamp(timestamp)
        stormOutputCollector.emitDirect(id, streamId, values)
        val partitions = Array(StormUtil.stormTaskIdToGearpump(id).index)
        verify(taskContext, times(1)).output(MockUtil.argMatch[Message]({
          message: Message => {
            val expected = new GearpumpTuple(values, stormTaskId, streamId,
            Map(target -> partitions))

            val result = message.value == expected && message.timestamp.toEpochMilli == timestamp
            result
          }
        }))
    }
  }
} 
Example 73
Source File: GearpumpTupleSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.storm.topology

import java.util.{List => JList}

import backtype.storm.task.GeneralTopologyContext
import backtype.storm.tuple.Fields
import org.apache.gearpump.Time.MilliSeconds
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import scala.collection.JavaConverters._

class GearpumpTupleSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  property("GearpumpTuple should create Storm Tuple") {
    val tupleGen = for {
      values <- Gen.listOf[String](Gen.alphaStr).map(_.distinct.asJava.asInstanceOf[JList[AnyRef]])
      sourceTaskId <- Gen.chooseNum[Int](0, Int.MaxValue)
      sourceStreamId <- Gen.alphaStr
    } yield new GearpumpTuple(values, new Integer(sourceTaskId), sourceStreamId, null)

    forAll(tupleGen, Gen.alphaStr, Gen.chooseNum[Long](0, Long.MaxValue)) {
      (gearpumpTuple: GearpumpTuple, componentId: String, timestamp: MilliSeconds) =>
        val topologyContext = mock[GeneralTopologyContext]
        val fields = new Fields(gearpumpTuple.values.asScala.map(_.asInstanceOf[String]): _*)
        when(topologyContext.getComponentId(gearpumpTuple.sourceTaskId)).thenReturn(componentId)
        when(topologyContext.getComponentOutputFields(
          componentId, gearpumpTuple.sourceStreamId)).thenReturn(fields)

        val tuple = gearpumpTuple.toTuple(topologyContext, timestamp)

        tuple shouldBe a[TimedTuple]
        val timedTuple = tuple.asInstanceOf[TimedTuple]
        timedTuple.getValues shouldBe gearpumpTuple.values
        timedTuple.getSourceTask shouldBe gearpumpTuple.sourceTaskId
        timedTuple.getSourceComponent shouldBe componentId
        timedTuple.getSourceStreamId shouldBe gearpumpTuple.sourceStreamId
        timedTuple.getMessageId shouldBe null
        timedTuple.getFields shouldBe fields
        timedTuple.timestamp shouldBe timestamp
    }
  }
} 
Example 74
Source File: DefaultWindowRunnerSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.dsl.window.impl

import java.time.{Duration, Instant}

import org.apache.gearpump.Message
import org.apache.gearpump.streaming.dsl.api.functions.ReduceFunction
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.dsl.plan.functions.FoldRunner
import org.apache.gearpump.streaming.dsl.window.api.SessionWindows
import org.apache.gearpump.streaming.source.Watermark
import org.scalatest.{Matchers, PropSpec}
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks

class DefaultWindowRunnerSpec extends PropSpec with PropertyChecks
  with Matchers with MockitoSugar {

  property("DefaultWindowRunner should handle SessionWindow") {

    val data = List(
      Message(("foo", 1L), Instant.ofEpochMilli(1L)),
      Message(("foo", 1L), Instant.ofEpochMilli(15L)),
      Message(("foo", 1L), Instant.ofEpochMilli(25L)),
      Message(("foo", 1L), Instant.ofEpochMilli(26L))
    )

    type KV = (String, Long)
    implicit val system = MockUtil.system
    val reduce = ReduceFunction[KV]((kv1, kv2) => (kv1._1, kv1._2 + kv2._2))
    val windows = SessionWindows.apply(Duration.ofMillis(4L))
    val windowRunner = new WindowOperator[KV, Option[KV]](windows,
      new FoldRunner[KV, Option[KV]](reduce, "reduce"))

    data.foreach(m => windowRunner.foreach(TimestampedValue(m.value.asInstanceOf[KV], m.timestamp)))
    windowRunner.trigger(Watermark.MAX).outputs.toList shouldBe
      List(
        TimestampedValue(Some(("foo", 1)), Instant.ofEpochMilli(4)),
        TimestampedValue(Some(("foo", 1)), Instant.ofEpochMilli(18)),
        TimestampedValue(Some(("foo", 2)), Instant.ofEpochMilli(29))
      )
  }

} 
Example 75
Source File: GroupByTaskSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.dsl.task

import java.time.Instant

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.dsl.plan.functions.DummyRunner
import org.apache.gearpump.streaming.dsl.window.api.GlobalWindows
import org.apache.gearpump.streaming.{Constants, MockUtil}
import org.apache.gearpump.streaming.dsl.window.impl.WindowOperator
import org.apache.gearpump.streaming.source.Watermark
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.{Matchers, PropSpec}
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks

class GroupByTaskSpec extends PropSpec with PropertyChecks
  with Matchers with MockitoSugar {

  property("GroupByTask should trigger on watermark") {
    val longGen = Gen.chooseNum[Long](1L, 1000L).map(Instant.ofEpochMilli)

    forAll(longGen) { (time: Instant) =>
      val groupBy = mock[Any => Int]
      val windowRunner = new WindowOperator[Any, Any](GlobalWindows(), new DummyRunner[Any])
      val context = MockUtil.mockTaskContext
      val config = UserConfig.empty
        .withValue(
          Constants.GEARPUMP_STREAMING_OPERATOR, windowRunner)(MockUtil.system)

      val task = new GroupByTask[Any, Int, Any](groupBy, context, config)
      val value = time
      val message = Message(value, time)
      when(groupBy(time)).thenReturn(0)
      task.onNext(message)

      task.onWatermarkProgress(Watermark.MAX)
      verify(context).output(message)
    }
  }

} 
Example 76
Source File: TransformTaskSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.dsl.task

import java.time.Instant

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.dsl.window.impl.{TimestampedValue, TriggeredOutputs, StreamingOperator}
import org.mockito.Mockito.{verify, when}
import org.scalacheck.Gen
import org.scalatest.{Matchers, PropSpec}
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks

class TransformTaskSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  property("MergeTask should trigger on watermark") {
    val longGen = Gen.chooseNum[Long](1L, 1000L)
    val watermarkGen = longGen.map(Instant.ofEpochMilli)

    forAll(watermarkGen) { (watermark: Instant) =>
      val windowRunner = mock[StreamingOperator[Any, Any]]
      val context = MockUtil.mockTaskContext
      val config = UserConfig.empty
      val task = new TransformTask[Any, Any](windowRunner, context, config)
      val time = watermark.minusMillis(1L)
      val value: Any = time
      val message = Message(value, time)

      task.onNext(message)
      verify(windowRunner).foreach(TimestampedValue(value, time))

      when(windowRunner.trigger(watermark)).thenReturn(
        TriggeredOutputs(Some(TimestampedValue(value, time)), watermark))
      task.onWatermarkProgress(watermark)
      verify(context).output(message)
      verify(context).updateWatermark(watermark)
    }
  }

} 
Example 77
Source File: DAGSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming

import org.apache.gearpump.streaming.partitioner.PartitionerDescription
import org.apache.gearpump.streaming.task.TaskId
import org.apache.gearpump.util.Graph
import org.apache.gearpump.util.Graph.Node
import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class DAGSpec extends PropSpec with PropertyChecks with Matchers {

  val parallelismGen = Gen.chooseNum[Int](1, 100)

  property("DAG should be built correctly for a single task") {
    forAll(parallelismGen) { (parallelism: Int) =>
      val task = ProcessorDescription(id = 0, taskClass = "task", parallelism = parallelism)
      val graph = Graph[ProcessorDescription, PartitionerDescription](task)
      val dag = DAG(graph)
      dag.processors.size shouldBe 1
      assert(dag.taskCount == parallelism)
      dag.tasks.sortBy(_.index) shouldBe (0 until parallelism).map(index => TaskId(0, index))
      dag.graph.getEdges shouldBe empty
    }
  }
} 
Example 78
Source File: InMemoryCheckpointStoreSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.state.impl

import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class InMemoryCheckpointStoreSpec extends PropSpec with PropertyChecks with Matchers {

  property("InMemoryCheckpointStore should provide read / write checkpoint") {
    val timestampGen = Gen.chooseNum[Long](1, 1000)
    val checkpointGen = Gen.alphaStr.map(_.getBytes("UTF-8"))
    forAll(timestampGen, checkpointGen) { (timestamp: Long, checkpoint: Array[Byte]) =>
      val store = new InMemoryCheckpointStore
      store.recover(timestamp) shouldBe None
      store.persist(timestamp, checkpoint)
      store.recover(timestamp) shouldBe Some(checkpoint)
    }
  }
} 
Example 79
Source File: WindowSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.state.impl

import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import org.apache.gearpump.Time.MilliSeconds

class WindowSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  val windowSizeGen = Gen.chooseNum[Long](1L, 1000L)
  val windowStepGen = Gen.chooseNum[Long](1L, 1000L)
  val timestampGen = Gen.chooseNum[Long](0L, 1000L)
  property("Window should only slide when time passes window end") {
    forAll(timestampGen, windowSizeGen, windowStepGen) {
      (timestamp: MilliSeconds, windowSize: Long, windowStep: Long) =>
        val window = new Window(windowSize, windowStep)
        window.shouldSlide shouldBe false
        window.update(timestamp)
        window.shouldSlide shouldBe timestamp >= windowSize
    }
  }

  property("Window should slide by one or to given timestamp") {
    forAll(timestampGen, windowSizeGen, windowStepGen) {
      (timestamp: MilliSeconds, windowSize: Long, windowStep: Long) =>
        val window = new Window(windowSize, windowStep)
        window.range shouldBe(0L, windowSize)

        window.slideOneStep()
        window.range shouldBe(windowStep, windowSize + windowStep)

        window.slideTo(timestamp)
        val (startTime, endTime) = window.range
        if (windowStep > windowSize) {
          timestamp should (be >= startTime and be < (startTime + windowStep))
        } else {
          timestamp should (be >= startTime and be < endTime)
        }
    }
  }
} 
Example 80
Source File: CheckpointManagerSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.state.impl

import org.mockito.Mockito._
import org.mockito.{Matchers => MockitoMatchers}
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

import org.apache.gearpump.Time.MilliSeconds
import org.apache.gearpump.streaming.transaction.api.CheckpointStore

class CheckpointManagerSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  val timestampGen = Gen.chooseNum[Long](0L, 1000L)
  val checkpointIntervalGen = Gen.chooseNum[Long](100L, 10000L)
  property("CheckpointManager should recover from CheckpointStore") {
    forAll(timestampGen, checkpointIntervalGen) {
      (timestamp: MilliSeconds, checkpointInterval: Long) =>
        val checkpointStore = mock[CheckpointStore]
        val checkpointManager =
          new CheckpointManager(checkpointInterval, checkpointStore)
        checkpointManager.recover(timestamp)

        verify(checkpointStore).recover(timestamp)
    }
  }

  property("CheckpointManager should write checkpoint to CheckpointStore") {
    val checkpointGen = Gen.alphaStr.map(_.getBytes("UTF-8"))
    forAll(timestampGen, checkpointIntervalGen, checkpointGen) {
      (timestamp: MilliSeconds, checkpointInterval: Long, checkpoint: Array[Byte]) =>
        val checkpointStore = mock[CheckpointStore]
        val checkpointManager =
          new CheckpointManager(checkpointInterval, checkpointStore)
        checkpointManager.checkpoint(timestamp, checkpoint)

        verify(checkpointStore).persist(timestamp, checkpoint)
    }
  }

  property("CheckpointManager should close CheckpointStore") {
    forAll(checkpointIntervalGen) {
      (checkpointInterval: Long) =>
        val checkpointStore = mock[CheckpointStore]
        val checkpointManager =
          new CheckpointManager(checkpointInterval, checkpointStore)
        checkpointManager.close()
        verify(checkpointStore).close()
    }
  }

  property("CheckpointManager should update checkpoint time according to max message timestamp") {
    forAll(timestampGen, checkpointIntervalGen) {
      (timestamp: MilliSeconds, checkpointInterval: Long) =>
        val checkpointStore = mock[CheckpointStore]
        val checkpointManager =
          new CheckpointManager(checkpointInterval, checkpointStore)
        checkpointManager.update(timestamp)
        checkpointManager.getMaxMessageTime shouldBe timestamp

        val checkpointTime = checkpointManager.getCheckpointTime.get
        timestamp should (be < checkpointTime and be >= (checkpointTime - checkpointInterval))

        checkpointManager.checkpoint(checkpointTime, Array.empty[Byte])
        verify(checkpointStore).persist(MockitoMatchers.eq(checkpointTime),
          MockitoMatchers.anyObject[Array[Byte]]())
        checkpointManager.getCheckpointTime shouldBe empty
    }
  }
} 
Example 81
Source File: DataSinkTaskSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.sink

import java.time.Instant

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.prop.PropertyChecks
import org.scalatest.{PropSpec, Matchers}

class DataSinkTaskSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {


  property("DataSinkTask.onStart should call DataSink.open" ) {
    forAll(Gen.chooseNum[Long](0L, 1000L).map(Instant.ofEpochMilli)) { (startTime: Instant) =>
      val taskContext = MockUtil.mockTaskContext
      val config = UserConfig.empty
      val dataSink = mock[DataSink]
      val sinkTask = new DataSinkTask(taskContext, config, dataSink)
      sinkTask.onStart(startTime)
      verify(dataSink).open(taskContext)
    }
  }

  property("DataSinkTask.onNext should call DataSink.write") {
    forAll(Gen.alphaStr) { (str: String) =>
      val taskContext = MockUtil.mockTaskContext
      val config = UserConfig.empty
      val dataSink = mock[DataSink]
      val sinkTask = new DataSinkTask(taskContext, config, dataSink)
      val msg = Message(str)
      sinkTask.onNext(msg)
      verify(dataSink).write(msg)
    }
  }


  property("DataSinkTask.onStop should call DataSink.close") {
    val taskContext = MockUtil.mockTaskContext
    val config = UserConfig.empty
    val dataSink = mock[DataSink]
    val sinkTask = new DataSinkTask(taskContext, config, dataSink)
    sinkTask.onStop()
    verify(dataSink).close()
  }

} 
Example 82
Source File: DataSourceTaskSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.source

import java.time.Instant

import org.apache.gearpump.Message
import org.apache.gearpump.cluster.UserConfig
import org.apache.gearpump.streaming.MockUtil
import org.apache.gearpump.streaming.dsl.window.impl.{TimestampedValue, TriggeredOutputs, StreamingOperator}
import org.mockito.Mockito._
import org.scalacheck.Gen
import org.scalatest.mock.MockitoSugar
import org.scalatest.{Matchers, PropSpec}
import org.scalatest.prop.PropertyChecks

class DataSourceTaskSpec extends PropSpec with PropertyChecks with Matchers with MockitoSugar {

  property("DataSourceTask should setup data source") {
    forAll(Gen.chooseNum[Long](0L, 1000L).map(Instant.ofEpochMilli)) {
      (startTime: Instant) =>
      val taskContext = MockUtil.mockTaskContext
      implicit val system = MockUtil.system
      val dataSource = mock[DataSource]
      val config = UserConfig.empty
        .withInt(DataSourceConfig.SOURCE_READ_BATCH_SIZE, 1)
        val runner = mock[StreamingOperator[Any, Any]]
      val sourceTask = new DataSourceTask[Any, Any](dataSource, runner, taskContext, config)

      sourceTask.onStart(startTime)

      verify(dataSource).open(taskContext, startTime)
    }
  }

  property("DataSourceTask should read from DataSource and transform inputs") {
    forAll(Gen.alphaStr, Gen.chooseNum[Long](0L, 1000L).map(Instant.ofEpochMilli)) {
      (str: String, timestamp: Instant) =>
        val taskContext = MockUtil.mockTaskContext
        implicit val system = MockUtil.system
        val dataSource = mock[DataSource]
        val config = UserConfig.empty
          .withInt(DataSourceConfig.SOURCE_READ_BATCH_SIZE, 1)
        val processor = mock[StreamingOperator[String, String]]
        val sourceTask = new DataSourceTask[String, String](dataSource, processor,
          taskContext, config)
        val msg = Message(str, timestamp)
        when(dataSource.read()).thenReturn(msg)

        when(processor.flatMap(new TimestampedValue[String](msg))).thenReturn(
          Some(new TimestampedValue[String](msg))
        )
        when(processor.trigger(Watermark.MAX)).thenReturn(
          TriggeredOutputs[String](None, Watermark.MAX))

        sourceTask.onNext(Message("next"))
        sourceTask.onWatermarkProgress(Watermark.MAX)

        verify(taskContext).output(msg)
        verify(taskContext).updateWatermark(Watermark.MAX)
    }
  }

  property("DataSourceTask should teardown DataSource") {
    val taskContext = MockUtil.mockTaskContext
    implicit val system = MockUtil.system
    val dataSource = mock[DataSource]
    val config = UserConfig.empty
      .withInt(DataSourceConfig.SOURCE_READ_BATCH_SIZE, 1)
    val runner = mock[StreamingOperator[Any, Any]]
    val sourceTask = new DataSourceTask[Any, Any](dataSource, runner, taskContext, config)

    sourceTask.onStop()

    verify(dataSource).close()
  }
} 
Example 83
Source File: GTMSnippetSpec.scala    From play-ui   with Apache License 2.0 5 votes vote down vote up
package uk.gov.hmrc.play.views.layouts

import org.jsoup.Jsoup
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, WordSpec}
import play.api.Configuration
import play.api.test.Helpers._
import play.twirl.api.Html
import uk.gov.hmrc.play.config.GTMConfig
import uk.gov.hmrc.play.views.html.layouts.GTMSnippet
import scala.collection.JavaConverters._

class GTMSnippetSpec extends WordSpec with Matchers with PropertyChecks {
  "gtmSnippet" should {
    "include script tag with appropriate URL for container if gtm.container is defined" in {
      val transitionalUrl = "http://s3bucket/transitional/gtm.js"
      val mainUrl         = "http://s3bucket/main/gtm.js"
      val dataLayerUrl    = "http://s3bucket/include/gtm_dl.js"
      val containersAndUrls =
        Table(
          ("container", "url"),
          ("transitional", transitionalUrl),
          ("main", mainUrl)
        )

      forAll(containersAndUrls) { (container: String, url: String) =>
        val snippet = createSnippet(
          container       = Some(container),
          mainUrl         = Some(mainUrl),
          transitionalUrl = Some(transitionalUrl),
          dataLayerUrl    = Some(dataLayerUrl))
        script(snippet()) should contain(s"$url")
        script(snippet()) should contain(s"$dataLayerUrl")
      }
    }

    "not include script tag if gtm.container is not defined" in {
      val snippet = createSnippet(None, None, None, None)
      script(snippet()) shouldBe empty
    }
  }

  private def createSnippet(
    container: Option[String],
    mainUrl: Option[String],
    transitionalUrl: Option[String],
    dataLayerUrl: Option[String]): GTMSnippet = {
    val config = new GTMConfig(
      Configuration(
        Seq(
          container.map("gtm.container"              -> _),
          mainUrl.map("gtm.main.url"                 -> _),
          transitionalUrl.map("gtm.transitional.url" -> _),
          dataLayerUrl.map("gtm.data.layer.url"      -> _)
        ).flatten: _*))
    new GTMSnippet(config)
  }

  private def script(html: Html): List[String] = {
    val content  = contentAsString(html)
    val document = Jsoup.parse(content)
    document
      .head()
      .select("script")
      .iterator()
      .asScala
      .toList
      .map(_.attr("src"))
  }
} 
Example 84
Source File: ColorSpec.scala    From reftree   with GNU General Public License v3.0 5 votes vote down vote up
package reftree.geometry

import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}

class ColorSpec extends FlatSpec with Matchers with PropertyChecks {
  val e = 0.005

  def sameRgba(left: Color.RGBA, right: Color.RGBA) =
    (Color.rgbaComponents.get(left) zip Color.rgbaComponents.get(right)) foreach {
      case (x, y) ⇒ x shouldEqual y +- e
    }

  def sameHsla(left: Color.HSLA, right: Color.HSLA) =
    (Color.hslaComponents.get(left) zip Color.hslaComponents.get(right)) foreach {
      case (x, y) ⇒ x shouldEqual y +- e
    }

  val genColor = for {
    a ← Gen.choose(0.0, 1.0)
    b ← Gen.choose(0.0, 1.0)
    c ← Gen.choose(0.0, 1.0)
    d ← Gen.choose(0.0, 1.0)
  } yield Seq(a, b, c, d)

  val genRgba = genColor.map(Color.rgbaComponents.apply)
  val genHsla = genColor.map(Color.hslaComponents.apply)

  it should "parse RGBA colors" in {
    val colors = Table(
      "string" → "color",
      "#104E8b" → Color.RGBA(0x10 / 255.0, 0x4e / 255.0, 0x8b / 255.0, 1.0),
      "#228b22" → Color.RGBA(0x22 / 255.0, 0x8b / 255.0, 0x22 / 255.0, 1.0)
    )
    forAll(colors)((string, color) ⇒ sameRgba(color, Color.fromRgbaString(string)))
  }

  it should "convert RGBA to HSLA" in {
    val colors = Table(
      "rgba" → "hsla",
      "#104E8b" → Color.HSLA(210 / 360.0, 0.79, 0.3, 1.0),
      "#228b22" → Color.HSLA(120 / 360.0, 0.61, 0.34, 1.0)
    )
    forAll(colors)((string, color) ⇒ sameHsla(color, Color.fromRgbaString(string).toHsla))
  }

  it should "convert from RGBA to string and back" in {
    forAll(genRgba)(color ⇒ sameRgba(color, Color.fromRgbaString(color.toRgbString, color.a)))
  }

  it should "convert from RGBA to HSLA and back" in {
    forAll(genRgba)(color ⇒ sameRgba(color, color.toHsla.toRgba))
  }

  it should "convert from HSLA to RGBA and back" in {
    forAll(genHsla)(color ⇒ sameHsla(color, color.toRgba.toHsla))
  }
} 
Example 85
Source File: AnimationSpec.scala    From reftree   with GNU General Public License v3.0 5 votes vote down vote up
package reftree.diagram

import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}

class AnimationSpec extends FlatSpec with PropertyChecks with Matchers {
  it should "correctly iterate to fixpoint" in {
    forAll(Gen.posNum[Int]) { seed ⇒
      val builder = Animation.startWith(seed).iterate(_ + 1).iterateToFixpoint(_ / 2)
      val expectedSize = (math.log(seed + 1) / math.log(2)) + 3

      builder.frames should have size expectedSize.toInt
    }
  }

  it should "correctly iterate to fixpoint or max, when specified" in {
    forAll(Gen.posNum[Int]) { seed ⇒
      val max = 5
      val builder = Animation.startWith(seed).iterate(_ + 1).iterateToFixpointAtMost(max)(_ / 2)
      val expectedSize = math.min(max + 2, (math.log(seed + 1) / math.log(2)) + 3)

      builder.frames should have size expectedSize.toInt
    }
  }

  it should "correctly iterate while some predicate is true" in {
    val builder = Animation.startWith(-1).iterate(_ + 1).iterateWhile(_ < 100)(_ + 13)
    builder.frames shouldEqual Vector(-1, 0, 13, 26, 39, 52, 65, 78, 91)
  }

  it should "correctly iterate while some predicate is true or max is reached" in {
    val max = 5

    val builder1 = Animation.startWith(-1).iterate(_ + 1).iterateWhileAtMost(max)(_ < 100)(_ + 13)
    builder1.frames shouldEqual Vector(-1, 0, 13, 26, 39, 52, 65)

    val builder2 = Animation.startWith(-1).iterate(_ + 1).iterateWhileAtMost(max)(_ < 30)(_ + 13)
    builder2.frames shouldEqual Vector(-1, 0, 13, 26)
  }
} 
Example 86
Source File: PropertyDescriptorTest.scala    From ncdbg   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.programmaticallyspeaking.ncd.chrome.domains

import com.programmaticallyspeaking.ncd.chrome.domains.Runtime.{PropertyDescriptor, RemoteObject}
import com.programmaticallyspeaking.ncd.host.types.{ObjectPropertyDescriptor, PropertyDescriptorType}
import com.programmaticallyspeaking.ncd.host.{FunctionNode, ObjectId, ObjectNode}
import com.programmaticallyspeaking.ncd.testing.UnitTest
import org.scalatest.prop.PropertyChecks

class PropertyDescriptorTest extends UnitTest with PropertyChecks {

  private val anObject = ObjectNode("Object", ObjectId("x"))
  private val aFunction = FunctionNode("fun", "function(){}", ObjectId("x"))
  private val anotherFunction = FunctionNode("fun2", "function(){}", ObjectId("y"))

  implicit val remoteObjectConverter = RemoteObjectConverter.byReference

  "PropertyDescriptor from ObjectPropertyDescriptor" - {
    "should handle a generic descriptor" in {
      forAll { (configurable: Boolean, writable: Boolean, enumerable: Boolean, own: Boolean) =>
        val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Generic, configurable, enumerable, writable,
          own, None, None, None)
        val expected = PropertyDescriptor("test", writable, configurable, enumerable, own, None, None, None)
        PropertyDescriptor.from("test", desc) should be(expected)
      }
    }

    "should handle a data descriptor" in {
      forAll { (configurable: Boolean, writable: Boolean, enumerable: Boolean, own: Boolean) =>
        val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Data, configurable, enumerable, writable,
          own, Some(anObject), None, None)

        val remoteObj = RemoteObject.forObject("Object", anObject.objectId.toString)

        val expected = PropertyDescriptor("data", writable, configurable, enumerable, own, Some(remoteObj), None, None)
        PropertyDescriptor.from("data", desc) should be(expected)
      }
    }

    "should handle an accessor descriptor" in {
      forAll { (configurable: Boolean, writable: Boolean, enumerable: Boolean, own: Boolean) =>
        val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Accessor, configurable, enumerable, writable,
          own, None, Some(aFunction), Some(anotherFunction))

        val remoteFun = RemoteObject.forFunction(aFunction.name, aFunction.source, aFunction.objectId.toString)
        val remoteFun2 = RemoteObject.forFunction(anotherFunction.name, anotherFunction.source, anotherFunction.objectId.toString)

        val expected = PropertyDescriptor("acc", writable, configurable, enumerable, own, None, Some(remoteFun), Some(remoteFun2))
        PropertyDescriptor.from("acc", desc) should be(expected)
      }
    }

    "should set the name properly for a generic descriptor" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Generic, false, false, false, false, None, None, None)
      val expected = PropertyDescriptor("name", false, false, false, false, None, None, None)
      PropertyDescriptor.from("name", desc).name should be ("name")
    }

    "should set the name properly for a data descriptor" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Data, false, false, false, false, Some(anObject), None, None)
      PropertyDescriptor.from("name", desc).name should be ("name")
    }

    "should set the name properly for an accessor descriptor" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Accessor, false, false, false, false, None, Some(aFunction), None)
      PropertyDescriptor.from("name", desc).name should be ("name")
    }

    "should reject null name" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Generic, false, false, false, false, None, None, None)
      assertThrows[IllegalArgumentException](PropertyDescriptor.from(null, desc))
    }

    "should reject empty name" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Generic, false, false, false, false, None, None, None)
      assertThrows[IllegalArgumentException](PropertyDescriptor.from("", desc))
    }
  }
} 
Example 87
Source File: InternalPropertyDescriptorTest.scala    From ncdbg   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.programmaticallyspeaking.ncd.chrome.domains

import com.programmaticallyspeaking.ncd.chrome.domains.Runtime.{InternalPropertyDescriptor, PropertyDescriptor, RemoteObject}
import com.programmaticallyspeaking.ncd.host.types.{ObjectPropertyDescriptor, PropertyDescriptorType}
import com.programmaticallyspeaking.ncd.host.{FunctionNode, ObjectId, ObjectNode}
import com.programmaticallyspeaking.ncd.testing.UnitTest
import org.scalatest.prop.PropertyChecks

class InternalPropertyDescriptorTest extends UnitTest with PropertyChecks {

  private val anObject = ObjectNode("Object", ObjectId("x"))
  private val aFunction = FunctionNode("fun", "function(){}", ObjectId("x"))
  private val anotherFunction = FunctionNode("fun2", "function(){}", ObjectId("y"))

  implicit val remoteObjectConverter = RemoteObjectConverter.byReference

  "InternalPropertyDescriptor from ObjectPropertyDescriptor" - {
    "should handle a data descriptor" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Data, false, false, false, false, Some(anObject), None, None)

      val remoteObj = RemoteObject.forObject("Object", anObject.objectId.toString)

      val expected = InternalPropertyDescriptor("data", Some(remoteObj))
      InternalPropertyDescriptor.from("data", desc) should be(Some(expected))
    }

    "should ignore an accessor descriptor" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Accessor, false, false, false, false, None, Some(aFunction), None)

      InternalPropertyDescriptor.from("data", desc) should be('empty)
    }

    "should ignore a generic descriptor" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Generic, false, false, false, false, None, None, None)

      InternalPropertyDescriptor.from("data", desc) should be('empty)
    }

    "should reject null name" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Generic, false, false, false, false, None, None, None)
      assertThrows[IllegalArgumentException](PropertyDescriptor.from(null, desc))
    }

    "should reject empty name" in {
      val desc = ObjectPropertyDescriptor(PropertyDescriptorType.Generic, false, false, false, false, None, None, None)
      assertThrows[IllegalArgumentException](PropertyDescriptor.from("", desc))
    }
  }
} 
Example 88
Source File: UnitSpec.scala    From kafka-serialization   with Apache License 2.0 5 votes vote down vote up
package com.ovoenergy.kafka.serialization.testkit

import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.Arbitrary._
import org.scalatest.concurrent.{ScalaFutures, ScaledTimeSpans}
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, WordSpec}

object UnitSpec {

  case class Event(id: String, name: String)

  implicit val arbString: Arbitrary[String] = Arbitrary(for {
    length <- Gen.chooseNum(3, 64)
    chars <- Gen.listOfN(length, Gen.alphaNumChar)
  } yield chars.mkString)

  implicit val arbEvent: Arbitrary[Event] = Arbitrary(for {
    id <- arbitrary[String]
    name <- arbitrary[String]
  } yield Event(id, name))

}

abstract class UnitSpec extends WordSpec with Matchers with PropertyChecks with ScalaFutures with ScaledTimeSpans {

  override lazy val spanScaleFactor: Double =
    sys.env.get("TEST_TIME_FACTOR").map(_.toDouble).getOrElse(super.spanScaleFactor)
} 
Example 89
Source File: AvroRecordPropSpec.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
package io.amient.affinity.avro

import java.util.UUID

import io.amient.affinity.avro.record.AvroRecord
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen
import org.scalacheck.Gen._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class AvroRecordPropSpec extends PropSpec with PropertyChecks with Matchers {

  import SimpleEnum._

  property("Case Class constructor default arguments are AvroRecord defaults") {
    val b = SimpleRecord()
    assert(b == SimpleRecord(SimpleKey(0), A, Seq()))
    AvroRecord.read[SimpleRecord](AvroRecord.write(b, b.schema), b.schema) should equal(SimpleRecord(SimpleKey(0), A, Seq()))
    val c = Record_Current()
    assert(c == Record_Current(Seq(), Map(), Set()))
    AvroRecord.read[Record_Current](AvroRecord.write(c, c.schema), c.schema) should equal(Record_Current(Seq(), Map(), Set()))
  }

  def uuids: Gen[UUID] = for {
    hi <- arbitrary[Long]
    lo <- arbitrary[Long]
  } yield new UUID(hi, lo)

  property("java.lang.UUID can be represented as Avro Bytes") {
    forAll(uuids) { uuid: UUID =>
      val x = AvroUUID(uuid)
      val bytes = AvroRecord.write(x, x.schema)
      val copy = AvroRecord.read[AvroUUID](bytes, x.schema)
      copy.uuid should be(uuid)
    }
  }
  def bases: Gen[SimpleRecord] = for {
    id <- arbitrary[Int]
    side <- Gen.oneOf(SimpleEnum.A, SimpleEnum.B)
    ints <- listOf(arbitrary[Int])
  } yield SimpleRecord(SimpleKey(id), side, ints.map(SimpleKey(_)))

  def composites: Gen[Record_Current] = for {
     nitems <- Gen.choose(1, 2)
     items <- listOfN(nitems, bases)
     keys <- listOfN(nitems, Gen.alphaStr)
     longs <- listOf(arbitrary[Long])
  } yield Record_Current(items, keys.zip(items).toMap, longs.toSet)

  property("AvroRecord.write is fully reversible by AvroRecord.read") {
    forAll(composites) { composite: Record_Current =>
      val bytes = AvroRecord.write(composite, composite.schema)
      AvroRecord.read[Record_Current](bytes, composite.schema) should equal(composite )
    }
  }


} 
Example 90
Source File: BalancerSpec.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
package io.amient.affinity.core.cluster

import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class BalancerSpec extends PropSpec with PropertyChecks with Matchers {

  def partitions: Gen[Int] = Gen.choose(1, 1000)

  def nodes: Gen[Int] = Gen.choose(1, 100)

  def replFactors: Gen[Int] = Gen.choose(1, 100)

  property("all partitions should be represented and replicated exactly when num.nodes >= repl.factor") {
    forAll(partitions, replFactors, nodes) { (numPartitions: Int, replFactor: Int, numNodes: Int) =>
      val assignment: IndexedSeq[Vector[Int]] = Balancer.generateAssignment(numPartitions, replFactor, numNodes)
      assignment.size should be (numNodes)
      assignment.map(_.toSet).reduce(_ ++ _) should equal ((0 until numPartitions).toSet)
      whenever (numNodes >= replFactor) {
        val replicas = assignment.flatMap(_.map(pi => pi -> 1)).groupBy(_._1).mapValues(_.map(_._2).sum)
        replicas.foreach { case (_, r) => r should be (replFactor) }
      }
    }
  }

  property("generate balanced and deterministic assignment for uneven partitioning scheme") {
    Balancer.generateAssignment(7, 6, 9) should be(
      Vector(
        Vector(0,1,3,5),
        Vector(0,1,2,4,6),
        Vector(0,1,2,3,5),
        Vector(1,2,3,4,6),
        Vector(0,2,3,4,5),
        Vector(1,3,4,5,6),
        Vector(0,2,4,5,6),
        Vector(1,3,5,6),
        Vector(0,2,4,6)
      )
    )
  }

  property("generate balanced and deterministic assignment for even partitioning scheme") {
    Balancer.generateAssignment(6, 3, 6) should be(
      Vector(
        Vector(0,2,4),
        Vector(1,3,5),
        Vector(0,2,4),
        Vector(1,3,5),
        Vector(0,2,4),
        Vector(1,3,5)
      )
    )
  }
} 
Example 91
Source File: TimeCryptoProofSpec.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
package io.amient.affinity.core.util

import java.net.URL

import org.scalacheck.Gen
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class TimeCryptoProofSpec extends PropSpec with PropertyChecks with Matchers {

  val salts: Gen[Array[Byte]] = for {
    _ <- Gen.choose(Integer.MIN_VALUE + 1, Integer.MAX_VALUE)
  } yield TimeCryptoProof.generateSalt

  val hexSalts: Gen[String] = for {
    salt <- salts
  } yield TimeCryptoProof.toHex(salt)


  property("slat bytes to hex conversion is reversible") {
    forAll(salts) { salt =>
      val hexSalt = TimeCryptoProof.toHex(salt)
      val reversedSalt = TimeCryptoProof.fromHex(hexSalt)
      reversedSalt should equal(salt)
    }
  }

  property("CryptoProofSHA256 holds for all salts and args") {
    forAll(salts, Gen.alphaStr) { (salt, arg) =>

      val crypto = new TimeCryptoProofSHA256(salt)
      val signature: String = crypto.sign(arg)

      TimeCryptoProof.toHex(salt)
      val hexProof = new TimeCryptoProofSHA256(salt)
      val hexSignature = hexProof.sign(arg)
      signature should equal(hexSignature)
      try {
        assert(crypto.verify(signature, arg) == true)
        assert(hexProof.verify(signature, arg) == true)
        assert(crypto.verify(hexSignature, arg) == true)
        assert(hexProof.verify(hexSignature, arg) == true)
      } catch {
        case e: Throwable => e.printStackTrace(); throw e
      }
    }
  }

  property("example function is consistent with the implementation") {
    forAll(hexSalts, Gen.alphaStr) { (hexSalt, arg) =>
      val crypto = new TimeCryptoProofSHA256(hexSalt)
      try {
        assert(crypto.sign(arg) == crypto.timeBasedHash(arg, hexSalt, 0) == true)
        assert(crypto.verify(crypto.sign(arg), arg) == true)
        assert(crypto.verify(crypto.timeBasedHash(arg, hexSalt, 0), arg) == true)
      } catch {
        case e: Throwable => e.printStackTrace(); throw e
      }
    }
  }

  property("example function generates different signatures for different salts") {
    forAll(hexSalts, hexSalts) { case (salt1, salt2) =>
      whenever(salt1 != salt2) {
        val crypto1 = new TimeCryptoProofSHA256(salt1)
        val crypto2 = new TimeCryptoProofSHA256(salt2)
        val url = new URL("https://example.com/xyz?param=123456")
        val urlNoQuery = new URL("https://example.com/xyz")
        val sig1 = crypto1.sign(url.getPath)
        val sig1NoQuery = crypto1.sign(urlNoQuery.getPath)
        val sig2 = crypto2.sign(url.toString)
        val sig2NoQuery = crypto2.sign(urlNoQuery.toString)
        assert(sig1 != sig2)
        assert(sig1NoQuery != sig2NoQuery)
      }
    }
  }

} 
Example 92
Source File: MLeapModelConverterTest.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.local

import com.salesforce.op.test.TestCommon
import ml.combust.mleap.core.feature._
import ml.combust.mleap.core.types.ScalarShape
import org.apache.spark.ml.linalg.{DenseMatrix, Vectors}
import org.junit.runner.RunWith
import org.scalatest.PropSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.PropertyChecks

@RunWith(classOf[JUnitRunner])
class MLeapModelConverterTest extends PropSpec with PropertyChecks with TestCommon {

  val mleapModels = Table("mleapModels",
    BinarizerModel(0.0, ScalarShape()),
    BucketedRandomProjectionLSHModel(Seq(), 0.0, 0),
    BucketizerModel(Array.empty),
    ChiSqSelectorModel(Seq(), 0),
    CoalesceModel(Seq()),
    CountVectorizerModel(Array.empty, false, 0.0),
    DCTModel(false, 0),
    ElementwiseProductModel(Vectors.zeros(0)),
    FeatureHasherModel(0, Seq(), Seq(), Seq()),
    HashingTermFrequencyModel(),
    IDFModel(Vectors.zeros(0)),
    ImputerModel(0.0, 0.0, ""),
    InteractionModel(Array(), Seq()),
    MathBinaryModel(BinaryOperation.Add),
    MathUnaryModel(UnaryOperation.Log),
    MaxAbsScalerModel(Vectors.zeros(0)),
    MinHashLSHModel(Seq(), 0),
    MinMaxScalerModel(Vectors.zeros(0), Vectors.zeros(0)),
    NGramModel(0),
    NormalizerModel(0.0, 0),
    OneHotEncoderModel(Array()),
    PcaModel(DenseMatrix.zeros(0, 0)),
    PolynomialExpansionModel(0, 0),
    RegexIndexerModel(Seq(), None),
    RegexTokenizerModel(".*".r),
    ReverseStringIndexerModel(Seq()),
    StandardScalerModel(Some(Vectors.dense(Array(1.0))), Some(Vectors.dense(Array(1.0)))),
    StopWordsRemoverModel(Seq(), false),
    StringIndexerModel(Seq()),
    StringMapModel(Map()),
    TokenizerModel(),
    VectorAssemblerModel(Seq()),
    VectorIndexerModel(0, Map()),
    VectorSlicerModel(Array(), Array(), 0),
    WordLengthFilterModel(),
    WordToVectorModel(Map("a" -> 1), Array(1))
  )

  property("convert mleap models to functions") {
    forAll(mleapModels) { m =>
      val fn = MLeapModelConverter.modelToFunction(m)
      fn shouldBe a[Function[_, _]]
    }
  }

  property("error on unsupported models") {
    the[RuntimeException] thrownBy MLeapModelConverter.modelToFunction(model = "not at model") should have message
      "Unsupported MLeap model: java.lang.String"
  }

} 
Example 93
Source File: Base64Test.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.features.types

import java.nio.charset.Charset

import com.salesforce.op.test.TestCommon
import org.apache.commons.io.IOUtils
import org.junit.runner.RunWith
import org.scalatest.PropSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.PropertyChecks

@RunWith(classOf[JUnitRunner])
class Base64Test extends PropSpec with PropertyChecks with TestCommon {

  property("handle empty") {
    forAll(None) {
      (v: Option[String]) =>
        Base64(v).asBytes shouldBe None
        Base64(v).asString shouldBe None
        Base64(v).asInputStream shouldBe None
    }
  }

  property("can show byte contents") {
    forAll {
      (b: Array[Byte]) =>
        val b64 = toBase64(b)
        (Base64(b64).asBytes map (_.toList)) shouldBe Some(b.toList)
    }
  }

  property("can show string contents") {
    forAll {
      (s: String) =>
        val b64 = toBase64(s.getBytes)
        Base64(b64).asString shouldBe Some(s)
    }
  }

  property("produce a stream") {
    forAll {
      (s: String) =>
        val b64 = toBase64(s.getBytes)
        Base64(b64).asInputStream.map(IOUtils.toString(_, Charset.defaultCharset())) shouldBe Some(s)
    }
  }

  property("produce a stream and map over it") {
    forAll {
      (s: String) =>
        val b64 = toBase64(s.getBytes)
        Base64(b64).mapInputStream(IOUtils.toString(_, Charset.defaultCharset())) shouldBe Some(s)
    }
  }

  def toBase64(b: Array[Byte]): String = new String(java.util.Base64.getEncoder.encode(b))
} 
Example 94
Source File: ConcurrentCheck.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.features.types

import org.scalatest.Matchers
import org.scalatest.prop.{PropertyChecks, TableFor1}

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


trait ConcurrentCheck {
  self: PropertyChecks with Matchers =>

  def forAllConcurrentCheck[T](
    numThreads: Int = 10,
    numInvocationsPerThread: Int = 10000,
    atMost: Duration = 10.seconds,
    warmUp: Boolean = true,
    table: TableFor1[T],
    functionCheck: T => Unit
  ): Unit = {
    def doTest(t: TableFor1[T]): Unit = {
      forAll(t) { ft =>
        def testOne(): Unit = {
          var i = 0
          while (i < numInvocationsPerThread) {
            functionCheck(ft)
            i += 1
          }
        }
        val all = Future.sequence((0 until numThreads).map(_ => Future(testOne())))
        val res = Await.result(all, atMost)
        res.length shouldBe numThreads
      }
    }

    def measure(f: => Unit): Long = {
      val started = System.currentTimeMillis()
      val _ = f
      System.currentTimeMillis() - started
    }

    val warmUpElapsed = if (warmUp) Some(measure(doTest(table.take(1)))) else None
    val elapsed = measure(doTest(table))
    println(
      s"Tested with $numThreads concurrent threads. " +
        warmUpElapsed.map(v => s"Warm up: ${v}ms. ").getOrElse("") +
        s"Actual: ${elapsed}ms. " +
        s"Executed ${numInvocationsPerThread * numThreads} function invocations with average " +
        elapsed / (numInvocationsPerThread * numThreads).toDouble +
        "ms per function invocation."
    )
    elapsed should be <= atMost.toMillis
  }

} 
Example 95
Source File: URLTest.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.features.types

import com.salesforce.op.test.TestCommon
import org.junit.runner.RunWith
import org.scalatest.PropSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.PropertyChecks

@RunWith(classOf[JUnitRunner])
class URLTest extends PropSpec with PropertyChecks with TestCommon {

  val badOnes = Table("bad ones",
    None,
    Some(""),
    Some("protocol://domain.codomain"),
    Some("httpd://domain.codomain"),
    Some("http://domain."),
    Some("ftp://.codomain"),
    Some("https://.codomain"),
    Some("//domain.nambia"),
    Some("http://\u00ff\u0080\u007f\u0000.com") // scalastyle:off
  )

  val goodOnes = Table("good ones",
    "https://nothinghere.com?Eli=%E6%B8%87%40",
    "http://nothingthere.com?Chr=%E5%95%A9%E7%B1%85&Raj=%E7%B5%89%EC%AE%A1&Hir=%E5%B3%8F%E0%B4%A3",
    "ftp://my.red.book.com/amorcito.mio",
    "http://secret.gov?Cla=%E9%99%B9%E4%8A%93&Cha=%E3%95%98%EA%A3%A7&Eve=%EC%91%90%E8%87%B1",
    "ftp://nukes.mil?Lea=%E2%BC%84%EB%91%A3&Mur=%E2%83%BD%E1%92%83"
  )

  property("validate urls") {
    forAll(badOnes) {
      sample => URL(sample).isValid shouldBe false
    }
    forAll(goodOnes) {
      sample => URL(sample).isValid shouldBe true
    }
    forAll(goodOnes) {
      sample => URL(sample).isValid(protocols = Array("http")) shouldBe sample.startsWith("http:")
    }
  }

  property("extract domain") {
    val samples = Table("samples",
      "https://nothinghere.com?Eli=%E6%B8%87%40" -> "nothinghere.com",
      "http://nothingthere.com?Chr=%E5%85&Raj=%E7%B5%AE%A1&Hir=%8F%E0%B4%A3" -> "nothingthere.com",
      "ftp://my.red.book.com/amorcito.mio" -> "my.red.book.com",
      "http://secret.gov?Cla=%E9%99%B9%E4%8A%93&Cha=%E3&Eve=%EC%91%90%E8%87%B1" -> "secret.gov",
      "ftp://nukes.mil?Lea=%E2%BC%84%EB%91%A3&Mur=%E2%83%BD%E1%92%83" -> "nukes.mil"
    )

    URL(None).domain shouldBe None

    forAll(samples) {
      case (sample, expected) =>
        val url = URL(sample)
        val domain = url.domain
        domain shouldBe Some(expected)
    }
  }

  property("extract protocol") {
    val samples = Table("samples",
      "https://nothinghere.com?Eli=%E6%B8%87%40" -> "https",
      "http://nothingthere.com?Chr=%E5%85&Raj=%E7%B5%AE%A1&Hir=%8F%E0%B4%A3" -> "http",
      "ftp://my.red.book.com/amorcito.mio" -> "ftp"
    )

    URL(None).protocol shouldBe None

    forAll(samples) {
      case (sample, expected) =>
        val url = URL(sample)
        val domain = url.protocol
        domain shouldBe Some(expected)
    }
  }
} 
Example 96
Source File: RichRDDTest.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.utils.spark

import java.io.File

import com.holdenkarau.spark.testing.RDDGenerator
import com.salesforce.op.test.TestSparkContext
import org.apache.hadoop.io.compress.DefaultCodec
import org.apache.hadoop.mapred.JobConf
import org.joda.time.DateTime
import org.junit.runner.RunWith
import org.scalacheck.Arbitrary
import org.scalatest.PropSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.PropertyChecks


@RunWith(classOf[JUnitRunner])
class RichRDDTest extends PropSpec with PropertyChecks with TestSparkContext {
  import com.salesforce.op.utils.spark.RichRDD._

  val data = RDDGenerator.genRDD[(Int, Int)](sc)(Arbitrary.arbitrary[(Int, Int)])

  property("save as a text file") {
    forAll(data) { rdd =>
      val out = new File(tempDir, "op-richrdd-" + DateTime.now().getMillis).toString
      rdd.saveAsTextFile(out, None, new JobConf(rdd.context.hadoopConfiguration))
      spark.read.textFile(out).count() shouldBe rdd.count()
    }
  }
  property("save as a compressed text file") {
    forAll(data) { rdd =>
      val out = new File(tempDir, "op-richrdd-" + DateTime.now().getMillis).toString
      rdd.saveAsTextFile(out, Some(classOf[DefaultCodec]), new JobConf(rdd.context.hadoopConfiguration))
      spark.read.textFile(out).count() shouldBe rdd.count()
    }
  }

} 
Example 97
Source File: OpStatisticsPropertyTest.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.utils.stats

import com.salesforce.op.test.TestCommon
import org.apache.spark.mllib.linalg.DenseMatrix
import org.junit.runner.RunWith
import org.scalacheck.Gen
import org.scalatest.PropSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.PropertyChecks

@RunWith(classOf[JUnitRunner])
class OpStatisticsPropertyTest extends PropSpec with TestCommon with PropertyChecks {

  val genInt = Gen.posNum[Int]
  private def genArray(n: Int) = Gen.containerOfN[Array, Int](n, genInt)

  val genMatrix = for {
    rowSize <- Gen.choose(1, 13)
    colSize <- Gen.choose(1, 13)
    size = rowSize * colSize
    array <- genArray(size)
  } yield {
    new DenseMatrix(rowSize, colSize, array.map(_.toDouble))
  }

  property("cramerV function should produce results in expected ranges") {
    forAll(genMatrix) { (matrix: DenseMatrix) =>
      val res = OpStatistics.chiSquaredTest(matrix).cramersV
      if (matrix.numRows > 1 && matrix.numCols > 1) {
        res >= 0 shouldBe true
        res <= 1 shouldBe true
      } else {
        res.isNaN shouldBe true
      }
    }
  }
} 
Example 98
Source File: NumberTest.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.utils.numeric

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

@RunWith(classOf[JUnitRunner])
class NumberTest extends PropSpec with PropertyChecks with Matchers {

  val specials = Table("specials",
    Double.MinValue, Double.MaxValue,
    Double.MinPositiveValue, Double.NaN,
    Double.PositiveInfinity, Double.NegativeInfinity
  )

  property("validate numbers") {
    forAll { d: Double =>
      Number.isValid(d) should not be (d.isInfinity || d.isNaN)
    }
  }

  property("validate special numbers") {
    forAll(specials) { d =>
      Number.isValid(d) should not be (d.isInfinity || d.isNaN)
    }
  }
} 
Example 99
Source File: FileGeneratorTest.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.cli.gen

import com.salesforce.op.test.TestCommon
import org.junit.runner.RunWith
import org.scalatest.PropSpec
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.PropertyChecks


@RunWith(classOf[JUnitRunner])
class FileGeneratorTest extends PropSpec with TestCommon with PropertyChecks {

  private val sourceLeft = "
    """.stripMargin

  property("skips whitespace") {
    FileGenerator.skipWhitespace(sourceLeft, sourceLeft.lastIndexOf('/') + 1, +1) should equal(sourceLeft.indexOf('w'))
    FileGenerator.skipWhitespace(sourceRight, sourceRight.indexOf('/') - 1, -1) should equal(sourceRight.indexOf('d'))
  }

  property("skips expressions") {
    FileGenerator.skipExpr(bigSourceLeft, bigSourceLeft.indexOf('{'), +1) should equal(
      bigSourceLeft.lastIndexOf('}') + 1
    )
    FileGenerator.skipExpr(bigSourceRight, bigSourceRight.lastIndexOf('}'), -1) should equal(
      bigSourceRight.indexOf('{') - 1
    )
  }

  property("finds the right amount of substitution text in template") {
    FileGenerator.getSubstitutionEnd(bigSourceLeft, bigSourceLeft.lastIndexOf('/') + 1, +1) should equal(
      bigSourceLeft.lastIndexOf('}') + 1
    )
    FileGenerator.getSubstitutionEnd(bigSourceRight, bigSourceRight.indexOf('/') - 1, -1) should equal(
      bigSourceRight.indexOf('{') - 1
    )
  }

} 
Example 100
Source File: FeatureTestBase.scala    From TransmogrifAI   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package com.salesforce.op.test

import com.salesforce.op.features._
import com.salesforce.op.features.types._
import com.salesforce.op.utils.spark.RichDataset.RichDataset
import org.apache.spark.ml.{Estimator, Transformer}
import org.apache.spark.sql.Dataset
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Assertion, Suite}

import scala.reflect.ClassTag
import scala.reflect.runtime.universe.TypeTag


  def testOp[A <: FeatureType : TypeTag,
  B <: FeatureType : TypeTag,
  C <: FeatureType : TypeTag : FeatureTypeSparkConverter : ClassTag]
  (
    op: FeatureLike[A] => FeatureLike[B] => FeatureLike[C]
  ): BinaryTester[A, B, C] = new BinaryTester[A, B, C] {
    def of(v: (A, B)*): Checker[C] = new Checker[C] {
      def expecting(z: C*): Assertion = {
        val (data, f1, f2) = TestFeatureBuilder[A, B](v)
        val f = op(f1)(f2)
        checkFeature(f, data, expected = z, clue = s"Testing ${f.originStage.operationName} on $v: ")
      }
    }
  }

  sealed abstract class UnaryTester[A <: FeatureType,
  C <: FeatureType : TypeTag : FeatureTypeSparkConverter : ClassTag] {
    def of(x: A*): Checker[C]
  }

  sealed abstract class BinaryTester[A <: FeatureType,
  B <: FeatureType, C <: FeatureType : TypeTag : FeatureTypeSparkConverter : ClassTag] {
    def of(x: A, y: B): Checker[C] = of((x, y))
    def of(x: (A, B)*): Checker[C]
  }

  sealed abstract class Checker[C <: FeatureType : TypeTag : FeatureTypeSparkConverter : ClassTag] {
    def expecting(z: C*): Assertion

    protected def checkFeature(f: FeatureLike[C], data: Dataset[_], clue: String, expected: Seq[C]): Assertion = {
      val transformed = f.originStage match {
        case e: Estimator[_] => e.fit(data).transform(data)
        case t: Transformer => t.transform(data)
      }
      withClue(clue)(
        new RichDataset(transformed).collect[C](f) should contain theSameElementsInOrderAs expected
      )
    }
  }

} 
Example 101
Source File: PrefixComparatorsSuite.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.util.collection.unsafe.sort

import com.google.common.primitives.UnsignedBytes
import org.scalatest.prop.PropertyChecks
import org.apache.spark.SparkFunSuite
import org.apache.spark.unsafe.types.UTF8String

class PrefixComparatorsSuite extends SparkFunSuite with PropertyChecks {

  test("String prefix comparator") {//字符串的前缀比较器

    def testPrefixComparison(s1: String, s2: String): Unit = {
      val utf8string1 = UTF8String.fromString(s1)
      val utf8string2 = UTF8String.fromString(s2)
      val s1Prefix = PrefixComparators.StringPrefixComparator.computePrefix(utf8string1)
      val s2Prefix = PrefixComparators.StringPrefixComparator.computePrefix(utf8string2)
      val prefixComparisonResult = PrefixComparators.STRING.compare(s1Prefix, s2Prefix)

      val cmp = UnsignedBytes.lexicographicalComparator().compare(
        utf8string1.getBytes.take(8), utf8string2.getBytes.take(8))

      assert(
        (prefixComparisonResult == 0 && cmp == 0) ||
        (prefixComparisonResult < 0 && s1.compareTo(s2) < 0) ||
        (prefixComparisonResult > 0 && s1.compareTo(s2) > 0))
    }

    // scalastyle:off
    val regressionTests = Table(
      ("s1", "s2"),
      ("abc", "世界"),
      ("你好", "世界"),
      ("你好123", "你好122")
    )
    // scalastyle:on

    forAll (regressionTests) { (s1: String, s2: String) => testPrefixComparison(s1, s2) }
    forAll { (s1: String, s2: String) => testPrefixComparison(s1, s2) }
  }

  test("Binary prefix comparator") {//二进制前缀比较器

     def compareBinary(x: Array[Byte], y: Array[Byte]): Int = {
      for (i <- 0 until x.length; if i < y.length) {
        val res = x(i).compare(y(i))
        if (res != 0) return res
      }
      x.length - y.length
    }

    def testPrefixComparison(x: Array[Byte], y: Array[Byte]): Unit = {
      val s1Prefix = PrefixComparators.BinaryPrefixComparator.computePrefix(x)
      val s2Prefix = PrefixComparators.BinaryPrefixComparator.computePrefix(y)
      val prefixComparisonResult =
        PrefixComparators.BINARY.compare(s1Prefix, s2Prefix)
      assert(
        (prefixComparisonResult == 0) ||
        (prefixComparisonResult < 0 && compareBinary(x, y) < 0) ||
        (prefixComparisonResult > 0 && compareBinary(x, y) > 0))
    }

    // scalastyle:off
    val regressionTests = Table(
      ("s1", "s2"),
      ("abc", "世界"),
      ("你好", "世界"),
      ("你好123", "你好122")
    )
    // scalastyle:on

    forAll (regressionTests) { (s1: String, s2: String) =>
      testPrefixComparison(s1.getBytes("UTF-8"), s2.getBytes("UTF-8"))
    }
    forAll { (s1: String, s2: String) =>
      testPrefixComparison(s1.getBytes("UTF-8"), s2.getBytes("UTF-8"))
    }
  }
  //双前缀比较器正确处理NaN
  test("double prefix comparator handles NaNs properly") {
    val nan1: Double = java.lang.Double.longBitsToDouble(0x7ff0000000000001L)
    val nan2: Double = java.lang.Double.longBitsToDouble(0x7fffffffffffffffL)
    assert(nan1.isNaN)
    assert(nan2.isNaN)
    val nan1Prefix = PrefixComparators.DoublePrefixComparator.computePrefix(nan1)
    val nan2Prefix = PrefixComparators.DoublePrefixComparator.computePrefix(nan2)
    assert(nan1Prefix === nan2Prefix)
    val doubleMaxPrefix = PrefixComparators.DoublePrefixComparator.computePrefix(Double.MaxValue)
    assert(PrefixComparators.DOUBLE.compare(nan1Prefix, doubleMaxPrefix) === 1)
  }

} 
Example 102
Source File: PrefixComparatorsSuite.scala    From BigDatalog   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.util.collection.unsafe.sort

import com.google.common.primitives.UnsignedBytes
import org.scalatest.prop.PropertyChecks
import org.apache.spark.SparkFunSuite
import org.apache.spark.unsafe.types.UTF8String

class PrefixComparatorsSuite extends SparkFunSuite with PropertyChecks {

  test("String prefix comparator") {

    def testPrefixComparison(s1: String, s2: String): Unit = {
      val utf8string1 = UTF8String.fromString(s1)
      val utf8string2 = UTF8String.fromString(s2)
      val s1Prefix = PrefixComparators.StringPrefixComparator.computePrefix(utf8string1)
      val s2Prefix = PrefixComparators.StringPrefixComparator.computePrefix(utf8string2)
      val prefixComparisonResult = PrefixComparators.STRING.compare(s1Prefix, s2Prefix)

      val cmp = UnsignedBytes.lexicographicalComparator().compare(
        utf8string1.getBytes.take(8), utf8string2.getBytes.take(8))

      assert(
        (prefixComparisonResult == 0 && cmp == 0) ||
        (prefixComparisonResult < 0 && s1.compareTo(s2) < 0) ||
        (prefixComparisonResult > 0 && s1.compareTo(s2) > 0))
    }

    // scalastyle:off
    val regressionTests = Table(
      ("s1", "s2"),
      ("abc", "世界"),
      ("你好", "世界"),
      ("你好123", "你好122")
    )
    // scalastyle:on

    forAll (regressionTests) { (s1: String, s2: String) => testPrefixComparison(s1, s2) }
    forAll { (s1: String, s2: String) => testPrefixComparison(s1, s2) }
  }

  test("Binary prefix comparator") {

     def compareBinary(x: Array[Byte], y: Array[Byte]): Int = {
      for (i <- 0 until x.length; if i < y.length) {
        val res = x(i).compare(y(i))
        if (res != 0) return res
      }
      x.length - y.length
    }

    def testPrefixComparison(x: Array[Byte], y: Array[Byte]): Unit = {
      val s1Prefix = PrefixComparators.BinaryPrefixComparator.computePrefix(x)
      val s2Prefix = PrefixComparators.BinaryPrefixComparator.computePrefix(y)
      val prefixComparisonResult =
        PrefixComparators.BINARY.compare(s1Prefix, s2Prefix)
      assert(
        (prefixComparisonResult == 0) ||
        (prefixComparisonResult < 0 && compareBinary(x, y) < 0) ||
        (prefixComparisonResult > 0 && compareBinary(x, y) > 0))
    }

    // scalastyle:off
    val regressionTests = Table(
      ("s1", "s2"),
      ("abc", "世界"),
      ("你好", "世界"),
      ("你好123", "你好122")
    )
    // scalastyle:on

    forAll (regressionTests) { (s1: String, s2: String) =>
      testPrefixComparison(s1.getBytes("UTF-8"), s2.getBytes("UTF-8"))
    }
    forAll { (s1: String, s2: String) =>
      testPrefixComparison(s1.getBytes("UTF-8"), s2.getBytes("UTF-8"))
    }
  }

  test("double prefix comparator handles NaNs properly") {
    val nan1: Double = java.lang.Double.longBitsToDouble(0x7ff0000000000001L)
    val nan2: Double = java.lang.Double.longBitsToDouble(0x7fffffffffffffffL)
    assert(nan1.isNaN)
    assert(nan2.isNaN)
    val nan1Prefix = PrefixComparators.DoublePrefixComparator.computePrefix(nan1)
    val nan2Prefix = PrefixComparators.DoublePrefixComparator.computePrefix(nan2)
    assert(nan1Prefix === nan2Prefix)
    val doubleMaxPrefix = PrefixComparators.DoublePrefixComparator.computePrefix(Double.MaxValue)
    assert(PrefixComparators.DOUBLE.compare(nan1Prefix, doubleMaxPrefix) === 1)
  }

} 
Example 103
Source File: TypesTests.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigma.types

import org.scalatest.prop.PropertyChecks
import org.scalatest.{PropSpec, Matchers}
import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.Arbitrary._
import special.collections.CollGens
import scalan.util.Extensions._

class TypesTests extends PropSpec with PropertyChecks with Matchers with CollGens { testSuite =>

  val genSBoolean: Gen[Boolean] = arbBool.arbitrary.map(CBoolean(_))
  implicit val arbSBoolean = Arbitrary(genSBoolean)
  
  val genSByte: Gen[Byte] = arbByte.arbitrary.map(CByte(_))
  implicit val arbSByte = Arbitrary(genSByte)
  
  val genSInt: Gen[Int] = arbInt.arbitrary.map(CInt(_))
  implicit val arbSInt = Arbitrary(genSInt)
  
  property("Boolean methods") {
    forAll { x: Boolean =>
      x.toByte shouldBe CByte(x.value.toByte)
    }
  }

  property("Byte methods") {
    forAll { x: Byte =>
      x.toInt.value shouldBe x.value.toInt
    }
  }

  property("Int methods") {
    forAll(valGen) { in: scala.Int =>
      val x: Int = CInt(in)
      x.toByte.value shouldBe x.value.toByte
    }
  }
} 
Example 104
Source File: SerializationSpecification.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.serialization

import org.ergoplatform.validation.ValidationSpecification
import org.scalacheck.Gen
import org.scalatest.prop.{PropertyChecks, TableDrivenPropertyChecks, GeneratorDrivenPropertyChecks}
import org.scalatest.{PropSpec, Assertion, Matchers}
import org.scalacheck.Arbitrary._
import sigmastate.Values._
import sigmastate.SType
import sigmastate.serialization.generators._

trait SerializationSpecification extends PropSpec
  with PropertyChecks
  with GeneratorDrivenPropertyChecks
  with TableDrivenPropertyChecks
  with Matchers
  with ObjectGenerators
  with ConcreteCollectionGenerators
  with OpcodesGen
  with TransformerGenerators
  with RelationGenerators
  with ValidationSpecification {

  protected def roundTripTest[V <: Value[_ <: SType]](v: V): Assertion = {
    val bytes = ValueSerializer.serialize(v)
    predefinedBytesTest(v, bytes)
    predefinedBytesTestNotFomZeroElement(bytes, v)
  }

  protected def predefinedBytesTest[V <: Value[_ <: SType]](v: V, bytes: Array[Byte]): Assertion = {
    ValueSerializer.serialize(v) shouldEqual bytes
    val r = SigmaSerializer.startReader(bytes)
    val positionLimitBefore = r.positionLimit
    val dv = ValueSerializer.deserialize(r)
    dv shouldEqual v
    r.positionLimit shouldBe positionLimitBefore
  }

  //check that pos and consumed are being implented correctly
  protected def predefinedBytesTestNotFomZeroElement[V <: Value[_ <: SType]](bytes: Array[Byte], v: V): Assertion = {
    val randomInt = Gen.chooseNum(1, 20).sample.get
    val randomBytes = Gen.listOfN(randomInt, arbByte.arbitrary).sample.get.toArray
    val parsedVal = ValueSerializer.deserialize(randomBytes ++ bytes, randomInt)
    parsedVal shouldEqual v
  }
} 
Example 105
Source File: HelpersTests.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.utils

import org.scalatest.prop.PropertyChecks
import org.scalatest.{PropSpec, Matchers}
import sigmastate.serialization.generators.ObjectGenerators
import Helpers._

class HelpersTests extends PropSpec with PropertyChecks with Matchers with ObjectGenerators {
  property("xorU") {
    forAll(arrayGen[Byte]) { arr =>

      val x = xor(arr, arr)
      val cloned = arr.clone()
      xorU(cloned, arr)
      cloned shouldBe x

      val arr1 = x
      val arr2 = cloned
      val arr3 = xor(arr1, arr2)
      val res1 = xor(cloned, arr1, arr2, arr3)
      val res2 = cloned
      xorU(res2, Seq(arr1, arr2, arr3))

      res1 shouldBe res2
      println(arr.length)
    }
  }
} 
Example 106
Source File: SparseArrayContainerSpecification.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.utils

import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}
import sigmastate.serialization.generators.ObjectGenerators

class SparseArrayContainerSpecification extends PropSpec
  with ObjectGenerators
  with PropertyChecks
  with Matchers {

  private val distinctCodeValuePairsGen: Gen[Seq[(Byte, Long)]] = for {
    bytes <- distinctListOfGen(Arbitrary.arbByte.arbitrary)
    longs <- Gen.listOfN(bytes.length, Arbitrary.arbLong.arbitrary)
  } yield bytes.zip(longs)

  property("distinct codes") {
    val codeValuePairs = Seq[(Byte, Long)]((1, 1), (1, 2))
    an[RuntimeException] should be thrownBy
      new SparseArrayContainer(codeValuePairs)
  }

  property("get") {
    forAll(distinctCodeValuePairsGen) { codeValuePairs =>
      val cont = new SparseArrayContainer(codeValuePairs)
      codeValuePairs.foreach { case (code, v) =>
          cont(code) shouldBe v
      }
      val mappedValues = codeValuePairs.toMap
      (Byte.MinValue to Byte.MaxValue).foreach { i =>
        if (mappedValues.get(i.toByte).isEmpty)
          cont(i.toByte) shouldBe 0L
      }
    }
  }
} 
Example 107
Source File: SigmaBuilderTest.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package sigmastate.lang

import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}
import sigmastate.Values._
import sigmastate._
import sigmastate.lang.exceptions.{ArithException, ConstraintFailed}
import sigmastate.serialization.OpCodes

class SigmaBuilderTest extends PropSpec with PropertyChecks with Matchers with LangTests {

  property("StdSigmaBuilder construct nodes") {
    import StdSigmaBuilder._
    mkEQ(TrueLeaf, FalseLeaf) shouldEqual EQ(TrueLeaf, FalseLeaf)
    mkNEQ(TrueLeaf, FalseLeaf) shouldEqual NEQ(TrueLeaf, FalseLeaf)

    mkGT(IntConstant(1), IntConstant(1)) shouldEqual GT(IntConstant(1), IntConstant(1))
    mkGE(IntConstant(1), IntConstant(1)) shouldEqual GE(IntConstant(1), IntConstant(1))
    mkLT(IntConstant(1), IntConstant(1)) shouldEqual LT(IntConstant(1), IntConstant(1))
    mkLE(IntConstant(1), IntConstant(1)) shouldEqual LE(IntConstant(1), IntConstant(1))

    mkPlus(IntConstant(1), IntConstant(1)) shouldEqual
      ArithOp(IntConstant(1), IntConstant(1), OpCodes.PlusCode)
    mkMinus(IntConstant(1), IntConstant(1)) shouldEqual
      ArithOp(IntConstant(1), IntConstant(1), OpCodes.MinusCode)
    mkMultiply(IntConstant(1), IntConstant(1)) shouldEqual
      ArithOp(IntConstant(1), IntConstant(1), OpCodes.MultiplyCode)
    mkDivide(IntConstant(1), IntConstant(1)) shouldEqual
      ArithOp(IntConstant(1), IntConstant(1), OpCodes.DivisionCode)
    mkModulo(IntConstant(1), IntConstant(1)) shouldEqual
      ArithOp(IntConstant(1), IntConstant(1), OpCodes.ModuloCode)
  }

  property("TransformingSigmaBuilder apply upcast") {
    import TransformingSigmaBuilder._
    mkEQ(LongConstant(1), IntConstant(1)) shouldEqual EQ(LongConstant(1), Upcast(IntConstant(1), SLong))
    mkNEQ(LongConstant(1), IntConstant(1)) shouldEqual NEQ(LongConstant(1), Upcast(IntConstant(1), SLong))

    mkGT(LongConstant(1), IntConstant(1)) shouldEqual GT(LongConstant(1), Upcast(IntConstant(1), SLong))
    mkGE(LongConstant(1), IntConstant(1)) shouldEqual GE(LongConstant(1), Upcast(IntConstant(1), SLong))
    mkLT(LongConstant(1), IntConstant(1)) shouldEqual LT(LongConstant(1), Upcast(IntConstant(1), SLong))
    mkLE(LongConstant(1), IntConstant(1)) shouldEqual LE(LongConstant(1), Upcast(IntConstant(1), SLong))

    mkPlus(LongConstant(1), IntConstant(1)) shouldEqual
      ArithOp(LongConstant(1), Upcast(IntConstant(1), SLong), OpCodes.PlusCode)
    mkMinus(LongConstant(1), IntConstant(1)) shouldEqual
      ArithOp(LongConstant(1), Upcast(IntConstant(1), SLong), OpCodes.MinusCode)
    mkMultiply(LongConstant(1), IntConstant(1)) shouldEqual
      ArithOp(LongConstant(1), Upcast(IntConstant(1), SLong), OpCodes.MultiplyCode)
    mkDivide(LongConstant(1), IntConstant(1)) shouldEqual
      ArithOp(LongConstant(1), Upcast(IntConstant(1), SLong), OpCodes.DivisionCode)
    mkModulo(LongConstant(1), IntConstant(1)) shouldEqual
      ArithOp(LongConstant(1), Upcast(IntConstant(1), SLong), OpCodes.ModuloCode)
  }

  property("CheckingSigmaBuilder failing constraint") {
    import CheckingSigmaBuilder._
    an[ConstraintFailed] should be thrownBy mkEQ(LongConstant(1), IntConstant(1))
    an[ConstraintFailed] should be thrownBy mkNEQ(LongConstant(1), IntConstant(1))

    an[ConstraintFailed] should be thrownBy mkGT(LongConstant(1), IntConstant(1))
    an[ConstraintFailed] should be thrownBy mkGE(LongConstant(1), IntConstant(1))
    an[ConstraintFailed] should be thrownBy mkLT(LongConstant(1), IntConstant(1))
    an[ConstraintFailed] should be thrownBy mkLE(LongConstant(1), IntConstant(1))

    an[ConstraintFailed] should be thrownBy mkPlus(LongConstant(1), IntConstant(1))
    an[ConstraintFailed] should be thrownBy mkMinus(LongConstant(1), IntConstant(1))
    an[ConstraintFailed] should be thrownBy mkMultiply(LongConstant(1), IntConstant(1))
    an[ConstraintFailed] should be thrownBy mkDivide(LongConstant(1), IntConstant(1))
    an[ConstraintFailed] should be thrownBy mkModulo(LongConstant(1), IntConstant(1))
  }
} 
Example 108
Source File: ExamplesTests.scala    From sigmastate-interpreter   with MIT License 5 votes vote down vote up
package special.collections

import org.scalatest.prop.PropertyChecks
import org.scalatest.{Matchers, PropSpec}

class ExamplesTests extends PropSpec with PropertyChecks with Matchers with CollGens with ExampleGens { testSuite =>
  import Examples._
  val examples = new Examples(builder)
  import examples._

  property("checkTokenBalance") {
    forAll(genContext) { ctx =>
      checkTokenBalance(ctx)
    }
  }

  property("tokenTotal") {
    forAll(genContext) { ctx =>
      val tokenId = ctx.inputs(0).tokens(0)._1
      val t1 = tokenTotal1(ctx, tokenId)
      val t2 = tokenTotal2(ctx, tokenId)
      val t3 = tokenTotal3(ctx, tokenId)
      t1 shouldBe t2
      t2 shouldBe t3
    }
  }
} 
Example 109
Source File: MediaTypeSpec.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.http

import com.mesosphere.Generators.Implicits._
import org.scalatest.FreeSpec
import org.scalatest.prop.PropertyChecks
import scala.util.Success

final class MediaTypeSpec extends FreeSpec with PropertyChecks {

  private val testMediaType = MediaType(
    "application",
    MediaTypeSubType("vnd.dcos.test", Some("json")),
    Map("charset" -> "utf-8", "version" -> "v1")
  )

  "MediaType.parse(string) should" -  {
    "parse basic type" in  {
      val Success(t) = MediaType.parse("text/html")
      assertResult("text/html")(t.show)
    }

    "parse with suffix" in  {
      val Success(t) = MediaType.parse("""image/svg+xml""")
      assertResult("image")(t.`type`)
      assertResult("svg")(t.subType.value)
      assertResult(Some("xml"))(t.subType.suffix)
      assertResult("""image/svg+xml""")(t.show)
    }

    "parse parameters" in  {
      val Success(t) = MediaType.parse("""text/html; charset=utf-8; foo=bar""")
      assertResult("text")(t.`type`)
      assertResult("html")(t.subType.value)
      assertResult(Map(
        "charset" -> "utf-8",
        "foo" -> "bar"
      ))(t.parameters)
    }

    "lower-case type" in  {
      val Success(t) = MediaType.parse("""IMAGE/SVG+XML""")
      assertResult("""image/svg+xml""")(t.show)
    }

    "lower-case parameter names" in  {
      val Success(t) = MediaType.parse("""text/html; Charset=utf-8""")
      assertResult("text")(t.`type`)
      assertResult("html")(t.subType.value)
      assertResult(Map(
        "charset" -> "utf-8"
      ))(t.parameters)
    }

    "parse a vendor type" in {
      val Success(t) = MediaType.parse("application/vnd.dcos.test+json; charset=utf-8; version=v1")
      assertResult(testMediaType)(t)
    }

    "unquote parameter values" in  {
      val Success(t) = MediaType.parse("""text/html; charset="utf-8"""")
      assertResult("text")(t.`type`)
      assertResult("html")(t.subType.value)
      assertResult(Map(
        "charset" -> "utf-8"
      ))(t.parameters)
    }
  }

  "A MediaType should show correctly" - {
    "text/html" in {
      assertResult("text/html")(MediaType("text", MediaTypeSubType("html")).show)
    }
    "application/xhtml+xml" in {
      assertResult("application/xhtml+xml")(MediaType("application", MediaTypeSubType("xhtml", Some("xml"))).show)
    }
    "application/vnd.dcos.custom-request+json" in {
      assertResult("application/vnd.dcos.custom-request+json")(
        MediaType("application", MediaTypeSubType("vnd.dcos.custom-request", Some("json"))).show
      )
    }
  }

  "MediaType.show followed by MediaType.parse should be the identity function" in {
    forAll { (mediaType: MediaType) =>
      assertResult(Success(mediaType))(MediaType.parse(mediaType.show))
    }
  }

  // TODO package-add: Test for case-insensitive comparison

} 
Example 110
Source File: ResourceProxyHandlerSpec.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.cosmos.handler

import com.mesosphere.Generators.Implicits._
import com.mesosphere.cosmos.error.CosmosException
import com.mesosphere.cosmos.error.GenericHttpError
import io.lemonlabs.uri.Uri
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen
import org.scalatest.FreeSpec
import org.scalatest.Matchers
import org.scalatest.prop.PropertyChecks

final class ResourceProxyHandlerSpec extends FreeSpec with PropertyChecks with Matchers {

  type TestData = (Long, Uri)

  "When Content-Length is provided by the upstream server" - {

    "When Content-Length matches the actual content stream length" - {
      // scalastyle:off magic.number
      val genTestData: Gen[TestData] = for {
        actualLength <- Gen.chooseNum(2, 10)
        uri <- arbitrary[Uri]
      } yield {
        (actualLength.toLong, uri)
      }

      "Succeeds if Content-Length is below the limit" in {
        forAll(genTestData) { case (actualLength, uri) =>
          ResourceProxyHandler.validateContentLength(uri, Some(actualLength))
        }
      }

      "Fails if Content-Length is zero" in {
        forAll (genTestData) { case (_, uri) =>
          val exception = intercept[CosmosException](ResourceProxyHandler.validateContentLength(uri, Some(0)))
          assert(exception.error.isInstanceOf[GenericHttpError])
        }
      }
    }

  }

  "Fails when Content-Length is not provided by the upstream server" in {
    val exception = intercept[CosmosException](ResourceProxyHandler.validateContentLength(Uri.parse("/random"), None))
    assert(exception.error.isInstanceOf[GenericHttpError])
  }

  "Parses the filename correctly" in {

    ResourceProxyHandler.getFileNameFromUrl(
      Uri.parse("http://doesntreallymatter.com/c.d")
    ) shouldEqual Some("c.d")

    ResourceProxyHandler.getFileNameFromUrl(
      Uri.parse("http://doesntreallymatter.com/a/b/c.d")
    ) shouldEqual Some("c.d")

    ResourceProxyHandler.getFileNameFromUrl(
      Uri.parse("http://doesntreallymatter.com/a/b/c")
    ) shouldEqual Some("c")

    ResourceProxyHandler.getFileNameFromUrl(
      Uri.parse("http://doesntreallymatter.com/a/b/c/")
    ) shouldEqual Some("c")

    // These should never happen, but just in case.

    ResourceProxyHandler.getFileNameFromUrl(
      Uri.parse("http://doesntreallymatter.com/")
    ) shouldEqual None

    ResourceProxyHandler.getFileNameFromUrl(
      Uri.parse("https://doesntreallymatter.com")
    ) shouldEqual None

  }
} 
Example 111
Source File: VersionSpecificationSpec.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.universe.v3.model

import com.mesosphere.Generators.Implicits.arbVersion
import org.scalatest.FreeSpec
import org.scalatest.prop.PropertyChecks

final class VersionSpecificationSpec extends FreeSpec with PropertyChecks {

  "The matches() method" - {

    "always returns true on AnyVersion" in {
      forAll { (version: Version) => assert(AnyVersion.matches(version)) }
    }

    "returns true on ExactVersion when the versions are equal" in {
      forAll { (version: Version) => assert(ExactVersion(version).matches(version)) }
    }

    "returns false on ExactVersion when the versions are unequal" in {
      forAll { (v1: Version, v2: Version) =>
        whenever (v1 != v2) {
          assert(!ExactVersion(v1).matches(v2))
        }
      }
    }

  }

} 
Example 112
Source File: PackageDefinitionSpec.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.universe.v3.model

import com.mesosphere.Generators.nonNegNum
import org.scalacheck.Gen
import org.scalatest.FreeSpec
import org.scalatest.prop.PropertyChecks

final class PackageDefinitionSpec extends FreeSpec with PropertyChecks {

  "PackageDefinition$.ReleaseVersion" - {

    "ReleaseVersion$.validate should" - {

      "succeed on non-negative numbers" in {
        forAll (nonNegNum[Long]) { n =>
          whenever (n >= 0) {
            assert(ReleaseVersion.validate(n).isReturn)
          }
        }
      }

      "fail on negative numbers" in {
        forAll (Gen.negNum[Long]) { n =>
          whenever (n < 0) {
            assert(ReleaseVersion.validate(n).isThrow)
          }
        }
      }

    }

    "ReleaseVersion.value" in {
      forAll (nonNegNum[Long]) { n =>
        assertResult(n)(ReleaseVersion(n).value)
      }
    }

    "ReleaseVersion$.ordering orders by value" in {
      forAll (nonNegNum[Long], nonNegNum[Long]) { (a, b) =>
        whenever (a >= 0 && b >= 0) {
          val aVersion = ReleaseVersion(a)
          val bVersion = ReleaseVersion(b)

          assertResult(Ordering[Long].compare(a, b)) {
            Ordering[ReleaseVersion].compare(aVersion, bVersion)
          }
        }
      }
    }

  }

} 
Example 113
Source File: SemVerSpec.scala    From cosmos   with Apache License 2.0 5 votes vote down vote up
package com.mesosphere.universe.v3.model

import com.mesosphere.Generators.Implicits._
import org.scalatest.FreeSpec
import org.scalatest.Matchers
import org.scalatest.prop.PropertyChecks
import scala.util.Random

final class SemVerSpec extends FreeSpec with PropertyChecks with Matchers {
  "For all SemVer => String => SemVer" in {

    forAll { (expected: SemVer) =>
        val string = expected.toString
        val actual = SemVer(string).get

        actual shouldBe expected
    }
  }

  "Test semver ordering" in {
    val expected = List(
      "1.0.0-alpha",
      "1.0.0-alpha.1",
      "1.0.0-alpha.beta",
      "1.0.0-beta",
      "1.0.0-beta.2",
      "1.0.0-beta.11",
      "1.0.0-rc.1",
      "1.0.0",
      "1.0.2",
      "1.2.0",
      "1.11.0",
      "1.11.11",
      "2",
      "11.11.11"
    ).map(SemVer(_).get)

    val actual = Random.shuffle(expected).sorted

    actual shouldBe expected
  }
} 
Example 114
Source File: ControllerSpec.scala    From akka-ddd-cqrs-es-example   with MIT License 5 votes vote down vote up
package com.github.j5ik2o.bank.adaptor.util

import akka.http.scaladsl.model.{ HttpEntity, MediaTypes }
import akka.http.scaladsl.testkit.ScalatestRouteTest
import akka.testkit.TestKitBase
import akka.util.ByteString
import com.github.j5ik2o.scalatestplus.db.{ MySQLdConfig, UserWithPassword }
import com.wix.mysql.distribution.Version.v5_6_21
import io.circe.Encoder
import io.circe.syntax._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.prop.PropertyChecks
import org.scalatest.time.{ Millis, Seconds, Span }
import org.scalatest.{ BeforeAndAfterAll, FreeSpecLike, Matchers }

import scala.concurrent.duration._

object ControllerSpec {

  implicit class JsonOps[A](val self: A) extends AnyVal {
    def toEntity(implicit enc: Encoder[A]): HttpEntity.Strict =
      HttpEntity(MediaTypes.`application/json`, ByteString(self.asJson.noSpaces))
  }

}

abstract class ControllerSpec
    extends FreeSpecLike
    with PropertyChecks
    with Matchers
    with BeforeAndAfterAll
    with ScalaFutures
    with FlywayWithMySQLSpecSupport
    with ScalatestRouteTest
    with TestKitBase {
  override implicit val patienceConfig: PatienceConfig =
    PatienceConfig(timeout = Span(10, Seconds), interval = Span(200, Millis))

  override def afterAll: Unit = cleanUp()

  override protected lazy val mySQLdConfig: MySQLdConfig = MySQLdConfig(
    version = v5_6_21,
    port = Some(12345),
    userWithPassword = Some(UserWithPassword("bank", "passwd")),
    timeout = Some((30 seconds) * sys.env.getOrElse("SBT_TEST_TIME_FACTOR", "1").toDouble)
  )

} 
Example 115
Source File: ActorSpec.scala    From akka-ddd-cqrs-es-example   with MIT License 5 votes vote down vote up
package com.github.j5ik2o.bank.adaptor.util

import akka.actor.ActorSystem
import akka.testkit.{ ImplicitSender, TestKit }
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.prop.PropertyChecks
import org.scalatest.{ BeforeAndAfterAll, FreeSpecLike, Matchers }

abstract class ActorSpec(system: ActorSystem)
    extends TestKit(system)
    with FreeSpecLike
    with PropertyChecks
    with ImplicitSender
    with Matchers
    with BeforeAndAfterAll
    with ScalaFutures {

  override def afterAll: Unit = TestKit.shutdownActorSystem(system)

} 
Example 116
Source File: ORSetSuite.scala    From crdt   with Apache License 2.0 5 votes vote down vote up
package com.machinomy.crdt.op

import org.scalatest.{FunSuite, Matchers}
import org.scalatest.prop.PropertyChecks
import org.scalacheck.Gen
import com.github.nscala_time.time.Imports._
import com.machinomy.crdt.state.TombStone

class ORSetSuite extends FunSuite with PropertyChecks with Matchers {
  test("fresh is empty") {
    val set = ORSet[Int, DateTime]()
    assert(set.value.isEmpty)
  }

  test("add") {
    forAll(Gen.posNum[Int]) { (i: Int) =>
      val set = ORSet[Int, DateTime]()
      val (nextSet, operation) = set.add(i)
      nextSet.value should be(Set(i))
      operation.isDefined should be(true)
      operation.get.isInstanceOf[ORSet.Add[_, _]] should be(true)
    }
  }

  test("remove if present") {
    forAll(Gen.posNum[Int]) { (i: Int) =>
      val initial = ORSet[Int, DateTime]()
      val (set, _) = initial.add(i)
      val (finalSet, operation) = set.remove(i)
      finalSet.value should be(empty)
      operation.isDefined should be(true)
      operation.get.isInstanceOf[ORSet.Remove[_, _]] should be(true)
    }
  }

  test("remove if absent") {
    forAll(Gen.posNum[Int]) { (i: Int) =>
      val initial = ORSet[Int, DateTime]()
      val (set, operation) = initial.remove(i)
      set.value should be(empty)
      operation shouldNot be(empty)
      operation.get.isInstanceOf[ORSet.Remove[_, _]] should be(true)
    }
  }

  test("add operation") {
    forAll(Gen.posNum[Int]) { (i: Int) =>
      val set = ORSet[Int, DateTime]()
      val addOp = ORSet.Add(i, implicitly[TombStone[DateTime]].next)
      val (nextSet, operation) = set.run(addOp)
      nextSet.value should be(Set(i))
      operation shouldNot be(empty)
      operation.get.isInstanceOf[ORSet.Add[_, _]] should be(true)
    }
  }

  test("remove operation if present") {
    forAll(Gen.posNum[Int]) { (i: Int) =>
      val initial = ORSet[Int, DateTime]()
      val (set, _) = initial.add(i)
      val removeOp = ORSet.Remove(i, set.state(i))
      val (finalSet, operation) = set.run(removeOp)
      finalSet.value should be(empty)
      operation.isDefined should be(true)
      operation.get.isInstanceOf[ORSet.Remove[_, _]] should be(true)
    }
  }

  test("remove operation if absent") {
    forAll(Gen.posNum[Int]) { (i: Int) =>
      val initial = ORSet[Int, DateTime]()
      val removeOp = ORSet.Remove(i, Set(implicitly[TombStone[DateTime]].next))
      val (set, operation) = initial.run(removeOp)
      set.value should be(empty)
      operation should be(empty)
    }
  }
} 
Example 117
Source File: CounterSuite.scala    From crdt   with Apache License 2.0 5 votes vote down vote up
package com.machinomy.crdt.op

import org.scalatest.{FunSuite, Matchers}
import org.scalatest.prop.PropertyChecks
import org.scalacheck.Gen

class CounterSuite extends FunSuite with PropertyChecks with Matchers {
  test("fresh value is zero") {
    val counter = Counter[Int]()
    assert(counter.value == 0)
  }

  test("increment") {
    forAll(Gen.posNum[Int]) { (i: Int) =>
      val increment = Counter.Increment(i)
      val counter = Counter.update(Counter[Int](), increment)
      counter.value should be(i)
    }
  }

  test("decrement") {
    forAll(Gen.posNum[Int]) { (i: Int) =>
      val decrement = Counter.Decrement(i)
      val counter = Counter.update(Counter[Int](), decrement)
      counter.value should be(-1 * i)
    }
  }
} 
Example 118
Source File: GCounterSuite.scala    From crdt   with Apache License 2.0 5 votes vote down vote up
package com.machinomy.crdt.state

import cats.kernel.{Eq, Monoid}
import cats.syntax.all._
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.prop.PropertyChecks

class GCounterSuite extends FunSuite with PropertyChecks {
  val replicaGen = Gen.posNum[Int]
  val valueGen = Gen.posNum[Int]
  val counterGen = for {
    id <- replicaGen
    value <- valueGen
  } yield GCounter[Int, Int]() + (id -> value)
  val eq = implicitly[Eq[GCounter[Int, Int]]]

  test("Fresh GCounter is empty") {
    val fresh = GCounter[Int, Int]()
    assert(Monoid[GCounter[Int, Int]].isEmpty(fresh))
    assert(fresh.value === 0)
  }

  test("could be calculated") {
    val counter = GCounter[Int, Int]().increment(1, 2).increment(2, 3)
    assert(counter.value === 5)
  }

  test("could be combined") {
    val a = GCounter[Int, Int]() + (1, 2) + (2, 3)
    val b = GCounter[Int, Int]() + (1, 2) + (3, 4)
    val c = a |+| b
    assert(c.value === 2 + 3 + 4)
  }

  test("could present replica value") {
    val a = GCounter[Int, Int]()
    assert(a.get(0) === 0)
    val b = a.increment(1, 2).increment(2, 3)
    assert(b.get(1) === 2)
    assert(b.get(2) === 3)
  }

  test("equality") {
    val a = GCounter[Int, Int]()
    val b = GCounter[Int, Int]()
    assert(eq.eqv(a, b))

    val a1 = a + (1 -> 1)
    assert(eq.neqv(a1, b))
    val b1 = b + (1 -> 2)
    assert(a1 !== b1)
    assert(eq.neqv(a1, b1))
    val a2 = a1 + (1 -> 1)
    assert(eq.eqv(a2, b1))
  }

  test("associativity") {
    forAll(Gen.listOfN(3, counterGen)) {
      case x :: y :: z :: Nil =>
        val left = x |+| (y |+| z)
        val right = (x |+| y) |+| z
        assert(eq.eqv(left, right))
      case _ => throw new RuntimeException("This is unexpected, really")
    }
  }

  test("commutativity") {
    forAll(Gen.listOfN(2, counterGen)) {
      case x :: y :: Nil =>
        val left = x |+| y
        val right = y |+| x
        assert(eq.eqv(left, right))
      case _ => throw new RuntimeException("This is unexpected, really")
    }
  }

  test("idempotency") {
    forAll(Gen.listOf(counterGen)) { list =>
      whenever(list.nonEmpty) {
        val counter = list.reduce(_ |+| _)
        assert(eq.eqv(counter, counter |+| counter))
      }
    }
  }
} 
Example 119
Source File: DestinationInfoHttpEndpointSpec.scala    From core   with Apache License 2.0 5 votes vote down vote up
package com.smartbackpackerapp.http

import cats.Parallel
import cats.syntax.option._
import com.smartbackpackerapp.common.TaskAssertion
import com.smartbackpackerapp.config.SBConfiguration
import com.smartbackpackerapp.http.Http4sUtils._
import com.smartbackpackerapp.model.{Country, CountryCode, CountryName, Currency, VisaNotRequired, VisaRequirementsData}
import com.smartbackpackerapp.repository.algebra.VisaRequirementsRepository
import com.smartbackpackerapp.service.{AbstractExchangeRateService, CurrencyExchangeDTO, DestinationInfoService}
import monix.eval.Task
import org.http4s.{HttpService, Query, Request, Status, Uri}
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpecLike, Matchers}

class DestinationInfoHttpEndpointSpec extends FlatSpecLike with Matchers with DestinationInfoHttpEndpointFixture {

  forAll(examples) { (from, to, expectedStatus, expectedCountry, expectedVisa) =>
    it should s"retrieve visa requirements from $from to $to" in TaskAssertion {
      val request = Request[Task](uri = Uri(path = s"/$ApiVersion/traveling/$from/to/$to", query = Query(("baseCurrency", Some("EUR")))))

      httpService(request).value.map { task =>
        task.fold(fail("Empty response")){ response =>
          response.status should be (expectedStatus)

          val body = response.body.asString
          assert(body.contains(expectedCountry))
          assert(body.contains(expectedVisa))
        }
      }
    }
  }

}

trait DestinationInfoHttpEndpointFixture extends PropertyChecks {

  val examples = Table(
    ("from", "code", "expectedStatus","expectedCountry", "expectedVisa"),
    ("AR", "GB", Status.Ok, "United Kingdom", "VisaNotRequired"),
    ("AR", "KO", Status.NotFound, "Country not found", """{"code":"100","error":"Country not found KO"}"""),
    ("AR", "AR", Status.BadRequest, "Countries must be different", """{"code":"101","error":"Countries must be different!"}""")
  )

  private val repo = new VisaRequirementsRepository[Task] {
    override def findVisaRequirements(from: CountryCode, to: CountryCode): Task[Option[VisaRequirementsData]] = Task {
      if (to.value == "KO") none[VisaRequirementsData]
      else
      VisaRequirementsData(
        from = Country(CountryCode("AR"), CountryName("Argentina"), Currency("ARS")),
        to   = Country(CountryCode("GB"), CountryName("United Kingdom"), Currency("GBP")),
        visaCategory = VisaNotRequired,
        description = "90 days within any 180 day period"
      ).some
    }
  }

  private lazy val sbConfig = new SBConfiguration[Task]

  private val rateService = new AbstractExchangeRateService[Task](sbConfig) {
    override protected def retrieveExchangeRate(uri: String): Task[CurrencyExchangeDTO] = Task {
      CurrencyExchangeDTO("EUR", "", Map("RON" -> 4.59))
    }
  }

  private implicit val errorHandler = new HttpErrorHandler[Task]

  private implicit val parallel: Parallel[Task, Task] =
    Parallel[Task, Task.Par].asInstanceOf[Parallel[Task, Task]]

  val httpService: HttpService[Task] =
    taskMiddleware(
      new DestinationInfoHttpEndpoint(
        new DestinationInfoService[Task](sbConfig, repo, rateService)
      ).service
    )

} 
Example 120
Source File: VisaRestrictionIndexHttpEndpointSpec.scala    From core   with Apache License 2.0 5 votes vote down vote up
package com.smartbackpackerapp.http

import cats.effect.IO
import com.smartbackpackerapp.common.IOAssertion
import com.smartbackpackerapp.model.{Count, CountryCode, Ranking, Sharing, VisaRestrictionsIndex}
import com.smartbackpackerapp.repository.algebra.VisaRestrictionsIndexRepository
import com.smartbackpackerapp.service.VisaRestrictionIndexService
import org.http4s.{HttpService, Request, Status, Uri}
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpecLike, Matchers}

class VisaRestrictionIndexHttpEndpointSpec extends FlatSpecLike with Matchers with VisaRestrictionIndexFixture {

  forAll(examples) { (countryCode, expectedStatus) =>
    it should s"try to retrieve visa restriction index for $countryCode" in IOAssertion {
      val request = Request[IO](uri = Uri(path = s"/$ApiVersion/ranking/$countryCode"))

      httpService(request).value.map { task =>
        task.fold(fail("Empty response")){ response =>
          response.status should be (expectedStatus)
        }
      }
    }
  }

}

trait VisaRestrictionIndexFixture extends PropertyChecks {

  import Http4sUtils._

  private val repo = new VisaRestrictionsIndexRepository[IO] {
    override def findRestrictionsIndex(countryCode: CountryCode): IO[Option[VisaRestrictionsIndex]] =
      IO {
        if (countryCode.value == "AR") Some(VisaRestrictionsIndex(Ranking(0), Count(0), Sharing(0)))
        else None
      }
  }

  private implicit val errorHandler = new HttpErrorHandler[IO]

  val httpService: HttpService[IO] =
    ioMiddleware(
      new VisaRestrictionIndexHttpEndpoint(
        new VisaRestrictionIndexService[IO](repo)
      ).service
    )

  val examples = Table(
    ("countryCode", "expectedStatus"),
    ("AR", Status.Ok),
    ("XX", Status.NotFound)
  )

} 
Example 121
Source File: HealthInfoHttpEndpointSpec.scala    From core   with Apache License 2.0 5 votes vote down vote up
package com.smartbackpackerapp.http

import cats.effect.IO
import com.smartbackpackerapp.common.IOAssertion
import com.smartbackpackerapp.model.{CountryCode, Health, HealthAlert, HealthNotices, LevelOne, Vaccinations, Vaccine}
import com.smartbackpackerapp.repository.algebra.HealthRepository
import com.smartbackpackerapp.service.HealthService
import org.http4s.{HttpService, Request, Status, Uri}
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpecLike, Matchers}

class HealthInfoHttpEndpointSpec extends FlatSpecLike with Matchers with HealthInfoFixture {

  forAll(examples) { (countryCode, expectedStatus) =>
    it should s"try to retrieve health information for $countryCode" in IOAssertion {
      val request = Request[IO](uri = Uri(path = s"/$ApiVersion/health/$countryCode"))

      httpService(request).value.map { task =>
        task.fold(fail("Empty response")){ response =>
          response.status should be (expectedStatus)
        }
      }
    }
  }

}

trait HealthInfoFixture extends PropertyChecks {

  import Http4sUtils._

  private val testHealth = Health(
    vaccinations = Vaccinations(List.empty[Vaccine], List.empty[Vaccine], List.empty[Vaccine]),
    notices = HealthNotices(
      alertLevel = LevelOne,
      alerts = List.empty[HealthAlert]
    )
  )

  private val repo = new HealthRepository[IO] {
    override def findHealthInfo(countryCode: CountryCode): IO[Option[Health]] = IO {
      if (countryCode.value == "AR") Some(testHealth)
      else None
    }
  }

  private implicit val errorHandler = new HttpErrorHandler[IO]

  val httpService: HttpService[IO] =
    ioMiddleware(
      new HealthInfoHttpEndpoint(
        new HealthService[IO](repo)
      ).service
    )

  val examples = Table(
    ("countryCode", "expectedStatus"),
    ("AR", Status.Ok),
    ("XX", Status.NotFound)
  )

}