Skip to content

Commit

Permalink
Merge branch 'develop' into feature/114-remove-external-depedencies-i…
Browse files Browse the repository at this point in the history
…n-unit-test
  • Loading branch information
bobeal authored Apr 29, 2024
2 parents 7073d2a + 64999fb commit 98f90b5
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 171 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 @@ -593,12 +593,12 @@ class TemporalEntityAttributeService(
val currentTea =
getForEntityAndAttribute(entityUri, ngsiLdAttribute.name, ngsiLdAttributeInstance.datasetId)
.fold({ null }, { it })
val attributeMetadata = ngsiLdAttributeInstance.toTemporalAttributeMetadata().bind()
val attributePayload = expandedAttributes.getAttributeFromExpandedAttributes(
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId
)!!
if (currentTea != null) {
val attributeMetadata = ngsiLdAttributeInstance.toTemporalAttributeMetadata().bind()
val attributePayload = expandedAttributes.getAttributeFromExpandedAttributes(
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId
)!!
replaceAttribute(
currentTea,
ngsiLdAttribute,
Expand All @@ -615,17 +615,21 @@ class TemporalEntityAttributeService(
)
}.bind()
} else {
val message = if (ngsiLdAttributeInstance.datasetId != null)
"Attribute (datasetId: ${ngsiLdAttributeInstance.datasetId}) does not exist"
else
"Attribute (default instance) does not exist"
logger.info(message)
UpdateAttributeResult(
addAttribute(
entityUri,
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId,
UpdateOperationResult.IGNORED,
message
).right().bind()
attributeMetadata,
createdAt,
attributePayload,
sub
).map {
UpdateAttributeResult(
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId,
UpdateOperationResult.APPENDED,
null
)
}.bind()
}
}
}.fold({ it.left() }, { updateResultFromDetailedResult(it).right() })
Expand Down
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 @@ -243,13 +243,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 @@ -66,8 +66,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 @@ -193,11 +192,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 @@ -690,8 +690,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 @@ -731,8 +730,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 @@ -835,8 +833,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 @@ -866,12 +863,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 98f90b5

Please sign in to comment.