Skip to content

Commit

Permalink
Merge pull request #272 from DevJethava/unittest/usecases/UpdateCartI…
Browse files Browse the repository at this point in the history
…temUseCase

Cover unit test for use case UpdateCartItemUseCase.kt
  • Loading branch information
hieuwu authored Oct 18, 2024
2 parents 6b29f56 + 619e237 commit f6f2e2b
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class UpdateCartItemUseCaseImpl @Inject constructor(
@IoDispatcher private val ioDispatcher: CoroutineDispatcher
) : UpdateCartItemUseCase {
override suspend fun updateLineItem(lineItemModel: LineItemModel) {
// Check if the quantity is valid
if (lineItemModel.quantity == null || lineItemModel.quantity!! <= 0) {
throw IllegalArgumentException("Quantity must be greater than zero.")
}

// Check if the ID is valid
if (lineItemModel.id == null) {
throw IllegalArgumentException("Line item ID cannot be null.")
}

withContext(ioDispatcher) {
if (lineItemModel.quantity != null && lineItemModel.id != null) {
productRepository.updateLineItemQuantityById(
Expand All @@ -29,6 +39,11 @@ class UpdateCartItemUseCaseImpl @Inject constructor(
}

override suspend fun removeLineItem(lineItemModel: LineItemModel) {
// Check if the ID is valid
if (lineItemModel.id == null) {
throw IllegalArgumentException("Line item ID cannot be null.")
}

withContext(ioDispatcher) {
if (lineItemModel.id != null) {
productRepository.removeLineItemById(lineItemModel.id!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class UpdateCartItemUseCaseImplTest {
)
}

/**
* Test for updating line item with a valid value
*/
@Test
fun updateLineItem_correctValue_whenExecute_thenCallProductRepository() {
runBlocking {
Expand All @@ -49,17 +52,24 @@ class UpdateCartItemUseCaseImplTest {
}
}

@Test
/**
* Test for updateLineItem when quantity is null
* Should throw IllegalArgumentException because quantity must be greater than zero
*/
@Test(expected = IllegalArgumentException::class)
fun updateLineItem_quantityNull_whenExecute_thenCallProductRepository() {
runBlocking {
val input = LineItemModel(1L, null, null, null, null, null)
testee.updateLineItem(input)

Mockito.verifyNoInteractions(mockedOrderRepository)
// Mockito.verifyNoInteractions(mockedOrderRepository)
}
}

@Test
/**
* Test for null ID should throw IllegalArgumentException
*/
@Test(expected = IllegalArgumentException::class)
fun updateLineItem_idNull_whenExecute_thenCallProductRepository() {
runBlocking {
val input = LineItemModel(null, null, null, null, null, 1)
Expand All @@ -69,6 +79,9 @@ class UpdateCartItemUseCaseImplTest {
}
}

/**
* Test for removing line item with a valid ID
*/
@Test
fun removeLineItem_correctValue_whenExecute_thenCallProductRepository() {
runBlocking {
Expand All @@ -79,7 +92,10 @@ class UpdateCartItemUseCaseImplTest {
}
}

@Test
/**
* Test for null ID when removing line item should throw IllegalArgumentException
*/
@Test(expected = IllegalArgumentException::class)
fun removeLineItem_null_whenExecute_thenCallProductRepository() {
runBlocking {
val input = LineItemModel(null, null, null, null, null, null)
Expand All @@ -89,6 +105,9 @@ class UpdateCartItemUseCaseImplTest {
}
}

/**
* Test for retrieving a non-empty cart
*/
@Test
fun getCurrentCartNotEmpty_whenExecute_thenReturnOrdersInCart() {
val mockOrder = OrderModel(
Expand All @@ -114,6 +133,9 @@ class UpdateCartItemUseCaseImplTest {
}
}

/**
* Test for retrieving an empty cart
*/
@Test
fun getCurrentCartEmpty_whenExecute_thenReturnNull() {
whenever(mockedOrderRepository.getOneOrderByStatus(OrderStatus.IN_CART)).thenReturn(flow {
Expand All @@ -126,4 +148,107 @@ class UpdateCartItemUseCaseImplTest {
}
}
}

/**
* Edge case for updateLineItem with minimum quantity
*/
@Test
fun updateLineItem_minQuantity_whenExecute_thenCallProductRepository() {
runBlocking {
val input = LineItemModel(1L, null, null, null, null, 1) // minimum valid quantity
testee.updateLineItem(input)

verify(mockedProductRepository).updateLineItemQuantityById(1, input.id!!)
}
}

/**
* Edge case for updateLineItem with maximum quantity
*/
@Test
fun updateLineItem_maxQuantity_whenExecute_thenCallProductRepository() {
runBlocking {
val input = LineItemModel(1L, null, null, null, null, 1000) // maximum valid quantity
testee.updateLineItem(input)

verify(mockedProductRepository).updateLineItemQuantityById(1000, input.id!!)
}
}

/**
* Test for updateLineItem when invalid quantity
*/
@Test(expected = IllegalArgumentException::class)
fun updateLineItem_invalidQuantity_whenExecute_thenThrowException() {
runBlocking {
val input = LineItemModel(1L, null, null, null, null, -1) // invalid quantity
testee.updateLineItem(input)
}
}

/**
* Test for updateLineItem when Zero quantity
*/
@Test(expected = IllegalArgumentException::class)
fun updateLineItem_zeroQuantity_whenExecute_thenThrowException() {
runBlocking {
val input = LineItemModel(1L, null, null, null, null, 0) // zero quantity
testee.updateLineItem(input)
}
}

/**
* Test removeLineItem with null values
*/
@Test(expected = IllegalArgumentException::class)
fun removeLineItem_nullId_whenExecute_thenThrowException() {
runBlocking {
val input = LineItemModel(null, null, null, null, null, null)
testee.removeLineItem(input)
}
}

/**
* Test getCurrentCart with a populated cart
*/
@Test
fun getCurrentCartWithItems_whenExecute_thenReturnCorrectCart() {
val mockOrder = OrderModel(
id = "123",
status = OrderStatus.IN_CART.value,
address = "Test Address",
lineItemList = mutableListOf(
LineItemModel(1L, null, null, null, null, 2),
LineItemModel(2L, null, null, null, null, 3)
),
createdAt = "2024-10-15"
)
whenever(mockedOrderRepository.getOneOrderByStatus(OrderStatus.IN_CART)).thenReturn(flow {
emit(mockOrder)
})
runBlocking {
val result = testee.getCurrentCart()

result.collect { order ->
TestCase.assertNotNull(order)
TestCase.assertEquals(order?.lineItemList?.size, 2)
TestCase.assertEquals(order?.id, "123")
TestCase.assertEquals(order?.address, "Test Address")
}
}
}

/**
* Verify interaction with OrderRepository during removeLineItem
*/
@Test
fun removeLineItem_correctValue_whenExecute_thenNoInteractionWithOrderRepository() {
runBlocking {
val input = LineItemModel(1L, null, null, null, null, null)
testee.removeLineItem(input)

verify(mockedProductRepository).removeLineItemById(input.id!!)
Mockito.verifyNoInteractions(mockedOrderRepository)
}
}
}

0 comments on commit f6f2e2b

Please sign in to comment.