Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Simplify layer setup for integration-tests and reduce to two layers #2339

Merged
merged 9 commits into from Dec 19, 2022
2 changes: 1 addition & 1 deletion webapi/src/it/scala/org/knora/webapi/CoreSpec.scala
Expand Up @@ -43,7 +43,7 @@ abstract class CoreSpec
* The effect layers from which the App is built.
* Can be overriden in specs that need other implementations.
*/
lazy val effectLayers = core.LayersTest.defaultLayersTestWithoutSipi
lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers()

/**
* `Bootstrap` will ensure that everything is instantiated when the Runtime is created
Expand Down
2 changes: 1 addition & 1 deletion webapi/src/it/scala/org/knora/webapi/E2ESpec.scala
Expand Up @@ -67,7 +67,7 @@ abstract class E2ESpec
* The effect layers from which the App is built.
* Can be overriden in specs that need other implementations.
*/
lazy val effectLayers = core.LayersTest.defaultLayersTestWithoutSipi
lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers()

/**
* `Bootstrap` will ensure that everything is instantiated when the Runtime is created
Expand Down
Expand Up @@ -59,7 +59,7 @@ abstract class ITKnoraLiveSpec
* The effect layers from which the App is built.
* Can be overriden in specs that need other implementations.
*/
lazy val effectLayers = core.LayersTest.defaultLayersTestWithSipi
lazy val effectLayers = core.LayersTest.integrationTestsWithSipiAndFusekiTestcontainers

/**
* `Bootstrap` will ensure that everything is instantiated when the Runtime is created
Expand Down
2 changes: 1 addition & 1 deletion webapi/src/it/scala/org/knora/webapi/R2RSpec.scala
Expand Up @@ -50,7 +50,7 @@ abstract class R2RSpec
* The effect layers from which the App is built.
* Can be overriden in specs that need other implementations.
*/
lazy val effectLayers = core.LayersTest.defaultLayersTestWithoutSipi(system)
lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers(Some(system))

