Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyfrs committed May 20, 2024
1 parent d81c772 commit 621608b
Show file tree
Hide file tree
Showing 30 changed files with 1,097 additions and 198 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
<java.version>17</java.version>
<kotlin.version>1.8.22</kotlin.version>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>

<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
<testcontainers.version>1.19.1</testcontainers.version>

<skipITs>true</skipITs>
<skipOpenAPIGen>true</skipOpenAPIGen>

<sonar.organization>fiap-3soat-g15</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
Expand Down Expand Up @@ -375,6 +378,12 @@
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<configuration>
<excludes>
<exclude>**/*Config.*</exclude>
<exclude>**/*Mapper.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ interface StockApiClient {
consumes = ["application/json"]
)
fun getByProductNumber(@PathVariable("productNumber") productNumber: Long): Product

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ import java.util.*

@RestController
class CustomerController(
private val loadCustomersUseCase: LoadCustomerUseCase,
private val loadCustomerUseCase: LoadCustomerUseCase,
private val searchCustomerUseCase: SearchCustomerUseCase,
private val createCustomerUseCase: CreateCustomerUseCase,
private val updateCustomerUseCase: UpdateCustomerUseCase,
private val removeCustomerUseCase: RemoveCustomerUseCase,
) : CustomersAPI {
) : CustomersAPI
{
override fun getById(customerId: String): ResponseEntity<Customer?> {
customerId
.runCatching { UUID.fromString(this) }
.getOrElse { return ResponseEntity.notFound().build() }
.run { return ResponseEntity.ok(loadCustomersUseCase.getById(this)) }
.run { return ResponseEntity.ok(loadCustomerUseCase.getByCustomerId(this)) }
}

override fun findAll(): ResponseEntity<List<Customer>> {
return ResponseEntity.ok(loadCustomersUseCase.findAll())
return ResponseEntity.ok(loadCustomerUseCase.findAll())
}

override fun searchByName(name: String): ResponseEntity<List<Customer>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.fiap.order.driver.web.OrdersAPI
import com.fiap.order.driver.web.request.OrderRequest
import com.fiap.order.driver.web.response.PendingOrderResponse
import com.fiap.order.usecases.ChangeOrderStatusUseCase
import com.fiap.order.usecases.LoadOrderUseCase
import com.fiap.order.usecases.CreateOrderUseCase
import com.fiap.order.usecases.LoadOrderUseCase
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
import org.springframework.security.core.context.SecurityContextHolder
Expand All @@ -17,78 +17,72 @@ import java.util.*

@RestController
class OrderController(
private val loadOrdersUseCase: LoadOrderUseCase,
private val loadOrderUseCase: LoadOrderUseCase,
private val createOrderUseCase: CreateOrderUseCase,
private val changeOrderStatusUseCase: ChangeOrderStatusUseCase,
) : OrdersAPI
{
private val log = LoggerFactory.getLogger(javaClass)

override fun getByOrderNumber(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(loadOrdersUseCase.getByOrderNumber(orderNumber))
}
override fun getByOrderNumber(orderNumber: Long): ResponseEntity<Order> =
ResponseEntity.ok(loadOrderUseCase.getByOrderNumber(orderNumber))

override fun findAll(): ResponseEntity<List<Order>> {
return ResponseEntity.ok(loadOrdersUseCase.findAll())
}
override fun findAllActive(): ResponseEntity<List<Order>> =
ResponseEntity.ok(loadOrderUseCase.findAllActive())

override fun getByStatus(status: String): ResponseEntity<List<Order>> {
return ResponseEntity.ok(loadOrdersUseCase.findByStatus(OrderStatus.fromString(status)))
}
override fun findAll(): ResponseEntity<List<Order>> =
ResponseEntity.ok(loadOrderUseCase.findAll())

override fun findByStatus(status: String): ResponseEntity<List<Order>> =
ResponseEntity.ok(loadOrderUseCase.findByStatus(OrderStatus.fromString(status)))

override fun getByStatusAndCustomerId(
status: String,
customerId: String,
): ResponseEntity<List<Order>> {
override fun findByStatusAndCustomerId(status: String, customerId: String, ): ResponseEntity<List<Order>> {
val orderStatus = OrderStatus.fromString(status)
customerId
.runCatching { UUID.fromString(this) }
.getOrElse { return ResponseEntity.notFound().build() }
.let { loadOrdersUseCase.findByCustomerIdAndStatus(it, orderStatus) }
.let { loadOrderUseCase.findByStatusAndCustomerId(orderStatus, it) }
.run { return ResponseEntity.ok(this) }
}

override fun getByCustomerId(customerId: String): ResponseEntity<List<Order>> {
override fun findByCustomerId(customerId: String): ResponseEntity<List<Order>> =
customerId
.runCatching { UUID.fromString(this) }
.getOrElse { return ResponseEntity.notFound().build() }
.let { loadOrdersUseCase.findByCustomerId(it) }
.let { loadOrderUseCase.findByCustomerId(it) }
.run { return ResponseEntity.ok(this) }
}

override fun create(orderRequest: OrderRequest): ResponseEntity<PendingOrderResponse> {
var customerId: UUID? = null
try {
customerId = UUID.fromString(
(SecurityContextHolder.getContext().authentication.credentials as ClaimAccessor)
.getClaim("custom:CUSTOMER_ID")
override fun create(orderRequest: OrderRequest): ResponseEntity<PendingOrderResponse> =
ResponseEntity.ok(
createOrderUseCase.create(
customerId = getAuthenticatedCustomerId(),
items = orderRequest.toOrderItemsDomain()
)
} catch (ex: Exception) {
log.error(ex.message, ex.cause)
}

val orderToPayResponse = createOrderUseCase.create(customerId, orderRequest.toOrderItemsDomain())
)

return ResponseEntity.ok(orderToPayResponse)
}

override fun start(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(changeOrderStatusUseCase.startOrderPreparation(orderNumber))
}
override fun start(orderNumber: Long): ResponseEntity<Order> =
ResponseEntity.ok(changeOrderStatusUseCase.startOrderPreparation(orderNumber))

override fun finish(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(changeOrderStatusUseCase.finishOrderPreparation(orderNumber))
}
override fun finish(orderNumber: Long): ResponseEntity<Order> =
ResponseEntity.ok(changeOrderStatusUseCase.finishOrderPreparation(orderNumber))

override fun complete(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(changeOrderStatusUseCase.completeOrder(orderNumber))
}
override fun complete(orderNumber: Long): ResponseEntity<Order> =
ResponseEntity.ok(changeOrderStatusUseCase.completeOrder(orderNumber))

override fun cancel(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(changeOrderStatusUseCase.cancelOrder(orderNumber))
}
override fun cancel(orderNumber: Long): ResponseEntity<Order> =
ResponseEntity.ok(changeOrderStatusUseCase.cancelOrder(orderNumber))

override fun confirm(orderNumber: Long): ResponseEntity<Order> {
return ResponseEntity.ok(changeOrderStatusUseCase.confirmOrder(orderNumber))
}
override fun confirm(orderNumber: Long): ResponseEntity<Order> =
ResponseEntity.ok(changeOrderStatusUseCase.confirmOrder(orderNumber))

private fun getAuthenticatedCustomerId(): UUID? =
runCatching {
UUID.fromString(
(SecurityContextHolder.getContext().authentication.credentials as ClaimAccessor)
.getClaim("custom:CUSTOMER_ID")
)
}.getOrElse {
log.error(it.message, it.cause)
null
}
}
10 changes: 6 additions & 4 deletions src/main/kotlin/com/fiap/order/adapter/gateway/OrderGateway.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import com.fiap.order.domain.valueobjects.OrderStatus
import java.util.*

interface OrderGateway {
fun findAllActiveOrders(): List<Order>
fun findAll(): List<Order>

fun findAllActive(): List<Order>

fun findByOrderNumber(number: Long): Order?

fun findByStatus(status: OrderStatus): List<Order>

fun findByCustomerId(customerId: UUID): List<Order>

fun findByCustomerIdAndStatus(customerId: UUID, status: OrderStatus): List<Order>
fun findByStatusAndCustomerId(status: OrderStatus, customerId: UUID): List<Order>

fun findByCustomerId(customerId: UUID): List<Order>

fun upsert(order: Order): Order

fun deleteAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,21 @@ import org.mapstruct.factory.Mappers
import java.util.*

class CustomerGatewayImpl(
private val customerJpaRepository: CustomerJpaRepository,
private val customerRepository: CustomerJpaRepository,
) : CustomerGateway {
private val mapper: CustomerMapper = Mappers.getMapper(CustomerMapper::class.java)

override fun findAll(): List<Customer> {
return customerJpaRepository.findAll()
.map(mapper::toDomain)
}
override fun findAll(): List<Customer> =
customerRepository.findAll().map(mapper::toDomain)

override fun findById(customerId: UUID): Customer? {
return customerJpaRepository.findById(customerId.toString())
.map(mapper::toDomain)
.orElse(null)
}
override fun findById(customerId: UUID): Customer? =
customerRepository.findById(customerId.toString()).map(mapper::toDomain).orElse(null)

override fun searchByName(name: String): List<Customer> {
return customerJpaRepository.findByNameContainingIgnoreCase(name)
.map(mapper::toDomain)
}
override fun searchByName(name: String): List<Customer> =
customerRepository.findByNameContainingIgnoreCase(name).map(mapper::toDomain)

override fun searchByEmail(email: String): Customer? {
return customerJpaRepository.findByEmail(email)
.map(mapper::toDomain)
.orElse(null)
}
override fun searchByEmail(email: String): Customer? =
customerRepository.findByEmail(email).map(mapper::toDomain).orElse(null)

override fun create(customer: Customer): Customer {
customer.email
Expand All @@ -58,11 +48,8 @@ class CustomerGatewayImpl(
return persist(customer)
}

override fun searchByDocument(document: String): Customer? {
return customerJpaRepository.findByDocument(document)
.map(mapper::toDomain)
.orElse(null)
}
override fun searchByDocument(document: String): Customer? =
customerRepository.findByDocument(document).map(mapper::toDomain).orElse(null)

override fun update(customer: Customer): Customer {
val newItem =
Expand All @@ -78,7 +65,7 @@ class CustomerGatewayImpl(
override fun deleteById(customerId: UUID): Customer {
return findById(customerId)
?.let {
customerJpaRepository.deleteById(customerId.toString())
customerRepository.deleteById(customerId.toString())
it
}
?: throw SelfOrderManagementException(
Expand All @@ -88,12 +75,12 @@ class CustomerGatewayImpl(
}

override fun deleteAll() {
customerJpaRepository.deleteAll()
customerRepository.deleteAll()
}

private fun persist(customer: Customer): Customer =
customer
.let(mapper::toEntity)
.let(customerJpaRepository::save)
.let(customerRepository::save)
.let(mapper::toDomain)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import org.mapstruct.factory.Mappers
import java.util.*

class OrderGatewayImpl(
private val orderJpaRepository: OrderJpaRepository,
private val orderRepository: OrderJpaRepository,
) : OrderGateway {
private val mapper = Mappers.getMapper(OrderMapper::class.java)

override fun findAllActiveOrders(): List<Order> {
return orderJpaRepository
override fun findAll(): List<Order> =
orderRepository.findAll().map(mapper::toDomain)

override fun findAllActive(): List<Order>
= orderRepository
.findAllByStatusInOrderByStatusDescNumberAsc(
setOf(
OrderStatus.CONFIRMED,
Expand All @@ -23,31 +26,18 @@ class OrderGatewayImpl(
),
)
.map(mapper::toDomain)
}

override fun findByOrderNumber(number: Long): Order? {
return orderJpaRepository.findById(number)
.map(mapper::toDomain)
.orElse(null)
}

override fun findByStatus(status: OrderStatus): List<Order> {
return orderJpaRepository.findByStatus(status)
.map(mapper::toDomain)
}
override fun findByOrderNumber(number: Long): Order? =
orderRepository.findById(number).map(mapper::toDomain).orElse(null)

override fun findByCustomerId(customerId: UUID): List<Order> {
return orderJpaRepository.findByCustomerId(customerId)
.map(mapper::toDomain)
}
override fun findByStatus(status: OrderStatus): List<Order> =
orderRepository.findByStatus(status).map(mapper::toDomain)

override fun findByCustomerIdAndStatus(
customerId: UUID,
status: OrderStatus,
): List<Order> {
return orderJpaRepository.findByCustomerIdAndStatus(customerId, status)
.map(mapper::toDomain)
}
override fun findByStatusAndCustomerId(status: OrderStatus, customerId: UUID): List<Order> =
orderRepository.findByCustomerIdAndStatus(customerId, status).map(mapper::toDomain)

override fun findByCustomerId(customerId: UUID): List<Order> =
orderRepository.findByCustomerId(customerId).map(mapper::toDomain)

override fun upsert(order: Order): Order {
val currentOrder = order.number?.let { findByOrderNumber(number = order.number) }
Expand All @@ -65,12 +55,12 @@ class OrderGatewayImpl(
}

override fun deleteAll() {
orderJpaRepository.deleteAll()
orderRepository.deleteAll()
}

private fun persist(order: Order): Order =
order
.let(mapper::toEntity)
.let(orderJpaRepository::save)
.let(orderRepository::save)
.let(mapper::toDomain)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PaymentGatewayImpl(

return paymentsApiClient.create(paymentHTTPRequest)
}

companion object {
const val ANONYMOUS = "Anonymous"
}
Expand Down
Loading

0 comments on commit 621608b

Please sign in to comment.