-
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
ba694e0
commit f8d1f40
Showing
5 changed files
with
336 additions
and
4 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
28 changes: 28 additions & 0 deletions
28
...re/stocks/src/test/kotlin/br/com/stonks/feature/stocks/domain/types/StockAlertTypeTest.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,28 @@ | ||
package br.com.stonks.feature.stocks.domain.types | ||
|
||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
|
||
@RunWith(Parameterized::class) | ||
internal class StockAlertTypeTest( | ||
private val input: String, | ||
private val expected: StockAlertType, | ||
) { | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters | ||
fun arguments(): List<Array<Any>> = listOf( | ||
arrayOf("unknown", StockAlertType.UNKNOWN), | ||
arrayOf("high", StockAlertType.HIGH_PRICE), | ||
arrayOf("low", StockAlertType.LOW_PRICE), | ||
) | ||
} | ||
|
||
@Test | ||
fun `given type string when convert to enum return a mapped type`() { | ||
Assert.assertEquals(expected, StockAlertType.fromString(input)) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...e/stocks/src/test/kotlin/br/com/stonks/feature/stocks/domain/types/StockStatusTypeTest.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,28 @@ | ||
package br.com.stonks.feature.stocks.domain.types | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
|
||
@RunWith(Parameterized::class) | ||
internal class StockStatusTypeTest( | ||
private val input: String, | ||
private val expected: StockStatusType, | ||
) { | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters | ||
fun arguments(): List<Array<Any>> = listOf( | ||
arrayOf("unknown", StockStatusType.UNKNOWN), | ||
arrayOf("available", StockStatusType.AVAILABLE), | ||
arrayOf("unavailable", StockStatusType.UNAVAILABLE), | ||
) | ||
} | ||
|
||
@Test | ||
fun `given type string when convert to enum return a mapped type`() { | ||
assertEquals(expected, StockStatusType.fromString(input)) | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
...ocks/src/test/kotlin/br/com/stonks/feature/stocks/domain/usecase/StockAlertUseCaseTest.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,147 @@ | ||
package br.com.stonks.feature.stocks.domain.usecase | ||
|
||
import br.com.stonks.common.db.DefaultPrimaryKey | ||
import br.com.stonks.feature.stocks.domain.mapper.StockAlertModelToResponseMapper | ||
import br.com.stonks.feature.stocks.domain.mapper.StockAlertResponseToModelMapper | ||
import br.com.stonks.feature.stocks.domain.model.StockAlertModel | ||
import br.com.stonks.feature.stocks.domain.types.StockAlertType | ||
import br.com.stonks.feature.stocks.domain.types.StockStatusType | ||
import br.com.stonks.feature.stocks.repository.StockRepository | ||
import br.com.stonks.feature.stocks.repository.remote.response.StockAlertResponse | ||
import br.com.stonks.testing.rules.CoroutinesTestRule | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.coVerifyOrder | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class StockAlertUseCaseTest { | ||
|
||
@get:Rule | ||
var coroutinesTestRule = CoroutinesTestRule() | ||
|
||
private val stockRepositoryMock = mockk<StockRepository>() | ||
private val stockPriceComparatorUseCaseMock = mockk<StockPriceComparatorUseCase>() | ||
private val stockAlertModelMapperMock = mockk<StockAlertResponseToModelMapper>() | ||
private val stockAlertResponseMapperMock = mockk<StockAlertModelToResponseMapper>() | ||
|
||
private val sut = StockAlertUseCase( | ||
stockRepository = stockRepositoryMock, | ||
stockPriceComparatorUseCase = stockPriceComparatorUseCaseMock, | ||
stockAlertModelMapper = stockAlertModelMapperMock, | ||
stockAlertResponseMapper = stockAlertResponseMapperMock, | ||
) | ||
|
||
@Test | ||
fun `given use case when call local alerts then check price comparator and mapper to model`() = runTest { | ||
val fakeResponse = createFakeResponse() | ||
|
||
coEvery { | ||
stockRepositoryMock.listStockAlerts() | ||
} returns Result.success(listOf(fakeResponse)) | ||
|
||
coEvery { | ||
stockPriceComparatorUseCaseMock.checkStockPrice(fakeResponse) | ||
} returns StockAlertType.HIGH_PRICE | ||
|
||
sut.listStockAlerts() | ||
|
||
coVerifyOrder { | ||
stockPriceComparatorUseCaseMock.checkStockPrice( | ||
fakeResponse | ||
) | ||
stockAlertModelMapperMock.mapper( | ||
createFakeResponse(trigger = StockAlertType.HIGH_PRICE.status) | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given save alert when use default primary key then call insert method`() = runTest { | ||
val fakeAlert = createFakeAlert() | ||
val fakeResponse = createFakeResponse() | ||
|
||
every { | ||
stockAlertResponseMapperMock.mapper(fakeAlert) | ||
} returns fakeResponse | ||
|
||
coEvery { | ||
stockRepositoryMock.insertStockAlert(fakeResponse) | ||
} returns mockk() | ||
|
||
sut.saveStockAlert(fakeAlert) | ||
|
||
coVerify(exactly = 1) { | ||
stockRepositoryMock.insertStockAlert(fakeResponse) | ||
} | ||
|
||
coVerify(exactly = 0) { | ||
stockRepositoryMock.updateStockAlert(any()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given save alert when use generated key then call update method`() = runTest { | ||
val fakeAlert = createFakeAlert(id = 5L) | ||
val fakeResponse = createFakeResponse(id = 5L) | ||
|
||
every { | ||
stockAlertResponseMapperMock.mapper(fakeAlert) | ||
} returns fakeResponse | ||
|
||
coEvery { | ||
stockRepositoryMock.updateStockAlert(fakeResponse) | ||
} returns Result.success(mockk()) | ||
|
||
sut.saveStockAlert(fakeAlert) | ||
|
||
coVerify(exactly = 1) { | ||
stockRepositoryMock.updateStockAlert(fakeResponse) | ||
} | ||
|
||
coVerify(exactly = 0) { | ||
stockRepositoryMock.insertStockAlert(any()) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given use case when call delete stock then call delete method`() = runTest { | ||
val alertId = 1L | ||
|
||
coEvery { | ||
stockRepositoryMock.deleteStockAlert(alertId) | ||
} returns mockk() | ||
|
||
sut.deleteStockAlert(alertId) | ||
|
||
coVerify { | ||
stockRepositoryMock.deleteStockAlert(alertId) | ||
} | ||
} | ||
|
||
private fun createFakeAlert( | ||
id: Long = DefaultPrimaryKey, | ||
) = StockAlertModel( | ||
id = id, | ||
ticket = "lorem", | ||
alertValue = 1.0, | ||
status = StockStatusType.AVAILABLE, | ||
notificationTrigger = StockAlertType.HIGH_PRICE, | ||
) | ||
|
||
private fun createFakeResponse( | ||
id: Long = DefaultPrimaryKey, | ||
trigger: String = "unknown", | ||
) = StockAlertResponse( | ||
id = id, | ||
stockTicket = "lorem", | ||
alertValue = 5.0, | ||
status = "available", | ||
notificationTrigger = trigger, | ||
) | ||
} |
117 changes: 117 additions & 0 deletions
117
...stocks/src/test/kotlin/br/com/stonks/feature/stocks/repository/StockRepositoryImplTest.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,117 @@ | ||
package br.com.stonks.feature.stocks.repository | ||
|
||
import br.com.stonks.feature.stocks.domain.mapper.StockAlertEntityToResponseMapper | ||
import br.com.stonks.feature.stocks.domain.mapper.StockAlertResponseToEntityMapper | ||
import br.com.stonks.feature.stocks.repository.local.StockAlertEntity | ||
import br.com.stonks.feature.stocks.repository.local.StockLocalDataSource | ||
import br.com.stonks.feature.stocks.repository.remote.StockRemoteDataSource | ||
import br.com.stonks.feature.stocks.repository.remote.response.StockAlertResponse | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.coVerifyOrder | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
|
||
class StockRepositoryImplTest { | ||
|
||
private val fakeEntity = mockk<StockAlertEntity>() | ||
private val fakeResponse = mockk<StockAlertResponse>() | ||
private val stockRemoteDataSourceMock = mockk<StockRemoteDataSource>(relaxed = true) | ||
private val stockLocalDataSourceMock = mockk<StockLocalDataSource>(relaxed = true) | ||
private val stockAlertResponseMapperMock = mockk<StockAlertEntityToResponseMapper>(relaxed = true) | ||
private val stockAlertEntityMapperMock = mockk<StockAlertResponseToEntityMapper>(relaxed = true) | ||
|
||
private val sut = StockRepositoryImpl( | ||
stockRemoteDataSource = stockRemoteDataSourceMock, | ||
stockLocalDataSource = stockLocalDataSourceMock, | ||
stockAlertResponseMapper = stockAlertResponseMapperMock, | ||
stockAlertEntityMapper = stockAlertEntityMapperMock, | ||
) | ||
|
||
@Test | ||
fun `given repository when get remote alerts then call remote data source`() = runTest { | ||
sut.getRemoteStockAlerts() | ||
|
||
coVerify { stockRemoteDataSourceMock.getStockAlerts() } | ||
} | ||
|
||
@Test | ||
fun `given repository when get local alerts then call local data source`() = runTest { | ||
coEvery { | ||
stockLocalDataSourceMock.listStockAlerts() | ||
} returns Result.success(listOf(fakeEntity)) | ||
|
||
sut.listStockAlerts() | ||
|
||
coVerifyOrder { | ||
stockLocalDataSourceMock.listStockAlerts() | ||
stockAlertResponseMapperMock.mapper(fakeEntity) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given repository when get alert then call local data source`() = runTest { | ||
val alertId = 1L | ||
|
||
coEvery { | ||
stockLocalDataSourceMock.getStockAlert(alertId) | ||
} returns Result.success(fakeEntity) | ||
|
||
sut.getStockAlert(alertId) | ||
|
||
coVerifyOrder { | ||
stockLocalDataSourceMock.getStockAlert(alertId) | ||
stockAlertResponseMapperMock.mapper(fakeEntity) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given repository when insert alert then call local data source`() = runTest { | ||
coEvery { | ||
stockAlertEntityMapperMock.mapper(fakeResponse) | ||
} returns fakeEntity | ||
|
||
coEvery { | ||
stockLocalDataSourceMock.insertStockAlert(fakeEntity) | ||
} returns mockk() | ||
|
||
sut.insertStockAlert(fakeResponse) | ||
|
||
coVerify { | ||
stockLocalDataSourceMock.insertStockAlert(fakeEntity) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given repository when update alert then call local data source`() = runTest { | ||
coEvery { | ||
stockAlertEntityMapperMock.mapper(fakeResponse) | ||
} returns fakeEntity | ||
|
||
coEvery { | ||
stockLocalDataSourceMock.updateStockAlert(fakeEntity) | ||
} returns mockk() | ||
|
||
sut.updateStockAlert(fakeResponse) | ||
|
||
coVerify { | ||
stockLocalDataSourceMock.updateStockAlert(fakeEntity) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given repository when delete alert then call local data source`() = runTest { | ||
val alertId = 1L | ||
|
||
coEvery { | ||
stockLocalDataSourceMock.deleteStockAlert(alertId) | ||
} returns mockk() | ||
|
||
sut.deleteStockAlert(alertId) | ||
|
||
coVerify { | ||
stockLocalDataSourceMock.deleteStockAlert(alertId) | ||
} | ||
} | ||
} |