From d83496bc80db4a1b3f950c3824aac8c239b6eb18 Mon Sep 17 00:00:00 2001 From: Thomas BOUSSELIN Date: Thu, 16 Jan 2025 13:51:15 +0100 Subject: [PATCH] refactor: filterAttributes return type --- .../search/entity/web/EntityHandler.kt | 6 ++--- .../stellio/shared/model/ExpandedEntity.kt | 11 +++----- .../egm/stellio/shared/util/JsonLdUtils.kt | 25 +++++++++++-------- .../shared/model/ExpandedEntityTests.kt | 14 +++++------ 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/search-service/src/main/kotlin/com/egm/stellio/search/entity/web/EntityHandler.kt b/search-service/src/main/kotlin/com/egm/stellio/search/entity/web/EntityHandler.kt index 35dedbb43..f9b1a0f9d 100644 --- a/search-service/src/main/kotlin/com/egm/stellio/search/entity/web/EntityHandler.kt +++ b/search-service/src/main/kotlin/com/egm/stellio/search/entity/web/EntityHandler.kt @@ -14,7 +14,6 @@ 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 @@ -276,9 +275,8 @@ class EntityHandler( val expandedEntity = entityQueryService.queryEntity(entityId, sub.getOrNull()).bind() expandedEntity.checkContainsAnyOf(entitiesQuery.attrs).bind() - val filteredExpandedEntity = ExpandedEntity( - expandedEntity.filterAttributes(entitiesQuery.attrs, entitiesQuery.datasetId) - ) + val filteredExpandedEntity = expandedEntity.filterAttributes(entitiesQuery.attrs, entitiesQuery.datasetId) + compactEntity(filteredExpandedEntity, contexts) } diff --git a/shared/src/main/kotlin/com/egm/stellio/shared/model/ExpandedEntity.kt b/shared/src/main/kotlin/com/egm/stellio/shared/model/ExpandedEntity.kt index 4988d5e7d..943b434f8 100644 --- a/shared/src/main/kotlin/com/egm/stellio/shared/model/ExpandedEntity.kt +++ b/shared/src/main/kotlin/com/egm/stellio/shared/model/ExpandedEntity.kt @@ -88,14 +88,10 @@ data class ExpandedEntity( this.plus(propertyKey to JsonLdUtils.buildNonReifiedTemporalValue(dateTime)) else this - fun filterAttributes(includedAttributes: Set, includedDatasetIds: Set): Map = - filterEntityOnAttributes(this.members, includedAttributes, includedDatasetIds) - - private fun filterEntityOnAttributes( - members: Map, + fun filterAttributes( includedAttributes: Set, includedDatasetIds: Set, - ): Map = + ): ExpandedEntity = ExpandedEntity( if (includedAttributes.isEmpty() && includedDatasetIds.isEmpty()) { members } else @@ -113,6 +109,7 @@ data class ExpandedEntity( includedDatasetIds.contains(expandedAttributeInstance.getDatasetId().toString()) }.ifEmpty { null } } + ) } fun List.filterAttributes( @@ -120,5 +117,5 @@ fun List.filterAttributes( includedDatasetIds: Set ): List = this.map { - ExpandedEntity(it.filterAttributes(includedAttributes, includedDatasetIds)) + it.filterAttributes(includedAttributes, includedDatasetIds) } diff --git a/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt b/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt index f142ee3d7..80e2a296d 100644 --- a/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt +++ b/shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt @@ -98,6 +98,21 @@ object JsonLdUtils { val JSONLD_EXPANDED_ENTITY_SPECIFIC_MEMBERS = setOf(JSONLD_TYPE, NGSILD_SCOPE_PROPERTY) + const val NGSILD_CREATED_AT_TERM = "createdAt" + const val NGSILD_MODIFIED_AT_TERM = "modifiedAt" + const val NGSILD_DELETED_AT_TERM = "deletedAt" + val NGSILD_SYSATTRS_TERMS = setOf(NGSILD_CREATED_AT_TERM, NGSILD_MODIFIED_AT_TERM, NGSILD_DELETED_AT_TERM) + const val NGSILD_CREATED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_CREATED_AT_TERM" + const val NGSILD_MODIFIED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_MODIFIED_AT_TERM" + const val NGSILD_DELETED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_DELETED_AT_TERM" + val NGSILD_SYSATTRS_PROPERTIES = setOf( + NGSILD_CREATED_AT_PROPERTY, + NGSILD_MODIFIED_AT_PROPERTY, + NGSILD_DELETED_AT_PROPERTY + ) + const val NGSILD_OBSERVED_AT_TERM = "observedAt" + const val NGSILD_OBSERVED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_OBSERVED_AT_TERM" + // List of members that are part of a core entity base definition (i.e., without attributes) val JSONLD_EXPANDED_ENTITY_CORE_MEMBERS = setOf( @@ -118,16 +133,6 @@ object JsonLdUtils { NGSILD_MODIFIED_AT_TERM ) - const val NGSILD_CREATED_AT_TERM = "createdAt" - const val NGSILD_MODIFIED_AT_TERM = "modifiedAt" - val NGSILD_SYSATTRS_TERMS = setOf(NGSILD_CREATED_AT_TERM, NGSILD_MODIFIED_AT_TERM, NGSILD_DELETED_AT_TERM) - const val NGSILD_CREATED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_CREATED_AT_TERM" - const val NGSILD_MODIFIED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_MODIFIED_AT_TERM" - val NGSILD_SYSATTRS_PROPERTIES = setOf(NGSILD_CREATED_AT_PROPERTY, NGSILD_MODIFIED_AT_PROPERTY) - const val NGSILD_OBSERVED_AT_TERM = "observedAt" - const val NGSILD_OBSERVED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_OBSERVED_AT_TERM" - const val NGSILD_DELETED_AT_TERM = "deletedAt" - const val NGSILD_DELETED_AT_PROPERTY = "https://uri.etsi.org/ngsi-ld/$NGSILD_DELETED_AT_TERM" const val NGSILD_UNIT_CODE_PROPERTY = "https://uri.etsi.org/ngsi-ld/unitCode" const val NGSILD_UNIT_CODE_TERM = "unitCode" const val NGSILD_LOCATION_TERM = "location" diff --git a/shared/src/test/kotlin/com/egm/stellio/shared/model/ExpandedEntityTests.kt b/shared/src/test/kotlin/com/egm/stellio/shared/model/ExpandedEntityTests.kt index 2a1a6558d..f2d4085d1 100644 --- a/shared/src/test/kotlin/com/egm/stellio/shared/model/ExpandedEntityTests.kt +++ b/shared/src/test/kotlin/com/egm/stellio/shared/model/ExpandedEntityTests.kt @@ -104,7 +104,7 @@ class ExpandedEntityTests { val attributesToMatch: Set = parseAndExpandQueryParameter("managedBy", listOf(APIC_COMPOUND_CONTEXT)) - val filteredEntity = ExpandedEntity(entity.filterAttributes(attributesToMatch, emptySet())) + val filteredEntity = entity.filterAttributes(attributesToMatch, emptySet()) val compactedEntity = compactEntity(filteredEntity, listOf(APIC_COMPOUND_CONTEXT)) assertJsonPayloadsAreEqual(expectedEntity, serializeObject(compactedEntity)) @@ -129,7 +129,7 @@ class ExpandedEntityTests { val attributesToMatch: Set = parseAndExpandQueryParameter("name", listOf(APIC_COMPOUND_CONTEXT)) val datasetIdToMatch: Set = setOf("urn:ngsi-ld:Dataset:english-name") - val filteredEntity = ExpandedEntity(entity.filterAttributes(attributesToMatch, datasetIdToMatch)) + val filteredEntity = entity.filterAttributes(attributesToMatch, datasetIdToMatch) val compactedEntity = compactEntity(filteredEntity, listOf(APIC_COMPOUND_CONTEXT)) assertJsonPayloadsAreEqual(expectedEntity, serializeObject(compactedEntity)) } @@ -157,7 +157,7 @@ class ExpandedEntityTests { """.trimIndent() val datasetIdToMatch: Set = setOf("urn:ngsi-ld:Dataset:french-name") - val filteredEntity = ExpandedEntity(entity.filterAttributes(emptySet(), datasetIdToMatch)) + val filteredEntity = entity.filterAttributes(emptySet(), datasetIdToMatch) val compactedEntity = compactEntity(filteredEntity, listOf(APIC_COMPOUND_CONTEXT)) assertJsonPayloadsAreEqual(expectedEntity, serializeObject(compactedEntity)) } @@ -182,7 +182,7 @@ class ExpandedEntityTests { } """.trimIndent() - val filteredEntity = ExpandedEntity(entity.filterAttributes(emptySet(), setOf(NGSILD_NONE_TERM))) + val filteredEntity = entity.filterAttributes(emptySet(), setOf(NGSILD_NONE_TERM)) val compactedEntity = compactEntity(filteredEntity, listOf(APIC_COMPOUND_CONTEXT)) assertJsonPayloadsAreEqual(expectedEntity, serializeObject(compactedEntity)) } @@ -204,7 +204,7 @@ class ExpandedEntityTests { """.trimIndent() val attributesToMatch: Set = parseAndExpandQueryParameter("name", listOf(APIC_COMPOUND_CONTEXT)) - val filteredEntity = ExpandedEntity(entity.filterAttributes(attributesToMatch, setOf(NGSILD_NONE_TERM))) + val filteredEntity = entity.filterAttributes(attributesToMatch, setOf(NGSILD_NONE_TERM)) val compactedEntity = compactEntity(filteredEntity, listOf(APIC_COMPOUND_CONTEXT)) assertJsonPayloadsAreEqual(expectedEntity, serializeObject(compactedEntity)) } @@ -242,7 +242,7 @@ class ExpandedEntityTests { "urn:ngsi-ld:Dataset:english-name", "urn:ngsi-ld:Dataset:french-name" ) - val filteredEntity = ExpandedEntity(entity.filterAttributes(emptySet(), datasetIdToMatch)) + val filteredEntity = entity.filterAttributes(emptySet(), datasetIdToMatch) val compactedEntity = compactEntity(filteredEntity, listOf(APIC_COMPOUND_CONTEXT)) assertJsonPayloadsAreEqual(expectedEntity, serializeObject(compactedEntity)) } @@ -261,7 +261,7 @@ class ExpandedEntityTests { val attributesToMatch: Set = parseAndExpandQueryParameter("name", listOf(APIC_COMPOUND_CONTEXT)) val datasetIdToMatch: Set = setOf("urn:ngsi-ld:Dataset:managedBy") - val filteredEntity = ExpandedEntity(entity.filterAttributes(attributesToMatch, datasetIdToMatch)) + val filteredEntity = entity.filterAttributes(attributesToMatch, datasetIdToMatch) val compactedEntity = compactEntity(filteredEntity, listOf(APIC_COMPOUND_CONTEXT)) assertJsonPayloadsAreEqual(expectedEntity, serializeObject(compactedEntity)) }