Skip to content

Commit

Permalink
refactor: remove contexts from expanded entity representation
Browse files Browse the repository at this point in the history
once an entity is expanded, the contexts used to do the expansion should not be used anymore. so remove them to avoid any misusage of them.
  • Loading branch information
bobeal committed Apr 27, 2024
1 parent 3c80bbc commit 88f1eaf
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class EnabledAuthorizationService(
} else entityAccessControl
}
.map { it.serializeProperties(contexts) }
.map { ExpandedEntity(it, contexts) }
.map { ExpandedEntity(it) }

val count = entityAccessRightsService.getSubjectAccessRightsCount(
sub,
Expand Down Expand Up @@ -162,7 +162,7 @@ class EnabledAuthorizationService(
}

val jsonLdEntities = groups.second.map {
ExpandedEntity(it.serializeProperties(), contexts)
ExpandedEntity(it.serializeProperties())
}

Pair(groups.first, jsonLdEntities)
Expand All @@ -177,7 +177,7 @@ class EnabledAuthorizationService(
val usersCount = subjectReferentialService.getUsersCount().bind()

val jsonLdEntities = users.map {
ExpandedEntity(it.serializeProperties(contexts), contexts)
ExpandedEntity(it.serializeProperties(contexts))
}

Pair(usersCount, jsonLdEntities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ class QueryService(
private val attributeInstanceService: AttributeInstanceService,
private val temporalEntityAttributeService: TemporalEntityAttributeService
) {
suspend fun queryEntity(
entityId: URI,
contexts: List<String>
): Either<APIException, ExpandedEntity> =
suspend fun queryEntity(entityId: URI): Either<APIException, ExpandedEntity> =
either {
val entityPayload = entityPayloadService.retrieve(entityId).bind()
toJsonLdEntity(entityPayload, contexts)
toJsonLdEntity(entityPayload)
}

suspend fun queryEntities(
Expand All @@ -46,17 +43,14 @@ class QueryService(
if (entitiesIds.isEmpty())
return@either Pair<List<ExpandedEntity>, Int>(emptyList(), count)

val entitiesPayloads =
entityPayloadService.retrieve(entitiesIds)
.map { toJsonLdEntity(it, entitiesQuery.contexts) }
val entitiesPayloads = entityPayloadService.retrieve(entitiesIds).map { toJsonLdEntity(it) }

Pair(entitiesPayloads, count).right().bind()
}

suspend fun queryTemporalEntity(
entityId: URI,
temporalEntitiesQuery: TemporalEntitiesQuery,
contexts: List<String>
temporalEntitiesQuery: TemporalEntitiesQuery
): Either<APIException, ExpandedEntity> = either {
val attrs = temporalEntitiesQuery.entitiesQuery.attrs
val temporalEntityAttributes = temporalEntityAttributeService.getForEntity(entityId, attrs).let {
Expand All @@ -83,8 +77,7 @@ class QueryService(

TemporalEntityBuilder.buildTemporalEntity(
EntityTemporalResult(entityPayload, scopeHistory, temporalEntityAttributesWithInstances),
temporalEntitiesQuery,
contexts
temporalEntitiesQuery
)
}

Expand Down Expand Up @@ -175,8 +168,7 @@ class QueryService(
Pair(
TemporalEntityBuilder.buildTemporalEntities(
attributeInstancesPerEntityAndAttribute,
temporalEntitiesQuery,
temporalEntitiesQuery.entitiesQuery.contexts
temporalEntitiesQuery
),
count
)
Expand Down Expand Up @@ -235,11 +227,8 @@ class QueryService(
)
}

private fun toJsonLdEntity(
entityPayload: EntityPayload,
contexts: List<String>
): ExpandedEntity {
private fun toJsonLdEntity(entityPayload: EntityPayload): ExpandedEntity {
val deserializedEntity = entityPayload.payload.deserializeAsMap()
return ExpandedEntity(deserializedEntity, contexts)
return ExpandedEntity(deserializedEntity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ object TemporalEntityBuilder {

fun buildTemporalEntities(
queryResult: List<EntityTemporalResult>,
temporalEntitiesQuery: TemporalEntitiesQuery,
contexts: List<String>
temporalEntitiesQuery: TemporalEntitiesQuery
): List<ExpandedEntity> =
queryResult.map {
buildTemporalEntity(it, temporalEntitiesQuery, contexts)
buildTemporalEntity(it, temporalEntitiesQuery)
}

fun buildTemporalEntity(
entityTemporalResult: EntityTemporalResult,
temporalEntitiesQuery: TemporalEntitiesQuery,
contexts: List<String>
temporalEntitiesQuery: TemporalEntitiesQuery
): ExpandedEntity {
val temporalAttributes = buildTemporalAttributes(
entityTemporalResult.teaInstancesResult,
Expand All @@ -55,7 +53,7 @@ object TemporalEntityBuilder {
val expandedTemporalEntity = entityTemporalResult.entityPayload.serializeProperties()
.plus(temporalAttributes)
.plus(scopeAttributeInstances)
return ExpandedEntity(expandedTemporalEntity, contexts)
return ExpandedEntity(expandedTemporalEntity)
}

private fun buildTemporalAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,12 @@ class EntityHandler(

authorizationService.userCanReadEntity(entityId, sub).bind()

val expandedEntity = queryService.queryEntity(entityId, contexts).bind()
val expandedEntity = queryService.queryEntity(entityId).bind()

expandedEntity.checkContainsAnyOf(queryParams.attrs).bind()

val filteredExpandedEntity = ExpandedEntity(
expandedEntity.filterOnAttributes(queryParams.attrs),
expandedEntity.contexts
expandedEntity.filterOnAttributes(queryParams.attrs)
)
val compactedEntity = compactEntity(filteredExpandedEntity, contexts)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ class TemporalEntityHandler(
val expandedEntity = ExpandedEntity(
sortedJsonLdInstances
.keepFirstInstances()
.addCoreMembers(jsonLdTemporalEntity.id, jsonLdTemporalEntity.types),
contexts
.addCoreMembers(jsonLdTemporalEntity.id, jsonLdTemporalEntity.types)
)
val ngsiLdEntity = expandedEntity.toNgsiLdEntity().bind()

Expand Down Expand Up @@ -191,11 +190,7 @@ class TemporalEntityHandler(
val temporalEntitiesQuery =
composeTemporalEntitiesQuery(applicationProperties.pagination, params, contexts).bind()

val temporalEntity = queryService.queryTemporalEntity(
entityId,
temporalEntitiesQuery,
contexts
).bind()
val temporalEntity = queryService.queryTemporalEntity(entityId, temporalEntitiesQuery).bind()

val compactedEntity = compactEntity(temporalEntity, contexts)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class V0_29__JsonLd_migration : BaseJavaMigration() {
""".trimIndent()
)

val jsonLdEntity = ExpandedEntity(expandedEntity, contexts)
val jsonLdEntity = ExpandedEntity(expandedEntity)
val ngsiLdEntity = runBlocking {
jsonLdEntity.toNgsiLdEntity()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class QueryServiceTests {
fun `it should return a JSON-LD entity when querying by id`() = runTest {
coEvery { entityPayloadService.retrieve(any<URI>()) } returns gimmeEntityPayload().right()

queryService.queryEntity(entityUri, APIC_COMPOUND_CONTEXTS)
queryService.queryEntity(entityUri)
.shouldSucceedWith {
assertEquals(entityUri.toString(), it.id)
assertEquals(listOf(BEEHIVE_TYPE), it.types)
Expand All @@ -68,7 +68,7 @@ class QueryServiceTests {
fun `it should return an API exception if no entity exists with the given id`() = runTest {
coEvery { entityPayloadService.retrieve(any<URI>()) } returns ResourceNotFoundException("").left()

queryService.queryEntity(entityUri, APIC_COMPOUND_CONTEXTS)
queryService.queryEntity(entityUri)
.shouldFail {
assertTrue(it is ResourceNotFoundException)
}
Expand Down Expand Up @@ -120,8 +120,7 @@ class QueryServiceTests {
withTemporalValues = false,
withAudit = false,
withAggregatedValues = false
),
APIC_COMPOUND_CONTEXTS
)
).fold({
assertInstanceOf(ResourceNotFoundException::class.java, it)
assertEquals(
Expand Down Expand Up @@ -172,8 +171,7 @@ class QueryServiceTests {
withTemporalValues = false,
withAudit = false,
withAggregatedValues = false
),
APIC_COMPOUND_CONTEXTS
)
)

coVerify {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class TemporalEntityBuilderTests {
withTemporalValues = false,
withAudit = false,
withAggregatedValues = false
),
APIC_COMPOUND_CONTEXTS
)
)
assertJsonPayloadsAreEqual(
loadSampleData("expectations/beehive_empty_outgoing.jsonld"),
Expand Down Expand Up @@ -81,8 +80,7 @@ class TemporalEntityBuilderTests {
withTemporalValues,
withAudit,
false
),
APIC_COMPOUND_CONTEXTS
)
)
assertJsonPayloadsAreEqual(
expectation,
Expand All @@ -107,8 +105,7 @@ class TemporalEntityBuilderTests {
withTemporalValues,
withAudit,
false
),
APIC_COMPOUND_CONTEXTS
)
)
assertJsonPayloadsAreEqual(
expectation,
Expand Down Expand Up @@ -186,8 +183,7 @@ class TemporalEntityBuilderTests {
withTemporalValues = false,
withAudit = false,
withAggregatedValues = true
),
APIC_COMPOUND_CONTEXTS
)
)

assertJsonPayloadsAreEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,8 +691,7 @@ class EntityAccessControlHandlerTests {
"@id" to "urn:ngsi-ld:group:1",
"@type" to listOf(GROUP_TYPE),
NGSILD_NAME_PROPERTY to buildExpandedPropertyValue("egm")
),
listOf(NGSILD_TEST_CORE_CONTEXT)
)
)
)
).right()
Expand Down Expand Up @@ -732,8 +731,7 @@ class EntityAccessControlHandlerTests {
"@type" to listOf(GROUP_TYPE),
NGSILD_NAME_PROPERTY to buildExpandedPropertyValue("egm"),
AuthContextModel.AUTH_REL_IS_MEMBER_OF to buildExpandedPropertyValue("true")
),
listOf(AUTHZ_TEST_COMPOUND_CONTEXT)
)
)
)
).right()
Expand Down Expand Up @@ -836,8 +834,7 @@ class EntityAccessControlHandlerTests {
"givenName",
"familyName",
mapOf("profile" to "stellio-user", "username" to "username")
).serializeProperties(AUTHZ_TEST_COMPOUND_CONTEXTS),
listOf(NGSILD_TEST_CORE_CONTEXT)
).serializeProperties(AUTHZ_TEST_COMPOUND_CONTEXTS)
)
)
).right()
Expand Down Expand Up @@ -867,12 +864,9 @@ class EntityAccessControlHandlerTests {
.jsonPath("[0].modifiedAt").doesNotExist()
}

private suspend fun createJsonLdEntity(
entityAccessRights: EntityAccessRights,
context: String = NGSILD_TEST_CORE_CONTEXT
): ExpandedEntity {
private suspend fun createJsonLdEntity(entityAccessRights: EntityAccessRights): ExpandedEntity {
val earSerialized = entityAccessRights.serializeProperties(AUTHZ_TEST_COMPOUND_CONTEXTS)
return ExpandedEntity(earSerialized, listOf(context))
return ExpandedEntity(earSerialized)
}

private fun createEntityAccessRight(
Expand Down
Loading

0 comments on commit 88f1eaf

Please sign in to comment.