diff --git a/webapi/src/test/scala/org/knora/webapi/responders/v2/ReadResourceV2LensLearningSpec.scala b/webapi/src/test/scala/org/knora/webapi/responders/v2/ReadResourceV2LensLearningSpec.scala deleted file mode 100644 index ca70a2aa18..0000000000 --- a/webapi/src/test/scala/org/knora/webapi/responders/v2/ReadResourceV2LensLearningSpec.scala +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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.responders.v2 -import monocle.* -import monocle.Lens -import monocle.Optional -import monocle.macros.* -import zio.test.* - -import org.knora.webapi.messages.SmartIri -import org.knora.webapi.messages.StringFormatter -import org.knora.webapi.messages.v2.responder.resourcemessages.ReadResourceV2 -import org.knora.webapi.messages.v2.responder.valuemessages.* -import org.knora.webapi.slice.admin.domain.model.KnoraProject.CopyrightAttribution -import org.knora.webapi.slice.admin.domain.model.KnoraProject.License - -object ReadResourceV2LensLearningSpec extends ZIOSpecDefault { - - val fileValueContentLens: Lens[ReadValueV2, ValueContentV2] = - Lens[ReadValueV2, ValueContentV2](_.valueContent)(fc => { - case rv: ReadLinkValueV2 => - fc match { - case lv: LinkValueContentV2 => rv.copy(valueContent = lv) - case _ => rv - } - case rv: ReadTextValueV2 => - fc match { - case tv: TextValueContentV2 => rv.copy(valueContent = tv) - case _ => rv - } - case ov: ReadOtherValueV2 => ov.copy(valueContent = fc) - }) - - val fileValueContentPrism: Prism[ValueContentV2, FileValueContentV2] = - GenPrism[ValueContentV2, FileValueContentV2] - - val fileValueLens: Lens[FileValueContentV2, FileValueV2] = - Lens[FileValueContentV2, FileValueV2](_.fileValue)(fv => { - case vc: MovingImageFileValueContentV2 => vc.copy(fileValue = fv) - case vc: StillImageFileValueContentV2 => vc.copy(fileValue = fv) - case vc: AudioFileValueContentV2 => vc.copy(fileValue = fv) - case vc: DocumentFileValueContentV2 => vc.copy(fileValue = fv) - case vc: StillImageExternalFileValueContentV2 => vc.copy(fileValue = fv) - case vc: ArchiveFileValueContentV2 => vc.copy(fileValue = fv) - case vc: TextFileValueContentV2 => vc.copy(fileValue = fv) - }) - - val copyrightAttributionLens: Lens[FileValueV2, Option[CopyrightAttribution]] = - GenLens[FileValueV2](_.copyrightAttribution) - val licenseLens: Lens[FileValueV2, Option[License]] = GenLens[FileValueV2](_.license) - - type ReadResourceValues = Map[SmartIri, Seq[ReadValueV2]] - - private val commonComposed: Optional[ReadValueV2, FileValueV2] = - fileValueContentLens.andThen(fileValueContentPrism).andThen(fileValueLens) - private val composedLicense = commonComposed.andThen(licenseLens) - private val composedCopyright = commonComposed.andThen(copyrightAttributionLens) - - private def setIfMissing[T]( - optional: Optional[ReadValueV2, Option[T]], - ): Option[T] => ReadResourceV2 => ReadResourceV2 = - newValue => - readResource => { - val newValues: ReadResourceValues = readResource.values.map((iri: SmartIri, seq: Seq[ReadValueV2]) => - ( - iri, - seq.map { readValue => - optional.getOption(readValue).flatten match { - case Some(_) => readValue - case None => optional.replace(newValue)(readValue) - } - }, - ), - ) - readResource.copy(values = newValues) - } - - val setCopyrightAttributionIfMissing: Option[CopyrightAttribution] => ReadResourceV2 => ReadResourceV2 = - setIfMissing(composedCopyright) - - val setLicenseIfMissing: Option[License] => ReadResourceV2 => ReadResourceV2 = setIfMissing(composedLicense) - - val sf = StringFormatter.getInitializedTestInstance - - val aLicense = License.unsafeFrom("CC-BY-4.0") - val anotherLicense = License.unsafeFrom("Apache-2.0") - - val aCopyrightAttribution = CopyrightAttribution.unsafeFrom("2020, John Doe") - val anotherCopyrightAttribution = CopyrightAttribution.unsafeFrom("2024, Jane Doe") - - val fileValueWithoutLicenseOrCopyright = - FileValueV2("internalFilename", "internalMimeType", None, None, None, None) - val fileValueWithALicense = - FileValueV2("internalFilename", "internalMimeType", None, None, None, Some(aLicense)) - val fileValueWithACopyrightAttribution = - FileValueV2("internalFilename", "internalMimeType", None, None, Some(aCopyrightAttribution), None) - - private val fileValueLensesSuite = suite("FileValueV2 lenses")( - test("licenseLens should replace an empty license with a new one") { - val actual = licenseLens.replace(Some(aLicense))(fileValueWithoutLicenseOrCopyright) - assertTrue(actual.license == Some(aLicense)) - }, - test("licenseLens should replace an existing license with a new one") { - val actual = licenseLens.replace(Some(anotherLicense))(fileValueWithALicense) - assertTrue(actual.license == Some(anotherLicense)) - }, - test("copyrightAttributionLens should replace an empty copyrightAttribution with a new one") { - val aCopyrightAttribution = CopyrightAttribution.unsafeFrom("2020, John Doe") - val actual = copyrightAttributionLens.replace(Some(aCopyrightAttribution))(fileValueWithoutLicenseOrCopyright) - assertTrue(actual.copyrightAttribution == Some(aCopyrightAttribution)) - }, - test("copyrightAttributionLens should replace an existing copyrightAttribution with a new one") { - val actual = - copyrightAttributionLens.replace(Some(anotherCopyrightAttribution))(fileValueWithACopyrightAttribution) - assertTrue(actual.copyrightAttribution == Some(anotherCopyrightAttribution)) - }, - ) - - val spec = suite("ReadResourceV2LensLearning")( - fileValueLensesSuite, - ) -}