-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
932deb6
commit 1e898c4
Showing
7 changed files
with
164 additions
and
24 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
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/concept/stc/coroutines/DispatchersProvider.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,19 @@ | ||
package concept.stc.coroutines | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* Provides coroutines dispatchers. | ||
* This class allows to replace background dispatchers with test dispatchers | ||
* for unit and integration testing. | ||
*/ | ||
@Component | ||
class DispatchersProvider { | ||
|
||
/** | ||
* Get the IO dispatcher. | ||
*/ | ||
val io: CoroutineDispatcher get() = Dispatchers.IO | ||
} |
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
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/concept/stc/data/local/MovieCrudRepository.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,21 @@ | ||
package concept.stc.data.local | ||
|
||
import concept.stc.data.local.entity.MovieEntity | ||
import org.springframework.data.r2dbc.repository.Query | ||
import org.springframework.data.repository.kotlin.CoroutineCrudRepository | ||
|
||
/** | ||
* Repository to manage database operations for [MovieEntity]. | ||
*/ | ||
interface MovieCrudRepository : CoroutineCrudRepository<MovieEntity, Int> { | ||
|
||
/** | ||
* Find movie by IMDB ID. | ||
* | ||
* @param imdbId the IMDB ID. | ||
* | ||
* @return the movie entity or null if not found. | ||
*/ | ||
@Query("SELECT * FROM movies WHERE imdb_id = :imdbId") | ||
suspend fun getMovieByImdbId(imdbId: String): MovieEntity? | ||
} |
This file was deleted.
Oops, something went wrong.
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,86 @@ | ||
package concept.stc.data | ||
|
||
import concept.stc.coroutines.DispatchersProvider | ||
import concept.stc.data.local.MovieCrudRepository | ||
import concept.stc.data.mapper.toEntity | ||
import concept.stc.data.remote.ApiClient | ||
import concept.stc.data.remote.model.SearchResponse | ||
import io.mockk.Called | ||
import io.mockk.clearMocks | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Test | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class ApiServiceTest { | ||
|
||
private val apiClient = mockk<ApiClient>() | ||
private val repository = mockk<MovieCrudRepository>() | ||
private val dispatchers = mockk<DispatchersProvider> { | ||
coEvery { io } returns UnconfinedTestDispatcher() | ||
} | ||
|
||
private val service = ApiService(apiClient, repository, dispatchers) | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
clearMocks(apiClient, repository) | ||
} | ||
|
||
@Test | ||
fun `when load movies, given API response, then save them to database`() = runTest { | ||
// Given | ||
val movie = _movie.copy(imdbID = "testId") | ||
val searchResponse = _searchResponse.copy(movies = listOf(movie)) | ||
|
||
coEvery { apiClient.search(any()) } returns searchResponse | ||
coEvery { repository.getMovieByImdbId(any()) } returns null | ||
coEvery { repository.save(any()) } returns movie.toEntity() | ||
|
||
// When | ||
service.loadMovies("test") | ||
|
||
// Then | ||
coVerify { apiClient.search("test") } | ||
coVerify { repository.getMovieByImdbId("testId") } | ||
coVerify { repository.save(movie.toEntity()) } | ||
} | ||
|
||
@Test | ||
fun `when load movies, given movie is saved already, then should not save it`() = runTest { | ||
// Given | ||
val movie = _movie.copy(imdbID = "testId") | ||
val searchResponse = _searchResponse.copy(movies = listOf(movie)) | ||
|
||
coEvery { apiClient.search(any()) } returns searchResponse | ||
coEvery { repository.getMovieByImdbId(any()) } returns movie.toEntity() | ||
coEvery { repository.save(any()) } returns movie.toEntity() | ||
|
||
// When | ||
service.loadMovies("test") | ||
|
||
// Then | ||
coVerify { apiClient.search("test") } | ||
coVerify { repository.getMovieByImdbId("testId") } | ||
coVerify { repository.save(movie.toEntity()) wasNot Called } | ||
} | ||
|
||
private val _movie = SearchResponse.Movie( | ||
title = "", | ||
year = "", | ||
imdbID = "", | ||
type = "", | ||
poster = "" | ||
) | ||
|
||
private val _searchResponse = SearchResponse( | ||
movies = emptyList(), | ||
totalResults = 0, | ||
response = "" | ||
) | ||
} |
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