-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in-memory FileStore for unit tests (#1465)
Previously, we were using MockK to stub out the file store in unit tests of photo code. Replace it with a simple in-memory implementation so tests don't have to configure the test doubles.
- Loading branch information
Showing
4 changed files
with
88 additions
and
47 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/test/kotlin/com/terraformation/backend/file/InMemoryFileStore.kt
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,75 @@ | ||
package com.terraformation.backend.file | ||
|
||
import java.io.ByteArrayInputStream | ||
import java.io.InputStream | ||
import java.net.URI | ||
import java.nio.file.FileAlreadyExistsException | ||
import java.nio.file.NoSuchFileException | ||
import java.nio.file.Path | ||
import java.time.Instant | ||
import org.junit.jupiter.api.fail | ||
import org.springframework.http.MediaType | ||
|
||
class InMemoryFileStore(private val pathGenerator: PathGenerator? = null) : FileStore { | ||
private var counter = 0 | ||
private val files = mutableMapOf<URI, ByteArray>() | ||
private val deletedFiles = mutableSetOf<URI>() | ||
|
||
fun assertFileNotExists( | ||
url: URI, | ||
message: String = "$url was found in file store but shouldn't have been" | ||
) { | ||
if (url in files) { | ||
fail(message) | ||
} | ||
} | ||
|
||
fun assertFileExists(url: URI) { | ||
if (url !in files) { | ||
fail("$url not found in file store") | ||
} | ||
} | ||
|
||
fun assertFileWasDeleted(url: URI) { | ||
if (url !in deletedFiles) { | ||
fail("$url was not deleted from file store") | ||
} | ||
} | ||
|
||
override fun delete(url: URI) { | ||
files.remove(url) ?: throw NoSuchFileException("$url") | ||
deletedFiles.add(url) | ||
} | ||
|
||
override fun read(url: URI): SizedInputStream { | ||
val bytes = getFile(url) | ||
return SizedInputStream( | ||
ByteArrayInputStream(bytes), bytes.size.toLong(), MediaType.APPLICATION_OCTET_STREAM) | ||
} | ||
|
||
override fun size(url: URI): Long { | ||
return getFile(url).size.toLong() | ||
} | ||
|
||
override fun write(url: URI, contents: InputStream) { | ||
if (url in files) { | ||
throw FileAlreadyExistsException("$url") | ||
} | ||
|
||
files[url] = contents.readAllBytes() | ||
} | ||
|
||
override fun canAccept(url: URI): Boolean = true | ||
|
||
override fun getUrl(path: Path): URI = URI("file:///$path") | ||
|
||
override fun newUrl(timestamp: Instant, category: String, contentType: String): URI { | ||
return if (pathGenerator != null) { | ||
getUrl(pathGenerator.generatePath(timestamp, category, contentType)) | ||
} else { | ||
URI("file:///$timestamp/$category/${counter++}") | ||
} | ||
} | ||
|
||
private fun getFile(url: URI) = files[url] ?: throw NoSuchFileException("$url") | ||
} |
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
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