Skip to content

Commit

Permalink
Change response type (#45)
Browse files Browse the repository at this point in the history
* Change response type from like to string(ProductId)

* Remove API-Call and update product version

* Change response type

* Added config to generate bean of product repository

* Changes done in service to interact with product repository

* Updated tests
  • Loading branch information
Harsh3305 authored May 11, 2023
1 parent b424890 commit c754be5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 38 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ dependencies {
testImplementation("io.projectreactor:reactor-test")
// Custom-Pageable
implementation("com.hrv.mart:custom-pageable:0.0.1-SNAPSHOT")
// product
implementation("com.hrv.mart:product:0.0.2")
}
detekt {
toolVersion = "1.22.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.backendlike.config

import com.hrv.mart.product.repository.ProductRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient

@Component
class ProductConfiguration (
@Autowired
private val webClientBuilder: WebClient.Builder,
)
{
@Bean
fun getProductRepository() =
ProductRepository(webClientBuilder)

}
11 changes: 9 additions & 2 deletions src/main/kotlin/com/example/backendlike/service/LikeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@ package com.example.backendlike.service
import com.hrv.mart.custompageable.Pageable
import com.example.backendlike.model.Like
import com.example.backendlike.repository.LikeRepository
import com.hrv.mart.product.Product
import com.hrv.mart.product.repository.ProductRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.http.HttpStatus
import org.springframework.http.server.reactive.ServerHttpResponse
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono

@Service
class LikeService (
@Autowired
private val likeRepository: LikeRepository
private val likeRepository: LikeRepository,
@Autowired
private val productRepository: ProductRepository
)
{
fun getAllLikesOfUser(userId: String, pageRequest: PageRequest) =
likeRepository.findLikeByUserId(userId, pageRequest)
.map { it.productId }
.flatMap { productRepository.getProductByProductId(it) }
.collectList()
.flatMap { likes ->
likeRepository.countLikeByUserId(userId)
.map {totalSize ->
Pageable<Like>(
Pageable<Product>(
data = likes,
size = pageRequest.pageSize.toLong(),
nextPage = Pageable.getNextPage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.hrv.mart.custompageable.Pageable
import com.example.backendlike.model.Like
import com.example.backendlike.repository.LikeRepository
import com.example.backendlike.service.LikeService
import com.hrv.mart.product.Product
import com.hrv.mart.product.repository.ProductRepository
import org.junit.jupiter.api.Test
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
Expand All @@ -17,39 +19,58 @@ import java.util.*
class LikeControllerTest {
private val likeRepository = mock(LikeRepository::class.java)
private val response = mock(ServerHttpResponse::class.java)
private val likeService = LikeService(likeRepository)
private val productRepository = mock(ProductRepository::class.java)
private val likeService = LikeService(likeRepository, productRepository)
private val likeController = LikeController(likeService)
private val like = Like(
private val like1 = Like(
userId = "userID",
productId = "productID"
productId = "test_image_1"
)
private val like2 = Like(
userId = "userID",
productId = "test_image_2"
)
private val product1 = Product(
name = "Test Product",
description = "Only for testing",
images = listOf("https://image.hrv-mart.com/test_image_1"),
price = 100,
id = "test_image_1"
)
private val product2 = Product(
name = "Test Product 2",
description = "Only for testing",
images = listOf("https://image.hrv-mart.com/test_image_2"),
price = 100,
id = "test_image_2"
)
@Test
fun `should insert like in database when not exist in database`() {
doReturn(Mono.just(like)).`when`(likeRepository).insert(like)
StepVerifier.create(likeController.addProductToLike(like, response))
doReturn(Mono.just(like1)).`when`(likeRepository).insert(like1)
StepVerifier.create(likeController.addProductToLike(like1, response))
.expectNext("Like added successfully")
.verifyComplete()
}
@Test
fun `should not insert like in database when it exist in database`() {
doReturn(Mono.error<Exception>(Exception("Like already exist")))
.`when`(likeRepository)
.insert(like)
StepVerifier.create(likeController.addProductToLike(like, response))
.insert(like1)
StepVerifier.create(likeController.addProductToLike(like1, response))
.expectNext("Like already exist")
.verifyComplete()
}
@Test
fun `should remove like from database when it does not exist in database`() {
doReturn(Mono.just(true))
.`when`(likeRepository)
.existsByUserIdAndProductId(like.userId, like.productId)
.existsByUserIdAndProductId(like1.userId, like1.productId)
doReturn(Mono.empty<Void>())
.`when`(likeRepository)
.deleteByUserIdAndProductId(like.userId, like.productId)
.deleteByUserIdAndProductId(like1.userId, like1.productId)
StepVerifier.create(likeController.removeProductFromLike(
userId = like.userId,
productId = like.productId,
userId = like1.userId,
productId = like1.productId,
response = response
))
.expectNext("Like removed successfully")
Expand All @@ -59,10 +80,10 @@ class LikeControllerTest {
fun `should not remove like from database if it does not exist in database`() {
doReturn(Mono.just(false))
.`when`(likeRepository)
.existsByUserIdAndProductId(like.userId, like.productId)
.existsByUserIdAndProductId(like1.userId, like1.productId)
StepVerifier.create(likeController.removeProductFromLike(
userId = like.userId,
productId = like.productId,
userId = like1.userId,
productId = like1.productId,
response = response
))
.expectNext("Like not found")
Expand All @@ -72,10 +93,10 @@ class LikeControllerTest {
fun `should return true if like exist in database`() {
doReturn(Mono.just(true))
.`when`(likeRepository)
.existsByUserIdAndProductId(like.userId, like.productId)
.existsByUserIdAndProductId(like1.userId, like1.productId)
StepVerifier.create(likeController.isProductLikedByUser(
productId = like.productId,
userId = like.userId
productId = like1.productId,
userId = like1.userId
))
.expectNext(true)
.verifyComplete()
Expand All @@ -84,10 +105,10 @@ class LikeControllerTest {
fun `should return false if like does not exist in database`() {
doReturn(Mono.just(false))
.`when`(likeRepository)
.existsByUserIdAndProductId(like.userId, like.productId)
.existsByUserIdAndProductId(like1.userId, like1.productId)
StepVerifier.create(likeController.isProductLikedByUser(
productId = like.productId,
userId = like.userId
productId = like1.productId,
userId = like1.userId
))
.expectNext(false)
.verifyComplete()
Expand All @@ -96,19 +117,28 @@ class LikeControllerTest {
fun `should return all likes by user if it exist in database`() {
val size = 10L
val index = 0L

val expected = Pageable(
nextPage = null,
size = size,
data = listOf(like)
data = listOf(product1, product2)
)
doReturn(Flux.just(like))
doReturn(Flux.just(like1, like2))
.`when`(likeRepository)
.findLikeByUserId(like.userId, PageRequest.of(index.toInt(), size.toInt()))
doReturn(Mono.just(1L))
.findLikeByUserId(like1.userId, PageRequest.of(index.toInt(), size.toInt()))

doReturn(Mono.just(2L))
.`when`(likeRepository)
.countLikeByUserId(like.userId)
.countLikeByUserId(like1.userId)

doReturn(Mono.just(product1))
.`when`(productRepository)
.getProductByProductId(product1.id)
doReturn(Mono.just(product2))
.`when`(productRepository)
.getProductByProductId(product2.id)
StepVerifier.create(likeController.getAllLikesOfUser(
userId = like.userId,
userId = like1.userId,
size = Optional.of(size.toInt()),
page = Optional.of(index.toInt())
))
Expand All @@ -119,19 +149,23 @@ class LikeControllerTest {
fun `should return all likes with next index by user if it exist in database`() {
val size = 1L
val index = 0L

val expected = Pageable(
nextPage = 1,
size = size,
data = listOf(like, like)
data = listOf(product1)
)
doReturn(Flux.just(like, like))
doReturn(Flux.just(like1))
.`when`(likeRepository)
.findLikeByUserId(like.userId, PageRequest.of(index.toInt(), size.toInt()))
.findLikeByUserId(like1.userId, PageRequest.of(index.toInt(), size.toInt()))
doReturn(Mono.just(2L))
.`when`(likeRepository)
.countLikeByUserId(like.userId)
.countLikeByUserId(like1.userId)
doReturn(Mono.just(product1))
.`when`(productRepository)
.getProductByProductId(product1.id)
StepVerifier.create(likeController.getAllLikesOfUser(
userId = like.userId,
userId = like1.userId,
size = Optional.of(size.toInt()),
page = Optional.of(index.toInt())
))
Expand All @@ -145,16 +179,16 @@ class LikeControllerTest {
val expected = Pageable(
nextPage = null,
size = size,
data = emptyList<Like>()
data = emptyList<Product>()
)
doReturn(Flux.empty<Like>())
doReturn(Flux.empty<String>())
.`when`(likeRepository)
.findLikeByUserId(like.userId, PageRequest.of(index.toInt(), size.toInt()))
.findLikeByUserId(like1.userId, PageRequest.of(index.toInt(), size.toInt()))
doReturn(Mono.just(1L))
.`when`(likeRepository)
.countLikeByUserId(like.userId)
.countLikeByUserId(like1.userId)
StepVerifier.create(likeController.getAllLikesOfUser(
userId = like.userId,
userId = like1.userId,
size = Optional.of(size.toInt()),
page = Optional.of(index.toInt())
))
Expand Down

0 comments on commit c754be5

Please sign in to comment.