Skip to content

Commit

Permalink
refactor: Use java.nio.file.Path instead of java.io.File (DSP-1124) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Geer committed Dec 7, 2020
1 parent b9ad8de commit 74223d0
Show file tree
Hide file tree
Showing 39 changed files with 843 additions and 799 deletions.
10 changes: 5 additions & 5 deletions webapi/src/it/scala/org/knora/webapi/ITKnoraLiveSpec.scala
Expand Up @@ -19,7 +19,7 @@

package org.knora.webapi

import java.io.File
import java.nio.file.{Files, Path, Paths}

import akka.actor.{ActorRef, ActorSystem, Props}
import akka.event.LoggingAdapter
Expand Down Expand Up @@ -228,13 +228,13 @@ class ITKnoraLiveSpec(_system: ActorSystem)
// Make a multipart/form-data request containing the files.

val formDataParts: Seq[Multipart.FormData.BodyPart] = filesToUpload.map { fileToUpload =>
val fileToSend = new File(fileToUpload.path)
assert(fileToSend.exists(), s"File ${fileToUpload.path} does not exist")
val fileToSend: Path = Paths.get(fileToUpload.path)
assert(Files.exists(fileToSend), s"File ${fileToUpload.path} does not exist")

Multipart.FormData.BodyPart(
"file",
HttpEntity.fromPath(fileToUpload.mimeType, fileToSend.toPath),
Map("filename" -> fileToSend.getName)
HttpEntity.fromPath(fileToUpload.mimeType, fileToSend),
Map("filename" -> fileToSend.getFileName.toString)
)
}

Expand Down
Expand Up @@ -19,8 +19,8 @@

package org.knora.webapi.e2e.v1

import java.io.{File, FileInputStream, FileOutputStream}
import java.net.URLEncoder
import java.nio.file.{Files, Paths, StandardCopyOption}

import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers._
Expand Down Expand Up @@ -298,24 +298,17 @@ class KnoraSipiIntegrationV1ITSpec
}

"create an 'p0803-incunabula:book' and an 'p0803-incunabula:page' with file parameters via XML import" in {
val fileToUpload = new File(pathToChlaus)
val fileToUpload = Paths.get(pathToChlaus)

// To be able to run packaged tests inside Docker, we need to copy
// the file first to a place which is shared with sipi
val dest = FileUtil.createTempFile(settings, Some("jpg"))
new FileOutputStream(dest).getChannel
.transferFrom(
new FileInputStream(fileToUpload).getChannel,
0,
Long.MaxValue
)

val absoluteFilePath = dest.getAbsolutePath
Files.copy(fileToUpload, dest, StandardCopyOption.REPLACE_EXISTING)

// Upload the image to Sipi.
val sipiUploadResponse: SipiUploadResponse = uploadToSipi(
loginToken = loginToken,
filesToUpload = Seq(FileToUpload(path = absoluteFilePath, mimeType = MediaTypes.`image/tiff`))
filesToUpload = Seq(FileToUpload(path = dest.toAbsolutePath.toString, mimeType = MediaTypes.`image/tiff`))
)

val uploadedFile: SipiUploadResponseEntry = sipiUploadResponse.uploadedFiles.head
Expand Down Expand Up @@ -370,7 +363,7 @@ class KnoraSipiIntegrationV1ITSpec
val locdata = pageJson.fields("resinfo").asJsObject.fields("locdata").asJsObject
val origname = locdata.fields("origname").asInstanceOf[JsString].value
val imageUrl = locdata.fields("path").asInstanceOf[JsString].value
assert(origname == dest.getName)
assert(origname == dest.getFileName.toString)

// Request the file from Sipi.
val sipiGetRequest = Get(imageUrl) ~> addCredentials(BasicHttpCredentials(userEmail, password))
Expand Down Expand Up @@ -414,7 +407,7 @@ class KnoraSipiIntegrationV1ITSpec

// add a mapping referring to the XSLT as the default XSL transformation

val mapping = FileUtil.readTextFile(new File(pathToMappingWithXSLT))
val mapping = FileUtil.readTextFile(Paths.get(pathToMappingWithXSLT))

val updatedMapping = addXSLTIriToMapping(mapping, resId)

Expand Down Expand Up @@ -449,7 +442,7 @@ class KnoraSipiIntegrationV1ITSpec
}

"create a sample BEOL letter" in {
val mapping = FileUtil.readTextFile(new File(pathToBEOLLetterMapping))
val mapping = FileUtil.readTextFile(Paths.get(pathToBEOLLetterMapping))

val paramsForMapping =
s"""
Expand Down Expand Up @@ -481,7 +474,7 @@ class KnoraSipiIntegrationV1ITSpec

// create a letter via bulk import

val bulkXML = FileUtil.readTextFile(new File(pathToBEOLBulkXML))
val bulkXML = FileUtil.readTextFile(Paths.get(pathToBEOLBulkXML))

val bulkRequest = Post(
baseApiUrl + "/v1/resources/xmlimport/" + URLEncoder.encode("http://rdfh.ch/projects/yTerZGyxjZVqFMNNKXCDPF",
Expand Down Expand Up @@ -532,7 +525,7 @@ class KnoraSipiIntegrationV1ITSpec

// add a mapping referring to the XSLT as the default XSL transformation

val mapping = FileUtil.readTextFile(new File(pathToBEOLStandoffTEIMapping))
val mapping = FileUtil.readTextFile(Paths.get(pathToBEOLStandoffTEIMapping))

val updatedMapping = addXSLTIriToMapping(mapping, resId)

Expand Down

0 comments on commit 74223d0

Please sign in to comment.