com.typesafe.config.ConfigValueFactory Scala Examples

The following examples show how to use com.typesafe.config.ConfigValueFactory. 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: AffinityCli.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
import io.amient.affinity.core.config.CfgStruct

import scala.collection.JavaConverters._
import scala.util.control.NonFatal

trait Tool {
  def Conf: CfgStruct[_]

  def cliOptions = Conf.toMap.asScala.keys.map(cliArg).mkString(",")

  def cliArg(propertyName: String): String = s"--${propertyName.replace(".", "-")}"

  def propKey(cli: String): String = cli.drop(2).replace("-", ".")

  def apply(config: Config): Unit

  final def apply(args: List[String]): Unit = apply(args, ConfigFactory.empty)

  def apply(args: List[String], config: Config): Unit = {
    val cliKeys = Conf.toMap.asScala.keys.map(prop => cliArg(prop) -> prop).toMap
    args match {
      case cliArg :: cliValue :: tail if cliKeys.contains(cliArg) =>
        apply(tail, config.withValue(propKey(cliArg), ConfigValueFactory.fromAnyRef(cliValue)))
      case Nil => apply(config)
      case options => throw new IllegalArgumentException(s"Unknown command arguments: ${options.mkString(" ")}")
    }
  }

}

object AffinityCli extends App {

  val tools: Map[String, Tool] = Map(
    "timelog" -> TimeLogTool,
    "rebalance" -> RebalanceTool,
    "doc" -> DocTool
  )

try {
    if (args.length == 0) throw new IllegalArgumentException("Missing command argument")
    val command = args(0)
    val tool = tools(command)
    try {
      tool.apply(args.toList.drop(1))
    } catch {
      case e: IllegalArgumentException =>
        println(e.getMessage)
        println(s"Usage: affinity-cli $command [arguments]")
        println("\nRequired arguments: ")
        tool.Conf.toMap.asScala.filter(_._2.isRequired).foreach {
          case (name, cfg) =>
            println(s"\t\t${(tool.cliArg(name) + " <"+cfg.parameterInfo+">").padTo(40, ' ')}\t${cfg.description}")
        }
        println("\nOptional arguments: ")
        tool.Conf.toMap.asScala.filter(!_._2.isRequired).foreach {
          case (name, cfg) =>
            println(s"\t\t${(tool.cliArg(name) + " <"+cfg.parameterInfo+">").padTo(40, ' ')}\t${cfg.description}")
        }
    }
  } catch {
    case e: IllegalArgumentException =>
      println(e.getMessage)
      println("\nUsage: affinity-cli <command> [arguments]")
      println("\nAvailable commands:")
      tools.foreach {
        case (command, tool) => println(s"\t\t${command.padTo(15, ' ')}\t${tool.Conf.description} [${tool.cliOptions}]")
      }
      sys.exit(1)
    case NonFatal(e) =>
      e.printStackTrace()
      sys.exit(2)
  }
} 
Example 2
Source File: ConfigReaderSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import com.typesafe.config.{ ConfigFactory, ConfigObject, ConfigOriginFactory, ConfigParseOptions, ConfigValue, ConfigValueFactory }
import org.scalacheck.{ Arbitrary, Gen }
import pureconfig.error._

class ConfigReaderSuite extends BaseSuite {
  implicit override val generatorDrivenConfig = PropertyCheckConfiguration(minSuccessful = 100)

  val intReader = ConfigReader[Int]
  val strReader = ConfigReader[String]

  def intSummedReader(n: Int) = new ConfigReader[Int] {
    def from(cur: ConfigCursor) = intReader.from(cur).right.map(_ + n)
  }

  // generate configs that always read correctly as strings, but not always as integers
  val genConfig: Gen[ConfigValue] =
    Gen.frequency(80 -> Gen.chooseNum(Int.MinValue, Int.MaxValue), 20 -> Gen.alphaStr)
      .map(ConfigValueFactory.fromAnyRef)

  val genFailureReason: Gen[FailureReason] =
    Gen.const(UnknownKey(""))

  implicit val arbConfig = Arbitrary(genConfig)
  implicit val arbFailureReason = Arbitrary(genFailureReason)

  behavior of "ConfigReader"

  it should "have a correct map method" in forAll { (conf: ConfigValue, f: Int => String) =>
    intReader.map(f).from(conf) shouldEqual intReader.from(conf).right.map(f)
  }

  it should "have a map method that wraps exceptions in a ConfigReaderFailure" in {
    val throwable = new Exception("Exception message.")
    val cr = ConfigReader[Int].map({ _ => throw throwable })
    cr.from(ConfigValueFactory.fromAnyRef(1)) should failWith(ExceptionThrown(throwable))
  }

  it should "have a correct emap method" in forAll { (conf: ConfigValue, f: Int => Either[FailureReason, String]) =>
    def getReason[A](failures: ConfigReaderFailures): FailureReason = failures match {
      case ConfigReaderFailures(ConvertFailure(reason, _, _)) => reason
      case _ => throw new Exception(s"Unexpected value: $failures")
    }
    intReader.emap(f).from(conf).left.map(getReason) shouldEqual
      intReader.from(conf).left.map(getReason).right.flatMap(f)
  }

  it should "have a correct flatMap method" in forAll { conf: ConfigValue =>
    val g: Int => ConfigReader[Int] = intSummedReader
    intReader.flatMap(g).from(conf) shouldEqual intReader.from(conf).right.flatMap(g(_).from(conf))
  }

  it should "have a correct zip method" in forAll { conf: ConfigValue =>
    def zip[A, B](r1: ConfigReader[A], r2: ConfigReader[B]): ConfigReader.Result[(A, B)] = {
      (r1.from(conf), r2.from(conf)) match {
        case (Right(a), Right(b)) => Right((a, b))
        case (Left(fa), Right(_)) => Left(fa)
        case (Right(_), Left(fb)) => Left(fb)
        case (Left(fa), Left(fb)) => Left(fa ++ fb)
      }
    }

    intReader.zip(strReader).from(conf) shouldEqual zip(intReader, strReader)
    strReader.zip(intReader).from(conf) shouldEqual zip(strReader, intReader)
    intReader.zip(intReader).from(conf) shouldEqual zip(intReader, intReader)
    strReader.zip(strReader).from(conf) shouldEqual zip(strReader, strReader)
  }

  it should "have a correct orElse method" in forAll { conf: ConfigValue =>
    def orElse[AA, A <: AA, B <: AA](r1: ConfigReader[A], r2: ConfigReader[B]): ConfigReader.Result[AA] = {
      (r1.from(conf), r2.from(conf)) match {
        case (Right(a), _) => Right(a)
        case (Left(_), Right(b)) => Right(b)
        case (Left(fa), Left(fb)) => Left(fa ++ fb)
      }
    }

    // results are explicitly typed so that we also test the resulting type of `orElse`
    intReader.orElse(strReader).from(conf) shouldEqual orElse[Any, Int, String](intReader, strReader)
    strReader.orElse(intReader).from(conf) shouldEqual orElse[Any, String, Int](strReader, intReader)
    intReader.orElse(intReader).from(conf) shouldEqual orElse[Int, Int, Int](intReader, intReader)
    strReader.orElse(strReader).from(conf) shouldEqual orElse[String, String, String](strReader, strReader)
  }

  it should "have a correct contramapConfig method" in forAll { conf: ConfigValue =>
    val wrappedConf = conf.atKey("value").root()
    val unwrap = { cv: ConfigValue => cv.asInstanceOf[ConfigObject].get("value") }

    intReader.contramapConfig(unwrap).from(wrappedConf) shouldEqual intReader.from(conf)
  }
} 
Example 3
Source File: CoproductConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import com.typesafe.config.{ ConfigFactory, ConfigObject, ConfigValueFactory }
import org.scalacheck.{ Arbitrary, Gen }
import pureconfig.error._
import pureconfig.generic.auto._

class CoproductConvertersSuite extends BaseSuite {

  behavior of "ConfigConvert"

  val genBirdConfig: Gen[BirdConfig] = Arbitrary.arbBool.arbitrary.map(BirdConfig.apply)
  val genCatConfig: Gen[CatConfig] = Arbitrary.arbInt.arbitrary.map(CatConfig.apply)
  val genDogConfig: Gen[DogConfig] = Arbitrary.arbInt.arbitrary.map(DogConfig.apply)
  val genAnimalConfig: Gen[AnimalConfig] = Gen.oneOf(genBirdConfig, genCatConfig, genDogConfig)
  implicit val arbAnimalConfig = Arbitrary(genAnimalConfig)

  checkArbitrary[AnimalConfig]

  it should "read disambiguation information on sealed families by default" in {
    val conf = ConfigFactory.parseString("{ type = dog-config, age = 2 }")
    ConfigConvert[AnimalConfig].from(conf.root()) shouldEqual Right(DogConfig(2))
  }

  it should "write disambiguation information on sealed families by default" in {
    val conf = ConfigConvert[AnimalConfig].to(DogConfig(2))
    conf shouldBe a[ConfigObject]
    conf.asInstanceOf[ConfigObject].get("type") shouldEqual ConfigValueFactory.fromAnyRef("dog-config")
  }

  it should "return a proper ConfigReaderFailure if the hint field in a coproduct is missing" in {
    val conf = ConfigFactory.parseString("{ can-fly = true }")
    ConfigConvert[AnimalConfig].from(conf.root()) should failWithType[KeyNotFound]
  }

  it should "return a proper ConfigReaderFailure when a coproduct config is missing" in {
    case class AnimalCage(animal: AnimalConfig)
    ConfigConvert[AnimalCage].from(ConfigFactory.empty().root()) should failWithType[KeyNotFound]
  }
} 
Example 4
Source File: package.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import java.time._
import java.time.format.DateTimeFormatter

import com.typesafe.config.ConfigValueFactory
import pureconfig.ConfigConvert.{ catchReadError, viaNonEmptyString }
import pureconfig.error.FailureReason

import scala.collection.JavaConverters._


package object configurable {

  def localDateConfigConvert(formatter: DateTimeFormatter): ConfigConvert[LocalDate] =
    viaNonEmptyString[LocalDate](
      catchReadError(LocalDate.parse(_, formatter)), _.format(formatter))

  def localTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[LocalTime] =
    viaNonEmptyString[LocalTime](
      catchReadError(LocalTime.parse(_, formatter)), _.format(formatter))

  def localDateTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[LocalDateTime] =
    viaNonEmptyString[LocalDateTime](
      catchReadError(LocalDateTime.parse(_, formatter)), _.format(formatter))

  def monthDayConfigConvert(formatter: DateTimeFormatter): ConfigConvert[MonthDay] =
    viaNonEmptyString[MonthDay](
      catchReadError(MonthDay.parse(_, formatter)), _.format(formatter))

  def offsetDateTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[OffsetDateTime] =
    viaNonEmptyString[OffsetDateTime](
      catchReadError(OffsetDateTime.parse(_, formatter)), _.format(formatter))

  def offsetTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[OffsetTime] =
    viaNonEmptyString[OffsetTime](
      catchReadError(OffsetTime.parse(_, formatter)), _.format(formatter))

  def yearMonthConfigConvert(formatter: DateTimeFormatter): ConfigConvert[YearMonth] =
    viaNonEmptyString[YearMonth](
      catchReadError(YearMonth.parse(_, formatter)), _.format(formatter))

  def zonedDateTimeConfigConvert(formatter: DateTimeFormatter): ConfigConvert[ZonedDateTime] =
    viaNonEmptyString[ZonedDateTime](
      catchReadError(ZonedDateTime.parse(_, formatter)), _.format(formatter))

  def genericMapReader[K, V](keyParser: String => Either[FailureReason, K])(implicit readerV: Derivation[ConfigReader[V]]): ConfigReader[Map[K, V]] =
    ConfigReader.fromCursor { cursor =>
      cursor.asMap.right.flatMap { map =>
        map.foldLeft[ConfigReader.Result[Map[K, V]]](Right(Map.empty)) {
          case (acc, (key, valueCursor)) =>
            val eitherKeyOrError = cursor.scopeFailure(keyParser(key))
            val eitherValueOrError = readerV.value.from(valueCursor)
            ConfigReader.Result.zipWith(acc, ConfigReader.Result.zipWith(eitherKeyOrError, eitherValueOrError)(_ -> _))(_ + _)
        }
      }
    }

  def genericMapWriter[K, V](keyFormatter: K => String)(implicit writerV: Derivation[ConfigWriter[V]]): ConfigWriter[Map[K, V]] =
    ConfigWriter.fromFunction[Map[K, V]](map =>
      ConfigValueFactory.fromMap(map.map {
        case (key, value) =>
          keyFormatter(key) -> writerV.value.to(value)
      }.asJava))
} 
Example 5
Source File: DocSvr.scala    From Raphtory   with Apache License 2.0 5 votes vote down vote up
package com.raphtory.core.clustersetup

import akka.actor.ActorSystem
import akka.actor.Address
import akka.actor.ExtendedActorSystem
import akka.cluster.Cluster
import akka.cluster.Member
import akka.event.LoggingAdapter
import akka.management.cluster.bootstrap.ClusterBootstrap
import akka.management.javadsl.AkkaManagement
import com.raphtory.core.clustersetup.util.ConfigUtils._
import com.raphtory.core.utils.Utils
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigValueFactory

import scala.collection.JavaConversions
import scala.collection.JavaConversions._

trait DocSvr {

  def seedLoc: String

  implicit val system: ActorSystem
  val docker = System.getenv().getOrDefault("DOCKER", "false").trim.toBoolean

