Skip to content

Commit

Permalink
Fixed tests by adding a callback model, renamed more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
MormonJesus69420 committed Oct 8, 2024
1 parent e66c19e commit cd0d12b
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.springframework.web.server.ServerWebInputException
description = "Payload for creating and editing an order in Hermes WLS, and appropriate storage system(s).",
example = """
{
"orderId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"hostName": "AXIELL",
"hostOrderId": "mlt-12345-order",
"status": "NOT_STARTED",
Expand Down Expand Up @@ -86,7 +85,7 @@ fun Order.toApiOrderPayload() =
callbackUrl = callbackUrl
)

fun ApiOrderPayload.toOrder(): Order =
fun ApiOrderPayload.toOrder() =
Order(
hostName = hostName,
hostOrderId = hostOrderId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data class SynqOrderStatusUpdatePayload(
val status: SynqOrderStatus,
@Schema(
description = "Name of the host system which placed the order/owns the order products/items.",
example = "Axiell"
example = "AXIELL"
)
val hostName: HostName,
@Schema(
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/no/nb/mlt/wls/domain/WLSService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import no.nb.mlt.wls.domain.ports.inbound.UpdateOrder
import no.nb.mlt.wls.domain.ports.inbound.ValidationException
import no.nb.mlt.wls.domain.ports.inbound.toItem
import no.nb.mlt.wls.domain.ports.inbound.toOrder
import no.nb.mlt.wls.domain.ports.outbound.CallbackHandler
import no.nb.mlt.wls.domain.ports.outbound.DuplicateResourceException
import no.nb.mlt.wls.domain.ports.outbound.InventoryNotifier
import no.nb.mlt.wls.domain.ports.outbound.ItemId
import no.nb.mlt.wls.domain.ports.outbound.ItemRepository
import no.nb.mlt.wls.domain.ports.outbound.OrderRepository
Expand All @@ -36,7 +36,7 @@ class WLSService(
private val itemRepository: ItemRepository,
private val orderRepository: OrderRepository,
private val storageSystemFacade: StorageSystemFacade,
private val callbackHandler: CallbackHandler
private val inventoryNotifier: InventoryNotifier
) : AddNewItem, CreateOrder, DeleteOrder, UpdateOrder, GetOrder, GetItem, OrderStatusUpdate, MoveItem {
override suspend fun addItem(itemMetadata: ItemMetadata): Item {
getItem(itemMetadata.hostName, itemMetadata.hostId)?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package no.nb.mlt.wls.domain.ports.outbound
import no.nb.mlt.wls.domain.model.Item
import no.nb.mlt.wls.domain.model.Order

interface CallbackHandler {
fun handleItemCallback(item: Item)
interface InventoryNotifier {
fun itemChanged(item: Item)

fun handleOrderCallback(order: Order)
fun orderChanged(order: Order)
}
4 changes: 2 additions & 2 deletions src/main/kotlin/no/nb/mlt/wls/infrastructure/BeansConfig.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package no.nb.mlt.wls.infrastructure

import no.nb.mlt.wls.domain.WLSService
import no.nb.mlt.wls.infrastructure.callbacks.CallbackHandlerAdapter
import no.nb.mlt.wls.infrastructure.callbacks.InventoryNotifierAdapter
import no.nb.mlt.wls.infrastructure.repositories.item.ItemRepositoryMongoAdapter
import no.nb.mlt.wls.infrastructure.repositories.order.MongoOrderRepositoryAdapter
import no.nb.mlt.wls.infrastructure.synq.SynqAdapter
Expand All @@ -15,6 +15,6 @@ class BeansConfig {
synqAdapter: SynqAdapter,
itemMongoAdapter: ItemRepositoryMongoAdapter,
orderMongoAdapter: MongoOrderRepositoryAdapter,
callbackHandler: CallbackHandlerAdapter
callbackHandler: InventoryNotifierAdapter
) = WLSService(itemMongoAdapter, orderMongoAdapter, synqAdapter, callbackHandler)
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package no.nb.mlt.wls.infrastructure.callbacks

import no.nb.mlt.wls.application.hostapi.order.toApiOrderPayload
import no.nb.mlt.wls.domain.model.Item
import no.nb.mlt.wls.domain.model.Order
import no.nb.mlt.wls.domain.ports.outbound.CallbackHandler
import no.nb.mlt.wls.domain.ports.outbound.InventoryNotifier
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient

@Component
class CallbackHandlerAdapter(
class InventoryNotifierAdapter(
private val webClient: WebClient
) : CallbackHandler {
override fun handleItemCallback(item: Item) {
) : InventoryNotifier {
override fun itemChanged(item: Item) {
TODO("Future task")
}

override fun handleOrderCallback(order: Order) {
override fun orderChanged(order: Order) {
webClient
.post()
.uri(order.callbackUrl)
.bodyValue(order.toApiOrderPayload())
.bodyValue(order.toNotificationOrderPayload())
.retrieve()
.bodyToMono(Void::class.java)
.retry(5)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package no.nb.mlt.wls.infrastructure.callbacks

import io.swagger.v3.oas.annotations.media.Schema
import no.nb.mlt.wls.domain.model.HostName
import no.nb.mlt.wls.domain.model.Order
import no.nb.mlt.wls.domain.model.Owner

@Schema(
description = "Payload for creating and editing an order in Hermes WLS, and appropriate storage system(s).",
example = """
{
"hostName": "AXIELL",
"hostOrderId": "mlt-12345-order",
"status": "NOT_STARTED",
"orderLine": [
{
"hostId": "mlt-12345",
"status": "NOT_STARTED"
}
],
"orderType": "LOAN",
"owner": "NB",
"receiver": {
"name": "Doug Dimmadome",
"address": "Dimmsdale Dimmadome, 21st Ave. Texas"
},
"callbackUrl": "https://example.com/send/callback/here"
}
"""
)
data class NotificationOrderPayload(
@Schema(
description = "Name of the host system which made the order.",
examples = ["AXIELL", "ALMA", "ASTA", "BIBLIOFIL"]
)
val hostName: HostName,
@Schema(
description = "Order ID from the host system which made the order.",
example = "mlt-12345-order"
)
val hostOrderId: String,
@Schema(
description = "Current status of the order as a whole.",
examples = ["NOT_STARTED", "IN_PROGRESS", "COMPLETED", "DELETED"]
)
val status: Order.Status?,
@Schema(
description = "List of items in the order, also called order lines."
)
val orderLine: List<Order.OrderItem>,
@Schema(
description = "Describes what type of order this is",
examples = ["LOAN", "DIGITIZATION"]
)
val orderType: Order.Type,
@Schema(
description = "Who's the owner of the material in the order.",
examples = ["NB", "ARKIVVERKET"]
)
val owner: Owner?,
@Schema(
description = "Who's the receiver of the material in the order."
)
val receiver: Order.Receiver,
@Schema(
description = "Callback URL for the order used to update the order information in the host system.",
example = "https://example.com/send/callback/here"
)
val callbackUrl: String
)

fun Order.toNotificationOrderPayload() =
NotificationOrderPayload(
hostName = hostName,
hostOrderId = hostOrderId,
status = status,
orderLine = orderLine,
orderType = orderType,
owner = owner,
receiver = receiver,
callbackUrl = callbackUrl
)

fun NotificationOrderPayload.toOrder() =
Order(
hostName = hostName,
hostOrderId = hostOrderId,
status = status ?: Order.Status.NOT_STARTED,
orderLine =
orderLine.map {
Order.OrderItem(it.hostId, it.status)
},
orderType = orderType,
owner = owner,
receiver = receiver,
callbackUrl = callbackUrl
)
42 changes: 21 additions & 21 deletions src/test/kotlin/no/nb/mlt/wls/domain/WLSServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import no.nb.mlt.wls.domain.ports.inbound.ItemMetadata
import no.nb.mlt.wls.domain.ports.inbound.ItemNotFoundException
import no.nb.mlt.wls.domain.ports.inbound.OrderNotFoundException
import no.nb.mlt.wls.domain.ports.inbound.ValidationException
import no.nb.mlt.wls.domain.ports.outbound.CallbackHandler
import no.nb.mlt.wls.domain.ports.outbound.InventoryNotifier
import no.nb.mlt.wls.domain.ports.outbound.ItemRepository
import no.nb.mlt.wls.domain.ports.outbound.OrderRepository
import no.nb.mlt.wls.domain.ports.outbound.StorageSystemException
Expand All @@ -32,7 +32,7 @@ class WLSServiceTest {
private val orderRepoMock = mockk<OrderRepository>()
private val itemRepoMock = mockk<ItemRepository>()
private val storageSystemRepoMock = mockk<StorageSystemFacade>()
private val callbackHandlerMock = mockk<CallbackHandler>()
private val inventoryNotifierMock = mockk<InventoryNotifier>()

@BeforeEach
fun beforeEach() {
Expand All @@ -47,7 +47,7 @@ class WLSServiceTest {
coEvery { itemRepoMock.createItem(any()) } answers { Mono.just(expectedItem) }
coJustRun { storageSystemRepoMock.createItem(any()) }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
val itemResult =
cut.addItem(
Expand Down Expand Up @@ -76,7 +76,7 @@ class WLSServiceTest {
coEvery { itemRepoMock.createItem(any()) } answers { Mono.just(testItem.copy()) }
coJustRun { storageSystemRepoMock.createItem(any()) }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)

runTest {
val newItem =
Expand Down Expand Up @@ -106,7 +106,7 @@ class WLSServiceTest {

coEvery { itemRepoMock.getItem(HostName.AXIELL, "12345") } answers { expectedItem }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
val itemResult = cut.getItem(HostName.AXIELL, "12345")
assertThat(itemResult).isEqualTo(expectedItem)
Expand All @@ -117,7 +117,7 @@ class WLSServiceTest {
fun `getItem should return null if item does not exist`() {
coEvery { itemRepoMock.getItem(HostName.AXIELL, "12345") } answers { null }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
val itemResult = cut.getItem(HostName.AXIELL, "12345")
assertThat(itemResult).isEqualTo(null)
Expand All @@ -134,7 +134,7 @@ class WLSServiceTest {
coEvery { itemRepoMock.getItem(any(), any()) } returns testItem
coEvery { itemRepoMock.moveItem(any(), any(), any(), any()) } returns expectedItem

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
val movedItem = cut.moveItem(testItem.hostId, testItem.hostName, 1.0, "Somewhere nice")
assertThat(movedItem).isEqualTo(expectedItem)
Expand All @@ -149,7 +149,7 @@ class WLSServiceTest {
fun `moveItem should fail when item does not exist`() {
coEvery { itemRepoMock.moveItem(any(), any(), any(), any()) } throws ItemNotFoundException("Item not found")

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
assertThrows<RuntimeException> {
cut.moveItem(testItem.hostId, testItem.hostName, 1.0, "Somewhere nice")
Expand All @@ -165,7 +165,7 @@ class WLSServiceTest {
fun `moveItem throws when count is invalid`() {
coEvery { itemRepoMock.moveItem(any(), any(), -1.0, any()) } throws ValidationException("Location cannot be blank")

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
assertThrows<RuntimeException> {
cut.moveItem(testItem.hostId, testItem.hostName, -1.0, "Somewhere nice")
Expand All @@ -181,7 +181,7 @@ class WLSServiceTest {
fun `moveItem throws when location is blank`() {
coEvery { itemRepoMock.moveItem(any(), any(), any(), any()) } throws ValidationException("Item not found")

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
assertThrows<RuntimeException> {
cut.moveItem(testItem.hostId, testItem.hostName, 1.0, " ")
Expand All @@ -202,7 +202,7 @@ class WLSServiceTest {
coEvery { orderRepoMock.createOrder(any()) } answers { expectedOrder }
coJustRun { storageSystemRepoMock.createOrder(any()) }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
val order = cut.createOrder(testOrder.toCreateOrderDTO())

Expand All @@ -219,7 +219,7 @@ class WLSServiceTest {
orderRepoMock.getOrder(testOrder.hostName, testOrder.hostOrderId)
} answers { testOrder.copy() }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
val order =
cut.createOrder(
Expand All @@ -239,7 +239,7 @@ class WLSServiceTest {
coEvery { orderRepoMock.getOrder(any(), any()) } answers { null }
coEvery { itemRepoMock.doesEveryItemExist(any()) } answers { false }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
assertThrows<ValidationException> {
cut.createOrder(testOrder.toCreateOrderDTO())
Expand All @@ -254,7 +254,7 @@ class WLSServiceTest {
coJustRun { orderRepoMock.deleteOrder(any(), any()) }
coJustRun { storageSystemRepoMock.deleteOrder(any(), any()) }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)

runTest {
cut.deleteOrder(HostName.AXIELL, "12345")
Expand All @@ -268,7 +268,7 @@ class WLSServiceTest {
coEvery { storageSystemRepoMock.deleteOrder(any(), any()) } throws StorageSystemException("Order not found", null)
coEvery { orderRepoMock.getOrder(any(), any()) } throws OrderNotFoundException("Order not found")

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)

runTest {
assertThrows<StorageSystemException> {
Expand All @@ -284,7 +284,7 @@ class WLSServiceTest {
coJustRun { storageSystemRepoMock.deleteOrder(any(), any()) }
coEvery { orderRepoMock.deleteOrder(any(), any()) } throws OrderNotFoundException("Order not found")

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)

runTest {
assertThrows<OrderNotFoundException> {
Expand All @@ -297,7 +297,7 @@ class WLSServiceTest {

@Test
fun `updateOrder with valid items should complete`() {
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
coEvery { itemRepoMock.doesEveryItemExist(any()) } answers { true }
coEvery { orderRepoMock.getOrder(any(), any()) } answers { testOrder.copy() }
coEvery { storageSystemRepoMock.updateOrder(any()) } answers { updatedOrder }
Expand All @@ -324,7 +324,7 @@ class WLSServiceTest {

@Test
fun `updateOrder should fail when order does not exist`() {
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)

coEvery { itemRepoMock.doesEveryItemExist(any()) } answers { true }
coEvery { orderRepoMock.getOrder(any(), any()) } throws OrderNotFoundException("Order not found")
Expand All @@ -349,7 +349,7 @@ class WLSServiceTest {

@Test
fun `updateOrder should fail when items do not exist`() {
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
coEvery { itemRepoMock.doesEveryItemExist(any()) } answers { false }

runTest {
Expand All @@ -376,7 +376,7 @@ class WLSServiceTest {

coEvery { orderRepoMock.getOrder(HostName.AXIELL, "12345") } answers { expectedItem }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
val order = cut.getOrder(HostName.AXIELL, "12345")
assertThat(order).isEqualTo(expectedItem)
Expand All @@ -387,7 +387,7 @@ class WLSServiceTest {
fun `getOrder should return null when order does not exists in DB`() {
coEvery { orderRepoMock.getOrder(any(), any()) } answers { null }

val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, callbackHandlerMock)
val cut = WLSService(itemRepoMock, orderRepoMock, storageSystemRepoMock, inventoryNotifierMock)
runTest {
val order = cut.getOrder(HostName.AXIELL, "12345")
assertThat(order).isNull()
Expand Down

0 comments on commit cd0d12b

Please sign in to comment.