Skip to content

Commit

Permalink
fix: addicionando testes no repository do carrinho
Browse files Browse the repository at this point in the history
  • Loading branch information
flflima committed Apr 1, 2021
1 parent dad7193 commit 4c03c37
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 32 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

mainClassName = 'br.com.dev.shoppingcart.AppKt'
group = 'br.com.dev'
version = '2.1.1'
version = '2.1.2'

repositories {
mavenCentral()
Expand Down Expand Up @@ -82,7 +82,7 @@ jacocoTestCoverageVerification {
enabled = true
counter = 'BRANCH'
value = 'COVEREDRATIO'
minimum = 0.5
minimum = 0.8
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/br/com/dev/shoppingcart/config/KoinConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package br.com.dev.shoppingcart.config

import br.com.dev.shoppingcart.domain.database.Database
import br.com.dev.shoppingcart.domain.database.Database.allProducts
import br.com.dev.shoppingcart.domain.database.Database.allProductsCart
import br.com.dev.shoppingcart.domain.repository.CartRepository
import br.com.dev.shoppingcart.domain.repository.ProductRepository
import br.com.dev.shoppingcart.domain.service.CartService
Expand All @@ -24,8 +27,8 @@ object KoinConfig {
}

private val repositoriesModules = module {
single { CartRepository() }
single { ProductRepository() }
single { CartRepository(Database.allCarts, allProductsCart, allProducts) }
single { ProductRepository(allProducts) }
}

private val objectMapperModule = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class Product(

fun Product.toProductDTO(quantity: Int = 0) =
ProductDTO(
id = this.id,
name = this.name,
price = this.price,
description = this.description,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package br.com.dev.shoppingcart.domain.repository

import br.com.dev.shoppingcart.domain.database.Database.allCarts
import br.com.dev.shoppingcart.domain.database.Database.allProducts
import br.com.dev.shoppingcart.domain.database.Database.allProductsCart
import br.com.dev.shoppingcart.domain.model.Cart
import br.com.dev.shoppingcart.domain.model.CartProduct
import br.com.dev.shoppingcart.domain.model.Product

class CartRepository {
class CartRepository(
private val carts: MutableList<Cart>,
private val cartProducts: MutableList<CartProduct>,
private val products: MutableList<Product>
) {

fun getCartByUserId(userId: String): List<Cart> = allCarts.filter { it.userId == userId }
fun getProductsByCartId(cartId: Long) =
allProductsCart.filter { it.cartId == cartId }
.flatMap { productCart -> allProducts.filter { it.id == productCart.productID } }
fun getCartByUserId(userId: String): List<Cart> = carts.filter { it.userId == userId }

fun getProductsByCartId(cartId: Long): List<Product> =
cartProducts.filter { it.cartId == cartId }
.flatMap { productCart -> products.filter { it.id == productCart.productID } }

fun createCart(userId: String, productId: Long? = null, quantity: Int = 0): List<Cart> =
getCartByUserId(userId).let {
if (it.isEmpty()) {
val cart = Cart((allCarts.size + 1).toLong(), userId)
allCarts.add(cart)
val cart = Cart((carts.size + 1).toLong(), userId)
carts.add(cart)

if (productId != null) {
val cartProduct = CartProduct(cart.id, productId, quantity)
allProductsCart.add(cartProduct)
cartProducts.add(cartProduct)
}

listOf(cart)
Expand All @@ -31,15 +33,19 @@ class CartRepository {
}
}

fun addProduct(userId: String, productId: Long, quantity: Int): List<Cart> =
fun addProduct(userId: String, productId: Long, quantity: Int): List<CartProduct> =
getCartByUserId(userId).let {
if (it.isEmpty()) {
createCart(userId, productId, quantity)
} else {
allProductsCart.filter { c -> c.productID == productId }
.forEach { cartProduct -> cartProduct.addQuantity(quantity) }
it
if (cartProducts.none { c -> c.productID == productId }) {
val cartProduct = CartProduct(it.first().id, productId, quantity)
cartProducts.add(cartProduct)
} else {
cartProducts.forEach { cartProduct -> cartProduct.addQuantity(quantity) }
}
}
return cartProducts.filter { cartProduct -> cartProduct.productID == productId }
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package br.com.dev.shoppingcart.domain.repository

import br.com.dev.shoppingcart.domain.database.Database.allProducts
import br.com.dev.shoppingcart.domain.model.Product
import br.com.dev.shoppingcart.web.dto.ProductDTO

class ProductRepository {
class ProductRepository(private val products: MutableList<Product>) {

fun findProductById(productId: Long) = allProducts.firstOrNull {
fun findProductById(productId: Long) = products.firstOrNull {
it.id == productId
}

fun saveProduct(productDTO: ProductDTO): Product {
val product = Product(
(allProducts.size + 1).toLong(),
(products.size + 1).toLong(),
productDTO.name,
productDTO.price,
productDTO.description,
productDTO.category
)
allProducts.add(product)
products.add(product)
return product
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CartService(private val cartRepository: CartRepository, private val produc
productRepository.findProductById(productId).let {
if (it != null) {
return cartRepository.addProduct(userId, productId, quantity).let { cartProducts ->
CartDTO(cartProducts.first().userId, cartProducts.map { _ -> it.toProductDTO(quantity) })
CartDTO(userId, cartProducts.map { _ -> it.toProductDTO(cartProducts.first().quantity) })
}
}
throw NotFoundResponse("Product not found!")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,238 @@
package br.com.dev.shoppingcart.domain.repository

import br.com.dev.shoppingcart.domain.model.Cart
import br.com.dev.shoppingcart.domain.model.CartProduct
import br.com.dev.shoppingcart.domain.model.Product
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

import org.junit.jupiter.api.Assertions.*

@ExtendWith(MockKExtension::class)
internal class CartRepositoryTest {

private lateinit var cut: CartRepository

@MockK
private lateinit var carts: MutableList<Cart>

@MockK
private lateinit var cartProducts: MutableList<CartProduct>

@MockK
private lateinit var products: MutableList<Product>

@BeforeEach
fun setUp() {
this.cut = CartRepository(carts, cartProducts, products)
}

@Test
fun `given an user id if it exists must return a list of carts`() {
// arrange
this.cut = CartRepository(mutableListOf(Cart(1, "1")), cartProducts, products)

// act
val cart = this.cut.getCartByUserId("1")

// assert
assertThat(cart).isNotEmpty
assertThat(cart).size().isEqualTo(1)
}

@Test
fun `given an user id if it does not exist must return an empty list`() {
// arrange
this.cut = CartRepository(mutableListOf(Cart(1, "1")), cartProducts, products)

// act
val cart = this.cut.getCartByUserId("10")

// assert
assertThat(cart).isEmpty()
}

@Test
fun `given an user id if it does not has a cart must create a new one and return in the list`() {
// arrange
val cartProducts = mutableListOf<CartProduct>()
this.cut = CartRepository(mutableListOf(), cartProducts, products)

// act
val carts = this.cut.createCart("10")

// assert
assertThat(carts).isNotEmpty
assertThat(carts).size().isEqualTo(1)
assertThat(carts.first().userId).isEqualTo("10")
assertThat(cartProducts).isEmpty()
}

@Test
fun `given an user id and a product id if it does not has a cart must create a new one with products and return in the list`() {
// arrange
val cartProducts = mutableListOf<CartProduct>()
this.cut = CartRepository(mutableListOf(), cartProducts, products)

// act
val carts = this.cut.createCart("1", 1)

// assert
assertThat(carts).isNotEmpty
assertThat(carts).size().isEqualTo(1)
assertThat(carts.first().userId).isEqualTo("1")
assertThat(cartProducts).size().isEqualTo(1)
}

@Test
fun `given an user id that has a cart must not create a new one and return in the list`() {
// arrange
val cartProducts = mutableListOf<CartProduct>()
this.cut = CartRepository(mutableListOf(Cart(1, "1")), cartProducts, products)

// act
val carts = this.cut.createCart("1", 1)

// assert
assertThat(carts).isNotEmpty
assertThat(carts).size().isEqualTo(1)
assertThat(carts.first().userId).isEqualTo("1")
assertThat(cartProducts).isEmpty()
}

@Test
fun `given a valid cart id must return a list of products`() {
// arrange
val cart = mutableListOf(Cart(1, "1"))
val cartProducts = mutableListOf(CartProduct(1, 1))
val products = mutableListOf(Product(1, "Tenis", 100.0, "", ""))
this.cut = CartRepository(cart, cartProducts, products)

// act
val productsByCartId = this.cut.getProductsByCartId(1)

// assert
assertThat(productsByCartId).isNotEmpty
assertThat(productsByCartId.first().id).isEqualTo(1)
}

@Test
fun `given a valid cart id but with no products in the cart must return an empty list of products`() {
// arrange
val cart = mutableListOf(Cart(1, "1"))
val cartProducts = mutableListOf(CartProduct(1, 1))
val products = mutableListOf(Product(10, "Tenis", 100.0, "", ""))
this.cut = CartRepository(cart, cartProducts, products)

// act
val productsByCartId = this.cut.getProductsByCartId(1)

// assert
assertThat(productsByCartId).isEmpty()
}

@Test
fun getCartByUserId() {
fun `given an invalid cart id must return an empty list of products`() {
// arrange
val cart = mutableListOf(Cart(1, "1"))
val cartProducts = mutableListOf(CartProduct(1, 1))
val products = mutableListOf(Product(1, "Tenis", 100.0, "", ""))
this.cut = CartRepository(cart, cartProducts, products)

// act
val productsByCartId = this.cut.getProductsByCartId(100)

// assert
assertThat(productsByCartId).isEmpty()
}

@Test
fun createCart() {
fun `when adding a product to a cart if the cart is empty or does not exists must return a new cart`() {
// arrange
val products = mutableListOf(Product(1, "Tenis", 100.0, "", ""))
val cartProducts = mutableListOf<CartProduct>()
this.cut = CartRepository(mutableListOf(), cartProducts, products)

// act
val carts = this.cut.addProduct("1", 1, 10)

// assert
assertThat(carts).isNotEmpty
assertThat(carts).size().isEqualTo(1)
assertThat(carts.first().cartId).isEqualTo(1)

assertThat(cartProducts).size().isEqualTo(1)
assertThat(cartProducts.first().productID).isEqualTo(1)
assertThat(cartProducts.first().quantity).isEqualTo(10)
}

@Test
fun addProduct() {
fun `when adding a product to a cart if the cart already exists and is empty must return a cart with a new product`() {
// arrange
val cart = mutableListOf(Cart(1, "1"))
val cartProducts = mutableListOf(CartProduct(1, 1))
val products = mutableListOf(
Product(1, "Tenis", 100.0, "", ""),
Product(2, "Tenis em Promocao", 99.0, "", "")
)
this.cut = CartRepository(cart, cartProducts, products)

// act
val carts = this.cut.addProduct("1", 2, 5)

// assert
assertThat(carts).isNotEmpty
assertThat(carts).size().isEqualTo(1)
assertThat(carts.first().cartId).isEqualTo(1)

assertThat(cartProducts).size().isEqualTo(2)
}

@Test
fun `when adding a product to a cart if the cart already exists and has the same product that is being added must return a cart product quantity updated`() {
// arrange
val cart = mutableListOf(Cart(1, "1"))
val cartProducts = mutableListOf(CartProduct(1, 1, 2))
val products = mutableListOf(
Product(1, "Tenis", 100.0, "", ""),
Product(2, "Tenis em Promocao", 99.0, "", "")
)
this.cut = CartRepository(cart, cartProducts, products)

// act
val carts = this.cut.addProduct("1", 1, 5)

// assert
assertThat(carts).isNotEmpty
assertThat(carts).size().isEqualTo(1)
assertThat(carts.first().cartId).isEqualTo(1)

assertThat(cartProducts).size().isEqualTo(1)
assertThat(cartProducts.first().quantity).isEqualTo(7)
}

@Test
fun `when adding a product to a cart if the cart already exists but is empty then must return a cart product quantity updated`() {
// arrange
val cart = mutableListOf(Cart(1, "1"))
val cartProducts = mutableListOf<CartProduct>()
val products = mutableListOf(
Product(1, "Tenis", 100.0, "", ""),
Product(2, "Tenis em Promocao", 99.0, "", "")
)
this.cut = CartRepository(cart, cartProducts, products)

// act
val carts = this.cut.addProduct("1", 1, 5)

// assert
assertThat(carts).isNotEmpty
assertThat(carts).size().isEqualTo(1)
assertThat(carts.first().cartId).isEqualTo(1)

assertThat(cartProducts).size().isEqualTo(1)
assertThat(cartProducts.first().quantity).isEqualTo(5)
}
}
Loading

0 comments on commit 4c03c37

Please sign in to comment.