  val clusterSystemName: String = Utils.clusterSystemName
  val ssn: String               = java.util.UUID.randomUUID.toString

  
  def printConfigInfo(config: Config, system: ActorSystem): Unit = {
    val log: LoggingAdapter = system.log

    val systemConfig: SystemConfig = config.parse()
    val bindAddress: SocketAddress = systemConfig.bindAddress
    val tcpAddress: SocketAddress  = systemConfig.tcpAddress

    log.info(s"Created ActorSystem with ID: $ssn")

    log.info(s"Binding ActorSystem internally to address ${bindAddress.host}:${bindAddress.port}")
    log.info(s"Binding ActorSystem externally to host ${tcpAddress.host}:${tcpAddress.port}")

    log.info(s"Registering the following seeds to ActorSystem: ${systemConfig.seeds}")
    log.info(s"Registering the following roles to ActorSystem: ${systemConfig.roles}")

    // FIXME: This is bit unorthodox ...
    val akkaSystemUrl: Address = system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress
    log.info(s"ActorSystem successfully initialised at the following Akka URL: $akkaSystemUrl")
  }
} 
Example 6
Source File: Runner.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.bg

import cmwell.common.ZStoreOffsetsService
import cmwell.driver.Dao
import cmwell.fts.FTSService
import cmwell.irw.IRWService
import cmwell.zstore.ZStore
import com.typesafe.config.{ConfigFactory, ConfigValueFactory}
import com.typesafe.scalalogging.LazyLogging
import k.grid.service.ServiceTypes
import k.grid.{Grid, GridConnection}
import uk.org.lidalia.sysoutslf4j.context.SysOutOverSLF4J

import scala.collection.JavaConverters._
import scala.concurrent.duration._
import scala.language.postfixOps


        neighborhoodPartitions.foldLeft(basicServiceTypes) { (st,par) =>
          st.add(s"BGActor$par", classOf[CMWellBGActor], par,
            config.withValue("cmwell.bg.persist.commands.partition", ConfigValueFactory.fromAnyRef(par))
              .withValue("cmwell.bg.index.commands.partition", ConfigValueFactory.fromAnyRef(par)),
            irwService, ftsService, zStore, offsetsService
          )
        }
      }

      Grid.setGridConnection(GridConnection(memberName = "bg", labels = Set("bg")))
      Grid.declareServices(serviceTypes)

      Grid.joinClient

      Thread.sleep(60000)
    } catch {
      case t: Throwable =>
        logger.error(s"BG Process failed to start thus exiting. Reason:\n${cmwell.common.exception.getStackTrace(t)}")
        sys.exit(1)
    }
  }
} 
Example 7
Source File: BgTestHelpers.scala    From CM-Well   with Apache License 2.0 5 votes vote down vote up
package cmwell.bg.test

import java.util.Properties

import cmwell.driver.Dao
import cmwell.fts.FTSService
import com.typesafe.config.{ConfigFactory, ConfigValueFactory}
import org.apache.kafka.clients.producer.KafkaProducer
import org.elasticsearch.action.ActionListener
import org.elasticsearch.action.admin.indices.create.{CreateIndexRequest, CreateIndexResponse}
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest
import org.elasticsearch.action.support.master.AcknowledgedResponse
import org.elasticsearch.common.xcontent.XContentType
import concurrent.duration._
import scala.concurrent.{Await, Promise}
import scala.io.Source

object BgTestHelpers {
  def kafkaProducer(bootstrapServers: String)= {
    val producerProperties = new Properties
    producerProperties.put("bootstrap.servers", bootstrapServers)
    producerProperties.put("key.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer")
    producerProperties.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer")
    new KafkaProducer[Array[Byte], Array[Byte]](producerProperties)
  }

  def dao(address: String, port: Int) = {
    // scalastyle:off
    val initCommands = Some(List(
      "CREATE KEYSPACE IF NOT EXISTS data2 WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1};",
      "CREATE TABLE IF NOT EXISTS data2.Path ( path text, uuid text, last_modified timestamp, PRIMARY KEY ( path, last_modified, uuid ) ) WITH CLUSTERING ORDER BY (last_modified DESC, uuid ASC) AND compression = { 'class' : 'LZ4Compressor' } AND caching = {'keys':'ALL', 'rows_per_partition':'1'};",
      "CREATE TABLE IF NOT EXISTS data2.Infoton (uuid text, quad text, field text, value text, data blob, PRIMARY KEY (uuid,quad,field,value)) WITH compression = { 'class' : 'LZ4Compressor' } AND caching = {'keys':'ALL', 'rows_per_partition':'1000'};"
    ))
    // scalastyle:on
    Dao("Test","data2", address, port, initCommands = initCommands)
  }

  def ftsOverridesConfig(address: String, port: Int) = {
    ConfigFactory.load()
      .withValue("ftsService.clusterName", ConfigValueFactory.fromAnyRef("docker-cluster"))
      .withValue("ftsService.transportAddress", ConfigValueFactory.fromIterable(java.util.Arrays.asList(address)))
      .withValue("ftsService.transportPort", ConfigValueFactory.fromAnyRef(port))
  }

  def initFTSService(ftsService: FTSService) = {
    val putTemplateRequest = new PutIndexTemplateRequest("indices_template")
    val indicesTemplateStr = {
      val templateSource = Source.fromURL(this.getClass.getResource("/indices_template.json"))
      try templateSource.getLines.mkString("\n") finally templateSource.close()
    }
    putTemplateRequest.source(indicesTemplateStr, XContentType.JSON)
    val putTemplatePromise = Promise[AcknowledgedResponse]()
    ftsService.client.admin().indices().putTemplate(putTemplateRequest, new ActionListener[AcknowledgedResponse] {
      override def onResponse(response: AcknowledgedResponse): Unit = putTemplatePromise.success(response)
      override def onFailure(e: Exception): Unit = putTemplatePromise.failure(e)
    })
    val putTemplateAck = Await.result(putTemplatePromise.future, 1.minute)
    if (!putTemplateAck.isAcknowledged)
      throw new Exception("ES didn't acknowledge the put template request")
    val createIndexPromise = Promise[AcknowledgedResponse]()
    ftsService.client.admin().indices().create(new CreateIndexRequest("cm_well_p0_0"), new ActionListener[CreateIndexResponse] {
      override def onResponse(response: CreateIndexResponse): Unit = createIndexPromise.success(response)
      override def onFailure(e: Exception): Unit = createIndexPromise.failure(e)
    })
    val createIndexResponse = Await.result(putTemplatePromise.future, 1.minute)
    if (!createIndexResponse.isAcknowledged)
      throw new Exception("ES didn't acknowledge the create index request")
  }

} 
Example 8
Source File: PostgresAsyncContextConfigSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill

import java.io.File

import com.github.mauricio.async.db.SSLConfiguration
import com.github.mauricio.async.db.SSLConfiguration.Mode
import com.typesafe.config.{ ConfigFactory, ConfigValueFactory }

class PostgresAsyncContextConfigSpec extends Spec {

  "parses ssl config" in {
    val config = ConfigFactory.empty()
      .withValue("user", ConfigValueFactory.fromAnyRef("user"))
      .withValue("port", ConfigValueFactory.fromAnyRef(5432))
      .withValue("host", ConfigValueFactory.fromAnyRef("host"))
      .withValue("sslmode", ConfigValueFactory.fromAnyRef("require"))
      .withValue("sslrootcert", ConfigValueFactory.fromAnyRef("./file.crt"))
    val context = new PostgresAsyncContextConfig(config)
    context.configuration.ssl mustEqual SSLConfiguration(Mode.Require, Some(new File("./file.crt")))
  }
} 
Example 9
Source File: PostgresJAsyncContextConfigSpec.scala    From quill   with Apache License 2.0 5 votes vote down vote up
package io.getquill

import java.io.File

import com.github.jasync.sql.db.SSLConfiguration
import com.github.jasync.sql.db.SSLConfiguration.Mode
import com.typesafe.config.{ ConfigFactory, ConfigValueFactory }

class PostgresJAsyncContextConfigSpec extends Spec {

  "parses ssl config" in {
    val config = ConfigFactory.empty()
      .withValue("user", ConfigValueFactory.fromAnyRef("user"))
      .withValue("port", ConfigValueFactory.fromAnyRef(5432))
      .withValue("host", ConfigValueFactory.fromAnyRef("host"))
      .withValue("sslmode", ConfigValueFactory.fromAnyRef("require"))
      .withValue("sslrootcert", ConfigValueFactory.fromAnyRef("./file.crt"))
    val context = new PostgresJAsyncContextConfig(config)
    context.connectionPoolConfiguration.getSsl mustEqual new SSLConfiguration(Mode.Require, new File("./file.crt"))
  }
} 
Example 10
Source File: ParamParser.scala    From DataQuality   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package models.config

import com.typesafe.config.{ConfigValue, ConfigValueFactory}

import scala.collection.JavaConversions._
import scala.util.matching.Regex.MatchIterator


object ParamParser {

  def parseParam(string: String): ConfigValue = {
    val regex = "\\[.*\\]".r

    string match {
      case regex() =>
        val r = """(?:\.,|#)?\w+""".r
        val matches: MatchIterator = r findAllIn string
        ConfigValueFactory.fromIterable(matches.toSeq)
      case _ =>
        ConfigValueFactory.fromAnyRef(string)
    }
  }

  def unquote(string: String): String = {
    val regex = "\".*\"".r
    string match {
      case regex() => string.substring(1, string.length-1)
      case _ => string
    }
  }

} 
Example 11
Source File: ParamParser.scala    From DataQuality   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package dbmodel.config

import com.typesafe.config.{ConfigValue, ConfigValueFactory}

import scala.collection.JavaConversions._
import scala.util.matching.Regex.MatchIterator


object ParamParser {

  def parseParam(string: String): ConfigValue = {
    val regex = "\\[.*\\]".r

    string match {
      case regex() =>
        val r = """(?:\.,|#)?\w+""".r
        val matches: MatchIterator = r findAllIn string
        ConfigValueFactory.fromIterable(matches.toSeq)
      case _ =>
        ConfigValueFactory.fromAnyRef(string)
    }
  }

  def unquote(string: String): String = {
    val regex = "\".*\"".r
    string match {
      case regex() => string.substring(1, string.length-1)
      case _ => string
    }
  }

} 
Example 12
Source File: AkkaSerializationSystemTest.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
package io.amient.affinity.avro

import akka.serialization.SerializationExtension
import com.typesafe.config.{ConfigFactory, ConfigValueFactory}
import io.amient.affinity.avro.HttpSchemaRegistry.HttpAvroConf
import io.amient.affinity.avro.record.AvroRecord
import io.amient.affinity.kafka.EmbeddedConfluentRegistry
import io.amient.affinity.{AffinityActorSystem, Conf}
import org.scalatest.FlatSpec


case class ExampleType(val id: Int) extends AvroRecord {
  override def hashCode(): Int = id.hashCode()
}

class AkkaSerializationSystemTest extends FlatSpec with EmbeddedConfluentRegistry {

  val config = ConfigFactory.empty
    .withValue(Conf.Affi.SystemName.path, ConfigValueFactory.fromAnyRef("CfTest"))
    .withValue(Conf.Affi.Avro.Class.path, ConfigValueFactory.fromAnyRef(classOf[HttpSchemaRegistry].getName))
    .withValue(HttpAvroConf(Conf.Affi.Avro).HttpSchemaRegistryUrl.path, ConfigValueFactory.fromAnyRef(registryUrl))


  assert(config.getString(HttpAvroConf(Conf.Affi.Avro).HttpSchemaRegistryUrl.path) == registryUrl)

  override def numPartitions = 2

  "Confluent Schema Registry " should "be available via akka SerializationExtension" in {
    val system = AffinityActorSystem.create(config)
    try {
      val serialization = SerializationExtension(system)
      val serde = serialization.serializerFor(classOf[ExampleType])
      assert(serde.fromBinary(serde.toBinary(ExampleType(101))) == ExampleType(101))
    } finally {
      system.terminate()
    }
  }
} 
Example 13
Source File: ConfigConvertSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import com.typesafe.config.{ ConfigValue, ConfigValueFactory, ConfigValueType }
import org.scalacheck.{ Arbitrary, Gen }
import pureconfig.error.{ ExceptionThrown, WrongType, CannotConvert }
import ConfigConvertSuite._

class ConfigConvertSuite extends BaseSuite {
  implicit override val generatorDrivenConfig = PropertyCheckConfiguration(minSuccessful = 100)

  val intConvert = ConfigConvert[Int]

  // generate configs that always read correctly as strings, but not always as integers
  val genConfig: Gen[ConfigValue] =
    Gen.frequency(80 -> Gen.chooseNum(Int.MinValue, Int.MaxValue), 20 -> Gen.alphaStr)
      .map(ConfigValueFactory.fromAnyRef)

  implicit val arbConfig = Arbitrary(genConfig)

  behavior of "ConfigConvert"

  it should "have a correct xmap method" in forAll { (f: Int => String, g: String => Int) =>
    forAll { str: String => intConvert.xmap(f, g).to(str) shouldEqual intConvert.to(g(str)) }
    forAll { conf: ConfigValue => intConvert.xmap(f, g).from(conf) shouldEqual intConvert.from(conf).right.map(f) }
  }

