Skip to content

Commit

Permalink
feat: renaming to DistributedEntityConsumptionService
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasBousselin committed Jan 13, 2025
1 parent 825a348 commit 80c2cff
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 68 deletions.
47 changes: 45 additions & 2 deletions search-service/config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,53 @@
<ID>ClassNaming:V0_29__JsonLd_migration.kt$V0_29__JsonLd_migration : BaseJavaMigration</ID>
<ID>ComplexCondition:EntitiesQueryUtils.kt$geoQuery == null &amp;&amp; q.isNullOrEmpty() &amp;&amp; typeSelection.isNullOrEmpty() &amp;&amp; attrs.isEmpty()</ID>
<ID>Filename:V0_29__JsonLd_migration.kt$db.migration.V0_29__JsonLd_migration.kt</ID>
<ID>ImportOrdering:EntityHandler.kt$import arrow.core.getOrElse import arrow.core.left import
arrow.core.raise.either import arrow.core.right import com.egm.stellio.search.csr.model.addWarnings import
com.egm.stellio.search.csr.service.DistributedEntityConsumptionService import
com.egm.stellio.search.csr.service.ContextSourceUtils import
com.egm.stellio.search.entity.service.EntityQueryService import
com.egm.stellio.search.entity.service.EntityService import
com.egm.stellio.search.entity.service.LinkedEntityService import
com.egm.stellio.search.entity.util.composeEntitiesQueryFromGet import
com.egm.stellio.search.entity.util.validateMinimalQueryEntitiesParameters import
com.egm.stellio.shared.config.ApplicationProperties import com.egm.stellio.shared.model.BadRequestDataException
import com.egm.stellio.shared.model.ExpandedEntity import
com.egm.stellio.shared.model.NgsiLdDataRepresentation.Companion.parseRepresentations import
com.egm.stellio.shared.model.ResourceNotFoundException import com.egm.stellio.shared.model.filterAttributes import
com.egm.stellio.shared.model.toFinalRepresentation import com.egm.stellio.shared.model.toNgsiLdEntity import
com.egm.stellio.shared.queryparameter.AllowedParameters import com.egm.stellio.shared.queryparameter.OptionsValue
import com.egm.stellio.shared.queryparameter.QP import com.egm.stellio.shared.queryparameter.QueryParameter import
com.egm.stellio.shared.util.GEO_JSON_CONTENT_TYPE import com.egm.stellio.shared.util.JSON_LD_CONTENT_TYPE import
com.egm.stellio.shared.util.JSON_MERGE_PATCH_CONTENT_TYPE import
com.egm.stellio.shared.util.JsonLdUtils.compactEntities import
com.egm.stellio.shared.util.JsonLdUtils.compactEntity import
com.egm.stellio.shared.util.JsonLdUtils.expandAttribute import
com.egm.stellio.shared.util.JsonLdUtils.expandAttributes import
com.egm.stellio.shared.util.JsonLdUtils.expandJsonLdEntity import
com.egm.stellio.shared.util.JsonLdUtils.expandJsonLdTerm import
com.egm.stellio.shared.util.JsonUtils.serializeObject import com.egm.stellio.shared.util.buildQueryResponse import
com.egm.stellio.shared.util.extractPayloadAndContexts import com.egm.stellio.shared.util.getApplicableMediaType
import com.egm.stellio.shared.util.getContextFromLinkHeaderOrDefault import
com.egm.stellio.shared.util.getSubFromSecurityContext import com.egm.stellio.shared.util.missingPathErrorResponse
import com.egm.stellio.shared.util.parseTimeParameter import
com.egm.stellio.shared.util.prepareGetSuccessResponseHeaders import com.egm.stellio.shared.util.toUri import
com.egm.stellio.shared.web.BaseHandler import org.springframework.http.HttpHeaders import
org.springframework.http.HttpStatus import org.springframework.http.MediaType.APPLICATION_JSON_VALUE import
org.springframework.http.ResponseEntity import org.springframework.util.MultiValueMap import
org.springframework.validation.annotation.Validated import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping import
org.springframework.web.bind.annotation.PatchMapping import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping import
org.springframework.web.bind.annotation.PutMapping import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader import
org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController import reactor.core.publisher.Mono import
java.net.URI
</ID>
<ID>LongMethod:AttributeInstanceService.kt$AttributeInstanceService$@Transactional suspend fun create(attributeInstance: AttributeInstance): Either&lt;APIException, Unit&gt;</ID>
<ID>LongMethod:EnabledAuthorizationServiceTests.kt$EnabledAuthorizationServiceTests$@Test fun `it should return serialized access control entities with other rigths if user is owner`()</ID>
<ID>LongMethod:EntityAccessControlHandler.kt$EntityAccessControlHandler$@PostMapping("/{subjectId}/attrs", consumes = [MediaType.APPLICATION_JSON_VALUE, JSON_LD_CONTENT_TYPE]) suspend fun addRightsOnEntities( @RequestHeader httpHeaders: HttpHeaders, @PathVariable subjectId: String, @RequestBody requestBody: Mono&lt;String&gt;, @AllowedParameters @RequestParam queryParams: MultiValueMap&lt;String, String&gt; ): ResponseEntity&lt;*&gt;</ID>
<ID>LongMethod:EntityEventService.kt$EntityEventService$private fun publishAttributeChangeEvent( sub: String?, tenantName: String, entityId: URI, entityTypesAndPayload: Pair&lt;List&lt;ExpandedTerm&gt;, String&gt;, attributeOperationResult: SucceededAttributeOperationResult )</ID>
<ID>LongMethod:EntityHandler.kt$EntityHandler$@GetMapping("/{entityId}", produces = [APPLICATION_JSON_VALUE, JSON_LD_CONTENT_TYPE, GEO_JSON_CONTENT_TYPE]) suspend fun getByURI( @RequestHeader httpHeaders: HttpHeaders, @PathVariable entityId: URI, @AllowedParameters( implemented = [ QP.OPTIONS, QP.FORMAT, QP.TYPE, QP.ATTRS, QP.GEOMETRY_PROPERTY, QP.LANG, QP.CONTAINED_BY, QP.JOIN, QP.JOIN_LEVEL, QP.DATASET_ID, ], notImplemented = [QP.PICK, QP.OMIT, QP.ENTITY_MAP, QP.LOCAL, QP.VIA] ) @RequestParam queryParams: MultiValueMap&lt;String, String&gt; ): ResponseEntity&lt;*&gt;</ID>
<ID>LongMethod:EntityHandler.kt$EntityHandler$@GetMapping(produces = [APPLICATION_JSON_VALUE, JSON_LD_CONTENT_TYPE, GEO_JSON_CONTENT_TYPE]) suspend fun getEntities( @RequestHeader httpHeaders: HttpHeaders, @AllowedParameters( implemented = [ QP.OPTIONS, QP.FORMAT, QP.COUNT, QP.OFFSET, QP.LIMIT, QP.ID, QP.TYPE, QP.ID_PATTERN, QP.ATTRS, QP.Q, QP.GEOMETRY, QP.GEOREL, QP.COORDINATES, QP.GEOPROPERTY, QP.GEOMETRY_PROPERTY, QP.LANG, QP.SCOPEQ, QP.CONTAINED_BY, QP.JOIN, QP.JOIN_LEVEL, QP.DATASET_ID, ], notImplemented = [QP.PICK, QP.OMIT, QP.EXPAND_VALUES, QP.CSF, QP.ENTITY_MAP, QP.LOCAL, QP.VIA] ) @RequestParam queryParams: MultiValueMap&lt;String, String&gt; ): ResponseEntity&lt;*&gt;</ID>
<ID>LongMethod:LinkedEntityServiceTests.kt$LinkedEntityServiceTests$@Test fun `it should inline entities up to the asked 2nd level`()</ID>
<ID>LongMethod:PatchAttributeTests.kt$PatchAttributeTests.Companion$@JvmStatic fun mergePatchProvider(): Stream&lt;Arguments&gt;</ID>
<ID>LongMethod:PatchAttributeTests.kt$PatchAttributeTests.Companion$@JvmStatic fun partialUpdatePatchProvider(): Stream&lt;Arguments&gt;</ID>
Expand All @@ -29,6 +70,8 @@
<ID>LongParameterList:EntityAttributeService.kt$EntityAttributeService$( entityUri: URI, ngsiLdAttributes: List&lt;NgsiLdAttribute&gt;, expandedAttributes: ExpandedAttributes, disallowOverwrite: Boolean, createdAt: ZonedDateTime, sub: Sub? )</ID>
<ID>LongParameterList:TemporalEntityHandler.kt$TemporalEntityHandler$( @RequestHeader httpHeaders: HttpHeaders, @PathVariable entityId: URI, @PathVariable attrId: String, @PathVariable instanceId: URI, @RequestBody requestBody: Mono&lt;String&gt;, @AllowedParameters(notImplemented = [QP.LOCAL, QP.VIA]) @RequestParam queryParams: MultiValueMap&lt;String, String&gt; )</ID>
<ID>LongParameterList:V0_29__JsonLd_migration.kt$V0_29__JsonLd_migration$( entityId: URI, attributeName: ExpandedTerm, datasetId: URI?, attributePayload: ExpandedAttributeInstance, ngsiLdAttributeInstance: NgsiLdAttributeInstance, defaultCreatedAt: ZonedDateTime )</ID>
<ID>MaxLineLength:DistributedEntityConsumptionServiceTests.kt$DistributedEntityConsumptionServiceTests$fun</ID>
<ID>MaximumLineLength:DistributedEntityConsumptionServiceTests.kt$DistributedEntityConsumptionServiceTests$</ID>
<ID>NestedBlockDepth:V0_29__JsonLd_migration.kt$V0_29__JsonLd_migration$override fun migrate(context: Context)</ID>
<ID>SwallowedException:TemporalQueryUtils.kt$e: IllegalArgumentException</ID>
</CurrentIssues>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ import java.net.URI
typealias QueryEntitiesResponse = Pair<List<CompactedEntity>, Int?>

