Skip to content

Commit

Permalink
refactor: filterAttributes return type (#1316)
Browse files Browse the repository at this point in the history
* refactor: filterAttributes return type

* refactor: filterAttributes return type
  • Loading branch information
thomasBousselin authored Jan 20, 2025
1 parent f30c18e commit 3f38fd5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ data class ExpandedEntity(
this.plus(propertyKey to JsonLdUtils.buildNonReifiedTemporalValue(dateTime))
else this

fun filterAttributes(includedAttributes: Set<String>, includedDatasetIds: Set<String>): Map<String, Any> =
filterEntityOnAttributes(this.members, includedAttributes, includedDatasetIds)

private fun filterEntityOnAttributes(
members: Map<String, Any>,
fun filterAttributes(
includedAttributes: Set<String>,
includedDatasetIds: Set<String>,
): Map<String, Any> =
): ExpandedEntity = ExpandedEntity(
if (includedAttributes.isEmpty() && includedDatasetIds.isEmpty()) {
members
} else
Expand All @@ -113,12 +109,13 @@ data class ExpandedEntity(
includedDatasetIds.contains(expandedAttributeInstance.getDatasetId().toString())
}.ifEmpty { null }
}
)
}

fun List<ExpandedEntity>.filterAttributes(
includedAttributes: Set<String>,
includedDatasetIds: Set<String>
): List<ExpandedEntity> =
this.map {
ExpandedEntity(it.filterAttributes(includedAttributes, includedDatasetIds))
it.filterAttributes(includedAttributes, includedDatasetIds)
}
25 changes: 15 additions & 10 deletions shared/src/main/kotlin/com/egm/stellio/shared/util/JsonLdUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ExpandedEntityTests {

val attributesToMatch: Set<String> = 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))
Expand All @@ -129,7 +129,7 @@ class ExpandedEntityTests {

val attributesToMatch: Set<String> = parseAndExpandQueryParameter("name", listOf(APIC_COMPOUND_CONTEXT))
val datasetIdToMatch: Set<String> = 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))
}
Expand Down Expand Up @@ -157,7 +157,7 @@ class ExpandedEntityTests {
""".trimIndent()

val datasetIdToMatch: Set<String> = 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))
}
Expand All @@ -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))
}
Expand All @@ -204,7 +204,7 @@ class ExpandedEntityTests {
""".trimIndent()

val attributesToMatch: Set<String> = 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))
}
Expand Down Expand Up @@ -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))
}
Expand All @@ -261,7 +261,7 @@ class ExpandedEntityTests {

val attributesToMatch: Set<String> = parseAndExpandQueryParameter("name", listOf(APIC_COMPOUND_CONTEXT))
val datasetIdToMatch: Set<String> = 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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class NotificationService(
val contexts = it.jsonldContext?.let { listOf(it.toString()) } ?: it.contexts

val compactedEntity = compactEntity(
ExpandedEntity(filteredEntity),
filteredEntity,
contexts
).toFinalRepresentation(
NgsiLdDataRepresentation(
Expand Down

0 comments on commit 3f38fd5

Please sign in to comment.