mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +03:00
move and refactor order service to core
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.github.dannecron.demo.core.dto
|
||||
|
||||
import com.github.dannecron.demo.db.serialialization.OffsetDateTimeSerialization
|
||||
import com.github.dannecron.demo.db.serialialization.UuidSerialization
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
|
||||
@Serializable
|
||||
data class Order(
|
||||
val id: Long,
|
||||
@Serializable(with = UuidSerialization::class)
|
||||
val guid: UUID,
|
||||
val customerId: Long,
|
||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||
val deliveredAt: OffsetDateTime?,
|
||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||
val createdAt: OffsetDateTime,
|
||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||
val updatedAt: OffsetDateTime?,
|
||||
) {
|
||||
fun isDelivered(): Boolean = deliveredAt != null
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.github.dannecron.demo.core.dto
|
||||
|
||||
import com.github.dannecron.demo.db.serialialization.OffsetDateTimeSerialization
|
||||
import com.github.dannecron.demo.db.serialialization.UuidSerialization
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
|
||||
@Serializable
|
||||
data class OrderProduct(
|
||||
@Serializable(with = UuidSerialization::class)
|
||||
val guid: UUID,
|
||||
val orderId: Long,
|
||||
val productId: Long,
|
||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||
val createdAt: OffsetDateTime,
|
||||
@Serializable(with = OffsetDateTimeSerialization::class)
|
||||
val updatedAt: OffsetDateTime?,
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.github.dannecron.demo.core.services.order
|
||||
|
||||
import com.github.dannecron.demo.core.dto.Order
|
||||
import com.github.dannecron.demo.core.dto.OrderProduct
|
||||
import com.github.dannecron.demo.core.dto.Product
|
||||
|
||||
interface OrderProductService {
|
||||
fun create(order: Order, product: Product): OrderProduct
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.github.dannecron.demo.core.services.order
|
||||
|
||||
import com.github.dannecron.demo.core.dto.Order
|
||||
import com.github.dannecron.demo.core.dto.OrderProduct
|
||||
import com.github.dannecron.demo.core.dto.Product
|
||||
import com.github.dannecron.demo.core.services.generation.CommonGenerator
|
||||
import com.github.dannecron.demo.db.entity.order.OrderProductEntity
|
||||
import com.github.dannecron.demo.db.repository.OrderProductRepository
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class OrderProductServiceImpl(
|
||||
private val orderProductRepository: OrderProductRepository,
|
||||
private val commonGenerator: CommonGenerator
|
||||
) : OrderProductService {
|
||||
override fun create(order: Order, product: Product): OrderProduct = OrderProductEntity(
|
||||
guid = commonGenerator.generateUUID(),
|
||||
orderId = order.id,
|
||||
productId = product.id,
|
||||
createdAt = commonGenerator.generateCurrentTime(),
|
||||
updatedAt = null,
|
||||
).let(orderProductRepository::save)
|
||||
.toCore()
|
||||
|
||||
private fun OrderProductEntity.toCore() = OrderProduct(
|
||||
guid = guid,
|
||||
orderId = orderId,
|
||||
productId = productId,
|
||||
createdAt = createdAt,
|
||||
updatedAt = updatedAt,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.github.dannecron.demo.core.services.order
|
||||
|
||||
import com.github.dannecron.demo.core.dto.Customer
|
||||
import com.github.dannecron.demo.core.dto.Order
|
||||
|
||||
interface OrderService {
|
||||
|
||||
fun findByCustomerId(customerId: Long): List<Order>
|
||||
|
||||
fun create(customer: Customer): Order
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.github.dannecron.demo.core.services.order
|
||||
|
||||
import com.github.dannecron.demo.core.dto.Customer
|
||||
import com.github.dannecron.demo.core.dto.Order
|
||||
import com.github.dannecron.demo.core.services.generation.CommonGenerator
|
||||
import com.github.dannecron.demo.db.entity.order.OrderEntity
|
||||
import com.github.dannecron.demo.db.repository.OrderRepository
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class OrderServiceImpl(
|
||||
private val orderRepository: OrderRepository,
|
||||
private val commonGenerator: CommonGenerator,
|
||||
) : OrderService {
|
||||
override fun findByCustomerId(customerId: Long): List<Order> =
|
||||
orderRepository.findByCustomerId(customerId).map { it.toCore() }
|
||||
|
||||
override fun create(customer: Customer): Order = OrderEntity(
|
||||
id = null,
|
||||
guid = commonGenerator.generateUUID(),
|
||||
customerId = customer.id,
|
||||
deliveredAt = null,
|
||||
createdAt = commonGenerator.generateCurrentTime(),
|
||||
updatedAt = null,
|
||||
).let(orderRepository::save)
|
||||
.toCore()
|
||||
|
||||
private fun OrderEntity.toCore() = Order(
|
||||
id = id!!,
|
||||
guid = guid,
|
||||
customerId = customerId,
|
||||
deliveredAt = deliveredAt,
|
||||
createdAt = createdAt,
|
||||
updatedAt = updatedAt,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.github.dannecron.demo.core.services.order
|
||||
|
||||
import com.github.dannecron.demo.core.dto.Order
|
||||
import com.github.dannecron.demo.core.dto.OrderProduct
|
||||
import com.github.dannecron.demo.core.dto.Product
|
||||
import com.github.dannecron.demo.core.services.generation.CommonGenerator
|
||||
import com.github.dannecron.demo.db.entity.order.OrderProductEntity
|
||||
import com.github.dannecron.demo.db.repository.OrderProductRepository
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.doReturn
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.times
|
||||
import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.whenever
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class OrderProductServiceImplTest {
|
||||
private val mockGuid = UUID.randomUUID()
|
||||
private val mockCurrentTime = OffsetDateTime.now()
|
||||
|
||||
private val orderProductRepository: OrderProductRepository = mock()
|
||||
private val commonGenerator: CommonGenerator = mock {
|
||||
on { generateUUID() } doReturn mockGuid
|
||||
on { generateCurrentTime() } doReturn mockCurrentTime
|
||||
}
|
||||
|
||||
private val orderProductService = OrderProductServiceImpl(
|
||||
orderProductRepository = orderProductRepository,
|
||||
commonGenerator = commonGenerator
|
||||
)
|
||||
|
||||
private val orderId = 22L
|
||||
private val productId = 12L
|
||||
private val order = Order(
|
||||
id = orderId,
|
||||
guid = UUID.randomUUID(),
|
||||
customerId = 44L,
|
||||
deliveredAt = null,
|
||||
createdAt = OffsetDateTime.now(),
|
||||
updatedAt = null,
|
||||
)
|
||||
private val product = Product(
|
||||
id = productId,
|
||||
guid = UUID.randomUUID(),
|
||||
name = "prod",
|
||||
description = "some",
|
||||
price = 10050,
|
||||
createdAt = OffsetDateTime.now(),
|
||||
updatedAt = null,
|
||||
deletedAt = null
|
||||
)
|
||||
private val orderProductEntity = OrderProductEntity(
|
||||
guid = mockGuid,
|
||||
orderId = orderId,
|
||||
productId = productId,
|
||||
createdAt = mockCurrentTime,
|
||||
updatedAt = null,
|
||||
)
|
||||
private val orderProduct = OrderProduct(
|
||||
guid = mockGuid,
|
||||
orderId = orderId,
|
||||
productId = productId,
|
||||
createdAt = mockCurrentTime,
|
||||
updatedAt = null,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `create - success`() {
|
||||
whenever(orderProductRepository.save(any<OrderProductEntity>())).thenReturn(orderProductEntity)
|
||||
|
||||
val result = orderProductService.create(order, product)
|
||||
|
||||
assertEquals(orderProduct, result)
|
||||
|
||||
verify(orderProductRepository, times(1)).save(orderProductEntity)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.github.dannecron.demo.core.services.order
|
||||
|
||||
import com.github.dannecron.demo.core.dto.Customer
|
||||
import com.github.dannecron.demo.core.dto.Order
|
||||
import com.github.dannecron.demo.core.services.generation.CommonGenerator
|
||||
import com.github.dannecron.demo.db.entity.order.OrderEntity
|
||||
import com.github.dannecron.demo.db.repository.OrderRepository
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.doReturn
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.times
|
||||
import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.whenever
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class OrderServiceImplTest {
|
||||
private val mockGuid = UUID.randomUUID()
|
||||
private val mockCurrentTime = OffsetDateTime.now()
|
||||
|
||||
private val orderRepository: OrderRepository = mock()
|
||||
private val commonGenerator: CommonGenerator = mock {
|
||||
on { generateUUID() } doReturn mockGuid
|
||||
on { generateCurrentTime() } doReturn mockCurrentTime
|
||||
}
|
||||
|
||||
private val orderService = OrderServiceImpl(
|
||||
orderRepository = orderRepository,
|
||||
commonGenerator = commonGenerator,
|
||||
)
|
||||
|
||||
private val customerId = 123L
|
||||
private val orderEntity = OrderEntity(
|
||||
id = 22,
|
||||
guid = mockGuid,
|
||||
customerId = customerId,
|
||||
deliveredAt = null,
|
||||
createdAt = mockCurrentTime,
|
||||
updatedAt = null,
|
||||
)
|
||||
private val order = Order(
|
||||
id = 22,
|
||||
guid = mockGuid,
|
||||
customerId = customerId,
|
||||
deliveredAt = null,
|
||||
createdAt = mockCurrentTime,
|
||||
updatedAt = null,
|
||||
)
|
||||
|
||||
private val customer = Customer(
|
||||
id = customerId,
|
||||
guid = UUID.randomUUID(),
|
||||
name = "name",
|
||||
cityId = null,
|
||||
createdAt = OffsetDateTime.now(),
|
||||
updatedAt = null,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun `findByCustomerId - success`() {
|
||||
whenever(orderRepository.findByCustomerId(any())).thenReturn(listOf(orderEntity))
|
||||
|
||||
val result = orderService.findByCustomerId(customerId)
|
||||
|
||||
assertEquals(listOf(order), result)
|
||||
|
||||
verify(orderRepository, times(1)).findByCustomerId(customerId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `create - success`() {
|
||||
val orderEntityForCreation = orderEntity.copy(id = null)
|
||||
|
||||
whenever(orderRepository.save(any<OrderEntity>())).thenReturn(orderEntity)
|
||||
|
||||
val result = orderService.create(customer)
|
||||
|
||||
assertEquals(order, result)
|
||||
|
||||
verify(orderRepository, times(1)).save(orderEntityForCreation)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user