  it should "have a xmap method that wraps exceptions in a ConfigReaderFailure" in {
    val throwable = new Exception("Exception message.")
    val cc = ConfigConvert[Int].xmap[String]({ _ => throw throwable }, { _: String => 42 })
    cc.from(ConfigValueFactory.fromAnyRef(1)) should failWith(
      ExceptionThrown(throwable))
    cc.from(ConfigValueFactory.fromAnyRef("test")) should failWith(
      WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)))
  }

  it should "have a xemap method that allows specifying custom failure messages" in {
    ConfigConvert[EvenInt].from(ConfigValueFactory.fromAnyRef(1)) should
      failWith(CannotConvert("1", "EvenInt", EvenInt.err(1)))
  }

  it should "correctly read using xemap" in {
    ConfigConvert[EvenInt].from(ConfigValueFactory.fromAnyRef(2)) shouldEqual Right(EvenInt(2))
  }
}

object ConfigConvertSuite {
  case class EvenInt(i: Int) extends AnyVal
  object EvenInt {
    def err(i: Int): String = s"Cannot construct an EvenInt from $i because it's not even"

    def safely(i: Int): Either[String, EvenInt] =
      if (i % 2 == 0) Right(EvenInt(i)) else Left(err(i))

    implicit val configConvert: ConfigConvert[EvenInt] =
      ConfigConvert[Int].xemap(
        i => safely(i).left.map(s => CannotConvert(i.toString, "EvenInt", s)),
        _.i)
  }
} 
Example 14
Source File: CoordinatorEmbeddedSpec.scala    From affinity   with Apache License 2.0 5 votes vote down vote up
package io.amient.affinity.core.cluster

import java.util.concurrent.atomic.AtomicReference

import akka.actor.{Actor, ActorRef, Props}
import com.typesafe.config.{ConfigFactory, ConfigValueFactory}
import io.amient.affinity.avro.MemorySchemaRegistry
import io.amient.affinity.core.cluster.Coordinator.MembershipUpdate
import io.amient.affinity.{AffinityActorSystem, Conf}
import org.scalatest.{FlatSpec, Matchers}

import scala.collection.JavaConverters._

class CoordinatorEmbeddedSpec extends FlatSpec with Matchers {

  "CoordinatorEmbedded instances" should "share the underlying space for the same id and group" in {
    val config = ConfigFactory.empty()
      .withValue(Conf.Affi.SystemName.path, ConfigValueFactory.fromAnyRef("101"))
      .withValue(Conf.Affi.Node.path, ConfigValueFactory.fromMap(Map[String, String]().asJava))
      .withValue(Conf.Affi.Avro.Class.path, ConfigValueFactory.fromAnyRef(classOf[MemorySchemaRegistry].getName))
      .withValue(Conf.Affi.Coordinator.Class.path, ConfigValueFactory.fromAnyRef(classOf[CoordinatorEmbedded].getName))
    val system = AffinityActorSystem.create(config)
    try {
      val coordinator1 = Coordinator.create(system, "group1")
      val actor1 = system.actorOf(Props(new Actor {
        override def receive: Receive = {
          case null =>
        }
      }), "actor1")
      coordinator1.register(actor1.path)
      val update1 = new AtomicReference[scala.collection.Set[ActorRef]](Set.empty)
      update1 synchronized {
        coordinator1.watch(system.actorOf(Props(new Actor {
          override def receive: Receive = {
            case MembershipUpdate(masters) => update1 synchronized update1.set(masters.values.toSet)
          }
        }), "subscriber1"))
      }
      coordinator1.close()

      val coordinator2 = Coordinator.create(system, "group1")
      val update2 = new AtomicReference[scala.collection.Set[ActorRef]](Set.empty)
      update2 synchronized {
        coordinator2.watch(system.actorOf(Props(new Actor {
          override def receive: Receive = {
            case MembershipUpdate(masters) => update2 synchronized update2.set(masters.values.toSet)
          }
        }), "subscriber2"))
        update2.wait(1000)
        update2.get.map(_.path.toString) should be(Set("akka://101/user/actor1"))
        update1.get.map(_.path.toString) should be(Set("akka://101/user/actor1"))
      }
      coordinator2.close()


    } finally {
      system.terminate()
    }

  }

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

import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger

import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model.StatusCode
import akka.http.scaladsl.model.StatusCodes.SeeOther
import com.typesafe.config.ConfigValueFactory
import io.amient.affinity.Conf
import io.amient.affinity.core.util.AffinityTestBase
import io.amient.affinity.kafka.EmbeddedKafka
import org.scalatest.{FlatSpec, Matchers}

import scala.collection.JavaConverters._
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.language.postfixOps
import scala.util.{Failure, Random, Success, Try}


class Failover2Spec extends FlatSpec with AffinityTestBase with EmbeddedKafka with Matchers {

  val specTimeout = 15 seconds

  override def numPartitions = 2

  def config = configure("failoverspecs", Some(zkConnect), Some(kafkaBootstrap))

  val node1 = new Node(config.withValue(Conf.Affi.Node.Gateway.Class.path, ConfigValueFactory.fromAnyRef(classOf[FailoverTestGateway].getName)))
  val node2 = new Node(config.withValue(Conf.Affi.Node.Containers("keyspace1").path, ConfigValueFactory.fromIterable(List(0,1).asJava)))
  val node3 = new Node(config.withValue(Conf.Affi.Node.Containers("keyspace1").path, ConfigValueFactory.fromIterable(List(0,1).asJava)))

  override def beforeAll(): Unit = try {
    node1.start()
    node2.start()
    node3.start()
    node1.awaitClusterReady()
  } finally {
    super.beforeAll()
  }

  override def afterAll(): Unit = try {
    node1.shutdown()
    node2.shutdown()
    node3.shutdown()
  } finally {
    super.afterAll()
  }

  "Master Transition" should "not lead to inconsistent state" in {
    val requestCount = new AtomicInteger(0)
    val expected = new ConcurrentHashMap[String, String]()
    import scala.concurrent.ExecutionContext.Implicits.global

    val random = new Random()
    val requests = scala.collection.mutable.ListBuffer[Future[Try[StatusCode]]]()
    for (i <- (1 to 250)) {
      val key = random.nextInt.toString
      val value = random.nextInt.toString
      requests += node1.http(POST, s"/$key/$value") map {
        case response =>
          expected.put(key, value)
          if (i == 25) {
            //after a few writes have succeeded kill one node
            node2.shutdown()
          }
          Success(response.status)
      } recover {
        case e: Throwable =>
          Failure(e)
      }
    }
    requestCount.set(requests.size)
    Await.result(Future.sequence(requests), specTimeout).foreach(_ should be(Success(SeeOther)))
    println(s"${requests.size} successful requests")

    expected.asScala.foreach { case (key, value) =>
      val (status, entity) = Await.result(node1.http(GET, s"/$key").map { response => (response.status, response.entity)}, specTimeout / 3)
      status.intValue should be (200)
      val expectedEntity = jsonStringEntity(value)
      entity should be(expectedEntity)
    }
  }

} 
Example 16
Source File: Main.scala    From distributed-cache-on-k8s-poc   with MIT License 5 votes vote down vote up
import akka.actor.ActorSystem
import akka.event.Logging
import akka.http.scaladsl.Http
import akka.stream.{ ActorMaterializer, ActorMaterializerSettings }
import cluster.ClusterStateInformer
import com.typesafe.config.{ Config, ConfigFactory, ConfigValueFactory }
import http.Route

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

object Main {

  def main(args: Array[String]): Unit = {
    val config: Config = {
      import scala.collection.JavaConverters._
      val seedNodes = ClusterSetup.seedNodes()
      ConfigFactory.empty()
        .withValue("akka.cluster.seed-nodes", ConfigValueFactory.fromIterable(seedNodes.map(seedNode => s"akka.tcp://${ClusterSetup.actorSystemName()}@$seedNode").asJava))
        .withValue("akka.remote.netty.tcp.hostname", ConfigValueFactory.fromAnyRef(ClusterSetup.podName() + "." + ClusterSetup.domain()))
        .withValue("akka.remote.netty.tcp.port", ConfigValueFactory.fromAnyRef(ClusterSetup.remoteBindingPort()))
        .withFallback(ConfigFactory.load())
        .resolve()
    }

    implicit val system: ActorSystem = ActorSystem(ClusterSetup.actorSystemName(), config)
    val logging = Logging(system, "main")
    implicit val mat = ActorMaterializer(materializerSettings = Some(ActorMaterializerSettings(system)))
    val routes = new Route(system)
    Http().bindAndHandle(routes.routes, "0.0.0.0", 9000).onComplete {
      case Success(s) => logging.info("Successfully started")
      case Failure(f) => logging.error(f, "Server cannot be started!!!!")
    }

    system.actorOf(ClusterStateInformer.props(), "cluster-informer")
  }

}

object ClusterSetup {
  def seedNodes(): Iterable[String] = sys.env.get("AKKA_SEED_NODES").map(_.split(",")).get.toIterable

  def domain(): String = sys.env.getOrElse("AKKA_REMOTING_BIND_DOMAIN", throw new RuntimeException("No domain found."))

  def podName(): String = sys.env.getOrElse("POD_NAME", throw new RuntimeException("No podname found."))

  def remoteBindingPort(): String = sys.env.getOrElse("AKKA_REMOTING_BIND_PORT", throw new RuntimeException("No port found."))

  def actorSystemName(): String = sys.env.getOrElse("AKKA_ACTOR_SYSTEM_NAME", throw new RuntimeException("No actorsystem name found."))
} 
Example 17
Source File: FunctionConfigStorage.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.master.data

import java.io.File
import java.nio.file.Paths
import java.util.concurrent.Executors

import com.typesafe.config.{ConfigValueFactory, ConfigFactory, ConfigValueType, Config}
import io.hydrosphere.mist.master.models.FunctionConfig
import io.hydrosphere.mist.utils.Logger

import scala.collection.JavaConverters._
import scala.concurrent.{Future, ExecutionContext}
import scala.util._

class FunctionConfigStorage(
  fsStorage: FsStorage[FunctionConfig],
  val defaults: Seq[FunctionConfig]
)(implicit ex: ExecutionContext) {

  private val defaultMap = defaults.map(e => e.name -> e).toMap

  def all: Future[Seq[FunctionConfig]] =
    Future { fsStorage.entries } map (seq => {
      val merged = defaultMap ++ seq.map(a => a.name -> a).toMap
      merged.values.toSeq
    })

  def get(name: String): Future[Option[FunctionConfig]] = {
    Future { fsStorage.entry(name) } flatMap {
      case s @ Some(_) => Future.successful(s)
      case None => Future.successful(defaultMap.get(name))
    }
  }

  def delete(name: String): Future[Option[FunctionConfig]] = {
    Future { fsStorage.delete(name) }
  }

  def update(ec: FunctionConfig): Future[FunctionConfig] =
    Future { fsStorage.write(ec.name, ec) }

}

object FunctionConfigStorage extends Logger {

  def create(
    storagePath: String,
    defaultConfigPath: String): FunctionConfigStorage = {

    val defaults = fromDefaultsConfig(defaultConfigPath)
    val fsStorage = new FsStorage(checkDirectory(storagePath), ConfigRepr.EndpointsRepr)
    val ec = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(3))
    new FunctionConfigStorage(fsStorage, defaults)(ec)
  }

  def fromDefaultsConfig(path: String): Seq[FunctionConfig] = {
    val file = new File(path)
    if (!file.exists()) {
      Seq.empty
    } else {
      logger.warn("Starting using router conf (that feature will be removed - please use http api for uploading functions)")
      val directory = Paths.get(path).getParent
      val config = ConfigFactory.parseFile(file)
        .withValue("location", ConfigValueFactory.fromAnyRef(directory.toString))
        .resolve()
      parseConfig(config)
    }
  }

  def parseConfig(config: Config): Seq[FunctionConfig] = {
    def parse(name: String): Try[FunctionConfig] = Try {
      val part = config.getConfig(name)
      ConfigRepr.EndpointsRepr.fromConfig(name, part)
    }

    config.root().keySet().asScala
      .filter(k => config.getValue(k).valueType() == ConfigValueType.OBJECT)
      .map(name => parse(name))
      .foldLeft(List.empty[FunctionConfig])({
        case (lst, Failure(e)) =>
          logger.warn("Invalid configuration for function", e)
          lst
        case (lst, Success(c)) => lst :+ c
      })
  }
} 
Example 18
Source File: FStorageSpec.scala    From mist   with Apache License 2.0 5 votes vote down vote up
package io.hydrosphere.mist.master.data

import java.nio.file.Paths

import com.typesafe.config.{Config, ConfigValueFactory}
import io.hydrosphere.mist.master.models.NamedConfig
import org.apache.commons.io.FileUtils
import org.scalatest._

class FStorageSpec extends FunSpec with Matchers with BeforeAndAfter {

  case class TestEntry(
    name: String,
    value: Int
  ) extends NamedConfig

  val testEntryConfigRepr = new ConfigRepr[TestEntry] {
    import scala.collection.JavaConverters._

    override def fromConfig(config: Config): TestEntry = {
      TestEntry(config.getString("name"), config.getInt("value"))
    }

    override def toConfig(a: TestEntry): Config = {
      val map = Map("value" -> ConfigValueFactory.fromAnyRef(a.value))
      ConfigValueFactory.fromMap(map.asJava).toConfig
    }
  }

  val path = "./target/file_store_test"

  before {
    val f = Paths.get(path).toFile
    if (f.exists()) FileUtils.deleteDirectory(f)
  }

