From 30e4f06c4588d04202d0c96c6bff353ef2895271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kleinb=C3=B6lting?= Date: Tue, 19 Mar 2024 17:58:38 +0100 Subject: [PATCH] refactor: Remove accessible makro (Scala3 perparation) (#3135) --- .../responders/v2/SearchResponderV2Spec.scala | 109 ++++++++++++++---- .../responders/v2/ValuesResponderV2Spec.scala | 11 +- .../responders/v2/SearchResponderV2.scala | 9 +- .../webapi/routing/v2/ResourcesRouteV2.scala | 17 +-- .../domain/service/OntologyRepo.scala | 2 - .../resourceinfo/ResourceInfoLayers.scala | 3 +- .../api/service/RestResourceInfoService.scala | 39 +++---- .../domain/ResourceInfoRepo.scala | 2 - .../api/service/MaintenanceServiceSpec.scala | 2 +- .../repo/service/OntologyRepoLiveSpec.scala | 30 +++-- .../api/LiveRestResourceInfoServiceSpec.scala | 69 +++++------ .../api/TriplestoreServiceInMemory.scala | 2 - 12 files changed, 171 insertions(+), 124 deletions(-) diff --git a/integration/src/test/scala/org/knora/webapi/responders/v2/SearchResponderV2Spec.scala b/integration/src/test/scala/org/knora/webapi/responders/v2/SearchResponderV2Spec.scala index 79501326e6..69d597a715 100644 --- a/integration/src/test/scala/org/knora/webapi/responders/v2/SearchResponderV2Spec.scala +++ b/integration/src/test/scala/org/knora/webapi/responders/v2/SearchResponderV2Spec.scala @@ -5,12 +5,18 @@ package org.knora.webapi.responders.v2 +import zio.ZIO + import dsp.errors.BadRequestException import org.knora.webapi.* +import org.knora.webapi.ApiV2Schema +import org.knora.webapi.SchemaRendering import org.knora.webapi.SchemaRendering.apiV2SchemaWithOption import org.knora.webapi.messages.IriConversions.* +import org.knora.webapi.messages.SmartIri import org.knora.webapi.messages.StringFormatter import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject +import org.knora.webapi.messages.util.search.ConstructQuery import org.knora.webapi.messages.v2.responder.resourcemessages.* import org.knora.webapi.messages.v2.responder.valuemessages.ReadValueV2 import org.knora.webapi.messages.v2.responder.valuemessages.StillImageFileValueContentV2 @@ -18,6 +24,8 @@ import org.knora.webapi.responders.v2.ResourcesResponseCheckerV2.compareReadReso import org.knora.webapi.routing.UnsafeZioRun import org.knora.webapi.sharedtestdata.SharedTestDataADM import org.knora.webapi.sharedtestdata.SharedTestDataADM.anonymousUser +import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri +import org.knora.webapi.slice.admin.domain.model.User import org.knora.webapi.slice.resourceinfo.domain.IriConverter import org.knora.webapi.util.ZioScalaTestUtil.assertFailsWithA @@ -41,12 +49,75 @@ class SearchResponderV2Spec extends CoreSpec { ) private val searchResponderV2SpecFullData = new SearchResponderV2SpecFullData + // accessor methods + private def fulltextSearchV2( + searchValue: IRI, + offset: Int, + limitToProject: Option[ProjectIri], + limitToResourceClass: Option[SmartIri], + limitToStandoffClass: Option[SmartIri], + returnFiles: Boolean, + schemaAndOptions: SchemaRendering, + requestingUser: User, + ) = ZIO.serviceWithZIO[SearchResponderV2]( + _.fulltextSearchV2( + searchValue, + offset, + limitToProject, + limitToResourceClass, + limitToStandoffClass, + returnFiles, + schemaAndOptions, + requestingUser, + ), + ) + + private def searchResourcesByLabelV2( + searchValue: String, + offset: Int, + limitToProject: Option[ProjectIri], + limitToResourceClass: Option[SmartIri], + targetSchema: ApiV2Schema, + requestingUser: User, + ) = ZIO.serviceWithZIO[SearchResponderV2]( + _.searchResourcesByLabelV2(searchValue, offset, limitToProject, limitToResourceClass, targetSchema, requestingUser), + ) + + private def searchResourcesByLabelCountV2( + searchValue: String, + limitToProject: Option[ProjectIri], + limitToResourceClass: Option[SmartIri], + ) = ZIO.serviceWithZIO[SearchResponderV2]( + _.searchResourcesByLabelCountV2(searchValue, limitToProject, limitToResourceClass), + ) + + private def gravsearchV2(query: ConstructQuery, schemaAndOptions: SchemaRendering, user: User) = + ZIO.serviceWithZIO[SearchResponderV2](_.gravsearchV2(query, schemaAndOptions, user)) + + private def searchResourcesByProjectAndClassV2( + projectIri: SmartIri, + resourceClass: SmartIri, + orderByProperty: Option[SmartIri], + page: Int, + schemaAndOptions: SchemaRendering, + requestingUser: User, + ) = ZIO.serviceWithZIO[SearchResponderV2]( + _.searchResourcesByProjectAndClassV2( + projectIri, + resourceClass, + orderByProperty, + page, + schemaAndOptions, + requestingUser, + ), + ) + "The search responder v2" should { "perform a fulltext search for 'Narr'" in { val result = UnsafeZioRun.runOrThrow( - SearchResponderV2.fulltextSearchV2( + fulltextSearchV2( searchValue = "Narr", offset = 0, limitToProject = None, @@ -63,7 +134,7 @@ class SearchResponderV2Spec extends CoreSpec { "perform a fulltext search for 'Dinge'" in { val result = UnsafeZioRun.runOrThrow( - SearchResponderV2.fulltextSearchV2( + fulltextSearchV2( searchValue = "Dinge", offset = 0, limitToProject = None, @@ -80,7 +151,7 @@ class SearchResponderV2Spec extends CoreSpec { "return a Bad Request error if fulltext search input is invalid" in { val result = UnsafeZioRun.run( - SearchResponderV2.fulltextSearchV2( + fulltextSearchV2( searchValue = "qin(", offset = 0, limitToProject = None, @@ -97,7 +168,7 @@ class SearchResponderV2Spec extends CoreSpec { "return files attached to full-text search results" in { val result: ReadResourcesSequenceV2 = UnsafeZioRun.runOrThrow( - SearchResponderV2.fulltextSearchV2( + fulltextSearchV2( searchValue = "p7v", offset = 0, limitToProject = None, @@ -124,7 +195,7 @@ class SearchResponderV2Spec extends CoreSpec { "perform an extended search for books that have the title 'Zeitglöcklein des Lebens'" in { val searchResult = UnsafeZioRun.runOrThrow( - SearchResponderV2.gravsearchV2( + gravsearchV2( searchResponderV2SpecFullData.constructQueryForBooksWithTitleZeitgloecklein, apiV2SchemaWithOption(MarkupRendering.Xml), anonymousUser, @@ -140,7 +211,7 @@ class SearchResponderV2Spec extends CoreSpec { "perform an extended search for books that do not have the title 'Zeitglöcklein des Lebens'" in { val searchResult = UnsafeZioRun.runOrThrow( - SearchResponderV2.gravsearchV2( + gravsearchV2( searchResponderV2SpecFullData.constructQueryForBooksWithoutTitleZeitgloecklein, apiV2SchemaWithOption(MarkupRendering.Xml), anonymousUser, @@ -158,7 +229,7 @@ class SearchResponderV2Spec extends CoreSpec { .asSmartIri("http://www.knora.org/ontology/0803/incunabula#book") .mapAttempt(_.toOntologySchema(ApiV2Complex)) .map(Some(_)) - result <- SearchResponderV2.searchResourcesByLabelV2( + result <- searchResourcesByLabelV2( searchValue = "Narrenschiff", offset = 0, limitToProject = None, @@ -179,7 +250,7 @@ class SearchResponderV2Spec extends CoreSpec { .asSmartIri("http://www.knora.org/ontology/0803/incunabula#book") .mapAttempt(_.toOntologySchema(ApiV2Complex)) .map(Some(_)) - result <- SearchResponderV2.searchResourcesByLabelV2( + result <- searchResourcesByLabelV2( searchValue = "Narrenschiff", offset = 0, limitToProject = None, @@ -201,7 +272,7 @@ class SearchResponderV2Spec extends CoreSpec { .asSmartIri("http://www.knora.org/ontology/0803/incunabula#book") .mapAttempt(_.toOntologySchema(ApiV2Complex)) .map(Some(_)) - result <- SearchResponderV2.searchResourcesByLabelCountV2( + result <- searchResourcesByLabelCountV2( searchValue = "Narrenschiff", limitToProject = None, limitToResourceClass, @@ -221,7 +292,7 @@ class SearchResponderV2Spec extends CoreSpec { .asSmartIri("http://www.knora.org/ontology/0803/incunabula#book") .mapAttempt(_.toOntologySchema(ApiV2Complex)) .map(Some(_)) - result <- SearchResponderV2.searchResourcesByLabelCountV2( + result <- searchResourcesByLabelCountV2( searchValue = "Passio sancti Meynrhadi martyris et heremite", limitToProject = None, limitToResourceClass, @@ -234,7 +305,7 @@ class SearchResponderV2Spec extends CoreSpec { "search by project and resource class" in { val result = UnsafeZioRun.runOrThrow( - SearchResponderV2.searchResourcesByProjectAndClassV2( + searchResourcesByProjectAndClassV2( projectIri = SharedTestDataADM.incunabulaProject.id.toSmartIri, resourceClass = "http://0.0.0.0:3333/ontology/0803/incunabula/v2#book".toSmartIri, orderByProperty = Some("http://0.0.0.0:3333/ontology/0803/incunabula/v2#title".toSmartIri), @@ -249,7 +320,7 @@ class SearchResponderV2Spec extends CoreSpec { "search for list label" in { val result = UnsafeZioRun.runOrThrow( - SearchResponderV2.fulltextSearchV2( + fulltextSearchV2( searchValue = "non fiction", offset = 0, limitToProject = None, @@ -269,7 +340,7 @@ class SearchResponderV2Spec extends CoreSpec { "search for list label and find sub-nodes" in { val result = UnsafeZioRun.runOrThrow( - SearchResponderV2.fulltextSearchV2( + fulltextSearchV2( searchValue = "novel", offset = 0, limitToProject = None, @@ -289,7 +360,7 @@ class SearchResponderV2Spec extends CoreSpec { "perform an extended search for a particular compound object (book)" in { val searchResult = UnsafeZioRun.runOrThrow( - SearchResponderV2.gravsearchV2( + gravsearchV2( searchResponderV2SpecFullData.constructQueryForIncunabulaCompundObject, apiV2SchemaWithOption(MarkupRendering.Xml), anonymousUser, @@ -300,17 +371,15 @@ class SearchResponderV2Spec extends CoreSpec { "perform an extended search ordered asc by label" in { val queryAsc = searchResponderV2SpecFullData.constructQuerySortByLabel - val ascResult = UnsafeZioRun.runOrThrow( - SearchResponderV2.gravsearchV2(queryAsc, apiV2SchemaWithOption(MarkupRendering.Xml), anonymousUser), - ) + val ascResult = + UnsafeZioRun.runOrThrow(gravsearchV2(queryAsc, apiV2SchemaWithOption(MarkupRendering.Xml), anonymousUser)) assert(ascResult.resources.head.label == "A blue thing") } "perform an extended search ordered desc by label" in { val queryDesc = searchResponderV2SpecFullData.constructQuerySortByLabelDesc - val descResult = UnsafeZioRun.runOrThrow( - SearchResponderV2.gravsearchV2(queryDesc, apiV2SchemaWithOption(MarkupRendering.Xml), anonymousUser), - ) + val descResult = + UnsafeZioRun.runOrThrow(gravsearchV2(queryDesc, apiV2SchemaWithOption(MarkupRendering.Xml), anonymousUser)) assert(descResult.resources.head.label == "visible thing with hidden int values") } } diff --git a/integration/src/test/scala/org/knora/webapi/responders/v2/ValuesResponderV2Spec.scala b/integration/src/test/scala/org/knora/webapi/responders/v2/ValuesResponderV2Spec.scala index 950d809d44..d7afdadcc3 100644 --- a/integration/src/test/scala/org/knora/webapi/responders/v2/ValuesResponderV2Spec.scala +++ b/integration/src/test/scala/org/knora/webapi/responders/v2/ValuesResponderV2Spec.scala @@ -7,6 +7,7 @@ package org.knora.webapi.responders.v2 import org.apache.pekko.testkit.ImplicitSender import org.scalatest.compatible.Assertion +import zio.ZIO import java.time.Instant import java.util.UUID @@ -198,10 +199,12 @@ class ValuesResponderV2Spec extends CoreSpec with ImplicitSender { // Run the query. val result = UnsafeZioRun.runOrThrow( - SearchResponderV2.gravsearchV2( - GravsearchParser.parseQuery(gravsearchQuery), - SchemaRendering.apiV2SchemaWithOption(MarkupRendering.Xml), - requestingUser, + ZIO.serviceWithZIO[SearchResponderV2]( + _.gravsearchV2( + GravsearchParser.parseQuery(gravsearchQuery), + SchemaRendering.apiV2SchemaWithOption(MarkupRendering.Xml), + requestingUser, + ), ), ) diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala index 7e128f0bba..f9e9ab4608 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/SearchResponderV2.scala @@ -7,7 +7,6 @@ package org.knora.webapi.responders.v2 import com.typesafe.scalalogging.LazyLogging import zio.* -import zio.macros.accessible import dsp.errors.AssertionException import dsp.errors.BadRequestException @@ -82,7 +81,7 @@ case class ResourceCountV2(numberOfResources: Int) extends KnoraJsonLDResponseV2 ), ) } -@accessible + trait SearchResponderV2 { /** @@ -151,7 +150,7 @@ trait SearchResponderV2 { */ def fulltextSearchV2( searchValue: IRI, - offset: RuntimeFlags, + offset: Int, limitToProject: Option[ProjectIri], limitToResourceClass: Option[SmartIri], limitToStandoffClass: Option[SmartIri], @@ -187,7 +186,7 @@ trait SearchResponderV2 { */ def searchResourcesByLabelV2( searchValue: String, - offset: RuntimeFlags, + offset: Int, limitToProject: Option[ProjectIri], limitToResourceClass: Option[SmartIri], targetSchema: ApiV2Schema, @@ -209,7 +208,7 @@ trait SearchResponderV2 { projectIri: SmartIri, resourceClass: SmartIri, orderByProperty: Option[SmartIri], - page: RuntimeFlags, + page: Int, schemaAndOptions: SchemaRendering, requestingUser: User, ): Task[ReadResourcesSequenceV2] diff --git a/webapi/src/main/scala/org/knora/webapi/routing/v2/ResourcesRouteV2.scala b/webapi/src/main/scala/org/knora/webapi/routing/v2/ResourcesRouteV2.scala index 238bfce598..b7d8028475 100644 --- a/webapi/src/main/scala/org/knora/webapi/routing/v2/ResourcesRouteV2.scala +++ b/webapi/src/main/scala/org/knora/webapi/routing/v2/ResourcesRouteV2.scala @@ -10,6 +10,7 @@ import org.apache.pekko.http.scaladsl.server.Directives.* import org.apache.pekko.http.scaladsl.server.PathMatcher import org.apache.pekko.http.scaladsl.server.Route import zio.* +import zio.ZIO import java.time.Instant @@ -181,13 +182,15 @@ final case class ResourcesRouteV2(appConfig: AppConfig)( case (schema, options) => SchemaRendering(schema, options) } requestingUser <- Authenticator.getUserADM(requestContext) - response <- SearchResponderV2.searchResourcesByProjectAndClassV2( - projectIri, - resourceClass, - maybeOrderByProperty, - page, - targetSchema, - requestingUser, + response <- ZIO.serviceWithZIO[SearchResponderV2]( + _.searchResourcesByProjectAndClassV2( + projectIri, + resourceClass, + maybeOrderByProperty, + page, + targetSchema, + requestingUser, + ), ) } yield response diff --git a/webapi/src/main/scala/org/knora/webapi/slice/ontology/domain/service/OntologyRepo.scala b/webapi/src/main/scala/org/knora/webapi/slice/ontology/domain/service/OntologyRepo.scala index 9ec7ed85dd..469155c8fc 100644 --- a/webapi/src/main/scala/org/knora/webapi/slice/ontology/domain/service/OntologyRepo.scala +++ b/webapi/src/main/scala/org/knora/webapi/slice/ontology/domain/service/OntologyRepo.scala @@ -6,7 +6,6 @@ package org.knora.webapi.slice.ontology.domain.service import zio.Task -import zio.macros.accessible import org.knora.webapi.messages.v2.responder.ontologymessages.ReadClassInfoV2 import org.knora.webapi.messages.v2.responder.ontologymessages.ReadOntologyV2 @@ -15,7 +14,6 @@ import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri import org.knora.webapi.slice.common.repo.service.Repository import org.knora.webapi.slice.resourceinfo.domain.InternalIri -@accessible trait OntologyRepo extends Repository[ReadOntologyV2, InternalIri] { override def findById(id: InternalIri): Task[Option[ReadOntologyV2]] diff --git a/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/ResourceInfoLayers.scala b/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/ResourceInfoLayers.scala index 66d040ebc5..26db691725 100644 --- a/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/ResourceInfoLayers.scala +++ b/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/ResourceInfoLayers.scala @@ -13,7 +13,6 @@ import org.knora.webapi.slice.common.api.TapirToPekkoInterpreter import org.knora.webapi.slice.resourceinfo.api.ResourceInfoEndpoints import org.knora.webapi.slice.resourceinfo.api.ResourceInfoRoutes import org.knora.webapi.slice.resourceinfo.api.service.RestResourceInfoService -import org.knora.webapi.slice.resourceinfo.api.service.RestResourceInfoServiceLive import org.knora.webapi.slice.resourceinfo.domain.IriConverter import org.knora.webapi.slice.resourceinfo.repo.ResourceInfoRepoLive import org.knora.webapi.store.triplestore.api.TriplestoreService @@ -25,6 +24,6 @@ object ResourceInfoLayers { Nothing, RestResourceInfoService & ResourceInfoEndpoints & ResourceInfoRoutes, ] = - ResourceInfoRepoLive.layer >>> RestResourceInfoServiceLive.layer >+> ResourceInfoEndpoints.layer >+> ResourceInfoRoutes.layer + ResourceInfoRepoLive.layer >>> RestResourceInfoService.layer >+> ResourceInfoEndpoints.layer >+> ResourceInfoRoutes.layer } diff --git a/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/api/service/RestResourceInfoService.scala b/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/api/service/RestResourceInfoService.scala index 51e5c546ea..9a2fd0642e 100644 --- a/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/api/service/RestResourceInfoService.scala +++ b/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/api/service/RestResourceInfoService.scala @@ -6,7 +6,6 @@ package org.knora.webapi.slice.resourceinfo.api.service import zio.* -import zio.macros.accessible import java.time.Instant @@ -19,28 +18,7 @@ import org.knora.webapi.slice.resourceinfo.api.model.ResourceInfoDto import org.knora.webapi.slice.resourceinfo.domain.IriConverter import org.knora.webapi.slice.resourceinfo.domain.ResourceInfoRepo -@accessible -trait RestResourceInfoService { - - /** - * Queries the existing resources of a certain resource class of a single project and returns the [[ResourceInfoDto]] in a [[ListResponseDto]] - * List can be sorted determined by the ordering. - * @param projectIri an external IRI for the project - * @param resourceClass an external IRI to the resource class to retrieve - * @param order sort by property - * @param orderBy sort by ascending or descending - * @return the [[ListResponseDto]] for the project and resource class - */ - def findByProjectAndResourceClass( - projectIri: IriIdentifier, - resourceClass: IRI, - order: Order, - orderBy: OrderBy, - ): Task[ListResponseDto] -} - -final case class RestResourceInfoServiceLive(repo: ResourceInfoRepo, iriConverter: IriConverter) - extends RestResourceInfoService { +final case class RestResourceInfoService(repo: ResourceInfoRepo, iriConverter: IriConverter) { private def lastModificationDateSort(order: Order)(one: ResourceInfoDto, two: ResourceInfoDto) = instant(order)(one.lastModificationDate, two.lastModificationDate) @@ -59,7 +37,16 @@ final case class RestResourceInfoServiceLive(repo: ResourceInfoRepo, iriConverte case (CreationDate, order) => resources.sortWith(creationDateSort(order)) } - override def findByProjectAndResourceClass( + /** + * Queries the existing resources of a certain resource class of a single project and returns the [[ResourceInfoDto]] in a [[ListResponseDto]] + * List can be sorted determined by the ordering. + * @param projectIri an external IRI for the project + * @param resourceClass an external IRI to the resource class to retrieve + * @param order sort by property + * @param orderBy sort by ascending or descending + * @return the [[ListResponseDto]] for the project and resource class + */ + def findByProjectAndResourceClass( projectIri: IriIdentifier, resourceClass: IRI, order: Order, @@ -73,6 +60,6 @@ final case class RestResourceInfoServiceLive(repo: ResourceInfoRepo, iriConverte } yield ListResponseDto(sort(resources, order, orderBy)) } -object RestResourceInfoServiceLive { - val layer = ZLayer.derive[RestResourceInfoServiceLive] +object RestResourceInfoService { + val layer = ZLayer.derive[RestResourceInfoService] } diff --git a/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/domain/ResourceInfoRepo.scala b/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/domain/ResourceInfoRepo.scala index 5fc4438c8a..70e9b7106e 100644 --- a/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/domain/ResourceInfoRepo.scala +++ b/webapi/src/main/scala/org/knora/webapi/slice/resourceinfo/domain/ResourceInfoRepo.scala @@ -6,11 +6,9 @@ package org.knora.webapi.slice.resourceinfo.domain import zio.Task -import zio.macros.accessible import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectIdentifierADM.IriIdentifier -@accessible trait ResourceInfoRepo { def findByProjectAndResourceClass(projectIri: IriIdentifier, resourceClass: InternalIri): Task[List[ResourceInfo]] } diff --git a/webapi/src/test/scala/org/knora/webapi/slice/admin/api/service/MaintenanceServiceSpec.scala b/webapi/src/test/scala/org/knora/webapi/slice/admin/api/service/MaintenanceServiceSpec.scala index 8c7eb329d1..2024cabb5b 100644 --- a/webapi/src/test/scala/org/knora/webapi/slice/admin/api/service/MaintenanceServiceSpec.scala +++ b/webapi/src/test/scala/org/knora/webapi/slice/admin/api/service/MaintenanceServiceSpec.scala @@ -53,7 +53,7 @@ object MaintenanceServiceSpec extends ZIOSpecDefault { | knora-base:internalFilename "$testAssetId.jp2"^^xsd:string; | } |""".stripMargin) - .flatMap(TestTripleStore.setDataset) + .flatMap(ds => ZIO.serviceWithZIO[TestTripleStore](_.setDataset(ds))) def queryForDim() = for { rowMap <- TriplestoreService diff --git a/webapi/src/test/scala/org/knora/webapi/slice/ontology/repo/service/OntologyRepoLiveSpec.scala b/webapi/src/test/scala/org/knora/webapi/slice/ontology/repo/service/OntologyRepoLiveSpec.scala index f46d3c381f..0c4436017e 100644 --- a/webapi/src/test/scala/org/knora/webapi/slice/ontology/repo/service/OntologyRepoLiveSpec.scala +++ b/webapi/src/test/scala/org/knora/webapi/slice/ontology/repo/service/OntologyRepoLiveSpec.scala @@ -6,6 +6,7 @@ package org.knora.webapi.slice.ontology.repo.service import zio.Scope +import zio.ZIO import zio.test.* import zio.test.ZIOSpecDefault @@ -31,19 +32,28 @@ object OntologyRepoLiveSpec extends ZIOSpecDefault { private val aKnownClassSmartIri: SmartIri = sf.toSmartIri(aKnownClassIri.value) private val ontologySmartIri: SmartIri = aKnownClassSmartIri.getOntologyFromEntity + // accessor + private def findById(id: InternalIri) = ZIO.serviceWithZIO[OntologyRepo](_.findById(id)) + private def findAll() = ZIO.serviceWithZIO[OntologyRepo](_.findAll()) + private def findClassBy(classIri: InternalIri) = ZIO.serviceWithZIO[OntologyRepo](_.findClassBy(classIri)) + private def findAllSuperClassesBy(classIri: InternalIri) = + ZIO.serviceWithZIO[OntologyRepo](_.findAllSuperClassesBy(classIri)) + private def findAllSubclassesBy(classIri: InternalIri) = + ZIO.serviceWithZIO[OntologyRepo](_.findAllSubclassesBy(classIri)) + val spec: Spec[TestEnvironment & Scope, Any] = suite("OntologyRepoLive")( suite("findOntologyBy(InternalIri)")( test("when searching for unknown iri => return None") { for { - actual <- OntologyRepo.findById(anUnknownInternalOntologyIri) + actual <- findById(anUnknownInternalOntologyIri) } yield assertTrue(actual.isEmpty) }, test("when searching for known iri => return Some(ReadOntology)") { val cacheData = OntologyCacheDataBuilder.builder(ontologySmartIri).build for { _ <- OntologyCacheFake.set(cacheData) - actual <- OntologyRepo.findById(ontologySmartIri.toInternalIri) + actual <- findById(ontologySmartIri.toInternalIri) } yield assertTrue(actual == cacheData.ontologies.get(ontologySmartIri)) }, ), @@ -59,19 +69,19 @@ object OntologyRepoLiveSpec extends ZIOSpecDefault { .build for { _ <- OntologyCacheFake.set(data) - actual <- OntologyRepo.findClassBy(aKnownClassIri) + actual <- findClassBy(aKnownClassIri) } yield assertTrue(actual.contains(knownClass)) }, test("when searching for unknown iri => return None") { for { - actual <- OntologyRepo.findClassBy(anUnknownClassIri) + actual <- findClassBy(anUnknownClassIri) } yield assertTrue(actual.isEmpty) }, ), suite("findAll()")( test("given cache is Empty => return empty List") { for { - actual <- OntologyRepo.findAll() + actual <- findAll() } yield assertTrue(actual.isEmpty) }, test("given cache has an ontology => return List of ontologies") { @@ -79,7 +89,7 @@ object OntologyRepoLiveSpec extends ZIOSpecDefault { val cacheData = OntologyCacheDataBuilder.builder.addOntology(ontology).build for { _ <- OntologyCacheFake.set(cacheData) - actual <- OntologyRepo.findAll() + actual <- findAll() } yield assertTrue(actual == List(ontology)) }, ), @@ -94,7 +104,7 @@ object OntologyRepoLiveSpec extends ZIOSpecDefault { ) for { _ <- OntologyCacheFake.set(data.build).debug - actual <- OntologyRepo.findAllSubclassesBy(Biblio.Class.Publication) + actual <- findAllSubclassesBy(Biblio.Class.Publication) } yield assertTrue(actual.isEmpty) }, test("findAllSubclassesBy multiple levels up across ontologies") { @@ -122,7 +132,7 @@ object OntologyRepoLiveSpec extends ZIOSpecDefault { .addOntology(biblioOntologyDefinition) for { _ <- OntologyCacheFake.set(data.build).debug - actual <- OntologyRepo.findAllSubclassesBy(Biblio.Class.Publication) + actual <- findAllSubclassesBy(Biblio.Class.Publication) actualIris = actual.map(_.entityInfoContent.classIri.toInternalIri) } yield assertTrue(actualIris == List(Biblio.Class.Article, Biblio.Class.JournalArticle)) }, @@ -138,7 +148,7 @@ object OntologyRepoLiveSpec extends ZIOSpecDefault { ) for { _ <- OntologyCacheFake.set(data.build).debug - actual <- OntologyRepo.findAllSuperClassesBy(Biblio.Class.Article) + actual <- findAllSuperClassesBy(Biblio.Class.Article) } yield assertTrue(actual.isEmpty) }, test("findAllSuperClassesBy multiple levels up across ontologies") { @@ -164,7 +174,7 @@ object OntologyRepoLiveSpec extends ZIOSpecDefault { .addOntology(biblioOntologyDefinition) for { _ <- OntologyCacheFake.set(data.build).debug - actual <- OntologyRepo.findAllSuperClassesBy(Biblio.Class.JournalArticle) + actual <- findAllSuperClassesBy(Biblio.Class.JournalArticle) actualIris = actual.map(_.entityInfoContent.classIri.toInternalIri) } yield assertTrue(actualIris == List(Biblio.Class.Article, Anything.Class.Thing, Biblio.Class.Publication)) }, diff --git a/webapi/src/test/scala/org/knora/webapi/slice/resourceinfo/api/LiveRestResourceInfoServiceSpec.scala b/webapi/src/test/scala/org/knora/webapi/slice/resourceinfo/api/LiveRestResourceInfoServiceSpec.scala index cfdd52f5a6..c0009d3cf8 100644 --- a/webapi/src/test/scala/org/knora/webapi/slice/resourceinfo/api/LiveRestResourceInfoServiceSpec.scala +++ b/webapi/src/test/scala/org/knora/webapi/slice/resourceinfo/api/LiveRestResourceInfoServiceSpec.scala @@ -6,6 +6,7 @@ package org.knora.webapi.slice.resourceinfo.api import zio.Exit +import zio.ZIO import zio.test.* import java.time.Instant.now @@ -13,15 +14,18 @@ import java.time.temporal.ChronoUnit.DAYS import java.util.UUID.randomUUID import dsp.errors.BadRequestException +import org.knora.webapi.IRI import org.knora.webapi.messages.StringFormatter +import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectIdentifierADM.IriIdentifier import org.knora.webapi.slice.resourceinfo.api.model.ListResponseDto import org.knora.webapi.slice.resourceinfo.api.model.QueryParams.Asc import org.knora.webapi.slice.resourceinfo.api.model.QueryParams.CreationDate import org.knora.webapi.slice.resourceinfo.api.model.QueryParams.Desc import org.knora.webapi.slice.resourceinfo.api.model.QueryParams.LastModificationDate +import org.knora.webapi.slice.resourceinfo.api.model.QueryParams.Order +import org.knora.webapi.slice.resourceinfo.api.model.QueryParams.OrderBy import org.knora.webapi.slice.resourceinfo.api.model.ResourceInfoDto import org.knora.webapi.slice.resourceinfo.api.service.RestResourceInfoService -import org.knora.webapi.slice.resourceinfo.api.service.RestResourceInfoServiceLive import org.knora.webapi.slice.resourceinfo.domain.IriConverter import org.knora.webapi.slice.resourceinfo.domain.ResourceInfo import org.knora.webapi.slice.resourceinfo.repo.ResourceInfoRepoFake @@ -31,18 +35,22 @@ import org.knora.webapi.slice.resourceinfo.repo.ResourceInfoRepoFake.unknownProj object LiveRestResourceInfoServiceSpec extends ZIOSpecDefault { + private def findByProjectAndResourceClass( + projectIri: IriIdentifier, + resourceClass: IRI, + order: Order, + orderBy: OrderBy, + ): ZIO[RestResourceInfoService, Throwable, ListResponseDto] = + ZIO.serviceWithZIO[RestResourceInfoService]( + _.findByProjectAndResourceClass(projectIri, resourceClass, order, orderBy), + ) + override def spec: Spec[Any, Any] = suite("LiveRestResourceInfoServiceSpec")( test("should fail with bad request given an invalid resourceClass") { for { - actual <- RestResourceInfoService - .findByProjectAndResourceClass( - knownProjectIRI, - "invalid-resource-class", - Asc, - LastModificationDate, - ) - .exit + actual <- + findByProjectAndResourceClass(knownProjectIRI, "invalid-resource-class", Asc, LastModificationDate).exit } yield assertTrue( actual == Exit.fail(BadRequestException("Invalid resourceClass: Couldn't parse IRI: invalid-resource-class")), ) @@ -50,33 +58,19 @@ object LiveRestResourceInfoServiceSpec extends ZIOSpecDefault { test("should return empty list if no resources found // unknown project and resourceClass") { for { actual <- - RestResourceInfoService.findByProjectAndResourceClass( - unknownProjectIRI, - "http://unknown-resource-class", - Asc, - LastModificationDate, - ) + findByProjectAndResourceClass(unknownProjectIRI, "http://unknown-resource-class", Asc, LastModificationDate) } yield assertTrue(actual == ListResponseDto.empty) }, test("should return empty list if no resources found // unknown resourceClass") { for { - actual <- RestResourceInfoService.findByProjectAndResourceClass( - knownProjectIRI, - "http://unknown-resource-class", - Asc, - LastModificationDate, - ) + actual <- + findByProjectAndResourceClass(knownProjectIRI, "http://unknown-resource-class", Asc, LastModificationDate) } yield assertTrue(actual == ListResponseDto.empty) }, test("should return empty list if no resources found // unknown project") { for { actual <- - RestResourceInfoService.findByProjectAndResourceClass( - unknownProjectIRI, - knownResourceClass.value, - Asc, - LastModificationDate, - ) + findByProjectAndResourceClass(unknownProjectIRI, knownResourceClass.value, Asc, LastModificationDate) } yield assertTrue(actual == ListResponseDto.empty) }, test( @@ -89,14 +83,8 @@ object LiveRestResourceInfoServiceSpec extends ZIOSpecDefault { val given2 = ResourceInfo("http://resourceIri/" + randomUUID, now.minus(20, DAYS), Some(now.minus(8, DAYS)), now) for { - _ <- ResourceInfoRepoFake.addAll(List(given1, given2), knownProjectIRI, knownResourceClass) - actual <- - RestResourceInfoService.findByProjectAndResourceClass( - knownProjectIRI, - knownResourceClass.value, - Asc, - LastModificationDate, - ) + _ <- ResourceInfoRepoFake.addAll(List(given1, given2), knownProjectIRI, knownResourceClass) + actual <- findByProjectAndResourceClass(knownProjectIRI, knownResourceClass.value, Asc, LastModificationDate) } yield { val items = List(given1, given2).map(ResourceInfoDto.from).sortBy(_.lastModificationDate) assertTrue(actual == model.ListResponseDto(items)) @@ -112,13 +100,8 @@ object LiveRestResourceInfoServiceSpec extends ZIOSpecDefault { val given2 = ResourceInfo("http://resourceIri/" + randomUUID, now.minus(20, DAYS), Some(now.minus(8, DAYS)), now) for { - _ <- ResourceInfoRepoFake.addAll(List(given1, given2), knownProjectIRI, knownResourceClass) - actual <- RestResourceInfoService.findByProjectAndResourceClass( - knownProjectIRI, - knownResourceClass.value, - Desc, - CreationDate, - ) + _ <- ResourceInfoRepoFake.addAll(List(given1, given2), knownProjectIRI, knownResourceClass) + actual <- findByProjectAndResourceClass(knownProjectIRI, knownResourceClass.value, Desc, CreationDate) } yield { val items = List(given1, given2).map(ResourceInfoDto.from).sortBy(_.creationDate).reverse assertTrue(actual == model.ListResponseDto(items)) @@ -127,7 +110,7 @@ object LiveRestResourceInfoServiceSpec extends ZIOSpecDefault { ).provide( IriConverter.layer, StringFormatter.test, - RestResourceInfoServiceLive.layer, + RestResourceInfoService.layer, ResourceInfoRepoFake.layer, ) } diff --git a/webapi/src/test/scala/org/knora/webapi/store/triplestore/api/TriplestoreServiceInMemory.scala b/webapi/src/test/scala/org/knora/webapi/store/triplestore/api/TriplestoreServiceInMemory.scala index 1f3e062a1e..0547b4e61b 100644 --- a/webapi/src/test/scala/org/knora/webapi/store/triplestore/api/TriplestoreServiceInMemory.scala +++ b/webapi/src/test/scala/org/knora/webapi/store/triplestore/api/TriplestoreServiceInMemory.scala @@ -23,7 +23,6 @@ import zio.ULayer import zio.URIO import zio.ZIO import zio.ZLayer -import zio.macros.accessible import java.io.InputStream import java.nio.charset.StandardCharsets @@ -54,7 +53,6 @@ import org.knora.webapi.util.ZScopedJavaIoStreams.byteArrayOutputStream import org.knora.webapi.util.ZScopedJavaIoStreams.fileInputStream import org.knora.webapi.util.ZScopedJavaIoStreams.fileOutputStream -@accessible trait TestTripleStore extends TriplestoreService { def setDataset(ds: Dataset): UIO[Unit] def getDataset: UIO[Dataset]