Skip to content
Permalink
Browse files
refactor: refactor test models (DEV-264) (#1975)
* refactor: start refactoring UploadFileRequest

* refactor: UploadFileRequest

* docs: document changes

* refactor: adjust tests to changed UploadFileRequest

* test: improve test coverage

* refactor: improve ChangeFileRequest

* refactor: use model in some more tests

* refactor: remove some code smells

* refactor: remove some code smells

* test: add test for custom values in message representation of FileModels

* refactor: tidy up, add more docstrings

* test: add tests for UpdateFileRequest

* Update FileModelsSpec.scala

* fix: update test meged from main, to fit new structure

* tests: prepare remaining test as stubs

* test: add remaining tests for FileModels

Co-authored-by: Ivan Subotic <400790+subotic@users.noreply.github.com>
  • Loading branch information
BalduinLandolt and subotic committed Jan 31, 2022
1 parent 65b88a2 commit 65952f963417d0a7f546d04b172ff768382feae9
@@ -442,12 +442,13 @@ class KnoraSipiIntegrationV2ITSpec

val jsonLdEntity = UploadFileRequest
.make(
fileType = FileType.StillImageFile,
internalFilename = uploadedFile.internalFilename,
fileType = FileType.StillImageFile(),
internalFilename = uploadedFile.internalFilename
)
.toJsonLd(
className = Some("ThingPicture"),
ontologyName = "anything"
)
.toJsonLd

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -511,10 +512,10 @@ class KnoraSipiIntegrationV2ITSpec
val uploadedFile: SipiUploadResponseEntry = sipiUploadResponse.uploadedFiles.head
uploadedFile.originalFilename should ===(trp88OriginalFilename)

// JSON describing the new image to Knora.
// JSON describing the new image to the API.
val jsonLdEntity = ChangeFileRequest
.make(
fileType = FileType.StillImageFile,
fileType = FileType.StillImageFile(),
internalFilename = uploadedFile.internalFilename,
resourceIri = stillImageResourceIri.get,
valueIri = stillImageFileValueIri.get,
@@ -567,7 +568,7 @@ class KnoraSipiIntegrationV2ITSpec
// JSON describing the new image to Knora.
val jsonLdEntity = ChangeFileRequest
.make(
fileType = FileType.StillImageFile,
fileType = FileType.StillImageFile(),
internalFilename = internalFilename,
resourceIri = stillImageResourceIri.get,
valueIri = stillImageFileValueIri.get,
@@ -605,12 +606,13 @@ class KnoraSipiIntegrationV2ITSpec
val jsonLdEntity =
UploadFileRequest
.make(
fileType = FileType.DocumentFile,
fileType = FileType.DocumentFile(),
internalFilename = uploadedFile.internalFilename
)
.toJsonLd(
className = Some("ThingDocument"),
internalFilename = uploadedFile.internalFilename,
ontologyName = "anything"
)
.toJsonLd

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -668,7 +670,7 @@ class KnoraSipiIntegrationV2ITSpec
// Update the value.
val jsonLdEntity = ChangeFileRequest
.make(
fileType = FileType.DocumentFile,
fileType = FileType.DocumentFile(),
internalFilename = uploadedFile.internalFilename,
resourceIri = pdfResourceIri.get,
valueIri = pdfValueIri.get,
@@ -721,12 +723,13 @@ class KnoraSipiIntegrationV2ITSpec

val jsonLdEntity = UploadFileRequest
.make(
fileType = FileType.DocumentFile,
fileType = FileType.DocumentFile(),
internalFilename = uploadedFile.internalFilename
)
.toJsonLd(
className = Some("ThingDocument"),
internalFilename = uploadedFile.internalFilename,
ontologyName = "anything"
)
.toJsonLd

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -754,7 +757,7 @@ class KnoraSipiIntegrationV2ITSpec
fileType = FileType.TextFile,
internalFilename = uploadedFile.internalFilename
)
.toJsonLd
.toJsonLd()

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -858,8 +861,8 @@ class KnoraSipiIntegrationV2ITSpec
// Create the resource in the API.

val jsonLdEntity = UploadFileRequest
.make(fileType = FileType.StillImageFile, internalFilename = uploadedFile.internalFilename)
.toJsonLd
.make(fileType = FileType.StillImageFile(), internalFilename = uploadedFile.internalFilename)
.toJsonLd()

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -884,7 +887,7 @@ class KnoraSipiIntegrationV2ITSpec

val jsonLdEntity = UploadFileRequest
.make(fileType = FileType.TextFile, internalFilename = uploadedFile.internalFilename)
.toJsonLd
.toJsonLd()

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -989,7 +992,7 @@ class KnoraSipiIntegrationV2ITSpec

val jsonLdEntity = UploadFileRequest
.make(fileType = FileType.TextFile, internalFilename = uploadedFile.internalFilename)
.toJsonLd
.toJsonLd()

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -1013,7 +1016,7 @@ class KnoraSipiIntegrationV2ITSpec

val jsonLdEntity = UploadFileRequest
.make(fileType = FileType.ArchiveFile, internalFilename = uploadedFile.internalFilename)
.toJsonLd
.toJsonLd()

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -1121,7 +1124,7 @@ class KnoraSipiIntegrationV2ITSpec

val jsonLdEntity = UploadFileRequest
.make(fileType = FileType.ArchiveFile, internalFilename = uploadedFile.internalFilename)
.toJsonLd
.toJsonLd()

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -1180,7 +1183,7 @@ class KnoraSipiIntegrationV2ITSpec
// Create the resource in the API.
val jsonLdEntity = UploadFileRequest
.make(fileType = FileType.AudioFile, internalFilename = uploadedFile.internalFilename)
.toJsonLd
.toJsonLd()

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -1285,8 +1288,8 @@ class KnoraSipiIntegrationV2ITSpec

// Create the resource in the API.
val jsonLdEntity = UploadFileRequest
.make(fileType = FileType.MovingImageFile, internalFilename = uploadedFile.internalFilename)
.toJsonLd
.make(fileType = FileType.MovingImageFile(), internalFilename = uploadedFile.internalFilename)
.toJsonLd()

val request = Post(
s"$baseApiUrl/v2/resources",
@@ -1344,7 +1347,7 @@ class KnoraSipiIntegrationV2ITSpec

val jsonLdEntity = ChangeFileRequest
.make(
fileType = FileType.MovingImageFile,
fileType = FileType.MovingImageFile(),
resourceIri = videoResourceIri.get,
internalFilename = uploadedFile.internalFilename,
valueIri = videoValueIri.get
@@ -5,62 +5,179 @@

package org.knora.webapi.models.filemodels

import org.knora.webapi.ApiV2Complex
import org.knora.webapi.messages.{OntologyConstants, SmartIri, StringFormatter}
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.v2.responder.valuemessages.{
ArchiveFileValueContentV2,
AudioFileValueContentV2,
DocumentFileValueContentV2,
FileValueContentV2,
FileValueV2,
MovingImageFileValueContentV2,
StillImageFileValueContentV2,
TextFileValueContentV2
}

object FileModelUtil {
private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance

def getFileRepresentationClassIri(fileType: FileType): SmartIri = fileType match {
case FileType.DocumentFile => OntologyConstants.KnoraApiV2Complex.DocumentRepresentation.toSmartIri
case FileType.StillImageFile => OntologyConstants.KnoraApiV2Complex.StillImageRepresentation.toSmartIri
case FileType.MovingImageFile => OntologyConstants.KnoraApiV2Complex.MovingImageRepresentation.toSmartIri
case FileType.TextFile => OntologyConstants.KnoraApiV2Complex.TextRepresentation.toSmartIri
case FileType.AudioFile => OntologyConstants.KnoraApiV2Complex.AudioRepresentation.toSmartIri
case FileType.ArchiveFile => OntologyConstants.KnoraApiV2Complex.ArchiveRepresentation.toSmartIri
case FileType.DocumentFile(_, _, _) => OntologyConstants.KnoraApiV2Complex.DocumentRepresentation.toSmartIri
case FileType.StillImageFile(_, _) => OntologyConstants.KnoraApiV2Complex.StillImageRepresentation.toSmartIri
case FileType.MovingImageFile(_, _) => OntologyConstants.KnoraApiV2Complex.MovingImageRepresentation.toSmartIri
case FileType.TextFile => OntologyConstants.KnoraApiV2Complex.TextRepresentation.toSmartIri
case FileType.AudioFile => OntologyConstants.KnoraApiV2Complex.AudioRepresentation.toSmartIri
case FileType.ArchiveFile => OntologyConstants.KnoraApiV2Complex.ArchiveRepresentation.toSmartIri
}

def getFileRepresentationPropertyIri(fileType: FileType): SmartIri = fileType match {
case FileType.DocumentFile => OntologyConstants.KnoraApiV2Complex.HasDocumentFileValue.toSmartIri
case FileType.StillImageFile => OntologyConstants.KnoraApiV2Complex.HasStillImageFileValue.toSmartIri
case FileType.MovingImageFile => OntologyConstants.KnoraApiV2Complex.HasMovingImageFileValue.toSmartIri
case FileType.TextFile => OntologyConstants.KnoraApiV2Complex.HasTextFileValue.toSmartIri
case FileType.AudioFile => OntologyConstants.KnoraApiV2Complex.HasAudioFileValue.toSmartIri
case FileType.ArchiveFile => OntologyConstants.KnoraApiV2Complex.HasArchiveFileValue.toSmartIri
case FileType.DocumentFile(_, _, _) => OntologyConstants.KnoraApiV2Complex.HasDocumentFileValue.toSmartIri
case FileType.StillImageFile(_, _) => OntologyConstants.KnoraApiV2Complex.HasStillImageFileValue.toSmartIri
case FileType.MovingImageFile(_, _) => OntologyConstants.KnoraApiV2Complex.HasMovingImageFileValue.toSmartIri
case FileType.TextFile => OntologyConstants.KnoraApiV2Complex.HasTextFileValue.toSmartIri
case FileType.AudioFile => OntologyConstants.KnoraApiV2Complex.HasAudioFileValue.toSmartIri
case FileType.ArchiveFile => OntologyConstants.KnoraApiV2Complex.HasArchiveFileValue.toSmartIri
}

def getFileValuePropertyName(fileType: FileType): String = fileType match {
case FileType.DocumentFile => "knora-api:hasDocumentFileValue"
case FileType.StillImageFile => "knora-api:hasStillImageFileValue"
case FileType.MovingImageFile => "knora-api:hasMovingImageFileValue"
case FileType.TextFile => "knora-api:hasTextFileValue"
case FileType.AudioFile => "knora-api:hasAudioFileValue"
case FileType.ArchiveFile => "knora-api:hasArchiveFileValue"
case FileType.DocumentFile(_, _, _) => "knora-api:hasDocumentFileValue"
case FileType.StillImageFile(_, _) => "knora-api:hasStillImageFileValue"
case FileType.MovingImageFile(_, _) => "knora-api:hasMovingImageFileValue"
case FileType.TextFile => "knora-api:hasTextFileValue"
case FileType.AudioFile => "knora-api:hasAudioFileValue"
case FileType.ArchiveFile => "knora-api:hasArchiveFileValue"
}

def getDefaultClassName(fileType: FileType): String = fileType match {
case FileType.DocumentFile => "DocumentRepresentation"
case FileType.StillImageFile => "StillImageRepresentation"
case FileType.MovingImageFile => "MovingImageRepresentation"
case FileType.TextFile => "TextRepresentation"
case FileType.AudioFile => "AudioRepresentation"
case FileType.ArchiveFile => "ArchiveRepresentation"
case FileType.DocumentFile(_, _, _) => "DocumentRepresentation"
case FileType.StillImageFile(_, _) => "StillImageRepresentation"
case FileType.MovingImageFile(_, _) => "MovingImageRepresentation"
case FileType.TextFile => "TextRepresentation"
case FileType.AudioFile => "AudioRepresentation"
case FileType.ArchiveFile => "ArchiveRepresentation"
}

def getFileValueType(fileType: FileType): String = fileType match {
case FileType.DocumentFile => "knora-api:DocumentFileValue"
case FileType.StillImageFile => "knora-api:StillImageFileValue"
case FileType.MovingImageFile => "knora-api:MovingImageFileValue"
case FileType.TextFile => "knora-api:TextFileValue"
case FileType.AudioFile => "knora-api:AudioFileValue"
case FileType.ArchiveFile => "knora-api:ArchiveFileValue"
case FileType.DocumentFile(_, _, _) => "knora-api:DocumentFileValue"
case FileType.StillImageFile(_, _) => "knora-api:StillImageFileValue"
case FileType.MovingImageFile(_, _) => "knora-api:MovingImageFileValue"
case FileType.TextFile => "knora-api:TextFileValue"
case FileType.AudioFile => "knora-api:AudioFileValue"
case FileType.ArchiveFile => "knora-api:ArchiveFileValue"
}

def getFileValueTypeIRI(fileType: FileType): SmartIri = fileType match {
case FileType.DocumentFile(_, _, _) => OntologyConstants.KnoraApiV2Complex.DocumentFileValue.toSmartIri
case FileType.StillImageFile(_, _) => OntologyConstants.KnoraApiV2Complex.StillImageFileValue.toSmartIri
case FileType.MovingImageFile(_, _) => OntologyConstants.KnoraApiV2Complex.MovingImageFileValue.toSmartIri
case FileType.TextFile => OntologyConstants.KnoraApiV2Complex.TextFileValue.toSmartIri
case FileType.AudioFile => OntologyConstants.KnoraApiV2Complex.AudioFileValue.toSmartIri
case FileType.ArchiveFile => OntologyConstants.KnoraApiV2Complex.AudioFileValue.toSmartIri
}

def getJsonLdContext(ontology: String): String = {
def getFileValueContent(
fileType: FileType,
internalFilename: String,
internalMimeType: Option[String],
originalFilename: Option[String],
originalMimeType: Option[String],
comment: Option[String]
): FileValueContentV2 =
fileType match {
case FileType.DocumentFile(pageCount, dimX, dimY) =>
DocumentFileValueContentV2(
ontologySchema = ApiV2Complex,
fileValue = FileValueV2(
internalFilename = internalFilename,
internalMimeType = internalMimeType match {
case Some(v) => v
case None => "application/pdf"
},
originalFilename = originalFilename,
originalMimeType = originalMimeType match {
case Some(_) => originalMimeType
case None => Some("application/pdf")
}
),
pageCount = pageCount,
dimX = dimX,
dimY = dimY,
comment = comment
)
case FileType.StillImageFile(dimX, dimY) =>
StillImageFileValueContentV2(
ontologySchema = ApiV2Complex,
fileValue = FileValueV2(
internalFilename = internalFilename,
internalMimeType = internalMimeType match {
case Some(v) => v
case None => "image/jp2"
},
originalFilename = originalFilename,
originalMimeType = originalMimeType
),
dimX = dimX,
dimY = dimY,
comment = comment
)
case FileType.MovingImageFile(dimX, dimY) =>
MovingImageFileValueContentV2(
ontologySchema = ApiV2Complex,
fileValue = FileValueV2(
internalFilename = internalFilename,
internalMimeType = internalMimeType.get,
originalFilename = originalFilename,
originalMimeType = internalMimeType
),
dimX = dimX,
dimY = dimY
)
case FileType.TextFile =>
TextFileValueContentV2(
ontologySchema = ApiV2Complex,
fileValue = FileValueV2(
internalFilename = internalFilename,
internalMimeType = internalMimeType.get,
originalFilename = originalFilename,
originalMimeType = internalMimeType
)
)
case FileType.AudioFile =>
AudioFileValueContentV2(
ontologySchema = ApiV2Complex,
fileValue = FileValueV2(
internalFilename = internalFilename,
internalMimeType = internalMimeType.get,
originalFilename = originalFilename,
originalMimeType = internalMimeType
)
)
case FileType.ArchiveFile =>
ArchiveFileValueContentV2(
ontologySchema = ApiV2Complex,
fileValue = FileValueV2(
internalFilename = internalFilename,
internalMimeType = internalMimeType match {
case Some(v) => v
case None => "application/zip"
},
originalFilename = originalFilename,
originalMimeType = internalMimeType
),
comment = comment
)
}

def getJsonLdContext(ontology: String, ontologyIRI: Option[String] = None): String = {
val ontologies = ontology match {
case "anything" =>
FileModelConstants.defaultJsonLdContextMap + ("anything" -> "http://0.0.0.0:3333/ontology/0001/anything/v2#")
case _ => FileModelConstants.defaultJsonLdContextMap
case prefix =>
ontologyIRI match {
case Some(iri) => FileModelConstants.defaultJsonLdContextMap + (prefix -> iri)
case None => FileModelConstants.defaultJsonLdContextMap
}
}
val lines = ontologies.toList
.map(x => s""" "${x._1}": "${x._2}\"""")
@@ -94,9 +211,13 @@ object FileModelConstants {

sealed trait FileType
object FileType {
case object DocumentFile extends FileType
case object StillImageFile extends FileType
case object MovingImageFile extends FileType
case class DocumentFile(
pageCount: Option[Int] = Some(1),
dimX: Option[Int] = Some(100),
dimY: Option[Int] = Some(100)
) extends FileType
case class StillImageFile(dimX: Int = 100, dimY: Int = 100) extends FileType
case class MovingImageFile(dimX: Int = 100, dimY: Int = 100) extends FileType
case object TextFile extends FileType
case object AudioFile extends FileType
case object ArchiveFile extends FileType

0 comments on commit 65952f9

Please sign in to comment.