  it("should store files") {
    val storage = FsStorage.create(path, testEntryConfigRepr)

    storage.write("one", TestEntry("one", 1))
    storage.write("two", TestEntry("two", 2))

    storage.entries should contain allOf(
      TestEntry("one", 1),
      TestEntry("two", 2)
    )

    storage.delete("one")
    storage.entries should contain allElementsOf(Seq(TestEntry("two", 2)))
  }

} 
Example 19
Source File: WriteCoordinatorSpec.scala    From NSDb   with Apache License 2.0 5 votes vote down vote up
package io.radicalbit.nsdb.cluster.coordinator

import akka.actor.ActorSystem
import akka.pattern.ask
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import com.typesafe.config.{ConfigFactory, ConfigValueFactory}
import io.radicalbit.nsdb.protocol.MessageProtocol.Commands._
import org.scalatest._

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

class WriteCoordinatorSpec
    extends TestKit(
      ActorSystem(
        "WriteCoordinatorSpec",
        ConfigFactory
          .load()
          .withValue("nsdb.sharding.interval", ConfigValueFactory.fromAnyRef("5s"))
      ))
    with ImplicitSender
    with WordSpecLike
    with Matchers
    with BeforeAndAfter
    with BeforeAndAfterAll
    with WriteCoordinatorBehaviour {

  lazy val basePath = "target/test_index/WriteCoordinatorSpec"

  val db        = "writeCoordinatorSpecDB"
  val namespace = "namespace"

  implicit val timeout = Timeout(10 seconds)

  override def beforeAll: Unit = {
    Await.result(writeCoordinatorActor ? SubscribeCommitLogCoordinator(commitLogCoordinator, "localhost"), 10 seconds)
    Await.result(writeCoordinatorActor ? SubscribeMetricsDataActor(metricsDataActor, "localhost"), 10 seconds)
    Await.result(writeCoordinatorActor ? SubscribePublisher(publisherActor, "localhost"), 10 seconds)
    Await.result(writeCoordinatorActor ? DeleteNamespace(db, namespace), 10 seconds)
    Await.result(schemaCoordinator ? UpdateSchemaFromRecord(db, namespace, "testMetric", record1), 10 seconds)
  }

  "WriteCoordinator" should behave.like(defaultBehaviour)
} 
Example 20
Source File: NSDbMiniClusterConfigProvider.scala    From NSDb   with Apache License 2.0 5 votes vote down vote up
package io.radicalbit.nsdb.minicluster

import java.time.Duration

import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
import io.radicalbit.nsdb.common.configuration.NSDbConfigProvider

trait NSDbMiniClusterConfigProvider extends NSDbConfigProvider {

  def hostname: String
  def storageDir: String
  def passivateAfter: Duration
  def replicationFactor: Int

  override lazy val userDefinedConfig: Config =
    ConfigFactory
      .parseResources("nsdb-minicluster.conf")
      .withValue("nsdb.node.hostname", ConfigValueFactory.fromAnyRef(hostname))
      .withValue("nsdb.grpc.interface", ConfigValueFactory.fromAnyRef(hostname))
      .withValue("nsdb.http.interface", ConfigValueFactory.fromAnyRef(hostname))
      .withValue("nsdb.storage.base-path", ConfigValueFactory.fromAnyRef(storageDir))
      .withValue("nsdb.cluster.replication-factor", ConfigValueFactory.fromAnyRef(replicationFactor))
      .resolve()

  override lazy val lowLevelTemplateConfig: Config =
    mergeConf(userDefinedConfig,
              ConfigFactory.parseResources("application-native.conf"),
              ConfigFactory.parseResources("application-common.conf"))
} 
Example 21
Source File: ContainerBuilder.scala    From service-container   with Apache License 2.0 5 votes vote down vote up
package com.github.vonnagy.service.container

import com.github.vonnagy.service.container.core.CoreConfig
import com.github.vonnagy.service.container.health.HealthCheck
import com.github.vonnagy.service.container.http.routing.RoutedEndpoints
import com.github.vonnagy.service.container.listener.ContainerLifecycleListener
import com.github.vonnagy.service.container.service.ContainerService


case class ContainerBuilder(
                             endpoints: Seq[Class[_ <: RoutedEndpoints]] = Seq.empty,
                             healthChecks: Seq[HealthCheck] = Seq.empty,
                             props: Seq[(String, Props)] = Seq.empty,
                             listeners: Seq[ContainerLifecycleListener] = Seq.empty,
                             config: Config = ConfigFactory.empty,
                             name: String = "service-container",
                             system: Option[ActorSystem] = None
                           ) extends CoreConfig {

  def withConfig(conf: Config): ContainerBuilder = copy(config = conf)

  def withRoutes(routes: Class[_ <: RoutedEndpoints]*): ContainerBuilder = copy(endpoints = routes)

  def withConfigValue(name: String, value: Any): ContainerBuilder =
    copy(config = this.config.withValue(name, ConfigValueFactory.fromAnyRef(value)))

  def withHealthChecks(checks: HealthCheck*): ContainerBuilder = copy(healthChecks = checks)

  def withActors(actors: (String, Props)*): ContainerBuilder = copy(props = actors)

  def withListeners(obs: ContainerLifecycleListener*): ContainerBuilder = copy(listeners = obs)

  def withActorSystem(sys: ActorSystem): ContainerBuilder = copy(system = Some(sys))

  def withName(name: String): ContainerBuilder = copy(name = name)

  def build: ContainerService = {
    implicit val actorSystem = system.getOrElse(ActorSystem.create(name, getConfig(Some(config))))
    val svc = new ContainerService(endpoints, healthChecks, props, listeners, name) with App
    svc
  }

  def validateConfig(paths: String*) = {
    paths.foreach { path =>
      if (!config.hasPath(path)) {
        throw new MissingConfigException(s"Missing required config property: '$path'.")
      }
    }
  }
}

class MissingConfigException(s: String) extends RuntimeException(s) 
Example 22
Source File: CheckpointWriter.scala    From berilia   with Apache License 2.0 5 votes vote down vote up
package com.criteo.dev.cluster.config

import com.typesafe.config.{Config, ConfigFactory, ConfigRenderOptions, ConfigValueFactory}

import collection.JavaConverters._

object CheckpointWriter {
  def apply(checkpoint: Checkpoint): Config = {
    import checkpoint._
    ConfigFactory.empty
      .withValue("created", ConfigValueFactory.fromAnyRef(created.toEpochMilli))
      .withValue("updated", ConfigValueFactory.fromAnyRef(updated.toEpochMilli))
      .withValue("todo", ConfigValueFactory.fromIterable(todo.asJava))
      .withValue("finished", ConfigValueFactory.fromIterable(finished.asJava))
      .withValue("failed", ConfigValueFactory.fromIterable(failed.asJava))
      .withValue("invalid", ConfigValueFactory.fromIterable(invalid.asJava))
  }

  def render(checkpoint: Checkpoint): String = {
    apply(checkpoint).root.render(ConfigRenderOptions.defaults().setJson(false).setOriginComments(false))
  }
} 
Example 23
Source File: AkkaConfig.scala    From cave   with MIT License 5 votes vote down vote up
package init

import com.typesafe.config.{ConfigValueFactory, ConfigFactory}
import org.apache.commons.logging.LogFactory
import scala.collection.JavaConversions._

class AkkaConfig(hostname: Option[String], awsWrapper: AwsWrapper) {

  private final val Log = LogFactory.getLog(this.getClass)
  private final val AkkaPort = 2551
  private final val Localhost = "localhost"

  private val (host: String, siblings: List[String]) = hostname match {
    case Some(name) if name.length > 0 => (name, awsWrapper.getNodes("scheduler"))
    case _ => (Localhost, List(Localhost))
  }

  private val seeds = siblings map (ip => s"akka.tcp://scheduler@$ip:$AkkaPort")
  Log.warn(s"Seeds: ${seeds.mkString}")

  private val overrideConfig = ConfigFactory.empty()
    .withValue("akka.remote.netty.tcp.hostname", ConfigValueFactory.fromAnyRef(host))
    .withValue("akka.remote.netty.tcp.port", ConfigValueFactory.fromAnyRef(AkkaPort))
    .withValue("akka.cluster.seed-nodes", ConfigValueFactory.fromIterable(seeds))

  private val defaults = ConfigFactory.load()

  val config = overrideConfig withFallback defaults
} 
Example 24
Source File: BasicAuthenticationExtension.scala    From scruid   with Apache License 2.0 5 votes vote down vote up
package ing.wbaa.druid.auth.basic

import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.model.headers.{ Authorization, BasicHttpCredentials }
import com.typesafe.config.{ Config, ConfigFactory, ConfigValueFactory }
import com.typesafe.scalalogging.LazyLogging
import ing.wbaa.druid.client.{
  NoopRequestInterceptor,
  RequestInterceptor,
  RequestInterceptorBuilder
}


class BasicAuthenticationExtension(username: String, password: String)
    extends NoopRequestInterceptor {
  override def interceptRequest(request: HttpRequest): HttpRequest =
    request.withHeaders(Authorization(BasicHttpCredentials(username, password)))

  override def exportConfig: Config =
    ConfigFactory
      .empty()
      .withValue("username", ConfigValueFactory.fromAnyRef(username))
      .withValue("password", ConfigValueFactory.fromAnyRef(password))
}

object BasicAuthenticationExtension extends RequestInterceptorBuilder with LazyLogging {

  override def apply(config: Config): RequestInterceptor = {

    val username =
      Option(config.getString("username")).getOrElse {
        throw new IllegalStateException(
          "BasicAuthenticationExtension requires 'username' configuration parameter to be specified"
        )
      }

    val password =
      Option(config.getString("password")).getOrElse {
        throw new IllegalStateException(
          "BasicAuthenticationExtension requires 'password' configuration parameter to be specified"
        )
      }

    logger.info(s"BasicAuthenticationExtension[username=$username] created")
    new BasicAuthenticationExtension(username, password)
  }
} 
Example 25
Source File: HoconInputTest.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package hocon

import java.time.{Duration, Period}

import com.avsystem.commons.serialization.json.JsonStringOutput
import com.avsystem.commons.serialization.{GenCodecRoundtripTest, Input, Output}
import com.typesafe.config.{ConfigFactory, ConfigMemorySize, ConfigValue, ConfigValueFactory, ConfigValueType}

class HoconInputTest extends GenCodecRoundtripTest {
  type Raw = ConfigValue

  def writeToOutput(write: Output => Unit): ConfigValue = {
    val sb = new JStringBuilder
    write(new JsonStringOutput(sb))
    val config = ConfigFactory.parseString(s"""{"f":${sb.toString}}""")
    if (config.getIsNull("f")) ConfigValueFactory.fromAnyRef(null) else config.getValue("f")
  }

  def createInput(raw: ConfigValue): Input =
    new HoconInput(raw)

  def rawInput(any: Any): HoconInput =
    new HoconInput(ConfigValueFactory.fromAnyRef(any))

  test("value type reading") {
    assert(rawInput(null).valueType == ConfigValueType.NULL)
    assert(rawInput("kek").valueType == ConfigValueType.STRING)
    assert(rawInput(42).valueType == ConfigValueType.NUMBER)
    assert(rawInput(true).valueType == ConfigValueType.BOOLEAN)
    assert(rawInput(JMap()).valueType == ConfigValueType.OBJECT)
    assert(rawInput(JList()).valueType == ConfigValueType.LIST)
  }

  test("duration reading") {
    assert(rawInput("34s").readDuration() == Duration.ofSeconds(34))
  }

  test("period reading") {
    assert(rawInput("5m").readPeriod() == Period.ofMonths(5))
  }

  test("temporal amount reading") {
    assert(rawInput("5 minutes").readTemporal() == Duration.ofMinutes(5))
    assert(rawInput("5 months").readTemporal() == Period.ofMonths(5))
  }

  test("size in bytes reading") {
    assert(rawInput("100M").readSizeInBytes() == 100 * 1024 * 1024L)
  }

  test("memory size reading") {
    assert(rawInput("100M").readMemorySize() == ConfigMemorySize.ofBytes(100 * 1024 * 1024L))
  }

  test("number reading") {
    assert(rawInput(42.0).readNumber().doubleValue == 42.0)
  }
} 
Example 26
Source File: PostgresConnection.scala    From matcher   with MIT License 5 votes vote down vote up
package com.wavesplatform.dex.settings

import cats.syntax.apply._
import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
import com.wavesplatform.dex.settings.utils.ConfigSettingsValidator
import com.wavesplatform.dex.settings.utils.ConfigSettingsValidator._
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ValueReader

case class PostgresConnection(host: String, portNumber: Int, database: String, user: String, password: String, dataSourceClassName: String) {

  def getConfig: Config = {
    ConfigFactory
      .empty()
      .withValue("dataSource.serverName", ConfigValueFactory.fromAnyRef(host))
      .withValue("dataSource.portNumber", ConfigValueFactory.fromAnyRef(portNumber))
      .withValue("dataSource.databaseName", ConfigValueFactory.fromAnyRef(database))
      .withValue("dataSource.user", ConfigValueFactory.fromAnyRef(user))
      .withValue("dataSource.password", ConfigValueFactory.fromAnyRef(password))
      .withValue("dataSourceClassName", ConfigValueFactory.fromAnyRef(dataSourceClassName))
  }
}

object PostgresConnection {

