Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
webapi/src/test/scala/org/knora/webapi/slice/admin/domain/service/ZipUtilitySpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.knora.webapi.slice.admin.domain.service | ||
|
||
import zio._ | ||
import zio.nio.file._ | ||
import zio.test._ | ||
|
||
object ZipUtilitySpec extends ZIOSpecDefault { | ||
private val testFolderName = "test-folder" | ||
private val testFileName = "test-file.txt" | ||
private val testFileContent = List("This is a test file.", "Content is not important.") | ||
|
||
private val createTestDirectory = for { | ||
tmp <- Files.createTempDirectoryScoped(Some("tmp"), fileAttributes = Nil).logError | ||
_ <- Files.createDirectory(tmp / testFolderName) | ||
_ <- Files.createFile(tmp / testFolderName / testFileName) | ||
_ <- Files.writeLines(tmp / testFolderName / testFileName, testFileContent) | ||
} yield tmp | ||
|
||
private def verifyUnzipped(path: Path) = for { | ||
folderExists <- Files.isDirectory(path / testFolderName) | ||
_ <- ZIO.logError(s"Folder does not exist: ${path / testFolderName}").when(!folderExists) | ||
expectedFile = path / testFolderName / testFileName | ||
fileContentIsCorrect <- | ||
Files.isRegularFile(expectedFile) && Files.readAllLines(expectedFile).map(_ == testFileContent) | ||
_ <- ZIO.logError(s"File content $expectedFile is not correct").when(!fileContentIsCorrect) | ||
} yield folderExists && fileContentIsCorrect | ||
|
||
override val spec: Spec[Scope, Throwable] = suite("ZipUtility")( | ||
test("creating the test directory should work") { | ||
for { | ||
tmp <- createTestDirectory | ||
createdCorrectly <- verifyUnzipped(tmp) | ||
} yield assertTrue(createdCorrectly) | ||
}, | ||
test("should zip/unzip a folder with a single file") { | ||
for { | ||
zipThis <- createTestDirectory | ||
tmpZipped <- Files.createTempDirectoryScoped(Some("zipped"), fileAttributes = Nil) | ||
zipped <- ZipUtility.zipFolder(zipThis, tmpZipped) | ||
tmpUnzipped <- Files.createTempDirectoryScoped(Some("unzipped"), fileAttributes = Nil) | ||
unzipped <- ZipUtility.unzipFile(zipped, tmpUnzipped) | ||
result <- verifyUnzipped(unzipped) | ||
} yield assertTrue(result) | ||
} | ||
) | ||
} |