RestApiWorkerTest.kt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package net.folivo.android.smsGatewayServer
  2. import android.content.ContentResolver
  3. import android.database.MatrixCursor
  4. import android.net.Uri
  5. import android.provider.OpenableColumns
  6. import io.kotest.matchers.shouldBe
  7. import io.mockk.every
  8. import io.mockk.mockk
  9. import org.junit.Test
  10. import org.junit.runner.RunWith
  11. import org.robolectric.RobolectricTestRunner
  12. import java.security.KeyStoreException
  13. @RunWith(RobolectricTestRunner::class)
  14. class RestApiWorkerTest {
  15. //getKeyStoreFileName
  16. @Test
  17. fun `when there is a file path starts with "content" and includes not the file name, it should get the name`() {
  18. val testUri = Uri.parse("content://com.android.providers.downloads.documents/document/msf%3A1035")
  19. val contentResolverMock = mockk<ContentResolver>()
  20. val matrixCursor = MatrixCursor(arrayOf(OpenableColumns.DISPLAY_NAME))
  21. matrixCursor.addRow(arrayOf("certificate.p12"))
  22. every {
  23. contentResolverMock.query(
  24. testUri,
  25. null,
  26. null,
  27. null,
  28. null
  29. )
  30. } returns matrixCursor
  31. RestApiWorker.getKeyStoreFileName(contentResolverMock, testUri).shouldBe("certificate")
  32. }
  33. @Test
  34. fun `when there is a file path starts with "content" and includes the file name, it should get the name`() {
  35. val testUri = Uri.parse("content://com.android.providers.downloads.documents/document/certificate.p12")
  36. val contentResolverMock = mockk<ContentResolver>()
  37. val matrixCursor = MatrixCursor(arrayOf(OpenableColumns.DISPLAY_NAME))
  38. matrixCursor.addRow(arrayOf("certificate.p12"))
  39. every {
  40. contentResolverMock.query(
  41. testUri,
  42. null,
  43. null,
  44. null,
  45. null
  46. )
  47. } returns matrixCursor
  48. RestApiWorker.getKeyStoreFileName(contentResolverMock, testUri).shouldBe("certificate")
  49. }
  50. @Test
  51. fun `when there is a file path starts not with "content" and includes the file name, it should get the name`() {
  52. val testUri = Uri.parse("/storage/emulated/0/Download/certificate.p12")
  53. val contentResolverMock = mockk<ContentResolver>()
  54. val matrixCursor = MatrixCursor(arrayOf(OpenableColumns.DISPLAY_NAME))
  55. matrixCursor.addRow(arrayOf("certificate.p12"))
  56. every {
  57. contentResolverMock.query(
  58. testUri,
  59. null,
  60. null,
  61. null,
  62. null
  63. )
  64. } returns matrixCursor
  65. RestApiWorker.getKeyStoreFileName(contentResolverMock, testUri).shouldBe("certificate")
  66. }
  67. @Test(expected = KeyStoreException::class)
  68. fun `when there is an empty file path, it should throw a KeyStoreException`(){
  69. val testUri = Uri.parse("")
  70. val contentResolverMock = mockk<ContentResolver>()
  71. every {
  72. contentResolverMock.query(
  73. testUri,
  74. null,
  75. null,
  76. null,
  77. null
  78. )
  79. } returns null
  80. RestApiWorker.getKeyStoreFileName(contentResolverMock, testUri)
  81. }
  82. }