Skip to content

Commit

Permalink
feat: use webClient for handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasBousselin committed Sep 6, 2024
1 parent 4275497 commit 0a83c97
Showing 1 changed file with 76 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.egm.stellio.search.csr.handler

import arrow.core.left
import arrow.core.right
import com.egm.stellio.search.common.util.deserializeAsMap
import com.egm.stellio.search.common.config.SearchProperties
import com.egm.stellio.search.csr.model.ContextSourceRegistration
import com.egm.stellio.search.csr.service.ContextSourceRegistrationService
import com.egm.stellio.shared.config.ApplicationProperties
Expand All @@ -12,45 +12,47 @@ import com.egm.stellio.shared.util.*
import com.ninjasquad.springmockk.MockkBean
import io.mockk.coEvery
import io.mockk.coVerify
import io.r2dbc.postgresql.codec.Json
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.core.io.ClassPathResource
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf
import org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockJwt
import org.springframework.test.context.ActiveProfiles
import reactor.core.publisher.Mono
import org.springframework.test.web.reactive.server.WebTestClient

@ActiveProfiles("test")
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.NONE,
classes = [ContextSourceRegistrationHandler::class]
)
@EnableConfigurationProperties(ApplicationProperties::class)
@EnableConfigurationProperties(ApplicationProperties::class, SearchProperties::class)
@AutoConfigureWebTestClient(timeout = "30000")
@WebFluxTest(ContextSourceRegistrationHandler::class)
class ContextSourceRegistrationHandlerTests {

@Autowired
private lateinit var contextSourceRegistrationHandler: ContextSourceRegistrationHandler

@MockkBean
private lateinit var contextSourceRegistrationService: ContextSourceRegistrationService

@Autowired
private lateinit var webClient: WebTestClient

private val csrUri = "/ngsi-ld/v1/csourceRegistrations"
private val id = "urn:ngsi-ld:ContextSourceRegistration:1".toUri()
private val endpoint = "http://my.csr.endpoint/".toUri()
private val headers = HttpHeaders().apply {
accept = listOf(JSON_LD_MEDIA_TYPE)
contentType = JSON_LD_MEDIA_TYPE
}

val headersWithContext = HttpHeaders().apply {
accept = listOf(MediaType.APPLICATION_JSON)
contentType = MediaType.APPLICATION_JSON
add(HttpHeaders.LINK, AQUAC_HEADER_LINK)
@BeforeAll
fun configureWebClientDefaults() {
webClient = webClient.mutate()
.apply(mockJwt().jwt { it.subject(MOCK_USER_SUB) })
.apply(csrf())
.defaultHeaders {
it.accept = listOf(JSON_LD_MEDIA_TYPE)
it.contentType = JSON_LD_MEDIA_TYPE
}
.build()
}

@Test
Expand All @@ -61,11 +63,14 @@ class ContextSourceRegistrationHandlerTests {
coEvery { contextSourceRegistrationService.isCreatorOf(any(), any()) } returns true.right()
coEvery { contextSourceRegistrationService.getById(any()) } returns contextSourceRegistration.right()

val response = contextSourceRegistrationHandler.getByURI(headers, id, null)
val newCSR = Json.of(response.body as String).deserializeAsMap()
webClient.get()
.uri("$csrUri/$id")
.exchange()
.expectStatus().isOk
.expectBody()
.jsonPath("$..createdAt").doesNotExist()
.jsonPath("$..modifiedAt").doesNotExist()

assertNull(newCSR["modifiedAt"])
assertNull(newCSR["createdAt"])
coVerify { contextSourceRegistrationService.getById(id) }
}

Expand All @@ -77,10 +82,12 @@ class ContextSourceRegistrationHandlerTests {
coEvery { contextSourceRegistrationService.isCreatorOf(any(), any()) } returns true.right()
coEvery { contextSourceRegistrationService.getById(any()) } returns contextSourceRegistration.right()

val response = contextSourceRegistrationHandler.getByURI(headers, id, QUERY_PARAM_OPTIONS_SYSATTRS_VALUE)
val newCSR = Json.of(response.body as String).deserializeAsMap()

assertNotNull(newCSR["createdAt"])
webClient.get()
.uri("$csrUri/$id?options=sysAttrs")
.exchange()
.expectStatus().isOk
.expectBody()
.jsonPath("$..createdAt").exists()

coVerify { contextSourceRegistrationService.getById(id) }
}
Expand All @@ -93,8 +100,10 @@ class ContextSourceRegistrationHandlerTests {
coEvery { contextSourceRegistrationService.isCreatorOf(any(), any()) } returns true.right()
coEvery { contextSourceRegistrationService.getById(any()) } returns ResourceNotFoundException("").left()

val response = contextSourceRegistrationHandler.getByURI(headers, id, QUERY_PARAM_OPTIONS_SYSATTRS_VALUE)
assertEquals(HttpStatus.NOT_FOUND, response.statusCode)
webClient.get()
.uri("$csrUri/$id")
.exchange()
.expectStatus().isNotFound

coVerify { contextSourceRegistrationService.getById(contextSourceRegistration.id) }
}
Expand All @@ -105,7 +114,10 @@ class ContextSourceRegistrationHandlerTests {
coEvery { contextSourceRegistrationService.isCreatorOf(any(), any()) } returns true.right()
coEvery { contextSourceRegistrationService.delete(any()) } returns ResourceNotFoundException("").left()

contextSourceRegistrationHandler.delete(id)
webClient.delete()
.uri("$csrUri/$id")
.exchange()
.expectStatus().isNotFound

coVerify { contextSourceRegistrationService.delete(eq(id)) }
}
Expand All @@ -116,7 +128,10 @@ class ContextSourceRegistrationHandlerTests {
coEvery { contextSourceRegistrationService.isCreatorOf(any(), any()) } returns true.right()
coEvery { contextSourceRegistrationService.delete(any()) } returns Unit.right()

contextSourceRegistrationHandler.delete(id)
webClient.delete()
.uri("$csrUri/$id")
.exchange()
.expectStatus().isNoContent

coVerify { contextSourceRegistrationService.delete(eq(id)) }
}
Expand All @@ -128,10 +143,16 @@ class ContextSourceRegistrationHandlerTests {

coEvery { contextSourceRegistrationService.create(any(), any()) } returns AlreadyExistsException("").left()

contextSourceRegistrationHandler.create(
headersWithContext,
Mono.just(jsonLdFile.contentAsByteArray.decodeToString())
)
webClient.post()
.uri(csrUri)
.headers {
it.accept = listOf(MediaType.APPLICATION_JSON)
it.contentType = MediaType.APPLICATION_JSON
it.add(HttpHeaders.LINK, AQUAC_HEADER_LINK)
}
.bodyValue(jsonLdFile)
.exchange()
.expectStatus().isEqualTo(409)

coVerify(exactly = 1) { contextSourceRegistrationService.create(any(), any()) }
}
Expand All @@ -143,10 +164,17 @@ class ContextSourceRegistrationHandlerTests {

coEvery { contextSourceRegistrationService.create(any(), any()) } returns Unit.right()

contextSourceRegistrationHandler.create(
headersWithContext,
Mono.just(jsonLdFile.contentAsByteArray.decodeToString())
)
webClient.post()
.uri(csrUri)
.headers {
it.accept = listOf(MediaType.APPLICATION_JSON)
it.contentType = MediaType.APPLICATION_JSON
it.add(HttpHeaders.LINK, AQUAC_HEADER_LINK)
}
.bodyValue(jsonLdFile)
.exchange()
.expectStatus().isCreated
.expectHeader().exists("Location")

coVerify(exactly = 1) { contextSourceRegistrationService.create(any(), any()) }
}
Expand All @@ -158,10 +186,12 @@ class ContextSourceRegistrationHandlerTests {

coEvery { contextSourceRegistrationService.create(any(), any()) } returns Unit.right()

contextSourceRegistrationHandler.create(
headers,
Mono.just(jsonLdFile.contentAsByteArray.decodeToString())
)
webClient.post()
.uri(csrUri)
.bodyValue(jsonLdFile)
.exchange()
.expectStatus().isCreated
.expectHeader().exists("Location")

coVerify(exactly = 1) { contextSourceRegistrationService.create(any(), any()) }
}
Expand Down

0 comments on commit 0a83c97

Please sign in to comment.