-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from aivanovski/feature/add-fake-file-for-aut…
…otests Add fake file handling for autotests
- Loading branch information
Showing
25 changed files
with
840 additions
and
253 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,9 @@ | |
project.properties | ||
/.project | ||
/tmp | ||
|
||
# Clojure | ||
.lsp/ | ||
.clj-kondo/ | ||
.cpcache/ | ||
.nrepl-port |
Binary file not shown.
10 changes: 10 additions & 0 deletions
10
app/src/debug/kotlin/com/ivanovsky/passnotes/data/repository/file/Extensions.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,10 @@ | ||
package com.ivanovsky.passnotes.data.repository.file | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.Locale | ||
|
||
private val DATE_FORMAT = SimpleDateFormat("yyyy-MM-dd", Locale.US) | ||
|
||
fun parseDate(str: String): Long { | ||
return DATE_FORMAT.parse(str)?.time ?: throw IllegalArgumentException() | ||
} |
192 changes: 192 additions & 0 deletions
192
app/src/debug/kotlin/com/ivanovsky/passnotes/data/repository/file/FakeFileContentFactory.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,192 @@ | ||
package com.ivanovsky.passnotes.data.repository.file | ||
|
||
import app.keemobile.kotpass.database.KeePassDatabase | ||
import app.keemobile.kotpass.database.encode | ||
import com.ivanovsky.passnotes.data.repository.file.databaseDsl.EntryEntity | ||
import com.ivanovsky.passnotes.data.repository.file.databaseDsl.GroupEntity | ||
import com.ivanovsky.passnotes.data.repository.file.databaseDsl.KotpassTreeDsl | ||
import java.io.ByteArrayOutputStream | ||
|
||
class FakeFileContentFactory { | ||
|
||
fun createDefaultLocalDatabase(): ByteArray { | ||
return KotpassTreeDsl.tree(ROOT) { | ||
group(GROUP_EMAIL) | ||
group(GROUP_INTERNET) { | ||
group(GROUP_CODING) { | ||
entry(ENTRY_LEETCODE) | ||
entry(ENTRY_NEETCODE) | ||
entry(ENTRY_GITHUB) | ||
} | ||
group(GROUP_GAMING) { | ||
entry(ENTRY_STADIA) | ||
} | ||
group(GROUP_SHOPPING) | ||
group(GROUP_SOCIAL) | ||
|
||
entry(ENTRY_GOOGLE) | ||
entry(ENTRY_APPLE) | ||
entry(ENTRY_MICROSOFT) | ||
} | ||
entry(ENTRY_NAS_LOGIN) | ||
entry(ENTRY_LAPTOP_LOGIN) | ||
entry(ENTRY_LOCAL) | ||
} | ||
.toByteArray() | ||
} | ||
|
||
fun createDefaultRemoteDatabase(): ByteArray { | ||
return KotpassTreeDsl.tree(ROOT) { | ||
group(GROUP_EMAIL) | ||
group(GROUP_INTERNET) { | ||
group(GROUP_CODING) { | ||
entry(ENTRY_LEETCODE) | ||
entry(ENTRY_NEETCODE) | ||
entry(ENTRY_GITLAB) | ||
} | ||
group(GROUP_GAMING) { | ||
entry(ENTRY_STADIA) | ||
} | ||
group(GROUP_SHOPPING) | ||
group(GROUP_SOCIAL) | ||
|
||
entry(ENTRY_GOOGLE) | ||
entry(ENTRY_APPLE) | ||
entry(ENTRY_MICROSOFT) | ||
} | ||
entry(ENTRY_NAS_LOGIN) | ||
entry(ENTRY_LAPTOP_LOGIN) | ||
entry(ENTRY_MAC_BOOK_LOGIN) | ||
entry(ENTRY_REMOTE) | ||
} | ||
.toByteArray() | ||
} | ||
|
||
private fun KeePassDatabase.toByteArray(): ByteArray { | ||
return ByteArrayOutputStream().use { out -> | ||
this.encode(out) | ||
out.toByteArray() | ||
} | ||
} | ||
|
||
companion object { | ||
private val ROOT = GroupEntity(title = "Database") | ||
private val GROUP_INTERNET = GroupEntity(title = "Internet") | ||
private val GROUP_EMAIL = GroupEntity(title = "Email") | ||
|
||
private val GROUP_CODING = GroupEntity(title = "Coding") | ||
private val GROUP_GAMING = GroupEntity(title = "Gaming") | ||
private val GROUP_SHOPPING = GroupEntity(title = "Shopping") | ||
private val GROUP_SOCIAL = GroupEntity(title = "Social") | ||
|
||
private val ENTRY_LOCAL = EntryEntity( | ||
title = "Local", | ||
username = "john.doe", | ||
password = "abc123", | ||
created = parseDate("2020-01-01"), | ||
modified = parseDate("2020-01-01") | ||
) | ||
|
||
private val ENTRY_REMOTE = EntryEntity( | ||
title = "Remote", | ||
username = "john.doe", | ||
password = "abc123", | ||
created = parseDate("2020-01-01"), | ||
modified = parseDate("2020-01-01") | ||
) | ||
|
||
private val ENTRY_NAS_LOGIN = EntryEntity( | ||
title = "My NAS Login", | ||
username = "john.doe", | ||
password = "abc123", | ||
created = parseDate("2020-01-01"), | ||
modified = parseDate("2020-01-01") | ||
) | ||
|
||
private val ENTRY_LAPTOP_LOGIN = EntryEntity( | ||
title = "My Laptop Login", | ||
username = "john.doe", | ||
password = "abc123", | ||
created = parseDate("2020-01-02"), | ||
modified = parseDate("2020-01-02") | ||
) | ||
|
||
private val ENTRY_MAC_BOOK_LOGIN = EntryEntity( | ||
title = "My Mac Book Login", | ||
username = "john.doe", | ||
password = "abc123", | ||
created = parseDate("2020-02-01"), | ||
modified = parseDate("2020-02-01") | ||
) | ||
|
||
private val ENTRY_GOOGLE = EntryEntity( | ||
title = "My Google Login", | ||
username = "[email protected]", | ||
password = "abc123", | ||
url = "https://google.com", | ||
created = parseDate("2020-01-03"), | ||
modified = parseDate("2020-01-03") | ||
) | ||
|
||
private val ENTRY_APPLE = EntryEntity( | ||
title = "My Apple Login", | ||
username = "[email protected]", | ||
password = "abc123", | ||
url = "https://apple.com", | ||
created = parseDate("2020-01-04"), | ||
modified = parseDate("2020-01-04") | ||
) | ||
|
||
private val ENTRY_MICROSOFT = EntryEntity( | ||
title = "My Microsoft Login", | ||
username = "[email protected]", | ||
password = "abc123", | ||
url = "https://microsoft.com", | ||
created = parseDate("2020-01-05"), | ||
modified = parseDate("2020-01-05") | ||
) | ||
|
||
private val ENTRY_LEETCODE = EntryEntity( | ||
title = "My LeetCode Login", | ||
username = "[email protected]", | ||
password = "abc123", | ||
url = "https://leetcode.com", | ||
created = parseDate("2020-01-06"), | ||
modified = parseDate("2020-01-06") | ||
) | ||
|
||
private val ENTRY_NEETCODE = EntryEntity( | ||
title = "My NeetCode Login", | ||
username = "[email protected]", | ||
url = "https://neetcode.io/practice", | ||
created = parseDate("2020-01-07"), | ||
modified = parseDate("2020-01-07") | ||
) | ||
|
||
private val ENTRY_GITHUB = EntryEntity( | ||
title = "My GitHub Login", | ||
username = "[email protected]", | ||
password = "abc123", | ||
url = "https://github.com", | ||
created = parseDate("2020-01-08"), | ||
modified = parseDate("2020-01-08") | ||
) | ||
|
||
private val ENTRY_GITLAB = EntryEntity( | ||
title = "My GitLab Login", | ||
username = "[email protected]", | ||
password = "abc123", | ||
url = "https://gitlab.com", | ||
created = parseDate("2020-01-08"), | ||
modified = parseDate("2020-01-08") | ||
) | ||
|
||
private val ENTRY_STADIA = EntryEntity( | ||
title = "My Stadia Login", | ||
username = "[email protected]", | ||
password = "abc123", | ||
created = parseDate("2020-01-09"), | ||
modified = parseDate("2020-01-09") | ||
) | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
app/src/debug/kotlin/com/ivanovsky/passnotes/data/repository/file/FakeFileOutputStream.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,54 @@ | ||
package com.ivanovsky.passnotes.data.repository.file | ||
|
||
import java.io.ByteArrayOutputStream | ||
import java.io.IOException | ||
import java.io.OutputStream | ||
import timber.log.Timber | ||
|
||
class FakeFileOutputStream( | ||
private val onFinished: (bytes: ByteArray) -> Unit | ||
) : OutputStream() { | ||
|
||
private val out = ByteArrayOutputStream() | ||
private var isFailed = false | ||
private var isClosed = false | ||
|
||
override fun write(b: Int) { | ||
throwIfInvalidState() | ||
|
||
try { | ||
out.write(b) | ||
} catch (exception: IOException) { | ||
Timber.d(exception) | ||
isFailed = true | ||
throw IOException(exception) | ||
} | ||
} | ||
|
||
override fun flush() { | ||
throwIfInvalidState() | ||
} | ||
|
||
override fun close() { | ||
if (isClosed || isFailed) { | ||
return | ||
} | ||
|
||
val bytes = try { | ||
out.toByteArray() | ||
} catch (exception: IOException) { | ||
Timber.d(exception) | ||
isFailed = true | ||
throw IOException(exception) | ||
} | ||
|
||
onFinished.invoke(bytes) | ||
} | ||
|
||
private fun throwIfInvalidState() { | ||
when { | ||
isFailed -> throw IOException("Invalid state: failed") | ||
isClosed -> throw IOException("Invalid state: closed") | ||
} | ||
} | ||
} |
Oops, something went wrong.