Skip to content

Commit

Permalink
refactor: Migrate ListRouteV2 to tapir (DEV-3852) (#3307)
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone authored Jul 8, 2024
1 parent 1f0d221 commit 9b3d6c7
Show file tree
Hide file tree
Showing 27 changed files with 493 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

package org.knora.webapi.config

import com.typesafe.config.ConfigFactory
import zio.*
import zio.config.*
import zio.config.typesafe.TypesafeConfigProvider

import org.knora.webapi.config.AppConfig.AppConfigurations
import org.knora.webapi.testcontainers.DspIngestTestContainer
Expand Down Expand Up @@ -64,26 +61,14 @@ object AppConfigForTestContainers {
ZIO.succeed(newConfig)
}

/**
* Reads in the application configuration using ZIO-Config. ZIO-Config is capable of loading
* the Typesafe-Config format. Reads the 'app' configuration from 'application.conf'.
*/
private val source: ConfigProvider =
TypesafeConfigProvider.fromTypesafeConfig(ConfigFactory.load().getConfig("app").resolve)

/**
* Instantiates our config class hierarchy using the data from the 'app' configuration from 'application.conf'.
*/
private val config: UIO[AppConfig] = read(AppConfig.descriptor from source).orDie

/**
* Altered AppConfig with ports from TestContainers for DSP-Ingest, Fuseki and Sipi.
*/
val testcontainers
: ZLayer[DspIngestTestContainer & FusekiTestContainer & SipiTestContainer, Nothing, AppConfigurations] = {
val appConfigLayer = ZLayer {
for {
appConfig <- config
appConfig <- AppConfig.parseConfig
fusekiContainer <- ZIO.service[FusekiTestContainer]
sipiContainer <- ZIO.service[SipiTestContainer]
dspIngestContainer <- ZIO.service[DspIngestTestContainer]
Expand All @@ -101,7 +86,7 @@ object AppConfigForTestContainers {
val fusekiOnlyTestcontainer: ZLayer[FusekiTestContainer, Nothing, AppConfigurations] = {
val appConfigLayer = ZLayer {
for {
appConfig <- config
appConfig <- AppConfig.parseConfig
fusekiContainer <- ZIO.service[FusekiTestContainer]
alteredConfig <- alterFusekiPort(appConfig, fusekiContainer)
} yield alteredConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import org.knora.webapi.slice.infrastructure.CacheManager
import org.knora.webapi.slice.infrastructure.InfrastructureModule
import org.knora.webapi.slice.infrastructure.api.ManagementEndpoints
import org.knora.webapi.slice.infrastructure.api.ManagementRoutes
import org.knora.webapi.slice.lists.api.ListsApiModule
import org.knora.webapi.slice.lists.domain.ListsService
import org.knora.webapi.slice.ontology.api.service.RestCardinalityService
import org.knora.webapi.slice.ontology.api.service.RestCardinalityServiceLive
import org.knora.webapi.slice.ontology.domain.service.CardinalityService
Expand Down Expand Up @@ -110,6 +112,7 @@ object LayersTest {
InferenceOptimizationService &
IriConverter &
IriService &
ListsApiModule.Provided &
ListsResponder &
MessageRelay &
OntologyCache &
Expand Down Expand Up @@ -146,7 +149,7 @@ object LayersTest {
ApiV2Endpoints &
HttpServer &
IIIFRequestMessageHandler &
ListsResponderV2 &
ListsService &
OntologyResponderV2 &
RepositoryUpdater &
ResourcesResponderV2 &
Expand All @@ -172,7 +175,9 @@ object LayersTest {
IriConverter.layer,
IriService.layer,
KnoraResponseRenderer.layer,
ListsApiModule.layer,
ListsResponder.layer,
ListsService.layer,
ManagementEndpoints.layer,
ManagementRoutes.layer,
MessageRelayLive.layer,
Expand Down Expand Up @@ -217,7 +222,7 @@ object LayersTest {
RepositoryUpdater.layer,
HttpServer.layer,
ApiV2Endpoints.layer,
ListsResponderV2.layer,
ListsService.layer,
IIIFRequestMessageHandlerLive.layer,
IriService.layer,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.knora.webapi.e2e.v2
import org.apache.pekko
import spray.json.JsValue
import spray.json.JsonParser
import zio.*

import java.net.URLEncoder
import java.nio.file.Paths
Expand All @@ -16,19 +17,25 @@ import scala.concurrent.ExecutionContextExecutor
import org.knora.webapi.*
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.util.rdf.RdfModel
import org.knora.webapi.routing.v2.ListsRouteV2
import org.knora.webapi.routing.UnsafeZioRun
import org.knora.webapi.slice.lists.api.ListsApiV2Routes
import org.knora.webapi.util.FileUtil

import pekko.http.javadsl.model.StatusCodes
import pekko.http.scaladsl.model.headers.Accept
import pekko.http.scaladsl.server.Directives.*

/**
* End-to-end test specification for the lists endpoint. This specification uses the Spray Testkit as documented
* here: http://spray.io/documentation/1.2.2/spray-testkit/
*/
class ListsRouteV2R2RSpec extends R2RSpec {

private val listsPath = ListsRouteV2().makeRoute
private val listsPath = UnsafeZioRun.runOrThrow(
for {
r <- ZIO.serviceWith[ListsApiV2Routes](_.routes)
} yield r.reduce(_ ~ _),
)

implicit val ec: ExecutionContextExecutor = system.dispatcher

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.knora.webapi.slice.lists

import org.apache.pekko
import org.apache.pekko.testkit.ImplicitSender
import zio.*

import org.knora.webapi.CoreSpec
import org.knora.webapi.messages.admin.responder.listsmessages
import org.knora.webapi.messages.admin.responder.listsmessages.ListADM
import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralSequenceV2
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralV2
import org.knora.webapi.messages.v2.responder.listsmessages.ListGetResponseV2
import org.knora.webapi.messages.v2.responder.listsmessages.NodeGetResponseV2
import org.knora.webapi.routing.UnsafeZioRun
import org.knora.webapi.sharedtestdata.SharedListsTestDataADM
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.slice.admin.domain.model.ListProperties.ListIri
import org.knora.webapi.slice.lists.domain.ListsService

class ListsServiceSpec extends CoreSpec with ImplicitSender {

private val listsService = ZIO.serviceWithZIO[ListsService]

override lazy val rdfDataObjects: List[RdfDataObject] = List(
RdfDataObject(
path = "test_data/project_data/anything-data.ttl",
name = "http://www.knora.org/data/0001/anything",
),
)

"The ListsService" should {
"return a list" in {
val actual = UnsafeZioRun.runOrThrow(
listsService(
_.getList(ListIri.unsafeFrom("http://rdfh.ch/lists/0001/treeList"), SharedTestDataADM.anythingUser2),
),
)
assert(
actual == ListGetResponseV2(
list = ListADM(
listinfo = SharedListsTestDataADM.treeListInfo.sorted,
children = SharedListsTestDataADM.treeListChildNodes.map(_.sorted),
),
userLang = "de",
fallbackLang = "en",
),
)
}

"return a node" in {
val actual = UnsafeZioRun.runOrThrow(
listsService(
_.getNode(ListIri.unsafeFrom("http://rdfh.ch/lists/0001/treeList11"), SharedTestDataADM.anythingUser2),
),
)
assert(
actual == NodeGetResponseV2(
node = listsmessages.ListChildNodeInfoADM(
name = Some("Tree list node 11"),
id = "http://rdfh.ch/lists/0001/treeList11",
labels = StringLiteralSequenceV2(
stringLiterals = Vector(
StringLiteralV2.from(
value = "Tree list node 11",
language = Some("en"),
),
),
),
position = 1,
hasRootNode = "http://rdfh.ch/lists/0001/treeList",
comments = StringLiteralSequenceV2.empty,
),
userLang = "de",
fallbackLang = "en",
),
)
}
}
}
16 changes: 8 additions & 8 deletions webapi/src/main/scala/org/knora/webapi/config/AppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.typesafe.config.ConfigFactory
import zio.*
import zio.config.*
import zio.config.magnolia.*
import zio.config.magnolia.DeriveConfig.given
import zio.config.typesafe.*

import java.time.Duration
Expand Down Expand Up @@ -180,15 +179,16 @@ object AppConfig {
type AppConfigurationsTest = AppConfig & DspIngestConfig & Triplestore & Features & Sipi
type AppConfigurations = AppConfigurationsTest & InstrumentationServerConfig & JwtConfig & KnoraApi

val descriptor: Config[AppConfig] = deriveConfig[AppConfig].mapKey(toKebabCase)
val parseConfig: UIO[AppConfig] = {
val descriptor = deriveConfig[AppConfig].mapKey(toKebabCase)
val source = TypesafeConfigProvider.fromTypesafeConfig(ConfigFactory.load().getConfig("app").resolve)
read(descriptor from source).orDie
}

val layer: ULayer[AppConfigurations] = {
val appConfigLayer = ZLayer {
val source = TypesafeConfigProvider.fromTypesafeConfig(ConfigFactory.load().getConfig("app").resolve)
read(descriptor from source)
.tap(c => ZIO.logInfo("Feature: ALLOW_ERASE_PROJECTS enabled").when(c.features.allowEraseProjects))
.orDie
}
val appConfigLayer = ZLayer.fromZIO(
parseConfig.tap(c => ZIO.logInfo("Feature: ALLOW_ERASE_PROJECTS enabled").when(c.features.allowEraseProjects)),
)
projectAppConfigurations(appConfigLayer).tap(_ => ZIO.logInfo(">>> AppConfig Initialized <<<"))
}

Expand Down
8 changes: 6 additions & 2 deletions webapi/src/main/scala/org/knora/webapi/core/LayersLive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import org.knora.webapi.slice.common.repo.service.PredicateObjectMapper
import org.knora.webapi.slice.infrastructure.InfrastructureModule
import org.knora.webapi.slice.infrastructure.api.ManagementEndpoints
import org.knora.webapi.slice.infrastructure.api.ManagementRoutes
import org.knora.webapi.slice.lists.api.ListsApiModule
import org.knora.webapi.slice.lists.domain.ListsService
import org.knora.webapi.slice.ontology.api.service.RestCardinalityService
import org.knora.webapi.slice.ontology.api.service.RestCardinalityServiceLive
import org.knora.webapi.slice.ontology.domain.service.CardinalityService
Expand Down Expand Up @@ -90,8 +92,9 @@ object LayersLive {
InfrastructureModule.Provided &
InstrumentationServerConfig &
IriConverter &
ListsApiModule.Provided &
ListsResponder &
ListsResponderV2 &
ListsService &
MessageRelay &
OntologyCache &
OntologyCacheHelpers &
Expand Down Expand Up @@ -147,8 +150,9 @@ object LayersLive {
IriConverter.layer,
IriService.layer,
KnoraResponseRenderer.layer,
ListsApiModule.layer,
ListsResponder.layer,
ListsResponderV2.layer,
ListsService.layer,
ManagementEndpoints.layer,
ManagementRoutes.layer,
MessageRelayLive.layer,
Expand Down
Loading

0 comments on commit 9b3d6c7

Please sign in to comment.