-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added framework for order controller tests
- Loading branch information
1 parent
eb42f14
commit 75a4ed4
Showing
1 changed file
with
143 additions
and
0 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
src/test/kotlin/no/nb/mlt/wls/order/controller/OrderControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package no.nb.mlt.wls.order.controller | ||
|
||
import com.ninjasquad.springmockk.MockkBean | ||
import io.mockk.junit5.MockKExtension | ||
import kotlinx.coroutines.reactor.awaitSingle | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.test.runTest | ||
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.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.repository.OrderRepository | ||
import no.nb.mlt.wls.order.service.OrderService | ||
import no.nb.mlt.wls.order.service.SynqOrderService | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT | ||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories | ||
import org.springframework.security.test.context.support.WithMockUser | ||
import org.springframework.test.web.reactive.server.WebTestClient | ||
|
||
@EnableTestcontainers | ||
@TestInstance(PER_CLASS) | ||
@AutoConfigureWebTestClient | ||
@ExtendWith(MockKExtension::class) | ||
@EnableMongoRepositories("no.nb.mlt.wls") | ||
@SpringBootTest(webEnvironment = RANDOM_PORT) | ||
class OrderControllerTest( | ||
@Autowired val repository: OrderRepository | ||
) { | ||
@MockkBean | ||
private lateinit var synqOrderService: SynqOrderService | ||
|
||
private lateinit var webTestClient: WebTestClient | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
webTestClient = | ||
WebTestClient | ||
.bindToController(OrderController(OrderService(repository, synqOrderService))) | ||
.configureClient() | ||
.baseUrl("/v1/order") | ||
.build() | ||
|
||
populateDb() | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
fun `createOrder with valid payload creates order`() = | ||
runTest { | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
fun `createOrder with duplicate payload returns OK`() { | ||
// How to handle this? OK or error? | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
fun `createOrder payload with different data but same ID returns DB entry`() { | ||
// How to handle this? OK or error? | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
fun `createOrder where SynQ says it's a duplicate returns OK`() { | ||
// How to handle this? OK or error? | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
fun `createOrder handles SynQ error`() { | ||
} | ||
|
||
// ///////////////////////////////////////////////////////////////////////////// | ||
// //////////////////////////////// Test Help ////////////////////////////////// | ||
// ///////////////////////////////////////////////////////////////////////////// | ||
|
||
// Will be used in most tests | ||
private val testOrderPayload = | ||
ApiOrderPayload( | ||
orderId = "axiell-order-69", | ||
hostName = HostName.AXIELL, | ||
hostOrderId = "axiell-order-69", | ||
status = OrderStatus.NOT_STARTED, | ||
productLine = listOf(ProductLine("mlt-420", OrderLineStatus.NOT_STARTED)), | ||
orderType = OrderType.LOAN, | ||
owner = Owner.NB, | ||
receiver = | ||
OrderReceiver( | ||
name = "name", | ||
address = "address", | ||
postalCode = "postalCode", | ||
city = "city", | ||
phoneNumber = "phoneNumber", | ||
location = "location" | ||
), | ||
callbackUrl = "callbackUrl" | ||
) | ||
|
||
// Will exist in the database | ||
private val duplicateOrderPayload = | ||
ApiOrderPayload( | ||
orderId = "order-123456", | ||
hostName = HostName.AXIELL, | ||
hostOrderId = "order-123456", | ||
status = OrderStatus.NOT_STARTED, | ||
productLine = listOf(ProductLine("product-123456", OrderLineStatus.NOT_STARTED)), | ||
orderType = OrderType.LOAN, | ||
owner = Owner.NB, | ||
receiver = | ||
OrderReceiver( | ||
name = "name", | ||
address = "address", | ||
postalCode = "postalCode", | ||
city = "city", | ||
phoneNumber = "phoneNumber", | ||
location = "location" | ||
), | ||
callbackUrl = "callbackUrl" | ||
) | ||
|
||
fun populateDb() { | ||
// Make sure we start with clean DB instance for each test | ||
runBlocking { | ||
repository.deleteAll().then(repository.save(duplicateOrderPayload.toOrder())).awaitSingle() | ||
} | ||
} | ||
} |