diff --git a/src/test/kotlin/no/nb/mlt/wls/product/controller/ProductControllerTest.kt b/src/test/kotlin/no/nb/mlt/wls/product/controller/ProductControllerTest.kt index 4e8d7b12..f9ebae9e 100644 --- a/src/test/kotlin/no/nb/mlt/wls/product/controller/ProductControllerTest.kt +++ b/src/test/kotlin/no/nb/mlt/wls/product/controller/ProductControllerTest.kt @@ -1,5 +1,7 @@ package no.nb.mlt.wls.product.controller +import com.ninjasquad.springmockk.MockkBean +import io.mockk.every import io.mockk.junit5.MockKExtension import no.nb.mlt.wls.EnableTestcontainers import no.nb.mlt.wls.core.data.Environment.NONE @@ -22,11 +24,16 @@ import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWeb 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.http.HttpStatus import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity import org.springframework.security.test.context.support.WithMockUser import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf import org.springframework.test.web.reactive.server.WebTestClient import org.springframework.test.web.reactive.server.expectBody +import org.springframework.web.client.HttpClientErrorException +import org.springframework.web.server.ServerErrorException +import java.net.URI @EnableTestcontainers @TestInstance(PER_CLASS) @@ -35,9 +42,11 @@ import org.springframework.test.web.reactive.server.expectBody @EnableMongoRepositories("no.nb.mlt.wls") @SpringBootTest(webEnvironment = RANDOM_PORT) class ProductControllerTest( - @Autowired val repository: ProductRepository, - @Autowired val synqProductService: SynqProductService + @Autowired val repository: ProductRepository ) { + @MockkBean + private lateinit var synqProductService: SynqProductService + private lateinit var webTestClient: WebTestClient @BeforeEach @@ -55,6 +64,10 @@ class ProductControllerTest( @Test @WithMockUser fun `createProduct with valid payload creates product`() { + every { + synqProductService.createProduct(any()) + } returns ResponseEntity.created(URI.create("")).build() + webTestClient .mutateWith(csrf()) .post() @@ -111,6 +124,10 @@ class ProductControllerTest( @Test @WithMockUser fun `createProduct where SynQ says it's a duplicate returns OK`() { + every { + synqProductService.createProduct(any()) + } returns ResponseEntity.ok().build() + webTestClient .mutateWith(csrf()) .post() @@ -124,6 +141,16 @@ class ProductControllerTest( @Test @WithMockUser fun `createProduct handles SynQ error`() { + every { + synqProductService.createProduct(any()) + }.throws( + ServerErrorException( + "Failed to create product in SynQ, the storage system responded with " + + "error code: '1002' and error text: 'Unknown product category TEST.'", + HttpClientErrorException(HttpStatus.NOT_FOUND, "Not found") + ) + ) + webTestClient .mutateWith(csrf()) .post()