  implicit val postgresConnectionReader: ValueReader[PostgresConnection] = { (cfg, path) =>
    val cfgValidator = ConfigSettingsValidator(cfg)

    (
      cfgValidator.validate[String](s"$path.server-name"),
      cfgValidator.validate[Int](s"$path.port-number"),
      cfgValidator.validate[String](s"$path.database"),
      cfgValidator.validate[String](s"$path.user"),
      cfgValidator.validate[String](s"$path.password"),
      cfgValidator.validate[String](s"$path.data-source-class-name")
    ) mapN PostgresConnection.apply getValueOrThrowErrors
  }
} 
Example 27
Source File: HBaseConnectorSuite.scala    From darwin   with Apache License 2.0 5 votes vote down vote up
package it.agilelab.darwin.connector.hbase

import java.nio.file.Files

import com.typesafe.config.{ConfigFactory, ConfigValueFactory}
import it.agilelab.darwin.common.Connector
import org.apache.avro.reflect.ReflectData
import org.apache.avro.{Schema, SchemaNormalization}
import org.apache.hadoop.hbase.HBaseTestingUtility
import org.scalatest.BeforeAndAfterAll
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class HBaseConnectorSuite extends AnyFlatSpec with Matchers with BeforeAndAfterAll {

  var connector: Connector = _

  "HBaseConnector" should "load all existing schemas" in {
    connector.fullLoad()
  }

  it should "insert and retrieve" in {
    val schemas = Seq(ReflectData.get().getSchema(classOf[HBaseMock]), ReflectData.get().getSchema(classOf[HBase2Mock]))
      .map(s => SchemaNormalization.parsingFingerprint64(s) -> s)
    connector.insert(schemas)
    val loaded: Seq[(Long, Schema)] = connector.fullLoad()
    assert(loaded.size == schemas.size)
    assert(loaded.forall(schemas.contains))
    val schema = connector.findSchema(loaded.head._1)
    assert(schema.isDefined)
    assert(schema.get == loaded.head._2)
    val noSchema = connector.findSchema(-1L)
    assert(noSchema.isEmpty)
  }

  "connector.tableCreationHint" should "print the correct hint for table creation" in {
    connector.tableCreationHint() should be(
      """To create namespace and table from an HBase shell issue:
        |  create_namespace 'AVRO'
        |  create 'AVRO:SCHEMA_REPOSITORY', '0'""".stripMargin)
  }

  "connector.tableExists" should "return true with existent table" in {
    connector.tableExists() should be(true)
  }

  override def beforeAll(): Unit = {

    connector = new HBaseConnectorCreator().create(HBaseConnectorSuite.config)

    connector.createTable()
  }


}

object HBaseConnectorSuite {
  private lazy val config = {
    val util = new HBaseTestingUtility()
    val minicluster = util.startMiniCluster()

    //Hbase connector can only load configurations from a file path so we need to render the hadoop conf
    val confFile = Files.createTempFile("prefix", "suffix")
    val stream = Files.newOutputStream(confFile)
    minicluster.getConfiguration.writeXml(stream)
    stream.flush()
    stream.close()
    val hbaseConfigPath = ConfigValueFactory.fromAnyRef(confFile.toAbsolutePath.toString)

    //HbaseConnector will only load conf if hbase-site and core-site are given,
    //we give the same file to each.
    sys.addShutdownHook(minicluster.shutdown())
    ConfigFactory.load()
      .withValue(ConfigurationKeys.HBASE_SITE, hbaseConfigPath)
      .withValue(ConfigurationKeys.CORE_SITE, hbaseConfigPath)
  }

} 
Example 28
Source File: Boot.scala    From mleap   with Apache License 2.0 5 votes vote down vote up
package ml.combust.mleap.serving

import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
import ml.combust.mleap.BuildInfo


object Boot extends App {
  val info = BuildInfo()

  val parser = new scopt.OptionParser[Config](info.name) {
    head(info.name, info.version)
    help("help").text("prints this usage text")

    opt[Int]( "grpc-port").text("specify grpc port number").action {
      (port, config) => config.withValue("ml.combust.mleap.grpc.server.port", ConfigValueFactory.fromAnyRef(port))
    }
  }

  parser.parse(args, ConfigFactory.load()) match {
    case Some(config) => new RunServer(config).run()
    case None =>
  }
} 
Example 29
Source File: Boot.scala    From mleap   with Apache License 2.0 5 votes vote down vote up
package ml.combust.mleap.grpc.server

import akka.actor.ActorSystem
import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
import ml.combust.mleap.BuildInfo

object Boot extends App {
  val info = BuildInfo()

  val parser = new scopt.OptionParser[Config](info.name) {
    head(info.name, info.version)
    help("help").text("prints this usage text")

    opt[Int]('p', "port").text("specify port number").action {
      (port, config) => config.withValue("port", ConfigValueFactory.fromAnyRef(port))
    }
  }

  parser.parse(args, ConfigFactory.load().getConfig("ml.combust.mleap.grpc.server")) match {
    case Some(config) =>
      implicit val system: ActorSystem = ActorSystem("Propel")
      new RunServer(config).run()
    case None =>
  }
} 
Example 30
Source File: UIService.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.experiments.yarn.appmaster

import akka.actor._
import com.typesafe.config.{ConfigFactory, ConfigValueFactory}
import org.apache.gearpump.cluster.ClusterConfig
import org.apache.gearpump.services.main.Services
import org.apache.gearpump.transport.HostPort
import org.apache.gearpump.util.{ActorUtil, Constants, LogUtil}

import scala.concurrent.Future

trait UIFactory {
  def props(masters: List[HostPort], host: String, port: Int): Props
}


class UIService(masters: List[HostPort], host: String, port: Int) extends Actor {
  private val LOG = LogUtil.getLogger(getClass)

  private val supervisor = ActorUtil.getFullPath(context.system, context.parent.path)
  private var configFile: java.io.File = null

  private implicit val dispatcher = context.dispatcher

  override def postStop(): Unit = {
    if (configFile != null) {
      configFile.delete()
      configFile = null
    }

    // TODO: fix this
    // Hack around to Kill the UI server
    Services.kill()
  }

  override def preStart(): Unit = {
    Future(start())
  }

  def start(): Unit = {
    val mastersArg = masters.mkString(",")
    LOG.info(s"Launching services -master $mastersArg")

    configFile = java.io.File.createTempFile("uiserver", ".conf")

    val config = context.system.settings.config.
      withValue(Constants.GEARPUMP_SERVICE_HOST, ConfigValueFactory.fromAnyRef(host)).
      withValue(Constants.GEARPUMP_SERVICE_HTTP, ConfigValueFactory.fromAnyRef(port.toString)).
      withValue(Constants.NETTY_TCP_HOSTNAME, ConfigValueFactory.fromAnyRef(host))

    ClusterConfig.saveConfig(config, configFile)

    val master = masters.head

    ConfigFactory.invalidateCaches()
    launch(supervisor, master.host, master.port, configFile.toString)
  }

  // Launch the UI server
  def launch(supervisor: String, masterHost: String, masterPort: Int, configFile: String): Unit = {
    Services.main(Array("-supervisor", supervisor, "-master", s"$masterHost:$masterPort"
      , "-conf", configFile))
  }

  override def receive: Receive = {
    case _ =>
      LOG.error(s"Unknown message received")
  }
}

object UIService extends UIFactory {
  override def props(masters: List[HostPort], host: String, port: Int): Props = {
    Props(new UIService(masters, host, port))
  }
} 
Example 31
Source File: TaskLocator.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.streaming.appmaster

import com.typesafe.config.{Config, ConfigFactory, ConfigRenderOptions, ConfigValueFactory}
import org.apache.gearpump.cluster.worker.WorkerId
import org.apache.gearpump.streaming.appmaster.TaskLocator.{Localities, Locality, NonLocality, WorkerLocality}
import org.apache.gearpump.streaming.task.TaskId

import scala.collection.JavaConverters._
import scala.util.Try


  case class Localities(localities: Map[WorkerId, Array[TaskId]])

  object Localities {
    val pattern = "task_([0-9]+)_([0-9]+)".r

    // To avoid polluting the classpath, we do the JSON translation ourself instead of
    // introducing JSON library dependencies directly.
    def fromJson(json: String): Localities = {
      val localities = ConfigFactory.parseString(json).getAnyRef("localities")
        .asInstanceOf[java.util.Map[String, String]].asScala.map { pair =>
        val workerId: WorkerId = WorkerId.parse(pair._1)
        val tasks = pair._2.split(",").map { task =>
          val pattern(processorId, taskIndex) = task
          TaskId(processorId.toInt, taskIndex.toInt)
        }
        (workerId, tasks)
      }.toMap
      new Localities(localities)
    }

    def toJson(localities: Localities): String = {
      val map = localities.localities.toList.map { pair =>
        (WorkerId.render(pair._1), pair._2.map(task =>
          s"task_${task.processorId}_${task.index}").mkString(","))
      }.toMap.asJava
      ConfigFactory.empty().withValue("localities", ConfigValueFactory.fromAnyRef(map)).
        root.render(ConfigRenderOptions.concise())
    }
  }
} 
Example 32
Source File: Local.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.cluster.main

import akka.actor.{ActorSystem, Props}
import com.typesafe.config.ConfigValueFactory
import org.apache.gearpump.cluster.ClusterConfig
import org.apache.gearpump.cluster.master.{Master => MasterActor}
import org.apache.gearpump.cluster.worker.{Worker => WorkerActor}
import org.apache.gearpump.util.Constants._
import org.apache.gearpump.util.LogUtil.ProcessType
import org.apache.gearpump.util.{ActorUtil, Constants, LogUtil, MasterClientCommand, Util}
import org.slf4j.Logger

import scala.collection.JavaConverters._
import scala.concurrent.Await
import scala.concurrent.duration.Duration

object Local extends MasterClientCommand with ArgumentsParser {
  override def akkaConfig: Config = ClusterConfig.master()

  var LOG: Logger = LogUtil.getLogger(getClass)

  override val options: Array[(String, CLIOption[Any])] =
    Array("sameprocess" -> CLIOption[Boolean]("", required = false, defaultValue = Some(false)),
      "workernum" -> CLIOption[Int]("<how many workers to start>", required = false,
        defaultValue = Some(2)))

  override val description = "Start a local cluster"

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

    this.LOG = {
      LogUtil.loadConfiguration(akkaConf, ProcessType.LOCAL)
      LogUtil.getLogger(getClass)
    }

    val config = parse(args)
    if (null != config) {
      local(config.getInt("workernum"), config.getBoolean("sameprocess"), akkaConf)
    }
  }

  def local(workerCount: Int, sameProcess: Boolean, akkaConf: Config): Unit = {
    if (sameProcess) {
      LOG.info("Starting local in same process")
      System.setProperty("LOCAL", "true")
    }
    val masters = akkaConf.getStringList(Constants.GEARPUMP_CLUSTER_MASTERS)
      .asScala.flatMap(Util.parseHostList)
    val local = akkaConf.getString(Constants.GEARPUMP_HOSTNAME)

    if (masters.size != 1 && masters.head.host != local) {
      LOG.error(s"The ${Constants.GEARPUMP_CLUSTER_MASTERS} is not match " +
        s"with ${Constants.GEARPUMP_HOSTNAME}")
    } else {

      val hostPort = masters.head
      implicit val system = ActorSystem(MASTER, akkaConf.
        withValue("akka.remote.netty.tcp.port", ConfigValueFactory.fromAnyRef(hostPort.port))
      )

      val master = system.actorOf(Props[MasterActor], MASTER)
      val masterPath = ActorUtil.getSystemAddress(system).toString + s"/user/$MASTER"

      0.until(workerCount).foreach { id =>
        system.actorOf(Props(classOf[WorkerActor], master), classOf[WorkerActor].getSimpleName + id)
      }

      Await.result(system.whenTerminated, Duration.Inf)
    }
  }
} 
Example 33
Source File: EmbeddedCluster.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.cluster.embedded

import scala.collection.JavaConverters._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import akka.actor.{ActorRef, ActorSystem, Props}
import com.typesafe.config.{Config, ConfigValueFactory}
import org.apache.gearpump.cluster.ClusterConfig
import org.apache.gearpump.cluster.master.{Master => MasterActor}
import org.apache.gearpump.cluster.worker.{Worker => WorkerActor}
import org.apache.gearpump.util.Constants.{GEARPUMP_CLUSTER_EXECUTOR_WORKER_SHARE_SAME_PROCESS, GEARPUMP_CLUSTER_MASTERS, GEARPUMP_METRIC_ENABLED, MASTER}
import org.apache.gearpump.util.{LogUtil, Util}


class EmbeddedCluster(inputConfig: Config) {
  private val LOG = LogUtil.getLogger(getClass)
  private val workerCount: Int = 1
  private val port = Util.findFreePort().get
  private[embedded] val config: Config = getConfig(inputConfig, port)
  private[embedded] val system: ActorSystem = ActorSystem(MASTER, config)
  private[embedded] val master: ActorRef = system.actorOf(Props[MasterActor], MASTER)

  0.until(workerCount).foreach { id =>
    system.actorOf(Props(classOf[WorkerActor], master), classOf[WorkerActor].getSimpleName + id)
  }

  LOG.info("=================================")
  LOG.info("Local Cluster is started at: ")
  LOG.info(s"                 127.0.0.1:$port")
  LOG.info(s"To see UI, run command: services -master 127.0.0.1:$port")

  private def getConfig(inputConfig: Config, port: Int): Config = {
    val config = inputConfig.
      withValue("akka.remote.netty.tcp.port", ConfigValueFactory.fromAnyRef(port)).
      withValue(GEARPUMP_CLUSTER_MASTERS,
        ConfigValueFactory.fromIterable(List(s"127.0.0.1:$port").asJava)).
      withValue(GEARPUMP_CLUSTER_EXECUTOR_WORKER_SHARE_SAME_PROCESS,
        ConfigValueFactory.fromAnyRef(true)).
      withValue(GEARPUMP_METRIC_ENABLED, ConfigValueFactory.fromAnyRef(true)).
      withValue("akka.actor.provider",
        ConfigValueFactory.fromAnyRef("akka.cluster.ClusterActorRefProvider"))
    config
  }

