org.scalatest.OptionValues Scala Examples
The following examples show how to use org.scalatest.OptionValues.
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: CreatableTests.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.operation import cats.Applicative import cats.implicits._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.Utils.retry import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.http4s.Status import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import org.scalatest.{Assertion, OptionValues} trait CreatableTests[F[_], Resource <: { def metadata: Option[ObjectMeta] }] extends AnyFlatSpec with Matchers with OptionValues with MinikubeClientProvider[F] { def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[F]): Creatable[F, Resource] def getChecked(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]): F[Resource] def sampleResource(resourceName: String, labels: Map[String, String] = Map.empty): Resource def modifyResource(resource: Resource): Resource def checkUpdated(updatedResource: Resource): Assertion def createChecked(namespaceName: String, resourceName: String)( implicit client: KubernetesClient[F] ): F[Resource] = createChecked(namespaceName, resourceName, Map.empty) def createChecked(namespaceName: String, resourceName: String, labels: Map[String, String])( implicit client: KubernetesClient[F] ): F[Resource] = { val resource = sampleResource(resourceName, labels) for { status <- namespacedApi(namespaceName).create(resource) _ = status shouldBe Status.Created resource <- getChecked(namespaceName, resourceName) } yield resource } "create" should s"create a $resourceName" in usingMinikube { implicit client => createChecked(resourceName.toLowerCase, "create-resource") } "createOrUpdate" should s"create a $resourceName" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) resourceName = "create-update-resource" status <- namespacedApi(namespaceName).createOrUpdate(sampleResource(resourceName)) _ = status shouldBe Status.Created _ <- getChecked(namespaceName, resourceName) } yield () } def createOrUpdate(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]) = for { resource <- getChecked(namespaceName, resourceName) status <- namespacedApi(namespaceName).createOrUpdate(modifyResource(resource)) _ = status shouldBe Status.Ok } yield () it should s"update a $resourceName already created" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) resourceName <- Applicative[F].pure("update-resource") _ <- createChecked(namespaceName, resourceName) _ <- retry(createOrUpdate(namespaceName, resourceName)) updatedResource <- getChecked(namespaceName, resourceName) _ = checkUpdated(updatedResource) } yield () } }
Example 2
Source File: MeanValueImputerTest.scala From doddle-model with Apache License 2.0 | 5 votes |
package io.picnicml.doddlemodel.impute import breeze.linalg.{DenseMatrix, DenseVector} import io.picnicml.doddlemodel.TestingUtils import io.picnicml.doddlemodel.data.Feature.{CategoricalFeature, FeatureIndex, NumericalFeature} import io.picnicml.doddlemodel.impute.MeanValueImputer.ev import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class MeanValueImputerTest extends AnyFlatSpec with Matchers with TestingUtils with OptionValues { "Mean value imputer" should "impute the numerical features" in { val xMissing = DenseMatrix( List(Float.NaN, 1.0f, 2.0f), List(3.0f, Float.NaN, 5.0f), List(6.0f, 7.0f, 8.0f) ) val xImputedExpected = DenseMatrix( List(4.5f, 1.0f, 2.0f), List(3.0f, Float.NaN, 5.0f), List(6.0f, 7.0f, 8.0f) ) val imputer = MeanValueImputer(FeatureIndex.apply(List(NumericalFeature, CategoricalFeature, NumericalFeature))) val fittedImputer = ev.fit(imputer, xMissing) breezeEqual(fittedImputer.means.value, DenseVector(4.5f, 5.0f)) shouldBe true breezeEqual(ev.transform(fittedImputer, xMissing), xImputedExpected) shouldBe true } it should "impute a subset of numerical features" in { val xMissing = DenseMatrix( List(Float.NaN, 1.0f, 2.0f), List(3.0f, Float.NaN, 5.0f), List(6.0f, 7.0f, 8.0f) ) val xImputedExpected = DenseMatrix( List(4.5f, 1.0f, 2.0f), List(3.0f, Float.NaN, 5.0f), List(6.0f, 7.0f, 8.0f) ) val imputer = MeanValueImputer(FeatureIndex.numerical(List(0, 2))) val fittedImputer = ev.fit(imputer, xMissing) breezeEqual(fittedImputer.means.value, DenseVector(4.5f, 5.0f)) shouldBe true breezeEqual(ev.transform(fittedImputer, xMissing), xImputedExpected) shouldBe true } }
Example 3
Source File: StatefulSetsApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect.{ConcurrentEffect, IO} import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.operation._ import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.apps.v1._ import io.k8s.api.core.v1._ import io.k8s.apimachinery.pkg.apis.meta.v1.{LabelSelector, ObjectMeta} import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class StatefulSetsApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, StatefulSet] with GettableTests[IO, StatefulSet] with ListableTests[IO, StatefulSet, StatefulSetList] with ReplaceableTests[IO, StatefulSet] with DeletableTests[IO, StatefulSet, StatefulSetList] with DeletableTerminatedTests[IO, StatefulSet, StatefulSetList] with WatchableTests[IO, StatefulSet] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[StatefulSet].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.statefulSets override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.statefulSets.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = { val label = Option(Map("app" -> "test")) StatefulSet( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), spec = Option( StatefulSetSpec( serviceName = "service-name", selector = LabelSelector(matchLabels = label), template = PodTemplateSpec( metadata = Option(ObjectMeta(name = Option(resourceName), labels = label)), spec = Option(PodSpec(containers = Seq(Container("test", image = Option("docker"))))) ) ) ) ) } val updateStrategy = Option( StatefulSetUpdateStrategy( `type` = Option("RollingUpdate"), rollingUpdate = Option(RollingUpdateStatefulSetStrategy(partition = Option(10))) ) ) override def modifyResource(resource: StatefulSet) = resource.copy( metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name))), spec = resource.spec.map(_.copy(updateStrategy = updateStrategy)) ) override def checkUpdated(updatedResource: StatefulSet) = updatedResource.spec.value.updateStrategy shouldBe updateStrategy override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.statefulSets.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, StatefulSet] = client.statefulSets.namespace(namespaceName) }
Example 4
Source File: ReplicaSetsApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect.{ConcurrentEffect, IO} import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.operation._ import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.apps.v1._ import io.k8s.api.core.v1._ import io.k8s.apimachinery.pkg.apis.meta.v1.{LabelSelector, ObjectMeta} import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class ReplicaSetsApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, ReplicaSet] with GettableTests[IO, ReplicaSet] with ListableTests[IO, ReplicaSet, ReplicaSetList] with ReplaceableTests[IO, ReplicaSet] with DeletableTests[IO, ReplicaSet, ReplicaSetList] with DeletableTerminatedTests[IO, ReplicaSet, ReplicaSetList] with WatchableTests[IO, ReplicaSet] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[ReplicaSet].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.replicaSets override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.replicaSets.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = { val label = Option(Map("app" -> "test")) ReplicaSet( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), spec = Option( ReplicaSetSpec( selector = LabelSelector(matchLabels = label), template = Option( PodTemplateSpec( metadata = Option(ObjectMeta(name = Option(resourceName), labels = label)), spec = Option(PodSpec(containers = Seq(Container("test", image = Option("docker"))))) ) ) ) ) ) } val replicas = Option(5) override def modifyResource(resource: ReplicaSet) = resource.copy( metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name))), spec = resource.spec.map(_.copy(replicas = replicas)) ) override def checkUpdated(updatedResource: ReplicaSet) = updatedResource.spec.value.replicas shouldBe replicas override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.replicaSets.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, ReplicaSet] = client.replicaSets.namespace(namespaceName) }
Example 5
Source File: ConfigMapsApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.operation._ import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.core.v1.{ConfigMap, ConfigMapList} import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class ConfigMapsApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, ConfigMap] with GettableTests[IO, ConfigMap] with ListableTests[IO, ConfigMap, ConfigMapList] with ReplaceableTests[IO, ConfigMap] with DeletableTests[IO, ConfigMap, ConfigMapList] with WatchableTests[IO, ConfigMap] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[ConfigMap].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.configMaps override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.configMaps.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = ConfigMap( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), data = Option(Map("test" -> "data")) ) val data = Option(Map("test" -> "updated-data")) override def modifyResource(resource: ConfigMap) = resource.copy(metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name))), data = data) override def checkUpdated(updatedResource: ConfigMap) = updatedResource.data shouldBe data override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.configMaps.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, ConfigMap] = client.configMaps.namespace(namespaceName) }
Example 6
Source File: CronJobsApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect.{ConcurrentEffect, IO} import com.goyeau.kubernetes.client.operation._ import com.goyeau.kubernetes.client.KubernetesClient import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.batch.v1.JobSpec import io.k8s.api.batch.v1beta1.{CronJob, CronJobList, CronJobSpec, JobTemplateSpec} import io.k8s.api.core.v1._ import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class CronJobsApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, CronJob] with GettableTests[IO, CronJob] with ListableTests[IO, CronJob, CronJobList] with ReplaceableTests[IO, CronJob] with DeletableTests[IO, CronJob, CronJobList] with DeletableTerminatedTests[IO, CronJob, CronJobList] with WatchableTests[IO, CronJob] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[CronJob].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.cronJobs override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.cronJobs.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = CronJob( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), spec = Option( CronJobSpec( schedule = "1 * * * *", jobTemplate = JobTemplateSpec( spec = Option( JobSpec( template = PodTemplateSpec( metadata = Option(ObjectMeta(name = Option(resourceName))), spec = Option( PodSpec( containers = Seq(Container("test", image = Option("docker"))), restartPolicy = Option("Never") ) ) ) ) ) ) ) ) ) val schedule = "2 * * * *" override def modifyResource(resource: CronJob) = resource.copy( metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name))), spec = resource.spec.map(_.copy(schedule = schedule)) ) override def checkUpdated(updatedResource: CronJob) = updatedResource.spec.value.schedule shouldBe schedule override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.cronJobs.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, CronJob] = client.cronJobs.namespace(namespaceName) }
Example 7
Source File: ServiceAccountsApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.operation._ import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.core.v1._ import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class ServiceAccountsApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, ServiceAccount] with GettableTests[IO, ServiceAccount] with ListableTests[IO, ServiceAccount, ServiceAccountList] with ReplaceableTests[IO, ServiceAccount] with DeletableTests[IO, ServiceAccount, ServiceAccountList] with WatchableTests[IO, ServiceAccount] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[ServiceAccount].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.serviceAccounts override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.serviceAccounts.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = ServiceAccount( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))) ) val labels = Option(Map("test" -> "updated-label")) override def modifyResource(resource: ServiceAccount) = resource.copy(metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name), labels = labels))) override def checkUpdated(updatedResource: ServiceAccount) = updatedResource.metadata.value.labels shouldBe labels override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.serviceAccounts.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, ServiceAccount] = client.serviceAccounts.namespace(namespaceName) }
Example 8
Source File: HorizontalPodAutoscalersApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.operation._ import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.autoscaling.v1.{ CrossVersionObjectReference, HorizontalPodAutoscaler, HorizontalPodAutoscalerList, HorizontalPodAutoscalerSpec } import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class HorizontalPodAutoscalersApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, HorizontalPodAutoscaler] with GettableTests[IO, HorizontalPodAutoscaler] with ListableTests[IO, HorizontalPodAutoscaler, HorizontalPodAutoscalerList] with ReplaceableTests[IO, HorizontalPodAutoscaler] with DeletableTests[IO, HorizontalPodAutoscaler, HorizontalPodAutoscalerList] with WatchableTests[IO, HorizontalPodAutoscaler] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[HorizontalPodAutoscaler].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.horizontalPodAutoscalers override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.horizontalPodAutoscalers.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = HorizontalPodAutoscaler( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), spec = Option(HorizontalPodAutoscalerSpec(scaleTargetRef = CrossVersionObjectReference("kind", "name"), maxReplicas = 2)) ) val maxReplicas = 3 override def modifyResource(resource: HorizontalPodAutoscaler) = resource.copy( metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name))), spec = resource.spec.map(_.copy(maxReplicas = maxReplicas)) ) override def checkUpdated(updatedResource: HorizontalPodAutoscaler) = updatedResource.spec.get.maxReplicas shouldBe maxReplicas override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.horizontalPodAutoscalers.namespace(namespaceName) override def watchApi( namespaceName: String )(implicit client: KubernetesClient[IO]): Watchable[IO, HorizontalPodAutoscaler] = client.horizontalPodAutoscalers.namespace(namespaceName) }
Example 9
Source File: DeploymentsApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect.{ConcurrentEffect, IO} import com.goyeau.kubernetes.client.operation._ import com.goyeau.kubernetes.client.{IntValue, KubernetesClient, StringValue} import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.apps.v1._ import io.k8s.api.core.v1._ import io.k8s.apimachinery.pkg.apis.meta.v1.{LabelSelector, ObjectMeta} import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class DeploymentsApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, Deployment] with GettableTests[IO, Deployment] with ListableTests[IO, Deployment, DeploymentList] with ReplaceableTests[IO, Deployment] with DeletableTests[IO, Deployment, DeploymentList] with DeletableTerminatedTests[IO, Deployment, DeploymentList] with WatchableTests[IO, Deployment] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[Deployment].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.deployments override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.deployments.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = { val label = Option(Map("app" -> "test")) Deployment( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), spec = Option( DeploymentSpec( selector = LabelSelector(matchLabels = label), template = PodTemplateSpec( metadata = Option(ObjectMeta(name = Option(resourceName), labels = label)), spec = Option(PodSpec(containers = Seq(Container("test", image = Option("docker"))))) ) ) ) ) } val strategy = Option( DeploymentStrategy( `type` = Option("RollingUpdate"), rollingUpdate = Option(RollingUpdateDeployment(maxSurge = Option(StringValue("25%")), maxUnavailable = Option(IntValue(10)))) ) ) override def modifyResource(resource: Deployment) = resource.copy( metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name))), spec = resource.spec.map(_.copy(strategy = strategy)) ) override def checkUpdated(updatedResource: Deployment) = updatedResource.spec.value.strategy shouldBe strategy override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.deployments.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, Deployment] = client.deployments.namespace(namespaceName) }
Example 10
Source File: ServicesApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.operation._ import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.core.v1._ import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class ServicesApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, Service] with GettableTests[IO, Service] with ListableTests[IO, Service, ServiceList] with ReplaceableTests[IO, Service] with DeletableTests[IO, Service, ServiceList] with WatchableTests[IO, Service] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[Service].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.services override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.services.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = Service( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), spec = Option(ServiceSpec(ports = Option(Seq(ServicePort(2000))))) ) val labels = Option(Map("test" -> "updated-label")) override def modifyResource(resource: Service) = resource.copy(metadata = resource.metadata.map(_.copy(labels = labels))) override def checkUpdated(updatedResource: Service) = updatedResource.metadata.value.labels shouldBe labels override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.services.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, Service] = client.services.namespace(namespaceName) }
Example 11
Source File: IngressesApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.operation._ import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.networking.v1beta1.{Ingress, IngressList, IngressRule, IngressSpec} import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class IngressesApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, Ingress] with GettableTests[IO, Ingress] with ListableTests[IO, Ingress, IngressList] with ReplaceableTests[IO, Ingress] with DeletableTests[IO, Ingress, IngressList] with WatchableTests[IO, Ingress] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[Ingress].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.ingresses override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.ingresses.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = Ingress( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), spec = Option( IngressSpec( rules = Some(Seq(IngressRule(Some("host")))) ) ) ) private val updatedHost: Option[IngressSpec] = Option( IngressSpec( rules = Some(Seq(IngressRule(Some("host2")))) ) ) override def modifyResource(resource: Ingress) = resource.copy( metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name))), spec = updatedHost ) override def checkUpdated(updatedResource: Ingress) = updatedResource.spec should be( updatedHost ) override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.ingresses.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, Ingress] = client.ingresses.namespace(namespaceName) }
Example 12
Source File: JobsApiTest.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.api import cats.effect.{ConcurrentEffect, IO} import com.goyeau.kubernetes.client.operation._ import com.goyeau.kubernetes.client.KubernetesClient import io.chrisdavenport.log4cats.Logger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import io.k8s.api.batch.v1.{Job, JobList, JobSpec} import io.k8s.api.core.v1._ import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class JobsApiTest extends AnyFlatSpec with Matchers with OptionValues with CreatableTests[IO, Job] with GettableTests[IO, Job] with ListableTests[IO, Job, JobList] with ReplaceableTests[IO, Job] with DeletableTests[IO, Job, JobList] with DeletableTerminatedTests[IO, Job, JobList] with WatchableTests[IO, Job] with ContextProvider { implicit lazy val F: ConcurrentEffect[IO] = IO.ioConcurrentEffect implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] lazy val resourceName = classOf[Job].getSimpleName override def api(implicit client: KubernetesClient[IO]) = client.jobs override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) = client.jobs.namespace(namespaceName) override def sampleResource(resourceName: String, labels: Map[String, String]) = Job( metadata = Option(ObjectMeta(name = Option(resourceName), labels = Option(labels))), spec = Option( JobSpec( template = PodTemplateSpec( metadata = Option(ObjectMeta(name = Option(resourceName))), spec = Option( PodSpec(containers = Seq(Container("test", image = Option("docker"))), restartPolicy = Option("Never")) ) ) ) ) ) val labels = Map("app" -> "test") override def modifyResource(resource: Job) = resource.copy( metadata = Option(ObjectMeta(name = resource.metadata.flatMap(_.name), labels = Option(labels))) ) override def checkUpdated(updatedResource: Job) = (updatedResource.metadata.value.labels.value.toSeq should contain).allElementsOf(labels.toSeq) override def deleteApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Deletable[IO] = client.jobs.namespace(namespaceName) override def watchApi(namespaceName: String)(implicit client: KubernetesClient[IO]): Watchable[IO, Job] = client.jobs.namespace(namespaceName) }
Example 13
Source File: MostFrequentValueImputerTest.scala From doddle-model with Apache License 2.0 | 5 votes |
package io.picnicml.doddlemodel.impute import breeze.linalg.{DenseMatrix, DenseVector} import io.picnicml.doddlemodel.TestingUtils import io.picnicml.doddlemodel.data.Feature.{CategoricalFeature, FeatureIndex, NumericalFeature} import io.picnicml.doddlemodel.impute.MostFrequentValueImputer.ev import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class MostFrequentValueImputerTest extends AnyFlatSpec with Matchers with TestingUtils with OptionValues { "Most frequent value imputer" should "impute the categorical features" in { val xMissing = DenseMatrix( List(Float.NaN, 1.0f, 2.0f), List(3.0f, Float.NaN, 5.0f), List(6.0f, 7.0f, 8.0f), List(6.0f, 7.0f, 2.0f) ) val xImputedExpected = DenseMatrix( List(6.0f, 1.0f, 2.0f), List(3.0f, Float.NaN, 5.0f), List(6.0f, 7.0f, 8.0f), List(6.0f, 7.0f, 2.0f) ) val featureIndex = FeatureIndex.apply(List(CategoricalFeature, NumericalFeature, CategoricalFeature)) val imputer = MostFrequentValueImputer(featureIndex) val fittedImputer = ev.fit(imputer, xMissing) breezeEqual(fittedImputer.mostFrequent.value, DenseVector(6.0f, 2.0f)) shouldBe true breezeEqual(ev.transform(fittedImputer, xMissing), xImputedExpected) shouldBe true } it should "impute a subset of categorical features" in { val xMissing = DenseMatrix( List(Float.NaN, 1.0f, 2.0f), List(3.0f, Float.NaN, 5.0f), List(6.0f, 7.0f, 8.0f), List(6.0f, 7.0f, 2.0f) ) val xImputedExpected = DenseMatrix( List(Float.NaN, 1.0f, 2.0f), List(3.0f, 7.0f, 5.0f), List(6.0f, 7.0f, 8.0f), List(6.0f, 7.0f, 2.0f) ) val featureIndex = FeatureIndex.categorical(List(1, 2)) val imputer = MostFrequentValueImputer(featureIndex) val fittedImputer = ev.fit(imputer, xMissing) breezeEqual(fittedImputer.mostFrequent.value, DenseVector(7.0f, 2.0f)) shouldBe true breezeEqual(ev.transform(fittedImputer, xMissing), xImputedExpected) shouldBe true } }
Example 14
Source File: DeletableTerminatedTests.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.operation import cats.Applicative import cats.implicits._ import com.goyeau.kubernetes.client.KubernetesClient import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.http4s.Status import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers trait DeletableTerminatedTests[F[_], Resource <: { def metadata: Option[ObjectMeta] }, ResourceList <: { def items: Seq[Resource] }] extends AnyFlatSpec with Matchers with OptionValues with MinikubeClientProvider[F] { def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[F]): DeletableTerminated[F] def createChecked(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]): F[Resource] def listNotContains(namespaceName: String, resourceNames: Seq[String], labels: Map[String, String] = Map.empty)( implicit client: KubernetesClient[F] ): F[ResourceList] def deleteTerminated(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]): F[Status] = namespacedApi(namespaceName).deleteTerminated(resourceName) "deleteTerminated" should s"delete $resourceName and block until fully deleted" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) resourceName <- Applicative[F].pure("delete-terminated-resource") _ <- deleteTerminated(namespaceName, resourceName) _ <- listNotContains(namespaceName, Seq(resourceName)) } yield () } it should "fail on non existing namespace" in usingMinikube { implicit client => for { status <- deleteTerminated("non-existing", "non-existing") _ = status shouldBe Status.NotFound } yield () } it should s"fail on non existing $resourceName" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) status <- deleteTerminated(namespaceName, "non-existing") _ = status shouldBe Status.NotFound } yield () } }
Example 15
Source File: ListableTests.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.operation import cats.Applicative import cats.implicits._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.api.NamespacesApiTest import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import scala.language.reflectiveCalls trait ListableTests[F[_], Resource <: { def metadata: Option[ObjectMeta] }, ResourceList <: { def items: Seq[Resource] }] extends AnyFlatSpec with Matchers with OptionValues with MinikubeClientProvider[F] { val resourceIsNamespaced = true def api(implicit client: KubernetesClient[F]): Listable[F, ResourceList] def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[F]): Listable[F, ResourceList] def createChecked(namespaceName: String, resourceName: String, labels: Map[String, String] = Map.empty)( implicit client: KubernetesClient[F] ): F[Resource] def listContains(namespaceName: String, resourceNames: Seq[String], labels: Map[String, String] = Map.empty)( implicit client: KubernetesClient[F] ): F[ResourceList] = for { resourceList <- namespacedApi(namespaceName).list(labels) _ = (resourceList.items.map(_.metadata.value.name.value) should contain).allElementsOf(resourceNames) } yield resourceList def listAllContains(resourceNames: Seq[String])( implicit client: KubernetesClient[F] ): F[ResourceList] = for { resourceList <- api.list() _ = (resourceList.items.map(_.metadata.value.name.value) should contain).allElementsOf(resourceNames) } yield resourceList def listNotContains(namespaceName: String, resourceNames: Seq[String], labels: Map[String, String] = Map.empty)( implicit client: KubernetesClient[F] ): F[ResourceList] = for { resourceList <- namespacedApi(namespaceName).list(labels) _ = (resourceList.items.map(_.metadata.value.name.value) should contain).noElementsOf(resourceNames) } yield resourceList "list" should s"list ${resourceName}s" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) resourceName <- Applicative[F].pure("list-resource") _ <- listNotContains(namespaceName, Seq(resourceName)) _ <- createChecked(namespaceName, resourceName) _ <- listContains(namespaceName, Seq(resourceName)) } yield () } "list" should s"list ${resourceName}s with a label" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) noLabelResourceName <- Applicative[F].pure("no-label-resource") _ <- createChecked(namespaceName, noLabelResourceName) withLabelResourceName <- Applicative[F].pure("label-resource") labels = Map("test" -> "1") _ <- createChecked(namespaceName, withLabelResourceName, labels) _ <- listNotContains(namespaceName, Seq(noLabelResourceName), labels) _ <- listContains(namespaceName, Seq(withLabelResourceName), labels) } yield () } it should s"list ${resourceName}s in all namespaces" in usingMinikube { implicit client => assume(resourceIsNamespaced) for { namespaceResourceNames <- Applicative[F].pure( (0 to 1).map(i => (s"${resourceName.toLowerCase}-$i", s"list-all-${resourceName.toLowerCase}-$i")) ) _ <- namespaceResourceNames.toList.traverse { case (namespaceName, resourceName) => NamespacesApiTest.createChecked[F](namespaceName) *> createChecked(namespaceName, resourceName) } _ <- listAllContains(namespaceResourceNames.map(_._2)) _ <- namespaceResourceNames.toList.traverse { case (namespaceName, _) => client.namespaces.delete(namespaceName) } } yield () } }
Example 16
Source File: ReplaceableTests.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.operation import cats.Applicative import cats.implicits._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.Utils.retry import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.http4s.Status import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import org.scalatest.{Assertion, OptionValues} trait ReplaceableTests[F[_], Resource <: { def metadata: Option[ObjectMeta] }] extends AnyFlatSpec with Matchers with OptionValues with MinikubeClientProvider[F] { def namespacedApi(namespaceName: String)( implicit client: KubernetesClient[F] ): Replaceable[F, Resource] def createChecked(namespaceName: String, resourceName: String)( implicit client: KubernetesClient[F] ): F[Resource] def getChecked(namespaceName: String, resourceName: String)( implicit client: KubernetesClient[F] ): F[Resource] def sampleResource(resourceName: String, labels: Map[String, String] = Map.empty): Resource def modifyResource(resource: Resource): Resource def checkUpdated(updatedResource: Resource): Assertion def replace(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]) = for { resource <- getChecked(namespaceName, resourceName) status <- namespacedApi(namespaceName).replace(modifyResource(resource)) _ = status shouldBe Status.Ok } yield () "replace" should s"replace a $resourceName" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) resourceName <- Applicative[F].pure("some-resource") _ <- createChecked(namespaceName, resourceName) _ <- retry(replace(namespaceName, resourceName)) replaced <- getChecked(namespaceName, resourceName) _ = checkUpdated(replaced) } yield () } it should "fail on non existing namespace" in usingMinikube { implicit client => for { status <- namespacedApi("non-existing").replace( sampleResource("non-existing") ) _ = status should (equal(Status.NotFound).or( equal( Status.InternalServerError ) )) } yield () } it should s"fail on non existing $resourceName" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) status <- namespacedApi(namespaceName).replace( sampleResource("non-existing") ) _ = status should (equal(Status.NotFound).or( equal( Status.InternalServerError ) )) } yield () } }
Example 17
Source File: GettableTests.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.operation import cats.Applicative import cats.implicits._ import com.goyeau.kubernetes.client.KubernetesClient import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.http4s.client.UnexpectedStatus import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import scala.language.reflectiveCalls trait GettableTests[F[_], Resource <: { def metadata: Option[ObjectMeta] }] extends AnyFlatSpec with Matchers with OptionValues with MinikubeClientProvider[F] { def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[F]): Gettable[F, Resource] def createChecked(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]): F[Resource] def getChecked(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]): F[Resource] = for { resource <- namespacedApi(namespaceName).get(resourceName) _ = resource.metadata.value.namespace.value shouldBe namespaceName _ = resource.metadata.value.name.value shouldBe resourceName } yield resource "get" should s"get a $resourceName" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) resourceName <- Applicative[F].pure("some-resource-get") _ <- createChecked(namespaceName, resourceName) _ <- getChecked(namespaceName, resourceName) } yield () } it should "fail on non existing namespace" in intercept[UnexpectedStatus] { usingMinikube(implicit client => getChecked("non-existing", "non-existing")) } it should s"fail on non existing $resourceName" in intercept[UnexpectedStatus] { usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) _ <- getChecked(namespaceName, "non-existing") } yield () } } }
Example 18
Source File: DeletableTests.scala From kubernetes-client with Apache License 2.0 | 5 votes |
package com.goyeau.kubernetes.client.operation import cats.Applicative import cats.implicits._ import com.goyeau.kubernetes.client.KubernetesClient import com.goyeau.kubernetes.client.Utils._ import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta import org.http4s.Status import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers trait DeletableTests[F[_], Resource <: { def metadata: Option[ObjectMeta] }, ResourceList <: { def items: Seq[Resource] }] extends AnyFlatSpec with Matchers with OptionValues with MinikubeClientProvider[F] { def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[F]): Deletable[F] def createChecked(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]): F[Resource] def listNotContains(namespaceName: String, resourceNames: Seq[String], labels: Map[String, String] = Map.empty)( implicit client: KubernetesClient[F] ): F[ResourceList] def delete(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[F]): F[Status] = namespacedApi(namespaceName).delete(resourceName) "delete" should s"delete a $resourceName" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) resourceName <- Applicative[F].pure("delete-resource") _ <- createChecked(namespaceName, resourceName) _ <- delete(namespaceName, resourceName) _ <- retry(listNotContains(namespaceName, Seq(resourceName))) } yield () } it should "fail on non existing namespace" in usingMinikube { implicit client => for { status <- delete("non-existing", "non-existing") _ = status shouldBe Status.NotFound } yield () } it should s"fail on non existing $resourceName" in usingMinikube { implicit client => for { namespaceName <- Applicative[F].pure(resourceName.toLowerCase) status <- delete(namespaceName, "non-existing") _ = status shouldBe Status.NotFound } yield () } }
Example 19
Source File: TestBase.scala From cats-scalatest with Apache License 2.0 | 5 votes |
package cats.scalatest import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec abstract class TestBase extends AnyWordSpec with Matchers with OptionValues { val thisRecord = "I will not buy this record, it is scratched." val thisTobacconist = "Ah! I will not buy this tobacconist's, it is scratched." val hovercraft = "Yes, cigarettes. My hovercraft is full of eels." // As advised by @sjrd: https://gitter.im/scala-js/scala-js?at=5ce51e9b9d64e537bcef6f08 final val isJS = 1.0.toString() == "1" final val isJVM = !isJS final def thisLineNumber: Int = { val st = Thread.currentThread.getStackTrace if (!st(2).getMethodName.contains("thisLineNumber")) st(2).getLineNumber else st(3).getLineNumber } }
Example 20
Source File: AccessControlListSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.acls import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.config.AppConfig.HttpConfig import ch.epfl.bluebrain.nexus.iam.types.Identity._ import ch.epfl.bluebrain.nexus.iam.types.{Identity, Permission} import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{Inspectors, OptionValues} class AccessControlListSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with OptionValues with Resources { "An Access Control List" should { val user: Identity = User("uuid", "realm") val group: Identity = Group("mygroup", "myrealm") val readWrite = Set(Permission("acls/read").value, Permission("acls/write").value) val manage = Set(Permission("acls/manage").value) implicit val http: HttpConfig = HttpConfig("some", 8080, "v1", "http://nexus.example.com") "converted to Json" in { val acls = AccessControlList(user -> readWrite, group -> manage) val json = jsonContentOf("/acls/acl.json") acls.asJson shouldEqual json } "convert from Json" in { val acls = AccessControlList(user -> readWrite, group -> manage) val json = jsonContentOf("/acls/acl.json") json.as[AccessControlList].rightValue shouldEqual acls } "remove ACL" in { val read = Permission.unsafe("read") val write = Permission.unsafe("write") val other = Permission.unsafe("other") val acl = AccessControlList(user -> Set(read, write), group -> Set(other)) val acl2 = AccessControlList(group -> Set(read)) acl -- acl2 shouldEqual acl acl -- AccessControlList(user -> Set(read), group -> Set(other)) shouldEqual AccessControlList(user -> Set(write)) acl -- AccessControlList(user -> Set(read)) shouldEqual AccessControlList(user -> Set(write), group -> Set(other)) } } }
Example 21
Source File: IdentitySpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.client.types import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.client.config.IamClientConfig import ch.epfl.bluebrain.nexus.iam.client.types.Identity._ import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.syntax._ import org.scalatest.{Inspectors, OptionValues} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class IdentitySpec extends AnyWordSpecLike with Matchers with Inspectors with OptionValues with Resources with EitherValues { "An identity" should { implicit val config: IamClientConfig = IamClientConfig(url"http://nexus.example.com", url"http://internal.nexus.example.com", "v1") val user = User("mysubject", "myrealm") val group = Group("mygroup", "myrealm") val authenticated = Authenticated("myrealm") "be created from ids" in { val cases = List[(AbsoluteIri, Identity)]( url"http://nexus.example.com/v1/realms/myrealm/users/mysubject" -> user, url"https://random.com/v1/realms/myrealm/users/mysubject" -> user, url"http://nexus.example.com/v1/realms/myrealm/groups/mygroup" -> group, url"https://random.com/v1/realms/myrealm/groups/mygroup" -> group, url"http://nexus.example.com/v1/realms/myrealm/authenticated" -> authenticated, url"https://random.com/v1/realms/myrealm/authenticated" -> authenticated, url"http://nexus.example.com/v1/anonymous" -> Anonymous, url"https://random.com/v1/anonymous" -> Anonymous ) forAll(cases) { case (iri, identity) => Identity(iri).value shouldEqual identity } } "converted to Json" in { val userJson = jsonContentOf("/identities/produce/user.json") val groupJson = jsonContentOf("/identities/produce/group.json") val authenticatedJson = jsonContentOf("/identities/produce/authenticated.json") val anonymousJson = jsonContentOf("/identities/produce/anonymous.json") val cases = List(user -> userJson, group -> groupJson, Anonymous -> anonymousJson, authenticated -> authenticatedJson) forAll(cases) { case (model: Subject, json) => model.asJson shouldEqual json (model: Identity).asJson shouldEqual json case (model: Identity, json) => model.asJson shouldEqual json } } "convert from Json" in { val userJson = jsonContentOf("/identities/consume/user.json") val groupJson = jsonContentOf("/identities/consume/group.json") val authenticatedJson = jsonContentOf("/identities/consume/authenticated.json") val anonymousJson = jsonContentOf("/identities/consume/anonymous.json") val cases = List(user -> userJson, group -> groupJson, Anonymous -> anonymousJson, authenticated -> authenticatedJson) forAll(cases) { case (model: Subject, json) => json.as[Subject].rightValue shouldEqual model json.as[Identity].rightValue shouldEqual (model: Identity) case (model: Identity, json) => json.as[Identity].rightValue shouldEqual model } } } }
Example 22
Source File: Issue542.scala From guardrail with MIT License | 5 votes |
package core.issues import cats.effect.IO import cats.data.Kleisli import org.http4s._ import org.http4s.circe._ import org.http4s.client.{ Client => Http4sClient } import org.http4s.headers._ import org.http4s.implicits._ import cats.instances.future._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.time.SpanSugar._ import org.scalatest.{ EitherValues, FunSuite, Matchers, OptionValues } import tests.scalatest.EitherTValues class Issue542Suite extends FunSuite with Matchers with EitherValues with ScalaFutures with EitherTValues with OptionValues { override implicit val patienceConfig = PatienceConfig(10 seconds, 1 second) test("base64 bytes can be sent") { import base64.server.http4s.{ FooResponse, Handler, Resource } import base64.server.http4s.definitions.Foo import base64.server.http4s.Implicits.Base64String val route = new Resource[IO]().routes(new Handler[IO] { def foo(respond: FooResponse.type)(): IO[FooResponse] = IO.pure(respond.Ok(Foo(Some(new Base64String("foo".getBytes()))))) }) val client = Http4sClient.fromHttpApp[IO](route.orNotFound) val req = Request[IO](method = Method.GET, uri = Uri.unsafeFromString("/foo")) client .fetch(req)({ case Status.Ok(resp) => resp.status should equal(Status.Ok) resp.contentType should equal(Some(`Content-Type`(MediaType.application.json))) resp.contentLength should equal(Some(16)) jsonOf[IO, Foo].decode(resp, strict = false).rightValue }) .unsafeRunSync() .value .value .data should equal("foo".getBytes()) } test("base64 bytes can be received") { import base64.client.http4s.Client import base64.client.http4s.definitions.Foo import base64.client.http4s.Implicits.Base64String import org.http4s.dsl._ def staticClient: Http4sClient[IO] = { implicit val fooOkEncoder = jsonEncoderOf[IO, Foo] val response = new Http4sDsl[IO] { def route: HttpApp[IO] = Kleisli.liftF(Ok(Foo(Some(Base64String("foo".getBytes()))))) } Http4sClient.fromHttpApp[IO](response.route) } Client .httpClient(staticClient, "http://localhost:80") .foo() .attempt .unsafeRunSync() .fold( _ => fail("Error"), _.fold( handleOk = _.value.value.data should equal("foo".getBytes()) ) ) } }
Example 23
Source File: WebBrowserBehavior.scala From ScalaWebTest with Apache License 2.0 | 5 votes |
package org.scalawebtest.integration.browser.behaviors import org.scalatest.OptionValues import org.scalawebtest.core.IntegrationFlatSpec trait WebBrowserBehavior extends OptionValues { self: IntegrationFlatSpec => def aWebBrowserWithElementLookup(): Unit = { it should "be capable to find a single element in an HTML document" in { navigateTo("/") find(cssSelector("h1")).value.text shouldBe "ScalaWebTest - Mock Server" } it should "be capable to find a list of elements in an HTML document" in { navigateTo("/elementsList.jsp") findAll(cssSelector("li")) should have size 3 } } }
Example 24
Source File: StatisticsSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.indexing import java.util.regex.Pattern.quote import ch.epfl.bluebrain.nexus.commons.test.Resources import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.config.AppConfig import ch.epfl.bluebrain.nexus.kg.config.Vocabulary.nxv import ch.epfl.bluebrain.nexus.kg.indexing.Statistics.{CompositeViewStatistics, ViewStatistics} import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.commons.circe.syntax._ import io.circe.Printer import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{Inspectors, OptionValues} class StatisticsSpec extends AnyWordSpecLike with Matchers with OptionValues with TestHelper with Resources with Inspectors { "Statistics" should { val sourceId = genIri val projectionId = nxv.defaultElasticSearchIndex.value val single: Statistics = ViewStatistics(10L, 1L, 2L, 12L, None, None, None) val singleJson = jsonContentOf("/view/statistics.json").removeKeys("projectionId") val composite: Statistics = CompositeViewStatistics(IdentifiedProgress(sourceId, projectionId, single.asInstanceOf[ViewStatistics])) val compositeJson = jsonContentOf("/view/composite_statistics.json", Map(quote("{sourceId}") -> sourceId.asString)) val printer: Printer = Printer.noSpaces.copy(dropNullValues = true) "be encoded" in { forAll(List(single -> singleJson, composite -> compositeJson)) { case (model, json) => printer.print(model.asJson.sortKeys(AppConfig.orderedKeys)) shouldEqual printer.print(json) } } } }
Example 25
Source File: TypeRegistrySpec.scala From scalapb-json4s with Apache License 2.0 | 5 votes |
package scalapb.json4s import com.google.protobuf.wrappers.UInt64Value import jsontest.test3.{MyTest3, Test3Proto, Wrapper} import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.must.Matchers class TypeRegistrySpec extends AnyFlatSpec with Matchers with OptionValues { "addFile" should "add all messages in the file" in { val reg = TypeRegistry().addFile(Test3Proto) reg.findType("type.googleapis.com/jsontest.MyTest3").value must be(MyTest3) reg.findType("type.googleapis.com/jsontest.Wrapper").value must be(Wrapper) reg .findType("type.googleapis.com/google.protobuf.UInt64Value") .value must be(UInt64Value) reg.findType("type.googleapis.com/something.else") must be(None) } "addMessage" should "not add other messages from same file" in { val reg = TypeRegistry().addMessage[MyTest3] reg.findType("type.googleapis.com/jsontest.MyTest3").value must be(MyTest3) reg.findType("type.googleapis.com/jsontest.Wrapper") must be(None) } }
Example 26
Source File: ArtefactsSpecs.scala From releaser with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.releaser import java.nio.file.Files import org.scalatest.{Matchers, OptionValues, WordSpec} import uk.gov.hmrc.releaser.github.Repo class ArtefactsSpecs extends WordSpec with Matchers with OptionValues{ "MavenArtefacts.transformersForSupportedFiles" should { "generate the correct list of transformers" in { val ver = mavenVersionMapping("time", "time") val artefacts = new MavenArtefacts(ver, Files.createTempDirectory("test")) val files = List( "time/uk/gov/hmrc/time_2.11/1.3.0-1-g21312cc/time_2.11-1.3.0-1-g21312cc.jar", "time/time_2.11-1.3.0-1-g21312cc.zip", "time/time_2.11-1.3.0-1-g21312cc.tgz", "time/time_2.11-1.3.0-1-g21312cc-assembly.jar", "time/time_2.11-1.3.0-1-g21312cc-sources.jar", "time/uk/gov/hmrc/time_2.11/1.3.0-1-g21312cc/time_2.11-1.3.0-1-g21312cc.pom", "time/time_2.11-1.3.0-1-g21312cc.pom.md5", "time/time_2.11-other-1.3.0-1-g21312cc.tgz", "time/time_2.11-other-1.3.0-1-g21312cc.jar" ) val result: Map[String, Option[Transformer]] = artefacts.transformersForSupportedFiles(filePaths = files).toMap result foreach println result.size shouldBe 8 result("time/uk/gov/hmrc/time_2.11/1.3.0-1-g21312cc/time_2.11-1.3.0-1-g21312cc.jar").get.isInstanceOf[JarManifestTransformer] shouldBe true result("time/time_2.11-1.3.0-1-g21312cc.zip").get.isInstanceOf[CopyAndRenameTransformer] shouldBe true result("time/time_2.11-1.3.0-1-g21312cc.tgz").get.isInstanceOf[TgzTransformer] shouldBe true result("time/time_2.11-1.3.0-1-g21312cc-assembly.jar").get.isInstanceOf[JarManifestTransformer] shouldBe true result("time/time_2.11-1.3.0-1-g21312cc-sources.jar").get.isInstanceOf[JarManifestTransformer] shouldBe true result("time/uk/gov/hmrc/time_2.11/1.3.0-1-g21312cc/time_2.11-1.3.0-1-g21312cc.pom").get.isInstanceOf[PomTransformer] shouldBe true result("time/time_2.11-other-1.3.0-1-g21312cc.tgz").get.isInstanceOf[TgzTransformer] shouldBe true result("time/time_2.11-other-1.3.0-1-g21312cc.jar").get.isInstanceOf[JarManifestTransformer] shouldBe true } } "IvyArtefacts.transformersForSupportedFiles" should { "generate the correct list of transformers" in { val ver = mavenVersionMapping("sbt-bobby", "sbt-bobby") val artefacts = new IvyArtefacts(ver, Files.createTempDirectory("test")) val files = List( "sbt-bobby.jar", "sbt-bobby.zip", "sbt-bobby.tgz", "sbt-bobby-assembly.jar", "sbt-bobby-other.jar", "sbt-bobby-sources.jar", "ivy.xml", "ivy.xml.md5" ) val result: Map[String, Option[Transformer]] = artefacts.transformersForSupportedFiles(filePaths = files).toMap result.size shouldBe 7 result("sbt-bobby.jar").get.isInstanceOf[JarManifestTransformer] shouldBe true result("sbt-bobby.zip").get.isInstanceOf[CopyAndRenameTransformer] shouldBe true result("sbt-bobby.tgz").get.isInstanceOf[CopyAndRenameTransformer] shouldBe true result("sbt-bobby-assembly.jar").get.isInstanceOf[JarManifestTransformer] shouldBe true result("sbt-bobby-other.jar").get.isInstanceOf[JarManifestTransformer] shouldBe true result("sbt-bobby-sources.jar").get.isInstanceOf[JarManifestTransformer] shouldBe true result("ivy.xml").get.isInstanceOf[IvyTransformer] shouldBe true result.contains("ivy.xml.md5") shouldBe false } } private def mavenVersionMapping(artefactName:String = "a", repoName:String = "a", rcVersion:String = "1.3.0-1-g21312cc", releaseVersion:String = "1.4.0") = VersionMapping( RepoFlavours.mavenRepository, artefactName, Repo(repoName), ReleaseCandidateVersion(rcVersion), ReleaseVersion(releaseVersion)) }
Example 27
Source File: GithubHttpSpecs.scala From releaser with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.releaser.github import org.scalatest.{Matchers, OptionValues, WordSpec} import play.api.libs.json.{JsNumber, JsObject} import play.api.libs.ws.{EmptyBody, WSAuthScheme} import uk.gov.hmrc.ServiceCredentials class GithubHttpSpecs extends WordSpec with Matchers with OptionValues{ "GithubHttpSpecs" should { "build request holder" in { val githubHttp = new GithubHttp(ServiceCredentials("Charles", "123")) val body = JsObject(Seq("a" -> JsNumber(1))) val call = githubHttp.buildCall("POST", "http://example.com", Some(body)) call.method shouldBe "POST" call.body should not be EmptyBody call.url shouldBe "http://example.com" call.auth.value shouldBe (("Charles", "123", WSAuthScheme.BASIC)) } } }
Example 28
Source File: security_api_yaml.scala From api-first-hand with MIT License | 5 votes |
package security.api.yaml import de.zalando.play.controllers._ import org.scalacheck._ import org.scalacheck.Arbitrary._ import org.scalacheck.Prop._ import org.scalacheck.Test._ import org.specs2.mutable._ import org.specs2.execute._ import play.api.test.Helpers._ import play.api.test._ import play.api.mvc.MultipartFormData.FilePart import play.api.mvc._ import org.junit.runner.RunWith import java.net.URLEncoder import com.fasterxml.jackson.databind.ObjectMapper import play.api.http.Writeable import play.api.libs.Files.TemporaryFile import play.api.test.Helpers.{status => requestStatusCode_} import play.api.test.Helpers.{contentAsString => requestContentAsString_} import play.api.test.Helpers.{contentType => requestContentType_} import org.scalatest.{OptionValues, WordSpec} import org.scalatestplus.play.{OneAppPerTest, WsScalaTestClient} import Generators._ import de.zalando.play.controllers.ArrayWrapper //noinspection ScalaStyle class Security_api_yamlSpec extends WordSpec with OptionValues with WsScalaTestClient with OneAppPerTest { def toPath[T](value: T)(implicit binder: PathBindable[T]): String = Option(binder.unbind("", value)).getOrElse("") def toQuery[T](key: String, value: T)(implicit binder: QueryStringBindable[T]): String = Option(binder.unbind(key, value)).getOrElse("") def toHeader[T](value: T)(implicit binder: QueryStringBindable[T]): String = Option(binder.unbind("", value)).getOrElse("") def checkResult(props: Prop): org.specs2.execute.Result = Test.check(Test.Parameters.default, props).status match { case Failed(args, labels) => val failureMsg = labels.mkString("\n") + " given args: " + args.map(_.arg).mkString("'", "', '","'") org.specs2.execute.Failure(failureMsg) case Proved(_) | Exhausted | Passed => org.specs2.execute.Success() case PropException(_, e: IllegalStateException, _) => org.specs2.execute.Error(e.getMessage) case PropException(_, e, labels) => val error = if (labels.isEmpty) e.getLocalizedMessage else labels.mkString("\n") org.specs2.execute.Failure(error) } private def parserConstructor(mimeType: String) = PlayBodyParsing.jacksonMapper(mimeType) def parseResponseContent[T](mapper: ObjectMapper, content: String, mimeType: Option[String], expectedType: Class[T]) = if (expectedType.getCanonicalName == "scala.runtime.Null$") null else mapper.readValue(content, expectedType) }
Example 29
Source File: instagram_api_yaml.scala From api-first-hand with MIT License | 5 votes |
package instagram.api.yaml import de.zalando.play.controllers._ import org.scalacheck._ import org.scalacheck.Arbitrary._ import org.scalacheck.Prop._ import org.scalacheck.Test._ import org.specs2.mutable._ import org.specs2.execute._ import play.api.test.Helpers._ import play.api.test._ import play.api.mvc.MultipartFormData.FilePart import play.api.mvc._ import org.junit.runner.RunWith import java.net.URLEncoder import com.fasterxml.jackson.databind.ObjectMapper import play.api.http.Writeable import play.api.libs.Files.TemporaryFile import play.api.test.Helpers.{status => requestStatusCode_} import play.api.test.Helpers.{contentAsString => requestContentAsString_} import play.api.test.Helpers.{contentType => requestContentType_} import org.scalatest.{OptionValues, WordSpec} import org.scalatestplus.play.{OneAppPerTest, WsScalaTestClient} import Generators._ import scala.math.BigInt import scala.math.BigDecimal //noinspection ScalaStyle class Instagram_api_yamlSpec extends WordSpec with OptionValues with WsScalaTestClient with OneAppPerTest { def toPath[T](value: T)(implicit binder: PathBindable[T]): String = Option(binder.unbind("", value)).getOrElse("") def toQuery[T](key: String, value: T)(implicit binder: QueryStringBindable[T]): String = Option(binder.unbind(key, value)).getOrElse("") def toHeader[T](value: T)(implicit binder: QueryStringBindable[T]): String = Option(binder.unbind("", value)).getOrElse("") def checkResult(props: Prop): org.specs2.execute.Result = Test.check(Test.Parameters.default, props).status match { case Failed(args, labels) => val failureMsg = labels.mkString("\n") + " given args: " + args.map(_.arg).mkString("'", "', '","'") org.specs2.execute.Failure(failureMsg) case Proved(_) | Exhausted | Passed => org.specs2.execute.Success() case PropException(_, e: IllegalStateException, _) => org.specs2.execute.Error(e.getMessage) case PropException(_, e, labels) => val error = if (labels.isEmpty) e.getLocalizedMessage else labels.mkString("\n") org.specs2.execute.Failure(error) } private def parserConstructor(mimeType: String) = PlayBodyParsing.jacksonMapper(mimeType) def parseResponseContent[T](mapper: ObjectMapper, content: String, mimeType: Option[String], expectedType: Class[T]) = if (expectedType.getCanonicalName == "scala.runtime.Null$") null else mapper.readValue(content, expectedType) }
Example 30
Source File: RequestsSpec.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.api.http.requests import com.wavesplatform.account.KeyPair import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.transaction.transfer.TransferTransaction import com.wavesplatform.{NoShrink, TransactionGen} import org.scalacheck.Gen import org.scalatest.{FreeSpec, Matchers, OptionValues} import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import play.api.libs.json._ class RequestsSpec extends FreeSpec with Matchers with OptionValues with ScalaCheckPropertyChecks with TransactionGen with NoShrink { private def transferRequestGen(version: Int): Gen[(KeyPair, JsObject)] = (for { sender <- accountGen recipient <- accountGen proofs <- proofsGen } yield ( sender, Json.obj( "type" -> 4, "version" -> version, "senderPublicKey" -> sender.publicKey.toString, "assetId" -> JsNull, "attachment" -> "", "feeAssetId" -> JsNull, "timestamp" -> System.currentTimeMillis(), "fee" -> 100000, "amount" -> 10000, "recipient" -> recipient.publicKey.toAddress.stringRepr, "proofs" -> JsArray(proofs.proofs.map(p => JsString(p.toString))) ) )).label(s"Transfer Request v$version") "TransferRequest" - { "accepts proofs for version >= 2" in { TransferTransaction.supportedVersions.filter(_ >= 2).foreach { version => forAll(transferRequestGen(version)) { case (sender, json) => val request = json.as[TransferRequest] val tx = request.toTxFrom(sender.publicKey).explicitGet() request.proofs.value should be(tx.proofs) } } } } }
Example 31
Source File: BlockV5GrpcSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.grpc import com.google.protobuf.ByteString import com.typesafe.config.Config import com.wavesplatform.api.grpc.BlockRangeRequest import com.wavesplatform.block.Block import com.wavesplatform.common.state.ByteStr import com.wavesplatform.crypto import com.wavesplatform.it.api.SyncGrpcApi._ import com.wavesplatform.it.sync.activation.ActivationStatusRequest import com.wavesplatform.it.transactions.NodesFromDocker import com.wavesplatform.it.{GrpcIntegrationSuiteWithThreeAddress, NodeConfigs, ReportingTestName} import org.scalatest.{CancelAfterFailure, FreeSpec, Matchers, OptionValues} import scala.concurrent.duration._ class BlockV5GrpcSuite extends FreeSpec with Matchers with CancelAfterFailure with NodesFromDocker with ActivationStatusRequest with ReportingTestName with OptionValues with GrpcIntegrationSuiteWithThreeAddress { override def nodeConfigs: Seq[Config] = NodeConfigs.newBuilder .overrideBase(_.quorum(0)) .withDefault(1) .withSpecial(1, _.nonMiner) .buildNonConflicting() "block v5 appears and blockchain grows" - { "when feature activation happened" in { sender.waitForHeight(sender.height + 1, 2.minutes) val currentHeight = sender.height val blockV5 = sender.blockAt(currentHeight) val blockV5ById = sender.blockById(ByteString.copyFrom(blockV5.id().arr)) blockV5.header.version shouldBe Block.ProtoBlockVersion blockV5.id().arr.length shouldBe crypto.DigestLength blockV5.signature.arr.length shouldBe crypto.SignatureLength blockV5.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5.transactionsRootValid(), "transactionsRoot is not valid") blockV5ById.header.version shouldBe Block.ProtoBlockVersion blockV5ById.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5ById.transactionsRootValid(), "transactionsRoot is not valid") sender.waitForHeight(currentHeight + 1, 2.minutes) val blockAfterVRFUsing = sender.blockAt(currentHeight + 1) val blockAfterVRFUsingById = sender.blockById(ByteString.copyFrom(blockAfterVRFUsing.id().arr)) blockAfterVRFUsing.header.version shouldBe Block.ProtoBlockVersion blockAfterVRFUsing.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength ByteStr(sender.blockHeaderAt(currentHeight + 1).reference.toByteArray) shouldBe blockV5.id() blockAfterVRFUsingById.header.version shouldBe Block.ProtoBlockVersion blockAfterVRFUsingById.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockAfterVRFUsingById.transactionsRootValid(), "transactionsRoot is not valid") val blockSeqOfBlocksV5 = sender.blockSeq(currentHeight, currentHeight + 2) for (blockV5 <- blockSeqOfBlocksV5) { blockV5.header.version shouldBe Block.ProtoBlockVersion blockV5.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5.transactionsRootValid(), "transactionsRoot is not valid") } val blockSeqOfBlocksV5ByAddress = sender.blockSeqByAddress(miner.address, currentHeight, currentHeight + 2) for (blockV5 <- blockSeqOfBlocksV5ByAddress) { blockV5.header.generator shouldBe miner.keyPair.publicKey blockV5.header.version shouldBe Block.ProtoBlockVersion blockV5.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5.transactionsRootValid(), "transactionsRoot is not valid") } val blockSeqOfBlocksV5ByPKGrpc = NodeExtGrpc(sender).blockSeq( currentHeight, currentHeight + 2, BlockRangeRequest.Filter.GeneratorPublicKey(ByteString.copyFrom(miner.keyPair.publicKey.arr)) ) for (blockV5 <- blockSeqOfBlocksV5ByPKGrpc) { blockV5.header.generator shouldBe miner.keyPair.publicKey blockV5.header.version shouldBe Block.ProtoBlockVersion blockV5.header.generationSignature.arr.length shouldBe Block.GenerationVRFSignatureLength assert(blockV5.transactionsRootValid(), "transactionsRoot is not valid") } } } }
Example 32
Source File: InvokeCalcIssueSuite.scala From Waves with MIT License | 5 votes |
package com.wavesplatform.it.sync.smartcontract import com.typesafe.config.Config import com.wavesplatform.common.utils.EitherExt2 import com.wavesplatform.features.BlockchainFeatures import com.wavesplatform.it.NodeConfigs import com.wavesplatform.it.NodeConfigs.Default import com.wavesplatform.it.api.SyncHttpApi._ import com.wavesplatform.it.sync._ import com.wavesplatform.it.transactions.BaseTransactionSuite import com.wavesplatform.it.util._ import com.wavesplatform.lang.v1.estimator.v3.ScriptEstimatorV3 import com.wavesplatform.state.BinaryDataEntry import com.wavesplatform.transaction.smart.script.ScriptCompiler import org.scalatest.{CancelAfterFailure, Matchers, OptionValues} class InvokeCalcIssueSuite extends BaseTransactionSuite with Matchers with CancelAfterFailure with OptionValues { import InvokeCalcIssueSuite._ override protected def nodeConfigs: Seq[Config] = NodeConfigs .Builder(Default, 1, Seq.empty) .overrideBase(_.quorum(0)) .overrideBase(_.preactivatedFeatures((BlockchainFeatures.BlockV5.id, 0), (BlockchainFeatures.BlockV5.id, 0))) .buildNonConflicting() private val smartAcc = firstAddress private val callerAcc = secondAddress test("calculateAssetId should return right unique id for each invoke") { sender.setScript( smartAcc, Some(ScriptCompiler.compile(dAppV4, ScriptEstimatorV3).explicitGet()._1.bytes().base64), fee = setScriptFee + smartFee, waitForTx = true ) sender .invokeScript( callerAcc, smartAcc, Some("i"), args = List.empty, fee = invokeFee + issueFee, // dAppV4 contains 1 Issue action waitForTx = true ) val assetId = sender.getDataByKey(smartAcc, "id").as[BinaryDataEntry].value.toString sender .invokeScript( callerAcc, smartAcc, Some("i"), args = List.empty, fee = invokeFee + issueFee, // dAppV4 contains 1 Issue action waitForTx = true ) val secondAssetId = sender.getDataByKey(smartAcc, "id").as[BinaryDataEntry].value.toString sender.assetBalance(smartAcc, assetId).balance shouldBe 100 sender.assetBalance(smartAcc, secondAssetId).balance shouldBe 100 val assetDetails = sender.assetsDetails(assetId) assetDetails.decimals shouldBe decimals assetDetails.name shouldBe assetName assetDetails.reissuable shouldBe reissuable assetDetails.description shouldBe assetDescr assetDetails.minSponsoredAssetFee shouldBe None } } object InvokeCalcIssueSuite { val assetName = "InvokeAsset" val assetDescr = "Invoke asset descr" val amount = 100 val decimals = 0 val reissuable = true private val dAppV4: String = s"""{-# STDLIB_VERSION 4 #-} |{-# CONTENT_TYPE DAPP #-} | |@Callable(i) |func i() = { |let issue = Issue("$assetName", "$assetDescr", $amount, $decimals, $reissuable, unit, 0) |let id = calculateAssetId(issue) |[issue, | BinaryEntry("id", id)] |} | |""".stripMargin }
Example 33
Source File: ResourceDecoderSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.serializers import java.time.Instant import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.client.types.Identity.User import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.config.Schemas import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.kg.resources.{Id, ResourceF, ResourceGraph} import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.Decoder import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{Inspectors, OptionValues} class ResourceDecoderSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with ScalatestRouteTest with OptionValues with Resources with TestHelper { private val json = jsonContentOf("/serialization/resource.json") private val projectRef = ProjectRef(genUUID) private val id = url"http://example.com/prefix/myId" private val graph = json.toGraph(id).rightValue private implicit val decoder: Decoder[ResourceGraph] = ResourceF.resourceGraphDecoder(projectRef) private val model = ResourceF( Id(projectRef, url"http://example.com/prefix/myId"), 1L, Set(url"https://example.com/vocab/A", url"https://example.com/vocab/B"), deprecated = false, Map.empty, None, Instant.parse("2020-01-17T12:45:01.479676Z"), Instant.parse("2020-01-17T13:45:01.479676Z"), User("john", "bbp"), User("brenda", "bbp"), Schemas.unconstrainedRef, graph ) "A resource" should { "be decoded" in { json.as[ResourceGraph].rightValue shouldEqual model } } }
Example 34
Source File: DiskStorageOperationsSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.storage import java.nio.file.Paths import akka.http.scaladsl.model.{ContentTypes, Uri} import cats.effect.IO import ch.epfl.bluebrain.nexus.commons.test._ import ch.epfl.bluebrain.nexus.commons.test.io.IOEitherValues import ch.epfl.bluebrain.nexus.kg.config.AppConfig._ import ch.epfl.bluebrain.nexus.kg.config.Settings import ch.epfl.bluebrain.nexus.kg.resources.file.File.FileDescription import ch.epfl.bluebrain.nexus.kg.resources.Id import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.kg.{KgError, TestHelper} import ch.epfl.bluebrain.nexus.sourcing.RetryStrategyConfig import org.mockito.IdiomaticMockito import org.scalatest.{BeforeAndAfter, OptionValues} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class DiskStorageOperationsSpec extends ActorSystemFixture("DiskStorageOperationsSpec") with AnyWordSpecLike with Matchers with BeforeAndAfter with IdiomaticMockito with IOEitherValues with Resources with TestHelper with OptionValues { private val appConfig = Settings(system).appConfig private implicit val sc: StorageConfig = appConfig.storage.copy( DiskStorageConfig(Paths.get("/tmp"), "SHA-256", read, write, false, 1024L), RemoteDiskStorageConfig("http://example.com", "v1", None, "SHA-256", read, write, true, 1024L), S3StorageConfig("MD5", read, write, true, 1024L), "password", "salt", RetryStrategyConfig("linear", 300.millis, 5.minutes, 100, 1.second) ) private val project = ProjectRef(genUUID) private val storage = Storage.DiskStorage.default(project) private val resId = Id(storage.ref, genIri) private val fileDesc = FileDescription("my file.txt", ContentTypes.`text/plain(UTF-8)`) "DiskStorageOperations" should { "verify when the storage exists" in { val verify = new DiskStorageOperations.VerifyDiskStorage[IO](storage) verify.apply.accepted } "save and fetch files" in { val save = new DiskStorageOperations.SaveDiskFile[IO](storage) val fetch = new DiskStorageOperations.FetchDiskFile[IO]() val source = genSource val attr = save.apply(resId, fileDesc, source).ioValue attr.bytes shouldEqual 16L attr.filename shouldEqual fileDesc.filename attr.mediaType shouldEqual fileDesc.mediaType.value attr.location shouldEqual Uri(s"file:///tmp/${mangle(project, attr.uuid, "my%20file.txt")}") attr.path shouldEqual attr.location.path.tail.tail.tail val fetched = fetch.apply(attr).ioValue consume(source) shouldEqual consume(fetched) } "not link files" in { val link = new DiskStorageOperations.LinkDiskFile[IO]() link.apply(resId, fileDesc, Uri.Path("/foo")).failed[KgError] shouldEqual KgError.UnsupportedOperation } } }
Example 35
Source File: SparqlLinkSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.indexing import java.time.{Clock, Instant, ZoneId} import ch.epfl.bluebrain.nexus.commons.sparql.client.SparqlResults.Binding import ch.epfl.bluebrain.nexus.kg.config.Schemas._ import ch.epfl.bluebrain.nexus.kg.config.Vocabulary.nxv import ch.epfl.bluebrain.nexus.kg.indexing.SparqlLink.{SparqlExternalLink, SparqlResourceLink} import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.Vocabulary._ import ch.epfl.bluebrain.nexus.rdf.implicits._ import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers class SparqlLinkSpec extends AnyWordSpecLike with Matchers with OptionValues { "A SparqlLink" should { val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) val id = url"http://example.com/id" val property = url"http://example.com/friend" val property2 = url"http://example.com/friend2" val paths = List(property, property2) "build SparqlExternalLink from SPARQL response" in { val bindings = Map( "s" -> Binding("uri", id.asString), "paths" -> Binding("literal", s"${property.asString} ${property2.asString}") ) SparqlExternalLink(bindings).value shouldEqual SparqlExternalLink(id, paths) } "build SparqlResourceLink from SPARQL response" in { val self = url"http://127.0.0.1:8080/v1/resources/myorg/myproject/_/id" val project = url"http://127.0.0.1:8080/v1/projects/myorg/myproject/" val author = url"http://127.0.0.1:8080/v1/realms/myrealm/users/me" val bindings = Map( "s" -> Binding("uri", id.asString), "paths" -> Binding("literal", s"${property.asString} ${property2.asString}"), "_rev" -> Binding("literal", "1", datatype = Some(xsd.long.asString)), "_self" -> Binding("uri", self.asString), "_project" -> Binding("uri", project.asString), "types" -> Binding("literal", s"${nxv.Resolver.asString} ${nxv.Schema.asString}"), "_constrainedBy" -> Binding("uri", unconstrainedSchemaUri.asString), "_createdBy" -> Binding("uri", author.asString), "_updatedBy" -> Binding("uri", author.asString), "_createdAy" -> Binding("uri", author.asString), "_createdAt" -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)), "_updatedAt" -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)), "_deprecated" -> Binding("literal", "false", datatype = Some(xsd.boolean.asString)) ) SparqlResourceLink(bindings).value shouldEqual SparqlResourceLink( id, project, self, 1L, Set[AbsoluteIri](nxv.Schema, nxv.Resolver), false, clock.instant(), clock.instant(), author, author, unconstrainedRef, paths ) } } }
Example 36
Source File: ChangePathSpec.scala From ScalaWebTest with Apache License 2.0 | 5 votes |
package org.scalawebtest.integration.navigation import org.scalatest.OptionValues import org.scalawebtest.integration.ScalaWebTestBaseSpec class ChangePathSpec extends ScalaWebTestBaseSpec with OptionValues { path = "/a.jsp" "When url is defined it" should "automatically navigate to page A" in { find(cssSelector("h1")).value.text shouldEqual "a" } it should "navigate to page B" in { navigateTo("/b.jsp") find(cssSelector("h1")).value.text shouldEqual "b" } it should "navigate to page A again" in { navigateTo("/a.jsp") find(cssSelector("h1")).value.text shouldEqual "a" } it should "navigate to page B again" in { navigateTo("/b.jsp") find(cssSelector("h1")).value.text shouldEqual "b" } }
Example 37
Source File: LinkDescriptionSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resources import akka.http.scaladsl.model.Uri.Path import akka.http.scaladsl.model.{ContentType, ContentTypes} import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Randomness} import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.kg.resources.Rejection.InvalidResourceFormat import ch.epfl.bluebrain.nexus.kg.resources.file.File.{FileDescription, LinkDescription} import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.Json import io.circe.syntax._ import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class LinkDescriptionSpec extends AnyWordSpecLike with Matchers with TestHelper with Randomness with EitherValues with OptionValues { private abstract class Ctx { val id = Id(ProjectRef(genUUID), genIri) val p = genString() + "/" + genString() val f = genString() val m = "application/json" def jsonLink(mediaType: String = m, filename: String = f, path: String = p): Json = Json.obj("filename" -> filename.asJson, "path" -> path.asJson, "mediaType" -> mediaType.asJson) } "A Link Description" should { "be decoded correctly" in new Ctx { LinkDescription(id, jsonLink()).rightValue shouldEqual LinkDescription(Path(p), Some(f), ContentType.parse(m).toOption) LinkDescription(id, Json.obj("path" -> p.asJson)).rightValue shouldEqual LinkDescription(Path(p), None, None) } "accept missing filename" in new Ctx { LinkDescription(id, jsonLink().removeKeys("filename")).rightValue shouldEqual LinkDescription(Path(p), None, ContentType.parse(m).toOption) } "reject empty filename" in new Ctx { LinkDescription(id, jsonLink(filename = "")).leftValue shouldBe a[InvalidResourceFormat] } "accept missing mediaType" in new Ctx { LinkDescription(id, jsonLink().removeKeys("mediaType")).rightValue shouldEqual LinkDescription(Path(p), Some(f), None) } "reject wrong mediaType format" in new Ctx { LinkDescription(id, jsonLink(mediaType = genString())).leftValue shouldBe a[InvalidResourceFormat] } "reject missing path" in new Ctx { LinkDescription(id, jsonLink().removeKeys("path")).leftValue shouldBe a[InvalidResourceFormat] } "be converted to a FileDescription correctly" in new Ctx { val fileDesc1 = FileDescription.from(LinkDescription(Path("/foo/bar/file.ext"), None, None)) fileDesc1.filename shouldEqual "file.ext" fileDesc1.mediaType shouldEqual None fileDesc1.defaultMediaType shouldEqual ContentTypes.`application/octet-stream` val fileDesc2 = FileDescription.from(LinkDescription(Path("/foo/bar/somedir/"), None, ContentType.parse(m).toOption)) fileDesc2.filename shouldEqual "somedir" fileDesc2.mediaType.value shouldEqual ContentTypes.`application/json` val fileDesc3 = FileDescription.from(LinkDescription(Path("/foo/bar/baz"), Some("file.json"), ContentType.parse(m).toOption)) fileDesc3.filename shouldEqual "file.json" fileDesc3.mediaType.value shouldEqual ContentTypes.`application/json` } } }
Example 38
Source File: CompositeResolutionSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resolve import java.time.Clock import java.util.UUID import java.util.regex.Pattern.quote import cats.instances.try_._ import cats.syntax.show._ import ch.epfl.bluebrain.nexus.commons.test.Resources import ch.epfl.bluebrain.nexus.kg.resources.Ref import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{OptionValues, TryValues} import scala.util.Try class CompositeResolutionSpec extends AnyWordSpecLike with Resources with Matchers with TryValues with OptionValues { "CompositeResolution" should { implicit val clock = Clock.systemUTC val resource1Uri: AbsoluteIri = url"http://nexus.example.com/resources/static/${UUID.randomUUID().toString}" val resource2Uri: AbsoluteIri = url"http://nexus.example.com/resources/static/${UUID.randomUUID().toString}" val staticResolution1 = StaticResolution[Try]( Map( resource1Uri -> jsonContentOf( "/resolve/simple-resource.json", Map(quote("{id}") -> resource1Uri.show, quote("{random}") -> UUID.randomUUID().toString) ) ) ) val staticResolution2 = StaticResolution[Try]( Map( resource2Uri -> jsonContentOf( "/resolve/simple-resource.json", Map(quote("{id}") -> resource2Uri.show, quote("{random}") -> UUID.randomUUID().toString) ) ) ) val staticResolution3 = StaticResolution[Try]( Map( resource1Uri -> jsonContentOf( "/resolve/simple-resource.json", Map(quote("{id}") -> resource1Uri.show, quote("{random}") -> UUID.randomUUID().toString) ) ) ) val compositeResolution = CompositeResolution[Try](List(staticResolution1, staticResolution2, staticResolution3)) "return the resource from the first resolver which returns the resource" in { compositeResolution.resolve(Ref(resource1Uri)).success.value.value shouldEqual staticResolution1 .resolve(Ref(resource1Uri)) .success .value .value compositeResolution.resolve(Ref(resource2Uri)).success.value.value shouldEqual staticResolution2 .resolve(Ref(resource2Uri)) .success .value .value } } }
Example 39
Source File: TraitReferenceSpec.scala From vamp with Apache License 2.0 | 5 votes |
package io.vamp.model.artifact import org.scalatest.prop.GeneratorDrivenPropertyChecks import org.scalatest.{ Matchers, OptionValues, WordSpec } class TraitReferenceSpec extends WordSpec with Matchers with OptionValues with GeneratorDrivenPropertyChecks { "TraitReference" should { "convert string to TraitReference via: referenceFor" in { forAll("cluster", "group", "name") { (cluster: String, group: String, name: String) ⇒ whenever(!cluster.contains('.') && !group.contains('.') && !name.contains('.')) { val traitReferenceString = s"$cluster.$group.$name" TraitReference.referenceFor(traitReferenceString).value should be(TraitReference(cluster, group, name)) } } } "referenceFor & toReference: referenceFor(x.toReference) should yield same result" in { forAll("cluster", "group", "name") { (cluster: String, group: String, name: String) ⇒ whenever(!cluster.contains('.') && !group.contains('.') && !name.contains('.')) { TraitReference.referenceFor(TraitReference(cluster, group, name).reference).value should be(TraitReference(cluster, group, name)) } } } } }
Example 40
Source File: StreamletDefinitionSpec.scala From cloudflow with Apache License 2.0 | 5 votes |
package cloudflow.streamlets import com.typesafe.config.ConfigFactory import org.scalatest.{ MustMatchers, OptionValues, TryValues, WordSpec } class StreamletDefinitionSpec extends WordSpec with MustMatchers with TryValues with OptionValues { "A valid StreamletConfig" should { val config = ConfigFactory.load("config-map-sample.json") val streamletConfig = StreamletDefinition.read(config).get "the loaded instances must contain class, instance and port information" in { val expectedStreamlet = ("sensor-data", "cloudflow.examples.sensordata.SensorDataIngress$") streamletConfig.streamletRef must be(expectedStreamlet._1) streamletConfig.streamletClass must be(expectedStreamlet._2) } "a loaded instance must have port configuration" in { val ports = streamletConfig.portMappings val expectedPorts = Map( "accepted" -> Topic("accepted"), "rejected" -> Topic("rejected") ) ports.foreach(portMapping ⇒ expectedPorts(portMapping.port) must be(portMapping.topic)) } "a loaded instance must have its own configuration" in { val config = streamletConfig.config config.getInt("cloudflow.internal.server.container-port") must be(2049) } "a loaded instance must have the common configuration" in { config.getString("cloudflow.common.attribute") must be("value") config.getString("cloudflow.kafka.bootstrap-servers") must be("cloudflow-kafka.lightbend:9092") } "a loaded instance must not have runner configuration" in { val config = streamletConfig.config config.hasPath("runner") must be(false) } } }
Example 41
Source File: CallStatsAggregatorSpec.scala From cloudflow with Apache License 2.0 | 5 votes |
package carly.aggregator import java.time.Instant import carly.data._ import cloudflow.spark.testkit._ import cloudflow.spark.sql.SQLImplicits._ import org.scalatest.OptionValues class CallStatsAggregatorSpec extends SparkScalaTestSupport with OptionValues { val streamlet = new CallStatsAggregator() val testKit = SparkStreamletTestkit(session).withConfigParameterValues(ConfigParameterValue(streamlet.GroupByWindow, "1 minute"), ConfigParameterValue(streamlet.Watermark, "1 minute")) "CallStatsAggregator" should { "produce elements to its outlet" in { // setup inlet tap on inlet port val in = testKit.inletAsTap[CallRecord](streamlet.in) // setup outlet tap on outlet port val out = testKit.outletAsTap[AggregatedCallStats](streamlet.out) val ts = Instant.now.toEpochMilli / 1000 val crs = (1 to 10).toList.map { i ⇒ CallRecord( s"user-1", s"user-2", (if (i % 2 == 0) "incoming" else "outgoing"), i * 10, ts ) } in.addData(crs) val run = testKit.run(streamlet, Seq(in), Seq(out)) // get data from outlet tap val results = out.asCollection(session) // assert val aggregate = results.headOption.value aggregate.totalCallDuration must be(550) aggregate.avgCallDuration must (be > 54.9 and be < 55.1) run.totalRows must be > 0L } } }
Example 42
Source File: CaseClassUnitTests.scala From scala-cass with MIT License | 5 votes |
package com.weather.scalacass import com.weather.scalacass.util.CassandraWithTableTester import syntax._ import org.scalatest.OptionValues class CaseClassUnitTests extends CassandraWithTableTester("testDB", "testTable", List("str varchar", "str2 ascii", "b blob", "d decimal", "f float", "net inet", "tid timeuuid", "vi varint", "i int", "bi bigint", "bool boolean", "dub double", "l list<varchar>", "m map<varchar, bigint>", "s set<double>", "ts timestamp", "id uuid", "sblob set<blob>"), List("str")) with OptionValues { case class Everything(str: String, d: BigDecimal, f: Float, net: java.net.InetAddress, l: Option[List[String]]) case class Everything2(str2: String, d: BigDecimal, f: Float, net: java.net.InetAddress, l: Option[List[String]]) "case class with Everything" should "materialize" in { val e = Everything("asdf", BigDecimal(0), 12.0f, java.net.InetAddress.getByName("localhost"), None) val e2 = Everything2(e.str, e.d, e.f, e.net, e.l) insert(Seq(("str", e.str), ("d", e.d.underlying), ("f", Float.box(e.f)), ("net", e.net))) getOne.as[Everything] shouldBe e getOne.getAs[Everything] shouldBe Some(e) getOne.getOrElse(e.copy(str = "asdf2")) shouldBe e getOne.getOrElse(e2) shouldBe e2 } }
Example 43
Source File: UpdateBehaviorTests.scala From scala-cass with MIT License | 5 votes |
package com.weather.scalacass import com.datastax.driver.core.ResultSet import com.weather.scalacass.ScalaSession.UpdateBehavior import com.weather.scalacass.util.CassandraWithTableTester import org.scalatest.OptionValues import com.weather.scalacass.syntax._ object UpdateBehaviorTests { val db = "testDB" val table = "testTable" } class UpdateBehaviorTests extends CassandraWithTableTester(UpdateBehaviorTests.db, UpdateBehaviorTests.table, List("str varchar", "l list<varchar>", "s set<double>"), List("str")) with OptionValues { import UpdateBehaviorTests.table lazy val ss = ScalaSession(UpdateBehaviorTests.db)(client.session) case class Query(str: String) case class Insert(str: String, l: List[String], s: Set[Double]) val baseStr = "some item" val base = Insert(baseStr, List("asdf"), Set(1.0)) val baseQuery = Query(baseStr) def insertOne(i: Insert = base): Result[ResultSet] = ss.insert(table, i).execute() "explicit replacement" should "act as before" in { case class Replacing(l: UpdateBehavior.Replace[List, String], s: UpdateBehavior.Replace[Set, Double]) val instance = Replacing(List("fdsa"), Set(2.0)) insertOne() ss.update(table, instance, baseQuery).execute() val res = ss.selectOneStar(table, baseQuery).execute().right.toOption.flatten.value.as[Insert] res.str shouldBe baseStr res.l should contain theSameElementsAs instance.l.coll res.s should contain theSameElementsAs instance.s.coll } "implicit replacement" should "also act as before" in { case class ReplacingImplicit(l: List[String], s: Set[Double]) val instance = ReplacingImplicit(List("fafa"), Set(3.0)) insertOne() ss.update(table, instance, baseQuery).execute() val res = ss.selectOneStar(table, baseQuery).execute().right.toOption.flatten.value.as[Insert] res.str shouldBe baseStr res.l should contain theSameElementsAs instance.l res.s should contain theSameElementsAs instance.s } "add" should "combine the two entries" in { case class Adding(l: UpdateBehavior.Add[List, String], s: UpdateBehavior.Add[Set, Double]) val instance = Adding(List("afaf"), Set(4.0)) insertOne() ss.update(table, instance, baseQuery).execute() val res = ss.selectOneStar(table, baseQuery).execute().right.toOption.flatten.value.as[Insert] res.str shouldBe baseStr res.l should contain theSameElementsAs base.l ::: instance.l.coll res.s should contain theSameElementsAs base.s ++ instance.s.coll } "subtract" should "subtract from the original entry" in { case class Subtracting(l: UpdateBehavior.Subtract[List, String], s: UpdateBehavior.Subtract[Set, Double]) val instance = Subtracting(List("another str"), Set(5.0)) val expandedBase = base.copy(l = instance.l.coll ::: base.l, s = instance.s.coll ++ base.s) insertOne(expandedBase) val preres = ss.selectOneStar(table, baseQuery).execute().right.toOption.flatten.value.as[Insert] preres.str shouldBe baseStr preres.l should contain theSameElementsAs expandedBase.l preres.s should contain theSameElementsAs expandedBase.s ss.update(table, instance, baseQuery).execute() val res = ss.selectOneStar(table, baseQuery).execute().right.toOption.flatten.value.as[Insert] res.str shouldBe baseStr res.l should contain theSameElementsAs base.l res.s should contain theSameElementsAs base.s } }
Example 44
Source File: ConsistencyLevelUnitTest.scala From scala-cass with MIT License | 5 votes |
package com.weather.scalacass import com.datastax.driver.core.ConsistencyLevel import com.weather.scalacass.scsession.{ SCBatchStatement, SCStatement }, SCStatement.RightBiasedEither import com.weather.scalacass.util.CassandraWithTableTester import org.scalatest.{ Assertion, OptionValues } object ConsistencyLevelUnitTest { val db = "actionsdb" val table = "actionstable" } class ConsistencyLevelUnitTest extends CassandraWithTableTester(ConsistencyLevelUnitTest.db, ConsistencyLevelUnitTest.table, List("str varchar", "otherstr varchar", "d double"), List("str")) with OptionValues { import ConsistencyLevelUnitTest.{ db, table } lazy val ss = ScalaSession(ConsistencyLevelUnitTest.db)(client.session) case class Query(str: String) case class Insert(str: String, otherstr: String, d: Double) case class Update(otherstr: String, d: Double) val insertValue = Insert("str", "otherstr", 1234.0) val queryValue = Query(insertValue.str) val updateValue = Update("updatedStr", 4321.0) def checkConsistency[T <: SCStatement[_]](statement: T, clOpt: Option[ConsistencyLevel]): Assertion = { clOpt match { case Some(cl) => statement.toString should include(s"<CONSISTENCY $cl>") case None => statement.toString should not include "<CONSISTENCY" } val bound = statement.prepareAndBind().right.value bound.preparedStatement.getConsistencyLevel shouldBe clOpt.orNull } def fullCheck[T <: SCStatement[_]](statement: T)(plusConsistency: (T, ConsistencyLevel) => T, minusConsistency: T => T, cl: ConsistencyLevel): Assertion = { val statementWithConsistency = plusConsistency(statement, cl) val statementWithNoConsistency = minusConsistency(statement) checkConsistency(statement, None) checkConsistency(statementWithConsistency, Some(cl)) checkConsistency(statement, None) checkConsistency(statementWithConsistency, Some(cl)) checkConsistency(statementWithNoConsistency, None) } "setting consistency" should "work with inserts" in { fullCheck(ss.insert(table, insertValue))(_ consistency _, _.defaultConsistency, ConsistencyLevel.ONE) } it should "work with updates" in { fullCheck(ss.update(table, updateValue, queryValue))(_ consistency _, _.defaultConsistency, ConsistencyLevel.LOCAL_ONE) } it should "work with selects" in { fullCheck(ss.selectStar(table, queryValue))(_ consistency _, _.defaultConsistency, ConsistencyLevel.SERIAL) fullCheck(ss.select[Update](table, queryValue))(_ consistency _, _.defaultConsistency, ConsistencyLevel.SERIAL) fullCheck(ss.selectOneStar(table, queryValue))(_ consistency _, _.defaultConsistency, ConsistencyLevel.SERIAL) fullCheck(ss.selectOne[Update](table, queryValue))(_ consistency _, _.defaultConsistency, ConsistencyLevel.SERIAL) } it should "work with deletes" in { fullCheck(ss.deleteRow(table, queryValue))(_ consistency _, _.defaultConsistency, ConsistencyLevel.ANY) } it should "work with raw" in { fullCheck(ss.rawStatement(s"INSERT INTO $db.$table (str, otherstr, d) VALUES (?, ?, ?)"))(_ consistency _, _.defaultConsistency, ConsistencyLevel.LOCAL_QUORUM) fullCheck(ss.rawSelectOne(s"SELECT * FROM $db.$table WHERE str=? LIMIT 1"))(_ consistency _, _.defaultConsistency, ConsistencyLevel.LOCAL_SERIAL) fullCheck(ss.rawSelect(s"SELECT * FROM $db.$table WHERE str=?"))(_ consistency _, _.defaultConsistency, ConsistencyLevel.LOCAL_SERIAL) } it should "work with batches" in { def checkConsistencyBatch(statement: SCBatchStatement, clOpt: Option[ConsistencyLevel]): Assertion = { clOpt match { case Some(cl) => statement.toString should include(s"<CONSISTENCY $cl>") case None => statement.toString should not include "<CONSISTENCY" } val bound = statement.mkBatch.right.value bound.getSerialConsistencyLevel shouldBe clOpt.getOrElse(cluster.getConfiguration.getQueryOptions.getSerialConsistencyLevel) } val statement = ss.batchOf(ss.insert(table, insertValue)) val statementWithConsistency = statement.consistency(ConsistencyLevel.LOCAL_SERIAL) val statementWithNoConsistency = statementWithConsistency.defaultConsistency checkConsistencyBatch(statement, None) checkConsistencyBatch(statementWithConsistency, Some(ConsistencyLevel.LOCAL_SERIAL)) checkConsistencyBatch(statement, None) checkConsistencyBatch(statementWithConsistency.consistency(ConsistencyLevel.SERIAL), Some(ConsistencyLevel.SERIAL)) checkConsistencyBatch(statementWithNoConsistency, None) } }
Example 45
Source File: LinearClassifierTest.scala From doddle-model with Apache License 2.0 | 5 votes |
package io.picnicml.doddlemodel.linear import breeze.linalg.{DenseMatrix, DenseVector} import breeze.numerics.sigmoid import cats.syntax.option._ import io.picnicml.doddlemodel.TestingUtils import io.picnicml.doddlemodel.data.{Features, RealVector, Simplex, Target} import io.picnicml.doddlemodel.linear.typeclasses.LinearClassifier import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers case class DummyLinearClassifier(numClasses: Option[Int], w: Option[RealVector]) class LinearClassifierTest extends AnyFlatSpec with Matchers with OptionValues with TestingUtils { val ev: LinearClassifier[DummyLinearClassifier] = new LinearClassifier[DummyLinearClassifier] { override def numClasses(model: DummyLinearClassifier): Option[Int] = model.numClasses override protected def w(model: DummyLinearClassifier): Option[RealVector] = model.w override protected[doddlemodel] def copy(model: DummyLinearClassifier, numClasses: Int): DummyLinearClassifier = model.copy(numClasses = numClasses.some) override protected def copy(model: DummyLinearClassifier, w: RealVector): DummyLinearClassifier = model.copy(w = w.some) override protected def predictStateless(model: DummyLinearClassifier, w: RealVector, x: Features): Target = x * w override protected def predictProbaStateless(model: DummyLinearClassifier, w: RealVector, x: Features): Simplex = sigmoid(x * w).asDenseMatrix.t override protected[linear] def lossStateless(model: DummyLinearClassifier, w: RealVector, x: Features, y: Target): Float = 0.0f override protected[linear] def lossGradStateless(model: DummyLinearClassifier, w: RealVector, x: Features, y: Target): RealVector = w } private val x = DenseMatrix.rand[Float](10, 5, rand = randomUniform) private val y = DenseVector.vertcat(DenseVector.zeros[Float](5), DenseVector.ones[Float](5)) private val model = DummyLinearClassifier(none, none) "Linear classifier" should "throw an exception when using fit, predict on trained, untrained models" in { an [IllegalArgumentException] should be thrownBy ev.predict(model, x) val trainedModel = ev.fit(model, x, y) an [IllegalArgumentException] should be thrownBy ev.fit(trainedModel, x, y) } it should "implement predictor functions" in { ev.isFitted(model) shouldBe false val trainedModel = ev.fit(model, x, y) ev.isFitted(trainedModel) shouldBe true val yPred = ev.predict(trainedModel, x) yPred.length shouldEqual y.length } it should "set the number of classes after fit" in { ev.numClasses(model).isEmpty shouldBe true val trainedModel = ev.fit(model, x, y) ev.numClasses(trainedModel).value shouldBe 2 } it should "throw an exception if fitting a model with an invalid target variable" in { val invalidCategoricalY = DenseVector.zeros[Float](10) an [IllegalArgumentException] should be thrownBy ev.fit(model, x, invalidCategoricalY) val invalidRealY = DenseVector.rand[Float](10, rand = randomUniform) an [IllegalArgumentException] should be thrownBy ev.fit(model, x, invalidRealY) } }
Example 46
Source File: MostFrequentClassifierTest.scala From doddle-model with Apache License 2.0 | 5 votes |
package io.picnicml.doddlemodel.dummy.classification import breeze.linalg.sum import io.picnicml.doddlemodel.data.{loadBreastCancerDataset, loadIrisDataset} import io.picnicml.doddlemodel.dummy.classification.MostFrequentClassifier.ev import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class MostFrequentClassifierTest extends AnyFlatSpec with Matchers with OptionValues { "Most frequent classifier" should "infer the most frequent class from the iris dataset" in { val (x, y, _) = loadIrisDataset val model = MostFrequentClassifier() val trainedModel = ev.fit(model, x, y) trainedModel.mostFrequentClass.value shouldBe 0.0 sum(ev.predict(trainedModel, x)) shouldBe 0.0 } it should "infer the most frequent class from the breast cancer dataset" in { val (x, y, _) = loadBreastCancerDataset val model = MostFrequentClassifier() val trainedModel = ev.fit(model, x, y) trainedModel.mostFrequentClass.value shouldBe 1.0 sum(ev.predict(trainedModel, x)) shouldBe x.rows.toDouble } }
Example 47
Source File: RefreshTokenGrantHandlerSpec.scala From tsec with MIT License | 5 votes |
package tsec.oauth2.provider import java.time.Instant import cats.effect.IO import cats.syntax.either._ import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.Matchers._ import tsec.oauth2.provider.ValidatedRequest.ValidatedRefreshToken import tsec.oauth2.provider.grantHandler.RefreshTokenGrantHandler import tsec.oauth2.provider.grantHandler.RefreshTokenHandler import scala.concurrent.duration._ class RefreshTokenGrantHandlerSpec extends AnyFlatSpec with OptionValues { it should "handle request" in { val dataHandler = new RefreshTokenHandler[IO, MockUser] { override def validateClient(request: ValidatedRefreshToken): IO[Boolean] = IO.pure(true) override def findAuthInfoByRefreshToken(refreshToken: String): IO[Option[AuthInfo[MockUser]]] = IO.pure( Some( AuthInfo( user = MockUser(10000, "username"), clientId = Some("clientId1"), scope = None, redirectUri = None ) ) ) override def refreshAccessToken(authInfo: AuthInfo[MockUser], refreshToken: String): IO[AccessToken] = IO.pure(AccessToken("token1", Some(refreshToken), None, Some(3600 seconds), Instant.now())) override def createAccessToken(authInfo: AuthInfo[MockUser]): IO[AccessToken] = ??? override def getStoredAccessToken(authInfo: AuthInfo[MockUser]): IO[Option[AccessToken]] = ??? } val handler = new RefreshTokenGrantHandler[IO, MockUser](dataHandler) val f = handler.handleRequest( ValidatedRefreshToken(ClientCredential("clientId1", Some("ClientSecret1")), "refreshToken1", Some("all")) ) val result = f.value.unsafeRunSync().toOption.get result.tokenType should be("Bearer") result.accessToken should be("token1") result.expiresIn.value.toMillis should (be <= 3600L and be > 3595L) result.refreshToken should be(Some("refreshToken1")) result.scope should be(None) } }
Example 48
Source File: IndexingConfigSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.sourcing.projections import java.io.File import akka.actor.ActorSystem import akka.testkit.TestKit import ch.epfl.bluebrain.nexus.sourcing.RetryStrategyConfig import ch.epfl.bluebrain.nexus.sourcing.projections.IndexingConfig.PersistProgressConfig import com.typesafe.config.ConfigFactory import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import pureconfig.generic.auto._ import pureconfig.ConfigSource import scala.concurrent.duration._ class IndexingConfigSpec extends TestKit(ActorSystem("IndexingConfigSpec")) with AnyWordSpecLike with Matchers with OptionValues { val config = IndexingConfig( 10, 40.millis, RetryStrategyConfig("exponential", 100.milliseconds, 10.hours, 7, 5.seconds), PersistProgressConfig(1000, 5.seconds) ) "IndexingConfig" should { "read from config file" in { val readConfig = ConfigFactory.parseFile(new File(getClass.getResource("/example-indexing.conf").toURI)) ConfigSource.fromConfig(readConfig).loadOrThrow[IndexingConfig] shouldEqual config } } }
Example 49
Source File: WsProxySpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.http.ws import com.github.tomakehurst.wiremock.client.VerificationException import com.github.tomakehurst.wiremock.client.WireMock._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{BeforeAndAfterAll, OptionValues} import org.scalatestplus.mockito.MockitoSugar import play.api.Play import play.api.libs.ws.WSProxyServer import play.api.test.FakeApplication import uk.gov.hmrc.http.HeaderCarrier class WsProxySpec extends AnyWordSpecLike with Matchers with MockitoSugar with OptionValues with BeforeAndAfterAll { implicit val hc = HeaderCarrier() lazy val fakeApplication = FakeApplication() "A proxied get request" should { "correctly make a request via the specified proxy server" in new Setup { val wSProxyServer = mock[WSProxyServer] object ProxiedGet extends WSProxy { override def applicableHeaders(url: String)(implicit hc: HeaderCarrier): Seq[(String, String)] = Nil def wsProxyServer = Some(wSProxyServer) } val request = ProxiedGet.buildRequest("http://example.com", headers = Seq.empty) request.proxyServer.value shouldBe (wSProxyServer) } } "A proxied get request, without a defined proxy configuration, i.e. for use in environments where a proxy does not exist" should { "still work by making the request without using a proxy server" in new Setup { object ProxiedGet extends WSProxy { override def applicableHeaders(url: String)(implicit hc: HeaderCarrier): Seq[(String, String)] = Nil def wsProxyServer = None } val request = ProxiedGet.buildRequest("http://example.com", headers = Seq.empty) request.proxyServer shouldBe (None) } } class Setup extends WireMockEndpoints { val responseData = "ResourceABC" val resourcePath = s"/resource/abc" def endpointBaseUrl = s"http://localhost:$endpointPort" def fullResourceUrl = s"$endpointBaseUrl$resourcePath" def setupEndpointExpectations() { endpointMock.register( get(urlEqualTo(resourcePath)) .willReturn( aResponse() .withHeader("Content-Type", "text/plain") .withBody(responseData))) proxyMock.register( get(urlMatching(resourcePath)) .willReturn(aResponse().proxiedFrom(endpointBaseUrl))) } def assertEndpointWasCalled() { endpointMock.verifyThat(getRequestedFor(urlEqualTo(resourcePath))) } def assertCallViaProxy() { endpointMock.verifyThat(getRequestedFor(urlEqualTo(resourcePath))) } def assertProxyNotUsed() { a[VerificationException] should be thrownBy proxyMock.verifyThat(getRequestedFor(urlEqualTo(resourcePath))) } } override def beforeAll() { super.beforeAll() Play.start(fakeApplication) } override def afterAll() { super.afterAll() Play.stop(fakeApplication) } }
Example 50
Source File: OriginSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.binders import org.scalatest.{EitherValues, Matchers, OptionValues, WordSpecLike} import uk.gov.hmrc.play.binders.Origin._ class OriginSpec extends WordSpecLike with Matchers with EitherValues with OptionValues { "Origin" should { "be valid" in { Origin("testing1").origin shouldBe "testing1" Origin("Testing1").origin shouldBe "Testing1" Origin("test-ing1").origin shouldBe "test-ing1" Origin("tesA.ing1").origin shouldBe "tesA.ing1" Origin(List.fill(100)('0').mkString).origin shouldBe List.fill(100)('0').mkString } "be invalid" in { an[IllegalArgumentException] should be thrownBy Origin("withInvalidCharacters!") an[IllegalArgumentException] should be thrownBy Origin("with white spaces") an[IllegalArgumentException] should be thrownBy Origin("") an[IllegalArgumentException] should be thrownBy Origin(List.fill(101)('0').mkString) } } "Origin binder" should { "default when origin has invalid characters" in { queryBinder.bind("origin", Map("origin" -> Seq("!asdasd"))).value.right.value should be(Origin("unknown")) } "default when no origin supplied" in { queryBinder.bind("origin", Map("origin" -> Seq.empty)).value.right.value should be(Origin("unknown")) } "take the first when two origins supplied" in { queryBinder.bind("origin", Map("origin" -> Seq("origin1", "origin2"))).value.right.value should be( Origin("origin1")) } "create origin" in { queryBinder.bind("origin", Map("origin" -> Seq("theOrigin"))).value.right.value should be(Origin("theOrigin")) } } "Unbinding a continue URL" should { "return the value" in { queryBinder.unbind("origin", Origin("tax-account-router")) should be("origin=tax-account-router") } } }
Example 51
Source File: ContinueUrlSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.binders import org.scalatest.{EitherValues, Matchers, OptionValues, WordSpecLike} import uk.gov.hmrc.play.binders.ContinueUrl._ class ContinueUrlSpec extends WordSpecLike with Matchers with EitherValues with OptionValues { "isAbsoluteUrl" should { "return true for an absolute URL" in { ContinueUrl("http://www.example.com").isAbsoluteUrl shouldBe true } "return false for a relative URL" in { ContinueUrl("/service/page").isAbsoluteUrl shouldBe false } } "isRelativeUrl" should { "return false for an absolute URL" in { ContinueUrl("http://www.example.com").isRelativeUrl shouldBe false } "return true for a relative URL" in { ContinueUrl("/service/page").isRelativeUrl shouldBe true } } "not work for protocol-relative urls" in { an[IllegalArgumentException] should be thrownBy ContinueUrl("//some/value?with=query") an[IllegalArgumentException] should be thrownBy ContinueUrl("///some/value?with=query") an[IllegalArgumentException] should be thrownBy ContinueUrl("////some/value?with=query") } "not work for urls with @" in { an[IllegalArgumentException] should be thrownBy ContinueUrl("/some/value?with=query@meh") } "not work for urls with /\\" in { an[IllegalArgumentException] should be thrownBy ContinueUrl("/\\www.example.com") } "not work for path-relative urls" in { an[IllegalArgumentException] should be thrownBy ContinueUrl("some/value?with=query") } "not work for non-urls" in { an[IllegalArgumentException] should be thrownBy ContinueUrl("someasdfasdfa") } "encodedUrl should produce the expected result" in { ContinueUrl("/some/value?with=query").encodedUrl shouldBe "%2Fsome%2Fvalue%3Fwith%3Dquery" } "Binding a continue URL" should { "work for host-relative URLs" in { val url = "/some/value" queryBinder.bind("continue", Map("continue" -> Seq(url))).value.right.value should be(ContinueUrl(url)) } "work for host-relative URLs with query Params" in { val url = "/some/value?with=query" queryBinder.bind("continue", Map("continue" -> Seq(url))).value.right.value should be(ContinueUrl(url)) } "not work for path-relative urls" in { val url = "some/value?with=query" queryBinder.bind("continue", Map("continue" -> Seq(url))).value.left.value should be( s"'$url' is not a valid continue URL") } "not work for non-urls" in { val url = "::" queryBinder.bind("continue", Map("continue" -> Seq(url))).value.left.value should be( s"'$url' is not a valid continue URL") } } "Unbinding a continue URL" should { "return the value" in { queryBinder.unbind("continue", ContinueUrl("/some/url")) should be("continue=%2Fsome%2Furl") } } }
Example 52
Source File: ParameterSpec.scala From hyperion with Apache License 2.0 | 5 votes |
package com.krux.hyperion.expression import java.time.ZonedDateTime import org.scalatest.{ OptionValues, WordSpec } import com.krux.hyperion.aws.AdpParameter class ParameterSpec extends WordSpec with OptionValues { "Parameter[ZonedDateTime]" should { "be serialized in the correct datetime format" in { implicit val values = new ParameterValues() val dateTimeParam = Parameter[ZonedDateTime]("datetime", ZonedDateTime.parse("2014-04-02T00:00:00Z")) assert(dateTimeParam.serialize.value === AdpParameter( id = "my_datetime", `default` = Some("2014-04-02T00:00:00") )) } } }
Example 53
Source File: PriorityIssueTest.scala From circe-magnolia with Apache License 2.0 | 5 votes |
package io.circe.magnolia.incompat import io.circe.{Encoder, Json} import io.circe.magnolia.CirceMagnoliaSuite import io.circe.magnolia.derivation.encoder.auto._ import org.scalatest.OptionValues case class MapContainer(theMap: Map[String, List[Int]]) class PriorityIssueTest extends CirceMagnoliaSuite with OptionValues { // todo: uncomment when https://github.com/propensive/magnolia/issues/89 is fixed "Circe Magnolia Encoder" should "use instances from companion even if they are not imported" ignore { val encoder = Encoder[MapContainer] val json = encoder(MapContainer(Map("f" -> List(1, 2, 3)))) json.hcursor.downField("theMap").downField("f").focus.value shouldBe Json.arr(Json.fromInt(1), Json.fromInt(2), Json.fromInt(3)) } "Circe Magnolia Encoder" should "use instances from companion if they are explicitly imported" in { import Encoder._ val encoder = Encoder[MapContainer] val json = encoder(MapContainer(Map("f" -> List(1, 2, 3)))) json.hcursor.downField("theMap").downField("f").focus.value shouldBe Json.arr(Json.fromInt(1), Json.fromInt(2), Json.fromInt(3)) } }
Example 54
Source File: CloudFrontSignerTests.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.s3 import akka.http.scaladsl.model.Uri.Path import com.typesafe.config.ConfigFactory import java.time.Instant import org.apache.openwhisk.core.ConfigKeys import org.apache.openwhisk.core.database.s3.S3AttachmentStoreProvider.S3Config import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatest.{FlatSpec, Matchers, OptionValues} import pureconfig._ import pureconfig.generic.auto._ @RunWith(classOf[JUnitRunner]) class CloudFrontSignerTests extends FlatSpec with Matchers with OptionValues { val qt = "\"\"\"" val privateKey = """-----BEGIN RSA PRIVATE KEY----- |MIIBPAIBAAJBAOY+Q7vyH1SnCUoFIpzqmZe1TNCxiE6zuiMRmjuJqiAzQWdb5hEA |ZaC+f7Lcu53IvczZR0KsP4JndzG23rVg/y0CAwEAAQJBAMK+F3x4ppdrUSgSf9xJ |cfAnoPlDsA8hZWcUFGgXYJYqKYw3NqoYG5fwyZ7xrwdMhpbdgD++nsBC/JMwUhEB |h+ECIQDzj5Tbd7WvfaKGjozwQgHA9u3f53kxCWovpFEngU6VNwIhAPIAkAPnzuDr |q3cEyAbM49ozjyc6/NOV6QK65HQj1gC7AiBrax/Ty3At/dL4VVaDgBkV6dHvtj8V |CXnzmRzRt43Y8QIhAIzrvPE5RGP/eEqHUz96glhm276Zf+5qBlTbpfrnf0/PAiEA |r1vFsvC8+KSHv7XGU1xfeiHHpHxEfDvJlX7/CxeWumQ= |-----END RSA PRIVATE KEY----- |""".stripMargin val keyPairId = "OPENWHISKISFUNTOUSE" val configString = s"""whisk { | s3 { | bucket = "openwhisk-test" | prefix = "dev" | cloud-front-config { | domain-name = "foo.com" | key-pair-id = "$keyPairId" | private-key = $qt$privateKey$qt | timeout = 10 m | } | } |}""".stripMargin behavior of "CloudFront config" it should "generate a signed url" in { val config = ConfigFactory.parseString(configString).withFallback(ConfigFactory.load()) val s3Config = loadConfigOrThrow[S3Config](config, ConfigKeys.s3) val signer = CloudFrontSigner(s3Config.cloudFrontConfig.get) val expiration = Instant.now().plusSeconds(s3Config.cloudFrontConfig.get.timeout.toSeconds) val uri = signer.getSignedURL("bar") val query = uri.query() //A signed url is of format //https://<domain-name>/<object key>?Expires=xxx&Signature=xxx&Key-Pair-Id=xxx uri.scheme shouldBe "https" uri.path.tail shouldBe Path("bar") query.get("Expires") shouldBe Some(expiration.getEpochSecond.toString) query.get("Signature") shouldBe defined query.get("Key-Pair-Id").value shouldBe keyPairId } }
Example 55
Source File: CosmosDBUtilTest.scala From openwhisk with Apache License 2.0 | 5 votes |
package org.apache.openwhisk.core.database.cosmosdb import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatest.{FlatSpec, Matchers, OptionValues} import spray.json._ import org.apache.openwhisk.utils.JsHelpers @RunWith(classOf[JUnitRunner]) class CosmosDBUtilTest extends FlatSpec with Matchers with OptionValues { behavior of "prepare field" it should "always have id" in { val result = fieldsAsJson() val expected = """{"id" : "r['id']"}""".parseJson result shouldBe expected result.fields("id") shouldBe JsString("r['id']") } it should "build a json like string" in { val result = fieldsAsJson("a") val expected = """{ "a" : "r['a']", "id" : "r['id']"}""".parseJson result shouldBe expected result.fields("a") shouldBe JsString("r['a']") } it should "support nested fields" in { val result = fieldsAsJson("a", "b.c") val expected = """{ | "a": "r['a']", | "b": { | "c": "r['b']['c']" | }, | "id": "r['id']" |}""".stripMargin.parseJson result shouldBe expected JsHelpers.getFieldPath(result, "b", "c").value shouldBe JsString("r['b']['c']") } private def fieldsAsJson(fields: String*) = toJson(CosmosDBUtil.prepareFieldClause(fields.toSet)) private def toJson(s: String): JsObject = { //Strip of last `As VIEW` s.replace(" AS view", "") .replaceAll(raw"(r[\w'\[\]]+)", "\"$1\"") .parseJson .asJsObject } behavior of "escaping" it should "escape /" in { CosmosDBUtil.escapeId("foo/bar") shouldBe "foo|bar" } it should "throw exception when input contains replacement char |" in { an[IllegalArgumentException] should be thrownBy CosmosDBUtil.escapeId("foo/bar|baz") } it should "support unescaping" in { val s = "foo/bar" CosmosDBUtil.unescapeId(CosmosDBUtil.escapeId(s)) shouldBe s } }
Example 56
Source File: HttpMetricsSpec.scala From kamon-http4s with Apache License 2.0 | 5 votes |
package kamon.http4s import cats.effect._ import kamon.testkit.InstrumentInspection import org.http4s.HttpRoutes import org.http4s.dsl.io._ import org.http4s.server.Server import org.http4s.server.blaze.BlazeServerBuilder import org.scalatest.concurrent.Eventually import org.scalatest.time.SpanSugar import org.scalatest.{Matchers, OptionValues, WordSpec} import cats.implicits._ import kamon.http4s.middleware.server.KamonSupport import kamon.instrumentation.http.HttpServerMetrics import org.http4s.client.blaze.BlazeClientBuilder import org.http4s.client.Client import scala.concurrent.ExecutionContext import org.http4s.implicits._ class HttpMetricsSpec extends WordSpec with Matchers with Eventually with SpanSugar with InstrumentInspection.Syntax with OptionValues { implicit val contextShift: ContextShift[IO] = IO.contextShift(ExecutionContext.global) implicit val timer: Timer[IO] = IO.timer(ExecutionContext.global) val srv = BlazeServerBuilder[IO] .bindLocal(43567) .withHttpApp(KamonSupport(HttpRoutes.of[IO] { case GET -> Root / "tracing" / "ok" => Ok("ok") case GET -> Root / "tracing" / "not-found" => NotFound("not-found") case GET -> Root / "tracing" / "error" => InternalServerError("This page will generate an error!") }, "/127.0.0.1", 43567).orNotFound) .resource val client = BlazeClientBuilder[IO](ExecutionContext.global).withMaxTotalConnections(10).resource val metrics = Resource.liftF(IO(HttpServerMetrics.of("http4s.server", "/127.0.0.1", 43567))) def withServerAndClient[A](f: (Server[IO], Client[IO], HttpServerMetrics.HttpServerInstruments) => IO[A]): A = (srv, client, metrics).tupled.use(f.tupled).unsafeRunSync() private def get[F[_]: ConcurrentEffect](path: String)(server: Server[F], client: Client[F]): F[String] = { client.expect[String](s"http://127.0.0.1:${server.address.getPort}$path") } "The HttpMetrics" should { "track the total of active requests" in withServerAndClient { (server, client, serverMetrics) => val requests = List .fill(100) { get("/tracing/ok")(server, client) }.parSequence_ val test = IO { serverMetrics.activeRequests.distribution().max should be > 1L serverMetrics.activeRequests.distribution().min shouldBe 0L } requests *> test } "track the response time with status code 2xx" in withServerAndClient { (server, client, serverMetrics) => val requests: IO[Unit] = List.fill(100)(get("/tracing/ok")(server, client)).sequence_ val test = IO(serverMetrics.requestsSuccessful.value should be >= 0L) requests *> test } "track the response time with status code 4xx" in withServerAndClient { (server, client, serverMetrics) => val requests: IO[Unit] = List.fill(100)(get("/tracing/not-found")(server, client).attempt).sequence_ val test = IO(serverMetrics.requestsClientError.value should be >= 0L) requests *> test } "track the response time with status code 5xx" in withServerAndClient { (server, client, serverMetrics) => val requests: IO[Unit] = List.fill(100)(get("/tracing/error")(server, client).attempt).sequence_ val test = IO(serverMetrics.requestsServerError.value should be >= 0L) requests *> test } } }
Example 57
Source File: UnitSpec.scala From vat-api with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.vatapi import org.joda.time.{DateTime, DateTimeZone, LocalDate} import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.{AnyWordSpec, AsyncWordSpec} import play.api.test.{DefaultAwaitTimeout, FutureAwaits} import scala.concurrent.duration._ import scala.concurrent.{Await, Future} import scala.language.postfixOps trait BaseUnitSpec extends Matchers with OptionValues with TestUtils with FutureAwaits with DefaultAwaitTimeout { implicit val timeout: FiniteDuration = 5 seconds def await[T](f: Future[T])(implicit duration: FiniteDuration = timeout): T = Await.result(f, duration) } trait UnitSpec extends AnyWordSpec with BaseUnitSpec{ implicit def extractAwait[A](future: Future[A]): A = await[A](future) def await[A](future: Future[A])(implicit timeout: Duration): A = Await.result(future, timeout) } trait AsyncUnitSpec extends AsyncWordSpec with BaseUnitSpec trait TestUtils { private val vrnGenerator = VrnGenerator() def now: DateTime = DateTime.now(DateTimeZone.UTC) def generateVrn = vrnGenerator.nextVrn() implicit def toLocalDate(d: DateTime): LocalDate = d.toLocalDate } object TestUtils extends TestUtils
Example 58
Source File: ResourceSpec.scala From vat-api with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.vatapi.resources import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import play.api.http.{HeaderNames, MimeTypes, Status} import play.api.mvc.ControllerComponents import play.api.test.Helpers.stubControllerComponents import play.api.test.{DefaultAwaitTimeout, ResultExtractors} import uk.gov.hmrc.domain.Vrn import uk.gov.hmrc.vatapi.TestUtils import uk.gov.hmrc.vatapi.config.AppContext import uk.gov.hmrc.vatapi.mocks.auth.MockAuthorisationService trait ResourceSpec extends AnyWordSpec with Matchers with OptionValues with TestUtils with ResultExtractors with HeaderNames with Status with DefaultAwaitTimeout with MimeTypes with MockAuthorisationService { val vrn: Vrn = generateVrn val mockAppContext = mock[AppContext] lazy val cc: ControllerComponents = stubControllerComponents() def mockAuthAction(vrn: Vrn) = { MockAuthorisationService.authCheck(vrn) } def mockAuthActionWithNrs(vrn: Vrn) = { MockAuthorisationService.authCheckWithNrsRequirement(vrn) } }
Example 59
Source File: SourcingSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.sourcing import org.scalactic.source import org.scalatest.{Inspectors, OptionValues, TryValues} import org.scalatest.exceptions.{StackDepthException, TestFailedException} import org.scalatest.matchers.should.Matchers trait SourcingSpec extends org.scalatest.wordspec.AnyWordSpecLike with Matchers with Inspectors with OptionValues with TryValues { class EitherValuable[L, R](either: Either[L, R], pos: source.Position) { def rightValue: R = either match { case Right(value) => value case Left(_) => throw new TestFailedException( (_: StackDepthException) => Some("The Either value is not a Right(_)"), None, pos ) } def leftValue: L = either match { case Left(value) => value case Right(_) => throw new TestFailedException( (_: StackDepthException) => Some("The Either value is not a Left(_)"), None, pos ) } } implicit def convertEitherToValuable[L, R](either: Either[L, R])(implicit p: source.Position): EitherValuable[L, R] = new EitherValuable(either, p) }
Example 60
Source File: JsonFormatSpecJVM.scala From scalapb-circe with MIT License | 5 votes |
package scalapb_circe import org.scalatest.OptionValues import jsontest.test._ import com.google.protobuf.util.{JsonFormat => JavaJsonFormat} import com.google.protobuf.any.{Any => PBAny} import com.google.protobuf.util.JsonFormat.{TypeRegistry => JavaTypeRegistry} import scalapb_json._ import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.must.Matchers class JsonFormatSpecJVM extends AnyFlatSpec with Matchers with OptionValues { val TestProto = MyTest().update( _.hello := "Foo", _.foobar := 37, _.primitiveSequence := Seq("a", "b", "c"), _.repMessage := Seq(MyTest(), MyTest(hello = Some("h11"))), _.optMessage := MyTest().update(_.foobar := 39), _.stringToInt32 := Map("foo" -> 14, "bar" -> 19), _.intToMytest := Map(14 -> MyTest(), 35 -> MyTest(hello = Some("boo"))), _.repEnum := Seq(MyEnum.V1, MyEnum.V2, MyEnum.UNKNOWN), _.optEnum := MyEnum.V2, _.intToEnum := Map(32 -> MyEnum.V1, 35 -> MyEnum.V2), _.stringToBool := Map("ff" -> false, "tt" -> true), _.boolToString := Map(false -> "ff", true -> "tt"), _.optBool := false ) "fromJsonString" should "read json produced by Java" in { val javaJson = JavaJsonFormat.printer().print(MyTest.toJavaProto(TestProto)) JsonFormat.fromJsonString[MyTest](javaJson) must be(TestProto) } "Java parser" should "read json strings produced by us" in { val b = jsontest.Test.MyTest.newBuilder JavaJsonFormat.parser().merge(JsonFormat.toJsonString(TestProto), b) TestProto must be(MyTest.fromJavaProto(b.build)) } val anyEnabledJavaTypeRegistry = JavaTypeRegistry.newBuilder().add(TestProto.companion.javaDescriptor).build() val anyEnabledJavaPrinter = JavaJsonFormat.printer().usingTypeRegistry(anyEnabledJavaTypeRegistry) val anyEnabledTypeRegistry = TypeRegistry.empty.addMessageByCompanion(TestProto.companion) val anyEnabledParser = new Parser(typeRegistry = anyEnabledTypeRegistry) "Any" should "parse JSON produced by Java for a packed TestProto" in { val javaAny = com.google.protobuf.Any.pack(MyTest.toJavaProto(TestProto)) val javaJson = anyEnabledJavaPrinter.print(javaAny) anyEnabledParser.fromJsonString[PBAny](javaJson).unpack[MyTest] must be(TestProto) } }
Example 61
Source File: RedisSourceTest.scala From spark-redis with BSD 3-Clause "New" or "Revised" License | 5 votes |
package org.apache.spark.sql.redis.stream import org.scalatest.{FunSuite, Matchers, OptionValues} class RedisSourceTest extends FunSuite with Matchers with OptionValues { test("testGetOffsetRanges") { val startOffsets = RedisSourceOffset(Map("mystream" -> RedisConsumerOffset("group55", "0-0"))) val endOffsets = RedisSourceOffset(Map("mystream" -> RedisConsumerOffset("group55", "0-1"))) val consumerConfig = RedisConsumerConfig("mystream", "group55", "consumer", 1000, 100) val consumerConfigs = Seq(consumerConfig) val offsetRanges = RedisSource.getOffsetRanges(Some(startOffsets), endOffsets, consumerConfigs) offsetRanges.head shouldBe RedisSourceOffsetRange(Some("0-0"), "0-1", consumerConfig) } }
Example 62
Source File: LoggerSpec.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.mwz.sonar.scala.util import cats.effect.IO import com.mwz.sonar.scala.util.syntax.Optionals._ import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import org.scalatest.{LoneElement, OptionValues} import org.sonar.api.utils.log.LoggerLevel._ import org.sonar.api.utils.log.SonarLogTester class LoggerSpec extends AnyFlatSpec with Matchers with LoneElement with OptionValues with SonarLogTester { trait Context { val log: IO[Logger[IO]] = Logger.create(classOf[LoggerSpec], "test") } it should "log debug" in new Context { log.flatMap(_.debug("debug")).unsafeRunSync() logsFor(DEBUG) shouldBe Seq("[sonar-scala-test] debug") } it should "log info" in new Context { log.flatMap(_.info("info")).unsafeRunSync() logsFor(INFO) shouldBe Seq("[sonar-scala-test] info") } it should "log warn" in new Context { log.flatMap(_.warn("warn")).unsafeRunSync() logsFor(WARN) shouldBe Seq("[sonar-scala-test] warn") } it should "log error" in new Context { log.flatMap(_.error("error")).unsafeRunSync() logsFor(ERROR) shouldBe Seq("[sonar-scala-test] error") } it should "log error with a throwable" in new Context { log.flatMap(_.error("error", new Exception("cause"))).unsafeRunSync() val result = getLogsFor(ERROR).loneElement result.getFormattedMsg shouldBe "[sonar-scala-test] error" val exception = result.getArgs.toOption.value.loneElement exception shouldBe a[Exception] exception.asInstanceOf[Exception].getMessage shouldBe "cause" } it should "default the prefix to sonar-scala" in { val log: IO[Logger[IO]] = Logger.create(classOf[LoggerSpec]) log.flatMap(_.info("info")).unsafeRunSync() logsFor(INFO) shouldBe Seq("[sonar-scala] info") } }
Example 63
Source File: SonarFileSystemSpec.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.mwz.sonar.scala package util package syntax import java.nio.file.{Path, Paths} import cats.instances.list._ import cats.instances.option._ import com.mwz.sonar.scala.util.syntax.SonarFileSystem._ import org.mockito.ArgumentMatchers._ import org.mockito.Mockito._ import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import org.scalatestplus.mockito.MockitoSugar import org.sonar.api.batch.fs.FileSystem import org.sonar.api.batch.fs.internal.DefaultFileSystem class SonarFileSystemSpec extends AnyFlatSpec with Matchers with OptionValues with MockitoSugar { it should "attempt to resolve paths" in { val fs = new DefaultFileSystem(Paths.get("./")) val paths = List(Paths.get("path/1"), Paths.get("path/2")) fs.resolve(paths) shouldBe List( Paths.get("./").resolve("path/1").toAbsolutePath.normalize.toFile, Paths.get("./").resolve("path/2").toAbsolutePath.normalize.toFile ) val path: Option[Path] = Some(Paths.get("another/path")) fs.resolve(path).value shouldBe Paths.get("./").resolve("another/path").toAbsolutePath.normalize.toFile } it should "handle exceptions gracefully" in { val fs = mock[FileSystem] val path = List(Paths.get("path")) when(fs.resolvePath(any())).thenThrow(new RuntimeException()) fs.resolve(path) shouldBe empty } }
Example 64
Source File: OptionalsSpec.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.mwz.sonar.scala package util package syntax import java.util.Optional import Optionals._ import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class OptionalsSpec extends AnyFlatSpec with Matchers with OptionValues { it should "convert Java Optional to Scala Option" in { Optional.of("test").toOption.value shouldBe "test" Optional.empty[String].toOption shouldBe empty } it should "convert Scala Option to Java Optional" in { Some("test").toOptional shouldBe Optional.of("test") None.toOptional shouldBe Optional.empty } }
Example 65
Source File: SonarConfigSpec.scala From sonar-scala with GNU Lesser General Public License v3.0 | 5 votes |
package com.mwz.sonar.scala package util package syntax import java.nio.file.Paths import com.mwz.sonar.scala.util.syntax.SonarConfig._ import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import org.sonar.api.config.internal.MapSettings class SonarConfigSpec extends AnyFlatSpec with Matchers with OptionValues { "config" should "get paths" in { val conf = new MapSettings() .setProperty("path", "this/is/a/path, another/path") .asConfig() val defaultPaths = List(Paths.get("default/path"), Paths.get("default/path2")) conf.getPaths("path", defaultPaths) shouldBe List( Paths.get("this/is/a/path"), Paths.get("another/path") ) conf.getPaths("not.a.path", defaultPaths) shouldBe defaultPaths } it should "get a boolean" in { val conf = new MapSettings() .setProperty("bool.true", "true") .setProperty("bool.true2", "TRUE") .setProperty("bool.false", "false") .asConfig() conf.getAs[Boolean]("bool.true") shouldBe true conf.getAs[Boolean]("bool.true2") shouldBe true conf.getAs[Boolean]("bool.false") shouldBe false conf.getAs[Boolean]("not.a.bool") shouldBe false } it should "get a string" in { val conf = new MapSettings() .setProperty("text", "hello") .setProperty("number", "55") .setProperty("bool", "true") .asConfig() conf.getAs[String]("text").value shouldBe "hello" conf.getAs[String]("number").value shouldBe "55" conf.getAs[String]("bool").value shouldBe "true" conf.getAs[String]("empty") shouldBe empty } }
Example 66
Source File: FTracingSpec.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.doobie import cats.effect.{ContextShift, IO} import io.opencensus.scala.Tracing import io.opencensus.scala.http.testSuite.MockTracing import io.opencensus.trace.{BlankSpan, Status} import org.scalatest.{OptionValues, Outcome} import scala.concurrent.ExecutionContext.global import scala.util.Try import org.scalatest.flatspec import org.scalatest.matchers.should.Matchers class FTracingSpec extends flatspec.FixtureAnyFlatSpec with Matchers with OptionValues { implicit val cs: ContextShift[IO] = IO.contextShift(global) case class TestInput(fTracing: FTracing[IO], mock: MockTracing) override protected def withFixture(test: OneArgTest): Outcome = test(clientTracingWithMock()) override type FixtureParam = TestInput behavior of "FTracingSpec" it should "start with the correct name" in { f => f.fTracing.traceF(IO(()), "testSpan", None).unsafeRunSync() f.mock.startedSpans should have size 1 f.mock.startedSpans.head.name shouldBe "testSpan" } it should "trace with parent Span" in { f => val parentSpan = BlankSpan.INSTANCE f.fTracing.traceF(IO(()), "testSpan", Some(parentSpan)).unsafeRunSync() f.mock.startedSpans should have size 1 f.mock.startedSpans.head.parentContext.value shouldBe parentSpan.getContext } it should "stop after normal exit" in { f => f.fTracing.traceF(IO(()), "testSpan", None).unsafeRunSync() f.mock.endedSpans should have size 1 f.mock.endedSpans.head._2.value.getCanonicalCode shouldBe Status.OK.getCanonicalCode } it should "stop after error" in { f => Try( f.fTracing .traceF(IO.raiseError(new Exception("TEST")), "testSpan", None) .unsafeRunSync() ) f.mock.endedSpans should have size 1 f.mock.endedSpans.head._2.value.getCanonicalCode shouldBe Status.INTERNAL.getCanonicalCode } def clientTracingWithMock() = { val mockTracing = new MockTracing val fTracing = new FTracing[IO] { override protected val tracing: Tracing = mockTracing } TestInput(fTracing, mockTracing) } }
Example 67
Source File: TracingMiddlewareSpec.scala From opencensus-scala with Apache License 2.0 | 5 votes |
package io.opencensus.scala.http4s import cats.effect.IO import io.opencensus.scala.http.ServiceData import io.opencensus.scala.http4s.TracingService.{TracingService, withSpan} import io.opencensus.trace.Span import org.http4s._ import org.scalatest.OptionValues import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers class TracingMiddlewareSpec extends AnyFlatSpec with Matchers with OptionValues with ServiceRequirementsSpec { behavior of "TracingMiddleware" val ex = new Exception("test exception") val data = ServiceData("serviceX", "x.s.2") def tracingService( response: Span => IO[Response[IO]] = span => Ok(span.getContext.toString) ): TracingService[IO] = TracingService[IO] { case GET -> Root / "my" / "fancy" / "path" withSpan span => response(span) } def service(response: IO[Response[IO]] = Ok()): HttpRoutes[IO] = HttpRoutes.of[IO] { case GET -> Root / "my" / "fancy" / "path" => response } "tracing with span" should behave like testService( successServiceFromMiddleware = _.fromTracingService(tracingService()), failingServiceFromMiddleware = _.fromTracingService(tracingService(_ => IO.raiseError(ex))), badRequestServiceFromMiddleware = _.fromTracingService(tracingService(_ => BadRequest())), errorServiceFromMiddleware = _.fromTracingService(tracingService(_ => InternalServerError())) ) "tracing with span and with service data" should behave like testService( successServiceFromMiddleware = _.fromTracingService(tracingService(), data), failingServiceFromMiddleware = _.fromTracingService(tracingService(_ => IO.raiseError(ex)), data), badRequestServiceFromMiddleware = _.fromTracingService(tracingService(_ => BadRequest()), data), errorServiceFromMiddleware = _.fromTracingService(tracingService(_ => InternalServerError()), data), Some(data) ) it should "pass the span to the service" in { val (middleware, _) = middlewareWithMock() val responseBody = middleware .fromTracingService(tracingService()) .run(request) .value .flatMap(_.get.as[String]) .unsafeRunSync() responseBody should include("SpanContext") } "tracing without span" should behave like testService( successServiceFromMiddleware = _.withoutSpan(service()), failingServiceFromMiddleware = _.withoutSpan(service(IO.raiseError(ex))), badRequestServiceFromMiddleware = _.withoutSpan(service(BadRequest())), errorServiceFromMiddleware = _.withoutSpan(service(InternalServerError())) ) "tracing without span and with service data" should behave like testService( successServiceFromMiddleware = _.withoutSpan(service(), data), failingServiceFromMiddleware = _.withoutSpan(service(IO.raiseError(ex)), data), badRequestServiceFromMiddleware = _.withoutSpan(service(BadRequest()), data), errorServiceFromMiddleware = _.withoutSpan(service(InternalServerError()), data), Some(data) ) }
Example 68
Source File: ConfSpec.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.codegen.conf import java.nio.file.Paths import org.scalatest.{FlatSpec, Matchers, OptionValues} class ConfSpec extends FlatSpec with Matchers with OptionValues { behavior of "Conf.parse" it should "return error when no arguments are passed" in { Conf.parse(Array.empty) shouldBe empty } it should "return error when only inputs are passed" in { Conf.parse(Array("foo")) shouldBe empty } it should "return error when only output is passed" in { Conf.parse(Array("-o", "bar")) shouldBe empty } it should "return error when only inputs and decoder class are passed" in { Conf.parse(Array("-d", "package.ClassName", "input")) shouldBe empty } it should "return a Conf when input, output and a known backend are passed" in { Conf.parse(Array("-o", "output", "input")) shouldNot be(empty) } it should "return a Conf when input, output, a known backend and deocder FQCN are passed" in { Conf.parse(Array("-o", "output", "-d", "package.ClassName", "input")) shouldNot be(empty) } // XXX SC remove in Scala 2.13. aggregatingNatureOfGenTraversable is // mis-signed because it forces Map[K, V] to destructure as // TRAV[e] = Map[K, e], which is of course not <: GenTraversable[e]. And it's // needless, as proven below, just like the similar problem with // Future.traverse's sig import scala.collection.GenTraversable, org.scalatest.enablers.Aggregating private[this] implicit def `fixed sig aggregatingNatureOfGenTraversable`[ E: org.scalactic.Equality, TRAV]: Aggregating[TRAV with GenTraversable[E]] = Aggregating.aggregatingNatureOfGenTraversable[E, GenTraversable] it should "return a Conf with expected single unmapped input and output" in { val conf = Conf.parse(Array("-o", "output", "input")).value conf.darFiles should contain theSameElementsAs Map(Paths.get("input") -> None) } it should "return error when illegal Decoder class is passed" in { Conf.parse(Array("-o", "output", "-d", "$illegal")) shouldBe empty } it should "return a Conf with expected single mapped input, output and backend" in { val conf = Conf.parse(Array("-o", "output", "input=input.prefix")).value conf.darFiles should contain theSameElementsAs Map(Paths.get("input") -> Some("input.prefix")) } it should "return a Conf with expected multiple mapped inputs, output and backend" in { val conf = Conf .parse(Array("-o", "output", "input1=input1.prefix", "input2=input2.prefix")) .value conf.darFiles should contain theSameElementsAs Map( Paths.get("input1") -> Some("input1.prefix"), Paths.get("input2") -> Some("input2.prefix") ) } it should "return a Conf with expected multiple mixed inputs, output and backend" in { val conf = Conf.parse(Array("-o", "output", "input1=input1.prefix", "input2")).value conf.darFiles should contain theSameElementsAs Map( Paths.get("input1") -> Some("input1.prefix"), Paths.get("input2") -> None ) } }
Example 69
Source File: TemplateClassSpec.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.lf.codegen.backend.java.inner import java.util.Optional import com.daml.ledger.javaapi import com.squareup.javapoet.{ClassName, ParameterizedTypeName, TypeName} import javax.lang.model.element.Modifier import org.scalatest.{FlatSpec, Matchers, OptionValues, TryValues} import scala.collection.JavaConverters.iterableAsScalaIterableConverter final class TemplateClassSpec extends FlatSpec with Matchers with OptionValues with TryValues { behavior of "TemplateClass.generateFromIdAndRecord" it should "generate a public static method" in { fromIdAndRecord.modifiers.asScala should contain only (Modifier.STATIC, Modifier.PUBLIC) } it should "generate a method returning the class itself" in { fromIdAndRecord.returnType shouldEqual className } it should "generate a method taking the expected parameters (with contract key)" in { val parameters = fromIdAndRecord.parameters.asScala.map(p => p.name -> p.`type`).toList parameters should contain theSameElementsInOrderAs Seq( "contractId" -> string, "record$" -> record, "agreementText" -> optionalString, "key" -> optionalContractKey, "signatories" -> setOfStrings, "observers" -> setOfStrings ) } it should "generate a method taking the expected parameters (without contract key)" in { val parameters = fromIdAndRecordWithoutKey.parameters.asScala.map(p => p.name -> p.`type`).toList parameters should contain theSameElementsInOrderAs Seq( "contractId" -> string, "record$" -> record, "agreementText" -> optionalString, "signatories" -> setOfStrings, "observers" -> setOfStrings) } private[this] val className = ClassName.bestGuess("Test") private[this] val templateClassName = ClassName.bestGuess("Template") private[this] val idClassName = ClassName.bestGuess("Id") private[this] val ckClassName = ClassName.bestGuess("Ck") private[this] val fromIdAndRecord = TemplateClass.generateFromIdAndRecord( className, templateClassName, idClassName, Some(ckClassName)) private[this] val fromIdAndRecordWithoutKey = TemplateClass.generateFromIdAndRecord(className, templateClassName, idClassName, None) private[this] val string = TypeName.get(classOf[String]) private[this] val record = TypeName.get(classOf[javaapi.data.Record]) private[this] val optionalString = ParameterizedTypeName.get(classOf[Optional[_]], classOf[String]) private[this] val optionalContractKey = ParameterizedTypeName.get(ClassName.get(classOf[Optional[_]]), ckClassName) private[this] val setOfStrings = ParameterizedTypeName.get(classOf[java.util.Set[_]], classOf[String]) }
Example 70
Source File: TarFlowSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.storage import java.io.ByteArrayInputStream import java.nio.file.{Files, Path, Paths} import akka.actor.ActorSystem import akka.stream.alpakka.file.scaladsl.Directory import akka.stream.scaladsl.{FileIO, Source} import akka.testkit.TestKit import akka.util.ByteString import ch.epfl.bluebrain.nexus.storage.utils.{EitherValues, IOEitherValues, Randomness} import org.apache.commons.compress.archivers.tar.TarArchiveInputStream import org.apache.commons.io.FileUtils import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{BeforeAndAfterAll, Inspectors, OptionValues} import scala.annotation.tailrec class TarFlowSpec extends TestKit(ActorSystem("TarFlowSpec")) with AnyWordSpecLike with Matchers with IOEitherValues with Randomness with EitherValues with OptionValues with Inspectors with BeforeAndAfterAll { val basePath = Files.createTempDirectory("tarflow") val dir1 = basePath.resolve("one") val dir2 = basePath.resolve("two") override def afterAll(): Unit = { super.afterAll() FileUtils.cleanDirectory(basePath.toFile) () } type PathAndContent = (Path, String) "A TarFlow" should { Files.createDirectories(dir1) Files.createDirectories(dir2) def relativize(path: Path): String = basePath.getParent().relativize(path).toString "generate the byteString for a tar file correctly" in { val file1 = dir1.resolve("file1.txt") val file1Content = genString() val file2 = dir1.resolve("file3.txt") val file2Content = genString() val file3 = dir2.resolve("file3.txt") val file3Content = genString() val files = List(file1 -> file1Content, file2 -> file2Content, file3 -> file3Content) forAll(files) { case (file, content) => Source.single(ByteString(content)).runWith(FileIO.toPath(file)).futureValue } val byteString = Directory.walk(basePath).via(TarFlow.writer(basePath)).runReduce(_ ++ _).futureValue val bytes = new ByteArrayInputStream(byteString.toArray) val tar = new TarArchiveInputStream(bytes) @tailrec def readEntries( tar: TarArchiveInputStream, entries: List[PathAndContent] = Nil ): List[PathAndContent] = { val entry = tar.getNextTarEntry if (entry == null) entries else { val data = Array.ofDim[Byte](entry.getSize.toInt) tar.read(data) readEntries(tar, (Paths.get(entry.getName) -> ByteString(data).utf8String) :: entries) } } val directories = List(relativize(basePath) -> "", relativize(dir1) -> "", relativize(dir2) -> "") val untarred = readEntries(tar).map { case (path, content) => path.toString -> content } val expected = files.map { case (path, content) => relativize(path) -> content } ++ directories untarred should contain theSameElementsAs expected } } }
Example 71
Source File: AsyncCacheSpec.scala From scaffeine with Apache License 2.0 | 5 votes |
package com.github.blemale.scaffeine import org.scalatest.OptionValues import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import scala.concurrent.Future class AsyncCacheSpec extends AnyWordSpec with Matchers with ScalaFutures with OptionValues { "AsyncCache" should { "get value if present" in { val cache = Scaffeine().buildAsync[String, String]() cache.put("foo", Future.successful("present")) val fooValue = cache.getIfPresent("foo") val barValue = cache.getIfPresent("bar") fooValue.value.futureValue should be("present") barValue should be(None) } "get or compute value" in { val cache = Scaffeine().buildAsync[String, String]() cache.put("foo", Future.successful("present")) val fooValue = cache.get("foo", _ => "computed") val barValue = cache.get("bar", _ => "computed") fooValue.futureValue should be("present") barValue.futureValue should be("computed") } "get or compute async value" in { val cache = Scaffeine().buildAsync[String, String]() cache.put("foo", Future.successful("present")) val fooValue = cache.getFuture("foo", _ => Future.successful("computed")) val barValue = cache.getFuture("bar", _ => Future.successful("computed")) fooValue.futureValue should be("present") barValue.futureValue should be("computed") } "get or compute all values" in { val cache = Scaffeine().buildAsync[String, String]() cache.put("foo", Future.successful("present")) val values = cache.getAll(List("foo", "bar"), _.map(key => (key, "computed")).toMap) values.futureValue should contain only ("foo" -> "present", "bar" -> "computed") } "get or compute async all values" in { val cache = Scaffeine().buildAsync[String, String]() cache.put("foo", Future.successful("present")) val values = cache.getAllFuture( List("foo", "bar"), keys => Future.successful(keys.map(key => (key, "computed")).toMap) ) values.futureValue should contain only ("foo" -> "present", "bar" -> "computed") } "put value" in { val cache = Scaffeine().buildAsync[String, String]() cache.put("foo", Future.successful("present")) val fooValue = cache.getIfPresent("foo") fooValue.value.futureValue should be("present") } "expose a synchronous view of itself" in { val cache = Scaffeine().buildAsync[String, String]() val synchronousCache = cache.synchronous() synchronousCache shouldBe a[Cache[_, _]] } } }
Example 72
Source File: SchedulableTest.scala From dagr with MIT License | 5 votes |
package dagr.core.tasksystem import dagr.core.UnitSpec import dagr.core.execsystem.{Cores, Memory, ResourceSet} import org.scalatest.OptionValues class SchedulableTest extends UnitSpec with OptionValues { private val fixedTask = new FixedResources { override def applyResources(resources: ResourceSet) = () } // A variable task that takes twice as much memory in GB as available cores. private val doubleMemoryTask = new VariableResources { override def pickResources(availableResources: ResourceSet) = { val cores = availableResources.cores availableResources.subset(cores, Memory(s"${2*cores.value.toLong}g")) } } // A variable task that takes four times as much memory in GB as available cores. private val quadMemoryTask = new VariableResources { override def pickResources(availableResources: ResourceSet) = { val cores = availableResources.cores availableResources.subset(cores, Memory(s"${4*cores.value.toLong}g")) } } // A variable task that always takes the available cores and 2g of memory. private val twoGbTask = new VariableResources { override def pickResources(availableResources: ResourceSet) = availableResources.subset(availableResources.cores, Memory("2g")) } "Schedulable.minCores" should "work with fixed resources" in { fixedTask.requires(1, "2g").minCores().value.value shouldBe 1 fixedTask.requires(2, "2g").minCores().value.value shouldBe 2 fixedTask.requires(2, "2g").minCores(end=Cores(1)).isEmpty shouldBe true fixedTask.requires(2, "2g").minCores(start=Cores(1), end=Cores(2), step=Cores(2)).isEmpty shouldBe true } it should "work with variable resources" in { doubleMemoryTask.minCores(end=Cores(1)).value.value shouldBe 1 doubleMemoryTask.minCores(start=Cores(2), end=Cores(2), step=Cores(1)).value.value shouldBe 2 doubleMemoryTask.minCores(start=Cores(3), end=Cores(2), step=Cores(1)).isEmpty shouldBe true } "Schedulable.minMemory" should "work with fixed resources" in { fixedTask.requires(1, "2g").minMemory().value shouldBe Memory("2g") fixedTask.requires(2, "2g").minMemory().value shouldBe Memory("2g") fixedTask.requires(2, "2g").minMemory(end=Memory("1g")).isEmpty shouldBe true fixedTask.requires(2, "2g").minMemory(start=Memory("1g"), end=Memory("2g"), step=Memory("2g")).isEmpty shouldBe true } it should "work with variable resources" in { twoGbTask.minMemory().value.value shouldBe Memory("2g").value twoGbTask.minMemory(end=Memory("1g"), step=Memory("256m")).isEmpty shouldBe true twoGbTask.minMemory(end=Memory("2g"), step=Memory("256m")).value.value shouldBe Memory("2g").value twoGbTask.minMemory(start=Memory("1g"), end=Memory("2g"), step=Memory("1g")).value.value shouldBe Memory("2g").value twoGbTask.minMemory(start=Memory("3g"), end=Memory("2g"), step=Memory("1g")).isEmpty shouldBe true } "Schedulable.minCoresAndMemory" should "work with fixed resources" in { fixedTask.requires(2, "2g").minCoresAndMemory(memoryPerCore=Memory("1g")).value shouldBe ResourceSet(Cores(2), Memory("2g")) fixedTask.requires(2, "1g").minCoresAndMemory(memoryPerCore=Memory("512m")).value shouldBe ResourceSet(Cores(2), Memory("1g")) fixedTask.requires(2, "1g").minCoresAndMemory(memoryPerCore=Memory(Memory("1m").value / 2)).isEmpty shouldBe true } it should "work with variable resources" in { doubleMemoryTask.minCoresAndMemory(memoryPerCore=Memory("4g")).value shouldBe ResourceSet(Cores(1), Memory("2g")) doubleMemoryTask.minCoresAndMemory(memoryPerCore=Memory("2g")).value shouldBe ResourceSet(Cores(1), Memory("2g")) doubleMemoryTask.minCoresAndMemory(memoryPerCore=Memory("1g")).isEmpty shouldBe true quadMemoryTask.minCoresAndMemory(memoryPerCore=Memory("4g")).value shouldBe ResourceSet(Cores(1), Memory("4g")) quadMemoryTask.minCoresAndMemory(memoryPerCore=Memory("2g")).isEmpty shouldBe true quadMemoryTask.minCoresAndMemory(memoryPerCore=Memory("1g")).isEmpty shouldBe true } "Schedulable.minResources" should "work with fixed resources" in { fixedTask.requires(1, "2g").minResources().value shouldBe ResourceSet(Cores(1), Memory("2g")) fixedTask.requires(2, "24g").minResources().value shouldBe ResourceSet(Cores(2), Memory("24g")) fixedTask.requires(2, "24g").minResources(maximumResources=ResourceSet(Cores(1), Memory("24g"))).isEmpty shouldBe true } it should "work with variable resources" in { twoGbTask.minResources().value shouldBe ResourceSet(Cores(1), Memory("2g")) twoGbTask.minResources(ResourceSet(Cores(2), Memory("2g"))).value shouldBe ResourceSet(Cores(1), Memory("2g")) twoGbTask.minResources(ResourceSet(Cores(2), Memory("1g"))).value shouldBe ResourceSet(Cores(2), Memory("2g")) twoGbTask.minResources(ResourceSet(Cores(2), Memory("1023m"))).isEmpty shouldBe true doubleMemoryTask.minResources().value shouldBe ResourceSet(Cores(1), Memory("2g")) doubleMemoryTask.minResources(ResourceSet(Cores(2), Memory("1g"))).isEmpty shouldBe true quadMemoryTask.minResources().value shouldBe ResourceSet(Cores(1), Memory("4g")) quadMemoryTask.minResources(ResourceSet(Cores(1), Memory("4g"))).value shouldBe ResourceSet(Cores(1), Memory("4g")) quadMemoryTask.minResources(ResourceSet(Cores(1), Memory("3g"))).isEmpty shouldBe true } }
Example 73
Source File: ResourceSetTest.scala From dagr with MIT License | 5 votes |
package dagr.core.execsystem import dagr.core.UnitSpec import org.scalatest.OptionValues class ResourceSetTest extends UnitSpec with OptionValues { "ResourceSet.isEmpty" should "return true for the empty resource set" in { ResourceSet.empty.isEmpty shouldBe true } "ResourceSet" should "add and subtract resources" in { val original = ResourceSet(10, 10) val middle = ResourceSet(original) var running = original + middle running.cores.value shouldBe 20 running.memory.value shouldBe 20 running = running - middle running.cores.value shouldBe 10 running.memory.value shouldBe 10 running = running - Cores(10) running.cores.value shouldBe 0 } it should "subset resources" in { // doc examples ResourceSet(4.5, 10).subset(Cores(1), Cores(5), Memory(10)).value shouldBe ResourceSet(4, 10) ResourceSet(4.5, 10).subset(Cores(1), Cores(5.1), Memory(10)).value shouldBe ResourceSet(4.5, 10) ResourceSet(1.5, 10).subset(Cores(1), Cores(5), Memory(10)).value shouldBe ResourceSet(1, 10) ResourceSet(1.5, 10).subset(Cores(1.5), Cores(5), Memory(10)).value shouldBe ResourceSet(1.5, 10) val resources = ResourceSet(10, 10) resources.subset(ResourceSet(10, 10)).value shouldBe ResourceSet(10, 10) resources.subset(ResourceSet(10.5, 10)).isDefined shouldBe false resources.subset(ResourceSet(9.5, 10)).value shouldBe ResourceSet(9.5, 10) resources.subset(ResourceSet(5, 5)).value shouldBe ResourceSet(5, 5) resources.subset(ResourceSet(4.5, 5)).value shouldBe ResourceSet(4.5, 5) val halfACore = ResourceSet(0.5, 10) halfACore.subset(ResourceSet(0.5, 10)).value shouldBe ResourceSet(0.5, 10) halfACore.subset(Cores(0.25), Cores(0.5), Memory(10)).value shouldBe ResourceSet(0.5, 10) halfACore.subset(Cores(0.5), Cores(1), Memory(10)).value shouldBe ResourceSet(0.5, 10) halfACore.subset(Cores(0.1), Cores(1), Memory(10)).value shouldBe ResourceSet(0.1, 10) halfACore.subset(Cores(0.51), Cores(1), Memory(10)).isDefined shouldBe false halfACore.subset(Cores(0.25), Cores(0.3), Memory(10)).value shouldBe ResourceSet(0.3, 10) val fiveAndAHalfCores = ResourceSet(5.5, 10) fiveAndAHalfCores.subset(Cores(1.2), Cores(5.8), Memory(10)).value shouldBe ResourceSet(5.5, 10) } }
Example 74
Source File: AnySupportSpec.scala From cloudstate with Apache License 2.0 | 5 votes |
package io.cloudstate.javasupport.impl import com.example.shoppingcart.Shoppingcart import com.google.protobuf.{ByteString, Empty} import io.cloudstate.javasupport.Jsonable import io.cloudstate.protocol.entity.UserFunctionError import io.cloudstate.protocol.event_sourced.EventSourcedProto import org.scalatest.{Matchers, OptionValues, WordSpec} import scala.beans.BeanProperty class AnySupportSpec extends WordSpec with Matchers with OptionValues { private val anySupport = new AnySupport(Array(Shoppingcart.getDescriptor, EventSourcedProto.javaDescriptor), getClass.getClassLoader, "com.example") private val addLineItem = Shoppingcart.AddLineItem .newBuilder() .setName("item") .setProductId("id") .setQuantity(10) .build() "Any support" should { "support se/deserializing java protobufs" in { val any = anySupport.encodeScala(addLineItem) any.typeUrl should ===("com.example/" + Shoppingcart.AddLineItem.getDescriptor.getFullName) anySupport.decode(any) should ===(addLineItem) } "support se/deserializing scala protobufs" in { val error = UserFunctionError("error") val any = anySupport.encodeScala(UserFunctionError("error")) any.typeUrl should ===("com.example/cloudstate.UserFunctionError") anySupport.decode(any) should ===(error) } "support resolving a service descriptor" in { val methods = anySupport.resolveServiceDescriptor(Shoppingcart.getDescriptor.findServiceByName("ShoppingCart")) methods should have size 3 val method = methods("AddItem") // Input type method.inputType.typeUrl should ===("com.example/" + Shoppingcart.AddLineItem.getDescriptor.getFullName) method.inputType.typeClass should ===(classOf[Shoppingcart.AddLineItem]) val iBytes = method.inputType.asInstanceOf[ResolvedType[Any]].toByteString(addLineItem) method.inputType.parseFrom(iBytes) should ===(addLineItem) // Output type - this also checks that when java_multiple_files is true, it works method.outputType.typeUrl should ===("com.example/" + Empty.getDescriptor.getFullName) method.outputType.typeClass should ===(classOf[Empty]) val oBytes = method.outputType.asInstanceOf[ResolvedType[Any]].toByteString(Empty.getDefaultInstance) method.outputType.parseFrom(oBytes) should ===(Empty.getDefaultInstance) } def testPrimitive[T](name: String, value: T, defaultValue: T) = { val any = anySupport.encodeScala(value) any.typeUrl should ===(AnySupport.CloudStatePrimitive + name) anySupport.decode(any) should ===(value) val defaultAny = anySupport.encodeScala(defaultValue) defaultAny.typeUrl should ===(AnySupport.CloudStatePrimitive + name) defaultAny.value.size() shouldBe 0 anySupport.decode(defaultAny) should ===(defaultValue) } "support se/deserializing strings" in testPrimitive("string", "foo", "") "support se/deserializing ints" in testPrimitive("int32", 10, 0) "support se/deserializing longs" in testPrimitive("int64", 10L, 0L) "support se/deserializing floats" in testPrimitive("float", 0.5f, 0f) "support se/deserializing doubles" in testPrimitive("double", 0.5d, 0d) "support se/deserializing bytes" in testPrimitive("bytes", ByteString.copyFromUtf8("foo"), ByteString.EMPTY) "support se/deserializing booleans" in testPrimitive("bool", true, false) "support se/deserializing json" in { val myJsonable = new MyJsonable myJsonable.field = "foo" val any = anySupport.encodeScala(myJsonable) any.typeUrl should ===(AnySupport.CloudStateJson + classOf[MyJsonable].getName) anySupport.decode(any).asInstanceOf[MyJsonable].field should ===("foo") } } } @Jsonable class MyJsonable { @BeanProperty var field: String = _ }
Example 75
Source File: TezosTypesTest.scala From Conseil with Apache License 2.0 | 5 votes |
package tech.cryptonomic.conseil.common.tezos import java.time.Instant import org.scalatest.{EitherValues, Matchers, OptionValues, WordSpec} import tech.cryptonomic.conseil.common.tezos.TezosTypes._ class TezosTypesTest extends WordSpec with Matchers with OptionValues with EitherValues { val sut = TezosTypes "The Base58Check verifier" should { "accept an empty string" in { sut.isBase58Check("") shouldBe true } "accept a correctly encoded string" in { sut.isBase58Check( "signiRfcqmbGc6UtW1WzuJNGzRRsWDLpafxZZPwwTMntFwup8rTxXEgcLD5UBWkYmMqZECVEr33Xw5sh9NVi45c4FVAXvQSf" ) shouldBe true } "reject a string with forbidden chars" in { sut.isBase58Check( "signiRfcqmbGc6UtW1WzulJNGzRRsWDLpafxZZPwwTMntFwup8rTxXEgcLD5UBWkYmMqZECVEr33Xw5sh9NVi45c4FVAXvQSf" ) shouldBe false sut.isBase58Check( "$signiRfcqmbGc6UtW1WzulJNGzRRsWDpafxZZPwwTMntFwup8rTxXEgcLD5UBWkYmMqZECVEr33Xw5sh9NVi45c4FVAXvQSf" ) shouldBe false sut.isBase58Check( "signiRfcqmbGc6UtW1WzulJNGzRRsWDpafxZZPwwTMntFwup8rTxXEgcLD5UBWkYmMqZECVEr33Xw5sh9NVi45c4FVAXvQSf*" ) shouldBe false } "reject a string with spaces" in { sut.isBase58Check( "signiRfcqmbGc6UtW1WzuJNGzRRs DLpafxZZPwwTMntFwup8rTxXEgcLD5UBWkYmMqZECVEr33Xw5sh9NVi45c4FVAXvQSf" ) shouldBe false sut.isBase58Check( " signiRfcqmbGc6UtW1WzuJNGzRRsDLpafxZZPwwTMntFwup8rTxXEgcLD5UBWkYmMqZECVEr33Xw5sh9NVi45c4FVAXvQSf" ) shouldBe false sut.isBase58Check( "signiRfcqmbGc6UtW1WzuJNGzRRsDLpafxZZPwwTMntFwup8rTxXEgcLD5UBWkYmMqZECVEr33Xw5sh9NVi45c4FVAXvQSf " ) shouldBe false } } "The Syntax import" should { "allow building Block-tagged generic data" in { import TezosTypes.Syntax._ val someTime = Some(Instant.ofEpochMilli(0)) val content = "A content string" val (hash, level) = (BlockHash("hash"), 1) content.taggedWithBlock(hash, level, someTime, None, None) shouldEqual BlockTagged( hash, level, someTime, None, None, content ) } } "The BlockTagged wrapper" should { "convert to a tuple" in { val someTime = Some(Instant.ofEpochMilli(0)) val content = "A content string" val (hash, level) = (BlockHash("hash"), 1) BlockTagged(hash, level, someTime, None, None, content).asTuple shouldEqual (hash, level, someTime, None, None, content) } } }
Example 76
Source File: StakerDaoTest.scala From Conseil with Apache License 2.0 | 5 votes |
package tech.cryptonomic.conseil.indexer.tezos.michelson.contracts import org.scalatest.{Matchers, OptionValues, WordSpec} import tech.cryptonomic.conseil.common.tezos.TezosTypes._ class StakerDaoTest extends WordSpec with Matchers with OptionValues { "The Token Contracts operations for the StakerDao contract" should { "read a balance update from big map diff" in { //given //values taken from mainnet operations val ledgerId = ContractId("KT1EctCuorV2NfVb1XTQgvzJ88MQtWP8cMMv") val mapId = 20 val params = """ |{ | "prim": "Right", | "args": [ | { | "prim": "Left", | "args": [ | { | "prim": "Left", | "args": [ | { | "prim": "Right", | "args": [ | { | "prim": "Pair", | "args": [ | { | "bytes": "00007374616b65722d64616f2f7265736572766f6972" | }, | { | "prim": "Pair", | "args": [ | { | "bytes": "0000c528aa23546060e4459c5b37df752eea5bf5edc3" | }, | { | "int": "1" | } | ] | } | ] | } | ] | } | ] | } | ] | } | ] |} """.stripMargin val senderMapUpdate = Contract.BigMapUpdate( action = "update", key = Micheline("""{"bytes": "00007374616b65722d64616f2f7265736572766f6972"}"""), key_hash = ScriptId("exprucaLf6G5Robew77nwXRNR7gAbUJ3da2yAwqJdhyZCXZdEkLz8t"), big_map = Decimal(mapId), value = Some(Micheline("""{"int": "1499998"}""")) ) val receiverMapUpdate = Contract.BigMapUpdate( action = "update", key = Micheline("""{"bytes": "0000c528aa23546060e4459c5b37df752eea5bf5edc3"}"""), key_hash = ScriptId("exprtv5jtq14XnqMvskagVojNgSd8bXjxTZtDYY58MtAek5gbLKA4C"), big_map = Decimal(mapId), value = Some(Micheline("""{"int": "1"}""")) ) //register the token info val sut = TokenContracts.fromConfig(List(ledgerId -> "FA1.2-StakerDao")) //set the map id for the contract sut.setMapId(ledgerId, mapId) //when val balanceUpdates = List(senderMapUpdate, receiverMapUpdate).map( mapUpdate => sut .readBalance(ledgerId)( diff = mapUpdate, params = Some(Left(Parameters(Micheline(params)))) ) .value ) //then balanceUpdates should contain theSameElementsAs List( AccountId("tz1dcWXLS1UBeGc7EazGvoNE6D8YSzVkAsSa") -> BigInt(1), AccountId("tz1WAVpSaCFtLQKSJkrdVApCQC1TNK8iNxq9") -> BigInt(1499998) ) } } }
Example 77
Source File: ReferenceSetBuilderTest.scala From fgbio with MIT License | 5 votes |
package com.fulcrumgenomics.testing import java.nio.file.Files import com.fulcrumgenomics.fasta.Converters.FromSAMSequenceDictionary import com.fulcrumgenomics.fasta.SequenceDictionary import htsjdk.samtools.reference.ReferenceSequenceFileFactory import org.scalatest.OptionValues class ReferenceSetBuilderTest extends UnitSpec with OptionValues { "ReferenceSetBuilder" should "write a simple FASTA file, with accompanying .dict and .fai files" in { val builder = new ReferenceSetBuilder builder.add("chr1").add("AAAAAAAAAA", 500) // 5000 bases builder.add("chr2").add("CCCCCCCCCC", 500) // 5000 bases val fasta = builder.toTempFile() // Check the .dict file val dictIn = ReferenceSequenceFileFactory.getDefaultDictionaryForReferenceSequence(fasta) Files.exists(dictIn) shouldBe true val dict = SequenceDictionary.extract(dictIn) // Check the .fai file val fai = ReferenceSequenceFileFactory.getFastaIndexFileName(fasta) Files.exists(fai) shouldBe true // Read it back in val ref = ReferenceSequenceFileFactory.getReferenceSequenceFile(fasta) ref.isIndexed shouldBe true // since the .fai file exists ref.getSequenceDictionary.getSequences.size shouldBe 2 ref.getSequence("chr1").getBases.count(_ == 'A') shouldBe 5000 ref.getSequence("chr2").getBases.count(_ == 'C') shouldBe 5000 ref.getSequenceDictionary.fromSam.sameAs(dict) ref.close() } it should "write an assembly name into the dictionary if given one" in { val Seq(plus, minus) = Seq(Some("hg19"), None).map { assembly => val builder = new ReferenceSetBuilder(assembly=assembly) builder.add("chr1").add("A", 100) val path = builder.toTempFile() val dict = SequenceDictionary.extract(path) dict.length shouldBe 1 dict } plus("chr1").assembly.value shouldBe "hg19" minus("chr1").assembly shouldBe 'empty } it should "write species name into the dictionary if given one" in { val Seq(plus, minus) = Seq(Some("human"), None).map { species => val builder = new ReferenceSetBuilder(species=species) builder.add("chr1").add("A", 100) val path = builder.toTempFile() val dict = SequenceDictionary.extract(path) dict.length shouldBe 1 dict } plus("chr1").species.value shouldBe "human" minus("chr1").species shouldBe 'empty } it should "calculate MD5s for sequences only if asked" in { val builder = new ReferenceSetBuilder() builder.add("chr1").add("A", 100) val Seq(plus, minus) = Seq(true, false).map(md5 => builder.toTempFile(calculateMds5=md5)).map(SequenceDictionary.extract) plus("chr1").md5 shouldBe 'nonEmpty minus("chr1").md5 shouldBe 'empty } }
Example 78
Source File: UmiConsensusCallerTest.scala From fgbio with MIT License | 5 votes |
package com.fulcrumgenomics.umi import com.fulcrumgenomics.bam.api.SamOrder import com.fulcrumgenomics.testing.{SamBuilder, UnitSpec} import htsjdk.samtools.SAMFileHeader.GroupOrder import htsjdk.samtools.SAMReadGroupRecord import org.scalatest.OptionValues class UmiConsensusCallerTest extends UnitSpec with OptionValues { "UmiConsensusCaller" should "do nothing when the header has the right sort order in it" in { val builder = new SamBuilder(sort=Some(SamOrder.TemplateCoordinate)) var warning : Option[String] = None var error : Option[String] = None UmiConsensusCaller.checkSortOrder(builder.header, "foo.bam", w => warning = Some(w), e => error = Some(e)) warning shouldBe None error shouldBe None } it should "fire a warning if the file is query grouped and unsorted but without sub-sort specified" in { val builder = new SamBuilder(sort=Some(SamOrder.Unsorted)) builder.header.setGroupOrder(GroupOrder.query) var warning : Option[String] = None var error : Option[String] = None UmiConsensusCaller.checkSortOrder(builder.header, "foo.bam", w => warning = Some(w), e => error = Some(e)) warning.value.indexOf("foo.bam") should be >= 0 error shouldBe None } it should "fire an error if the file is coordinate sorted" in { val builder = new SamBuilder(sort=Some(SamOrder.Coordinate)) var warning : Option[String] = None var error : Option[String] = None UmiConsensusCaller.checkSortOrder(builder.header, "foo.bam", w => warning = Some(w), e => error = Some(e)) warning shouldBe None error.value.indexOf("foo.bam") should be >= 0 } "UmiConsensusCaller.outputHeader" should "be case-insensitive when merging platforms by changing them to upper-case" in { def buildRg(id: String, sample: String, library: String, platform: String): SAMReadGroupRecord = { val rg = new SAMReadGroupRecord(id) rg.setSample(sample) rg.setLibrary(library) rg.setPlatform(platform) return rg } val builder = new SamBuilder(sort=Some(SamOrder.TemplateCoordinate)) builder.header.addReadGroup(buildRg(id="B", sample="Sample", library="L1", platform="ILLUMINA")) builder.header.addReadGroup(buildRg(id="C", sample="Sample", library="L2", platform="illumina")) val outHeader = UmiConsensusCaller.outputHeader(in=builder.header, readGroupId="A") outHeader.getReadGroups.size shouldBe 1 val rg = outHeader.getReadGroup("A") rg.getSample shouldBe "Sample" rg.getLibrary shouldBe "L1,L2" rg.getPlatform shouldBe "ILLUMINA" } }
Example 79
Source File: NcbiRefSeqGffSourceTest.scala From fgbio with MIT License | 5 votes |
package com.fulcrumgenomics.util import java.nio.file.Paths import com.fulcrumgenomics.fasta.{SequenceDictionary, SequenceMetadata} import com.fulcrumgenomics.testing.UnitSpec import com.fulcrumgenomics.util.GeneAnnotations.Exon import org.scalatest.OptionValues class NcbiRefSeqGffSourceTest extends UnitSpec with OptionValues { // Excerpted from https://ftp.ncbi.nlm.nih.gov/genomes/refseq/vertebrate_mammalian/Homo_sapiens/latest_assembly_versions/GCF_000001405.39_GRCh38.p13/GCF_000001405.39_GRCh38.p13_genomic.gff.gz private val GffFile = Paths.get("src/test/resources/com/fulcrumgenomics/util/human.gff.gz") private val Chr1 = SequenceMetadata(name="chr1", length=249250621, aliases=Seq("1", "NC_000001.11")) private val AltChr1 = SequenceMetadata(name="1", length=249250621, aliases=Seq("chr1", "NC_000001.11")) private val DictEmpty = SequenceDictionary() private val DictChr1 = SequenceDictionary(Chr1) private val DictAlt1 = SequenceDictionary(AltChr1) "NcbiRefSeqSource" should "auto-map the accession to chr1 when given an empty sequence dictionary" in { val source = NcbiRefSeqGffSource(GffFile, includeXs=true, dict=DictEmpty) source should have size 7 // Pseudo-gene should not have been included source.get("DDX11L1") shouldBe None // Check a micro-RNA for details val mir = source.get("MIR6859-1").value mir.loci should have size 1 mir.loci.head.chrom shouldBe "chr1" mir.loci.head.start shouldBe 17369 mir.loci.head.end shouldBe 17436 mir.loci.head.transcripts should have size 1 mir.loci.head.transcripts.head.chrom shouldBe "chr1" mir.loci.head.transcripts.head.start shouldBe 17369 mir.loci.head.transcripts.head.end shouldBe 17436 mir.loci.head.transcripts.head.cdsStart shouldBe None mir.loci.head.transcripts.head.cdsEnd shouldBe None mir.loci.head.transcripts.head.negativeStrand shouldBe true mir.loci.head.transcripts.head.exons shouldBe Seq(Exon(17369, 17436)) // Check a lncRNA for specifics val lnc = source.get("MIR1302-2HG").value lnc.loci should have size 1 lnc.loci.head.transcripts should have size 1 lnc.loci.head.transcripts.head.chrom shouldBe "chr1" lnc.loci.head.transcripts.head.start shouldBe 29926 lnc.loci.head.transcripts.head.end shouldBe 31295 lnc.loci.head.transcripts.head.cdsStart shouldBe None lnc.loci.head.transcripts.head.cdsEnd shouldBe None lnc.loci.head.transcripts.head.negativeStrand shouldBe false lnc.loci.head.transcripts.head.exons shouldBe Seq(Exon(29926, 30039), Exon(30564, 30667), Exon(30976, 31295)) // Check a coding gene for val gene = source.get("OR4F5").value gene.loci should have size 1 gene.loci.head.transcripts should have size 1 gene.loci.head.transcripts.head.name shouldBe "NM_001005484.1" gene.loci.head.transcripts.head.chrom shouldBe "chr1" gene.loci.head.transcripts.head.start shouldBe 69091 gene.loci.head.transcripts.head.end shouldBe 70008 gene.loci.head.transcripts.head.cdsStart.value shouldBe 69091 gene.loci.head.transcripts.head.cdsEnd.value shouldBe 70008 gene.loci.head.transcripts.head.negativeStrand shouldBe false // Check a gene that has multiple transcripts val g2 = source.get("LOC100996442").value g2.loci should have size 1 g2.loci.head.transcripts should have size 14 } it should "still load all genes when given a dictionary that has all the used chroms in it" in { val source = NcbiRefSeqGffSource(GffFile, includeXs=true, dict=DictChr1) source should have size 7 for (gene <- source; locus <- gene.loci; tx <- locus) { locus.chrom shouldBe "chr1" tx.chrom shouldBe "chr1" } } it should "map the chromosome name using the dictionary" in { val source = NcbiRefSeqGffSource(GffFile, includeXs=true, dict=DictAlt1) source should have size 7 for (gene <- source; locus <- gene.loci; tx <- locus) { locus.chrom shouldBe "1" tx.chrom shouldBe "1" } } it should "exclude experimental transcripts (and genes with only exp transcripts)" in { val source = NcbiRefSeqGffSource(GffFile, includeXs=false, dict=DictChr1) source should have size 5 for (gene <- source; locus <- gene.loci; tx <- locus) { tx.name.charAt(0) should not be 'X' } } }
Example 80
Source File: VariantTest.scala From fgbio with MIT License | 5 votes |
package com.fulcrumgenomics.vcf.api import com.fulcrumgenomics.FgBioDef._ import com.fulcrumgenomics.testing.UnitSpec import org.scalatest.OptionValues class VariantTest extends UnitSpec with OptionValues { "Variant.isMissingValue" should "correctly handle missing and non-missing values" in { Variant.isMissingValue(".") shouldBe true Variant.isMissingValue('.') shouldBe true Variant.isMissingValue(Variant.MissingInt) shouldBe true Variant.isMissingValue(Variant.MissingFloat) shouldBe true Variant.isMissingValue(".1") shouldBe false Variant.isMissingValue(Float.NaN) shouldBe false Range(-500, 500).foreach { i => Variant.isMissingValue(i) shouldBe false} } }
Example 81
Source File: SampleTest.scala From fgbio with MIT License | 5 votes |
package com.fulcrumgenomics.illumina import com.fulcrumgenomics.testing.UnitSpec import org.scalatest.OptionValues class SampleTest extends UnitSpec with OptionValues{ "Sample.sampleBarcodeBases" should "return the sample barcodes if present" in { new Sample(0, "ID", "NAME", "LIBRARY").sampleBarcodeBases.flatten.isEmpty shouldBe true new Sample(0, "ID", "NAME", "LIBRARY", i7IndexBases=Some("GATTACA")).sampleBarcodeBases.flatten should contain theSameElementsInOrderAs Seq("GATTACA") new Sample(0, "ID", "NAME", "LIBRARY", i5IndexBases=Some("GATTACA")).sampleBarcodeBases.flatten should contain theSameElementsInOrderAs Seq("GATTACA") new Sample(0, "ID", "NAME", "LIBRARY", i7IndexBases=Some("GATTACA"), i5IndexBases=Some("TGTAATC")).sampleBarcodeBases.flatten should contain theSameElementsInOrderAs Seq("GATTACA", "TGTAATC") } it should "support extended attributes" in { val sample = new Sample(0, "ID", "NAME", "LIBRARY", extendedAttributes=Seq(("FOO", "1"), ("BAR", " 2 "), ("BAZ", " ")).toMap) sample.extendedAttribute("foo").value shouldBe "1" sample.extendedAttribute("Foo").value shouldBe "1" sample.extendedAttribute("FOO").value shouldBe "1" sample.extendedAttribute("bar").value shouldBe "2" sample.extendedAttribute("Bar").value shouldBe "2" sample.extendedAttribute("BAR").value shouldBe "2" sample.extendedAttribute("baz").isEmpty shouldBe true sample.extendedAttribute("Baz").isEmpty shouldBe true sample.extendedAttribute("BAZ").isEmpty shouldBe true sample.extendedAttribute("car").isEmpty shouldBe true sample.extendedAttribute("Car").isEmpty shouldBe true sample.extendedAttribute("CAR").isEmpty shouldBe true } it should "support throw an exception if extended attribute keys are not all uppercase" in { an[Exception] should be thrownBy new Sample(0, "ID", "NAME", "LIBRARY", extendedAttributes=Seq(("foo", "1"), ("bar", "2 ")).toMap) } }
Example 82
Source File: LedgerConfigurationClientImplTest.scala From daml with Apache License 2.0 | 5 votes |
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package com.daml.ledger.rxjava.grpc import java.util.concurrent.TimeUnit import com.daml.ledger.rxjava._ import com.daml.ledger.rxjava.grpc.helpers.{LedgerServices, TestConfiguration} import com.daml.ledger.api.v1.ledger_configuration_service.GetLedgerConfigurationResponse import org.scalatest.{FlatSpec, Matchers, OptionValues} final class LedgerConfigurationClientImplTest extends FlatSpec with Matchers with AuthMatchers with OptionValues { private val ledgerServices = new LedgerServices("ledger-configuration-service-ledger") behavior of "[5.1] LedgerConfigurationClientImpl.getLedgerConfiguration" it should "send the request to the Ledger" in { ledgerServices.withConfigurationClient(Seq(GetLedgerConfigurationResponse.defaultInstance)) { (client, _) => // to test that we send a request to the Ledger, we check if there is a response client.getLedgerConfiguration .timeout(TestConfiguration.timeoutInSeconds, TimeUnit.SECONDS) .blockingFirst() } } behavior of "[5.2] LedgerConfigurationClientImpl.getLedgerConfiguration" it should "send the request with the correct ledger ID" in { ledgerServices.withConfigurationClient(Seq(GetLedgerConfigurationResponse.defaultInstance)) { (client, service) => client.getLedgerConfiguration .timeout(TestConfiguration.timeoutInSeconds, TimeUnit.SECONDS) .blockingFirst() service.getLastRequest.value.ledgerId shouldEqual ledgerServices.ledgerId } } behavior of "Authorization" def toAuthenticatedServer(fn: LedgerConfigurationClient => Any): Any = ledgerServices.withConfigurationClient( Seq(GetLedgerConfigurationResponse.defaultInstance), mockedAuthService) { (client, _) => fn(client) } it should "deny access without a token" in { expectUnauthenticated { toAuthenticatedServer( _.getLedgerConfiguration .timeout(TestConfiguration.timeoutInSeconds, TimeUnit.SECONDS) .blockingFirst()) } } it should "deny access with insufficient authorization" in { expectUnauthenticated { toAuthenticatedServer( _.getLedgerConfiguration(emptyToken) .timeout(TestConfiguration.timeoutInSeconds, TimeUnit.SECONDS) .blockingFirst()) } } it should "allow access with sufficient authorization" in { toAuthenticatedServer( _.getLedgerConfiguration(publicToken) .timeout(TestConfiguration.timeoutInSeconds, TimeUnit.SECONDS) .blockingFirst()) } }
Example 83
Source File: OffsetSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.cli.sse import java.nio.file.{Files, Path} import java.util.UUID import cats.effect.{Blocker, ContextShift, IO} import ch.epfl.bluebrain.nexus.cli.Console import ch.epfl.bluebrain.nexus.cli.dummies.TestConsole import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.ExecutionContext class OffsetSpec extends AnyWordSpecLike with Matchers with OptionValues { implicit protected val cs: ContextShift[IO] = IO.contextShift(ExecutionContext.global) implicit private val blocker: Blocker = Blocker.liftExecutionContext(ExecutionContext.global) implicit private val console: Console[IO] = TestConsole[IO].unsafeRunSync() abstract class Ctx { protected val uuid: UUID = UUID.randomUUID protected val file: Path = Files.createTempFile("offset", ".conf") } "An offset" should { "be loaded from configuration" in new Ctx { Files.writeString(file, uuid.toString) (for { offset <- Offset.load(file) _ = offset.value shouldEqual Offset(uuid) } yield Files.deleteIfExists(file)).unsafeRunSync() } "be loaded from configuration but failed to convert to UUID" in new Ctx { Files.writeString(file, "not-an-uuid") (for { offset <- Offset.load(file) _ = offset shouldEqual None } yield Files.deleteIfExists(file)).unsafeRunSync() } "be written to file" in new Ctx { val offset = Offset(UUID.randomUUID()) (for { _ <- offset.write(file) _ = Files.readString(file) shouldEqual offset.value.toString } yield Files.deleteIfExists(file)).unsafeRunSync() } } }
Example 84
Source File: SparqlClientSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.cli.clients import cats.effect.IO import cats.implicits._ import ch.epfl.bluebrain.nexus.cli.{AbstractCliSpec, Console} import ch.epfl.bluebrain.nexus.cli.CliError.ClientError.{ClientStatusError, ServerStatusError} import ch.epfl.bluebrain.nexus.cli.config.{AppConfig, EnvConfig} import ch.epfl.bluebrain.nexus.cli.sse._ import ch.epfl.bluebrain.nexus.cli.utils.Http4sExtras import izumi.distage.model.definition.ModuleDef import org.http4s.circe.CirceEntityEncoder._ import org.http4s.client.Client import org.http4s.dsl.io._ import org.http4s.headers.`Content-Type` import org.http4s.{HttpApp, Response, Status, Uri} import org.scalatest.OptionValues class SparqlClientSpec extends AbstractCliSpec with Http4sExtras with OptionValues { private val sparqlResultsJson = jsonContentOf("/templates/sparql-results.json") private val sparqlResults = sparqlResultsJson.as[SparqlResults].toOption.value private val query = "SELECT * {?s ?p ?o} LIMIT 10" override def overrides: ModuleDef = new ModuleDef { include(defaultModules) make[Client[IO]].from { cfg: AppConfig => val token = cfg.env.token val ct = `Content-Type`(SparqlClient.`application/sparql-query`) val view = cfg.env.defaultSparqlView.renderString val httpApp = HttpApp[IO] { // success case req @ POST -> `v1` / "views" / OrgLabelVar(`orgLabel`) / ProjectLabelVar( `projectLabel` ) / `view` / "sparql" contentType `ct` optbearer `token` => req.as[String].flatMap { case `query` => Response[IO](Status.Ok).withEntity(sparqlResultsJson).pure[IO] case _ => Response[IO](Status.BadRequest).pure[IO] } // unknown view id case req @ POST -> `v1` / "views" / OrgLabelVar(`orgLabel`) / ProjectLabelVar( `projectLabel` ) / (_: String) / "sparql" contentType `ct` optbearer `token` => req.as[String].flatMap { case `query` => Response[IO](Status.NotFound).withEntity(notFoundJson).pure[IO] case _ => Response[IO](Status.BadRequest).pure[IO] } // unknown token case req @ POST -> `v1` / "views" / OrgLabelVar(`orgLabel`) / ProjectLabelVar( `projectLabel` ) / `view` / "sparql" contentType `ct` optbearer (_: Option[ BearerToken ]) => req.as[String].flatMap { case `query` => Response[IO](Status.Forbidden).withEntity(authFailedJson).pure[IO] case _ => Response[IO](Status.BadRequest).pure[IO] } // other - internal error case req @ POST -> "v1" /: (_: Path) contentType `ct` optbearer `token` => req.as[String].flatMap { case `query` => Response[IO](Status.InternalServerError).withEntity(internalErrorJson).pure[IO] case _ => Response[IO](Status.BadRequest).pure[IO] } } Client.fromHttpApp(httpApp) } } "A SparqlClient" should { "return sparql results" in { (client: Client[IO], console: Console[IO], env: EnvConfig) => val cl = SparqlClient(client, env, console) for { results <- cl.query(orgLabel, projectLabel, query) _ = results shouldEqual Right(sparqlResults) } yield () } "return not found" in { (client: Client[IO], console: Console[IO], env: EnvConfig) => val cl = SparqlClient(client, env, console) for { results <- cl.query(orgLabel, projectLabel, Uri.unsafeFromString(genString()), query) _ = results shouldEqual Left(ClientStatusError(Status.NotFound, notFoundJson.noSpaces)) } yield () } "return internal error" in { (client: Client[IO], console: Console[IO], env: EnvConfig) => val cl = SparqlClient(client, env, console) for { results <- cl.query(orgLabel, ProjectLabel(genString()), Uri.unsafeFromString(genString()), query) _ = results shouldEqual Left(ServerStatusError(Status.InternalServerError, internalErrorJson.noSpaces)) } yield () } "return bad token" in { (client: Client[IO], console: Console[IO], env: EnvConfig) => val cl = SparqlClient(client, env.copy(token = Some(BearerToken("bad"))), console) for { results <- cl.query(orgLabel, projectLabel, query) _ = results shouldEqual Left(ClientStatusError(Status.Forbidden, authFailedJson.noSpaces)) } yield () } } }
Example 85
Source File: AccessControlListSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.acls import ch.epfl.bluebrain.nexus.iam.types.Identity._ import ch.epfl.bluebrain.nexus.iam.types.{Identity, Permission} import ch.epfl.bluebrain.nexus.service.config.ServiceConfig.HttpConfig import ch.epfl.bluebrain.nexus.util.{EitherValues, Resources} import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{Inspectors, OptionValues} class AccessControlListSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with OptionValues with Resources { "An Access Control List" should { val user: Identity = User("uuid", "realm") val group: Identity = Group("mygroup", "myrealm") val readWrite = Set(Permission("acls/read").value, Permission("acls/write").value) val manage = Set(Permission("acls/manage").value) implicit val http: HttpConfig = HttpConfig("some", 8080, "v1", "http://nexus.example.com") "converted to Json" in { val acls = AccessControlList(user -> readWrite, group -> manage) val json = jsonContentOf("/acls/acl.json") acls.asJson shouldEqual json } "convert from Json" in { val acls = AccessControlList(user -> readWrite, group -> manage) val json = jsonContentOf("/acls/acl.json") json.as[AccessControlList].rightValue shouldEqual acls } "remove ACL" in { val read = Permission.unsafe("read") val write = Permission.unsafe("write") val other = Permission.unsafe("other") val acl = AccessControlList(user -> Set(read, write), group -> Set(other)) val acl2 = AccessControlList(group -> Set(read)) acl -- acl2 shouldEqual acl acl -- AccessControlList(user -> Set(read), group -> Set(other)) shouldEqual AccessControlList(user -> Set(write)) acl -- AccessControlList(user -> Set(read)) shouldEqual AccessControlList(user -> Set(write), group -> Set(other)) } } }
Example 86
Source File: ResourceDecoderSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.serializers import java.time.Instant import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.types.Identity.User import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.config.Schemas import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.kg.resources.{Id, ResourceF, ResourceGraph} import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.Decoder import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{Inspectors, OptionValues} class ResourceDecoderSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with ScalatestRouteTest with OptionValues with Resources with TestHelper { private val json = jsonContentOf("/serialization/resource.json") private val projectRef = ProjectRef(genUUID) private val id = url"http://example.com/prefix/myId" private val graph = json.toGraph(id).rightValue implicit private val decoder: Decoder[ResourceGraph] = ResourceF.resourceGraphDecoder(projectRef) private val model = ResourceF( Id(projectRef, url"http://example.com/prefix/myId"), 1L, Set(url"https://example.com/vocab/A", url"https://example.com/vocab/B"), deprecated = false, Map.empty, None, Instant.parse("2020-01-17T12:45:01.479676Z"), Instant.parse("2020-01-17T13:45:01.479676Z"), User("john", "bbp"), User("brenda", "bbp"), Schemas.unconstrainedRef, graph ) "A resource" should { "be decoded" in { json.as[ResourceGraph].rightValue shouldEqual model } } }
Example 87
Source File: DiskStorageOperationsSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.storage import java.nio.file.Paths import akka.http.scaladsl.model.{ContentTypes, Uri} import cats.effect.IO import ch.epfl.bluebrain.nexus.commons.test._ import ch.epfl.bluebrain.nexus.commons.test.io.IOEitherValues import ch.epfl.bluebrain.nexus.kg.config.KgConfig._ import ch.epfl.bluebrain.nexus.kg.resources.file.File.FileDescription import ch.epfl.bluebrain.nexus.kg.resources.Id import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.kg.{KgError, TestHelper} import ch.epfl.bluebrain.nexus.service.config.Settings import ch.epfl.bluebrain.nexus.sourcing.RetryStrategyConfig import org.mockito.IdiomaticMockito import org.scalatest.{BeforeAndAfter, OptionValues} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class DiskStorageOperationsSpec extends ActorSystemFixture("DiskStorageOperationsSpec") with AnyWordSpecLike with Matchers with BeforeAndAfter with IdiomaticMockito with IOEitherValues with Resources with TestHelper with OptionValues { implicit private val appConfig = Settings(system).serviceConfig implicit private val sc: StorageConfig = appConfig.kg.storage.copy( DiskStorageConfig(Paths.get("/tmp"), "SHA-256", read, write, false, 1024L), RemoteDiskStorageConfig("http://example.com", "v1", None, "SHA-256", read, write, true, 1024L), S3StorageConfig("MD5", read, write, true, 1024L), "password", "salt", RetryStrategyConfig("linear", 300.millis, 5.minutes, 100, 1.second) ) private val project = ProjectRef(genUUID) private val storage = Storage.DiskStorage.default(project) private val resId = Id(storage.ref, genIri) private val fileDesc = FileDescription("my file.txt", ContentTypes.`text/plain(UTF-8)`) "DiskStorageOperations" should { "verify when the storage exists" in { val verify = new DiskStorageOperations.VerifyDiskStorage[IO](storage) verify.apply.accepted } "save and fetch files" in { val save = new DiskStorageOperations.SaveDiskFile[IO](storage) val fetch = new DiskStorageOperations.FetchDiskFile[IO]() val source = genSource val attr = save.apply(resId, fileDesc, source).ioValue attr.bytes shouldEqual 16L attr.filename shouldEqual fileDesc.filename attr.mediaType shouldEqual fileDesc.mediaType.value attr.location shouldEqual Uri(s"file:///tmp/${mangle(project, attr.uuid, "my%20file.txt")}") attr.path shouldEqual attr.location.path.tail.tail.tail val fetched = fetch.apply(attr).ioValue consume(source) shouldEqual consume(fetched) } "not link files" in { val link = new DiskStorageOperations.LinkDiskFile[IO]() link.apply(resId, fileDesc, Uri.Path("/foo")).failed[KgError] shouldEqual KgError.UnsupportedOperation } } }
Example 88
Source File: SparqlLinkSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.indexing import java.time.{Clock, Instant, ZoneId} import ch.epfl.bluebrain.nexus.commons.sparql.client.SparqlResults.Binding import ch.epfl.bluebrain.nexus.kg.config.Schemas._ import ch.epfl.bluebrain.nexus.kg.indexing.SparqlLink.{SparqlExternalLink, SparqlResourceLink} import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.Vocabulary._ import ch.epfl.bluebrain.nexus.rdf.implicits._ import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv class SparqlLinkSpec extends AnyWordSpecLike with Matchers with OptionValues { "A SparqlLink" should { val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) val id = url"http://example.com/id" val property = url"http://example.com/friend" val property2 = url"http://example.com/friend2" val paths = List(property, property2) "build SparqlExternalLink from SPARQL response" in { val bindings = Map( "s" -> Binding("uri", id.asString), "paths" -> Binding("literal", s"${property.asString} ${property2.asString}") ) SparqlExternalLink(bindings).value shouldEqual SparqlExternalLink(id, paths) } "build SparqlResourceLink from SPARQL response" in { val self = url"http://127.0.0.1:8080/v1/resources/myorg/myproject/_/id" val project = url"http://127.0.0.1:8080/v1/projects/myorg/myproject/" val author = url"http://127.0.0.1:8080/v1/realms/myrealm/users/me" val bindings = Map( "s" -> Binding("uri", id.asString), "paths" -> Binding("literal", s"${property.asString} ${property2.asString}"), "_rev" -> Binding("literal", "1", datatype = Some(xsd.long.asString)), "_self" -> Binding("uri", self.asString), "_project" -> Binding("uri", project.asString), "types" -> Binding("literal", s"${nxv.Resolver.value.asString} ${nxv.Schema.value.asString}"), "_constrainedBy" -> Binding("uri", unconstrainedSchemaUri.asString), "_createdBy" -> Binding("uri", author.asString), "_updatedBy" -> Binding("uri", author.asString), "_createdAy" -> Binding("uri", author.asString), "_createdAt" -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)), "_updatedAt" -> Binding("literal", clock.instant().toString, datatype = Some(xsd.dateTime.asString)), "_deprecated" -> Binding("literal", "false", datatype = Some(xsd.boolean.asString)) ) SparqlResourceLink(bindings).value shouldEqual SparqlResourceLink( id, project, self, 1L, Set[AbsoluteIri](nxv.Schema.value, nxv.Resolver.value), false, clock.instant(), clock.instant(), author, author, unconstrainedRef, paths ) } } }
Example 89
Source File: StatisticsSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.indexing import java.util.regex.Pattern.quote import ch.epfl.bluebrain.nexus.commons.test.Resources import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.indexing.Statistics.{CompositeViewStatistics, ViewStatistics} import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.commons.circe.syntax._ import ch.epfl.bluebrain.nexus.service.config.ServiceConfig import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv import io.circe.Printer import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{Inspectors, OptionValues} class StatisticsSpec extends AnyWordSpecLike with Matchers with OptionValues with TestHelper with Resources with Inspectors { "Statistics" should { val sourceId = genIri val projectionId = nxv.defaultElasticSearchIndex.value val single: Statistics = ViewStatistics(10L, 1L, 2L, 12L, None, None, None) val singleJson = jsonContentOf("/view/statistics.json").removeKeys("projectionId") val composite: Statistics = CompositeViewStatistics(IdentifiedProgress(sourceId, projectionId, single.asInstanceOf[ViewStatistics])) val compositeJson = jsonContentOf("/view/composite_statistics.json", Map(quote("{sourceId}") -> sourceId.asString)) val printer: Printer = Printer.noSpaces.copy(dropNullValues = true) "be encoded" in { forAll(List(single -> singleJson, composite -> compositeJson)) { case (model, json) => printer.print(model.asJson.sortKeys(ServiceConfig.orderedKeys)) shouldEqual printer.print(json) } } } }
Example 90
Source File: LinkDescriptionSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resources import akka.http.scaladsl.model.Uri.Path import akka.http.scaladsl.model.{ContentType, ContentTypes} import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Randomness} import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.kg.resources.Rejection.InvalidResourceFormat import ch.epfl.bluebrain.nexus.kg.resources.file.File.{FileDescription, LinkDescription} import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.Json import io.circe.syntax._ import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class LinkDescriptionSpec extends AnyWordSpecLike with Matchers with TestHelper with Randomness with EitherValues with OptionValues { abstract private class Ctx { val id = Id(ProjectRef(genUUID), genIri) val p = genString() + "/" + genString() val f = genString() val m = "application/json" def jsonLink(mediaType: String = m, filename: String = f, path: String = p): Json = Json.obj("filename" -> filename.asJson, "path" -> path.asJson, "mediaType" -> mediaType.asJson) } "A Link Description" should { "be decoded correctly" in new Ctx { LinkDescription(id, jsonLink()).rightValue shouldEqual LinkDescription(Path(p), Some(f), ContentType.parse(m).toOption) LinkDescription(id, Json.obj("path" -> p.asJson)).rightValue shouldEqual LinkDescription(Path(p), None, None) } "accept missing filename" in new Ctx { LinkDescription(id, jsonLink().removeKeys("filename")).rightValue shouldEqual LinkDescription(Path(p), None, ContentType.parse(m).toOption) } "reject empty filename" in new Ctx { LinkDescription(id, jsonLink(filename = "")).leftValue shouldBe a[InvalidResourceFormat] } "accept missing mediaType" in new Ctx { LinkDescription(id, jsonLink().removeKeys("mediaType")).rightValue shouldEqual LinkDescription(Path(p), Some(f), None) } "reject wrong mediaType format" in new Ctx { LinkDescription(id, jsonLink(mediaType = genString())).leftValue shouldBe a[InvalidResourceFormat] } "reject missing path" in new Ctx { LinkDescription(id, jsonLink().removeKeys("path")).leftValue shouldBe a[InvalidResourceFormat] } "be converted to a FileDescription correctly" in new Ctx { val fileDesc1 = FileDescription.from(LinkDescription(Path("/foo/bar/file.ext"), None, None)) fileDesc1.filename shouldEqual "file.ext" fileDesc1.mediaType shouldEqual None fileDesc1.defaultMediaType shouldEqual ContentTypes.`application/octet-stream` val fileDesc2 = FileDescription.from(LinkDescription(Path("/foo/bar/somedir/"), None, ContentType.parse(m).toOption)) fileDesc2.filename shouldEqual "somedir" fileDesc2.mediaType.value shouldEqual ContentTypes.`application/json` val fileDesc3 = FileDescription.from(LinkDescription(Path("/foo/bar/baz"), Some("file.json"), ContentType.parse(m).toOption)) fileDesc3.filename shouldEqual "file.json" fileDesc3.mediaType.value shouldEqual ContentTypes.`application/json` } } }
Example 91
Source File: CompositeResolutionSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resolve import java.time.Clock import java.util.UUID import java.util.regex.Pattern.quote import cats.instances.try_._ import cats.syntax.show._ import ch.epfl.bluebrain.nexus.commons.test.Resources import ch.epfl.bluebrain.nexus.kg.resources.Ref import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{OptionValues, TryValues} import scala.util.Try class CompositeResolutionSpec extends AnyWordSpecLike with Resources with Matchers with TryValues with OptionValues { "CompositeResolution" should { implicit val clock = Clock.systemUTC val resource1Uri: AbsoluteIri = url"http://nexus.example.com/resources/static/${UUID.randomUUID().toString}" val resource2Uri: AbsoluteIri = url"http://nexus.example.com/resources/static/${UUID.randomUUID().toString}" val staticResolution1 = StaticResolution[Try]( Map( resource1Uri -> jsonContentOf( "/resolve/simple-resource.json", Map(quote("{id}") -> resource1Uri.show, quote("{random}") -> UUID.randomUUID().toString) ) ) ) val staticResolution2 = StaticResolution[Try]( Map( resource2Uri -> jsonContentOf( "/resolve/simple-resource.json", Map(quote("{id}") -> resource2Uri.show, quote("{random}") -> UUID.randomUUID().toString) ) ) ) val staticResolution3 = StaticResolution[Try]( Map( resource1Uri -> jsonContentOf( "/resolve/simple-resource.json", Map(quote("{id}") -> resource1Uri.show, quote("{random}") -> UUID.randomUUID().toString) ) ) ) val compositeResolution = CompositeResolution[Try](List(staticResolution1, staticResolution2, staticResolution3)) "return the resource from the first resolver which returns the resource" in { compositeResolution.resolve(Ref(resource1Uri)).success.value.value shouldEqual staticResolution1 .resolve(Ref(resource1Uri)) .success .value .value compositeResolution.resolve(Ref(resource2Uri)).success.value.value shouldEqual staticResolution2 .resolve(Ref(resource2Uri)) .success .value .value } } }
Example 92
Source File: KeyValueStoreConfigSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.cache import java.io.File import ch.epfl.bluebrain.nexus.sourcing.RetryStrategyConfig import com.typesafe.config.ConfigFactory import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.OptionValues import org.scalatest.matchers.should.Matchers import pureconfig.generic.auto._ import pureconfig.ConfigSource import scala.concurrent.duration._ class KeyValueStoreConfigSpec extends AnyWordSpecLike with Matchers with OptionValues { val config = KeyValueStoreConfig( 10.seconds, 10.seconds, RetryStrategyConfig("exponential", 100.millis, 10.hours, 7, 500.millis) ) "KeyValueStoreConfig" should { "read from config file" in { val readConfig = ConfigFactory.parseFile(new File(getClass.getResource("/commons/example-store.conf").toURI)) ConfigSource.fromConfig(readConfig).at("key-value-store").loadOrThrow[KeyValueStoreConfig] } } }