@Service
class ContextSourceCaller(
class DistributedEntityConsumptionService(
private val contextSourceRegistrationService: ContextSourceRegistrationService,
) {

val logger: Logger = LoggerFactory.getLogger(javaClass)

suspend fun retrieveEntityFromAllContextSources(
suspend fun distributeRetrieveEntityOperation(
id: URI,
httpHeaders: HttpHeaders,
queryParams: MultiValueMap<String, String>
Expand Down Expand Up @@ -99,7 +99,7 @@ class ContextSourceCaller(
)
}

suspend fun queryEntitiesFromAllContextSources(
suspend fun distributeQueryEntitiesOperation(
entitiesQuery: EntitiesQueryFromGet,
httpHeaders: HttpHeaders,
queryParams: MultiValueMap<String, String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import arrow.core.left
import arrow.core.raise.either
import arrow.core.right
import com.egm.stellio.search.csr.model.addWarnings
import com.egm.stellio.search.csr.service.ContextSourceCaller
import com.egm.stellio.search.csr.service.ContextSourceUtils
import com.egm.stellio.search.csr.service.DistributedEntityConsumptionService
import com.egm.stellio.search.entity.service.EntityQueryService
import com.egm.stellio.search.entity.service.EntityService
import com.egm.stellio.search.entity.service.LinkedEntityService
Expand Down Expand Up @@ -71,7 +71,7 @@ class EntityHandler(
private val applicationProperties: ApplicationProperties,
private val entityService: EntityService,
private val entityQueryService: EntityQueryService,
private val contextSourceCaller: ContextSourceCaller,
private val contextSourceCaller: DistributedEntityConsumptionService,
private val linkedEntityService: LinkedEntityService
) : BaseHandler() {

Expand Down
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.ContextSourceCaller
import com.egm.stellio.search.csr.service.DistributedEntityConsumptionService
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 contextSourceCaller: ContextSourceCaller
private lateinit var contextSourceCaller: DistributedEntityConsumptionService

@MockkBean(relaxed = true)
private lateinit var linkedEntityService: LinkedEntityService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ import wiremock.com.google.common.net.HttpHeaders.CONTENT_TYPE
@SpringBootTest
@WireMockTest(httpPort = 8089)
@ActiveProfiles("test")
class ContextSourceCallerTests : WithTimescaleContainer, WithKafkaContainer() {
class DistributedEntityConsumptionServiceTests : WithTimescaleContainer, WithKafkaContainer() {

@SpykBean
private lateinit var contextSourceCaller: ContextSourceCaller
private lateinit var contextSourceCaller: DistributedEntityConsumptionService

@Autowired
private lateinit var applicationProperties: ApplicationProperties
Expand Down Expand Up @@ -145,35 +145,36 @@ class ContextSourceCallerTests : WithTimescaleContainer, WithKafkaContainer() {
}

@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) }
}
fun `distributeQueryEntitiesOperation 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.distributeQueryEntitiesOperation(
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 {
Expand Down Expand Up @@ -220,32 +221,33 @@ class ContextSourceCallerTests : WithTimescaleContainer, WithKafkaContainer() {
}

@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) }
}
fun `distributeRetrieveEntityOperation 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.distributeRetrieveEntityOperation(
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.egm.stellio.search.common.config.SearchProperties
import com.egm.stellio.search.csr.CsrUtils.gimmeRawCSR
import com.egm.stellio.search.csr.model.MiscellaneousWarning
import com.egm.stellio.search.csr.model.NGSILDWarning
import com.egm.stellio.search.csr.service.ContextSourceCaller
import com.egm.stellio.search.csr.service.DistributedEntityConsumptionService
import com.egm.stellio.search.entity.model.EntitiesQueryFromGet
import com.egm.stellio.search.entity.model.NotUpdatedDetails
import com.egm.stellio.search.entity.model.UpdateResult
Expand Down Expand Up @@ -108,7 +108,7 @@ class EntityHandlerTests {
private lateinit var entityQueryService: EntityQueryService

@MockkBean
private lateinit var contextSourceCaller: ContextSourceCaller
private lateinit var contextSourceCaller: DistributedEntityConsumptionService

@MockkBean(relaxed = true)
private lateinit var linkedEntityService: LinkedEntityService
Expand Down

0 comments on commit 80c2cff

Please sign in to comment.