  def stop(): Unit = {
    system.stop(master)
    system.terminate()
    Await.result(system.whenTerminated, Duration.Inf)
  }
}

object EmbeddedCluster {
  def apply(): EmbeddedCluster = {
    new EmbeddedCluster(ClusterConfig.master())
  }
} 
Example 34
Source File: SerializerSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.serializer

import akka.actor.{ActorSystem, ExtendedActorSystem}

import com.esotericsoftware.kryo.io.{Input, Output}
import com.esotericsoftware.kryo.{Kryo, Serializer => KryoSerializer}
import com.typesafe.config.{ConfigFactory, ConfigValueFactory}

import org.apache.gearpump.cluster.TestUtil
import org.apache.gearpump.serializer.SerializerSpec._

import org.scalatest.mock.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}

import scala.collection.JavaConverters._
import scala.concurrent.Await
import scala.concurrent.duration.Duration


class SerializerSpec extends FlatSpec with Matchers with MockitoSugar {
  val config = ConfigFactory.empty.withValue("gearpump.serializers",
    ConfigValueFactory.fromAnyRef(Map(classOf[ClassA].getName -> classOf[ClassASerializer].getName,
      classOf[ClassB].getName -> classOf[ClassBSerializer].getName).asJava))

  "GearpumpSerialization" should "register custom serializers" in {
    val serialization = new GearpumpSerialization(config)
    val kryo = new Kryo
    serialization.customize(kryo)

    val forB = kryo.getRegistration(classOf[ClassB])
    assert(forB.getSerializer.isInstanceOf[ClassBSerializer])

    val forA = kryo.getRegistration(classOf[ClassA])
    assert(forA.getSerializer.isInstanceOf[ClassASerializer])
  }

  "FastKryoSerializer" should "serialize correctly" in {
    val myConfig = config.withFallback(TestUtil.DEFAULT_CONFIG.withoutPath("gearpump.serializers"))
    val system = ActorSystem("my", myConfig)

    val serializer = new FastKryoSerializer(system.asInstanceOf[ExtendedActorSystem])

    val bytes = serializer.serialize(new ClassA)
    val anotherA = serializer.deserialize(bytes)

    assert(anotherA.isInstanceOf[ClassA])
    system.terminate()
    Await.result(system.whenTerminated, Duration.Inf)
  }
}

object SerializerSpec {

  class ClassA {}

  class ClassASerializer extends KryoSerializer[ClassA] {
    override def write(kryo: Kryo, output: Output, `object`: ClassA): Unit = {
      output.writeString(classOf[ClassA].getName)
    }

    override def read(kryo: Kryo, input: Input, `type`: Class[ClassA]): ClassA = {
      val className = input.readString()
      Class.forName(className).newInstance().asInstanceOf[ClassA]
    }
  }

  class ClassB {}

  class ClassBSerializer extends KryoSerializer[ClassA] {
    override def write(kryo: Kryo, output: Output, `object`: ClassA): Unit = {}

    override def read(kryo: Kryo, input: Input, `type`: Class[ClassA]): ClassA = {
      null
    }
  }
} 
Example 35
Source File: ExecutorSystemLauncherSpec.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.cluster.appmaster

import org.apache.gearpump.cluster.worker.WorkerId

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

import akka.actor.{ActorSystem, Props}
import akka.testkit.TestProbe
import com.typesafe.config.ConfigValueFactory
import org.scalatest.{BeforeAndAfterAll, FlatSpec, Matchers}

import org.apache.gearpump.cluster.AppMasterToWorker.LaunchExecutor
import org.apache.gearpump.cluster.TestUtil
import org.apache.gearpump.cluster.WorkerToAppMaster.ExecutorLaunchRejected
import org.apache.gearpump.cluster.appmaster.ExecutorSystemLauncher._
import org.apache.gearpump.cluster.appmaster.ExecutorSystemScheduler.Session
import org.apache.gearpump.cluster.scheduler.Resource
import org.apache.gearpump.util.ActorSystemBooter.{ActorSystemRegistered, RegisterActorSystem}
import org.apache.gearpump.util.Constants

class ExecutorSystemLauncherSpec extends FlatSpec with Matchers with BeforeAndAfterAll {
  implicit var system: ActorSystem = null
  val workerId: WorkerId = WorkerId(0, 0L)
  val appId = 0
  val executorId = 0
  val url = "akka.tcp://[email protected]:3000"
  val session = Session(null, null)
  val launchExecutorSystemTimeout = 3000
  val activeConfig = TestUtil.DEFAULT_CONFIG.
    withValue(Constants.GEARPUMP_START_EXECUTOR_SYSTEM_TIMEOUT_MS,
      ConfigValueFactory.fromAnyRef(launchExecutorSystemTimeout))

  override def beforeAll(): Unit = {
    system = ActorSystem("test", activeConfig)
  }

  override def afterAll(): Unit = {
    system.terminate()
    Await.result(system.whenTerminated, Duration.Inf)
  }

  it should "report success when worker launch the system successfully" in {
    val worker = TestProbe()
    val client = TestProbe()

    val launcher = system.actorOf(Props(new ExecutorSystemLauncher(appId, session)))
    client.watch(launcher)
    client.send(launcher, LaunchExecutorSystem(WorkerInfo(workerId, worker.ref), 0, Resource(1)))

    worker.expectMsgType[LaunchExecutor]
    worker.reply(RegisterActorSystem(url))

    worker.expectMsgType[ActorSystemRegistered]

    client.expectMsgType[LaunchExecutorSystemSuccess]
    client.expectTerminated(launcher)
  }

  it should "report failure when worker refuse to launch the system explicitly" in {
    val worker = TestProbe()
    val client = TestProbe()

    val resource = Resource(4)

    val launcher = system.actorOf(Props(new ExecutorSystemLauncher(appId, session)))
    client.watch(launcher)
    client.send(launcher, LaunchExecutorSystem(WorkerInfo(workerId, worker.ref), 0, resource))

    worker.expectMsgType[LaunchExecutor]
    worker.reply(ExecutorLaunchRejected())

    client.expectMsg(LaunchExecutorSystemRejected(resource, null, session))
    client.expectTerminated(launcher)
  }

  it should "report timeout when trying to start a executor system on worker, " +
    "and worker doesn't response" in {
    val client = TestProbe()
    val worker = TestProbe()
    val launcher = system.actorOf(Props(new ExecutorSystemLauncher(appId, session)))
    client.send(launcher, LaunchExecutorSystem(WorkerInfo(workerId, worker.ref), 0, Resource(1)))
    client.watch(launcher)
    val waitFor = launchExecutorSystemTimeout + 10000
    client.expectMsgType[LaunchExecutorSystemTimeout](waitFor.milliseconds)
    client.expectTerminated(launcher, waitFor.milliseconds)
  }
} 
Example 36
Source File: MiniCluster.scala    From incubator-retired-gearpump   with Apache License 2.0 5 votes vote down vote up
package org.apache.gearpump.cluster

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.pattern.ask
import akka.testkit.TestActorRef
import com.typesafe.config.ConfigValueFactory
import org.apache.gearpump.cluster.AppMasterToMaster.GetAllWorkers
import org.apache.gearpump.cluster.MasterToAppMaster.WorkerList
import org.apache.gearpump.cluster.master.Master
import org.apache.gearpump.cluster.worker.Worker
import org.apache.gearpump.util.Constants

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

class MiniCluster {
  private val mockMasterIP = "127.0.0.1"

  implicit val system = ActorSystem("system", TestUtil.MASTER_CONFIG.
    withValue(Constants.NETTY_TCP_HOSTNAME, ConfigValueFactory.fromAnyRef(mockMasterIP)))

  val (mockMaster, worker) = {
    val master = system.actorOf(Props(classOf[Master]), "master")
    val worker = system.actorOf(Props(classOf[Worker], master), "worker")

    // Wait until worker register itself to master
    waitUtilWorkerIsRegistered(master)
    (master, worker)
  }

  def launchActor(props: Props): TestActorRef[Actor] = {
    TestActorRef(props)
  }

  private def waitUtilWorkerIsRegistered(master: ActorRef): Unit = {
    while (!isWorkerRegistered(master)) {}
  }

  private def isWorkerRegistered(master: ActorRef): Boolean = {
    import scala.concurrent.duration._
    implicit val dispatcher = system.dispatcher

    implicit val futureTimeout = Constants.FUTURE_TIMEOUT

    val workerListFuture = (master ? GetAllWorkers).asInstanceOf[Future[WorkerList]]

    // Waits until the worker is registered.
    val workers = Await.result[WorkerList](workerListFuture, 15.seconds)
    workers.workers.size > 0
  }

  def shutDown(): Unit = {
    system.terminate()
    Await.result(system.whenTerminated, Duration.Inf)
  }
} 
Example 37
Source File: AkkaTest.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.ledger.client.testing

import java.util
import java.util.concurrent.{Executors, ScheduledExecutorService}

import akka.NotUsed
import akka.actor.{ActorSystem, Scheduler}
import akka.stream.scaladsl.{Sink, Source}
import akka.stream.Materializer
import akka.util.ByteString
import com.daml.grpc.adapter.{ExecutionSequencerFactory, SingleThreadExecutionSequencerPool}
import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
import com.typesafe.scalalogging.LazyLogging
import org.scalatest.{BeforeAndAfterAll, Suite}

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContextExecutor, Future}
import scala.util.control.NonFatal

trait AkkaTest extends BeforeAndAfterAll with LazyLogging { self: Suite =>
  // TestEventListener is needed for log testing
  private val loggers =
    util.Arrays.asList("akka.event.slf4j.Slf4jLogger", "akka.testkit.TestEventListener")
  protected implicit val sysConfig: Config = ConfigFactory
    .load()
    .withValue("akka.loggers", ConfigValueFactory.fromIterable(loggers))
    .withValue("akka.logger-startup-timeout", ConfigValueFactory.fromAnyRef("30s"))
    .withValue("akka.stdout-loglevel", ConfigValueFactory.fromAnyRef("INFO"))
  protected implicit val system: ActorSystem = ActorSystem("test", sysConfig)
  protected implicit val ec: ExecutionContextExecutor =
    system.dispatchers.lookup("test-dispatcher")
  protected implicit val scheduler: Scheduler = system.scheduler
  protected implicit val schedulerService: ScheduledExecutorService =
    Executors.newSingleThreadScheduledExecutor()
  protected implicit val materializer: Materializer = Materializer(system)
  protected implicit val esf: ExecutionSequencerFactory =
    new SingleThreadExecutionSequencerPool("testSequencerPool")
  protected val timeout: FiniteDuration = 2.minutes
  protected val shortTimeout: FiniteDuration = 5.seconds

  protected def await[T](fun: => Future[T]): T = Await.result(fun, timeout)

  protected def awaitShort[T](fun: => Future[T]): T = Await.result(fun, shortTimeout)

  protected def drain(source: Source[ByteString, NotUsed]): ByteString = {
    val futureResult: Future[ByteString] = source.runFold(ByteString.empty) { (a, b) =>
      a.concat(b)
    }
    awaitShort(futureResult)
  }

  protected def drain[A, B](source: Source[A, B]): Seq[A] = {
    val futureResult: Future[Seq[A]] = source.runWith(Sink.seq)
    awaitShort(futureResult)
  }

  override protected def afterAll(): Unit = {
    try {
      val _ = await(system.terminate())
    } catch {
      case NonFatal(_) => ()
    }
    schedulerService.shutdownNow()
    super.afterAll()
  }
} 
Example 38
Source File: package.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.module.scalaz

import com.typesafe.config.{ ConfigValue, ConfigValueFactory }
import org.scalacheck.{ Arbitrary, Cogen, Gen }
import org.scalacheck.Arbitrary.{ arbitrary => arb }
import pureconfig.{ ConfigConvert, ConfigReader, ConfigWriter, Derivation }
import pureconfig.error._

import scala.collection.JavaConverters._
import scalaz.scalacheck.ScalaCheckBinding.GenMonad
import scalaz.syntax.applicative._

