Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller Test Improvements #33

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 109 additions & 10 deletions src/test/kotlin/no/nb/mlt/wls/order/controller/OrderControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import no.nb.mlt.wls.EnableTestcontainers
import no.nb.mlt.wls.core.data.HostName
import no.nb.mlt.wls.core.data.Owner
import no.nb.mlt.wls.core.data.synq.SynqError
import no.nb.mlt.wls.order.model.Order
import no.nb.mlt.wls.order.model.OrderLineStatus
import no.nb.mlt.wls.order.model.OrderReceiver
import no.nb.mlt.wls.order.model.OrderStatus
import no.nb.mlt.wls.order.model.OrderType
import no.nb.mlt.wls.order.model.ProductLine
import no.nb.mlt.wls.order.payloads.ApiOrderPayload
import no.nb.mlt.wls.order.payloads.toOrder
import no.nb.mlt.wls.order.payloads.toUpdateOrderPayload
import no.nb.mlt.wls.order.repository.OrderRepository
import no.nb.mlt.wls.order.service.SynqOrderService
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -32,6 +34,7 @@ import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.context.ApplicationContext
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf
Expand Down Expand Up @@ -102,14 +105,14 @@ class OrderControllerTest(
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.post()
.accept(MediaType.APPLICATION_JSON)
.bodyValue(duplicateOrderPayload)
.bodyValue(dop)
.exchange()
.expectStatus().isOk
.expectBody<ApiOrderPayload>()
.consumeWith { response ->
assertThat(response.responseBody?.hostOrderId).isEqualTo(duplicateOrderPayload.hostOrderId)
assertThat(response.responseBody?.hostName).isEqualTo(duplicateOrderPayload.hostName)
assertThat(response.responseBody?.productLine).isEqualTo(duplicateOrderPayload.productLine)
assertThat(response.responseBody?.hostOrderId).isEqualTo(dop.hostOrderId)
assertThat(response.responseBody?.hostName).isEqualTo(dop.hostName)
assertThat(response.responseBody?.productLine).isEqualTo(dop.productLine)
}
}

Expand All @@ -121,13 +124,13 @@ class OrderControllerTest(
.post()
.accept(MediaType.APPLICATION_JSON)
.bodyValue(
duplicateOrderPayload.copy(productLine = listOf(ProductLine("AAAAAAAAA", OrderLineStatus.PICKED)))
dop.copy(productLine = listOf(ProductLine("AAAAAAAAA", OrderLineStatus.PICKED)))
)
.exchange()
.expectStatus().isOk
.expectBody<ApiOrderPayload>()
.consumeWith { response ->
assertThat(response.responseBody?.productLine).isEqualTo(duplicateOrderPayload.productLine)
assertThat(response.responseBody?.productLine).isEqualTo(dop.productLine)
}
}

Expand Down Expand Up @@ -164,6 +167,86 @@ class OrderControllerTest(
.expectStatus().is5xxServerError
}

// TODO - Should this endpoint really be returning Orders directly?
anotheroneofthese marked this conversation as resolved.
Show resolved Hide resolved
@Test
fun `getOrder returns the order`() {
webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.get()
.uri("/{hostName}/{hostOrderId}", dop.hostName, dop.hostOrderId)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk
.expectBody(Order::class.java)
.consumeWith { response ->
assertThat(response?.responseBody?.hostOrderId.equals(dop.hostOrderId))
assertThat(response?.responseBody?.status?.equals(dop.status))
}
}

@Test
fun `getOrder for wrong client throws`() {
webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject("ALMA") })
.get()
.uri("/{hostName}/{hostOrderId}", dop.hostName, dop.hostOrderId)
.exchange()
.expectStatus().isForbidden
}

@Test
fun `updateOrder with valid payload updates order`() {
coEvery {
synqOrderService.updateOrder(any())
} returns ResponseEntity.ok().build()

val testPayload =
dop.toOrder().toUpdateOrderPayload()
.copy(
productLine =
listOf(
ProductLine("mlt-420", OrderLineStatus.NOT_STARTED),
ProductLine("mlt-421", OrderLineStatus.NOT_STARTED)
)
)

webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.put()
.bodyValue(testPayload)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk
.expectBody<ApiOrderPayload>()
.consumeWith { response ->
val products = response.responseBody?.productLine
products?.map {
assertThat(testPayload.productLine.contains(it))
}
}
}

@Test
fun `updateOrder when order is being processed errors`() {
val testPayload = testOrderPayload.copy(orderId = "mlt-test-order-processing", status = OrderStatus.IN_PROGRESS)
val testUpdatePayload = testPayload.toOrder().toUpdateOrderPayload().copy(orderType = OrderType.DIGITIZATION)
runTest {
repository.save(testPayload.toOrder()).awaitSingle()

webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.put()
.bodyValue(testUpdatePayload)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isEqualTo(HttpStatus.CONFLICT)
}
}

@Test
fun `deleteOrder with valid data deletes order`() =
runTest {
Expand All @@ -175,16 +258,31 @@ class OrderControllerTest(
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject("axiell") })
.delete()
.uri("/${duplicateOrderPayload.hostName}/${duplicateOrderPayload.hostOrderId}")
.uri("/${dop.hostName}/${dop.hostOrderId}")
anotheroneofthese marked this conversation as resolved.
Show resolved Hide resolved
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk

val order = repository.findByHostNameAndHostOrderId(duplicateOrderPayload.hostName, duplicateOrderPayload.hostOrderId).awaitSingleOrNull()
val order = repository.findByHostNameAndHostOrderId(dop.hostName, dop.hostOrderId).awaitSingleOrNull()

assertThat(order).isNull()
}

@Test
fun `deleteOrder handles synq error`() {
coEvery {
synqOrderService.deleteOrder(any(), any())
} returns ResponseEntity.internalServerError().build()
webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().jwt { it.subject(clientName) })
.delete()
.uri("/${dop.hostName}/${dop.hostOrderId}")
.exchange()
.expectStatus().is5xxServerError
assertThat(true)
}

// /////////////////////////////////////////////////////////////////////////////
// //////////////////////////////// Test Help //////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////
Expand All @@ -211,8 +309,9 @@ class OrderControllerTest(
callbackUrl = "callbackUrl"
)

// dop = Duplicate Order Payload
// Will exist in the database
private val duplicateOrderPayload =
private val dop =
anotheroneofthese marked this conversation as resolved.
Show resolved Hide resolved
ApiOrderPayload(
orderId = "order-123456",
hostName = HostName.AXIELL,
Expand All @@ -236,7 +335,7 @@ class OrderControllerTest(
fun populateDb() {
// Make sure we start with clean DB instance for each test
runBlocking {
repository.deleteAll().then(repository.save(duplicateOrderPayload.toOrder())).awaitSingle()
repository.deleteAll().then(repository.save(dop.toOrder())).awaitSingle()
}
}
}