Skip to content

Commit

Permalink
feat: add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasBousselin committed Jan 10, 2025
1 parent 11a359a commit 975dcc6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.egm.stellio.search.authorization.web

import com.egm.stellio.search.authorization.service.AuthorizationService
import com.egm.stellio.search.common.config.SearchProperties
import com.egm.stellio.search.csr.service.ContextSourceRegistrationService
import com.egm.stellio.search.csr.service.ContextSourceCaller
import com.egm.stellio.search.entity.service.EntityEventService
import com.egm.stellio.search.entity.service.EntityQueryService
import com.egm.stellio.search.entity.service.EntityService
Expand Down Expand Up @@ -42,7 +42,7 @@ class AnonymousUserHandlerTests {
private lateinit var entityQueryService: EntityQueryService

@MockkBean
private lateinit var contextSourceRegistrationService: ContextSourceRegistrationService
private lateinit var contextSourceCaller: ContextSourceCaller

@MockkBean(relaxed = true)
private lateinit var linkedEntityService: LinkedEntityService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.egm.stellio.search.csr.service

import arrow.core.left
import com.egm.stellio.search.csr.CsrUtils.gimmeRawCSR
import com.egm.stellio.search.csr.model.MiscellaneousPersistentWarning
import com.egm.stellio.search.csr.model.MiscellaneousWarning
import com.egm.stellio.search.csr.model.RevalidationFailedWarning
import com.egm.stellio.search.entity.util.composeEntitiesQueryFromGet
import com.egm.stellio.search.support.WithKafkaContainer
import com.egm.stellio.search.support.WithTimescaleContainer
import com.egm.stellio.shared.config.ApplicationProperties
import com.egm.stellio.shared.queryparameter.QueryParameter
import com.egm.stellio.shared.util.APIC_COMPOUND_CONTEXT
import com.egm.stellio.shared.util.GEO_JSON_CONTENT_TYPE
Expand All @@ -25,7 +28,12 @@ import com.github.tomakehurst.wiremock.client.WireMock.urlMatching
import com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo
import com.github.tomakehurst.wiremock.client.WireMock.verify
import com.github.tomakehurst.wiremock.junit5.WireMockTest
import com.ninjasquad.springmockk.MockkBean
import com.ninjasquad.springmockk.SpykBean
import io.mockk.coEvery
import io.mockk.coVerify
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertInstanceOf
import org.junit.jupiter.api.Assertions.assertNotNull
Expand All @@ -38,6 +46,7 @@ import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import org.springframework.test.context.ActiveProfiles
import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap
import wiremock.com.google.common.net.HttpHeaders.ACCEPT
import wiremock.com.google.common.net.HttpHeaders.CONTENT_TYPE

Expand All @@ -46,9 +55,15 @@ import wiremock.com.google.common.net.HttpHeaders.CONTENT_TYPE
@ActiveProfiles("test")
class ContextSourceCallerTests : WithTimescaleContainer, WithKafkaContainer {

@Autowired
@SpykBean
private lateinit var contextSourceCaller: ContextSourceCaller

@Autowired
private lateinit var applicationProperties: ApplicationProperties

@MockkBean
private lateinit var contextSourceRegistrationService: ContextSourceRegistrationService

private val apiaryId = "urn:ngsi-ld:Apiary:TEST"

private val emptyParams = LinkedMultiValueMap<String, String>()
Expand Down Expand Up @@ -129,6 +144,37 @@ class ContextSourceCallerTests : WithTimescaleContainer, WithKafkaContainer {
assertInstanceOf(RevalidationFailedWarning::class.java, response.leftOrNull())
}

@Test
fun `queryEntitiesFromAllSources should return the warnings sent by the CSRs and update the statuses`() = runTest {
val csr = gimmeRawCSR()

coEvery {
contextSourceRegistrationService
.getContextSourceRegistrations(any(), any(), any())
} returns listOf(csr, csr)

coEvery {
contextSourceCaller.queryEntitiesFromContextSource(any(), any(), any())
} returns MiscellaneousWarning(
"message with\nline\nbreaks",
csr
).left() andThen
MiscellaneousWarning("message", csr).left()

coEvery { contextSourceRegistrationService.updateContextSourceStatus(any(), any()) } returns Unit

val queryParams = MultiValueMap.fromSingleValue<String, String>(emptyMap())
val headers = HttpHeaders()

val (warnings, _) = contextSourceCaller.queryEntitiesFromAllContextSources(
composeEntitiesQueryFromGet(applicationProperties.pagination, queryParams, emptyList()).getOrNull()!!,
headers,
queryParams
)
assertThat(warnings).hasSize(2)
coVerify(exactly = 2) { contextSourceRegistrationService.updateContextSourceStatus(any(), false) }
}

@Test
fun `retrieveEntityFromContextSource should return the entity when the request succeeds`() = runTest {
val csr = gimmeRawCSR()
Expand Down Expand Up @@ -173,6 +219,34 @@ class ContextSourceCallerTests : WithTimescaleContainer, WithKafkaContainer {
assertInstanceOf(RevalidationFailedWarning::class.java, response.leftOrNull())
}

@Test
fun `retrieveEntityFromAllSources should return the warnings sent by the CSRs and update the statuses`() = runTest {
val csr = gimmeRawCSR()

coEvery {
contextSourceRegistrationService
.getContextSourceRegistrations(any(), any(), any())
} returns listOf(csr, csr)

coEvery {
contextSourceCaller.retrieveEntityFromContextSource(any(), any(), any(), any())
} returns MiscellaneousWarning(
"message with\nline\nbreaks",
csr
).left() andThen
MiscellaneousWarning("message", csr).left()

coEvery { contextSourceRegistrationService.updateContextSourceStatus(any(), any()) } returns Unit

val (warnings, _) = contextSourceCaller.retrieveEntityFromAllContextSources(
apiaryId.toUri(),
HttpHeaders(),
MultiValueMap.fromSingleValue(emptyMap())
)
assertThat(warnings).hasSize(2)
coVerify(exactly = 2) { contextSourceRegistrationService.updateContextSourceStatus(any(), false) }
}

@Test
fun `getDistributedInformation should return a MiscellaneousWarning if it receives no answer`() = runTest {
val csr = gimmeRawCSR().copy(endpoint = "http://localhost:invalid".toUri())
Expand Down

0 comments on commit 975dcc6

Please sign in to comment.