package object arbitrary {

  private[this] val MaxConfigDepth = 3
  private[this] val MaxCollectionLength = 3

  private[this] def genAny(depth: Int): Gen[Any] = {
    val genScalar: Gen[Any] =
      Gen.oneOf(
        Gen.oneOf(true, false),
        Gen.choose(Double.MinValue, Double.MaxValue),
        Gen.alphaStr)

    def genList(depth: Int): Gen[List[Any]] =
      Gen.choose(0, MaxCollectionLength).flatMap(n =>
        Gen.listOfN(n, Gen.lzy(genAny(depth - 1))))

    def genMap(depth: Int): Gen[Map[String, Any]] =
      Gen.choose(0, MaxCollectionLength).flatMap(n =>
        Gen.mapOfN(n, for { k <- Gen.alphaStr; v <- Gen.lzy(genAny(depth - 1)) } yield (k, v)))

    if (depth == 0) genScalar
    else Gen.frequency(
      3 -> genScalar,
      1 -> genList(depth).map(_.asJava),
      1 -> genMap(depth).map(_.asJava))
  }

  implicit val arbConfigValue: Arbitrary[ConfigValue] =
    Arbitrary(genAny(MaxConfigDepth).map(ConfigValueFactory.fromAnyRef))

  implicit val cogenConfigValue: Cogen[ConfigValue] =
    Cogen[String].contramap[ConfigValue](_.render)

  private[this] val genFailureReason: Gen[FailureReason] =
    Gen.oneOf(
      ^^(Gen.alphaStr, Gen.alphaStr, Gen.alphaStr)(CannotConvert.apply),
      ^(Gen.posNum[Int], Gen.posNum[Int])(WrongSizeString.apply),
      ^(Gen.posNum[Int], Gen.posNum[Int])(WrongSizeList.apply),
      Gen.alphaStr.map(k => KeyNotFound.apply(k)),
      Gen.alphaStr.map(UnknownKey.apply))

  implicit val arbConfigReaderFailures: Arbitrary[ConfigReaderFailures] =
    Arbitrary {
      for {
        n <- Gen.choose(1, MaxCollectionLength)
        l <- Gen.listOfN(n, genFailureReason).map(_.map(r => ConvertFailure(r, None, "")))
      } yield ConfigReaderFailures(l.head, l.tail: _*)
    }

  implicit val cogenConfigReaderFailures: Cogen[ConfigReaderFailures] =
    Cogen[String].contramap[ConfigReaderFailures](_.toString)

  implicit def arbConfigReader[A: Arbitrary]: Arbitrary[ConfigReader[A]] =
    Arbitrary(arb[ConfigValue => Either[ConfigReaderFailures, A]].map(ConfigReader.fromFunction))

  implicit def arbConfigWriter[A: Cogen]: Arbitrary[ConfigWriter[A]] =
    Arbitrary(arb[A => ConfigValue].map(ConfigWriter.fromFunction))

  implicit def arbConfigConvert[A: Arbitrary: Cogen]: Arbitrary[ConfigConvert[A]] =
    Arbitrary {
      for {
        reader <- arb[ConfigReader[A]]
        writer <- arb[ConfigWriter[A]]
      } yield ConfigConvert.fromReaderAndWriter(Derivation.Successful(reader), Derivation.Successful(writer))
    }
} 
Example 39
Source File: MagnoliaConfigWriter.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.module.magnolia

import scala.collection.JavaConverters._
import scala.reflect.ClassTag

import _root_.magnolia._
import com.typesafe.config.{ ConfigValue, ConfigValueFactory }
import pureconfig._
import pureconfig.generic.{ CoproductHint, ProductHint }


object MagnoliaConfigWriter {

  def combine[A](ctx: CaseClass[ConfigWriter, A])(implicit hint: ProductHint[A]): ConfigWriter[A] =
    if (ctx.typeName.full.startsWith("scala.Tuple")) combineTuple(ctx)
    else if (ctx.isValueClass) combineValueClass(ctx)
    else combineCaseClass(ctx)

  private def combineCaseClass[A](ctx: CaseClass[ConfigWriter, A])(implicit hint: ProductHint[A]): ConfigWriter[A] = new ConfigWriter[A] {
    def to(a: A): ConfigValue = {
      val fieldValues = ctx.parameters.map { param =>
        val valueOpt = param.typeclass match {
          case tc: WritesMissingKeys[param.PType @unchecked] =>
            tc.toOpt(param.dereference(a))
          case tc =>
            Some(tc.to(param.dereference(a)))
        }
        hint.to(valueOpt, param.label)
      }
      ConfigValueFactory.fromMap(fieldValues.flatten.toMap.asJava)
    }
  }

  private def combineTuple[A](ctx: CaseClass[ConfigWriter, A]): ConfigWriter[A] = new ConfigWriter[A] {
    override def to(a: A): ConfigValue = ConfigValueFactory.fromIterable(
      ctx.parameters.map { param => param.typeclass.to(param.dereference(a)) }.asJava)
  }

  private def combineValueClass[A](ctx: CaseClass[ConfigWriter, A]): ConfigWriter[A] = new ConfigWriter[A] {
    override def to(a: A): ConfigValue =
      ctx.parameters.map { param => param.typeclass.to(param.dereference(a)) }.head
  }

  def dispatch[A: ClassTag](ctx: SealedTrait[ConfigWriter, A])(implicit hint: CoproductHint[A]): ConfigWriter[A] = new ConfigWriter[A] {
    def to(a: A): ConfigValue = ctx.dispatch(a) { subtype =>
      hint.to(subtype.typeclass.to(subtype.cast(a)), subtype.typeName.short)
    }
  }
} 
Example 40
Source File: TupleConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.module.magnolia

import scala.collection.JavaConverters._
import scala.language.higherKinds

import com.typesafe.config.{ ConfigValueFactory, ConfigValueType }
import org.scalacheck.ScalacheckShapeless._
import pureconfig._
import pureconfig.error._
import pureconfig.module.magnolia.auto.reader._
import pureconfig.module.magnolia.auto.writer._

class TupleConvertersSuite extends BaseSuite {

  behavior of "ConfigConvert"

  // Check arbitrary Tuples
  checkArbitrary[Tuple1[Int]]
  checkArbitrary[(String, Int)]
  checkArbitrary[(Int, (Long, String), Boolean)]

  // Check arbitrary Tuples with custom types
  case class Foo(a: Int, b: String)
  checkArbitrary[(Long, Foo, Boolean, Foo)]

  // Check readers from objects and lists
  checkRead[(String, Int)](ConfigValueFactory.fromMap(Map("_1" -> "one", "_2" -> 2).asJava) -> (("one", 2)))
  checkRead[(String, Int)](ConfigValueFactory.fromMap(Map("0" -> "one", "1" -> 2).asJava) -> (("one", 2)))
  checkRead[(String, Int)](ConfigValueFactory.fromIterable(List("one", 2).asJava) -> (("one", 2)))

  // Check writers
  checkWrite[Tuple1[Int]](Tuple1(1) -> ConfigValueFactory.fromIterable(List(1).asJava))
  checkWrite[(String, Int)](("one", 2) -> ConfigValueFactory.fromIterable(List("one", 2).asJava))
  checkWrite[(Int, (Long, String), Boolean)]((1, (2l, "three"), false) -> ConfigValueFactory.fromIterable(List(1, List(2l, "three").asJava, false).asJava))

  // Check errors
  checkFailures[(String, Int)](
    ConfigValueFactory.fromAnyRef(Map("_1" -> "one", "_2" -> "two").asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)), emptyConfigOrigin, "_2")))
  checkFailures[(String, Int)](
    ConfigValueFactory.fromIterable(List("one", "two").asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)), emptyConfigOrigin, "1")))

  checkFailures[(Int, Int, Int)](
    ConfigValueFactory.fromIterable(List(1, "one").asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongSizeList(3, 2), emptyConfigOrigin, "")))

  checkFailures[(Int, Int, Int)](
    ConfigValueFactory.fromAnyRef(Map("_1" -> "one", "_2" -> 2).asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)), emptyConfigOrigin, "_1"),
      ConvertFailure(KeyNotFound("_3", Set()), emptyConfigOrigin, "")))

  checkFailures[(String, Int)](
    ConfigValueFactory.fromAnyRef("str") -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.LIST)), emptyConfigOrigin, "")))
} 
Example 41
Source File: CoproductConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.module.magnolia

import scala.language.higherKinds

import com.typesafe.config.{ ConfigFactory, ConfigObject, ConfigValueFactory }
import org.scalacheck.{ Arbitrary, Gen }
import pureconfig._
import pureconfig.error._
import pureconfig.module.magnolia.auto.reader._
import pureconfig.module.magnolia.auto.writer._

class CoproductConvertersSuite extends BaseSuite {

  behavior of "ConfigConvert"

  val genBirdConfig: Gen[BirdConfig] = Arbitrary.arbBool.arbitrary.map(BirdConfig.apply)
  val genCatConfig: Gen[CatConfig] = Arbitrary.arbInt.arbitrary.map(CatConfig.apply)
  val genDogConfig: Gen[DogConfig] = Arbitrary.arbInt.arbitrary.map(DogConfig.apply)
  val genAnimalConfig: Gen[AnimalConfig] = Gen.oneOf(genBirdConfig, genCatConfig, genDogConfig)
  implicit val arbAnimalConfig = Arbitrary(genAnimalConfig)

  checkArbitrary[AnimalConfig]

  it should "read disambiguation information on sealed families by default" in {
    val conf = ConfigFactory.parseString("{ type = dog-config, age = 2 }")
    ConfigConvert[AnimalConfig].from(conf.root()) shouldEqual Right(DogConfig(2))
  }

  it should "write disambiguation information on sealed families by default" in {
    val conf = ConfigConvert[AnimalConfig].to(DogConfig(2))
    conf shouldBe a[ConfigObject]
    conf.asInstanceOf[ConfigObject].get("type") shouldEqual ConfigValueFactory.fromAnyRef("dog-config")
  }

  it should "return a proper ConfigReaderFailure if the hint field in a coproduct is missing" in {
    val conf = ConfigFactory.parseString("{ can-fly = true }")
    ConfigConvert[AnimalConfig].from(conf.root()) should failWithType[KeyNotFound]
  }

  it should "return a proper ConfigReaderFailure when a coproduct config is missing" in {
    case class AnimalCage(animal: AnimalConfig)
    ConfigConvert[AnimalCage].from(ConfigFactory.empty().root()) should failWithType[KeyNotFound]
  }
} 
Example 42
Source File: EnumerationConfigWriterBuilder.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.generic

import com.typesafe.config.{ ConfigValue, ConfigValueFactory }
import pureconfig.ConfigWriter
import shapeless._
import shapeless.labelled._


trait EnumerationConfigWriterBuilder[A] {
  def build(transformName: String => String): ConfigWriter[A]
}

object EnumerationConfigWriterBuilder {
  implicit val deriveEnumerationWriterBuilderCNil: EnumerationConfigWriterBuilder[CNil] =
    new EnumerationConfigWriterBuilder[CNil] {
      def build(transformName: String => String): ConfigWriter[CNil] =
        new ConfigWriter[CNil] {
          def to(a: CNil): ConfigValue =
            throw new IllegalStateException("Cannot encode CNil. This is likely a bug in PureConfig.")
        }
    }

  implicit def deriveEnumerationWriterBuilderCCons[K <: Symbol, H, T <: Coproduct](
    implicit
    vName: Witness.Aux[K],
    hGen: LabelledGeneric.Aux[H, HNil],
    tWriterBuilder: EnumerationConfigWriterBuilder[T]): EnumerationConfigWriterBuilder[FieldType[K, H] :+: T] =
    new EnumerationConfigWriterBuilder[FieldType[K, H] :+: T] {
      def build(transformName: String => String): ConfigWriter[FieldType[K, H] :+: T] = {
        lazy val tWriter = tWriterBuilder.build(transformName)
        new ConfigWriter[FieldType[K, H] :+: T] {
          def to(a: FieldType[K, H] :+: T): ConfigValue = a match {
            case Inl(_) => ConfigValueFactory.fromAnyRef(transformName(vName.value.name))
            case Inr(r) => tWriter.to(r)
          }
        }
      }
    }

  implicit def deriveEnumerationWriterBuilder[A, Repr <: Coproduct](
    implicit
    gen: LabelledGeneric.Aux[A, Repr],
    reprWriterBuilder: EnumerationConfigWriterBuilder[Repr]): EnumerationConfigWriterBuilder[A] =
    new EnumerationConfigWriterBuilder[A] {
      def build(transformName: String => String): ConfigWriter[A] = {
        reprWriterBuilder.build(transformName).contramap(gen.to)
      }
    }
} 
Example 43
Source File: SeqShapedWriter.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.generic

import scala.collection.JavaConverters._

import com.typesafe.config.{ ConfigList, ConfigValue, ConfigValueFactory }
import pureconfig.{ ConfigWriter, Derivation }
import shapeless._


private[generic] trait SeqShapedWriter[Repr] extends ConfigWriter[Repr]

object SeqShapedWriter {

  implicit val hNilWriter: SeqShapedWriter[HNil] = new SeqShapedWriter[HNil] {
    override def to(v: HNil): ConfigValue = ConfigValueFactory.fromIterable(List().asJava)
  }

  implicit def hConsWriter[H, T <: HList](implicit hw: Derivation[Lazy[ConfigWriter[H]]], tw: Lazy[SeqShapedWriter[T]]): SeqShapedWriter[H :: T] =
    new SeqShapedWriter[H :: T] {
      override def to(v: H :: T): ConfigValue = {
        tw.value.to(v.tail) match {
          case cl: ConfigList =>
            ConfigValueFactory.fromIterable((hw.value.value.to(v.head) +: cl.asScala).asJava)
          case other =>
            throw new Exception(s"Unexpected value $other when trying to write a `ConfigValue` from an `HList`.")
        }
      }
    }
} 
Example 44
Source File: package.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.module.cats

import scala.collection.JavaConverters._

import com.typesafe.config.{ Config, ConfigValue, ConfigValueFactory }
import org.scalacheck.Arbitrary.{ arbitrary => arb }
import org.scalacheck.{ Arbitrary, Cogen, Gen }
import pureconfig._
import pureconfig.error.{ ConfigReaderFailures, ConvertFailure }

