org.scalatest.wordspec.AnyWordSpecLike Scala Examples
The following examples show how to use org.scalatest.wordspec.AnyWordSpecLike.
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: 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 2
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 3
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 4
Source File: TaggingAdapterSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.persistence import java.time.{Clock, Instant, ZoneId} import akka.persistence.journal.Tagged import cats.syntax.show._ import ch.epfl.bluebrain.nexus.iam.client.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.config.Schemas._ import ch.epfl.bluebrain.nexus.kg.config.Vocabulary._ import ch.epfl.bluebrain.nexus.kg.persistence.TaggingAdapterSpec.Other import ch.epfl.bluebrain.nexus.kg.resources.Event._ import ch.epfl.bluebrain.nexus.kg.resources.{Id, OrganizationRef, Ref} import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import io.circe.Json import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TaggingAdapterSpec extends AnyWordSpecLike with Matchers with Inspectors with TestHelper { "A TaggingAdapter" should { val clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) def genJson(): Json = Json.obj("key" -> Json.fromString(genString())) val adapter = new TaggingAdapter() val orgRef = OrganizationRef(genUUID) val id = Id(ProjectRef(genUUID), nxv.projects) val mapping = Map( Set( s"type=${nxv.Schema.value.show}", s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event" ) -> Created(id, orgRef, Ref(shaclSchemaUri), Set(nxv.Schema, nxv.Resource), genJson(), clock.instant(), Anonymous), Set( s"type=${nxv.Resolver.value.show}", s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event" ) -> Updated(id, orgRef, 1L, Set(nxv.Resource, nxv.Resolver), genJson(), clock.instant(), Anonymous), Set(s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event") -> Deprecated(id, orgRef, 1L, Set(nxv.Resource), clock.instant(), Anonymous), Set(s"project=${id.parent.id}", s"org=${orgRef.show}", "event") -> TagAdded(id, orgRef, 2L, 1L, "tag", clock.instant(), Anonymous) ) "set the appropriate tags" in { forAll(mapping.toList) { case (tags, ev) => adapter.toJournal(ev) shouldEqual Tagged(ev, tags) } } "return an empty manifest" in { adapter.manifest(Other(genString())) shouldEqual "" } } } object TaggingAdapterSpec { private[persistence] final case class Other(value: String) }
Example 5
Source File: QueryBuilderSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.search import ch.epfl.bluebrain.nexus.commons.test.Resources import ch.epfl.bluebrain.nexus.kg.config.Vocabulary._ import ch.epfl.bluebrain.nexus.kg.routes.SearchParams 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 class QueryBuilderSpec extends AnyWordSpecLike with Matchers with Resources { "QueryBuilder" should { val schema: AbsoluteIri = url"http://nexus.example.com/testSchema" "build query with deprecation" in { val expected = jsonContentOf("/search/query-deprecation.json") QueryBuilder.queryFor(SearchParams(deprecated = Some(true))) shouldEqual expected } "build empty query" in { val expected = jsonContentOf("/search/query.json") QueryBuilder.queryFor(SearchParams()) shouldEqual expected } "build query with schema and deprecation" in { val expected = jsonContentOf("/search/query-schema-deprecation.json") QueryBuilder.queryFor(SearchParams(deprecated = Some(true), schema = Some(schema))) shouldEqual expected } "build query with schema, deprecation and several types" in { val expected = jsonContentOf("/search/query-schema-deprecation-types.json") val params = SearchParams(deprecated = Some(true), schema = Some(schema), types = List(nxv.File.value, nxv.View.value)) QueryBuilder.queryFor(params) shouldEqual expected } "build query with schema" in { val expected = jsonContentOf("/search/query-schema.json") QueryBuilder.queryFor(SearchParams(schema = Some(schema))) shouldEqual expected } "build query with schema, rev, createdBy and full text search" in { val expected = jsonContentOf("/search/query-schema-rev-createdBy-q.json") val params = SearchParams( schema = Some(schema), rev = Some(1), createdBy = Some(url"http://nexus.example.com/user"), q = Some("some text") ) QueryBuilder.queryFor(params) shouldEqual expected } } }
Example 6
Source File: QueryResultEncoderSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.search import java.time.Instant import java.util.regex.Pattern.quote import akka.http.scaladsl.model.Uri import akka.http.scaladsl.model.Uri.Query import ch.epfl.bluebrain.nexus.commons.circe.syntax._ import ch.epfl.bluebrain.nexus.commons.search.QueryResult.{ScoredQueryResult, UnscoredQueryResult} import ch.epfl.bluebrain.nexus.commons.search.QueryResults import ch.epfl.bluebrain.nexus.commons.search.QueryResults.{ScoredQueryResults, UnscoredQueryResults} import ch.epfl.bluebrain.nexus.commons.test.{Randomness, Resources} import ch.epfl.bluebrain.nexus.kg.config.AppConfig import ch.epfl.bluebrain.nexus.kg.config.AppConfig.HttpConfig import ch.epfl.bluebrain.nexus.kg.search.QueryResultEncoder._ import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.Json import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class QueryResultEncoderSpec extends AnyWordSpecLike with Matchers with Resources with Randomness { implicit val orderedKeys = AppConfig.orderedKeys val org = genString() val proj = genString() val schema = genString() val now = Instant.now() implicit val http = HttpConfig("", 0, "v1", "http://nexus.com") implicit val uri = Uri(s"http://nexus.com/resources/$org/$proj/$schema?type=someType&from=10&size=10") val before = now.minusSeconds(60) "QueryResultsEncoder" should { def json(id: AbsoluteIri, createdAt: Instant): Json = jsonContentOf( "/resources/es-metadata.json", Map( quote("{id}") -> id.asString, quote("{org}") -> org, quote("{proj}") -> proj, quote("{schema}") -> schema, quote("{instant}") -> createdAt.toString ) ) deepMerge Json.obj("_original_source" -> Json.fromString(Json.obj("k" -> Json.fromInt(1)).noSpaces)) "encode ScoredQueryResults" in { val results: QueryResults[Json] = ScoredQueryResults[Json]( 3, 0.3f, List( ScoredQueryResult(0.3f, json(url"http://nexus.com/result1", before)), ScoredQueryResult(0.2f, json(url"http://nexus.com/result2", before)), ScoredQueryResult(0.1f, json(url"http://nexus.com/result3", now)) ), sort(now) ) results.asJson.sortKeys shouldEqual jsonContentOf( "/search/scored-query-results.json", Map( quote("{org}") -> org, quote("{proj}") -> proj, quote("{schema}") -> schema, quote("{before}") -> before.toString, quote("{lastElementCreated}") -> now.toString, quote("{after}") -> after(now) ) ) } "encode UnscoredQueryResults" in { val results: QueryResults[Json] = UnscoredQueryResults[Json]( 3, List( UnscoredQueryResult(json(url"http://nexus.com/result1", before)), UnscoredQueryResult(json(url"http://nexus.com/result2", before)), UnscoredQueryResult(json(url"http://nexus.com/result3", now)) ), sort(now) ) results.asJson.sortKeys shouldEqual jsonContentOf( "/search/unscored-query-results.json", Map( quote("{org}") -> org, quote("{proj}") -> proj, quote("{schema}") -> schema, quote("{before}") -> before.toString, quote("{lastElementCreated}") -> now.toString, quote("{after}") -> after(now) ) ) } } private def sort(instant: Instant): Option[String] = Some(Json.arr(Json.fromString(instant.toString)).noSpaces) private def after(instant: Instant): String = Query("after" -> List(Json.fromString(instant.toString)).asJson.noSpaces).toString() }
Example 7
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 8
Source File: RemoteDiskStorageOperationsSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.storage import akka.http.scaladsl.model.ContentTypes._ import akka.http.scaladsl.model.Uri import cats.effect.IO import ch.epfl.bluebrain.nexus.commons.test.io.IOEitherValues import ch.epfl.bluebrain.nexus.commons.test.{ActorSystemFixture, Resources} import ch.epfl.bluebrain.nexus.iam.client.types.{AuthToken, Permission} import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.resources.file.File.{Digest, FileAttributes, FileDescription} import ch.epfl.bluebrain.nexus.kg.resources.Id import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.kg.storage.Storage.RemoteDiskStorage import ch.epfl.bluebrain.nexus.storage.client.StorageClient import ch.epfl.bluebrain.nexus.storage.client.types.FileAttributes.{Digest => StorageDigest} import ch.epfl.bluebrain.nexus.storage.client.types.{FileAttributes => StorageFileAttributes} import org.mockito.{IdiomaticMockito, Mockito} import org.scalatest.BeforeAndAfter import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class RemoteDiskStorageOperationsSpec extends ActorSystemFixture("RemoteDiskStorageOperationsSpec") with AnyWordSpecLike with Matchers with BeforeAndAfter with IdiomaticMockito with IOEitherValues with Resources with TestHelper { private val endpoint = "http://nexus.example.com/v1" sealed trait Ctx { val cred = genString() implicit val token: Option[AuthToken] = Some(AuthToken(cred)) val path = Uri.Path(s"${genString()}/${genString()}") // format: off val storage = RemoteDiskStorage(ProjectRef(genUUID), genIri, 1L, false, false, "SHA-256", endpoint, Some(cred), genString(), Permission.unsafe(genString()), Permission.unsafe(genString()), 1024L) val attributes = FileAttributes(s"$endpoint/${storage.folder}/$path", path, s"${genString()}.json", `application/json`, 12L, Digest("SHA-256", genString())) // format: on } private val client = mock[StorageClient[IO]] before { Mockito.reset(client) } "RemoteDiskStorageOperations" should { "verify when storage exists" in new Ctx { client.exists(storage.folder) shouldReturn IO(true) val verify = new RemoteDiskStorageOperations.Verify[IO](storage, client) verify.apply.accepted } "verify when storage does not exists" in new Ctx { client.exists(storage.folder) shouldReturn IO(false) val verify = new RemoteDiskStorageOperations.Verify[IO](storage, client) verify.apply .rejected[String] shouldEqual s"Folder '${storage.folder}' does not exists on the endpoint '${storage.endpoint}'" } "fetch file" in new Ctx { val source = genSource client.getFile(storage.folder, path) shouldReturn IO(source) val fetch = new RemoteDiskStorageOperations.Fetch[IO](storage, client) val resultSource = fetch.apply(attributes).ioValue consume(resultSource) shouldEqual consume(source) } "link file" in new Ctx { val id = Id(storage.ref, genIri) val sourcePath = Uri.Path(s"${genString()}/${genString()}") val destRelativePath = Uri.Path(mangle(storage.ref, attributes.uuid, attributes.filename)) client.moveFile(storage.folder, sourcePath, destRelativePath) shouldReturn IO( StorageFileAttributes( attributes.location, attributes.bytes, StorageDigest(attributes.digest.algorithm, attributes.digest.value), attributes.mediaType ) ) val link = new RemoteDiskStorageOperations.Link[IO](storage, client) link .apply(id, FileDescription(attributes.uuid, attributes.filename, Some(attributes.mediaType)), sourcePath) .ioValue shouldEqual attributes.copy(path = destRelativePath) } } }
Example 9
Source File: CryptoSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.storage import java.util.Base64 import ch.epfl.bluebrain.nexus.commons.test.Randomness import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class CryptoSpec extends AnyWordSpecLike with Matchers with Randomness { private val accessKey = "AKIAIOSFODNN7EXAMPLE" private val secretKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" "The Crypto object" should { "always generate the same key for a given password" in { val key = Crypto.deriveKey("changeme", "salt") Base64.getEncoder.encodeToString(key.getEncoded) shouldEqual "FB4G2MHn/q6PXqpNkE1F5wBG7Ndsd9FtyeLcNQL0G40=" key.getAlgorithm shouldEqual "AES" key.getFormat shouldEqual "RAW" } "encode and decode secrets" in { val key = Crypto.deriveKey(genString(32), genString(16)) Crypto.decrypt(key, Crypto.encrypt(key, accessKey)) shouldEqual accessKey Crypto.decrypt(key, Crypto.encrypt(key, secretKey)) shouldEqual secretKey } } }
Example 10
Source File: RefSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resources import ch.epfl.bluebrain.nexus.kg.resources.Ref._ import cats.syntax.show._ import ch.epfl.bluebrain.nexus.commons.test.EitherValues import ch.epfl.bluebrain.nexus.rdf.Iri.{AbsoluteIri, Urn} import ch.epfl.bluebrain.nexus.rdf.implicits._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class RefSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues { "A Ref" should { "be constructed from an AbsoluteIri" in { val list = List[(AbsoluteIri, Ref)]( url"http://ex.com?rev=1&other=value" -> Revision(url"http://ex.com?other=value", 1L), url"http://ex.com?rev=1" -> Revision(url"http://ex.com", 1L), url"http://ex.com?tag=this&other=value" -> Ref.Tag(url"http://ex.com?other=value", "this"), url"http://ex.com?rev=1&tag=this&other=value" -> Revision(url"http://ex.com?other=value", 1L), url"http://ex.com?other=value" -> Latest(url"http://ex.com?other=value"), url"http://ex.com#fragment" -> Latest(url"http://ex.com#fragment"), Urn("urn:ex:a/b/c").rightValue -> Latest(Urn("urn:ex:a/b/c").rightValue), Urn("urn:ex:a/b/c?=rev=1").rightValue -> Revision(Urn("urn:ex:a/b/c").rightValue, 1L), Urn("urn:ex:a?=tag=this&other=value").rightValue -> Ref.Tag(Urn("urn:ex:a?=other=value").rightValue, "this") ) forAll(list) { case (iri, ref) => Ref(iri) shouldEqual ref } } "print properly" in { (Latest(url"http://ex.com#fragment"): Ref).show shouldEqual url"http://ex.com#fragment".show (Revision(url"http://ex.com?other=value", 1L): Ref).show shouldEqual url"http://ex.com?other=value".show + s" @ rev: '1'" (Ref.Tag(url"http://ex.com?other=value", "this"): Ref).show shouldEqual url"http://ex.com?other=value".show + s" @ tag: 'this'" } } }
Example 11
Source File: KryoSerializerInitSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.serializers import java.nio.file.Paths import akka.actor.ActorSystem import akka.serialization.SerializationExtension import akka.testkit.TestKit import ch.epfl.bluebrain.nexus.kg.TestHelper import io.altoo.akka.serialization.kryo.KryoSerializer import org.scalatest.TryValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class KryoSerializerInitSpec extends TestKit(ActorSystem("KryoSerializerInitSpec")) with AnyWordSpecLike with Matchers with TryValues with TestHelper { private val serialization = SerializationExtension(system) "A Path Kryo serialization" should { "succeed" in { val path = Paths.get("resources/application.conf") // Find the Serializer for it val serializer = serialization.findSerializerFor(path) serializer.getClass.equals(classOf[KryoSerializer]) shouldEqual true // Check serialization/deserialization val serialized = serialization.serialize(path) serialized.isSuccess shouldEqual true val deserialized = serialization.deserialize(serialized.get, path.getClass) deserialized.isSuccess shouldEqual true deserialized.success.value shouldEqual path } } }
Example 12
Source File: EmbeddedKafkaSpecSupport.scala From embedded-kafka-schema-registry with MIT License | 5 votes |
package net.manub.embeddedkafka.schemaregistry import java.net.{InetAddress, Socket} import net.manub.embeddedkafka.schemaregistry.EmbeddedKafkaSpecSupport.{ Available, NotAvailable, ServerStatus } import org.scalatest.Assertion import org.scalatest.concurrent.{Eventually, IntegrationPatience} import org.scalatest.matchers.should.Matchers import org.scalatest.time.{Milliseconds, Seconds, Span} import org.scalatest.wordspec.AnyWordSpecLike import scala.util.{Failure, Success, Try} trait EmbeddedKafkaSpecSupport extends AnyWordSpecLike with Matchers with Eventually with IntegrationPatience { implicit val config: PatienceConfig = PatienceConfig(Span(1, Seconds), Span(100, Milliseconds)) def expectedServerStatus(port: Int, expectedStatus: ServerStatus): Assertion = eventually { status(port) shouldBe expectedStatus } private def status(port: Int): ServerStatus = { Try(new Socket(InetAddress.getByName("localhost"), port)) match { case Failure(_) => NotAvailable case Success(_) => Available } } } object EmbeddedKafkaSpecSupport { sealed trait ServerStatus case object Available extends ServerStatus case object NotAvailable extends ServerStatus }
Example 13
Source File: ModelServiceIntegrationSpec.scala From full-scala-stack with Apache License 2.0 | 5 votes |
package api import akka.http.scaladsl.model.{ContentTypes, HttpEntity} import akka.http.scaladsl.testkit.ScalatestRouteTest import de.heikoseeberger.akkahttpupickle.UpickleSupport import model.{SampleModelObject, SimpleSearch} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.{AnyWordSpec, AnyWordSpecLike} import routes.ModelRoutes import upickle.default._ import util.ModelPickler import scala.concurrent.ExecutionContext class ModelServiceIntegrationSpec extends AnyWordSpec with Matchers with ScalatestRouteTest with ZIODirectives with UpickleSupport with ModelPickler { val service = new ModelRoutes with LiveEnvironment //TODO test your route here, we would probably not have a test like the one below in reality, since it's super simple. "The Service" should { "return one objects on a get" in { Get("/api/sampleModelObject/1") ~> service.apiRoute("") ~> check { val res = responseAs[Seq[SampleModelObject]].headOption println(res) assert(res.nonEmpty) } } "return some objects on a search" in { Post("/api/sampleModelObject/search", HttpEntity(ContentTypes.`application/json`, write(SimpleSearch()))) ~> service.apiRoute("") ~> check { val str = responseAs[ujson.Value] val res = responseAs[Seq[SampleModelObject]] println(res) assert(res.nonEmpty) } } } }
Example 14
Source File: LithiumMultiNodeSpec.scala From lithium with Apache License 2.0 | 5 votes |
package com.swissborg.lithium import akka.actor.{ActorSystem, Address} import akka.cluster.Cluster import akka.cluster.MemberStatus._ import akka.remote.testconductor.RoleName import akka.remote.testkit.{MultiNodeConfig, MultiNodeSpec, MultiNodeSpecCallbacks} import akka.testkit.ImplicitSender import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.{Eventually, IntegrationPatience} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike abstract class LithiumMultiNodeSpec(val config: MultiNodeConfig) extends MultiNodeSpec(config) with MultiNodeSpecCallbacks with AnyWordSpecLike with Matchers with BeforeAndAfterAll with ImplicitSender with Eventually with IntegrationPatience { override def beforeAll(): Unit = multiNodeSpecBeforeAll() override def afterAll(): Unit = multiNodeSpecAfterAll() private val addresses: Map[RoleName, Address] = roles.map(r => r -> node(r).address).toMap protected def addressOf(roleName: RoleName): Address = addresses(roleName) protected def waitToBecomeUnreachable(roleNames: RoleName*): Unit = awaitCond(allUnreachable(roleNames: _*)) protected def waitForSurvivors(roleNames: RoleName*): Unit = awaitCond(allSurvivors(roleNames: _*)) protected def waitForUp(roleNames: RoleName*): Unit = awaitCond(allUp(roleNames: _*)) protected def waitForSelfDowning(implicit system: ActorSystem): Unit = awaitCond(downedItself) protected def waitForAllLeaving(roleNames: RoleName*): Unit = awaitCond(allLeaving(roleNames: _*)) protected def waitExistsAllDownOrGone(groups: Seq[Seq[RoleName]]): Unit = awaitCond(existsAllDownOrGone(groups)) private def allUnreachable(roleNames: RoleName*): Boolean = roleNames.forall( role => Cluster(system).state.unreachable.exists(_.address === addressOf(role)) ) private def allSurvivors(roleNames: RoleName*): Boolean = roleNames.forall(role => Cluster(system).state.members.exists(_.address === addressOf(role))) private def allUp(roleNames: RoleName*): Boolean = roleNames.forall( role => Cluster(system).state.members.exists(m => m.address === addressOf(role) && m.status === Up) ) private def existsAllDownOrGone(groups: Seq[Seq[RoleName]]): Boolean = groups.exists(group => allLeaving(group: _*)) private def downedItself(implicit system: ActorSystem): Boolean = { val selfAddress = Cluster(system).selfAddress Cluster(system).state.members .exists( m => m.address === selfAddress && (m.status === Exiting || m.status === Down || m.status === Removed) ) } private def allLeaving(roleNames: RoleName*): Boolean = roleNames.forall { role => val members = Cluster(system).state.members val unreachable = Cluster(system).state.unreachable val address = addressOf(role) unreachable.isEmpty && // no unreachable members (members.exists(m => m.address === address && (m.status === Down || m.status === Exiting)) || // member is down !members.exists(_.address === address)) // member is not in the cluster } }
Example 15
Source File: LithiumSpec.scala From lithium with Apache License 2.0 | 5 votes |
package com.swissborg.lithium import cats.implicits._ import cats.{Applicative, Functor, Monoid} import com.swissborg.lithium.ArbitraryStrategy._ import com.swissborg.lithium.instances.ArbitraryTestInstances import com.swissborg.lithium.strategy._ import com.swissborg.lithium.utils.PostResolution import org.scalacheck.Arbitrary import org.scalactic.anyvals._ import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.Assertion import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import org.scalatest.matchers.should.Matchers trait LithiumSpec extends AnyWordSpecLike with Matchers with ScalaCheckPropertyChecks with ArbitraryTestInstances { implicit override val generatorDrivenConfig: PropertyCheckConfiguration = PropertyCheckConfiguration(minSuccessful = PosInt(1000), maxDiscardedFactor = PosZDouble(5), minSize = PosZInt(0), sizeRange = PosZInt(100), workers = PosInt(8)) final def simulate[F[_]: Functor, Strat[_[_]], S <: Scenario: Arbitrary](name: String)( run: F[Assertion] => Assertion )(implicit strategy: ArbitraryStrategy[Strat[F]], ev: Strat[F] <:< Strategy[F], M: Monoid[F[PostResolution]]): Unit = name in { forAll { simulation: Simulation[F, Strat, S] => run(simulation.splitBrainResolved.map(_ shouldBe true)) } } final def simulateWithNonCleanPartitions[F[_]: Applicative, Strat[_[_]], S <: Scenario: Arbitrary](name: String)( run: F[Assertion] => Assertion )(implicit strategy: ArbitraryStrategy[Strat[F]], ev: Strat[F] <:< Strategy[F], M: Monoid[F[PostResolution]]): Unit = simulate[F, Union[*[_], Strat, IndirectlyConnected], WithNonCleanPartitions[S]](name)(run) }
Example 16
Source File: KeepRefereeSuite.scala From lithium with Apache License 2.0 | 5 votes |
package com.swissborg.lithium package strategy import akka.actor.Address import akka.cluster.ClusterEvent.CurrentClusterState import akka.cluster.MemberStatus.Up import akka.cluster.swissborg.TestMember import cats.Id import org.scalatest.wordspec.AnyWordSpecLike import scala.collection.immutable.SortedSet import org.scalatest.matchers.should.Matchers class KeepRefereeSuite extends AnyWordSpecLike with Matchers { private val aa = TestMember(Address("akka", "sys", "a", 2552), Up) private val bb = TestMember(Address("akka", "sys", "b", 2552), Up) private val cc = TestMember(Address("akka", "sys", "c", 2552), Up) private val referee = aa.address.toString "KeepReferee" must { "down the unreachable nodes when being the referee node and reaching enough nodes" in { val w = WorldView.fromSnapshot(aa, CurrentClusterState(SortedSet(aa, bb, cc), Set(bb), seenBy = Set.empty)) new KeepReferee[Id](KeepRefereeConfig(referee, 1)).takeDecision(w) should ===( Decision.DownUnreachable(w) ) } "down the reachable nodes when being the referee and not reaching enough nodes" in { val w = WorldView.fromSnapshot(aa, CurrentClusterState(SortedSet(aa, bb, cc), Set(bb), seenBy = Set.empty)) new strategy.KeepReferee[Id](KeepRefereeConfig(referee, 3)).takeDecision(w) should ===( Decision.DownReachable(w) ) } "down the unreachable nodes when the referee is reachable and reaching enough nodes" in { val w = WorldView.fromSnapshot(cc, CurrentClusterState(SortedSet(aa, bb, cc), Set(bb), seenBy = Set.empty)) new strategy.KeepReferee[Id](KeepRefereeConfig(referee, 1)).takeDecision(w) should ===( Decision.DownUnreachable(w) ) } "down the reachable nodes when the referee is reachable and not reaching enough nodes" in { val w = WorldView.fromSnapshot(cc, CurrentClusterState(SortedSet(aa, bb, cc), Set(bb), seenBy = Set.empty)) new strategy.KeepReferee[Id](KeepRefereeConfig(referee, 3)).takeDecision(w) should ===( Decision.DownReachable(w) ) } "down the reachable nodes when the referee is unreachable" in { val w = WorldView.fromSnapshot(bb, CurrentClusterState(SortedSet(aa, bb, cc), Set(aa), seenBy = Set.empty)) new strategy.KeepReferee[Id](KeepRefereeConfig(referee, 1)).takeDecision(w) should ===( Decision.DownReachable(w) ) new strategy.KeepReferee[Id](KeepRefereeConfig(referee, 3)).takeDecision(w) should ===( Decision.DownReachable(w) ) } } }
Example 17
Source File: ClusterInternalsPublisherSpec.scala From lithium with Apache License 2.0 | 5 votes |
package akka.cluster.swissborg import akka.actor.ActorSystem import akka.cluster.ClusterEvent.{ReachabilityChanged, SeenChanged} import akka.cluster.Reachability import akka.testkit.{ImplicitSender, TestKit, TestProbe} import com.swissborg.lithium.internals.{LithiumReachabilityChanged, LithiumSeenChanged} import org.scalatest.BeforeAndAfterAll import org.scalatest.wordspec.AnyWordSpecLike import scala.collection.immutable.IndexedSeq import org.scalatest.matchers.should.Matchers class ClusterInternalsPublisherSpec extends TestKit(ActorSystem("lithium")) with ImplicitSender with AnyWordSpecLike with Matchers with BeforeAndAfterAll { override def afterAll(): Unit = TestKit.shutdownActorSystem(system) "ClusterInternalsPublisher" must { "convert and publish ReachabilityChanged events" in { system.actorOf(ClusterInternalsPublisher.props) val probe = TestProbe() system.eventStream.subscribe(probe.ref, classOf[LithiumReachabilityChanged]) system.eventStream.publish(ReachabilityChanged(Reachability(IndexedSeq.empty[Reachability.Record], Map.empty))) probe.expectMsgType[LithiumReachabilityChanged] } "convert and publish SeenChanged events" in { system.actorOf(ClusterInternalsPublisher.props) val probe = TestProbe() system.eventStream.subscribe(probe.ref, classOf[LithiumSeenChanged]) system.eventStream.publish(SeenChanged(false, Set.empty)) probe.expectMsg(LithiumSeenChanged(false, Set.empty)) } } }
Example 18
Source File: JobRepoTest.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.server.repository import fusion.inject.guice.testkit.GuiceApplicationTestkit import fusion.json.jackson.ScalaObjectMapper import mass.db.slick.{ PgProfile, SqlComponent } import mass.job.db.model.QrtzModels import mass.model.job.JobTrigger import org.scalatest.wordspec.AnyWordSpecLike class JobRepoTest extends GuiceApplicationTestkit with AnyWordSpecLike { private val qrtzModels = injectInstance[QrtzModels] private val objectMapper = injectInstance[ScalaObjectMapper] private val sqlSystem = injectInstance[SqlComponent] private val profile = injectInstance[PgProfile] import profile.api._ "JobRepoTest" should { val db = sqlSystem.db "filterWhere" in { val action = qrtzModels.QrtzJobDetailsModel.sortBy(_.createdAt.desc).take(30).result action.statements.foreach(println) val result = db.run(action).futureValue println(result) } "log" in { qrtzModels.QrtzTriggerLogModel.schema.createStatements.foreach(println) } } "JSON" should { "trigger" in { val jstr = """{"key":"kettle","triggerType":"CRON","startTime":"2018-09-12T13:00:11+08","endTime":null}""" val trigger = objectMapper.readValue[JobTrigger](jstr) println(trigger) } } }
Example 19
Source File: JGreeterServiceSpec.scala From akka-grpc with Apache License 2.0 | 5 votes |
package example.myapp.helloworld import scala.concurrent.Await import org.scalatest.BeforeAndAfterAll import scala.concurrent.duration._ import akka.actor.ActorSystem import akka.grpc.GrpcClientSettings import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory import org.scalatest.concurrent.ScalaFutures import org.scalatest.time.Span import example.myapp.helloworld.grpc._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class JGreeterServiceSpec extends Matchers with AnyWordSpecLike with BeforeAndAfterAll with ScalaFutures { implicit val patience = PatienceConfig(5.seconds, Span(100, org.scalatest.time.Millis)) implicit val serverSystem: ActorSystem = { // important to enable HTTP/2 in server ActorSystem's config val conf = ConfigFactory .parseString("akka.http.server.preview.enable-http2 = on") .withFallback(ConfigFactory.defaultApplication()) val sys = ActorSystem("GreeterServer", conf) // make sure servers are bound before using client GreeterServer.run(sys).toCompletableFuture.get PowerGreeterServer.run(sys).toCompletableFuture.get sys } val clientSystem = ActorSystem("GreeterClient") implicit val mat = ActorMaterializer.create(clientSystem) implicit val ec = clientSystem.dispatcher val clients = Seq(8090, 8091).map { port => GreeterServiceClient.create(GrpcClientSettings.connectToServiceAt("127.0.0.1", port).withTls(false), clientSystem) } override def afterAll: Unit = { Await.ready(clientSystem.terminate(), 5.seconds) Await.ready(serverSystem.terminate(), 5.seconds) } "GreeterService" should { "reply to single request" in { val reply = clients.head.sayHello(HelloRequest.newBuilder.setName("Alice").build()) reply.toCompletableFuture.get should ===(HelloReply.newBuilder.setMessage("Hello, Alice").build()) } } "GreeterServicePowerApi" should { Seq( ("Authorization", "Hello, Alice (authenticated)"), ("WrongHeaderName", "Hello, Alice (not authenticated)")).zipWithIndex.foreach { case ((mdName, expResp), ix) => s"use metadata in replying to single request ($ix)" in { val reply = clients.last .sayHello() .addHeader(mdName, "<some auth token>") .invoke(HelloRequest.newBuilder.setName("Alice").build()) reply.toCompletableFuture.get should ===(HelloReply.newBuilder.setMessage(expResp).build()) } } } }
Example 20
Source File: AkkaDiscoveryNameResolverSpec.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.internal import java.net.InetSocketAddress import akka.actor.ActorSystem import akka.grpc.{ GrpcClientSettings, GrpcServiceException } import akka.testkit.TestKit import io.grpc.Status import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.time.{ Millis, Seconds, Span } import org.scalatest.wordspec.AnyWordSpecLike import scala.collection.JavaConverters._ class AkkaDiscoveryNameResolverSpec extends TestKit(ActorSystem()) with AnyWordSpecLike with Matchers with ScalaFutures { implicit val ex = system.dispatcher implicit override val patienceConfig = PatienceConfig(timeout = scaled(Span(2, Seconds)), interval = scaled(Span(5, Millis))) "The AkkaDiscovery-backed NameResolver" should { "correctly report an error for an unknown hostname" in { val host = "example.invalid" val resolver = AkkaDiscoveryNameResolver(GrpcClientSettings.connectToServiceAt(host, 80)) val probe = new NameResolverListenerProbe() resolver.start(probe) val exception = probe.future.failed.futureValue.asInstanceOf[GrpcServiceException] exception shouldBe an[GrpcServiceException] exception.status.getCode == Status.UNKNOWN.getCode // FIXME: This description is not portable - it arises from native function response, which differs by OS // exception.status.getDescription should equal(host + ": Name or service not known") } "support serving a static host/port" in { // Unfortunately it needs to be an actually resolvable address... val host = "akka.io" val port = 4040 val resolver = AkkaDiscoveryNameResolver(GrpcClientSettings.connectToServiceAt(host, port)) val probe = new NameResolverListenerProbe() resolver.start(probe) val addresses = probe.future.futureValue match { case Seq(addressGroup) => addressGroup.getAddresses case _ => fail("Expected a single address group") } addresses.asScala.toSeq match { case Seq(address: InetSocketAddress) => address.getPort should be(port) address.getAddress.getHostName should be(host) case other => fail(s"Expected a single InetSocketAddress, got $other") } } } }
Example 21
Source File: AkkaDiscoveryNameResolverProviderSpec.scala From akka-grpc with Apache License 2.0 | 5 votes |
package akka.grpc.internal import java.net.URI import java.net.InetSocketAddress import java.util.{ List => JList } import scala.concurrent.ExecutionContext.Implicits._ import scala.concurrent.Future import scala.concurrent.Promise import scala.concurrent.duration._ import scala.collection.immutable import io.grpc.Attributes import io.grpc.NameResolver.Listener import io.grpc.EquivalentAddressGroup import akka.actor.ActorSystem import akka.discovery.Lookup import akka.discovery.ServiceDiscovery import akka.discovery.ServiceDiscovery.Resolved import akka.discovery.ServiceDiscovery.ResolvedTarget import akka.testkit.TestKit import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.time.{ Millis, Seconds, Span } import org.scalatest.wordspec.AnyWordSpecLike class AkkaDiscoveryNameResolverProviderSpec extends TestKit(ActorSystem()) with AnyWordSpecLike with Matchers with ScalaFutures { implicit override val patienceConfig = PatienceConfig(timeout = scaled(Span(2, Seconds)), interval = scaled(Span(5, Millis))) "AkkaDiscoveryNameResolverProviderSpec" should { "provide a NameResolver that uses the supplied serviceName" in { val serviceName = "testServiceName" val discovery = new ServiceDiscovery() { override def lookup(lookup: Lookup, resolveTimeout: FiniteDuration): Future[Resolved] = { lookup.serviceName should be(serviceName) Future.successful(Resolved(serviceName, immutable.Seq(ResolvedTarget("10.0.0.3", Some(4312), None)))) } } val provider = new AkkaDiscoveryNameResolverProvider( discovery, 443, portName = None, protocol = None, resolveTimeout = 3.seconds) val resolver = provider.newNameResolver(new URI("//" + serviceName), null) val addressGroupsPromise = Promise[List[EquivalentAddressGroup]] val listener = new Listener() { override def onAddresses(addresses: JList[EquivalentAddressGroup], attributes: Attributes): Unit = { import scala.collection.JavaConverters._ addressGroupsPromise.success(addresses.asScala.toList) } override def onError(error: io.grpc.Status): Unit = ??? } resolver.start(listener) val addressGroups = addressGroupsPromise.future.futureValue addressGroups.size should be(1) val addresses = addressGroups(0).getAddresses() addresses.size should be(1) val address = addresses.get(0).asInstanceOf[InetSocketAddress] address.getHostString() should be("10.0.0.3") address.getPort() should be(4312) } } }
Example 22
Source File: EtlWorkflowTest.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.workflow.etl import akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit import mass.rdp.RdpSystem import mass.rdp.etl.EtlWorkflow import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.Await import scala.concurrent.duration.Duration class EtlWorkflowTest extends ScalaTestWithActorTestKit with AnyWordSpecLike { var rdpSystem: RdpSystem = _ var etlWorkflow: EtlWorkflow = _ override protected def beforeAll(): Unit = { super.beforeAll() rdpSystem = RdpSystem(system) etlWorkflow = EtlWorkflow.fromXML(TestStub.graphXmlConfig, rdpSystem).get } override protected def afterAll(): Unit = { etlWorkflow.close() super.afterAll() } "EtlWorkflow" should { "show" in { etlWorkflow.connectors should not be empty etlWorkflow.connectors.foreach(c => println(s"connector: $c")) println(etlWorkflow.graph) etlWorkflow.connectors.foreach(println) println(etlWorkflow.graph.name) println(etlWorkflow.graph.graphSource) etlWorkflow.graph.graphFlows.foreach(println) println(etlWorkflow.graph.graphSink) } "run" in { val execution = etlWorkflow.run() val result = Await.result(execution.future, Duration.Inf) println(result) } } }
Example 23
Source File: EtlSchedulerWorkflowTest.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.workflow.etl import fusion.inject.guice.testkit.GuiceApplicationTestkit import mass.job.JobScheduler import mass.rdp.RdpSystem import org.scalatest.wordspec.AnyWordSpecLike class EtlSchedulerWorkflowTest extends GuiceApplicationTestkit with AnyWordSpecLike { private val rdpSystem: RdpSystem = injectInstance[RdpSystem] private val jobSystem: JobScheduler = injectInstance[JobScheduler] "EtlSchedulerWorkflowTest" should { "scheduler" in { // val conf = JobConf // .builder("test", "test") // .withCronExpress("10 * * * * ?") // .result // jobSystem.schedulerJob(conf, classOf[EtlJob], Map(EtlJob.WORKFLOW_STRING -> TestStub.graphConfig)) // // TimeUnit.MINUTES.sleep(5) } } }
Example 24
Source File: SchedulerTestkit.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.job import akka.actor.ActorSystem import akka.http.scaladsl.testkit.{ RouteTestTimeout, ScalatestRouteTest } import com.typesafe.scalalogging.StrictLogging import fusion.inject.guice.testkit.GuiceApplicationTestkit import fusion.json.jackson.http.JacksonSupport import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ abstract class SchedulerTestkit extends GuiceApplicationTestkit with AnyWordSpecLike with Matchers with BeforeAndAfterAll with ScalatestRouteTest with StrictLogging { override protected def createActorSystem(): ActorSystem = application.classicSystem implicit def routeTestTimeout: RouteTestTimeout = RouteTestTimeout(10.seconds) protected val jobScheduler: JobScheduler = injectInstance[JobScheduler] protected val jacksonSupport: JacksonSupport = injectInstance[JacksonSupport] }
Example 25
Source File: JobRunTest.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.job.component import fusion.inject.guice.testkit.GuiceApplicationTestkit import fusion.json.jackson.ScalaObjectMapper import mass.MassSettings import mass.job.JobSettings import mass.model.job.{ JobItem, Program } import org.scalatest.wordspec.AnyWordSpecLike class JobRunTest extends GuiceApplicationTestkit with AnyWordSpecLike { private val jobSettings = JobSettings(MassSettings(config)) private val objectMapper = injectInstance[ScalaObjectMapper] "JobRunTest" should { "run java" in { val item = JobItem(Program.JAVA, Seq(), "test.JavaMain") val result = JobRun.run(item, "test-java", jobSettings) println(objectMapper.prettyStringify(result)) result.exitValue shouldBe 0 result.start should be < result.end } "run scala" in { val item = JobItem(Program.SCALA, Seq(), "test.ScalaMain") val result = JobRun.run(item, "test-scala", jobSettings) println(objectMapper.prettyStringify(result)) result.exitValue shouldBe 0 result.start should be < result.end } "run bash -c" in { val item = JobItem(Program.SH, Seq("-c"), "echo 'ååå'") val result = JobRun.run(item, "test-bash", jobSettings) println(objectMapper.prettyStringify(result)) result.exitValue shouldBe 0 result.start should be < result.end } "run python -c" in { val item = JobItem(Program.PYTHON, Seq("-c"), "print('ååå')") val result = JobRun.run(item, "test-python", jobSettings) println(objectMapper.prettyStringify(result)) result.exitValue shouldBe 0 result.start should be < result.end } } }
Example 26
Source File: JobUtilsTest.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.job.util import java.nio.charset.StandardCharsets import java.nio.file.{ Files, Paths } import fusion.inject.guice.testkit.GuiceApplicationTestkit import fusion.json.jackson.ScalaObjectMapper import mass.MassSettings import mass.job.JobSettings import mass.message.job.JobUploadJobReq import org.scalatest.wordspec.AnyWordSpecLike class JobUtilsTest extends GuiceApplicationTestkit with AnyWordSpecLike { private implicit val ec = typedSystem.executionContext private val objectMapper = injectInstance[ScalaObjectMapper] private val jobSettings = JobSettings(MassSettings(configuration)) "JobService" should { "uploadJob" in { val fileName = "hello.zip" val originalFile = Paths.get(sys.props.get("user.dir").get + "/mass-job/src/universal/examples/sample-job/" + fileName) val file2 = originalFile.getParent.resolve("hello2.zip") Files.copy(originalFile, file2) println("file json string: " + objectMapper.stringify(file2)) val result = JobUtils.uploadJob(jobSettings, JobUploadJobReq(file2, fileName, StandardCharsets.UTF_8)).futureValue println(result) } } "conf" should { val str = """#key=sample |item { | name = "Hello world!" | program = "java" # JobēØåŗē±»åļ¼å½åęÆęJavaļ¼shļ¼bashļ¼ļ¼python | program-main = "hello.Hello" # åÆę§č”Javaäø»ē±»ååļ¼[éč¦å°ēØåŗęęJarå ćjob-programäøŗjavaę¶ęę |} |trigger { | trigger-type = "cron" # Jobē±»åļ¼å½åęÆęļ¼simplećcronćeventäøē§ | start-time = "2020-03-03 10:10:10" # Jobå¼å§ę§č”ę¶é“ļ¼åÆéļ¼ | end-time = "2020-03-13 10:10:10" # Jobē»ęę¶é“ļ¼åÆéļ¼ | repeat = 4 # Jobéå¤ę¬”ę°ļ¼job-typeäøŗsimpleę¶ęę | duration = 120.seconds # äø¤ę¬”Jobä¹é“ēę¶é“é“éļ¼job-typeäøŗsimpleę¶ęę | cron-express = "1 0 0 * * ?" # åŗäŗCRONēę„åč°åŗ¦é ē½®ļ¼job-typeäøŗcronę¶ęę |}""".stripMargin "parse" in { val either = JobUtils.parseJobConf(str) println(either) val req = either.toOption.value req.item.name shouldBe Some("Hello world!") } } }
Example 27
Source File: GreeterServiceSpec.scala From akka-grpc with Apache License 2.0 | 5 votes |
package example.myapp.helloworld import akka.actor.{ ActorSystem, ClassicActorSystemProvider } import akka.grpc.GrpcClientSettings import akka.stream.ActorMaterializer import com.typesafe.config.ConfigFactory import example.myapp.helloworld.grpc._ import org.junit.runner.RunWith import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.time.Span import org.scalatest.wordspec.AnyWordSpecLike import org.scalatestplus.junit.JUnitRunner import scala.concurrent.Await import scala.concurrent.duration._ @RunWith(classOf[JUnitRunner]) class GreeterSpec extends Matchers with AnyWordSpecLike with BeforeAndAfterAll with ScalaFutures { implicit val patience = PatienceConfig(10.seconds, Span(100, org.scalatest.time.Millis)) implicit val serverSystem: ActorSystem = { // important to enable HTTP/2 in server ActorSystem's config val conf = ConfigFactory .parseString("akka.http.server.preview.enable-http2 = on") .withFallback(ConfigFactory.defaultApplication()) val sys = ActorSystem("GreeterServer", conf) // make sure servers are bound before using client new GreeterServer(sys).run().futureValue new PowerGreeterServer(sys).run().futureValue sys } val clientSystem = ActorSystem("GreeterClient") implicit val mat = ActorMaterializer.create(clientSystem) implicit val ec = clientSystem.dispatcher val clients = Seq(8080, 8081).map { port => GreeterServiceClient( GrpcClientSettings .connectToServiceAt("127.0.0.1", port)(clientSystem.asInstanceOf[ClassicActorSystemProvider]) .withTls(false))(clientSystem.asInstanceOf[ClassicActorSystemProvider]) } override def afterAll: Unit = { Await.ready(clientSystem.terminate(), 5.seconds) Await.ready(serverSystem.terminate(), 5.seconds) } "GreeterService" should { "reply to single request" in { val reply = clients.head.sayHello(HelloRequest("Alice")) reply.futureValue should ===(HelloReply("Hello, Alice")) } } "GreeterServicePowerApi" should { Seq( ("Authorization", "Hello, Alice (authenticated)"), ("WrongHeaderName", "Hello, Alice (not authenticated)")).zipWithIndex.foreach { case ((mdName, expResp), ix) => s"use metadata in replying to single request ($ix)" in { val reply = clients.last.sayHello().addHeader(mdName, "<some auth token>").invoke(HelloRequest("Alice")) reply.futureValue should ===(HelloReply(expResp)) } } } }
Example 28
Source File: PgProfileTest.scala From fusion-data with Apache License 2.0 | 5 votes |
package mass.db.slick import fusion.inject.guice.testkit.GuiceApplicationTestkit import fusion.jdbc.FusionJdbc import org.scalatest.wordspec.AnyWordSpecLike class PgProfileTest extends GuiceApplicationTestkit with AnyWordSpecLike { private val profile = injectInstance[PgProfile] import profile.api._ private val db = databaseForDataSource(FusionJdbc(classicSystem).component) "test" in { val q = sql"select key from job_schedule".as[String] println("q.head: " + q.head) val result = db.run(q).futureValue println(result) } protected override def afterAll(): Unit = { db.close() super.afterAll() } }
Example 29
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 30
Source File: TarFlowSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.archives import java.nio.file.Files import java.time.{Clock, Instant, ZoneId} import akka.actor.ActorSystem import akka.stream.scaladsl.FileIO import akka.testkit.TestKit import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.storage.digestSink import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class TarFlowSpec extends TestKit(ActorSystem("TarFlowSpec")) with AnyWordSpecLike with Matchers with TestHelper with ScalaFutures { private implicit val ec = system.dispatcher private implicit val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault()) override implicit def patienceConfig: PatienceConfig = PatienceConfig(55.second, 150.milliseconds) "A TarFlow" should { "tar a bunch of sources" in { val digest = "3fef41c5afe7a7ee11ee9d556a564fb57784cc5247b24c6ca70783f396fa158a1c7952504d3e1aa441de20cf065d740eec454c6ffb7fbc4b6351b950ee51c886" val elems = 500 val contents = List.tabulate(2) { i => val content = (i until (i + elems)).toList.mkString(",") + "\n" ArchiveSource(content.length.toLong, s"some/path$i/$i.txt", produce(content)) } val path = Files.createTempFile("test", ".tar") TarFlow.write(contents).runWith(FileIO.toPath(path)).futureValue FileIO.fromPath(path).runWith(digestSink("SHA-512")).futureValue.value shouldEqual digest Files.delete(path) } } }
Example 31
Source File: ArchiveCacheSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.archives import java.time.{Clock, Instant, ZoneId} import cats.effect.{IO, Timer} import ch.epfl.bluebrain.nexus.admin.client.types.Project import ch.epfl.bluebrain.nexus.commons.test.ActorSystemFixture import ch.epfl.bluebrain.nexus.commons.test.io.IOOptionValues import ch.epfl.bluebrain.nexus.iam.client.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.archives.Archive.{File, Resource, ResourceDescription} import ch.epfl.bluebrain.nexus.kg.config.Settings import ch.epfl.bluebrain.nexus.kg.resources.Id import ch.epfl.bluebrain.nexus.kg.resources.syntax._ import org.scalatest.concurrent.Eventually import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class ArchiveCacheSpec extends ActorSystemFixture("ArchiveCacheSpec", true) with TestHelper with AnyWordSpecLike with Matchers with IOOptionValues with Eventually { override implicit def patienceConfig: PatienceConfig = PatienceConfig(10.second, 50.milliseconds) private val appConfig = Settings(system).appConfig private implicit val config = appConfig.copy(archives = appConfig.archives.copy(cacheInvalidateAfter = 500.millis, maxResources = 100)) private implicit val timer: Timer[IO] = IO.timer(system.dispatcher) private val cache: ArchiveCache[IO] = ArchiveCache[IO].unsafeToFuture().futureValue private implicit val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault()) private val instant = clock.instant() def randomProject() = { val instant = Instant.EPOCH // format: off Project(genIri, genString(), genString(), None, genIri, genIri, Map.empty, genUUID, genUUID, 1L, false, instant, genIri, instant, genIri) // format: on } "An archive cache" should { "write and read an Archive" in { val resId = Id(randomProject().ref, genIri) val resource1 = Resource(genIri, randomProject(), None, None, originalSource = true, None) val file1 = File(genIri, randomProject(), None, None, None) val archive = Archive(resId, instant, Anonymous, Set(resource1, file1)) val _ = cache.put(archive).value.some cache.get(archive.resId).value.some shouldEqual archive } "read a non existing resource" in { val resId = Id(randomProject().ref, genIri) cache.get(resId).value.ioValue shouldEqual None } "read after timeout" in { val resId = Id(randomProject().ref, genIri) val set = Set[ResourceDescription](Resource(genIri, randomProject(), None, None, originalSource = true, None)) val archive = Archive(resId, instant, Anonymous, set) val _ = cache.put(archive).value.some val time = System.currentTimeMillis() cache.get(resId).value.some shouldEqual archive eventually { cache.get(resId).value.ioValue shouldEqual None } val diff = System.currentTimeMillis() - time diff should be > config.archives.cacheInvalidateAfter.toMillis diff should be < config.archives.cacheInvalidateAfter.toMillis + 300 } } }
Example 32
Source File: TagSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resources 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 io.circe.Json import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TagSpec extends AnyWordSpecLike with Matchers with TestHelper with Randomness with EitherValues { private abstract class Ctx { val id = Id(ProjectRef(genUUID), genIri) def jsonTag(rev: Long, value: String): Json = Json.obj("@id" -> id.value.asString.asJson, "tag" -> value.asJson, "rev" -> rev.asJson) } "A Tag" should { "be converted to tag case class correctly" in new Ctx { val rev = genInt().toLong val tag = genString() Tag(id, jsonTag(rev, tag)).rightValue shouldEqual Tag(rev, tag) } "reject when rev is missing" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "tag" -> genString().asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'rev' field does not have the right format.") } "reject when rev is not a number" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "tag" -> genString().asJson, "rev" -> genString().asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'rev' field does not have the right format.") } "reject when tag is missing" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "rev" -> genInt().toLong.asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'tag' field does not have the right format.") } "reject when tag is empty" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "rev" -> genInt().toLong.asJson, "tag" -> "".asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'tag' field does not have the right format.") } "reject when tag is not a string" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "rev" -> genInt().toLong.asJson, "tag" -> genInt().asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'tag' field does not have the right format.") } } }
Example 33
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 34
Source File: FileAttributesSpec.scala From nexus-kg with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resources import akka.http.scaladsl.model.ContentTypes._ import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Randomness} import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.config.Vocabulary.nxv 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.FileAttributes import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.storage.client.types.FileAttributes.{Digest => StorageDigest} import ch.epfl.bluebrain.nexus.storage.client.types.{FileAttributes => StorageFileAttributes} import io.circe.Json import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class FileAttributesSpec extends AnyWordSpecLike with Matchers with TestHelper with Randomness with EitherValues { private abstract class Ctx { val id = Id(ProjectRef(genUUID), genIri) def jsonFileAttr( digest: StorageDigest, mediaType: String, location: String, bytes: Long, tpe: String = nxv.UpdateFileAttributes.prefix ): Json = Json.obj( "@id" -> id.value.asString.asJson, "@type" -> tpe.asJson, "digest" -> Json.obj("value" -> digest.value.asJson, "algorithm" -> digest.algorithm.asJson), "mediaType" -> mediaType.asJson, "location" -> location.asJson, "bytes" -> bytes.asJson ) val digest = StorageDigest("SHA-256", genString()) } "A storage FileAttributes" should { "be converted to file attributes case class correctly" in new Ctx { val expected = StorageFileAttributes("http://example.com", 10L, digest, `application/json`) val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L) FileAttributes(id, json).rightValue shouldEqual expected } "reject when algorithm digest is missing" in new Ctx { val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L).removeKeys("digest") FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'algorithm' field does not have the right format.") } "reject when digest value is empty" in new Ctx { override val digest = StorageDigest("SHA-256", "") val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'value' field does not have the right format.") } "reject when algorithm is empty" in new Ctx { override val digest = StorageDigest("", genString()) val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'algorithm' field does not have the right format.") } "reject when algorithm is invalid" in new Ctx { override val digest = StorageDigest(genString(), genString()) val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'algorithm' field does not have the right format.") } "reject when @type is missing" in new Ctx { val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L).removeKeys("@type") FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'@type' field does not have the right format.") } "reject when @type is invalid" in new Ctx { val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L, genIri.asString) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'@type' field does not have the right format.") } "reject when mediaType is invalid" in new Ctx { val json = jsonFileAttr(digest, "wrong", "http://example.com", 10L) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'mediaType' field does not have the right format.") } } }
Example 35
Source File: MatcherSpec.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.actors import akka.actor.ActorSystem import akka.testkit.TestKitBase import com.typesafe.config.ConfigFactory import com.wavesplatform.dex.domain.utils.ScorexLogging import com.wavesplatform.dex.settings.loadConfig import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Suite} abstract class MatcherSpec(_actorSystemName: String) extends AnyWordSpecLike with MatcherSpecLike { protected def actorSystemName: String = _actorSystemName } trait MatcherSpecLike extends TestKitBase with Matchers with BeforeAndAfterAll with BeforeAndAfterEach with ScorexLogging { this: Suite => protected def actorSystemName: String implicit override lazy val system: ActorSystem = ActorSystem( actorSystemName, loadConfig(ConfigFactory.empty()) ) override protected def afterAll(): Unit = { super.afterAll() shutdown(system) } }
Example 36
Source File: RateDBSpecification.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.db import com.wavesplatform.dex.{MatcherSpecBase, NoShrink} import org.scalacheck.Gen import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} class RateDBSpecification extends AnyWordSpecLike with Matchers with WithDB with MatcherSpecBase with PropertyChecks with NoShrink { private def test(f: RateDB => Unit): Unit = f { RateDB(db) } "RateDB" should { "add, get and delete rates" in test { rdb => val preconditions = Gen .listOfN( 100, for { asset <- arbitraryAssetGen rateValue <- Gen.choose(1, 100).map(_.toDouble / 100) } yield asset -> rateValue ) .map(_.toMap) forAll(preconditions) { map => map.foreach { case (asset, rateValue) => rdb.upsertRate(asset, rateValue) } rdb.getAllRates shouldBe map map.foreach { case (asset, _) => rdb.deleteRate(asset) } rdb.getAllRates.size shouldBe 0 } } "update rate if it already exists" in test { rdb => forAll(arbitraryAssetGen) { asset => rdb.upsertRate(asset, 1) rdb.getAllRates shouldBe Map(asset -> 1) rdb.upsertRate(asset, 2) rdb.getAllRates shouldBe Map(asset -> 2) rdb.deleteRate(asset) } } } }
Example 37
Source File: RateCacheSpecification.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.caches import com.wavesplatform.dex.db.{RateDB, WithDB} import com.wavesplatform.dex.domain.asset.Asset import com.wavesplatform.dex.domain.asset.Asset.Waves import com.wavesplatform.dex.{MatcherSpecBase, NoShrink} import org.scalacheck.Gen import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatestplus.scalacheck.{ScalaCheckPropertyChecks => PropertyChecks} class RateCacheSpecification extends AnyWordSpecLike with Matchers with WithDB with MatcherSpecBase with PropertyChecks with NoShrink { private def test(f: RateCache => Unit): Unit = { withClue("with DB") { f(RateCache(db)) } withClue("in mem") { f(RateCache.inMem) } } private val WavesRate: Map[Asset, Double] = Map(Waves -> 1d) "RateCache" should { "add, get and delete rates" in test { rc => val preconditions = Gen .listOfN( 100, for { asset <- arbitraryAssetGen rateValue <- Gen.choose(1, 100).map(_.toDouble / 100) } yield asset -> rateValue ) .map(_.toMap[Asset, Double]) forAll(preconditions) { map => map.foreach { case (asset, rateValue) => rc.upsertRate(asset, rateValue) shouldBe None } rc.getAllRates should matchTo(map ++ WavesRate) map.foreach { case (asset, rate) => rc.deleteRate(asset) shouldBe Some(rate) } rc.getAllRates should matchTo(WavesRate) } } "update rate if it already exists" in test { rc => forAll(arbitraryAssetGen) { asset: Asset => rc.upsertRate(asset, 1) shouldBe None rc.getAllRates should matchTo(Map(asset -> 1d) ++ WavesRate) rc.upsertRate(asset, 2) shouldBe Some(1d) rc.getAllRates should matchTo(Map(asset -> 2d) ++ WavesRate) rc.deleteRate(asset) shouldBe Some(2d) } } "correctly restore state from db" in { val rateDB = RateDB(db) val asset1 = mkAssetId("First") val asset2 = mkAssetId("Second") rateDB.upsertRate(asset1, 1.5) rateDB.upsertRate(asset2, 5.1) RateCache(db).getAllRates should matchTo(Map(asset1 -> 1.5, asset2 -> 5.1) ++ WavesRate) } } }
Example 38
Source File: OrderFeeSettingsCacheSpecification.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.caches import com.wavesplatform.dex.MatcherSpecBase import com.wavesplatform.dex.settings.OrderFeeSettings.{DynamicSettings, PercentSettings} import com.wavesplatform.dex.settings.{AssetType, OrderFeeSettings} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class OrderFeeSettingsCacheSpecification extends AnyWordSpecLike with Matchers with MatcherSpecBase { "OrderFeeSettingsCache" should { "prevent matcher start if settings wasn't set" in { a[IllegalArgumentException] should be thrownBy new OrderFeeSettingsCache(Map.empty[Long, OrderFeeSettings]) } "throw error if there is no settings for the given offset" in { a[IllegalStateException] should be thrownBy new OrderFeeSettingsCache(Map(100L -> DynamicSettings.symmetric(0.003.waves))) .getSettingsForOffset(1) } "correctly retrieve current fee settings" in { val settingsMap = Map( -1L -> DynamicSettings.symmetric(0.003.waves), 2L -> DynamicSettings(0.001.waves, 0.005.waves), 15L -> DynamicSettings(0.002.waves, 0.004.waves), 1000L -> PercentSettings(AssetType.AMOUNT, 0.005) ) val ofsc = new OrderFeeSettingsCache(settingsMap) def check(offset: Long, current: OrderFeeSettings, actual: OrderFeeSettings): Unit = { ofsc.getSettingsForOffset(offset) should matchTo(current) ofsc.getSettingsForOffset(offset + 1) should matchTo(actual) } check(offset = -1L, current = DynamicSettings(0.003.waves, 0.003.waves), actual = DynamicSettings(0.003.waves, 0.003.waves)) check(offset = 0L, current = DynamicSettings(0.003.waves, 0.003.waves), actual = DynamicSettings(0.003.waves, 0.003.waves)) check(offset = 17L, current = DynamicSettings(0.002.waves, 0.004.waves), actual = DynamicSettings(0.002.waves, 0.004.waves)) check(offset = 100L, current = DynamicSettings(0.002.waves, 0.004.waves), actual = DynamicSettings(0.002.waves, 0.004.waves)) check(offset = 999L, current = DynamicSettings(0.002.waves, 0.004.waves), actual = PercentSettings(AssetType.AMOUNT, 0.005)) } } }
Example 39
Source File: BlockchainCacheSpecification.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.grpc.integration.caches import java.time.Duration import java.util.concurrent.{ConcurrentHashMap, ExecutorService, Executors} import mouse.any.anySyntaxMouse import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.collection.JavaConverters._ import scala.concurrent._ class BlockchainCacheSpecification extends AnyWordSpecLike with Matchers with BeforeAndAfterAll { private val executor: ExecutorService = Executors.newCachedThreadPool implicit private val blockingContext: ExecutionContextExecutor = ExecutionContext.fromExecutor(executor) private class BlockchainCacheTest(loader: String => Future[String], expiration: Option[Duration], invalidationPredicate: String => Boolean) extends BlockchainCache[String, String](loader, expiration, invalidationPredicate) private def createCache(loader: String => Future[String], expiration: Option[Duration] = None, invalidationPredicate: String => Boolean = _ => false): BlockchainCacheTest = { new BlockchainCacheTest(loader, expiration, invalidationPredicate) } override def afterAll(): Unit = { super.afterAll() executor.shutdownNow() } private val andThenAwaitTimeout = 300 "BlockchainCache" should { "not keep failed futures" in { val goodKey = "good key" val badKey = "gRPC Error" val keyAccessMap = new ConcurrentHashMap[String, Int] unsafeTap (m => { m.put(goodKey, 0); m.put(badKey, 0) }) val gRPCError = new RuntimeException("gRPC Error occurred") val cache = createCache( key => { (if (key == badKey) Future.failed(gRPCError) else Future.successful(s"value = $key")) unsafeTap { _ => keyAccessMap.computeIfPresent(key, (_, prev) => prev + 1) } } ) val badKeyAccessCount = 10 Await.result( (1 to badKeyAccessCount).foldLeft { Future.successful("") } { (prev, _) => for { _ <- prev _ <- cache get goodKey r <- cache get badKey recover { case _ => "sad" } } yield { Thread.sleep(andThenAwaitTimeout); r } }, scala.concurrent.duration.Duration.Inf ) keyAccessMap.get(goodKey) shouldBe 1 keyAccessMap.get(badKey) should be > 1 } "not keep values according to the predicate" in { val goodKey = "111" val badKey = "222" val keyAccessMap = new ConcurrentHashMap[String, Int](Map(goodKey -> 0, badKey -> 0).asJava) val cache = createCache( key => { keyAccessMap.computeIfPresent(key, (_, prev) => prev + 1); Future.successful(key) }, invalidationPredicate = _.startsWith("2") ) Await.result( (1 to 10).foldLeft { Future.successful("") } { (prev, _) => for { _ <- prev _ <- cache get goodKey r <- cache get badKey } yield blocking { Thread.sleep(andThenAwaitTimeout); r } }, scala.concurrent.duration.Duration.Inf ) keyAccessMap.get(goodKey) shouldBe 1 keyAccessMap.get(badKey) should be > 1 } } }
Example 40
Source File: OrderJsonSpec.scala From matcher with MIT License | 5 votes |
package com.wavesplatform.dex.domain.order import cats.syntax.option._ import com.wavesplatform.dex.domain.account.KeyPair import com.wavesplatform.dex.domain.asset.Asset.{IssuedAsset, Waves} import com.wavesplatform.dex.domain.asset.AssetPair import com.wavesplatform.dex.domain.order.OrderJson.orderFormat import com.wavesplatform.dex.domain.order.OrderOps._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import play.api.libs.json.Json class OrderJsonSpec extends AnyWordSpecLike with Matchers { private val usd: IssuedAsset = IssuedAsset("USDN".getBytes) private val wavesUsdPair: AssetPair = AssetPair(Waves, usd) private val senderKeyPair = KeyPair("sender".getBytes) private val matcherKeyPair = KeyPair("matcher".getBytes) private val order: Order = OrderV3( senderKeyPair, matcherKeyPair, wavesUsdPair, OrderType.SELL, 123456789L, 119L, 1578074613225L, 1578077613225L, 300000L, Waves ) def getOrderJson(feeAssetRepresentation: Option[String]): String = s"""{ | "version" : 3, | "id" : "HCXVwUaETvHySqRyJSZ2NbnLeqTYP2FBkbwecXmMdbM6", | "sender" : "3N7YhNxYuoa59oCCMPuQRqSx6KM61FkGjYa", | "senderPublicKey" : "226pFho3kqHiCoiQVAUq5MVFkg3KzGLc2zLNsbH8GmE7", | "matcherPublicKey" : "J6ghck2hA2GNJTHGSLSeuCjKuLDGz8i83NfCMFVoWhvf", | "assetPair" : { | "amountAsset" : "WAVES", | "priceAsset" : "3BVv85" | }, | "orderType" : "sell", | "amount" : 123456789, | "price" : 119, | "timestamp" : 1578074613225, | "expiration" : 1578077613225, | "matcherFee" : 300000,${feeAssetRepresentation.fold("")(str => s"""\n"matcherFeeAssetId" : $str,""")} | "signature" : "7dsUd1bQZFWCEKyYqkPxd5AE7x88QUK6xJH86KcqtE2LHURxN92QEJRfojHkgezez6fppDGvTCUcr4ZmrPRKmTZ", | "proofs" : [ "7dsUd1bQZFWCEKyYqkPxd5AE7x88QUK6xJH86KcqtE2LHURxN92QEJRfojHkgezez6fppDGvTCUcr4ZmrPRKmTZ" ] |}""".stripMargin "Domain OrderV3" should { "be deserialized from JSON" when { "matcherFeeAssetId passed as null" in { Json.parse(getOrderJson("null".some)).as[Order] updateProofs order.proofs shouldBe order } "matcherFeeAssetId passed as WAVES" in { Json.parse(getOrderJson("\"WAVES\"".some)).as[Order] updateProofs order.proofs shouldBe order } "matcherFeeAssetId wasn't passed" in { Json.parse(getOrderJson(None)).as[Order] updateProofs order.proofs shouldBe order } "matcherFeeAssetId passed as Issued asset" in { Json .parse(getOrderJson(s""""${usd.id}"""".some)) .as[Order] .updateProofs(order.proofs) .updateFeeAsset(usd) shouldBe order.updateFeeAsset(usd) } } } }
Example 41
Source File: IngestorsEndpointSpec.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.ingest.http import akka.actor.Actor import akka.http.scaladsl.testkit.ScalatestRouteTest import akka.testkit.{TestActorRef, TestKit, TestProbe} import hydra.common.util.ActorUtils import hydra.ingest.IngestorInfo import hydra.ingest.services.IngestorRegistry.{FindAll, FindByName, LookupResult} import org.joda.time.DateTime import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class IngestorsEndpointSpec extends Matchers with AnyWordSpecLike with ScalatestRouteTest with HydraIngestJsonSupport { val ingestorsRoute = new IngestorRegistryEndpoint().route override def afterAll = { super.afterAll() TestKit.shutdownActorSystem( system, verifySystemShutdown = true, duration = 10 seconds ) } val probe = TestProbe() val ingestorInfo = IngestorInfo( ActorUtils.actorName(probe.ref), "test", probe.ref.path, DateTime.now ) val registry = TestActorRef( new Actor { override def receive = { case FindByName("tester") => sender ! LookupResult(Seq(ingestorInfo)) case FindAll => sender ! LookupResult(Seq(ingestorInfo)) } }, "ingestor_registry" ).underlyingActor "The ingestors endpoint" should { "returns all ingestors" in { Get("/ingestors") ~> ingestorsRoute ~> check { val r = responseAs[Seq[IngestorInfo]] r.size shouldBe 1 r(0).path shouldBe ingestorInfo.path r(0).group shouldBe ingestorInfo.group r(0).path shouldBe ingestorInfo.path r(0).registeredAt shouldBe ingestorInfo.registeredAt.withMillisOfSecond( 0 ) } } } }
Example 42
Source File: KafkaAdminAlgebraSpec.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.algebras import akka.actor.ActorSystem import cats.effect.{ContextShift, IO} import cats.implicits._ import hydra.kafka.util.KafkaUtils.TopicDetails import net.manub.embeddedkafka.{EmbeddedKafka, EmbeddedKafkaConfig} import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.ExecutionContext final class KafkaAdminAlgebraSpec extends AnyWordSpecLike with Matchers with BeforeAndAfterAll with EmbeddedKafka { private val port = 8023 implicit private val kafkaConfig: EmbeddedKafkaConfig = EmbeddedKafkaConfig(kafkaPort = port, zooKeeperPort = 3027) implicit private val contextShift: ContextShift[IO] = IO.contextShift(ExecutionContext.global) implicit private val system: ActorSystem = ActorSystem( "kafka-client-spec-system" ) override def beforeAll(): Unit = { super.beforeAll() EmbeddedKafka.start() } override def afterAll(): Unit = { super.afterAll() EmbeddedKafka.stop() } (for { live <- KafkaAdminAlgebra .live[IO](s"localhost:$port") test <- KafkaAdminAlgebra.test[IO] } yield { runTests(live) runTests(test, isTest = true) }).unsafeRunSync() private def runTests(kafkaClient: KafkaAdminAlgebra[IO], isTest: Boolean = false): Unit = { (if (isTest) "KafkaAdmin#test" else "KafkaAdmin#live") must { "create a topic" in { val topicName = "Topic1" val topicDetails = TopicDetails(3, 1.toShort) (kafkaClient.createTopic(topicName, topicDetails) *> kafkaClient .describeTopic(topicName) .map { case Some(topic) => topic.name shouldBe topicName topic.numberPartitions shouldBe topicDetails.numPartitions case None => fail("Found None when a Topic was Expected") }).unsafeRunSync() } "list all topics" in { kafkaClient.getTopicNames.unsafeRunSync() shouldBe List("Topic1") } "delete a topic" in { val topicToDelete = "topic_to_delete" (for { _ <- kafkaClient.createTopic(topicToDelete, TopicDetails(1, 1)) _ <- kafkaClient.deleteTopic(topicToDelete) maybeTopic <- kafkaClient.describeTopic(topicToDelete) } yield maybeTopic should not be defined).unsafeRunSync() } } } }
Example 43
Source File: MetadataAlgebraSpec.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.algebras import java.time.Instant import cats.data.NonEmptyList import cats.effect.{Concurrent, ContextShift, IO, Sync, Timer} import cats.implicits._ import hydra.avro.registry.SchemaRegistry import hydra.core.marshallers.History import hydra.kafka.algebras.MetadataAlgebra.TopicMetadataContainer import hydra.kafka.model.ContactMethod.Slack import hydra.kafka.model.TopicMetadataV2Request.Subject import hydra.kafka.model.{Public, StreamTypeV2, TopicMetadataV2, TopicMetadataV2Key, TopicMetadataV2Request, TopicMetadataV2Value} import io.chrisdavenport.log4cats.SelfAwareStructuredLogger import io.chrisdavenport.log4cats.slf4j.Slf4jLogger import org.apache.avro.generic.GenericRecord import org.scalatest.Assertion import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import retry.RetryPolicies._ import retry.syntax.all._ import retry.{RetryPolicy, _} import scala.concurrent.ExecutionContext import scala.concurrent.duration._ class MetadataAlgebraSpec extends AnyWordSpecLike with Matchers { implicit private val contextShift: ContextShift[IO] = IO.contextShift(ExecutionContext.global) private implicit val concurrentEffect: Concurrent[IO] = IO.ioConcurrentEffect private implicit val policy: RetryPolicy[IO] = limitRetries[IO](5) |+| exponentialBackoff[IO](500.milliseconds) private implicit val timer: Timer[IO] = IO.timer(ExecutionContext.global) private implicit def noop[A]: (A, RetryDetails) => IO[Unit] = retry.noop[IO, A] implicit private def unsafeLogger[F[_]: Sync]: SelfAwareStructuredLogger[F] = Slf4jLogger.getLogger[F] private implicit class RetryAndAssert[A](boolIO: IO[A]) { def retryIfFalse(check: A => Boolean): IO[Assertion] = boolIO.map(check).retryingM(identity, policy, noop).map(assert(_)) } private val metadataTopicName = "_internal.metadataTopic" private val consumerGroup = "Consumer Group" (for { kafkaClient <- KafkaClientAlgebra.test[IO] schemaRegistry <- SchemaRegistry.test[IO] metadata <- MetadataAlgebra.make(metadataTopicName, consumerGroup, kafkaClient, schemaRegistry, consumeMetadataEnabled = true) } yield { runTests(metadata, kafkaClient) }).unsafeRunSync() private def runTests(metadataAlgebra: MetadataAlgebra[IO], kafkaClientAlgebra: KafkaClientAlgebra[IO]): Unit = { "MetadataAlgebraSpec" should { "retrieve none for non-existant topic" in { val subject = Subject.createValidated("Non-existantTopic").get metadataAlgebra.getMetadataFor(subject).unsafeRunSync() shouldBe None } "retrieve metadata" in { val subject = Subject.createValidated("subject1").get val (genericRecordsIO, key, value) = getMetadataGenericRecords(subject) (for { record <- genericRecordsIO _ <- kafkaClientAlgebra.publishMessage(record, metadataTopicName) _ <- metadataAlgebra.getMetadataFor(subject).retryIfFalse(_.isDefined) metadata <- metadataAlgebra.getMetadataFor(subject) } yield metadata shouldBe Some(TopicMetadataContainer(key, value, None, None))).unsafeRunSync() } "retrieve all metadata" in { val subject = Subject.createValidated("subject2").get val (genericRecordsIO, key, value) = getMetadataGenericRecords(subject) (for { record <- genericRecordsIO _ <- kafkaClientAlgebra.publishMessage(record, metadataTopicName) _ <- metadataAlgebra.getMetadataFor(subject).retryIfFalse(_.isDefined) allMetadata <- metadataAlgebra.getAllMetadata } yield allMetadata should have length 2).unsafeRunSync() } } } private def getMetadataGenericRecords(subject: Subject): (IO[(GenericRecord, Option[GenericRecord])], TopicMetadataV2Key, TopicMetadataV2Value) = { val key = TopicMetadataV2Key(subject) val value = TopicMetadataV2Value( StreamTypeV2.Entity, deprecated = false, Public, NonEmptyList.one(Slack.create("#channel").get), Instant.now, List(), None) (TopicMetadataV2.encode[IO](key, Some(value)), key, value) } }
Example 44
Source File: TopicMetadataV2TransportSpec.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.kafka.model import hydra.kafka.model.TopicMetadataV2Request.Subject import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TopicMetadataV2TransportSpec extends AnyWordSpecLike with Matchers { "TopicMetadataV2Request" must { "Parse correct emails" in { val correctEmailAddress = "[email protected]" val correctEmail = ContactMethod.Email.create(correctEmailAddress) correctEmail.get shouldBe a[ContactMethod.Email] correctEmail.get.address.value shouldEqual (correctEmailAddress) } "Return None for incorrect emails" in { ContactMethod.Email.create("data-platform-team@pluralsight") shouldBe None } "Parse correct slacks" in { val correctSlackChannel = "#dev-data-platform" val correctSlack = ContactMethod.Slack.create(correctSlackChannel) correctSlack.get shouldBe a[ContactMethod.Slack] correctSlack.get.channel.value shouldEqual (correctSlackChannel) } "Return None for incorrect slacks" in { ContactMethod.Slack.create("Not a slack address") shouldBe None } "Parse correct Subjects" in { val correctSubjectString = "Foo" val correctSubject = Subject.createValidated(correctSubjectString) correctSubject.get shouldBe a[Subject] correctSubject.get.value shouldEqual (correctSubjectString) } "Return None for incorrect Subjects" in { Subject.createValidated("*NoT A \\ good subject") shouldBe None } } }
Example 45
Source File: TryWithSpec.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.common.util import java.io.Closeable import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.util.{Failure, Success} class TryWithSpec extends AnyWordSpecLike with Matchers { // Exceptions and errors here so we don't pay the stack trace creation cost multiple times val getResourceException = new RuntimeException val inFunctionException = new RuntimeException val inCloseException = new RuntimeException val getResourceError = new OutOfMemoryError val inFunctionError = new OutOfMemoryError val inCloseError = new OutOfMemoryError val goodResource = new Closeable { override def toString: String = "good resource" def close(): Unit = {} } "TryWith" should { "catch exceptions getting the resource" in { TryWith(throw getResourceException)(println) shouldBe Failure( getResourceException ) } "catch exceptions in the function" in { TryWith(goodResource) { _ => throw inFunctionException } shouldBe Failure(inFunctionException) } "catch exceptions while closing" in { TryWith(new Closeable { def close(): Unit = throw inCloseException })(_.toString) shouldBe Failure(inCloseException) } "note suppressed exceptions" in { val ex = new RuntimeException val result = TryWith(new Closeable { def close(): Unit = throw inCloseException })(_ => throw ex) result shouldBe Failure(ex) val Failure(returnedException) = result returnedException.getSuppressed shouldBe Array(inCloseException) } "propagate errors getting the resource" in { intercept[OutOfMemoryError] { TryWith(throw getResourceError)(println) } shouldBe getResourceError } "propagate errors in the function" in { intercept[OutOfMemoryError] { TryWith(goodResource) { _ => throw inFunctionError } } shouldBe inFunctionError } "propagate errors while closing" in { intercept[OutOfMemoryError] { TryWith(new Closeable { def close(): Unit = throw inCloseError })(_.toString) } shouldBe inCloseError } "return the value from a successful run" in { TryWith(goodResource)(_.toString) shouldBe Success("good resource") } } }
Example 46
Source File: AuditLogProviderItTest.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy.provider import java.net.InetAddress import akka.actor.ActorSystem import akka.http.scaladsl.model.{HttpMethods, HttpRequest, RemoteAddress, StatusCodes} import com.ing.wbaa.rokku.proxy.config.KafkaSettings import com.ing.wbaa.rokku.proxy.data._ import com.ing.wbaa.rokku.proxy.handler.parsers.RequestParser.RequestTypeUnknown import net.manub.embeddedkafka.{EmbeddedKafka, EmbeddedKafkaConfig} import org.scalatest.diagrams.Diagrams import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.ExecutionContext class AuditLogProviderItTest extends AnyWordSpecLike with Diagrams with EmbeddedKafka with AuditLogProvider { implicit val testSystem: ActorSystem = ActorSystem("kafkaTest") private val testKafkaPort = 9093 override def auditEnabled = true override implicit val kafkaSettings: KafkaSettings = new KafkaSettings(testSystem.settings.config) { override val bootstrapServers: String = s"localhost:$testKafkaPort" } override implicit val executionContext: ExecutionContext = testSystem.dispatcher implicit val requestId: RequestId = RequestId("test") val s3Request = S3Request(AwsRequestCredential(AwsAccessKey("a"), None), Some("demobucket"), Some("s3object"), Read()) .copy(headerIPs = HeaderIPs(Some(RemoteAddress(InetAddress.getByName("127.0.0.1"))), Some(Seq(RemoteAddress(InetAddress.getByName("1.1.1.1")))), Some(RemoteAddress(InetAddress.getByName("2.2.2.2"))))) "AuditLogProvider" should { "send audit" in { implicit val config = EmbeddedKafkaConfig(kafkaPort = testKafkaPort) withRunningKafka { Thread.sleep(3000) val createEventsTopic = "audit_events" createCustomTopic(createEventsTopic) auditLog(s3Request, HttpRequest(HttpMethods.PUT, "http://localhost", Nil), "testUser", RequestTypeUnknown(), StatusCodes.Processing) val result = consumeFirstStringMessageFrom(createEventsTopic) assert(result.contains("\"eventName\":\"PUT\"")) assert(result.contains("\"sourceIPAddress\":\"ClientIp=unknown|X-Real-IP=127.0.0.1|X-Forwarded-For=1.1.1.1|Remote-Address=2.2.2.2\"")) assert(result.contains("\"x-amz-request-id\":\"test\"")) assert(result.contains("\"principalId\":\"testUser\"")) } } } }
Example 47
Source File: MessageProviderKafkaItTest.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy.provider import java.net.InetAddress import akka.actor.ActorSystem import akka.http.scaladsl.model.{HttpMethods, RemoteAddress} import com.ing.wbaa.rokku.proxy.config.KafkaSettings import com.ing.wbaa.rokku.proxy.data._ import com.ing.wbaa.rokku.proxy.handler.parsers.RequestParser.RequestTypeUnknown import net.manub.embeddedkafka.{EmbeddedKafka, EmbeddedKafkaConfig} import org.scalatest.RecoverMethods._ import org.scalatest.diagrams.Diagrams import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.ExecutionContext class MessageProviderKafkaItTest extends AnyWordSpecLike with Diagrams with EmbeddedKafka with MessageProviderKafka { implicit val testSystem: ActorSystem = ActorSystem("kafkaTest") private val testKafkaPort = 9093 override implicit val kafkaSettings: KafkaSettings = new KafkaSettings(testSystem.settings.config) { override val bootstrapServers: String = s"localhost:$testKafkaPort" } override implicit val executionContext: ExecutionContext = testSystem.dispatcher implicit val requestId: RequestId = RequestId("test") val s3Request = S3Request(AwsRequestCredential(AwsAccessKey("a"), None), Some("demobucket"), Some("s3object"), Read()) .copy(clientIPAddress = RemoteAddress(InetAddress.getByName("127.0.0.1"))) "KafkaMessageProvider" should { "Send message to correct topic with Put or Post" in { implicit val config = EmbeddedKafkaConfig(kafkaPort = testKafkaPort) withRunningKafka { Thread.sleep(3000) val createEventsTopic = "create_events" createCustomTopic(createEventsTopic) emitEvent(s3Request, HttpMethods.PUT, "testUser", RequestTypeUnknown()) val result = consumeFirstStringMessageFrom(createEventsTopic) assert(result.contains("s3:ObjectCreated:PUT")) } } "Send message to correct topic with Delete" in { implicit val config = EmbeddedKafkaConfig(kafkaPort = testKafkaPort) withRunningKafka { Thread.sleep(3000) val deleteEventsTopic = "delete_events" createCustomTopic(deleteEventsTopic) emitEvent(s3Request, HttpMethods.DELETE, "testUser", RequestTypeUnknown()) assert(consumeFirstStringMessageFrom(deleteEventsTopic).contains("s3:ObjectRemoved:DELETE")) } } "fail on incomplete data" in { recoverToSucceededIf[Exception](emitEvent(s3Request.copy(s3Object = None), HttpMethods.PUT, "testUser", RequestTypeUnknown())) } } }
Example 48
Source File: HttpRequestRecorderSpec.scala From rokku with Apache License 2.0 | 5 votes |
package com.ing.wbaa.rokku.proxy.persistence import java.net.InetAddress import akka.actor.{ ActorSystem, PoisonPill, Props } import akka.http.scaladsl.model.HttpHeader.ParsingResult import akka.http.scaladsl.model._ import akka.testkit.{ ImplicitSender, TestKit } import com.ing.wbaa.rokku.proxy.data._ import com.ing.wbaa.rokku.proxy.persistence.HttpRequestRecorder.{ ExecutedRequestCmd, LatestRequests, LatestRequestsResult } import org.scalatest.BeforeAndAfterAll import org.scalatest.diagrams.Diagrams import org.scalatest.wordspec.AnyWordSpecLike import scala.collection.immutable class HttpRequestRecorderSpec extends TestKit(ActorSystem("RequestRecorderTest")) with ImplicitSender with AnyWordSpecLike with Diagrams with BeforeAndAfterAll { override def afterAll: Unit = { TestKit.shutdownActorSystem(system) } private def convertStringsToAkkaHeaders(headers: List[String]): immutable.Seq[HttpHeader] = headers.map { p => val kv = p.split("=") HttpHeader.parse(kv(0), kv(1)) match { case ParsingResult.Ok(header, _) => header case ParsingResult.Error(error) => throw new Exception(s"Unable to convert to HttpHeader: ${error.summary}") } } val requestRecorder = system.actorOf(Props(classOf[HttpRequestRecorder]), "localhost-1") val headers = List("Remote-Address=0:0:0:0:0:0:0:1:58170", "Host=localhost:8987", "X-Amz-Content-SHA256=02502914aca52472205417e4c418ee499ba39ca1b283d99da26e295df2eccf32", "User-Agent=aws-cli/1.16.30 Python/2.7.5 Linux/3.10.0-862.14.4.el7.x86_64 botocore/1.12.20", "Content-MD5=Wf7l+rCPsVw8eqc34kVJ1g==", "Authorization=AWS4-HMAC-SHA256 Credential=6r24619bHVWvrxR5AMHNkGZ6vNRXoGCP/20190704/us-east-1/s3/aws4_request", "SignedHeaders=content-md5;host;x-amz-content-sha256;x-amz-date;x-amz-security-token", "Signature=271dda503da6fcf04cc058cb514b28a6d522a9b712ab553bfb88fb7814ab082f") val httpRequest = HttpRequest( HttpMethods.PUT, Uri("http://127.0.0.1:8010/home/testuser/file34"), convertStringsToAkkaHeaders(headers), HttpEntity.Empty.withContentType(ContentTypes.`application/octet-stream`).toString(), HttpProtocols.`HTTP/1.1` ) val userSTS = User(UserName("okUser"), Set(UserGroup("okGroup")), AwsAccessKey("accesskey"), AwsSecretKey("secretkey"), UserAssumeRole("")) val clientIPAddress = RemoteAddress(InetAddress.getByName("localhost"), Some(1234)) "RequestRecorder" should { "persist Http request event" in { requestRecorder ! ExecutedRequestCmd(httpRequest, userSTS, clientIPAddress) requestRecorder ! LatestRequests(1) expectMsg(LatestRequestsResult(List(ExecutedRequestEvt(httpRequest, userSTS, clientIPAddress)))) requestRecorder ! PoisonPill val requestRecorder1 = system.actorOf(Props(classOf[HttpRequestRecorder]), "localhost-2") requestRecorder1 ! LatestRequests(1) expectMsg(LatestRequestsResult(List(ExecutedRequestEvt(httpRequest, userSTS, clientIPAddress)))) } } }
Example 49
Source File: EmbeddedKafkaSpecSupport.scala From embedded-kafka with MIT License | 5 votes |
package net.manub.embeddedkafka import java.net.{InetAddress, Socket} import net.manub.embeddedkafka.EmbeddedKafkaSpecSupport.{ Available, NotAvailable, ServerStatus } import org.scalatest.Assertion import org.scalatest.concurrent.{Eventually, IntegrationPatience} import org.scalatest.matchers.should.Matchers import org.scalatest.time.{Milliseconds, Seconds, Span} import org.scalatest.wordspec.AnyWordSpecLike import scala.util.{Failure, Success, Try} trait EmbeddedKafkaSpecSupport extends AnyWordSpecLike with Matchers with Eventually with IntegrationPatience { implicit val config: PatienceConfig = PatienceConfig(Span(1, Seconds), Span(100, Milliseconds)) def expectedServerStatus(port: Int, expectedStatus: ServerStatus): Assertion = eventually { status(port) shouldBe expectedStatus } private def status(port: Int): ServerStatus = { Try(new Socket(InetAddress.getByName("localhost"), port)) match { case Failure(_) => NotAvailable case Success(_) => Available } } } object EmbeddedKafkaSpecSupport { sealed trait ServerStatus case object Available extends ServerStatus case object NotAvailable extends ServerStatus }
Example 50
Source File: LagomDevModeServiceDiscoverySpec.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.devmode.internal.registry import java.net.InetAddress import java.net.URI import akka.actor.ActorSystem import akka.discovery.ServiceDiscovery.Resolved import akka.discovery.ServiceDiscovery.ResolvedTarget import akka.testkit.TestKit import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.ScalaFutures._ import scala.collection.immutable import scala.concurrent.Future import scala.concurrent.duration._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class LagomDevModeServiceDiscoverySpec extends TestKit(ActorSystem("LagomDevModeSimpleServiceDiscoverySpec")) with AnyWordSpecLike with Matchers with BeforeAndAfterAll { private val client = new StaticServiceRegistryClient( Map( "test-service" -> List(URI.create("http://localhost:8080")), "test-service-without-port" -> List(URI.create("http://localhost")) ) ) protected override def afterAll(): Unit = { shutdown(verifySystemShutdown = true) } private val discovery = LagomDevModeServiceDiscovery(system) discovery.setServiceRegistryClient(client) "DevModeSimpleServiceDiscoverySpec" should { "resolve services in the registry" in { val expected = Resolved("test-service", List(ResolvedTarget("localhost", Some(8080), Some(InetAddress.getLocalHost)))) discovery.lookup("test-service", 100.milliseconds).futureValue shouldBe expected } "allow missing ports" in { val expected = Resolved("test-service-without-port", List(ResolvedTarget("localhost", None, Some(InetAddress.getLocalHost)))) discovery.lookup("test-service-without-port", 100.milliseconds).futureValue shouldBe expected } } } private class StaticServiceRegistryClient(registrations: Map[String, List[URI]]) extends ServiceRegistryClient { override def locateAll(serviceName: String, portName: Option[String]): Future[immutable.Seq[URI]] = Future.successful(registrations.getOrElse(serviceName, Nil)) }
Example 51
Source File: PostSpec.scala From lagom with Apache License 2.0 | 5 votes |
package docs.home.scaladsl.persistence //#unit-test import scala.concurrent.Await import scala.concurrent.duration._ import akka.Done import akka.actor.ActorSystem import com.lightbend.lagom.scaladsl.persistence.PersistentEntity.InvalidCommandException import com.lightbend.lagom.scaladsl.playjson.JsonSerializerRegistry import com.lightbend.lagom.scaladsl.testkit.PersistentEntityTestDriver import com.typesafe.config.ConfigFactory import org.scalactic.TypeCheckedTripleEquals import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class PostSpec extends AnyWordSpecLike with Matchers with BeforeAndAfterAll with TypeCheckedTripleEquals { val system = ActorSystem("PostSpec", JsonSerializerRegistry.actorSystemSetupFor(BlogPostSerializerRegistry)) override def afterAll(): Unit = { Await.ready(system.terminate, 10.seconds) } "Blog Post entity" must { "handle AddPost" in { val driver = new PersistentEntityTestDriver(system, new Post, "post-1") val content = PostContent("Title", "Body") val outcome = driver.run(AddPost(content)) outcome.events should ===(List(PostAdded("post-1", content))) outcome.state.published should ===(false) outcome.state.content should ===(Some(content)) outcome.replies should ===(List(AddPostDone("post-1"))) outcome.issues should be(Nil) } "validate title" in { val driver = new PersistentEntityTestDriver(system, new Post, "post-1") val outcome = driver.run(AddPost(PostContent("", "Body"))) outcome.replies.head.getClass should be(classOf[InvalidCommandException]) outcome.events.size should ===(0) outcome.issues should be(Nil) } "handle ChangeBody" in { val driver = new PersistentEntityTestDriver(system, new Post, "post-1") driver.run(AddPost(PostContent("Title", "Body"))) val outcome = driver.run(ChangeBody("New body 1"), ChangeBody("New body 2")) outcome.events should ===(List(BodyChanged("post-1", "New body 1"), BodyChanged("post-1", "New body 2"))) outcome.state.published should ===(false) outcome.state.content.get.body should ===("New body 2") outcome.replies should ===(List(Done, Done)) outcome.issues should be(Nil) } } } //#unit-test
Example 52
Source File: ActorSystemSpec.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.persistence import java.lang.reflect.Modifier import akka.actor.ActorSystem import akka.actor.CoordinatedShutdown import akka.actor.setup.ActorSystemSetup import akka.event.Logging import akka.event.LoggingAdapter import akka.testkit.ImplicitSender import akka.testkit.TestKit import com.typesafe.config.Config import com.typesafe.config.ConfigFactory import org.scalactic.CanEqual import org.scalactic.TypeCheckedTripleEquals import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike object ActorSystemSpec { // taken from akka-testkit's AkkaSpec private def testNameFromCallStack(classToStartFrom: Class[_]): String = { def isAbstractClass(className: String): Boolean = { try { Modifier.isAbstract(Class.forName(className).getModifiers) } catch { case _: Throwable => false // yes catch everything, best effort check } } val startFrom = classToStartFrom.getName val filteredStack = Thread.currentThread.getStackTrace.iterator .map(_.getClassName) // drop until we find the first occurrence of classToStartFrom .dropWhile(!_.startsWith(startFrom)) // then continue to the next entry after classToStartFrom that makes sense .dropWhile { case `startFrom` => true case str if str.startsWith(startFrom + "$") => true // lambdas inside startFrom etc case str if isAbstractClass(str) => true case _ => false } if (filteredStack.isEmpty) throw new IllegalArgumentException(s"Couldn't find [${classToStartFrom.getName}] in call stack") // sanitize for actor system name scrubActorSystemName(filteredStack.next()) } // taken from akka-testkit's AkkaSpec private def scrubActorSystemName(name: String): String = { name .replaceFirst("""^.*\.""", "") // drop package name .replaceAll("""\$\$?\w+""", "") // drop scala anonymous functions/classes .replaceAll("[^a-zA-Z_0-9]", "_") } } abstract class ActorSystemSpec(actorSystemFactory: () => ActorSystem) extends TestKit(actorSystemFactory()) with AnyWordSpecLike with Matchers with BeforeAndAfterAll with TypeCheckedTripleEquals with ImplicitSender { def this(testName: String, config: Config) = this(() => ActorSystem(testName, config)) def this(config: Config) = this(ActorSystemSpec.testNameFromCallStack(classOf[ActorSystemSpec]), config) def this(setup: ActorSystemSetup) = this(() => ActorSystem(ActorSystemSpec.testNameFromCallStack(classOf[ActorSystemSpec]), setup)) def this() = this(ConfigFactory.empty()) override def afterAll(): Unit = { shutdown() super.afterAll() } val log: LoggingAdapter = Logging(system, this.getClass) val coordinatedShutdown: CoordinatedShutdown = CoordinatedShutdown(system) // for ScalaTest === compare of Class objects implicit def classEqualityConstraint[A, B]: CanEqual[Class[A], Class[B]] = new CanEqual[Class[A], Class[B]] { def areEqual(a: Class[A], b: Class[B]) = a == b } }
Example 53
Source File: ServiceSupport.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.it import java.util.Collections import java.util.function.{ Function => JFunction } import akka.stream.Materializer import akka.stream.scaladsl.Source import org.scalatest.Inside import play.api.Application import play.api.Configuration import play.api.Environment import play.inject.guice.GuiceApplicationBuilder import scala.concurrent.Await import scala.concurrent.duration._ import scala.reflect.ClassTag import akka.japi.function.Procedure import com.google.inject.Binder import com.google.inject.Module import com.google.inject.TypeLiteral import com.lightbend.lagom.javadsl.testkit.ServiceTest import com.lightbend.lagom.javadsl.testkit.ServiceTest.TestServer import play.api.routing.Router import java.util import com.lightbend.lagom.internal.testkit.EmptyAdditionalRoutersModule import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike sealed trait HttpBackend { final val provider: String = s"play.core.server.${codeName}ServerProvider" val codeName: String } case object AkkaHttp extends HttpBackend { val codeName = "AkkaHttp" } case object Netty extends HttpBackend { val codeName = "Netty" } trait ServiceSupport extends AnyWordSpecLike with Matchers with Inside { def withServer( configureBuilder: GuiceApplicationBuilder => GuiceApplicationBuilder )(block: Application => Unit)(implicit httpBackend: HttpBackend): Unit = { val jConfigureBuilder = new JFunction[GuiceApplicationBuilder, GuiceApplicationBuilder] { override def apply(b: GuiceApplicationBuilder): GuiceApplicationBuilder = { configureBuilder(b) .overrides(EmptyAdditionalRoutersModule) .configure("play.server.provider", httpBackend.provider) } } val jBlock = new Procedure[TestServer] { override def apply(server: TestServer): Unit = { block(server.app.asScala()) } } val setup = ServiceTest.defaultSetup.configureBuilder(jConfigureBuilder).withCluster(false) ServiceTest.withServer(setup, jBlock) } def withClient[T: ClassTag]( configureBuilder: GuiceApplicationBuilder => GuiceApplicationBuilder )(block: Application => T => Unit)(implicit httpBackend: HttpBackend): Unit = { withServer(configureBuilder) { application => val client = application.injector.instanceOf[T] block(application)(client) } } implicit def materializer(implicit app: Application): Materializer = app.materializer def consume[T](source: Source[T, _])(implicit mat: Materializer): List[T] = { Await.result(source.runFold(List.empty[T])((list, t) => t :: list), 10.seconds).reverse } }
Example 54
Source File: STMultiNodeSpec.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.internal.cluster import akka.remote.testkit.MultiNodeSpecCallbacks import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike trait STMultiNodeSpec extends MultiNodeSpecCallbacks with AnyWordSpecLike with Matchers with BeforeAndAfterAll { override def beforeAll(): Unit = { super.beforeAll() multiNodeSpecBeforeAll() } override def afterAll(): Unit = { multiNodeSpecAfterAll() super.afterAll() } }
Example 55
Source File: ClusterMessageSerializerSpec.scala From lagom with Apache License 2.0 | 5 votes |
package com.lightbend.lagom.internal.cluster import akka.actor.ActorSystem import akka.actor.ExtendedActorSystem import akka.testkit.ImplicitSender import akka.testkit.TestKit import com.lightbend.lagom.internal.cluster.ClusterDistribution.EnsureActive import com.lightbend.lagom.internal.cluster.protobuf.msg.ClusterMessages.{ EnsureActive => ProtobufEnsureActive } import com.typesafe.config.ConfigFactory import org.scalactic.TypeCheckedTripleEquals import org.scalatest.BeforeAndAfterAll import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike object ClusterMessageSerializerSpec { def actorSystem(): ActorSystem = { val config = ConfigFactory.defaultReference() ActorSystem(classOf[ClusterMessageSerializerSpec].getSimpleName, config) } } class ClusterMessageSerializerSpec extends TestKit(ClusterMessageSerializerSpec.actorSystem()) with AnyWordSpecLike with Matchers with BeforeAndAfterAll with TypeCheckedTripleEquals with ImplicitSender { val clusterMessageSerializer = new ClusterMessageSerializer(system.asInstanceOf[ExtendedActorSystem]) "ClusterMessageSerializer" must { "serialize EnsureActive" in { val ensureActive = EnsureActive("entity-1") val bytes = clusterMessageSerializer.toBinary(ensureActive) ProtobufEnsureActive.parseFrom(bytes).getEntityId should be("entity-1") } "deserialize EnsureActive" in { val bytes = ProtobufEnsureActive.newBuilder().setEntityId("entity-2").build().toByteArray val ensureActive = clusterMessageSerializer.fromBinary(bytes, "E").asInstanceOf[EnsureActive] ensureActive.entityId should be("entity-2") } "fail to serialize other types" in { assertThrows[IllegalArgumentException] { clusterMessageSerializer.toBinary("Strings are not supported") } } "fail to deserialize with the wrong manifest" in { assertThrows[IllegalArgumentException] { val bytes = ProtobufEnsureActive.newBuilder().setEntityId("entity-2").build().toByteArray clusterMessageSerializer.fromBinary(bytes, "WRONG-MANIFEST") } } } protected override def afterAll(): Unit = { shutdown() super.afterAll() } }
Example 56
Source File: UnitSpec.scala From self-assessment-api with Apache License 2.0 | 5 votes |
package support import org.scalatest.EitherValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import play.api.http.{HeaderNames, MimeTypes, Status} import play.api.mvc.ControllerComponents import play.api.test.Helpers.stubControllerComponents import play.api.test.{DefaultAwaitTimeout, FutureAwaits, ResultExtractors} import uk.gov.hmrc.http.HeaderCarrier import scala.util.control.NoStackTrace trait UnitSpec extends AnyWordSpecLike with EitherValues with Matchers with FutureAwaits with DefaultAwaitTimeout with ResultExtractors with HeaderNames with Status with MimeTypes { lazy val controllerComponents: ControllerComponents = stubControllerComponents() implicit val hc: HeaderCarrier = HeaderCarrier() val testException: Throwable = new NoStackTrace { override def getMessage: String = "A test exception was thrown" } }
Example 57
Source File: TestSupport.scala From cedi-dtrace with Apache License 2.0 | 5 votes |
package com.ccadllc.cedi.dtrace package logging import cats.effect.{ IO, Sync } import io.circe._ import io.circe.syntax._ import org.scalacheck.Arbitrary import org.scalatest.Suite import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import shapeless.Lazy trait TestSupport extends AnyWordSpecLike with Matchers with ScalaCheckPropertyChecks with TraceGenerators with TestData { self: Suite => override def testEmitter[F[_]: Sync]: F[TraceSystem.Emitter[F]] = Sync[F].pure(LogEmitter.apply) val salesManagementSystem = TraceSystem(testSystemData, testEmitter[IO].unsafeRunSync, TraceSystem.realTimeTimer[IO]) val calculateQuarterlySalesTraceContext = TraceContext(quarterlySalesCalculationSpan, true, salesManagementSystem) def encodeGeneratedJson[A: Arbitrary](implicit encoder: Lazy[Encoder[A]]): Unit = { implicit val e = encoder.value "encode arbitrary instances to JSON" in { forAll { (msg: A) => msg.asJson.noSpaces should not be (None) } } } def encodeSpecificJson[A](a: A, json: Json)(implicit encoder: Lazy[Encoder[A]]): Unit = { implicit val e = encoder.value "encode specific instance to JSON and ensure it matches expected" in { a.asJson shouldBe json } } }
Example 58
Source File: WiremockTestServer.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.examples.utils import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import org.scalatest.BeforeAndAfterEach import org.scalatest.wordspec.AnyWordSpecLike trait WiremockTestServer extends AnyWordSpecLike with BeforeAndAfterEach { val wireMockServer = new WireMockServer(20001) override protected def beforeEach(): Unit = { wireMockServer.start() WireMock.configureFor("localhost", 20001) } override protected def afterEach(): Unit = { wireMockServer.stop() } }
Example 59
Source File: HttpVerbSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.http import java.util import com.typesafe.config.Config import org.mockito.Matchers.any import org.mockito.Mockito.when import org.scalatest.LoneElement import org.scalatestplus.mockito.MockitoSugar import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import uk.gov.hmrc.http.logging.{Authorization, ForwardedFor, RequestId, SessionId} class HttpVerbSpec extends AnyWordSpecLike with Matchers with MockitoSugar with LoneElement { "applicableHeaders" should { "should contain the values passed in by header-carrier" in { val url = "http://test.me" implicit val hc = HeaderCarrier( authorization = Some(Authorization("auth")), sessionId = Some(SessionId("session")), requestId = Some(RequestId("request")), token = Some(Token("token")), forwarded = Some(ForwardedFor("forwarded")) ) val httpRequest = new HttpVerb { override def configuration: Option[Config] = None } val result = httpRequest.applicableHeaders(url) result shouldBe hc.headers } "should include the User-Agent header when the 'appName' config value is present" in { val mockedConfig = mock[Config] when(mockedConfig.getStringList(any())).thenReturn(new util.ArrayList[String]()) when(mockedConfig.getString("appName")).thenReturn("myApp") when(mockedConfig.hasPathOrNull("appName")).thenReturn(true) val httpRequest = new HttpVerb { override def configuration: Option[Config] = Some(mockedConfig) } val result = httpRequest.applicableHeaders("http://test.me")(HeaderCarrier()) result.contains("User-Agent" -> "myApp") shouldBe true } "filter 'remaining headers' from request for external service calls" in { implicit val hc = HeaderCarrier( otherHeaders = Seq("foo" -> "secret!") ) val httpRequest = new HttpVerb { override def configuration: Option[Config] = None } val result = httpRequest.applicableHeaders("http://test.me") result.map(_._1) should not contain "foo" } "include 'remaining headers' in request for internal service call to .service URL" in { implicit val hc = HeaderCarrier( otherHeaders = Seq("foo" -> "secret!") ) val httpRequest = new HttpVerb { override def configuration: Option[Config] = None } for { url <- List("http://test.public.service/bar", "http://test.public.mdtp/bar") } { val result = httpRequest.applicableHeaders(url) assert(result.contains("foo" -> "secret!"), s"'other/remaining headers' for $url were not present") } } "include 'remaining headers' in request for internal service call to other configured internal URL pattern" in { val url = "http://localhost/foo" // an internal service call, according to config implicit val hc = HeaderCarrier( otherHeaders = Seq("foo" -> "secret!") ) import scala.collection.JavaConversions._ val mockedConfig = mock[Config] when(mockedConfig.getStringList("internalServiceHostPatterns")).thenReturn(List("localhost")) when(mockedConfig.hasPathOrNull("internalServiceHostPatterns")).thenReturn(true) val httpRequest = new HttpVerb { override def configuration: Option[Config] = Some(mockedConfig) } val result = httpRequest.applicableHeaders(url) result.contains("foo" -> "secret!") shouldBe true } } }
Example 60
Source File: HttpDeleteSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.http import akka.actor.ActorSystem import com.typesafe.config.Config import org.mockito.ArgumentCaptor import org.mockito.Matchers.{any, eq => is} import org.mockito.Mockito._ import org.scalatest.concurrent.PatienceConfiguration.{Interval, Timeout} import org.scalatest.time.{Millis, Seconds, Span} import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import org.scalatestplus.mockito.MockitoSugar import uk.gov.hmrc.http.hooks.HttpHook import scala.concurrent.{ExecutionContext, Future} import uk.gov.hmrc.http.HttpReads.Implicits._ class HttpDeleteSpec extends AnyWordSpecLike with Matchers with MockitoSugar with CommonHttpBehaviour { import ExecutionContext.Implicits.global class StubbedHttpDelete(doDeleteResult: Future[HttpResponse], doDeleteWithHeaderResult: Future[HttpResponse]) extends HttpDelete with ConnectionTracingCapturing { val testHook1 = mock[HttpHook] val testHook2 = mock[HttpHook] val hooks = Seq(testHook1, testHook2) override def configuration: Option[Config] = None override protected def actorSystem: ActorSystem = ActorSystem("test-actor-system") def appName: String = ??? def doDelete(url: String, headers: Seq[(String, String)])(implicit hc: HeaderCarrier, ec: ExecutionContext) = doDeleteResult } "HttpDelete" should { "be able to return plain responses" in { val response = HttpResponse(200, testBody) val testDelete = new StubbedHttpDelete(Future.successful(response), Future.successful(response)) testDelete.DELETE[HttpResponse](url, Seq("foo" -> "bar")).futureValue shouldBe response } "be able to return objects deserialised from JSON" in { val testDelete = new StubbedHttpDelete(Future.successful(HttpResponse(200, """{"foo":"t","bar":10}""")), Future.successful(HttpResponse(200, """{"foo":"t","bar":10}"""))) testDelete .DELETE[TestClass](url, Seq("foo" -> "bar")) .futureValue(Timeout(Span(2, Seconds)), Interval(Span(15, Millis))) shouldBe TestClass("t", 10) } behave like anErrorMappingHttpCall("DELETE", (url, responseF) => new StubbedHttpDelete(responseF, responseF).DELETE[HttpResponse](url, Seq("foo" -> "bar"))) behave like aTracingHttpCall("DELETE", "DELETE", new StubbedHttpDelete(defaultHttpResponse, defaultHttpResponse)) { _.DELETE[HttpResponse](url, Seq("foo" -> "bar")) } "Invoke any hooks provided" in { val dummyResponse = HttpResponse(200, testBody) val dummyResponseFuture = Future.successful(dummyResponse) val dummyHeader = Future.successful(dummyResponse) val testDelete = new StubbedHttpDelete(dummyResponseFuture, dummyHeader) testDelete.DELETE[HttpResponse](url, Seq("header" -> "foo")).futureValue val respArgCaptor1 = ArgumentCaptor.forClass(classOf[Future[HttpResponse]]) val respArgCaptor2 = ArgumentCaptor.forClass(classOf[Future[HttpResponse]]) verify(testDelete.testHook1).apply(is(url), is("DELETE"), is(None), respArgCaptor1.capture())(any(), any()) verify(testDelete.testHook2).apply(is(url), is("DELETE"), is(None), respArgCaptor2.capture())(any(), any()) // verifying directly without ArgumentCaptor didn't work as Futures were different instances // e.g. Future.successful(5) != Future.successful(5) respArgCaptor1.getValue.futureValue shouldBe dummyResponse respArgCaptor2.getValue.futureValue shouldBe dummyResponse } } }
Example 61
Source File: RestFormatsSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.http.controllers import org.joda.time.{DateTime, DateTimeZone, LocalDate, LocalDateTime} import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import play.api.libs.json.{JsSuccess, _} class RestFormatsSpec extends AnyWordSpecLike with Matchers { "localDateTimeRead" should { "return a LocalDateTime for correctly formatted JsString" in { val testDate = new LocalDateTime(0) val jsValue = RestFormats.localDateTimeWrite.writes(testDate) val JsSuccess(result, _) = RestFormats.localDateTimeRead.reads(jsValue) result shouldBe testDate } "return a JsError for a json value that is not a JsString" in { RestFormats.localDateTimeRead.reads(Json.obj()) shouldBe a[JsError] } "return a JsError for a JsString that is not a well-formatted date" in { RestFormats.localDateTimeRead.reads(JsString("not a valid date")) shouldBe a[JsError] } } "dateTimeRead" should { "return a DateTime in zone UTC for correctly formatted JsString" in { val testDate = new DateTime(0) val jsValue = RestFormats.dateTimeWrite.writes(testDate) val JsSuccess(result, _) = RestFormats.dateTimeRead.reads(jsValue) result shouldBe testDate.withZone(DateTimeZone.UTC) } "return a JsError for a json value that is not a JsString" in { RestFormats.dateTimeRead.reads(Json.obj()) shouldBe a[JsError] } "return a JsError for a JsString that is not a well-formatted date" in { RestFormats.dateTimeRead.reads(JsString("not a valid date")) shouldBe a[JsError] } } "localDateRead" should { "return a LocalDate in zone UTC for correctly formatted JsString" in { val json = JsString("1994-05-01") val expectedDate = new LocalDate(1994, 5, 1) val JsSuccess(result, _) = RestFormats.localDateRead.reads(json) result shouldBe expectedDate } "return a JsError for a json value that is not a JsString" in { RestFormats.localDateRead.reads(Json.obj()) shouldBe a[JsError] } "return a JsError for a JsString that is not a well-formatted date" in { RestFormats.localDateRead.reads(JsString("not a valid date")) shouldBe a[JsError] } "return a JsError for a JsString that is well formatted but has bad values" in { RestFormats.localDateRead.reads(JsString("1994-13-32")) shouldBe a[JsError] } } }
Example 62
Source File: JsPathEnrichmentSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.http.controllers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import play.api.libs.json._ class JsPathEnrichmentSpec extends AnyWordSpecLike with Matchers { "Parsing json when the path does not exist prior to the structure being parsed" should { "result in None without failure when early sections of path are not present" in new Setup { pathDoesNotExistJson.validate[Option[BigDecimal]] shouldBe JsSuccess(None) } "result in None without failure when the patch exists but the value does not" in new Setup { pathDoesNotExistJson.validate[Option[BigDecimal]] shouldBe JsSuccess(None) } "result in value when path exists" in new Setup { pathAndValueExistsJson.validate[Option[BigDecimal]] match { case s: JsSuccess[Option[BigDecimal]] => s.value shouldBe Some(BigDecimal("899.80")) case e: JsError => fail(s"Should have parsed bigDecimal, failed with $e") } } } class Setup { import JsPathEnrichment.RichJsPath implicit val reads: Reads[Option[BigDecimal]] = (JsPath \ "rti" \ "balance").tolerantReadNullable[BigDecimal] val pathDoesNotExistJson = Json.parse(""" |{ | "nonRti": { | "paidToDate": 200.25 | } |} """.stripMargin) val pathExistsAndValueMissingJson = Json.parse(""" |{ | "rti": { | "notTheBalance": 123.45 | } |} """.stripMargin) val pathAndValueExistsJson = Json.parse(""" |{ | "rti": { | "balance": 899.80 | } |} """.stripMargin) } }
Example 63
Source File: HttpPatchSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.http import akka.actor.ActorSystem import com.typesafe.config.Config import org.mockito.ArgumentCaptor import org.mockito.Matchers.{any, eq => is} import org.mockito.Mockito._ import org.scalatestplus.mockito.MockitoSugar import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import play.api.libs.json.{Json, Writes} import uk.gov.hmrc.http.hooks.{HookData, HttpHook} import scala.concurrent.{ExecutionContext, Future} import uk.gov.hmrc.http.HttpReads.Implicits._ class HttpPatchSpec extends AnyWordSpecLike with Matchers with CommonHttpBehaviour { import ExecutionContext.Implicits.global class StubbedHttpPatch(doPatchResult: Future[HttpResponse], doPatchWithHeaderResult: Future[HttpResponse]) extends HttpPatch with ConnectionTracingCapturing with MockitoSugar { val testHook1 = mock[HttpHook] val testHook2 = mock[HttpHook] val hooks = Seq(testHook1, testHook2) override def configuration: Option[Config] = None override protected def actorSystem: ActorSystem = ActorSystem("test-actor-system") def doPatch[A](url: String, body: A, headers: Seq[(String, String)])(implicit rds: Writes[A], hc: HeaderCarrier, ec: ExecutionContext) = doPatchResult } "HttpPatch" should { val testObject = TestRequestClass("a", 1) "be able to return plain responses" in { val response = HttpResponse(200, testBody) val testPatch = new StubbedHttpPatch(Future.successful(response), Future.successful(response)) testPatch.PATCH[TestRequestClass, HttpResponse](url, testObject, Seq("header" -> "foo")).futureValue shouldBe response } "be able to return objects deserialised from JSON" in { val response= Future.successful(HttpResponse(200, """{"foo":"t","bar":10}""")) val testPatch = new StubbedHttpPatch(response, response) testPatch.PATCH[TestRequestClass, TestClass](url, testObject, Seq("header" -> "foo")).futureValue should be(TestClass("t", 10)) } behave like anErrorMappingHttpCall( "PATCH", (url, responseF) => new StubbedHttpPatch(responseF, responseF).PATCH[TestRequestClass, HttpResponse](url, testObject, Seq("header" -> "foo"))) behave like aTracingHttpCall("PATCH", "PATCH", new StubbedHttpPatch(defaultHttpResponse, defaultHttpResponse)) { _.PATCH[TestRequestClass, HttpResponse](url, testObject, Seq("header" -> "foo")) } "Invoke any hooks provided" in { val dummyResponse = HttpResponse(200, testBody) val dummyResponseFuture = Future.successful(dummyResponse) val testPatch = new StubbedHttpPatch(dummyResponseFuture, dummyResponseFuture) val testJson = Json.stringify(trcreads.writes(testObject)) testPatch.PATCH[TestRequestClass, HttpResponse](url, testObject, Seq("header" -> "foo")).futureValue val respArgCaptor1 = ArgumentCaptor.forClass(classOf[Future[HttpResponse]]) val respArgCaptor2 = ArgumentCaptor.forClass(classOf[Future[HttpResponse]]) verify(testPatch.testHook1) .apply(is(url), is("PATCH"), is(Some(HookData.FromString(testJson))), respArgCaptor1.capture())(any(), any()) verify(testPatch.testHook2) .apply(is(url), is("PATCH"), is(Some(HookData.FromString(testJson))), respArgCaptor2.capture())(any(), any()) // verifying directly without ArgumentCaptor didn't work as Futures were different instances // e.g. Future.successful(5) != Future.successful(5) respArgCaptor1.getValue.futureValue shouldBe dummyResponse respArgCaptor2.getValue.futureValue shouldBe dummyResponse } } }
Example 64
Source File: CommonHttpBehaviour.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.http import java.net.ConnectException import java.util.concurrent.TimeoutException import org.scalatest.concurrent.ScalaFutures import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import play.api.libs.json.Json import uk.gov.hmrc.http.logging.{ConnectionTracing, LoggingDetails} import scala.collection.mutable import scala.concurrent.{ExecutionContext, Future} trait CommonHttpBehaviour extends ScalaFutures with Matchers with AnyWordSpecLike { case class TestClass(foo: String, bar: Int) implicit val tcreads = Json.format[TestClass] case class TestRequestClass(baz: String, bar: Int) implicit val trcreads = Json.format[TestRequestClass] implicit val hc = HeaderCarrier() val testBody = "testBody" val testRequestBody = "testRequestBody" val url = "http://some.url" def response(returnValue: Option[String] = None, statusCode: Int = 200) = Future.successful(HttpResponse( status = statusCode, body = returnValue.getOrElse("") )) val defaultHttpResponse = response() def anErrorMappingHttpCall(verb: String, httpCall: (String, Future[HttpResponse]) => Future[_]) = { s"throw a GatewayTimeout exception when the HTTP $verb throws a TimeoutException" in { implicit val hc = HeaderCarrier() val url: String = "http://some.nonexistent.url" val e = httpCall(url, Future.failed(new TimeoutException("timeout"))).failed.futureValue e should be(a[GatewayTimeoutException]) e.getMessage should startWith(verb) e.getMessage should include(url) } s"throw a BadGateway exception when the HTTP $verb throws a ConnectException" in { implicit val hc = HeaderCarrier() val url: String = "http://some.nonexistent.url" val e = httpCall(url, Future.failed(new ConnectException("timeout"))).failed.futureValue e should be(a[BadGatewayException]) e.getMessage should startWith(verb) e.getMessage should include(url) } } def aTracingHttpCall[T <: ConnectionTracingCapturing](verb: String, method: String, httpBuilder: => T)( httpAction: (T => Future[_]))(implicit mf: Manifest[T]) = s"trace exactly once when the HTTP $verb calls $method" in { val http = httpBuilder httpAction(http).futureValue http.traceCalls should have size 1 http.traceCalls.head._1 shouldBe verb } } trait ConnectionTracingCapturing extends ConnectionTracing { val traceCalls = mutable.Buffer[(String, String)]() override def withTracing[T](method: String, uri: String)( body: => Future[T])(implicit ld: LoggingDetails, ec: ExecutionContext) = { traceCalls += ((method, uri)) body } }
Example 65
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 66
Source File: WSProxyConfigurationSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.http.ws import org.scalatest.BeforeAndAfter import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import play.api.libs.ws.DefaultWSProxyServer import play.api.test.{FakeApplication, WithApplication} import uk.gov.hmrc.play.http.ws.WSProxyConfiguration.ProxyConfigurationException class WSProxyConfigurationSpec extends AnyWordSpecLike with Matchers with BeforeAndAfter { def proxyFlagConfiguredTo(value: Boolean): Map[String, Any] = Map("Dev.httpProxy.proxyRequiredForThisEnvironment" -> value) def proxyConfigWithFlagSetTo(flag: Option[Boolean] = None): Map[String, Any] = Map( "Dev.httpProxy.protocol" -> "https", "Dev.httpProxy.host" -> "localhost", "Dev.httpProxy.port" -> 7979, "Dev.httpProxy.username" -> "user", "Dev.httpProxy.password" -> "secret" ) ++ flag.fold(Map.empty[String, Any])(flag => proxyFlagConfiguredTo(flag)) val proxy = DefaultWSProxyServer( protocol = Some("https"), host = "localhost", port = 7979, principal = Some("user"), password = Some("secret") ) "If the proxyRequiredForThisEnvironment flag is not present, the WSProxyConfiguration apply method" should { "fail if no proxy is defined" in new WithApplication(FakeApplication()) { a[ProxyConfigurationException] should be thrownBy WSProxyConfiguration("Dev.httpProxy") } "return the proxy configuration if the proxy is defined" in new WithApplication( FakeApplication(additionalConfiguration = proxyConfigWithFlagSetTo(None))) { WSProxyConfiguration("Dev.httpProxy") shouldBe Some(proxy) } } "If the proxyRequiredForThisEnvironment flag is set to true, the WSProxyConfiguration apply method" should { "fail if no proxy is defined" in new WithApplication( FakeApplication(additionalConfiguration = proxyFlagConfiguredTo(value = true))) { a[ProxyConfigurationException] should be thrownBy WSProxyConfiguration("Dev.httpProxy") } "return the proxy configuration if the proxy is defined" in new WithApplication( FakeApplication(additionalConfiguration = proxyConfigWithFlagSetTo(Some(true)))) { WSProxyConfiguration("Dev.httpProxy") shouldBe Some(proxy) } } "If the proxyRequiredForThisEnvironment flag is set to false, the WSProxyConfiguration apply method" should { "return None if no proxy is defined" in new WithApplication( FakeApplication(additionalConfiguration = proxyFlagConfiguredTo(value = false))) { WSProxyConfiguration("Dev.httpProxy") shouldBe None } "return None if the proxy is defined" in new WithApplication( FakeApplication(additionalConfiguration = proxyConfigWithFlagSetTo(Some(false)))) { WSProxyConfiguration("Dev.httpProxy") shouldBe None } } }
Example 67
Source File: HttpTimeoutSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.http import java.net.{ServerSocket, URI} import java.util.concurrent.TimeoutException import org.scalatest.concurrent.ScalaFutures import org.scalatest.BeforeAndAfterAll import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import org.webbitserver.handler.{DelayedHttpHandler, StringHttpHandler} import org.webbitserver.netty.NettyWebServer import play.api.Play import play.api.test.FakeApplication import uk.gov.hmrc.http.HeaderCarrier import uk.gov.hmrc.play.http.ws.WSHttp import uk.gov.hmrc.play.test.TestHttpCore import scala.concurrent.Await import scala.concurrent.duration.DurationInt import scala.concurrent.ExecutionContext.Implicits.global class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll { lazy val fakeApplication = FakeApplication(additionalConfiguration = Map("ws.timeout.request" -> "1000")) override def beforeAll() { super.beforeAll() Play.start(fakeApplication) } override def afterAll() { super.afterAll() Play.stop(fakeApplication) } "HttpCalls" should { "be gracefully timeout when no response is received within the 'timeout' frame" in { val http = new WSHttp with TestHttpCore // get an unused port val ss = new ServerSocket(0) ss.close() val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}") val ws = new NettyWebServer(global, ss.getLocalSocketAddress, publicUri) try { //starts web server ws.add( "/test", new DelayedHttpHandler(global, 2000, new StringHttpHandler("application/json", "{name:'pong'}"))) ws.start().get() implicit val hc = HeaderCarrier() val start = System.currentTimeMillis() intercept[TimeoutException] { //make request to web server Await.result(http.doPost(s"$publicUri/test", "{name:'ping'}", Seq()), 5.seconds) } val diff = (System.currentTimeMillis() - start).toInt // there is test execution delay around 700ms diff should be >= 1000 diff should be < 2500 } finally { ws.stop() } } } }
Example 68
Source File: ConnectorSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.connectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import play.api.test.FakeApplication import play.api.test.Helpers._ import uk.gov.hmrc.http._ import uk.gov.hmrc.http.logging.{Authorization, ForwardedFor, RequestId, SessionId} class ConnectorSpec extends AnyWordSpecLike with Matchers { class TestConfig(val builderName: String, val builder: RequestBuilder, setupFunc: ((=> Any) => Any)) { def setup(f: => Any) = setupFunc(f) } val withFakeApp: (=> Any) => Any = running(FakeApplication()) def withoutFakeApp(f: => Any) = f val permutations = Seq( new TestConfig("PlayWS Request Builder", new PlayWSRequestBuilder {}, withFakeApp), new TestConfig( "WSClient Request Builder", new WSClientRequestBuilder with DefaultWSClientProvider {}, withoutFakeApp) ) "AuthConnector.buildRequest" should { permutations.foreach { p => s"add expected headers to the request using the ${p.builderName}" in p.setup { val testAuthorisation = Authorization("someauth") val forwarded = ForwardedFor("forwarded") val token = Token("token") val sessionId = SessionId("session") val requestId = RequestId("requestId") val deviceID = "deviceIdTest" val carrier: HeaderCarrier = HeaderCarrier( authorization = Some(testAuthorisation), token = Some(token), forwarded = Some(forwarded), sessionId = Some(sessionId), requestId = Some(requestId), deviceID = Some(deviceID), otherHeaders = Seq("path" -> "/the/request/path") ) val request = p.builder.buildRequest("authBase")(carrier) request.headers.get(HeaderNames.authorisation).flatMap(_.headOption) shouldBe Some(testAuthorisation.value) request.headers.get(HeaderNames.xForwardedFor).flatMap(_.headOption) shouldBe Some(forwarded.value) request.headers.get(HeaderNames.token).flatMap(_.headOption) shouldBe Some(token.value) request.headers.get(HeaderNames.xSessionId).flatMap(_.headOption) shouldBe Some(sessionId.value) request.headers.get(HeaderNames.xRequestId).flatMap(_.headOption) shouldBe Some(requestId.value) request.headers.get(HeaderNames.deviceID).flatMap(_.headOption) shouldBe Some(deviceID) request.headers.get("path") shouldBe None } } } }
Example 69
Source File: HttpTimeoutSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.http import java.net.{ServerSocket, URI} import java.util.concurrent.TimeoutException import org.scalatest.concurrent.ScalaFutures import org.scalatest.BeforeAndAfterAll import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import org.webbitserver.handler.{DelayedHttpHandler, StringHttpHandler} import org.webbitserver.netty.NettyWebServer import play.api.inject.guice.GuiceApplicationBuilder import play.api.libs.ws.WSClient import play.api.test.WsTestClient import play.api.{Configuration, Play} import uk.gov.hmrc.http.HeaderCarrier import uk.gov.hmrc.play.http.ws.WSHttp import uk.gov.hmrc.play.test.TestHttpCore import scala.concurrent.{Await, ExecutionContext} import scala.concurrent.duration.DurationInt class HttpTimeoutSpec extends AnyWordSpecLike with Matchers with ScalaFutures with BeforeAndAfterAll { import ExecutionContext.Implicits.global lazy val fakeApplication = GuiceApplicationBuilder(configuration = Configuration("play.ws.timeout.request" -> "1000ms")).build() override def beforeAll() { super.beforeAll() Play.start(fakeApplication) } override def afterAll() { super.afterAll() Play.stop(fakeApplication) } WsTestClient.withClient{ client => "HttpCalls" should { "be gracefully timeout when no response is received within the 'timeout' frame" in { val http = new WSHttp with TestHttpCore { override val wsClient = fakeApplication.injector.instanceOf[WSClient] } // get an unused port val ss = new ServerSocket(0) ss.close() val executor = ExecutionContext.global // fromExecutorService(ExecutionContext.global) val publicUri = URI.create(s"http://localhost:${ss.getLocalPort}") val ws = new NettyWebServer(executor, ss.getLocalSocketAddress, publicUri) try { //starts web server ws.add( "/test", new DelayedHttpHandler(executor, 2000, new StringHttpHandler("application/json", "{name:'pong'}"))) ws.start().get() implicit val hc = HeaderCarrier() val start = System.currentTimeMillis() intercept[TimeoutException] { //make request to web server Await.result(http.doPost(s"$publicUri/test", "{name:'ping'}", Seq()), 5.seconds) } val diff = (System.currentTimeMillis() - start).toInt // there is test execution delay around 700ms diff should be >= 1000 diff should be < 2500 } finally { ws.stop() } } } } }
Example 70
Source File: ConnectorSpec.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.connectors import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import org.scalatestplus.mockito.MockitoSugar import play.api.test.WsTestClient import uk.gov.hmrc.http._ import uk.gov.hmrc.http.logging.{Authorization, ForwardedFor, RequestId, SessionId} class ConnectorSpec extends AnyWordSpecLike with Matchers with MockitoSugar { WsTestClient.withClient(wsClient => { "AuthConnector.buildRequest" should { val builder = new WSClientRequestBuilder { val client = wsClient } s"add expected headers to the request" in { val testAuthorisation = Authorization("someauth") val forwarded = ForwardedFor("forwarded") val token = Token("token") val sessionId = SessionId("session") val requestId = RequestId("requestId") val deviceID = "deviceIdTest" val carrier: HeaderCarrier = HeaderCarrier( authorization = Some(testAuthorisation), token = Some(token), forwarded = Some(forwarded), sessionId = Some(sessionId), requestId = Some(requestId), deviceID = Some(deviceID), otherHeaders = Seq("path" -> "/the/request/path") ) val request = builder.buildRequest("http://auth.base")(carrier) request.headers.get(HeaderNames.authorisation).flatMap(_.headOption) shouldBe Some(testAuthorisation.value) request.headers.get(HeaderNames.xForwardedFor).flatMap(_.headOption) shouldBe Some(forwarded.value) request.headers.get(HeaderNames.token).flatMap(_.headOption) shouldBe Some(token.value) request.headers.get(HeaderNames.xSessionId).flatMap(_.headOption) shouldBe Some(sessionId.value) request.headers.get(HeaderNames.xRequestId).flatMap(_.headOption) shouldBe Some(requestId.value) request.headers.get(HeaderNames.deviceID).flatMap(_.headOption) shouldBe Some(deviceID) request.headers.get("path") shouldBe None } } }) }
Example 71
Source File: ProjectionsSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.sourcing.projections import akka.actor.ActorSystem import akka.persistence.query.Offset import akka.stream.scaladsl.Source import akka.testkit.{TestKit, TestKitBase} import cats.effect.{ContextShift, IO} import ch.epfl.bluebrain.nexus.sourcing.projections.Fixture.memoize import ch.epfl.bluebrain.nexus.sourcing.projections.ProjectionProgress._ import ch.epfl.bluebrain.nexus.sourcing.projections.ProjectionsSpec.SomeEvent import io.circe.generic.auto._ import org.scalatest.concurrent.Eventually import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{BeforeAndAfterAll, DoNotDiscover} import scala.concurrent.duration._ //noinspection TypeAnnotation @DoNotDiscover class ProjectionsSpec extends TestKitBase with AnyWordSpecLike with Matchers with TestHelpers with IOValues with Eventually with BeforeAndAfterAll { implicit override lazy val system: ActorSystem = SystemBuilder.persistence("ProjectionsSpec") implicit private val contextShift: ContextShift[IO] = IO.contextShift(system.dispatcher) override protected def afterAll(): Unit = { TestKit.shutdownActorSystem(system) super.afterAll() } "A Projection" should { val id = genString() val persistenceId = s"/some/${genString()}" val projections = memoize(Projections[IO, SomeEvent]).unsafeRunSync() val progress = OffsetProgress(Offset.sequence(42), 42, 42, 0) "store progress" in { projections.ioValue.recordProgress(id, progress).ioValue } "retrieve stored progress" in { projections.ioValue.progress(id).ioValue shouldEqual progress } "retrieve NoProgress for unknown projections" in { projections.ioValue.progress(genString()).ioValue shouldEqual NoProgress } val firstOffset: Offset = Offset.sequence(42) val secondOffset: Offset = Offset.sequence(98) val firstEvent = SomeEvent(1L, "description") val secondEvent = SomeEvent(2L, "description2") "store an event" in { projections.ioValue.recordFailure(id, persistenceId, 1L, firstOffset, firstEvent).ioValue } "store another event" in { projections.ioValue.recordFailure(id, persistenceId, 2L, secondOffset, secondEvent).ioValue } "retrieve stored events" in { val expected = Seq((firstEvent, firstOffset), (secondEvent, secondOffset)) eventually { logOf(projections.ioValue.failures(id)) should contain theSameElementsInOrderAs expected } } "retrieve empty list of events for unknown failure log" in { eventually { logOf(projections.ioValue.failures(genString())) shouldBe empty } } } private def logOf(source: Source[(SomeEvent, Offset), _]): Vector[(SomeEvent, Offset)] = { val f = source.runFold(Vector.empty[(SomeEvent, Offset)])(_ :+ _) IO.fromFuture(IO(f)).ioValue } implicit override def patienceConfig: PatienceConfig = PatienceConfig(30.seconds, 50.milliseconds) } object ProjectionsSpec { final case class SomeEvent(rev: Long, description: String) }
Example 72
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 73
Source File: ProjectionProgressSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.sourcing.projections import java.util.UUID import akka.persistence.query.{Offset, Sequence, TimeBasedUUID} import ch.epfl.bluebrain.nexus.sourcing.projections.ProjectionProgress._ import ch.epfl.bluebrain.nexus.sourcing.projections.implicits._ import io.circe.Encoder import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.{EitherValues, Inspectors} class ProjectionProgressSpec extends AnyWordSpecLike with Matchers with Inspectors with TestHelpers with EitherValues { "A ProjectionProgress" should { val mapping = Map( OffsetProgress(Sequence(14L), 2, 0, 1) -> jsonContentOf("/indexing/sequence-offset-progress.json"), OffsetProgress(TimeBasedUUID(UUID.fromString("ee7e4360-39ca-11e9-9ed5-dbdaa32f8986")), 32, 5, 10) -> jsonContentOf("/indexing/timebaseduuid-offset-progress.json"), NoProgress -> jsonContentOf("/indexing/no-offset-progress.json"), OffsetsProgress(Map("noOffset" -> NoProgress, "other" -> OffsetProgress(Sequence(2L), 10L, 2L, 0L))) -> jsonContentOf("/indexing/offsets-progress.json") ) "properly encode progress values" in { forAll(mapping.toList) { case (prog, repr) => Encoder[ProjectionProgress].apply(prog) shouldEqual repr } } "properly decode progress values" in { forAll(mapping.toList) { case (prog, repr) => repr.as[ProjectionProgress].rightValue shouldEqual prog } } "Add progress" in { val progress = OffsetsProgress(Map("noOffset" -> NoProgress, "other" -> OffsetProgress(Sequence(2L), 10L, 2L, 0L))) progress + ("noOffset", Sequence(1L), ProgressStatus.Failed("some error")) shouldEqual OffsetsProgress( Map( "noOffset" -> OffsetProgress(Sequence(1L), 1L, 0L, 1L), "other" -> OffsetProgress(Sequence(2L), 10L, 2L, 0L) ) ) progress + ("other", Sequence(3L), ProgressStatus.Discarded) shouldEqual OffsetsProgress(Map("noOffset" -> NoProgress, "other" -> OffsetProgress(Sequence(3L), 11L, 3L, 0L))) } "fetch minimum progress" in { val progress = OffsetsProgress( Map( "one" -> OffsetProgress(Sequence(1L), 2L, 1L, 0L), "other" -> OffsetProgress(Sequence(2L), 10L, 2L, 0L), "a" -> OffsetProgress(Sequence(0L), 0L, 0L, 0L) ) ) progress.minProgressFilter(_.length > 1) shouldEqual OffsetProgress(Sequence(1L), 2L, 1L, 0L) progress.minProgress shouldEqual OffsetProgress(Sequence(0L), 0L, 0L, 0L) } "test TimeBasedUUIDd ordering" in { val time1 = TimeBasedUUID(UUID.fromString("49225740-2019-11ea-a752-ffae2393b6e4")) // 2019-12-16T15:32:36.148Z[UTC] val time2 = TimeBasedUUID(UUID.fromString("91be23d0-2019-11ea-a752-ffae2393b6e4")) // 2019-12-16T15:34:37.965Z[UTC] val time3 = TimeBasedUUID(UUID.fromString("91f95810-2019-11ea-a752-ffae2393b6e4")) // 2019-12-16T15:34:38.353Z[UTC] val offset1: Offset = time1 val offset2: Offset = time2 val offset3: Offset = time3 time1.asInstant.isBefore(time2.asInstant) shouldEqual true time2.asInstant.isBefore(time3.asInstant) shouldEqual true offset1.gt(offset2) shouldEqual false offset3.gt(offset2) shouldEqual true List(time2, time1, time3).sorted(offsetOrdering) shouldEqual List(time1, time2, time3) } } }
Example 74
Source File: QueryDirectivesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.admin.directives import java.net.URLEncoder import java.util.UUID import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives.{complete, get} import akka.http.scaladsl.server.Route import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.admin.routes.SearchParams import ch.epfl.bluebrain.nexus.admin.routes.SearchParams.Field import ch.epfl.bluebrain.nexus.commons.http.JsonLdCirceSupport._ import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.service.config.ServiceConfig.HttpConfig import ch.epfl.bluebrain.nexus.service.config.Settings import ch.epfl.bluebrain.nexus.service.routes.Routes import ch.epfl.bluebrain.nexus.util.EitherValues import io.circe.generic.auto._ import org.mockito.IdiomaticMockito import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class QueryDirectivesSpec extends AnyWordSpecLike with ScalatestRouteTest with Matchers with ScalaFutures with EitherValues with IdiomaticMockito { private def genIri: AbsoluteIri = url"http://nexus.example.com/${UUID.randomUUID()}" private def encode(url: AbsoluteIri): String = URLEncoder.encode(url.asString, "UTF-8") private val config = Settings(system).serviceConfig implicit private val http: HttpConfig = config.http private def routes(inner: Route): Route = Routes.wrap(inner) "Query directives" should { "handle query params" in { val createdBy = genIri val updatedBy = genIri val type1 = genIri val type2 = genIri def projectParams = Routes.wrap( (get & QueryDirectives.searchParamsProjects) { params => complete(StatusCodes.OK -> params) } ) Get("/") ~> routes(projectParams) ~> check { responseAs[SearchParams] shouldEqual SearchParams.empty } Get( s"/?rev=1&deprecated=true&label=myLabel&type=${encode(type1)}&type=${encode(type2)}&createdBy=${encode(createdBy)}&updatedBy=${encode(updatedBy)}" ) ~> routes(projectParams) ~> check { responseAs[SearchParams] shouldEqual SearchParams( rev = Some(1L), deprecated = Some(true), projectLabel = Some(Field("myLabel", exactMatch = false)), types = Set(type1, type2), createdBy = Some(createdBy), updatedBy = Some(updatedBy) ) } } } }
Example 75
Source File: QueryResultsSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.search import cats.syntax.functor._ import ch.epfl.bluebrain.nexus.commons.search.QueryResult._ import ch.epfl.bluebrain.nexus.commons.search.QueryResults._ import io.circe.Json import io.circe.generic.auto._ import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class QueryResultsSpec extends AnyWordSpecLike with Matchers { "A QueryResults Functor" should { "transform the source and score values of the results" in { val qrs = ScoredQueryResults(1L, 1f, List(ScoredQueryResult(1f, 1))) qrs.map(_ + 1) shouldEqual ScoredQueryResults(1L, 1f, List(ScoredQueryResult(1f, 2))) } "transform the score values of the results" in { val qrs = UnscoredQueryResults(1L, List(UnscoredQueryResult(1))) qrs.map(_ + 1) shouldEqual UnscoredQueryResults(1L, List(UnscoredQueryResult(2))) } "transform the generic queryResults values" in { val qrs = UnscoredQueryResults(1L, List(UnscoredQueryResult(1))): QueryResults[Int] qrs.map(_ + 1) shouldEqual UnscoredQueryResults(1L, List(UnscoredQueryResult(2))) } "encodes a queryResults" in { val result = ScoredQueryResult(1f, 1): QueryResult[Int] val results = ScoredQueryResults(10L, 1f, List(result), Some("token")): QueryResults[Int] results.asJson shouldEqual Json.obj( "total" -> Json.fromLong(results.total), "token" -> Json.fromString("token"), "maxScore" -> Json.fromFloatOrNull(1f), "results" -> Json.arr(result.asJson) ) } "build from apply method" in { QueryResults(0L, List.empty[QueryResult[Int]]) shouldEqual UnscoredQueryResults(0L, List.empty[QueryResult[Int]]) QueryResults(0L, 1f, List.empty[QueryResult[Int]]) shouldEqual ScoredQueryResults( 0L, 1f, List.empty[QueryResult[Int]] ) } "change the underlying list type with copy method" in { val unscored = QueryResults(1L, List(UnscoredQueryResult(1))) unscored.copyWith(unscored.results.map(_.map(_.toString))) shouldEqual QueryResults( 1L, List(UnscoredQueryResult("1")) ) val scored = QueryResults(1L, List(ScoredQueryResult(1f, 1))) scored.copyWith(scored.results.map(_.map(_.toString))) shouldEqual QueryResults( 1L, List(ScoredQueryResult(1f, "1")) ) } } }
Example 76
Source File: QueryResultSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.search import cats.syntax.functor._ import ch.epfl.bluebrain.nexus.commons.search.QueryResult.ScoredQueryResult import io.circe.generic.auto._ import io.circe.{Encoder, Json} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class QueryResultSpec extends AnyWordSpecLike with Matchers { "A QueryResult Functor" should { implicit val queryResultEncoder: Encoder[ScoredQueryResult[Int]] = Encoder.encodeJson.contramap { qr => Json.obj( "resultId" -> Json.fromString("/some/path"), "score" -> Json.fromFloatOrString(qr.score), "source" -> Json.fromInt(qr.source) ) } "transform the source value" in { ScoredQueryResult(1f, 1).map(_ + 1) shouldEqual ScoredQueryResult(1f, 2) } "encodes a queryResult" in { import io.circe.syntax._ val result = ScoredQueryResult(1f, 1): QueryResult[Int] result.asJson shouldEqual Json.obj( "resultId" -> Json.fromString("/some/path"), "score" -> Json.fromFloatOrString(1f), "source" -> Json.fromInt(result.source) ) } } }
Example 77
Source File: StaticResourceIamAdminRoutesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.http.routes import java.util.UUID import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.commons.http.RdfMediaTypes import org.scalatest.Inspectors import java.util.regex.Pattern.quote import ch.epfl.bluebrain.nexus.util.Resources import com.typesafe.config.{Config, ConfigFactory} import io.circe.parser.parse import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class StaticResourceIamAdminRoutesSpec extends AnyWordSpecLike with Matchers with Inspectors with ScalatestRouteTest with Resources { val baseUri = "http://nexus.example.com/v1" override def testConfig: Config = ConfigFactory.empty() val staticRoutes = new StaticResourceRoutes( Map( "/contexts/context1" -> "/commons/static-routes-test/contexts/context1.json", "/contexts/context2" -> "/commons/static-routes-test/contexts/context2.json", "/schemas/schema1" -> "/commons/static-routes-test/schemas/schema1.json", "/schemas/schema2" -> "/commons/static-routes-test/schemas/schema2.json" ), "test", baseUri ).routes val baseReplacement = Map( quote("{{base}}") -> baseUri ) val files = Map( "/v1/test/contexts/context1" -> jsonContentOf( "/commons/static-routes-test/contexts/context1.json", baseReplacement ), "/v1/test/contexts/context2" -> jsonContentOf( "/commons/static-routes-test/contexts/context2.json", baseReplacement ), "/v1/test/schemas/schema1" -> jsonContentOf("/commons/static-routes-test/schemas/schema1.json", baseReplacement), "/v1/test/schemas/schema2" -> jsonContentOf("/commons/static-routes-test/schemas/schema2.json", baseReplacement) ) "A StaticResourceRoutes" should { "return static resources" in { forAll(files.toList) { case (path, json) => Get(path) ~> staticRoutes ~> check { status shouldEqual StatusCodes.OK contentType shouldEqual RdfMediaTypes.`application/ld+json`.toContentType parse(responseAs[String]).toOption.get shouldEqual json } } } "return 404 when resource doesn't exist" in { Get(s"/v1/test/schemas/${UUID.randomUUID().toString}") ~> staticRoutes ~> check { rejections shouldEqual Seq() } } } }
Example 78
Source File: PrefixDirectivesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.http.directives import akka.http.scaladsl.model.{StatusCodes, Uri} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.commons.http.directives.PrefixDirectives._ import com.typesafe.config.{Config, ConfigFactory} import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers class PrefixDirectivesSpec extends AnyWordSpecLike with Matchers with Inspectors with ScalatestRouteTest { override def testConfig: Config = ConfigFactory.empty() "A PrefixDirective" should { "match the prefix uri" in { forAll( Map( "" -> "", "/" -> "", "///" -> "", "/dev" -> "/dev", "/dev/" -> "/dev", "/dev///" -> "/dev", "/dev/sn/" -> "/dev/sn" ).toList ) { case (suffix, prefix) => val uri = Uri("http://localhost:80" + suffix) val route = uriPrefix(uri) { path("remainder") { get { complete(StatusCodes.OK) } } } Get(prefix + "/remainder") ~> route ~> check { status shouldEqual StatusCodes.OK } } } } }
Example 79
Source File: ErrorDirectivesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.http.directives import akka.http.scaladsl.marshalling.Marshal import akka.http.scaladsl.model.{HttpEntity, HttpResponse, StatusCodes} import akka.util.ByteString import ch.epfl.bluebrain.nexus.commons.circe.ContextUri import ch.epfl.bluebrain.nexus.commons.http.RdfMediaTypes import ch.epfl.bluebrain.nexus.commons.http.directives.ErrorDirectives._ import ch.epfl.bluebrain.nexus.commons.http.directives.ErrorDirectivesSpec.CustomError import ch.epfl.bluebrain.nexus.rdf.syntax.iri._ import ch.epfl.bluebrain.nexus.util.ActorSystemFixture import io.circe.generic.auto._ import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class ErrorDirectivesSpec extends ActorSystemFixture("ErrorDirectivesSpec") with AnyWordSpecLike with Matchers with ScalaFutures { implicit override val patienceConfig = PatienceConfig(3.seconds, 100.millis) "A ErrorDirectives" should { import system.dispatcher implicit val statusFromJson: StatusFrom[CustomError] = StatusFrom((_: CustomError) => StatusCodes.NotFound) implicit val contextUri: ContextUri = ContextUri(url"http://localhost.com/error/") "marshall error JSON-LD" in { val error = CustomError("some error") val jsonString = s"""{"@context":"${contextUri.value}","message":"${error.message}"}""" Marshal(error).to[HttpResponse].futureValue shouldEqual HttpResponse( status = StatusCodes.NotFound, entity = HttpEntity.Strict(RdfMediaTypes.`application/ld+json`, ByteString(jsonString, "UTF-8")) ) } } } object ErrorDirectivesSpec { final case class CustomError(message: String) }
Example 80
Source File: RejectionHandlingSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.http import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Rejection import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.util.EitherValues import com.typesafe.config.{Config, ConfigFactory} import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._ import io.circe.{Json, Printer} import io.circe.parser._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class RejectionHandlingSpec extends AnyWordSpecLike with Matchers with Inspectors with ScalatestRouteTest with EitherValues { class Custom extends Rejection override def testConfig: Config = ConfigFactory.empty() implicit private val printer: Printer = Printer.spaces2.copy(dropNullValues = true) "A default rejection handler" should { val handler = RejectionHandling { _: Custom => StatusCodes.InternalServerError -> Json.obj("reason" -> Json.fromString("custom")) }.withFallback(RejectionHandling.notFound) "handle not found" in { val route = handleRejections(handler)(pathEnd(complete("ok"))) Get("/a") ~> route ~> check { val expected = s"""{ | "@context": "https://bluebrain.github.io/nexus/contexts/error.json", | "@type": "NotFound", | "reason": "The requested resource could not be found." |}""".stripMargin status shouldEqual StatusCodes.NotFound responseAs[Json] shouldEqual parse(expected).rightValue } } "handle missing query param" in { val route = handleRejections(handler)(parameter("rev".as[Long])(_ => complete("ok"))) Get("/a") ~> route ~> check { val expected = s"""{ | "@context": "https://bluebrain.github.io/nexus/contexts/error.json", | "@type": "MissingQueryParam", | "reason": "Request is missing required query parameter 'rev'." |}""".stripMargin status shouldEqual StatusCodes.BadRequest responseAs[Json] shouldEqual parse(expected).rightValue } } "handle custom" in { val route = handleRejections(handler)(reject(new Custom)) Get("/a") ~> route ~> check { val expected = s"""{ | "reason": "custom" |}""".stripMargin status shouldEqual StatusCodes.InternalServerError responseAs[Json] shouldEqual parse(expected).rightValue } } } }
Example 81
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] } } }
Example 82
Source File: ElasticSearchDecoderSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.es.client import ch.epfl.bluebrain.nexus.commons.search.QueryResults import ch.epfl.bluebrain.nexus.commons.search.QueryResult.ScoredQueryResult import ch.epfl.bluebrain.nexus.commons.search.QueryResults.{ScoredQueryResults, UnscoredQueryResults} import ch.epfl.bluebrain.nexus.util.Resources import io.circe.{Decoder, Json} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class ElasticSearchDecoderSpec extends AnyWordSpecLike with Matchers with Resources { "A ElasticSearchDecoder" should { implicit val D: Decoder[QueryResults[Json]] = ElasticSearchDecoder[Json] "decode ElasticSearch response " in { val response = jsonContentOf("/commons/es/elastic_response.json") val json1 = Json.obj("key" -> Json.fromString("a"), "key2" -> Json.fromString("b")) val json2 = Json.obj("key" -> Json.fromString("c"), "key2" -> Json.fromString("d")) response.as[QueryResults[Json]].toOption.get shouldEqual ScoredQueryResults( 2L, 1f, List(ScoredQueryResult(0.5f, json1), ScoredQueryResult(0.8f, json2)) ) } "decode ElasticSearch empty response" in { val response = jsonContentOf("/commons/es/elastic_response_0.json") response.as[QueryResults[Json]].toOption.get shouldEqual UnscoredQueryResults(0L, List.empty) } } }
Example 83
Source File: ElasticServer.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.commons.es.server.embed import java.nio.file.Files import java.util.Arrays._ import akka.http.scaladsl.model.Uri import ch.epfl.bluebrain.nexus.commons.es.server.embed.ElasticServer.MyNode import ch.epfl.bluebrain.nexus.util.{ActorSystemFixture, Randomness} import org.apache.commons.io.FileUtils import org.elasticsearch.common.settings.Settings import org.elasticsearch.index.reindex.ReindexPlugin import org.elasticsearch.node.Node import org.elasticsearch.painless.PainlessPlugin import org.elasticsearch.plugins.Plugin import org.elasticsearch.transport.Netty4Plugin import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.BeforeAndAfterAll import scala.jdk.CollectionConverters._ import scala.util.Try // $COVERAGE-OFF$ abstract class ElasticServer extends ActorSystemFixture("ElasticServer") with AnyWordSpecLike with BeforeAndAfterAll with Randomness { override protected def beforeAll(): Unit = { super.beforeAll() startElastic() } override protected def afterAll(): Unit = { stopElastic() super.afterAll() } val startPort = freePort() val endPort = startPort + 100 val esUri = Uri(s"http://localhost:$startPort") implicit val ec = system.dispatcher private val clusterName = "elasticsearch" private val dataDir = Files.createTempDirectory("elasticsearch_data_").toFile private val settings = Settings .builder() .put("path.home", dataDir.toString) .put("http.port", s"$startPort-$endPort") .put("http.cors.enabled", true) .put("cluster.name", clusterName) .put("http.type", "netty4") .build private lazy val node = new MyNode(settings, asList(classOf[Netty4Plugin], classOf[PainlessPlugin], classOf[ReindexPlugin])) def startElastic(): Unit = { node.start() () } def stopElastic(): Unit = { node.close() Try(FileUtils.forceDelete(dataDir)) () } } object ElasticServer extends Randomness { import java.util import org.elasticsearch.node.InternalSettingsPreparer private class MyNode(preparedSettings: Settings, classpathPlugins: util.Collection[Class[_ <: Plugin]]) extends Node( InternalSettingsPreparer .prepareEnvironment(preparedSettings, Map.empty[String, String].asJava, null, () => "elasticsearch"), classpathPlugins, true ) {} } // $COVERAGE-ON$
Example 84
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 85
Source File: TarFlowSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.archives import java.nio.file.Files import java.time.{Clock, Instant, ZoneId} import akka.actor.ActorSystem import akka.stream.scaladsl.FileIO import akka.testkit.TestKit import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.storage.digestSink import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class TarFlowSpec extends TestKit(ActorSystem("TarFlowSpec")) with AnyWordSpecLike with Matchers with TestHelper with ScalaFutures { implicit private val ec = system.dispatcher implicit private val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault()) implicit override def patienceConfig: PatienceConfig = PatienceConfig(55.second, 150.milliseconds) "A TarFlow" should { "tar a bunch of sources" in { val digest = "3fef41c5afe7a7ee11ee9d556a564fb57784cc5247b24c6ca70783f396fa158a1c7952504d3e1aa441de20cf065d740eec454c6ffb7fbc4b6351b950ee51c886" val elems = 500 val contents = List.tabulate(2) { i => val content = (i until (i + elems)).toList.mkString(",") + "\n" ArchiveSource(content.length.toLong, s"some/path$i/$i.txt", produce(content)) } val path = Files.createTempFile("test", ".tar") TarFlow.write(contents).runWith(FileIO.toPath(path)).futureValue FileIO.fromPath(path).runWith(digestSink("SHA-512")).futureValue.value shouldEqual digest Files.delete(path) } } }
Example 86
Source File: ArchiveCacheSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.archives import java.time.{Clock, Instant, ZoneId} import cats.effect.{IO, Timer} import ch.epfl.bluebrain.nexus.admin.client.types.Project import ch.epfl.bluebrain.nexus.commons.test.ActorSystemFixture import ch.epfl.bluebrain.nexus.commons.test.io.IOOptionValues import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.archives.Archive.{File, Resource, ResourceDescription} import ch.epfl.bluebrain.nexus.kg.resources.Id import ch.epfl.bluebrain.nexus.kg.resources.syntax._ import ch.epfl.bluebrain.nexus.service.config.Settings import org.scalatest.concurrent.Eventually import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ class ArchiveCacheSpec extends ActorSystemFixture("ArchiveCacheSpec", true) with TestHelper with AnyWordSpecLike with Matchers with IOOptionValues with Eventually { implicit override def patienceConfig: PatienceConfig = PatienceConfig(10.second, 50.milliseconds) private val appConfig = Settings(system).serviceConfig implicit private val config = appConfig.copy(kg = appConfig.kg.copy(archives = appConfig.kg.archives.copy(cacheInvalidateAfter = 500.millis, maxResources = 100)) ) implicit private val timer: Timer[IO] = IO.timer(system.dispatcher) implicit private val archivesCfg = config.kg.archives private val cache: ArchiveCache[IO] = ArchiveCache[IO].unsafeToFuture().futureValue implicit private val clock = Clock.fixed(Instant.EPOCH, ZoneId.systemDefault()) private val instant = clock.instant() def randomProject() = { val instant = Instant.EPOCH // format: off Project(genIri, genString(), genString(), None, genIri, genIri, Map.empty, genUUID, genUUID, 1L, false, instant, genIri, instant, genIri) // format: on } "An archive cache" should { "write and read an Archive" in { val resId = Id(randomProject().ref, genIri) val resource1 = Resource(genIri, randomProject(), None, None, originalSource = true, None) val file1 = File(genIri, randomProject(), None, None, None) val archive = Archive(resId, instant, Anonymous, Set(resource1, file1)) val _ = cache.put(archive).value.some cache.get(archive.resId).value.some shouldEqual archive } "read a non existing resource" in { val resId = Id(randomProject().ref, genIri) cache.get(resId).value.ioValue shouldEqual None } "read after timeout" in { val resId = Id(randomProject().ref, genIri) val set = Set[ResourceDescription](Resource(genIri, randomProject(), None, None, originalSource = true, None)) val archive = Archive(resId, instant, Anonymous, set) val _ = cache.put(archive).value.some val time = System.currentTimeMillis() cache.get(resId).value.some shouldEqual archive eventually { cache.get(resId).value.ioValue shouldEqual None } val diff = System.currentTimeMillis() - time diff should be > config.kg.archives.cacheInvalidateAfter.toMillis diff should be < config.kg.archives.cacheInvalidateAfter.toMillis + 300 } } }
Example 87
Source File: TagSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resources 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 io.circe.Json import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TagSpec extends AnyWordSpecLike with Matchers with TestHelper with Randomness with EitherValues { abstract private class Ctx { val id = Id(ProjectRef(genUUID), genIri) def jsonTag(rev: Long, value: String): Json = Json.obj("@id" -> id.value.asString.asJson, "tag" -> value.asJson, "rev" -> rev.asJson) } "A Tag" should { "be converted to tag case class correctly" in new Ctx { val rev = genInt().toLong val tag = genString() Tag(id, jsonTag(rev, tag)).rightValue shouldEqual Tag(rev, tag) } "reject when rev is missing" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "tag" -> genString().asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'rev' field does not have the right format.") } "reject when rev is not a number" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "tag" -> genString().asJson, "rev" -> genString().asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'rev' field does not have the right format.") } "reject when tag is missing" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "rev" -> genInt().toLong.asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'tag' field does not have the right format.") } "reject when tag is empty" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "rev" -> genInt().toLong.asJson, "tag" -> "".asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'tag' field does not have the right format.") } "reject when tag is not a string" in new Ctx { val json = Json.obj("@id" -> id.value.asString.asJson, "rev" -> genInt().toLong.asJson, "tag" -> genInt().asJson) Tag(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'tag' field does not have the right format.") } } }
Example 88
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 89
Source File: FileAttributesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resources import akka.http.scaladsl.model.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.FileAttributes import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.storage.client.types.FileAttributes.{Digest => StorageDigest} import ch.epfl.bluebrain.nexus.storage.client.types.{FileAttributes => StorageFileAttributes} import io.circe.Json import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class FileAttributesSpec extends AnyWordSpecLike with Matchers with TestHelper with Randomness with EitherValues { abstract private class Ctx { val id = Id(ProjectRef(genUUID), genIri) def jsonFileAttr( digest: StorageDigest, mediaType: String, location: String, bytes: Long, tpe: String = "UpdateFileAttributes" ): Json = Json.obj( "@id" -> id.value.asString.asJson, "@type" -> tpe.asJson, "digest" -> Json.obj("value" -> digest.value.asJson, "algorithm" -> digest.algorithm.asJson), "mediaType" -> mediaType.asJson, "location" -> location.asJson, "bytes" -> bytes.asJson ) val digest = StorageDigest("SHA-256", genString()) } "A storage FileAttributes" should { "be converted to file attributes case class correctly" in new Ctx { val expected = StorageFileAttributes("http://example.com", 10L, digest, `application/json`) val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L) FileAttributes(id, json).rightValue shouldEqual expected } "reject when algorithm digest is missing" in new Ctx { val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L).removeKeys("digest") FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'algorithm' field does not have the right format.") } "reject when digest value is empty" in new Ctx { override val digest = StorageDigest("SHA-256", "") val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'value' field does not have the right format.") } "reject when algorithm is empty" in new Ctx { override val digest = StorageDigest("", genString()) val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'algorithm' field does not have the right format.") } "reject when algorithm is invalid" in new Ctx { override val digest = StorageDigest(genString(), genString()) val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'algorithm' field does not have the right format.") } "reject when @type is missing" in new Ctx { val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L).removeKeys("@type") FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'@type' field does not have the right format.") } "reject when @type is invalid" in new Ctx { val json = jsonFileAttr(digest, "application/json", "http://example.com", 10L, genIri.asString) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'@type' field does not have the right format.") } "reject when mediaType is invalid" in new Ctx { val json = jsonFileAttr(digest, "wrong", "http://example.com", 10L) FileAttributes(id, json).leftValue shouldEqual InvalidResourceFormat(id.ref, "'mediaType' field does not have the right format.") } } }
Example 90
Source File: RefSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.resources import ch.epfl.bluebrain.nexus.kg.resources.Ref._ import cats.syntax.show._ import ch.epfl.bluebrain.nexus.commons.test.EitherValues import ch.epfl.bluebrain.nexus.rdf.Iri.{AbsoluteIri, Urn} import ch.epfl.bluebrain.nexus.rdf.implicits._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class RefSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues { "A Ref" should { "be constructed from an AbsoluteIri" in { val list = List[(AbsoluteIri, Ref)]( url"http://ex.com?rev=1&other=value" -> Revision(url"http://ex.com?other=value", 1L), url"http://ex.com?rev=1" -> Revision(url"http://ex.com", 1L), url"http://ex.com?tag=this&other=value" -> Ref.Tag(url"http://ex.com?other=value", "this"), url"http://ex.com?rev=1&tag=this&other=value" -> Revision(url"http://ex.com?other=value", 1L), url"http://ex.com?other=value" -> Latest(url"http://ex.com?other=value"), url"http://ex.com#fragment" -> Latest(url"http://ex.com#fragment"), Urn("urn:ex:a/b/c").rightValue -> Latest(Urn("urn:ex:a/b/c").rightValue), Urn("urn:ex:a/b/c?=rev=1").rightValue -> Revision(Urn("urn:ex:a/b/c").rightValue, 1L), Urn("urn:ex:a?=tag=this&other=value").rightValue -> Ref.Tag(Urn("urn:ex:a?=other=value").rightValue, "this") ) forAll(list) { case (iri, ref) => Ref(iri) shouldEqual ref } } "print properly" in { (Latest(url"http://ex.com#fragment"): Ref).show shouldEqual url"http://ex.com#fragment".show (Revision( url"http://ex.com?other=value", 1L ): Ref).show shouldEqual url"http://ex.com?other=value".show + s" @ rev: '1'" (Ref.Tag( url"http://ex.com?other=value", "this" ): Ref).show shouldEqual url"http://ex.com?other=value".show + s" @ tag: 'this'" } } }
Example 91
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 92
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 93
Source File: TaggingAdapterSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.persistence import java.time.{Clock, Instant, ZoneId} import akka.persistence.journal.Tagged import cats.syntax.show._ import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.config.Schemas._ import ch.epfl.bluebrain.nexus.kg.persistence.TaggingAdapterSpec.Other import ch.epfl.bluebrain.nexus.kg.resources.Event._ import ch.epfl.bluebrain.nexus.kg.resources.{Id, OrganizationRef, Ref} import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv import io.circe.Json import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TaggingAdapterSpec extends AnyWordSpecLike with Matchers with Inspectors with TestHelper { "A TaggingAdapter" should { val clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) def genJson(): Json = Json.obj("key" -> Json.fromString(genString())) val adapter = new TaggingAdapter() val orgRef = OrganizationRef(genUUID) val id = Id(ProjectRef(genUUID), nxv.projects.value) val mapping = Map( Set( s"type=${nxv.Schema.value.show}", s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event" ) -> Created( id, orgRef, Ref(shaclSchemaUri), Set(nxv.Schema.value, nxv.Resource.value), genJson(), clock.instant(), Anonymous ), Set( s"type=${nxv.Resolver.value.show}", s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event" ) -> Updated(id, orgRef, 1L, Set(nxv.Resource.value, nxv.Resolver.value), genJson(), clock.instant(), Anonymous), Set(s"type=${nxv.Resource.value.show}", s"project=${id.parent.id}", s"org=${orgRef.show}", "event") -> Deprecated(id, orgRef, 1L, Set(nxv.Resource.value), clock.instant(), Anonymous), Set(s"project=${id.parent.id}", s"org=${orgRef.show}", "event") -> TagAdded(id, orgRef, 2L, 1L, "tag", clock.instant(), Anonymous) ) "set the appropriate tags" in { forAll(mapping.toList) { case (tags, ev) => adapter.toJournal(ev) shouldEqual Tagged(ev, tags) } } "return an empty manifest" in { adapter.manifest(Other(genString())) shouldEqual "" } } } object TaggingAdapterSpec { final private[persistence] case class Other(value: String) }
Example 94
Source File: QueryBuilderSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.search import ch.epfl.bluebrain.nexus.commons.test.Resources import ch.epfl.bluebrain.nexus.kg.routes.SearchParams import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class QueryBuilderSpec extends AnyWordSpecLike with Matchers with Resources { "QueryBuilder" should { val schema: AbsoluteIri = url"http://nexus.example.com/testSchema" "build query with deprecation" in { val expected = jsonContentOf("/search/query-deprecation.json") QueryBuilder.queryFor(SearchParams(deprecated = Some(true))) shouldEqual expected } "build empty query" in { val expected = jsonContentOf("/search/query.json") QueryBuilder.queryFor(SearchParams()) shouldEqual expected } "build query with schema and deprecation" in { val expected = jsonContentOf("/search/query-schema-deprecation.json") QueryBuilder.queryFor(SearchParams(deprecated = Some(true), schema = Some(schema))) shouldEqual expected } "build query with schema, deprecation and several types" in { val expected = jsonContentOf("/search/query-schema-deprecation-types.json") val params = SearchParams(deprecated = Some(true), schema = Some(schema), types = List(nxv.File.value, nxv.View.value)) QueryBuilder.queryFor(params) shouldEqual expected } "build query with schema" in { val expected = jsonContentOf("/search/query-schema.json") QueryBuilder.queryFor(SearchParams(schema = Some(schema))) shouldEqual expected } "build query with schema, rev, createdBy and full text search" in { val expected = jsonContentOf("/search/query-schema-rev-createdBy-q.json") val params = SearchParams( schema = Some(schema), rev = Some(1), createdBy = Some(url"http://nexus.example.com/user"), q = Some("some text") ) QueryBuilder.queryFor(params) shouldEqual expected } } }
Example 95
Source File: QueryResultEncoderSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.search import java.time.Instant import java.util.regex.Pattern.quote import akka.http.scaladsl.model.Uri import akka.http.scaladsl.model.Uri.Query import ch.epfl.bluebrain.nexus.commons.circe.syntax._ import ch.epfl.bluebrain.nexus.commons.search.QueryResult.{ScoredQueryResult, UnscoredQueryResult} import ch.epfl.bluebrain.nexus.commons.search.QueryResults import ch.epfl.bluebrain.nexus.commons.search.QueryResults.{ScoredQueryResults, UnscoredQueryResults} import ch.epfl.bluebrain.nexus.commons.test.{Randomness, Resources} import ch.epfl.bluebrain.nexus.kg.search.QueryResultEncoder._ import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.service.config.ServiceConfig import ch.epfl.bluebrain.nexus.service.config.ServiceConfig.HttpConfig import io.circe.Json import io.circe.syntax._ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class QueryResultEncoderSpec extends AnyWordSpecLike with Matchers with Resources with Randomness { implicit val orderedKeys = ServiceConfig.orderedKeys val org = genString() val proj = genString() val schema = genString() val now = Instant.now() implicit val http = HttpConfig("", 0, "v1", "http://nexus.com") implicit val uri = Uri(s"http://nexus.com/resources/$org/$proj/$schema?type=someType&from=10&size=10") val before = now.minusSeconds(60) "QueryResultsEncoder" should { def json(id: AbsoluteIri, createdAt: Instant): Json = jsonContentOf( "/resources/es-metadata.json", Map( quote("{id}") -> id.asString, quote("{org}") -> org, quote("{proj}") -> proj, quote("{schema}") -> schema, quote("{instant}") -> createdAt.toString ) ) deepMerge Json.obj("_original_source" -> Json.fromString(Json.obj("k" -> Json.fromInt(1)).noSpaces)) "encode ScoredQueryResults" in { val results: QueryResults[Json] = ScoredQueryResults[Json]( 3, 0.3f, List( ScoredQueryResult(0.3f, json(url"http://nexus.com/result1", before)), ScoredQueryResult(0.2f, json(url"http://nexus.com/result2", before)), ScoredQueryResult(0.1f, json(url"http://nexus.com/result3", now)) ), sort(now) ) results.asJson.sortKeys shouldEqual jsonContentOf( "/search/scored-query-results.json", Map( quote("{org}") -> org, quote("{proj}") -> proj, quote("{schema}") -> schema, quote("{before}") -> before.toString, quote("{lastElementCreated}") -> now.toString, quote("{after}") -> after(now) ) ) } "encode UnscoredQueryResults" in { val results: QueryResults[Json] = UnscoredQueryResults[Json]( 3, List( UnscoredQueryResult(json(url"http://nexus.com/result1", before)), UnscoredQueryResult(json(url"http://nexus.com/result2", before)), UnscoredQueryResult(json(url"http://nexus.com/result3", now)) ), sort(now) ) results.asJson.sortKeys shouldEqual jsonContentOf( "/search/unscored-query-results.json", Map( quote("{org}") -> org, quote("{proj}") -> proj, quote("{schema}") -> schema, quote("{before}") -> before.toString, quote("{lastElementCreated}") -> now.toString, quote("{after}") -> after(now) ) ) } } private def sort(instant: Instant): Option[String] = Some(Json.arr(Json.fromString(instant.toString)).noSpaces) private def after(instant: Instant): String = Query("after" -> List(Json.fromString(instant.toString)).asJson.noSpaces).toString() }
Example 96
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 97
Source File: RemoteDiskStorageOperationsSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.storage import akka.http.scaladsl.model.ContentTypes._ import akka.http.scaladsl.model.Uri import cats.effect.IO import ch.epfl.bluebrain.nexus.commons.test.io.IOEitherValues import ch.epfl.bluebrain.nexus.commons.test.{ActorSystemFixture, Resources} import ch.epfl.bluebrain.nexus.iam.auth.AccessToken import ch.epfl.bluebrain.nexus.iam.client.types.AuthToken import ch.epfl.bluebrain.nexus.iam.types.Permission import ch.epfl.bluebrain.nexus.kg.TestHelper import ch.epfl.bluebrain.nexus.kg.resources.file.File.{Digest, FileAttributes, FileDescription} import ch.epfl.bluebrain.nexus.kg.resources.Id import ch.epfl.bluebrain.nexus.kg.resources.ProjectIdentifier.ProjectRef import ch.epfl.bluebrain.nexus.kg.storage.Storage.RemoteDiskStorage import ch.epfl.bluebrain.nexus.storage.client.StorageClient import ch.epfl.bluebrain.nexus.storage.client.types.FileAttributes.{Digest => StorageDigest} import ch.epfl.bluebrain.nexus.storage.client.types.{FileAttributes => StorageFileAttributes} import org.mockito.{IdiomaticMockito, Mockito} import org.scalatest.BeforeAndAfter import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class RemoteDiskStorageOperationsSpec extends ActorSystemFixture("RemoteDiskStorageOperationsSpec") with AnyWordSpecLike with Matchers with BeforeAndAfter with IdiomaticMockito with IOEitherValues with Resources with TestHelper { private val endpoint = "http://nexus.example.com/v1" // TODO: Remove when migrating ADMIN client implicit private def oldTokenConversion(implicit token: Option[AccessToken]): Option[AuthToken] = token.map(t => AuthToken(t.value)) sealed trait Ctx { val cred = genString() implicit val token: Option[AccessToken] = Some(AccessToken(cred)) val path = Uri.Path(s"${genString()}/${genString()}") // format: off val storage = RemoteDiskStorage(ProjectRef(genUUID), genIri, 1L, false, false, "SHA-256", endpoint, Some(cred), genString(), Permission.unsafe(genString()), Permission.unsafe(genString()), 1024L) val attributes = FileAttributes(s"$endpoint/${storage.folder}/$path", path, s"${genString()}.json", `application/json`, 12L, Digest("SHA-256", genString())) // format: on } private val client = mock[StorageClient[IO]] before { Mockito.reset(client) } "RemoteDiskStorageOperations" should { "verify when storage exists" in new Ctx { client.exists(storage.folder) shouldReturn IO(true) val verify = new RemoteDiskStorageOperations.Verify[IO](storage, client) verify.apply.accepted } "verify when storage does not exists" in new Ctx { client.exists(storage.folder) shouldReturn IO(false) val verify = new RemoteDiskStorageOperations.Verify[IO](storage, client) verify.apply .rejected[ String ] shouldEqual s"Folder '${storage.folder}' does not exists on the endpoint '${storage.endpoint}'" } "fetch file" in new Ctx { val source = genSource client.getFile(storage.folder, path) shouldReturn IO(source) val fetch = new RemoteDiskStorageOperations.Fetch[IO](storage, client) val resultSource = fetch.apply(attributes).ioValue consume(resultSource) shouldEqual consume(source) } "link file" in new Ctx { val id = Id(storage.ref, genIri) val sourcePath = Uri.Path(s"${genString()}/${genString()}") val destRelativePath = Uri.Path(mangle(storage.ref, attributes.uuid, attributes.filename)) client.moveFile(storage.folder, sourcePath, destRelativePath) shouldReturn IO( StorageFileAttributes( attributes.location, attributes.bytes, StorageDigest(attributes.digest.algorithm, attributes.digest.value), attributes.mediaType ) ) val link = new RemoteDiskStorageOperations.Link[IO](storage, client) link .apply(id, FileDescription(attributes.uuid, attributes.filename, Some(attributes.mediaType)), sourcePath) .ioValue shouldEqual attributes.copy(path = destRelativePath) } } }
Example 98
Source File: CryptoSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.storage import java.util.Base64 import ch.epfl.bluebrain.nexus.commons.test.Randomness import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class CryptoSpec extends AnyWordSpecLike with Matchers with Randomness { private val accessKey = "AKIAIOSFODNN7EXAMPLE" private val secretKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" "The Crypto object" should { "always generate the same key for a given password" in { val key = Crypto.deriveKey("changeme", "salt") Base64.getEncoder.encodeToString(key.getEncoded) shouldEqual "FB4G2MHn/q6PXqpNkE1F5wBG7Ndsd9FtyeLcNQL0G40=" key.getAlgorithm shouldEqual "AES" key.getFormat shouldEqual "RAW" } "encode and decode secrets" in { val key = Crypto.deriveKey(genString(32), genString(16)) Crypto.decrypt(key, Crypto.encrypt(key, accessKey)) shouldEqual accessKey Crypto.decrypt(key, Crypto.encrypt(key, secretKey)) shouldEqual secretKey } } }
Example 99
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 100
Source File: KryoSerializerInitSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.kg.serializers import java.nio.file.Paths import akka.actor.ActorSystem import akka.serialization.SerializationExtension import akka.testkit.TestKit import ch.epfl.bluebrain.nexus.kg.TestHelper import io.altoo.akka.serialization.kryo.KryoSerializer import org.scalatest.TryValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class KryoSerializerInitSpec extends TestKit(ActorSystem("KryoSerializerInitSpec")) with AnyWordSpecLike with Matchers with TryValues with TestHelper { private val serialization = SerializationExtension(system) "A Path Kryo serialization" should { "succeed" in { val path = Paths.get("resources/application.conf") // Find the Serializer for it val serializer = serialization.findSerializerFor(path) serializer.getClass.equals(classOf[KryoSerializer]) shouldEqual true // Check serialization/deserialization val serialized = serialization.serialize(path) serialized.isSuccess shouldEqual true val deserialized = serialization.deserialize(serialized.get, path.getClass) deserialized.isSuccess shouldEqual true deserialized.success.value shouldEqual path } } }
Example 101
Source File: IdentitiesRoutesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.routes import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.iam.acls.Acls import ch.epfl.bluebrain.nexus.iam.auth.{AccessToken, TokenRejection} import ch.epfl.bluebrain.nexus.iam.realms._ import ch.epfl.bluebrain.nexus.iam.testsyntax._ import ch.epfl.bluebrain.nexus.iam.types.Caller import ch.epfl.bluebrain.nexus.iam.types.IamError.InvalidAccessToken import ch.epfl.bluebrain.nexus.iam.types.Identity.{Anonymous, Authenticated, User} import ch.epfl.bluebrain.nexus.service.config.Settings import ch.epfl.bluebrain.nexus.service.marshallers.instances._ import ch.epfl.bluebrain.nexus.service.routes.Routes import ch.epfl.bluebrain.nexus.util.Resources import com.typesafe.config.{Config, ConfigFactory} import io.circe.Json import monix.eval.Task import org.mockito.matchers.MacroBasedMatchers import org.mockito.{IdiomaticMockito, Mockito} import org.scalatest.BeforeAndAfter import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ //noinspection TypeAnnotation class IdentitiesRoutesSpec extends AnyWordSpecLike with Matchers with ScalatestRouteTest with BeforeAndAfter with MacroBasedMatchers with Resources with ScalaFutures with IdiomaticMockito { implicit override def patienceConfig: PatienceConfig = PatienceConfig(3.seconds, 100.milliseconds) override def testConfig: Config = ConfigFactory.load("test.conf") private val config = Settings(system).serviceConfig implicit private val http = config.http private val realms: Realms[Task] = mock[Realms[Task]] private val acls: Acls[Task] = mock[Acls[Task]] before { Mockito.reset(realms, acls) } "The IdentitiesRoutes" should { val routes = Routes.wrap(new IdentitiesRoutes(acls, realms).routes) "return forbidden" in { val err = InvalidAccessToken(TokenRejection.InvalidAccessToken) realms.caller(any[AccessToken]) shouldReturn Task.raiseError(err) Get("/identities").addCredentials(OAuth2BearerToken("token")) ~> routes ~> check { status shouldEqual StatusCodes.Unauthorized } } "return anonymous" in { realms.caller(any[AccessToken]) shouldReturn Task.pure(Caller.anonymous) Get("/identities") ~> routes ~> check { status shouldEqual StatusCodes.OK responseAs[Json].sort shouldEqual jsonContentOf("/identities/anonymous.json") } } "return all identities" in { val user = User("theuser", "therealm") val auth = Authenticated("therealm") val caller = Caller(user, Set(user, Anonymous, auth)) realms.caller(any[AccessToken]) shouldReturn Task.pure(caller) Get("/identities").addCredentials(OAuth2BearerToken("token")) ~> routes ~> check { status shouldEqual StatusCodes.OK responseAs[Json].sort shouldEqual jsonContentOf("/identities/identities.json") } } } }
Example 102
Source File: RealmDirectivesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.directives import java.net.URLEncoder import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.commons.http.JsonLdCirceSupport._ import ch.epfl.bluebrain.nexus.iam.directives.RealmDirectives._ import ch.epfl.bluebrain.nexus.iam.routes.SearchParams import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.util.EitherValues import io.circe.generic.auto._ import org.mockito.IdiomaticMockito import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class RealmDirectivesSpec extends AnyWordSpecLike with ScalatestRouteTest with Matchers with ScalaFutures with EitherValues with IdiomaticMockito { private def route: Route = { (get & searchParams) { params => complete(params) } } private def encode(s: AbsoluteIri) = URLEncoder.encode(s.asString, "UTF-8") "Realm directives" should { "return query params" in { val createdBy: AbsoluteIri = url"http://example.com/created" val updatedBy: AbsoluteIri = url"http://example.com/updated" val tpe: AbsoluteIri = url"http://example.com/tpe" val tpe2: AbsoluteIri = url"http://example.com/tpe2" Get( s"/?rev=2&deprecated=true&type=${encode(tpe)}&type=${encode(tpe2)}&createdBy=${encode(createdBy)}&updatedBy=${encode(updatedBy)}" ) ~> route ~> check { responseAs[SearchParams] shouldEqual SearchParams( Some(true), Some(2L), Some(createdBy), Some(updatedBy), Set(tpe, tpe2) ) } } } }
Example 103
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 104
Source File: LabelSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import ch.epfl.bluebrain.nexus.rdf.Iri.{Path, Url} import ch.epfl.bluebrain.nexus.util.{EitherValues, Randomness} import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class LabelSpec extends AnyWordSpecLike with Matchers with Randomness with Inspectors with EitherValues { "A Label" should { "be constructed correctly from alphanumeric chars" in { forAll(1 to 32) { length => val string = genString(length, Vector.range('a', 'z') ++ Vector.range('0', '9')) Label.unsafe(string).value shouldEqual string Label(string).rightValue.value shouldEqual string } } "fail to construct for illegal formats" in { val cases = List("", " ", "a ", " a", "a-", "_") forAll(cases) { string => intercept[IllegalArgumentException](Label.unsafe(string)) Label(string).leftValue shouldEqual s"Label '$string' does not match pattern '${Label.regex.regex}'" } } "return its path representation" in { Label.unsafe("abc").toPath shouldEqual Path("/abc").rightValue } "return an iri representation" in { forAll(List("http://localhost", "http://localhost/")) { str => val base = Url(str).rightValue val label = Label.unsafe("abc") label.toIri(base) shouldEqual Url("http://localhost/abc").rightValue } } } }
Example 105
Source File: IdentitySpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import ch.epfl.bluebrain.nexus.util.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.types.Identity.{Anonymous, Authenticated, Group, Subject, User} import ch.epfl.bluebrain.nexus.service.config.ServiceConfig.HttpConfig import io.circe.syntax._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class IdentitySpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with Resources { "An Identity" should { val user = User("mysubject", "myrealm") val group = Group("mygroup", "myrealm") val authenticated = Authenticated("myrealm") implicit val http: HttpConfig = HttpConfig("some", 8080, "v1", "http://nexus.example.com") "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 106
Source File: GrantTypeSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import ch.epfl.bluebrain.nexus.util.EitherValues import ch.epfl.bluebrain.nexus.iam.types.GrantType._ import io.circe.{Decoder, Encoder, Json} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.Inspectors class GrantTypeSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues { "A GrantType" when { "using Camel encoders" should { import GrantType.Camel._ val map = Map( AuthorizationCode -> "authorizationCode", Implicit -> "implicit", Password -> "password", ClientCredentials -> "clientCredentials", DeviceCode -> "deviceCode", RefreshToken -> "refreshToken" ) "be encoded properly" in { val encoder = implicitly[Encoder[GrantType]] forAll(map.toList) { case (gt, expected) => encoder(gt) shouldEqual Json.fromString(expected) } } "be decoded properly" in { val decoder = implicitly[Decoder[GrantType]] forAll(map.toList) { case (expected, gt) => decoder.decodeJson(Json.fromString(gt)).rightValue shouldEqual expected } } "fail to decode for unknown string" in { val decoder = implicitly[Decoder[GrantType]] decoder.decodeJson(Json.fromString("incorrect")).leftValue } } "using Snake encoders" should { import GrantType.Snake._ val map = Map( AuthorizationCode -> "authorization_code", Implicit -> "implicit", Password -> "password", ClientCredentials -> "client_credentials", DeviceCode -> "device_code", RefreshToken -> "refresh_token" ) "be encoded properly" in { val encoder = implicitly[Encoder[GrantType]] forAll(map.toList) { case (gt, expected) => encoder(gt) shouldEqual Json.fromString(expected) } } "be decoded properly" in { val decoder = implicitly[Decoder[GrantType]] forAll(map.toList) { case (expected, gtString) => decoder.decodeJson(Json.fromString(gtString)).rightValue shouldEqual expected } } "fail to decode for unknown string" in { val decoder = implicitly[Decoder[GrantType]] decoder.decodeJson(Json.fromString("incorrect")).leftValue } } } }
Example 107
Source File: PermissionSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import ch.epfl.bluebrain.nexus.util.Randomness import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.util.Random class PermissionSpec extends AnyWordSpecLike with Matchers with Randomness { "A Permission" should { "be constructed correctly for valid strings" in { for (_ <- 1 to 100) { val valid = genValid Permission(valid) shouldEqual Some(Permission.unsafe(valid)) } } "fail to construct for illegal strings" in { Permission("") shouldEqual None Permission("1") shouldEqual None Permission("1abd") shouldEqual None Permission("_abd") shouldEqual None Permission("foƶ") shouldEqual None Permission("bar*") shouldEqual None Permission(genString(33)) shouldEqual None } } private def genValid: String = { val lower = 'a' to 'z' val upper = 'A' to 'Z' val numbers = '0' to '9' val symbols = List('-', '_', ':', '\\', '/') val head = genString(1, lower ++ upper) val tail = genString(Random.nextInt(32), lower ++ upper ++ numbers ++ symbols) head + tail } }
Example 108
Source File: ResourceFSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import java.time.{Clock, Instant, ZoneId} import ch.epfl.bluebrain.nexus.util.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.testsyntax._ import ch.epfl.bluebrain.nexus.iam.types.Identity.User import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.service.config.ServiceConfig.HttpConfig import ch.epfl.bluebrain.nexus.service.config.Vocabulary.nxv import io.circe.Printer import io.circe.syntax._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike //noinspection TypeAnnotation class ResourceFSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with Resources { "A ResourceMetadata" should { val user = User("mysubject", "myrealm") val user2 = User("mysubject2", "myrealm") implicit val http = HttpConfig("some", 8080, "v1", "http://nexus.example.com") val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) val instant = clock.instant() val id = url"http://example.com/id" val printer = Printer.spaces2.copy(dropNullValues = true) "be converted to Json correctly" when { "using multiple types" in { val json = jsonContentOf("/resources/write-response.json") val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList.value, nxv.Realm.value), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } "using a single type" in { val json = jsonContentOf("/resources/write-response-singletype.json") val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList.value), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } "using no types" in { val json = jsonContentOf("/resources/write-response-notypes.json") val model = ResourceMetadata(id, 1L, Set(), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } } } }
Example 109
Source File: JWKSetSerializerSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.io import akka.actor.{ActorSystem, ExtendedActorSystem} import akka.serialization.Serialization import akka.testkit.TestKit import com.nimbusds.jose.jwk.JWKSet import com.typesafe.config.ConfigFactory import org.scalatest.TryValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class JWKSetSerializerSpec extends TestKit(ActorSystem("JWKSetSerializerSpec", ConfigFactory.load("test.conf"))) with AnyWordSpecLike with Matchers with TryValues { private val serialization = new Serialization(system.asInstanceOf[ExtendedActorSystem]) private val json = """ |{ | "keys": [ | { | "kid": "-JoF9COvvt7UhyhJMC-YlTF6piRlZgQKRQks5sPMKxw", | "kty": "RSA", | "alg": "RS256", | "use": "sig", | "n": "iEk11wBlv0I4pawBSY6ZYCLvwVslfCvjwvg5tIAg9n", | "e": "AQAB" | } | ] |} """.stripMargin private val jwks = JWKSet.parse(json) "A JWKSetSerializer" should { "serialize and deserialize" in { val bytes = serialization.serialize(jwks).success.value val obj = serialization.deserialize(bytes, classOf[JWKSet]).success.value jwks.toJSONObject shouldEqual obj.toJSONObject // JWKSet doesn't have a proper equals method } } }
Example 110
Source File: TaggingAdapterSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.io import java.time.Instant import akka.persistence.journal.Tagged import ch.epfl.bluebrain.nexus.iam.acls.AclEvent.AclDeleted import ch.epfl.bluebrain.nexus.iam.permissions.PermissionsEvent.PermissionsDeleted import ch.epfl.bluebrain.nexus.iam.realms.RealmEvent.RealmDeprecated import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.iam.types.Label import ch.epfl.bluebrain.nexus.rdf.Iri.Path import ch.epfl.bluebrain.nexus.util.EitherValues import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TaggingAdapterSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues { private val pd = PermissionsDeleted(2L, Instant.EPOCH, Anonymous) private val ad = AclDeleted(Path("/a/b/c").rightValue, 2L, Instant.EPOCH, Anonymous) private val rd = RealmDeprecated(Label.unsafe("blah"), 2L, Instant.EPOCH, Anonymous) private val data = Map[AnyRef, (String, AnyRef)]( pd -> ("permissions-event" -> Tagged(pd, Set("permissions", "event"))), ad -> ("acl-event" -> Tagged(ad, Set("acl", "event"))), rd -> ("realm-event" -> Tagged(rd, Set("realm", "event"))), "a" -> ("" -> "a") ) "A TaggingAdapter" should { val adapter = new TaggingAdapter "return the correct manifests" in { forAll(data.toList) { case (event, (manifest, _)) => adapter.manifest(event) shouldEqual manifest } } "return the correct transformed event" in { forAll(data.toList) { case (event, (_, transformed)) => adapter.toJournal(event) shouldEqual transformed } } } }
Example 111
Source File: ActorSystemFixture.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.util import akka.actor.ActorSystem import akka.cluster.Cluster import akka.testkit._ import com.typesafe.config.{Config, ConfigFactory} import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.PatienceConfiguration import org.scalatest.concurrent.ScalaFutures._ import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.Promise import scala.concurrent.duration._ class ActorSystemFixture(name: String, startCluster: Boolean = false, configs: Vector[Config] = Vector.empty) extends TestKit( ActorSystem( name, (configs :+ ConfigFactory.load("test.conf")) .foldLeft(ConfigFactory.empty()) { case (c, e) => c withFallback e } .resolve() ) ) with AnyWordSpecLike with PatienceConfiguration with BeforeAndAfterAll { implicit override def patienceConfig: PatienceConfig = PatienceConfig(3.seconds.dilated, 100.milliseconds.dilated) override protected def beforeAll(): Unit = { super.beforeAll() if (startCluster) { val promise = Promise[Unit] Cluster(system).registerOnMemberUp(promise.success(())) Cluster(system).join(Cluster(system).selfAddress) promise.future.futureValue } } override protected def afterAll(): Unit = { TestKit.shutdownActorSystem(system) super.afterAll() } }
Example 112
Source File: StorageClientConfigSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.storage.client import ch.epfl.bluebrain.nexus.commons.test.EitherValues import ch.epfl.bluebrain.nexus.rdf.Iri.Urn import ch.epfl.bluebrain.nexus.rdf.implicits._ import ch.epfl.bluebrain.nexus.storage.client.config.StorageClientConfig import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class StorageClientConfigSpec extends AnyWordSpecLike with Matchers with EitherValues { "A StorageClientConfig" should { "build from Url" in { StorageClientConfig(url"http://example.com/subresource/v1") shouldEqual StorageClientConfig(url"http://example.com/subresource", "v1") StorageClientConfig(url"http://example.com/v1") shouldEqual StorageClientConfig(url"http://example.com", "v1") } "build from Urn" in { StorageClientConfig(Urn("urn:ietf:rfc:2648/subresource/v1").rightValue) shouldEqual (StorageClientConfig(Urn("urn:ietf:rfc:2648/subresource").rightValue, "v1")) } } }
Example 113
Source File: DecodingErrorSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.rdf import ch.epfl.bluebrain.nexus.rdf.CursorOp.{Down, Top} import ch.epfl.bluebrain.nexus.rdf.Vocabulary.rdf import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class DecodingErrorSpec extends AnyWordSpecLike with Matchers { "A DecodingError" should { "return the provided message" in { DecodingError("message", Nil).getMessage shouldEqual "message" DecodingError( "message", Top :: Down(rdf.first) :: Nil ).getMessage shouldEqual "message: Top,Down(http://www.w3.org/1999/02/22-rdf-syntax-ns#first)" } } }
Example 114
Source File: InfluxPointSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.cli.clients import java.time.Instant import java.util.regex.Pattern.quote import cats.effect.IO import cats.implicits._ import ch.epfl.bluebrain.nexus.cli.config.influx.TypeConfig import ch.epfl.bluebrain.nexus.cli.utils.{Resources, TimeTransformation} import fs2._ import fs2.text._ import org.http4s.EntityEncoder import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class InfluxPointSpec extends AnyWordSpecLike with Matchers with Resources with Inspectors with TimeTransformation { private def writeToString[A](a: A)(implicit W: EntityEncoder[IO, A]): String = Stream .emit(W.toEntity(a)) .covary[IO] .flatMap(_.body) .through(utf8Decode) .foldMonoid .compile .last .map(_.getOrElse("")) .unsafeRunSync "An InfluxPoint" should { val created = Instant.now() val updated = created.plusSeconds(5) "be created from SparqlResults" in { val sparqlResults = jsonContentOf( "/templates/sparql-results-influx.json", Map( quote("{created}") -> created.toString, quote("{updated}") -> updated.toString, quote("{bytes}") -> 1234.toString, quote("{project}") -> "myorg/myproject" ) ).as[SparqlResults].getOrElse(throw new IllegalArgumentException) val typeConfig = TypeConfig("https://neuroshapes.org/Subject", "", "datastats", Set("bytes"), "updated") val expected = InfluxPoint( "datastats", Map("created" -> created.toString, "project" -> "myorg/myproject", "deprecated" -> "false"), Map("bytes" -> "1234"), Some(updated) ) InfluxPoint.fromSparqlResults(sparqlResults, typeConfig) shouldEqual List(expected) } "converted to string" in { val point = InfluxPoint( "m1", Map("created" -> created.toString, "project" -> "org/proj", "deprecated" -> "false"), Map("bytes" -> "1234"), Some(updated) ) val pointNoTag = InfluxPoint( "m2", Map.empty, Map("bytes" -> "2345"), Some(updated) ) val list = List( point -> s"m1,created=${created.toString},project=org/proj,deprecated=false bytes=1234 ${toNano(updated)}", pointNoTag -> s"m2 bytes=2345 ${toNano(updated)}" ) forAll(list) { case (point, str) => writeToString(point) shouldEqual str } } } }
Example 115
Source File: SparqlResultsSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.cli.clients import ch.epfl.bluebrain.nexus.cli.clients.SparqlResults.{Binding, Bindings, Head} import ch.epfl.bluebrain.nexus.cli.utils.Resources import org.http4s.Uri import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class SparqlResultsSpec extends AnyWordSpecLike with Matchers with Resources { "A Sparql Json result" should { val json = jsonContentOf("/templates/sparql-results.json") val askJson = jsonContentOf("/templates/sparql-results-ask.json") val constrainedBy = Map( "s" -> Binding("uri", "https://example.com/subject"), "p" -> Binding("uri", "https://bluebrain.github.io/nexus/vocabulary/constrainedBy"), "o" -> Binding("uri", "https://bluebrain.github.io/nexus/schemas/resolver.json") ) val deprecated = Map( "s" -> Binding("uri", "https://example.com/subject"), "p" -> Binding("uri", "https://bluebrain.github.io/nexus/vocabulary/deprecated"), "o" -> Binding(`type` = "literal", value = "false", datatype = Some("http://www.w3.org/2001/XMLSchema#boolean")) ) val label = Map( "s" -> Binding("uri", "https://example.com/subject"), "p" -> Binding("uri", "https://bluebrain.github.io/nexus/vocabulary/label"), "o" -> Binding(`type` = "literal", value = "myText") ) val head = Head(List("s", "p", "o")) val qr = SparqlResults(head, Bindings(constrainedBy, deprecated, label)) val qrAsk = SparqlResults(Head(), Bindings(), Some(true)) "be decoded" in { json.as[SparqlResults] shouldEqual Right(qr) askJson.as[SparqlResults] shouldEqual Right(qrAsk) } "add head" in { val link = Uri.unsafeFromString("http://www.w3.org/TR/rdf-sparql-XMLres/example.rq") head ++ Head(List("s", "p", "name"), Some(List(link))) shouldEqual Head(List("s", "p", "o", "name"), Some(List(link))) } "add binding" in { (Bindings(constrainedBy) ++ Bindings(deprecated) ++ Bindings(label)) shouldEqual qr.results } } }
Example 116
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 117
Source File: StorageDirectivesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.storage.routes import java.util.regex.Pattern.quote import akka.http.scaladsl.model.{StatusCodes, Uri} import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.storage.JsonLdCirceSupport._ import ch.epfl.bluebrain.nexus.storage.routes.Routes.exceptionHandler import ch.epfl.bluebrain.nexus.storage.routes.StorageDirectives._ import ch.epfl.bluebrain.nexus.storage.utils.Resources import io.circe.Json import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class StorageDirectivesSpec extends AnyWordSpecLike with Matchers with ScalatestRouteTest with Inspectors with Resources { "the storage directives" when { def pathInvalidJson(path: Uri.Path): Json = jsonContentOf( "/error.json", Map( quote("{type}") -> "PathInvalid", quote( "{reason}" ) -> s"The provided location inside the bucket 'name' with the relative path '$path' is invalid." ) ) "dealing with file path extraction" should { val route = handleExceptions(exceptionHandler) { (extractRelativeFilePath("name") & get) { path => complete(s"$path") } } "reject when path contains 2 slashes" in { Get("///") ~> route ~> check { status shouldEqual StatusCodes.BadRequest responseAs[Json] shouldEqual pathInvalidJson(Uri.Path.Empty) } } "reject when path does not end with a segment" in { Get("/some/path/") ~> route ~> check { status shouldEqual StatusCodes.BadRequest responseAs[Json] shouldEqual pathInvalidJson(Uri.Path("some/path/")) } } "return path" in { Get("/some/path/file.txt") ~> route ~> check { responseAs[String] shouldEqual "some/path/file.txt" } } } "dealing with path validation" should { def route(path: Uri.Path) = handleExceptions(exceptionHandler) { (validatePath("name", path) & get) { complete(s"$path") } } "reject when some of the segments is . or .." in { val paths = List(Uri.Path("/./other/file.txt"), Uri.Path("/some/../file.txt")) forAll(paths) { path => Get(path.toString()) ~> route(path) ~> check { status shouldEqual StatusCodes.BadRequest responseAs[Json] shouldEqual pathInvalidJson(path) } } } "pass" in { Get("/some/path") ~> route(Uri.Path("/some/path")) ~> check { handled shouldEqual true } } } } }
Example 118
Source File: AppInfoRoutesSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.storage.routes import java.util.regex.Pattern.quote import akka.http.scaladsl.model.StatusCodes._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.storage.config.{AppConfig, Settings} import ch.epfl.bluebrain.nexus.storage.routes.instances._ import ch.epfl.bluebrain.nexus.storage.utils.Resources import ch.epfl.bluebrain.nexus.storage.{AkkaSource, IamIdentitiesClient, Storages} import io.circe.Json import monix.eval.Task import org.mockito.IdiomaticMockito import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class AppInfoRoutesSpec extends AnyWordSpecLike with Matchers with ScalatestRouteTest with IdiomaticMockito with Resources { "the app info routes" should { implicit val config: AppConfig = Settings(system).appConfig implicit val iamIdentities: IamIdentitiesClient[Task] = mock[IamIdentitiesClient[Task]] val route: Route = Routes(mock[Storages[Task, AkkaSource]]) "return application information" in { Get("/") ~> route ~> check { status shouldEqual OK responseAs[Json] shouldEqual jsonContentOf("/app-info.json", Map(quote("{version}") -> config.description.version)) } } } }
Example 119
Source File: AttributesComputationSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.storage.attributes import java.nio.charset.StandardCharsets import java.nio.file.{Files, Paths} import akka.actor.ActorSystem import akka.http.scaladsl.model.ContentTypes.`text/plain(UTF-8)` import akka.testkit.TestKit import cats.effect.IO import ch.epfl.bluebrain.nexus.storage.File.{Digest, FileAttributes} import ch.epfl.bluebrain.nexus.storage.StorageError.InternalError import ch.epfl.bluebrain.nexus.storage.utils.IOValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.ExecutionContextExecutor class AttributesComputationSpec extends TestKit(ActorSystem("AttributesComputationSpec")) with AnyWordSpecLike with Matchers with IOValues { implicit private val ec: ExecutionContextExecutor = system.dispatcher private trait Ctx { val path = Files.createTempFile("storage-test", ".txt") val (text, digest) = "something" -> "3fc9b689459d738f8c88a3a48aa9e33542016b7a4052e001aaa536fca74813cb" } "Attributes computation computation" should { val computation = AttributesComputation.akkaAttributes[IO] val alg = "SHA-256" "succeed" in new Ctx { Files.write(path, text.getBytes(StandardCharsets.UTF_8)) computation(path, alg).ioValue shouldEqual FileAttributes( s"file://$path", Files.size(path), Digest(alg, digest), `text/plain(UTF-8)` ) Files.deleteIfExists(path) } "fail when algorithm is wrong" in new Ctx { Files.write(path, text.getBytes(StandardCharsets.UTF_8)) computation(path, "wrong-alg").failed[InternalError] } "fail when file does not exists" in new Ctx { computation(Paths.get("/tmp/non/existing"), alg).failed[InternalError] } } }
Example 120
Source File: StringProcessLoggerSpec.scala From nexus with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.storage import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.sys.process._ class StringProcessLoggerSpec extends AnyWordSpecLike with Matchers { "A StringProcessLogger" should { "log stdout" in { val cmd = List("echo", "-n", "Hello", "world!") val process = Process(cmd) val logger = StringProcessLogger(cmd) val exitCode = process ! logger exitCode shouldEqual 0 logger.toString shouldEqual "Hello world!" } "log stderr" in { val cmd = List("cat", "/") val process = Process(cmd) val logger = StringProcessLogger(cmd) val exitCode = process ! logger exitCode should not be 0 logger.toString shouldEqual "cat: /: Is a directory" } } }
Example 121
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 122
Source File: PubSubThrottlerSpec.scala From akka-persistence-cassandra with Apache License 2.0 | 5 votes |
package akka.persistence.cassandra.journal import scala.concurrent.duration.DurationInt import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import akka.actor.{ ActorSystem, Props } import akka.testkit.{ TestKit, TestProbe } class PubSubThrottlerSpec extends TestKit(ActorSystem("CassandraConfigCheckerSpec")) with AnyWordSpecLike with Matchers { "PubSubThrottler" should { "eat up duplicate messages that arrive within the same [interval] window" in { val delegate = TestProbe() val throttler = system.actorOf(Props(new PubSubThrottler(delegate.ref, 5.seconds))) throttler ! "hello" throttler ! "hello" throttler ! "hello" delegate.within(2.seconds) { delegate.expectMsg("hello") } // Only first "hello" makes it through during the first interval. delegate.expectNoMessage(2.seconds) // Eventually, the interval will roll over and forward ONE further hello. delegate.expectMsg(10.seconds, "hello") delegate.expectNoMessage(2.seconds) throttler ! "hello" delegate.within(2.seconds) { delegate.expectMsg("hello") } } "allow differing messages to pass through within the same [interval] window" in { val delegate = TestProbe() val throttler = system.actorOf(Props(new PubSubThrottler(delegate.ref, 5.seconds))) throttler ! "hello" throttler ! "world" delegate.within(2.seconds) { delegate.expectMsg("hello") delegate.expectMsg("world") } } } }
Example 123
Source File: ReconnectSpec.scala From akka-persistence-cassandra with Apache License 2.0 | 5 votes |
package akka.persistence.cassandra import java.io.File import akka.actor.{ ActorSystem, Props } import akka.persistence.cassandra.CassandraLifecycle.AwaitPersistenceInit import akka.persistence.cassandra.testkit.CassandraLauncher import akka.testkit.{ ImplicitSender, SocketUtil, TestKit } import com.typesafe.config.ConfigFactory import org.scalatest.Suite import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike object ReconnectSpec { val freePort = SocketUtil.temporaryLocalPort() val config = ConfigFactory.parseString(s""" datastax-java-driver { basic.load-balancing-policy.local-datacenter = "datacenter1" // Will fail without this setting advanced.reconnect-on-init = true basic.contact-points = ["127.0.0.1:$freePort"] } """).withFallback(CassandraLifecycle.config) } // not using Cassandra Spec class ReconnectSpec extends TestKit(ActorSystem("ReconnectSpec", ReconnectSpec.config)) with Suite with ImplicitSender with AnyWordSpecLike with Matchers with ScalaFutures { "Reconnecting" must { "start with system off" in { val pa = system.actorOf(Props(new AwaitPersistenceInit("pid", "", ""))) pa ! "hello" expectNoMessage() CassandraLauncher.start( new File("target/ReconnectSpec"), configResource = CassandraLauncher.DefaultTestConfigResource, clean = true, port = ReconnectSpec.freePort, CassandraLauncher.classpathForResources("logback-test.xml")) try { CassandraLifecycle.awaitPersistenceInit(system) } finally { CassandraLauncher.stop() } } } }
Example 124
Source File: CassandraLauncherSpec.scala From akka-persistence-cassandra with Apache License 2.0 | 5 votes |
package akka.persistence.cassandra.testkit import java.io.File import java.net.InetSocketAddress import akka.actor.ActorSystem import akka.testkit.TestKit import com.datastax.oss.driver.api.core.CqlSession import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.matchers.should.Matchers import scala.concurrent.duration._ import org.scalatest.BeforeAndAfterAll class CassandraLauncherSpec extends TestKit(ActorSystem("CassandraLauncherSpec")) with Matchers with AnyWordSpecLike with BeforeAndAfterAll { override protected def afterAll(): Unit = { shutdown(system, verifySystemShutdown = true) CassandraLauncher.stop() super.afterAll() } private def testCassandra(): Unit = { val session = CqlSession .builder() .withLocalDatacenter("datacenter1") .addContactPoint(new InetSocketAddress("localhost", CassandraLauncher.randomPort)) .build() try session.execute("SELECT now() from system.local;").one() finally { session.close() } } "The CassandraLauncher" must { "support forking" in { val cassandraDirectory = new File("target/" + system.name) CassandraLauncher.start( cassandraDirectory, configResource = CassandraLauncher.DefaultTestConfigResource, clean = true, port = 0, CassandraLauncher.classpathForResources("logback-test.xml")) awaitAssert({ testCassandra() }, 45.seconds) CassandraLauncher.stop() an[Exception] shouldBe thrownBy(testCassandra()) } } }
Example 125
Source File: AuditSerialiserSpec.scala From play-auditing with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.audit.serialiser import java.time.Instant import play.api.libs.json.JsString import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import uk.gov.hmrc.play.audit.model.{DataCall, DataEvent, ExtendedDataEvent, MergedDataEvent} class AuditSerialiserSpec extends AnyWordSpecLike with Matchers { "AuditSerialiser" should { "serialise DataEvent" in { AuditSerialiser.serialise(DataEvent( auditSource = "myapp", auditType = "RequestReceived", eventId = "cb5ebe82-cf3c-4f15-bd92-39a6baa1f929", tags = Map("tagkey" -> "tagval"), detail = Map("detailkey" -> "detailval"), generatedAt = Instant.parse("2007-12-03T10:15:30.123Z") )).toString shouldBe """{"auditSource":"myapp","auditType":"RequestReceived","eventId":"cb5ebe82-cf3c-4f15-bd92-39a6baa1f929","tags":{"tagkey":"tagval"},"detail":{"detailkey":"detailval"},"generatedAt":"2007-12-03T10:15:30.123+0000"}""" } "serialise ExtendedDataEvent" in { AuditSerialiser.serialise(ExtendedDataEvent( auditSource = "myapp", auditType = "RequestReceived", eventId = "cb5ebe82-cf3c-4f15-bd92-39a6baa1f929", tags = Map("tagkey" -> "tagval"), detail = JsString("detail"), generatedAt = Instant.parse("2007-12-03T10:15:30.123Z") )).toString shouldBe """{"auditSource":"myapp","auditType":"RequestReceived","eventId":"cb5ebe82-cf3c-4f15-bd92-39a6baa1f929","tags":{"tagkey":"tagval"},"detail":"detail","generatedAt":"2007-12-03T10:15:30.123+0000"}""" } "serialise MergedDataEvent" in { AuditSerialiser.serialise(MergedDataEvent( auditSource = "myapp", auditType = "RequestReceived", eventId = "cb5ebe82-cf3c-4f15-bd92-39a6baa1f929", request = DataCall( tags = Map("requesttagkey" -> "requesttagval"), detail = Map("requestdetailkey" -> "requestdetailval"), generatedAt = Instant.parse("2007-12-03T10:15:30.123Z") ), response = DataCall( tags = Map("responsetagkey" -> "responsetagval"), detail = Map("responsedetailkey" -> "responsedetailval"), generatedAt = Instant.parse("2007-12-03T10:16:31.123Z") ) )).toString shouldBe """{"auditSource":"myapp","auditType":"RequestReceived","eventId":"cb5ebe82-cf3c-4f15-bd92-39a6baa1f929","request":{"tags":{"requesttagkey":"requesttagval"},"detail":{"requestdetailkey":"requestdetailval"},"generatedAt":"2007-12-03T10:15:30.123+0000"},"response":{"tags":{"responsetagkey":"responsetagval"},"detail":{"responsedetailkey":"responsedetailval"},"generatedAt":"2007-12-03T10:16:31.123+0000"}}""" } } }
Example 126
Source File: LoggingHandlerSpec.scala From play-auditing with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.audit.handler import org.mockito.Mockito._ import org.scalatest.wordspec.AnyWordSpecLike import org.scalatestplus.mockito.MockitoSugar import org.slf4j.Logger import play.api.libs.json.JsString import scala.concurrent.ExecutionContext.Implicits.global class LoggingHandlerSpec extends AnyWordSpecLike with MockitoSugar { val mockLog: Logger = mock[Logger] val loggingHandler = new LoggingHandler(mockLog) "LoggingHandler" should { "log the event" in { val expectedLogContent = """DS_EventMissed_AuditRequestFailure : audit item : "FAILED_EVENT"""" loggingHandler.sendEvent(JsString("FAILED_EVENT")) verify(mockLog).warn(expectedLogContent) } } }
Example 127
Source File: DatastreamHandlerUnitSpec.scala From play-auditing with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.audit.handler import akka.actor.ActorSystem import akka.stream.ActorMaterializer import org.scalatest.Inspectors import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import play.api.inject.DefaultApplicationLifecycle import play.api.libs.json.{JsString, JsValue} import uk.gov.hmrc.audit.HandlerResult import scala.concurrent.duration.DurationInt import scala.concurrent.{ExecutionContext, Future} import ExecutionContext.Implicits.global class DatastreamHandlerUnitSpec extends AnyWordSpecLike with Inspectors with Matchers with ScalaFutures { val datastreamHandler = new DatastreamHandler( scheme = "http", host = "localhost", port = 1234, path = "/some/path", connectTimeout = 2000.millis, requestTimeout = 2000.millis, userAgent = "the-micro-service-name", materializer = ActorMaterializer()(ActorSystem()), lifecycle = new DefaultApplicationLifecycle() ) { override def sendHttpRequest(event: JsValue)(implicit ec: ExecutionContext): Future[HttpResult] = Future.successful(HttpResult.Response(event.as[String].toInt)) } "Any Datastream response" should { "Return Success for any response code of 204" in { val result = datastreamHandler.sendEvent(JsString("204")).futureValue result shouldBe HandlerResult.Success } "Return Failure for any response code of 3XX or 401-412 or 414-499 or 5XX" in { forAll((300 to 399) ++ (401 to 412) ++ (414 to 499) ++ (500 to 599)) { code => val result = datastreamHandler.sendEvent(JsString(code.toString)).futureValue result shouldBe HandlerResult.Failure } } "Return Rejected for any response code of 400 or 413" in { forAll(Seq(400, 413)) { code => val result = datastreamHandler.sendEvent(JsString(code.toString)).futureValue result shouldBe HandlerResult.Rejected } } } }
Example 128
Source File: AuditTagsSpec.scala From play-auditing with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.audit.http.connector import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import uk.gov.hmrc.play.audit.AuditExtensions import uk.gov.hmrc.play.audit.EventKeys._ import uk.gov.hmrc.http.{HeaderCarrier, Token, UserId} import uk.gov.hmrc.http.logging._ class AuditTagsSpec extends AnyWordSpecLike with Matchers { import uk.gov.hmrc.http.HeaderNames._ import AuditExtensions._ val authorization = Authorization("authorization") val userId = UserId("userId") val token = Token("token") val forwarded = ForwardedFor("ipAdress") val sessionId = SessionId("1234567890") val requestId = RequestId("0987654321") val deviceId = "testDeviceId" val akamaiReputation = AkamaiReputation("foo") "Audit TAGS" should { "be present" in { val hc = new HeaderCarrier(Some(authorization), Some(userId), Some(token), Some(forwarded), Some(sessionId), Some(requestId), deviceID = Some(deviceId), akamaiReputation = Some(akamaiReputation)) val tags = hc.toAuditTags("theTransactionName", "/the/request/path") tags.size shouldBe 8 tags(xSessionId) shouldBe sessionId.value tags(xRequestId) shouldBe requestId.value tags(TransactionName) shouldBe "theTransactionName" tags(Path) shouldBe "/the/request/path" tags("clientIP") shouldBe "-" tags("clientPort") shouldBe "-" tags("Akamai-Reputation") shouldBe akamaiReputation.value tags("deviceID") shouldBe deviceId } "be defaulted" in { val hc = HeaderCarrier() val tags = hc.toAuditTags("defaultsWhenNothingSet", "/the/request/path") tags.size shouldBe 8 tags(xSessionId) shouldBe "-" tags(xRequestId) shouldBe "-" tags(TransactionName) shouldBe "defaultsWhenNothingSet" tags(Path) shouldBe "/the/request/path" tags("clientIP") shouldBe "-" tags("clientPort") shouldBe "-" tags("Akamai-Reputation") shouldBe "-" tags("deviceID") shouldBe "-" } "have more tags.clientIP and tags.clientPort" in { val hc = HeaderCarrier(trueClientIp = Some("192.168.1.1"), trueClientPort =Some("9999")) val tags = hc.toAuditTags("defaultsWhenNothingSet", "/the/request/path") tags("clientIP") shouldBe "192.168.1.1" tags("clientPort") shouldBe "9999" } } "Audit DETAILS" should { "be present" in { val hc = new HeaderCarrier(Some(authorization), Some(userId), Some(token), Some(forwarded), Some(sessionId), Some(requestId), deviceID = Some(deviceId)) val details = hc.toAuditDetails() details.size shouldBe 0 } "be defaulted" in { val hc = HeaderCarrier() val details = hc.toAuditDetails() details.size shouldBe 0 } "have more details only" in { val hc = HeaderCarrier(trueClientIp = Some("192.168.1.1"), trueClientPort =Some("9999")) val details = hc.toAuditDetails("more-details" -> "the details", "lots-of-details" -> "interesting info") details.size shouldBe 2 details("more-details") shouldBe "the details" details("lots-of-details") shouldBe "interesting info" } } }
Example 129
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 130
Source File: CallerDecoderSpec.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.types.Identity.{Anonymous, Authenticated, Group, User} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class CallerDecoderSpec extends AnyWordSpecLike with Matchers with Resources with EitherValues { "Caller decoder" should { "decode subject correctly" in { jsonContentOf("/identities/caller.json").as[Caller].rightValue shouldEqual Caller( User("nexus-test-user", "nexusdev"), Set( Anonymous, Authenticated("nexusdev"), Group("nexus-test-group", "nexusdev"), User("nexus-test-user", "nexusdev") ) ) } } }
Example 131
Source File: IdentitiesRoutesSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.routes import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.model.headers.OAuth2BearerToken import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.commons.test.Resources import ch.epfl.bluebrain.nexus.iam.auth.{AccessToken, TokenRejection} import ch.epfl.bluebrain.nexus.iam.config.{AppConfig, Settings} import ch.epfl.bluebrain.nexus.iam.marshallers.instances._ import ch.epfl.bluebrain.nexus.iam.realms._ import ch.epfl.bluebrain.nexus.iam.testsyntax._ import ch.epfl.bluebrain.nexus.iam.types.Caller import ch.epfl.bluebrain.nexus.iam.types.IamError.InvalidAccessToken import ch.epfl.bluebrain.nexus.iam.types.Identity.{Anonymous, Authenticated, User} import com.typesafe.config.{Config, ConfigFactory} import io.circe.Json import monix.eval.Task import org.mockito.matchers.MacroBasedMatchers import org.mockito.{IdiomaticMockito, Mockito} import org.scalatest.BeforeAndAfter import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.duration._ //noinspection TypeAnnotation class IdentitiesRoutesSpec extends AnyWordSpecLike with Matchers with ScalatestRouteTest with BeforeAndAfter with MacroBasedMatchers with Resources with ScalaFutures with IdiomaticMockito { override implicit def patienceConfig: PatienceConfig = PatienceConfig(3.seconds, 100.milliseconds) override def testConfig: Config = ConfigFactory.load("test.conf") private val appConfig: AppConfig = Settings(system).appConfig private implicit val http = appConfig.http private val realms: Realms[Task] = mock[Realms[Task]] before { Mockito.reset(realms) } "The IdentitiesRoutes" should { val routes = Routes.wrap(new IdentitiesRoutes(realms).routes) "return forbidden" in { val err = InvalidAccessToken(TokenRejection.InvalidAccessToken) realms.caller(any[AccessToken]) shouldReturn Task.raiseError(err) Get("/identities").addCredentials(OAuth2BearerToken("token")) ~> routes ~> check { status shouldEqual StatusCodes.Unauthorized } } "return anonymous" in { realms.caller(any[AccessToken]) shouldReturn Task.pure(Caller.anonymous) Get("/identities") ~> routes ~> check { status shouldEqual StatusCodes.OK responseAs[Json].sort shouldEqual jsonContentOf("/identities/anonymous.json") } } "return all identities" in { val user = User("theuser", "therealm") val auth = Authenticated("therealm") val caller = Caller(user, Set(user, Anonymous, auth)) realms.caller(any[AccessToken]) shouldReturn Task.pure(caller) Get("/identities").addCredentials(OAuth2BearerToken("token")) ~> routes ~> check { status shouldEqual StatusCodes.OK responseAs[Json].sort shouldEqual jsonContentOf("/identities/identities.json") } } } }
Example 132
Source File: RealmDirectivesSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.directives import java.net.URLEncoder import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.http.scaladsl.testkit.ScalatestRouteTest import ch.epfl.bluebrain.nexus.commons.http.JsonLdCirceSupport._ import ch.epfl.bluebrain.nexus.commons.test.EitherValues import ch.epfl.bluebrain.nexus.iam.directives.RealmDirectives._ import ch.epfl.bluebrain.nexus.iam.routes.SearchParams import ch.epfl.bluebrain.nexus.rdf.Iri.AbsoluteIri import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.generic.auto._ import org.mockito.IdiomaticMockito import org.scalatest.concurrent.ScalaFutures import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class RealmDirectivesSpec extends AnyWordSpecLike with ScalatestRouteTest with Matchers with ScalaFutures with EitherValues with IdiomaticMockito { private def route: Route = { (get & searchParams) { params => complete(params) } } private def encode(s: AbsoluteIri) = URLEncoder.encode(s.asString, "UTF-8") "Realm directives" should { "return query params" in { val createdBy: AbsoluteIri = url"http://example.com/created" val updatedBy: AbsoluteIri = url"http://example.com/updated" val tpe: AbsoluteIri = url"http://example.com/tpe" val tpe2: AbsoluteIri = url"http://example.com/tpe2" Get( s"/?rev=2&deprecated=true&type=${encode(tpe)}&type=${encode(tpe2)}&createdBy=${encode(createdBy)}&updatedBy=${encode(updatedBy)}" ) ~> route ~> check { responseAs[SearchParams] shouldEqual SearchParams( Some(true), Some(2L), Some(createdBy), Some(updatedBy), Set(tpe, tpe2) ) } } } }
Example 133
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 134
Source File: LabelSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Randomness} import ch.epfl.bluebrain.nexus.rdf.Iri.{Path, Url} import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class LabelSpec extends AnyWordSpecLike with Matchers with Randomness with Inspectors with EitherValues { "A Label" should { "be constructed correctly from alphanumeric chars" in { forAll(1 to 32) { length => val string = genString(length, Vector.range('a', 'z') ++ Vector.range('0', '9')) Label.unsafe(string).value shouldEqual string Label(string).rightValue.value shouldEqual string } } "fail to construct for illegal formats" in { val cases = List("", " ", "a ", " a", "a-", "_") forAll(cases) { string => intercept[IllegalArgumentException](Label.unsafe(string)) Label(string).leftValue shouldEqual s"Label '$string' does not match pattern '${Label.regex.regex}'" } } "return its path representation" in { Label.unsafe("abc").toPath shouldEqual Path("/abc").rightValue } "return an iri representation" in { forAll(List("http://localhost", "http://localhost/")) { str => val base = Url(str).rightValue val label = Label.unsafe("abc") label.toIri(base) shouldEqual Url("http://localhost/abc").rightValue } } } }
Example 135
Source File: IdentitySpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types 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.{Anonymous, Authenticated, Group, Subject, User} import io.circe.syntax._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class IdentitySpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with Resources { "An Identity" should { val user = User("mysubject", "myrealm") val group = Group("mygroup", "myrealm") val authenticated = Authenticated("myrealm") implicit val http: HttpConfig = HttpConfig("some", 8080, "v1", "http://nexus.example.com") "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 136
Source File: GrantTypeSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import ch.epfl.bluebrain.nexus.commons.test.EitherValues import ch.epfl.bluebrain.nexus.iam.types.GrantType._ import io.circe.{Decoder, Encoder, Json} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import org.scalatest.Inspectors class GrantTypeSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues { "A GrantType" when { "using Camel encoders" should { import GrantType.Camel._ val map = Map( AuthorizationCode -> "authorizationCode", Implicit -> "implicit", Password -> "password", ClientCredentials -> "clientCredentials", DeviceCode -> "deviceCode", RefreshToken -> "refreshToken" ) "be encoded properly" in { val encoder = implicitly[Encoder[GrantType]] forAll(map.toList) { case (gt, expected) => encoder(gt) shouldEqual Json.fromString(expected) } } "be decoded properly" in { val decoder = implicitly[Decoder[GrantType]] forAll(map.toList) { case (expected, gt) => decoder.decodeJson(Json.fromString(gt)).rightValue shouldEqual expected } } "fail to decode for unknown string" in { val decoder = implicitly[Decoder[GrantType]] decoder.decodeJson(Json.fromString("incorrect")).leftValue } } "using Snake encoders" should { import GrantType.Snake._ val map = Map( AuthorizationCode -> "authorization_code", Implicit -> "implicit", Password -> "password", ClientCredentials -> "client_credentials", DeviceCode -> "device_code", RefreshToken -> "refresh_token" ) "be encoded properly" in { val encoder = implicitly[Encoder[GrantType]] forAll(map.toList) { case (gt, expected) => encoder(gt) shouldEqual Json.fromString(expected) } } "be decoded properly" in { val decoder = implicitly[Decoder[GrantType]] forAll(map.toList) { case (expected, gtString) => decoder.decodeJson(Json.fromString(gtString)).rightValue shouldEqual expected } } "fail to decode for unknown string" in { val decoder = implicitly[Decoder[GrantType]] decoder.decodeJson(Json.fromString("incorrect")).leftValue } } } }
Example 137
Source File: PermissionSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import ch.epfl.bluebrain.nexus.commons.test.Randomness import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import scala.util.Random class PermissionSpec extends AnyWordSpecLike with Matchers with Randomness { "A Permission" should { "be constructed correctly for valid strings" in { for (_ <- 1 to 100) { val valid = genValid Permission(valid) shouldEqual Some(Permission.unsafe(valid)) } } "fail to construct for illegal strings" in { Permission("") shouldEqual None Permission("1") shouldEqual None Permission("1abd") shouldEqual None Permission("_abd") shouldEqual None Permission("foƶ") shouldEqual None Permission("bar*") shouldEqual None Permission(genString(33)) shouldEqual None } } private def genValid: String = { val lower = 'a' to 'z' val upper = 'A' to 'Z' val numbers = '0' to '9' val symbols = List('-', '_', ':', '\\', '/') val head = genString(1, lower ++ upper) val tail = genString(Random.nextInt(32), lower ++ upper ++ numbers ++ symbols) head + tail } }
Example 138
Source File: ResourceFSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.types import java.time.{Clock, Instant, ZoneId} import ch.epfl.bluebrain.nexus.commons.test.{EitherValues, Resources} import ch.epfl.bluebrain.nexus.iam.config.AppConfig.HttpConfig import ch.epfl.bluebrain.nexus.iam.config.Vocabulary._ import ch.epfl.bluebrain.nexus.iam.testsyntax._ import ch.epfl.bluebrain.nexus.iam.types.Identity.User import ch.epfl.bluebrain.nexus.rdf.implicits._ import io.circe.Printer import io.circe.syntax._ import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike //noinspection TypeAnnotation class ResourceFSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues with Resources { "A ResourceMetadata" should { val user = User("mysubject", "myrealm") val user2 = User("mysubject2", "myrealm") implicit val http = HttpConfig("some", 8080, "v1", "http://nexus.example.com") val clock: Clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault()) val instant = clock.instant() val id = url"http://example.com/id" val printer = Printer.spaces2.copy(dropNullValues = true) "be converted to Json correctly" when { "using multiple types" in { val json = jsonContentOf("/resources/write-response.json") val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList, nxv.Realm), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } "using a single type" in { val json = jsonContentOf("/resources/write-response-singletype.json") val model = ResourceMetadata(id, 1L, Set(nxv.AccessControlList), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } "using no types" in { val json = jsonContentOf("/resources/write-response-notypes.json") val model = ResourceMetadata(id, 1L, Set(), instant, user, instant, user2) model.asJson.sort.printWith(printer) shouldEqual json.printWith(printer) } } } }
Example 139
Source File: JWKSetSerializerSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.io import akka.actor.{ActorSystem, ExtendedActorSystem} import akka.serialization.Serialization import akka.testkit.TestKit import com.nimbusds.jose.jwk.JWKSet import com.typesafe.config.ConfigFactory import org.scalatest.TryValues import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class JWKSetSerializerSpec extends TestKit(ActorSystem("JWKSetSerializerSpec", ConfigFactory.load("akka-test.conf"))) with AnyWordSpecLike with Matchers with TryValues { private val serialization = new Serialization(system.asInstanceOf[ExtendedActorSystem]) private val json = """ |{ | "keys": [ | { | "kid": "-JoF9COvvt7UhyhJMC-YlTF6piRlZgQKRQks5sPMKxw", | "kty": "RSA", | "alg": "RS256", | "use": "sig", | "n": "iEk11wBlv0I4pawBSY6ZYCLvwVslfCvjwvg5tIAg9n", | "e": "AQAB" | } | ] |} """.stripMargin private val jwks = JWKSet.parse(json) "A JWKSetSerializer" should { "serialize and deserialize" in { val bytes = serialization.serialize(jwks).success.value val obj = serialization.deserialize(bytes, classOf[JWKSet]).success.value jwks.toJSONObject shouldEqual obj.toJSONObject // JWKSet doesn't have a proper equals method } } }
Example 140
Source File: TaggingAdapterSpec.scala From nexus-iam with Apache License 2.0 | 5 votes |
package ch.epfl.bluebrain.nexus.iam.io import java.time.Instant import akka.persistence.journal.Tagged import ch.epfl.bluebrain.nexus.commons.test.EitherValues import ch.epfl.bluebrain.nexus.iam.acls.AclEvent.AclDeleted import ch.epfl.bluebrain.nexus.iam.permissions.PermissionsEvent.PermissionsDeleted import ch.epfl.bluebrain.nexus.iam.realms.RealmEvent.RealmDeprecated import ch.epfl.bluebrain.nexus.iam.types.Identity.Anonymous import ch.epfl.bluebrain.nexus.iam.types.Label import ch.epfl.bluebrain.nexus.rdf.Iri.Path import org.scalatest.Inspectors import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike class TaggingAdapterSpec extends AnyWordSpecLike with Matchers with Inspectors with EitherValues { private val pd = PermissionsDeleted(2L, Instant.EPOCH, Anonymous) private val ad = AclDeleted(Path("/a/b/c").rightValue, 2L, Instant.EPOCH, Anonymous) private val rd = RealmDeprecated(Label.unsafe("blah"), 2L, Instant.EPOCH, Anonymous) private val data = Map[AnyRef, (String, AnyRef)]( pd -> ("permissions-event" -> Tagged(pd, Set("permissions", "event"))), ad -> ("acl-event" -> Tagged(ad, Set("acl", "event"))), rd -> ("realm-event" -> Tagged(rd, Set("realm", "event"))), "a" -> ("" -> "a") ) "A TaggingAdapter" should { val adapter = new TaggingAdapter "return the correct manifests" in { forAll(data.toList) { case (event, (manifest, _)) => adapter.manifest(event) shouldEqual manifest } } "return the correct transformed event" in { forAll(data.toList) { case (event, (_, transformed)) => adapter.toJournal(event) shouldEqual transformed } } } }
Example 141
Source File: EmailNotifierIntegrationSpec.scala From vinyldns with Apache License 2.0 | 5 votes |
package vinyldns.api.notifier.email import com.typesafe.config.{Config, ConfigFactory} import vinyldns.core.notifier._ import vinyldns.api.MySqlApiIntegrationSpec import vinyldns.mysql.MySqlIntegrationSpec import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import vinyldns.core.domain.batch._ import vinyldns.core.domain.record.RecordType import vinyldns.core.domain.record.AData import org.joda.time.DateTime import vinyldns.core.TestMembershipData._ import java.nio.file.{Files, Path, Paths} import cats.effect.{IO, Resource} import scala.collection.JavaConverters._ import org.scalatest.BeforeAndAfterEach import cats.implicits._ class EmailNotifierIntegrationSpec extends MySqlApiIntegrationSpec with MySqlIntegrationSpec with Matchers with AnyWordSpecLike with BeforeAndAfterEach { import vinyldns.api.domain.DomainValidations._ val emailConfig: Config = ConfigFactory.load().getConfig("vinyldns.email.settings") val targetDirectory = Paths.get("../../docker/email") override def beforeEach: Unit = deleteEmailFiles(targetDirectory).unsafeRunSync() override def afterEach: Unit = deleteEmailFiles(targetDirectory).unsafeRunSync() "Email Notifier" should { "send an email" in { val batchChange = BatchChange( okUser.id, okUser.userName, None, DateTime.now, List( SingleAddChange( Some("some-zone-id"), Some("zone-name"), Some("record-name"), "a" * HOST_MAX_LENGTH, RecordType.A, 300, AData("1.1.1.1"), SingleChangeStatus.Complete, None, None, None ) ), approvalStatus = BatchChangeApprovalStatus.AutoApproved ) val program = for { _ <- userRepository.save(okUser) notifier <- new EmailNotifierProvider() .load(NotifierConfig("", emailConfig), userRepository) _ <- notifier.notify(Notification(batchChange)) emailFiles <- retrieveEmailFiles(targetDirectory) } yield emailFiles val files = program.unsafeRunSync() files.length should be(1) } } def deleteEmailFiles(path: Path): IO[Unit] = for { files <- retrieveEmailFiles(path) _ <- files.traverse { file => IO(Files.delete(file)) } } yield () def retrieveEmailFiles(path: Path): IO[List[Path]] = Resource.fromAutoCloseable(IO(Files.newDirectoryStream(path, "*.eml"))).use { s => IO { s.iterator.asScala.toList } } }
Example 142
Source File: BatchChangeRepositoryIntegrationSpec.scala From vinyldns with Apache License 2.0 | 5 votes |
package vinyldns.api.domain.batch import org.joda.time.DateTime import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import vinyldns.api.MySqlApiIntegrationSpec import vinyldns.core.TestMembershipData._ import vinyldns.core.domain.batch.{ BatchChange, BatchChangeApprovalStatus, SingleAddChange, SingleChangeStatus } import vinyldns.core.domain.record.{AData, RecordType} import vinyldns.mysql.MySqlIntegrationSpec class BatchChangeRepositoryIntegrationSpec extends MySqlApiIntegrationSpec with MySqlIntegrationSpec with Matchers with AnyWordSpecLike { import vinyldns.api.domain.DomainValidations._ "MySqlBatchChangeRepository" should { "successfully save single change with max-length input name" in { val batchChange = BatchChange( okUser.id, okUser.userName, None, DateTime.now, List( SingleAddChange( Some("some-zone-id"), Some("zone-name"), Some("record-name"), "a" * HOST_MAX_LENGTH, RecordType.A, 300, AData("1.1.1.1"), SingleChangeStatus.Pending, None, None, None ) ), approvalStatus = BatchChangeApprovalStatus.AutoApproved ) val f = for { saveBatchChangeResult <- batchChangeRepository.save(batchChange) getSingleChangesResult <- batchChangeRepository.getSingleChanges( batchChange.changes.map(_.id) ) } yield (saveBatchChangeResult, getSingleChangesResult) val (saveResponse, singleChanges) = f.unsafeRunSync() saveResponse shouldBe batchChange singleChanges.foreach(_.inputName.length shouldBe HOST_MAX_LENGTH) } } }
Example 143
Source File: ActorSystemFixture.scala From nexus with Apache License 2.0 | 4 votes |
package ch.epfl.bluebrain.nexus.sourcing.projections import akka.actor.ActorSystem import akka.cluster.Cluster import akka.testkit._ import com.typesafe.config.ConfigFactory import org.scalatest.BeforeAndAfterAll import org.scalatest.concurrent.PatienceConfiguration import org.scalatest.concurrent.ScalaFutures._ import org.scalatest.wordspec.AnyWordSpecLike import scala.concurrent.Promise import scala.concurrent.duration._ class ActorSystemFixture(name: String, startCluster: Boolean = false) extends TestKit(ActorSystem(name, ConfigFactory.load("service-test.conf"))) with AnyWordSpecLike with PatienceConfiguration with BeforeAndAfterAll { implicit override def patienceConfig: PatienceConfig = PatienceConfig(3.seconds.dilated, 100.milliseconds.dilated) override protected def beforeAll(): Unit = { super.beforeAll() if (startCluster) { val promise = Promise[Unit] Cluster(system).registerOnMemberUp(promise.success(())) Cluster(system).join(Cluster(system).selfAddress) promise.future.futureValue } } override protected def afterAll(): Unit = { TestKit.shutdownActorSystem(system) super.afterAll() } }