Skip to content

Commit

Permalink
remove AssetIngestState #2
Browse files Browse the repository at this point in the history
  • Loading branch information
siers committed Nov 20, 2024
1 parent 6b03f50 commit e098d42
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ case class SipiServiceTestDelegator(
if (whichSipi.useLive) { live }
else { mock }

/**
* Asks Sipi for metadata about a file in the tmp folder, served from the 'knora.json' route.
*
* @param filename the path to the file.
* @return a [[FileMetadataSipiResponse]] containing the requested metadata.
*/
override def getFileMetadataFromSipiTemp(filename: String): Task[FileMetadataSipiResponse] =
sipiService.getFileMetadataFromSipiTemp(filename)

/**
* Asks DSP-Ingest for metadata about a file in permanent location, served from the 'knora.json' route.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import org.knora.webapi.messages.v2.responder.resourcemessages.ReadResourceV2
import org.knora.webapi.messages.v2.responder.standoffmessages.*
import org.knora.webapi.messages.v2.responder.valuemessages.ValueContentV2.FileInfo
import org.knora.webapi.routing.RouteUtilZ
import org.knora.webapi.routing.v2.AssetIngestState
import org.knora.webapi.routing.v2.AssetIngestState.*
import org.knora.webapi.slice.admin.api.model.MaintenanceRequests.AssetId
import org.knora.webapi.slice.admin.api.model.Project
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode
Expand Down Expand Up @@ -476,7 +474,6 @@ case class ReadOtherValueV2(
/**
* Represents a Knora value to be created in an existing resource.
*
* @param ingestState indicates the state of the file, either ingested or in temp folder
* @param resourceIri the resource the new value should be attached to.
* @param resourceClassIri the resource class that the client believes the resource belongs to.
* @param propertyIri the property of the new value. If the client wants to create a link, this must be a link value property.
Expand All @@ -496,7 +493,6 @@ case class CreateValueV2(
valueUUID: Option[UUID] = None,
valueCreationDate: Option[Instant] = None,
permissions: Option[String] = None,
ingestState: AssetIngestState = AssetInTemp,
)

/** A trait for classes representing information to be updated in a value. */
Expand Down Expand Up @@ -552,7 +548,6 @@ case class UpdateValueContentV2(
permissions: Option[String] = None,
valueCreationDate: Option[Instant] = None,
newValueVersionIri: Option[SmartIri] = None,
ingestState: AssetIngestState = AssetInTemp,
) extends UpdateValueV2

/**
Expand Down Expand Up @@ -678,15 +673,13 @@ object ValueContentV2 {
* Given the jsonLd contains a FileValueHasFilename, it will try to fetch the FileInfo from Sipi or Dsp-Ingest.
*
* @param shortcode The shortcode of the project
* @param ingestState The state of the file, either ingested already or in Sipi temp folder
* @param jsonLd the jsonLd object
* @return Some FileInfo if FileValueHasFilename found and the remote service returned the metadata.
* None if FileValueHasFilename is not found in the jsonLd object.
* Fails if the file is not found in the remote service or something goes wrong.
*/
def getFileInfo(
shortcode: Shortcode,
ingestState: AssetIngestState,
jsonLd: JsonLDObject,
): ZIO[SipiService, Throwable, Option[FileInfo]] =
fileInfoFromExternal(jsonLd.getString(FileValueHasFilename).toOption.flatten, shortcode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import org.knora.webapi.messages.v2.responder.resourcemessages.ReadResourceV2
import org.knora.webapi.messages.v2.responder.valuemessages.FileValueContentV2
import org.knora.webapi.messages.v2.responder.valuemessages.ReadValueV2
import org.knora.webapi.messages.v2.responder.valuemessages.StillImageExternalFileValueContentV2
import org.knora.webapi.routing.v2.AssetIngestState
import org.knora.webapi.routing.v2.AssetIngestState.*
import org.knora.webapi.slice.admin.domain.model.Permission
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.store.iiif.api.SipiService
Expand Down Expand Up @@ -91,17 +89,6 @@ trait ResourceUtilV2 {
fileValues: Seq[FileValueContentV2],
requestingUser: User,
): Task[T]

def doSipiPostUpdateIfInTemp[T <: UpdateResultInProject](
ingestState: AssetIngestState,
updateTask: Task[T],
fileValues: Seq[FileValueContentV2],
requestingUser: User,
): Task[T] =
ingestState match {
case AssetIngested => updateTask
case AssetInTemp => doSipiPostUpdate(updateTask, fileValues, requestingUser)
}
}

final case class ResourceUtilV2Live(triplestore: TriplestoreService, sipiService: SipiService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ final case class ValuesResponderV2(

// If we were creating a file value, have Sipi move the file to permanent storage if the update
// was successful, or delete the temporary file if the update failed.
resourceUtilV2.doSipiPostUpdateIfInTemp(
valueToCreate.ingestState,
resourceUtilV2.doSipiPostUpdate(
triplestoreUpdateFuture,
valueToCreate.valueContent.asOpt[FileValueContentV2].toSeq,
requestingUser,
Expand Down Expand Up @@ -956,8 +955,7 @@ final case class ValuesResponderV2(
makeTaskFutureToUpdateValueContent(updateValueContentV2),
)

resourceUtilV2.doSipiPostUpdateIfInTemp(
updateValueContentV2.ingestState,
resourceUtilV2.doSipiPostUpdate(
triplestoreUpdate,
updateValueContentV2.valueContent.asOpt[FileValueContentV2].toSeq,
requestingUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ object FileMetadataSipiResponse {

trait SipiService {

/**
* Asks Sipi for metadata about a file in the tmp folder, served from the 'knora.json' route.
*
* @param filename the path to the file.
* @return a [[FileMetadataSipiResponse]] containing the requested metadata.
*/
def getFileMetadataFromSipiTemp(filename: String): Task[FileMetadataSipiResponse]

/**
* Asks DSP-Ingest for metadata about a file in permanent location, served from the 'knora.json' route.
*
Expand Down Expand Up @@ -112,5 +104,4 @@ trait SipiService {
* @return The path to the downloaded asset. If the asset could not be downloaded, [[None]] is returned.
*/
def downloadAsset(asset: Asset, targetDir: Path, user: User): Task[Option[Path]]

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import sttp.client3.SttpBackend
import sttp.client3.httpclient.zio.HttpClientZioBackend
import sttp.model.Uri
import zio.*
import zio.json.DecoderOps
import zio.json.ast.Json
import zio.nio.file.Path

Expand All @@ -22,7 +21,6 @@ import dsp.errors.BadRequestException
import dsp.errors.NotFoundException
import org.knora.webapi.config.Sipi
import org.knora.webapi.messages.store.sipimessages.*
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.messages.v2.responder.SuccessResponseV2
import org.knora.webapi.slice.admin.api.model.MaintenanceRequests.AssetId
import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode
Expand All @@ -31,7 +29,6 @@ import org.knora.webapi.slice.admin.domain.service.Asset
import org.knora.webapi.slice.admin.domain.service.DspIngestClient
import org.knora.webapi.slice.infrastructure.Jwt
import org.knora.webapi.slice.infrastructure.JwtService
import org.knora.webapi.slice.infrastructure.Scope as AuthScope
import org.knora.webapi.slice.security.ScopeResolver
import org.knora.webapi.store.iiif.api.FileMetadataSipiResponse
import org.knora.webapi.store.iiif.api.SipiService
Expand All @@ -54,24 +51,6 @@ final case class SipiServiceLive(
s"${sipiConfig.internalBaseUrl}/${asset.belongsToProject.value}/${asset.internalFilename}"
}

/**
* Asks Sipi for metadata about a file, served from the 'knora.json' route.
*
* @param filename the file name
* @return a [[FileMetadataSipiResponse]] containing the requested metadata.
*/
override def getFileMetadataFromSipiTemp(filename: String): Task[FileMetadataSipiResponse] =
for {
jwt <- jwtService.createJwt(KnoraSystemInstances.Users.SystemUser.userIri, AuthScope.admin)
request = quickRequest
.get(uri"${sipiConfig.internalBaseUrl}/tmp/$filename/knora.json")
.header("Authorization", s"Bearer ${jwt.jwtString}")
body <- doSipiRequest(request)
res <- ZIO
.fromEither(body.fromJson[FileMetadataSipiResponse])
.mapError(e => SipiException(s"Invalid response from Sipi: $e, $body"))
} yield res

override def getFileMetadataFromDspIngest(shortcode: Shortcode, assetId: AssetId): Task[FileMetadataSipiResponse] =
for {
response <- dspIngestClient.getAssetInfo(shortcode, assetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ case class SipiServiceMock(ref: Ref[Map[SipiMockMethodName, Task[Object]]]) exte
ref.set(SipiMockMethodName.values.map(_ -> fail).toMap)
}

override def getFileMetadataFromSipiTemp(filename: String): Task[FileMetadataSipiResponse] =
getReturnValue(GetFileMetadataFromSipiTemp)

def moveTemporaryFileToPermanentStorage(
moveTemporaryFileToPermanentStorageRequestV2: MoveTemporaryFileToPermanentStorageRequest,
): Task[SuccessResponseV2] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,21 @@ object ValueContentV2Spec extends ZIOSpecDefault {

override def spec: Spec[Any, Option[Throwable]] =
suite("ValueContentV2.getFileInfo")(
suite("Given the asset is present in the tmp folder of Sipi")(
test("When getting file metadata with AssetInTemp from Sipi, then it should succeed") {
for {
temp <- ValueContentV2.getFileInfo(shortcode0001, AssetInTemp, jsonLdObj).some
} yield assertTrue(temp.metadata == expected)
},
test("When getting file metadata with AssetIngested from dsp-ingest, then it should fail") {
for {
exit <- ValueContentV2.getFileInfo(shortcode0001, AssetIngested, jsonLdObj).exit
} yield assert(exit)(failsWithA[AssertionException])
},
).provide(mockSipi(AssetInTemp)),
suite("Given the asset is ingested")(
test("When getting file metadata with AssetInTemp from Sipi, then it should fail") {
for {
exit <- ValueContentV2.getFileInfo(shortcode0001, AssetInTemp, jsonLdObj).exit
} yield assert(exit)(failsWithA[AssertionException])
},
test("When getting file metadata with AssetIngested from dsp-ingest, then it should succeed") {
for {
ingested <- ValueContentV2.getFileInfo(shortcode0001, AssetIngested, jsonLdObj).some
ingested <- ValueContentV2.getFileInfo(shortcode0001, jsonLdObj).some
} yield assertTrue(ingested.metadata == expected)
},
).provide(mockSipi(AssetIngested)),
).provide(mockSipi()),
)

private def mockSipi(flag: AssetIngestState) = ZLayer.succeed(new SipiService {

override def getFileMetadataFromSipiTemp(filename: String): Task[FileMetadataSipiResponse] =
if (flag == AssetInTemp) { ZIO.succeed(expected) }
else { ZIO.fail(AssertionException("fail")) }

private def mockSipi() = ZLayer.succeed(new SipiService {
override def getFileMetadataFromDspIngest(
shortcode: KnoraProject.Shortcode,
assetId: AssetId,
): Task[FileMetadataSipiResponse] =
if (flag == AssetIngested) { ZIO.succeed(expected) }
else { ZIO.fail(AssertionException("fail")) }
ZIO.succeed(expected)

// The following are unsupported operations because they are not used in the test
def moveTemporaryFileToPermanentStorage(
Expand Down

0 comments on commit e098d42

Please sign in to comment.