|
1 | 1 | package com.smarttoolfactory.data.repository
|
2 | 2 |
|
| 3 | +import com.google.common.truth.Truth |
| 4 | +import com.smarttoolfactory.data.mapper.DTOtoEntityMapper |
| 5 | +import com.smarttoolfactory.data.model.PostDTO |
| 6 | +import com.smarttoolfactory.data.model.PostEntity |
| 7 | +import com.smarttoolfactory.data.source.LocalPostDataSourceCoroutines |
| 8 | +import com.smarttoolfactory.data.source.RemotePostDataSourceCoroutines |
| 9 | +import com.smarttoolfactory.test_utils.RESPONSE_JSON_PATH |
| 10 | +import com.smarttoolfactory.test_utils.util.convertFromJsonToListOf |
| 11 | +import com.smarttoolfactory.test_utils.util.getResourceAsText |
| 12 | +import io.mockk.clearMocks |
| 13 | +import io.mockk.coEvery |
| 14 | +import io.mockk.coVerify |
| 15 | +import io.mockk.coVerifyOrder |
| 16 | +import io.mockk.every |
| 17 | +import io.mockk.just |
| 18 | +import io.mockk.mockk |
| 19 | +import io.mockk.runs |
| 20 | +import io.mockk.verify |
| 21 | +import kotlinx.coroutines.test.runBlockingTest |
3 | 22 | import org.junit.jupiter.api.AfterEach
|
4 | 23 | import org.junit.jupiter.api.BeforeEach
|
| 24 | +import org.junit.jupiter.api.Test |
5 | 25 | import org.junit.jupiter.api.TestInstance
|
6 | 26 |
|
7 | 27 | @TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
8 | | -internal class PostRepositoryCoroutinesTest { |
| 28 | +class PostRepositoryCoroutinesTest { |
| 29 | + |
| 30 | + companion object { |
| 31 | + |
| 32 | + val postDTOList = |
| 33 | + convertFromJsonToListOf<PostDTO>(getResourceAsText(RESPONSE_JSON_PATH))!! |
| 34 | + |
| 35 | + val postEntityList = |
| 36 | + convertFromJsonToListOf<PostEntity>(getResourceAsText(RESPONSE_JSON_PATH))!! |
| 37 | + } |
| 38 | + |
| 39 | + private lateinit var repository: PostRepositoryCoroutinesImpl |
| 40 | + |
| 41 | + private val localPostDataSource: LocalPostDataSourceCoroutines = mockk() |
| 42 | + private val remotePostDataSource: RemotePostDataSourceCoroutines = mockk() |
| 43 | + private val mapper: DTOtoEntityMapper = mockk() |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `given network error occurred, should throw Exception`() = runBlockingTest { |
| 47 | + |
| 48 | + // GIVEN |
| 49 | + coEvery { remotePostDataSource.getPostDTOs() } throws Exception("Network Exception") |
| 50 | + every { mapper.map(postDTOList) } returns postEntityList |
| 51 | + |
| 52 | + // WHEN |
| 53 | + val expected = try { |
| 54 | + remotePostDataSource.getPostDTOs() |
| 55 | + } catch (e: Exception) { |
| 56 | + e |
| 57 | + } |
| 58 | + |
| 59 | + // THEN |
| 60 | + Truth.assertThat(expected).isInstanceOf(Exception::class.java) |
| 61 | + coVerify { remotePostDataSource.getPostDTOs() } |
| 62 | + verify(exactly = 0) { mapper.map(postDTOList) } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `given PostDTO list returned from remote source, should return postEntity list`() = |
| 67 | + runBlockingTest { |
| 68 | + // GIVEN |
| 69 | + val actual = postEntityList |
| 70 | + coEvery { remotePostDataSource.getPostDTOs() } returns postDTOList |
| 71 | + every { mapper.map(postDTOList) } returns postEntityList |
| 72 | + |
| 73 | + // WHEN |
| 74 | + val expected = repository.fetchEntitiesFromRemote() |
| 75 | + |
| 76 | + // THEN |
| 77 | + Truth.assertThat(expected).isEqualTo(actual) |
| 78 | + // Check for order of call either |
| 79 | + coVerifyOrder { |
| 80 | + remotePostDataSource.getPostDTOs() |
| 81 | + mapper.map(postDTOList) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `given DB is empty should return an empty list`() = runBlockingTest { |
| 87 | + |
| 88 | + // GIVEN |
| 89 | + val expected = listOf<PostEntity>() |
| 90 | + coEvery { localPostDataSource.getPostEntities() } returns expected |
| 91 | + |
| 92 | + // WHEN |
| 93 | + val actual = repository.getPostEntitiesFromLocal() |
| 94 | + |
| 95 | + // THEN |
| 96 | + Truth.assertThat(actual).isEmpty() |
| 97 | + coVerify(exactly = 1) { localPostDataSource.getPostEntities() } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `given DB is populated should return data list`() = runBlockingTest { |
| 102 | + |
| 103 | + // GIVEN |
| 104 | + coEvery { localPostDataSource.getPostEntities() } returns postEntityList |
| 105 | + |
| 106 | + // WHEN |
| 107 | + val actual = repository.getPostEntitiesFromLocal() |
| 108 | + |
| 109 | + // THEN |
| 110 | + Truth.assertThat(actual) |
| 111 | + .containsExactlyElementsIn(postEntityList) |
| 112 | + coVerify(exactly = 1) { localPostDataSource.getPostEntities() } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `given entities, should save entities to DB`() = runBlockingTest { |
| 117 | + |
| 118 | + // GIVEN |
| 119 | + val idList = postEntityList.map { |
| 120 | + it.id.toLong() |
| 121 | + } |
| 122 | + |
| 123 | + coEvery { localPostDataSource.saveEntities(postEntityList) } returns idList |
| 124 | + |
| 125 | + // WHEN |
| 126 | + repository.savePostEntities(postEntityList) |
| 127 | + |
| 128 | + // THEN |
| 129 | + coVerify(exactly = 1) { localPostDataSource.saveEntities(postEntityList) } |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun `given no error should delete entities`() = runBlockingTest { |
| 134 | + |
| 135 | + // GIVEN |
| 136 | + coEvery { localPostDataSource.deletePostEntities() } just runs |
| 137 | + |
| 138 | + // WHEN |
| 139 | + repository.deletePostEntities() |
| 140 | + |
| 141 | + // THEN |
| 142 | + coVerify(exactly = 1) { localPostDataSource.deletePostEntities() } |
| 143 | + } |
9 | 144 |
|
10 | 145 | @BeforeEach
|
11 | 146 | fun setUp() {
|
| 147 | + repository = |
| 148 | + PostRepositoryCoroutinesImpl(localPostDataSource, remotePostDataSource, mapper) |
12 | 149 | }
|
13 | 150 |
|
14 | 151 | @AfterEach
|
15 | 152 | fun tearDown() {
|
| 153 | + clearMocks(localPostDataSource, remotePostDataSource, mapper) |
16 | 154 | }
|
17 | 155 | }
|
0 commit comments