package object arbitrary {

  private[this] val MaxConfigDepth = 3
  private[this] val MaxCollSize = 3

  private[this] def genAnyMapForConfig(maxDepth: Int): Gen[Map[String, Any]] = {
    Gen.choose(0, MaxCollSize).flatMap { sz =>
      Gen.mapOfN(sz, for { k <- Gen.alphaStr; v <- Gen.lzy(genAnyForConfig(maxDepth - 1)) } yield (k, v))
    }
  }

  private[this] def genAnyForConfig(maxDepth: Int): Gen[Any] = {
    val genScalar: Gen[Any] = Gen.oneOf(
      Gen.oneOf(true, false),
      Gen.choose(Int.MinValue, Int.MaxValue),
      Gen.choose(Double.MinValue, Double.MaxValue),
      Gen.alphaStr)

    def genList(maxDepth: Int): Gen[List[Any]] = Gen.choose(0, MaxCollSize).flatMap { sz =>
      Gen.listOfN(sz, Gen.lzy(genAnyForConfig(maxDepth - 1)))
    }

    if (maxDepth == 0) genScalar
    else Gen.frequency(
      3 -> genScalar,
      1 -> genList(maxDepth).map(_.asJava),
      1 -> genAnyMapForConfig(maxDepth).map(_.asJava))
  }

  implicit val arbConfigValue: Arbitrary[ConfigValue] = Arbitrary {
    genAnyForConfig(MaxConfigDepth).map(ConfigValueFactory.fromAnyRef)
  }

  implicit val arbConfig: Arbitrary[Config] = Arbitrary {
    genAnyMapForConfig(MaxConfigDepth).map(_.asJava).map(ConfigValueFactory.fromMap(_).toConfig)
  }

  implicit val cogenConfigValue: Cogen[ConfigValue] =
    Cogen[String].contramap[ConfigValue](_.render)

  implicit val arbConfigReaderFailures: Arbitrary[ConfigReaderFailures] = Arbitrary {
    Gen.const(ConfigReaderFailures(ConvertFailure(EmptyTraversableFound("List"), None, "")))
  }

  implicit val cogenConfigReaderFailures: Cogen[ConfigReaderFailures] =
    Cogen[String].contramap[ConfigReaderFailures](_.toString)

  implicit val arbConfigObjectSource: Arbitrary[ConfigObjectSource] = Arbitrary {
    Gen.either(arb[ConfigReaderFailures], arb[Config]).map(ConfigObjectSource(_))
  }

  implicit def arbConfigReader[A: Arbitrary]: Arbitrary[ConfigReader[A]] = Arbitrary {
    arb[ConfigValue => ConfigReader.Result[A]].map(ConfigReader.fromFunction)
  }

  implicit def arbConfigWriter[A: Cogen]: Arbitrary[ConfigWriter[A]] = Arbitrary {
    arb[A => ConfigValue].map(ConfigWriter.fromFunction)
  }

  implicit def arbConfigConvert[A: Arbitrary: Cogen]: Arbitrary[ConfigConvert[A]] = Arbitrary {
    for { reader <- arb[ConfigReader[A]]; writer <- arb[ConfigWriter[A]] }
      yield ConfigConvert.fromReaderAndWriter(Derivation.Successful(reader), Derivation.Successful(writer))
  }
} 
Example 45
Source File: CollectionConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import scala.collection.JavaConverters._
import scala.collection.immutable.{ HashSet, ListSet, Queue, TreeSet }

import com.typesafe.config.{ ConfigFactory, ConfigValueFactory, ConfigValueType }
import pureconfig.error.{ ConfigReaderFailures, ConvertFailure, WrongType }

class CollectionConvertersSuite extends BaseSuite {
  implicit override val generatorDrivenConfig = PropertyCheckConfiguration(minSuccessful = 100)

  behavior of "ConfigConvert"

  checkArbitrary[HashSet[String]]

  checkArbitrary[List[Float]]
  checkRead[List[Int]](
    // order of keys maintained
    ConfigValueFactory.fromMap(Map("2" -> 1, "0" -> 2, "1" -> 3).asJava) -> List(2, 3, 1),
    ConfigValueFactory.fromMap(Map("3" -> 2, "1" -> 4).asJava) -> List(4, 2),
    ConfigValueFactory.fromMap(Map("1" -> 1, "a" -> 2).asJava) -> List(1))

  checkFailures[List[Int]](
    ConfigValueFactory.fromMap(Map("b" -> 1, "a" -> 2).asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.OBJECT, Set(ConfigValueType.LIST)), emptyConfigOrigin, "")),
    ConfigValueFactory.fromMap(Map().asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.OBJECT, Set(ConfigValueType.LIST)), emptyConfigOrigin, "")))

  checkArbitrary[ListSet[Int]]

  checkArbitrary[Map[String, Int]]
  checkFailures[Map[String, Int]](
    // nested map should fail
    ConfigFactory.parseString("conf.a=1").root() -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.OBJECT, Set(ConfigValueType.NUMBER)), stringConfigOrigin(1), "conf")),
    // wrong value type should fail
    ConfigFactory.parseString("{ a=b }").root() -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)), stringConfigOrigin(1), "a")))

  checkArbitrary[Queue[Boolean]]

  checkArbitrary[Set[Double]]
  checkRead[Set[Int]](
    ConfigValueFactory.fromMap(Map("1" -> 4, "2" -> 5, "3" -> 6).asJava) -> Set(4, 5, 6))

  checkArbitrary[Stream[String]]

  checkArbitrary[TreeSet[Int]]

  checkArbitrary[Vector[Short]]

  checkArbitrary[Option[Int]]

  checkArbitrary[Array[Int]]
} 
Example 46
Source File: HListConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import scala.collection.JavaConverters._

import com.typesafe.config.ConfigValueFactory
import org.scalacheck.ScalacheckShapeless._
import pureconfig.generic.auto._
import pureconfig.generic.hlist._
import shapeless._

class HListConvertersSuite extends BaseSuite {

  behavior of "ConfigConvert"

  // Check arbitrary HLists
  checkArbitrary[Int :: HNil]
  checkArbitrary[String :: Int :: HNil]
  checkArbitrary[Int :: (Long :: String :: HNil) :: Boolean :: HNil]

  // Check arbitrary HList with custom types
  case class Foo(a: Int, b: String)
  checkArbitrary[Long :: Foo :: Boolean :: Foo :: HNil]

  // Check HNil
  val emptyConfigList = ConfigValueFactory.fromIterable(List().asJava)
  checkRead[HNil](emptyConfigList -> HNil)
  checkWrite[HNil](HNil -> emptyConfigList)
} 
Example 47
Source File: EnumerationsSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import com.typesafe.config.{ ConfigFactory, ConfigValueFactory, ConfigValueType }
import pureconfig.error.WrongType
import pureconfig.generic.error.NoValidCoproductOptionFound
import pureconfig.generic.semiauto._
import shapeless.test.illTyped

class EnumerationsSuite extends BaseSuite {

  sealed trait Color
  case object RainyBlue extends Color
  case object SunnyYellow extends Color

  val conf = ConfigFactory.parseString("{ type: person, name: John, surname: Doe }")

  behavior of "deriveEnumeration"

  it should "provide methods to derive readers for enumerations encoded as sealed traits" in {
    implicit val colorReader = deriveEnumerationReader[Color]

    ConfigReader[Color].from(ConfigValueFactory.fromAnyRef("rainy-blue")) shouldBe Right(RainyBlue)
    ConfigReader[Color].from(ConfigValueFactory.fromAnyRef("sunny-yellow")) shouldBe Right(SunnyYellow)

    val unknownValue = ConfigValueFactory.fromAnyRef("blue")
    ConfigReader[Color].from(unknownValue) should failWith(NoValidCoproductOptionFound(unknownValue, Seq.empty), "", emptyConfigOrigin)
    ConfigReader[Color].from(conf.root()) should failWith(WrongType(ConfigValueType.OBJECT, Set(ConfigValueType.STRING)), "", stringConfigOrigin(1))
  }

  it should "provide methods to derive writers for enumerations encoded as sealed traits" in {
    implicit val colorWriter = deriveEnumerationWriter[Color]

    ConfigWriter[Color].to(RainyBlue) shouldEqual ConfigValueFactory.fromAnyRef("rainy-blue")
    ConfigWriter[Color].to(SunnyYellow) shouldEqual ConfigValueFactory.fromAnyRef("sunny-yellow")
  }

  it should "provide methods to derive full converters for enumerations encoded as sealed traits" in {
    implicit val colorConvert = deriveEnumerationConvert[Color]

    ConfigConvert[Color].from(ConfigValueFactory.fromAnyRef("rainy-blue")) shouldBe Right(RainyBlue)
    ConfigConvert[Color].from(ConfigValueFactory.fromAnyRef("sunny-yellow")) shouldBe Right(SunnyYellow)
    ConfigConvert[Color].to(RainyBlue) shouldEqual ConfigValueFactory.fromAnyRef("rainy-blue")
    ConfigConvert[Color].to(SunnyYellow) shouldEqual ConfigValueFactory.fromAnyRef("sunny-yellow")
  }

  it should "provide customizable methods to derive readers for enumerations encoded as sealed traits" in {
    implicit val colorReader = deriveEnumerationReader[Color](ConfigFieldMapping(PascalCase, SnakeCase))

    ConfigReader[Color].from(ConfigValueFactory.fromAnyRef("rainy_blue")) shouldBe Right(RainyBlue)
    ConfigReader[Color].from(ConfigValueFactory.fromAnyRef("sunny_yellow")) shouldBe Right(SunnyYellow)
  }

  it should "provide customizable methods to derive writers for enumerations encoded as sealed traits" in {
    implicit val colorWriter = deriveEnumerationWriter[Color](ConfigFieldMapping(PascalCase, SnakeCase))

    ConfigWriter[Color].to(RainyBlue) shouldEqual ConfigValueFactory.fromAnyRef("rainy_blue")
    ConfigWriter[Color].to(SunnyYellow) shouldEqual ConfigValueFactory.fromAnyRef("sunny_yellow")
  }

  it should "provide customizable methods to derive full converters for enumerations encoded as sealed traits" in {
    implicit val colorConvert = deriveEnumerationConvert[Color](ConfigFieldMapping(PascalCase, SnakeCase))

    ConfigConvert[Color].from(ConfigValueFactory.fromAnyRef("rainy_blue")) shouldBe Right(RainyBlue)
    ConfigConvert[Color].from(ConfigValueFactory.fromAnyRef("sunny_yellow")) shouldBe Right(SunnyYellow)
    ConfigConvert[Color].to(RainyBlue) shouldEqual ConfigValueFactory.fromAnyRef("rainy_blue")
    ConfigConvert[Color].to(SunnyYellow) shouldEqual ConfigValueFactory.fromAnyRef("sunny_yellow")
  }

  it should "not allow deriving readers, writers and full converters for enumerations encoded as sealed traits whose subclasses are not all case objects" in {

    illTyped("deriveEnumerationReader[Entity]")
    illTyped("deriveEnumerationWriter[Entity]")
    illTyped("deriveEnumerationConvert[Entity]")
  }
} 
Example 48
Source File: TupleConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import scala.collection.JavaConverters._

import com.typesafe.config.{ ConfigValueFactory, ConfigValueType }
import org.scalacheck.ScalacheckShapeless._
import pureconfig.error._
import pureconfig.generic.auto._

class TupleConvertersSuite extends BaseSuite {

  behavior of "ConfigConvert"

  // Check arbitrary Tuples
  checkArbitrary[Tuple1[Int]]
  checkArbitrary[(String, Int)]
  checkArbitrary[(Int, (Long, String), Boolean)]

  // Check arbitrary Tuples with custom types
  case class Foo(a: Int, b: String)
  checkArbitrary[(Long, Foo, Boolean, Foo)]

  // Check readers from objects and lists
  checkRead[(String, Int)](ConfigValueFactory.fromMap(Map("_1" -> "one", "_2" -> 2).asJava) -> (("one", 2)))
  checkRead[(String, Int)](ConfigValueFactory.fromMap(Map("0" -> "one", "1" -> 2).asJava) -> (("one", 2)))
  checkRead[(String, Int)](ConfigValueFactory.fromIterable(List("one", 2).asJava) -> (("one", 2)))

  // Check writers
  checkWrite[Tuple1[Int]](Tuple1(1) -> ConfigValueFactory.fromIterable(List(1).asJava))
  checkWrite[(String, Int)](("one", 2) -> ConfigValueFactory.fromIterable(List("one", 2).asJava))
  checkWrite[(Int, (Long, String), Boolean)]((1, (2l, "three"), false) -> ConfigValueFactory.fromIterable(List(1, List(2l, "three").asJava, false).asJava))

  // Check errors
  checkFailures[(String, Int)](
    ConfigValueFactory.fromAnyRef(Map("_1" -> "one", "_2" -> "two").asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)), emptyConfigOrigin, "_2")))
  checkFailures[(String, Int)](
    ConfigValueFactory.fromIterable(List("one", "two").asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)), emptyConfigOrigin, "1")))

  checkFailures[(Int, Int, Int)](
    ConfigValueFactory.fromIterable(List(1, "one").asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongSizeList(3, 2), emptyConfigOrigin, "")))

  checkFailures[(Int, Int, Int)](
    ConfigValueFactory.fromAnyRef(Map("_1" -> "one", "_2" -> 2).asJava) -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.NUMBER)), emptyConfigOrigin, "_1"),
      ConvertFailure(KeyNotFound("_3", Set()), emptyConfigOrigin, "")))

  checkFailures[(String, Int)](
    ConfigValueFactory.fromAnyRef("str") -> ConfigReaderFailures(
      ConvertFailure(WrongType(ConfigValueType.STRING, Set(ConfigValueType.LIST)), emptyConfigOrigin, "")))
}