From 67c0e94934c90f762aae8d2b5f0bbc48999bd220 Mon Sep 17 00:00:00 2001 From: Oliver <20188437+olivergrabinski@users.noreply.github.com> Date: Tue, 5 Mar 2024 09:15:37 +0100 Subject: [PATCH] Integration tests --- .../delta/plugins/storage/files/Files.scala | 12 +++++++----- .../files/routes/FilesRoutesSpec.scala | 19 ++++++++++++++++++- .../main/paradox/docs/delta/api/files-api.md | 2 ++ .../nexus/tests/kg/files/FilesSpec.scala | 19 +++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/Files.scala b/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/Files.scala index a6b5c24b79..20c89c996a 100644 --- a/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/Files.scala +++ b/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/Files.scala @@ -585,7 +585,8 @@ object Files { def updatedCustomMetadata(e: FileCustomMetadataUpdated): Option[FileState] = state.map { s => val newAttributes = FileAttributes.setCustomMetadata(s.attributes, e.metadata) - s.copy(rev = e.rev, attributes = newAttributes,tags = s.tags ++ Tags(e.tag, e.rev), updatedAt = e.instant, updatedBy = e.subject) + val newTags = s.tags ++ Tags(e.tag, e.rev) + s.copy(rev = e.rev, attributes = newAttributes, tags = newTags, updatedAt = e.instant, updatedBy = e.subject) } def tagAdded(e: FileTagAdded): Option[FileState] = state.map { s => @@ -733,10 +734,11 @@ object Files { FileState.serializer, Tagger[FileEvent]( { - case f: FileCreated => f.tag.map(t => t -> f.rev) - case f: FileUpdated => f.tag.map(t => t -> f.rev) - case f: FileTagAdded => Some(f.tag -> f.targetRev) - case _ => None + case f: FileCreated => f.tag.map(t => t -> f.rev) + case f: FileUpdated => f.tag.map(t => t -> f.rev) + case f: FileTagAdded => Some(f.tag -> f.targetRev) + case f: FileCustomMetadataUpdated => f.tag.map(t => t -> f.rev) + case _ => None }, { case f: FileTagDeleted => Some(f.tag) diff --git a/delta/plugins/storage/src/test/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/FilesRoutesSpec.scala b/delta/plugins/storage/src/test/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/FilesRoutesSpec.scala index f62ca42ae9..fcae755fb7 100644 --- a/delta/plugins/storage/src/test/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/FilesRoutesSpec.scala +++ b/delta/plugins/storage/src/test/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/FilesRoutesSpec.scala @@ -5,7 +5,7 @@ import akka.http.scaladsl.model.ContentTypes.{`application/json`, `text/plain(UT import akka.http.scaladsl.model.MediaRanges._ import akka.http.scaladsl.model.MediaTypes.{`multipart/form-data`, `text/html`} import akka.http.scaladsl.model.headers._ -import akka.http.scaladsl.model.{HttpRequest, RequestEntity, StatusCodes, Uri} +import akka.http.scaladsl.model.{HttpRequest, MediaTypes, RequestEntity, StatusCodes, Uri} import akka.http.scaladsl.server.Route import cats.effect.IO import ch.epfl.bluebrain.nexus.delta.kernel.http.MediaTypeDetectorConfig @@ -434,6 +434,23 @@ class FilesRoutesSpec } } + "allow tagging when updating custom metadata" in { + givenAFile { id => + val metadata = genCustomMetadata() + val userTag = UserTag.unsafe("mytag") + + val headers = RawHeader("x-nxs-file-metadata", metadata.asJson.noSpaces) + Put(s"/v1/files/org/proj/$id?rev=1&tag=${userTag.value}").withHeaders(headers) ~> asWriter ~> routes ~> check { + status shouldEqual StatusCodes.OK + Get(s"/v1/files/org/proj/$id?tag=${userTag.value}") ~> Accept( + MediaTypes.`application/json` + ) ~> asReader ~> routes ~> check { + status shouldEqual StatusCodes.OK + } + } + } + } + "return an error when attempting to update custom metadata without providing it" in { givenAFile { id => Put(s"/v1/files/org/proj/$id?rev=1") ~> asWriter ~> routes ~> check { diff --git a/docs/src/main/paradox/docs/delta/api/files-api.md b/docs/src/main/paradox/docs/delta/api/files-api.md index be84fe0ffb..a1132d44d4 100644 --- a/docs/src/main/paradox/docs/delta/api/files-api.md +++ b/docs/src/main/paradox/docs/delta/api/files-api.md @@ -48,6 +48,7 @@ POST /v1/files/{org_label}/{project_label}?storage={storageId}&tag={tagName} ``` ... where + - `{storageId}` selects a specific storage backend where the file will be uploaded. This field is optional. When not specified, the default storage of the project is used. - `{tagName}` an optional label given to the file on its first revision. @@ -84,6 +85,7 @@ PUT /v1/files/{org_label}/{project_label}/{file_id}?storage={storageId}&tag={tag ``` ... where + - `{storageId}` selects a specific storage backend where the file will be uploaded. This field is optional. When not specified, the default storage of the project is used. - `{tagName}` an optional label given to the file on its first revision. diff --git a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/files/FilesSpec.scala b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/files/FilesSpec.scala index 8196b5eca3..97e3704c5d 100644 --- a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/files/FilesSpec.scala +++ b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/files/FilesSpec.scala @@ -140,7 +140,26 @@ class FilesSpec extends BaseIntegrationSpec { (updateCustomMetadata >> assertMetadataUpdated).accepted } + } + "update the custom metadata and tag at the same time" in { + givenAFile { id => + val updatedName = "new name" + val md = Json.obj("name" := updatedName) + val header = RawHeader("x-nxs-file-metadata", md.noSpaces) :: Nil + + val updateCustomMetadata = deltaClient + .putEmptyBody[Json](s"/files/$projectRef/$id?rev=1&tag=tag1", Writer, header) { expectOk } + + val assertMetadataUpdated = deltaClient.get[Json](s"/files/$projectRef/$id?tag=tag1", Writer) { + (json, response) => + response.status shouldEqual StatusCodes.OK + json.hcursor.get[Int]("_rev").rightValue shouldEqual 2 + json should have(nameField(updatedName)) + } + + (updateCustomMetadata >> assertMetadataUpdated).accepted + } } }