/**
* `Bootstrap` will ensure that everything is instantiated when the Runtime is created
Expand Down
151 changes: 51 additions & 100 deletions webapi/src/it/scala/org/knora/webapi/core/LayersTest.scala
@@ -1,15 +1,21 @@
package org.knora.webapi.core

import zio.ULayer
import zio.ZLayer

import org.knora.webapi.auth.JWTService
import org.knora.webapi.config.AppConfig
import org.knora.webapi.config.AppConfigForTestContainers
import org.knora.webapi.routing.ApiRoutes
import org.knora.webapi.store.cache.CacheServiceManager
import org.knora.webapi.store.cache.api.CacheService
import org.knora.webapi.store.cache.impl.CacheServiceInMemImpl
import org.knora.webapi.store.iiif.IIIFServiceManager
import org.knora.webapi.store.iiif.api.IIIFService
import org.knora.webapi.store.iiif.impl.IIIFServiceMockImpl
import org.knora.webapi.store.iiif.impl.IIIFServiceSipiImpl
import org.knora.webapi.store.triplestore.TriplestoreServiceManager
import org.knora.webapi.store.triplestore.api.TriplestoreService
import org.knora.webapi.store.triplestore.impl.TriplestoreServiceHttpConnectorImpl
import org.knora.webapi.store.triplestore.upgrade.RepositoryUpdater
import org.knora.webapi.testcontainers.FusekiTestContainer
Expand All @@ -24,129 +30,74 @@ object LayersTest {
type DefaultTestEnvironmentWithoutSipi = LayersLive.DspEnvironmentLive with FusekiTestContainer with TestClientService
type DefaultTestEnvironmentWithSipi = DefaultTestEnvironmentWithoutSipi with SipiTestContainer

/**
* All live layers with both Fuseki and Sipi testcontainers
*/
val defaultLayersTestWithSipi =
ZLayer.make[DefaultTestEnvironmentWithSipi](
ActorSystem.layer,
type CommonR0 = ActorSystem with IIIFService with AppConfig
type CommonR = ApiRoutes
Comment on lines +33 to +34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the CommonR0 and CommonR stand for? R is for "Routes", I guess? What about the 0? Could you write this explicitly? It would be clearer, I guess.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names for the parameters come from the signature of the function which uses it:
def makeSome[R0, R]. I am open for suggestions for a better naming.

with AppRouter
with CacheService
with CacheServiceManager
with HttpServer
with IIIFServiceManager
with RepositoryUpdater
with State
with TestClientService
with TriplestoreService
with TriplestoreServiceManager

private val commonLayersForAllIntegrationTests =
ZLayer.makeSome[CommonR0, CommonR](
ApiRoutes.layer,
AppConfigForTestContainers.testcontainers,
AppRouter.layer,
CacheServiceManager.layer,
CacheServiceInMemImpl.layer,
CacheServiceManager.layer,
HttpServer.layer,
IIIFServiceManager.layer,
IIIFServiceSipiImpl.layer, // alternative: MockSipiImpl.layer
JWTService.layer,
RepositoryUpdater.layer,
State.layer,
TriplestoreServiceManager.layer,
TestClientService.layer,
TriplestoreServiceHttpConnectorImpl.layer,
// testcontainers
SipiTestContainer.layer,
FusekiTestContainer.layer,
// Test services
TestClientService.layer
TriplestoreServiceManager.layer
)

/**
* All live layers - with ActorSystem - but without Sipi testcontainer
*/
val defaultLayersTestWithoutSipi =
ZLayer.make[DefaultTestEnvironmentWithoutSipi](
ActorSystem.layer,
ApiRoutes.layer,
AppConfigForTestContainers.fusekiOnlyTestcontainer,
AppRouter.layer,
CacheServiceManager.layer,
CacheServiceInMemImpl.layer,
HttpServer.layer,
IIIFServiceManager.layer,
IIIFServiceSipiImpl.layer, // alternative: MockSipiImpl.layer
JWTService.layer,
RepositoryUpdater.layer,
State.layer,
TriplestoreServiceManager.layer,
TriplestoreServiceHttpConnectorImpl.layer,
// testcontainers
private val fusekiAndSipiTestcontainers =
ZLayer.make[FusekiTestContainer with SipiTestContainer with AppConfig with JWTService with IIIFService](
AppConfigForTestContainers.testcontainers,
FusekiTestContainer.layer,
// Test services
TestClientService.layer
SipiTestContainer.layer,
IIIFServiceSipiImpl.layer,
JWTService.layer
)

/**
* All live layers - with ActorSystemTest - but without Sipi testcontainer
*/
def defaultLayersTestWithoutSipi(system: akka.actor.ActorSystem) =
ZLayer.make[DefaultTestEnvironmentWithoutSipi](
ActorSystemTest.layer(system),
ApiRoutes.layer,
private val fusekiTestcontainers =
ZLayer.make[FusekiTestContainer with AppConfig with JWTService with IIIFService](
AppConfigForTestContainers.fusekiOnlyTestcontainer,
AppRouter.layer,
CacheServiceManager.layer,
CacheServiceInMemImpl.layer,
HttpServer.layer,
IIIFServiceManager.layer,
IIIFServiceSipiImpl.layer, // alternative: MockSipiImpl.layer
JWTService.layer,
RepositoryUpdater.layer,
State.layer,
TriplestoreServiceManager.layer,
TriplestoreServiceHttpConnectorImpl.layer,
// testcontainers
FusekiTestContainer.layer,
// Test services
TestClientService.layer
IIIFServiceMockImpl.layer,
JWTService.layer
)

/**
* All live layers - with ActorSystemTest - but with the mocked IIIF layer
* Provides a layer for integration tests which depend on Fuseki as Testcontainers.
* Sipi/IIIFService will be mocked with the [[IIIFServiceMockImpl.l]]
* @param system An optional [[akka.actor.ActorSystem]] for use with Akka's [[akka.testkit.TestKit]]
* @return a [[ULayer]] with the [[DefaultTestEnvironmentWithoutSipi]]
*/
val defaultLayersTestWithMockedSipi =
def integrationTestsWithFusekiTestcontainers(
system: Option[akka.actor.ActorSystem] = None
): ULayer[DefaultTestEnvironmentWithoutSipi] =
ZLayer.make[DefaultTestEnvironmentWithoutSipi](
ActorSystem.layer,
ApiRoutes.layer,
AppConfigForTestContainers.fusekiOnlyTestcontainer,
AppRouter.layer,
CacheServiceManager.layer,
CacheServiceInMemImpl.layer,
HttpServer.layer,
IIIFServiceManager.layer,
IIIFServiceMockImpl.layer,
JWTService.layer,
RepositoryUpdater.layer,
State.layer,
TriplestoreServiceManager.layer,
TriplestoreServiceHttpConnectorImpl.layer,
// testcontainers
FusekiTestContainer.layer,
// Test services
TestClientService.layer
commonLayersForAllIntegrationTests,
fusekiTestcontainers,
system.map(ActorSystemTest.layer).getOrElse(ActorSystem.layer)
)

/**
* All live layers - with ActorSystemTest - but with the mocked IIIF layer
* Provides a layer for integration tests which depend on Fuseki and Sipi as Testcontainers.
* @return a [[ULayer]] with the [[DefaultTestEnvironmentWithSipi]]
*/
def defaultLayersTestWithMockedSipi(system: akka.actor.ActorSystem) =
ZLayer.make[DefaultTestEnvironmentWithoutSipi](
ActorSystemTest.layer(system),
ApiRoutes.layer,
AppConfigForTestContainers.fusekiOnlyTestcontainer,
AppRouter.layer,
CacheServiceManager.layer,
CacheServiceInMemImpl.layer,
HttpServer.layer,
IIIFServiceManager.layer,
IIIFServiceMockImpl.layer,
JWTService.layer,
RepositoryUpdater.layer,
State.layer,
TriplestoreServiceManager.layer,
TriplestoreServiceHttpConnectorImpl.layer,
// testcontainers
FusekiTestContainer.layer,
// Test services
TestClientService.layer
val integrationTestsWithSipiAndFusekiTestcontainers: ULayer[DefaultTestEnvironmentWithSipi] =
ZLayer.make[DefaultTestEnvironmentWithSipi](
commonLayersForAllIntegrationTests,
fusekiAndSipiTestcontainers,
ActorSystem.layer
)
}
Expand Up @@ -48,7 +48,7 @@ class SipiV1R2RSpec extends R2RSpec {

/* we need to run our app with the mocked sipi implementation */
override type Environment = core.LayersTest.DefaultTestEnvironmentWithoutSipi
override lazy val effectLayers = core.LayersTest.defaultLayersTestWithMockedSipi(system)
override lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers(Some(system))

object RequestParams {

Expand Down
Expand Up @@ -48,7 +48,7 @@ class ValuesV2R2RSpec extends R2RSpec {

/* we need to run our app with the mocked sipi implementation */
override type Environment = core.LayersTest.DefaultTestEnvironmentWithoutSipi
override lazy val effectLayers = core.LayersTest.defaultLayersTestWithMockedSipi(system)
override lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers(Some(system))

private val aThingPictureIri = "http://rdfh.ch/0001/a-thing-picture"

Expand Down
Expand Up @@ -7,13 +7,12 @@ package org.knora.webapi.responders.v1

import akka.testkit.ImplicitSender
import spray.json.JsValue

import java.util.UUID
import scala.concurrent.duration._

import dsp.errors.BadRequestException
import dsp.errors.NotFoundException
import dsp.errors.OntologyConstraintException

import org.knora.webapi._
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.OntologyConstants
Expand Down Expand Up @@ -656,7 +655,7 @@ class ResourcesResponderV1Spec extends CoreSpec with ImplicitSender {

/* we need to run our app with the mocked sipi implementation */
override type Environment = core.LayersTest.DefaultTestEnvironmentWithoutSipi
override lazy val effectLayers = core.LayersTest.defaultLayersTestWithMockedSipi
override lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers()

// The default timeout for receiving reply messages from actors.
private val timeout = 60.seconds
Expand Down
Expand Up @@ -54,7 +54,7 @@ class ValuesResponderV1Spec extends CoreSpec with ImplicitSender {

/* we need to run our app with the mocked sipi implementation */
override type Environment = core.LayersTest.DefaultTestEnvironmentWithoutSipi
override lazy val effectLayers = core.LayersTest.defaultLayersTestWithMockedSipi
override lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers()

override lazy val rdfDataObjects = List(
RdfDataObject(
Expand Down
Expand Up @@ -402,7 +402,7 @@ class ResourcesResponderV2Spec extends CoreSpec with ImplicitSender {

/* we need to run our app with the mocked sipi implementation */
override type Environment = core.LayersTest.DefaultTestEnvironmentWithoutSipi
override lazy val effectLayers = core.LayersTest.defaultLayersTestWithMockedSipi
override lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers()

override lazy val rdfDataObjects = List(
RdfDataObject(path = "test_data/all_data/incunabula-data.ttl", name = "http://www.knora.org/data/0803/incunabula"),
Expand Down
Expand Up @@ -61,7 +61,7 @@ class ValuesResponderV2Spec extends CoreSpec with ImplicitSender {

/* we need to run our app with the mocked sipi implementation */
override type Environment = core.LayersTest.DefaultTestEnvironmentWithoutSipi
override lazy val effectLayers = core.LayersTest.defaultLayersTestWithMockedSipi
override lazy val effectLayers = core.LayersTest.integrationTestsWithFusekiTestcontainers()

override lazy val rdfDataObjects = List(
RdfDataObject(
Expand Down
Expand Up @@ -20,7 +20,7 @@ trait HttpServer {
}

object HttpServer {
val layer: ZLayer[core.ActorSystem & AppConfig & ApiRoutes, Nothing, HttpServer] =
val layer: ZLayer[ActorSystem with AppConfig with ApiRoutes, Nothing, HttpServer] =
ZLayer.scoped {
for {
as <- ZIO.service[core.ActorSystem]
Expand Down