Skip to content

Commit

Permalink
refactoring in tests utils and expanded members
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Dec 31, 2023
1 parent b320df8 commit df15c3b
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.egm.stellio.shared.util.AttributeType
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_TYPE
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_OBSERVED_AT_PROPERTY
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_PREFIX
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_PROPERTY_VALUE
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_RELATIONSHIP_OBJECT
import com.egm.stellio.shared.util.JsonLdUtils.buildNonReifiedTemporalValue
import com.egm.stellio.shared.util.JsonLdUtils.expandJsonLdEntity
Expand Down Expand Up @@ -842,8 +841,8 @@ class TemporalEntityAttributeService(
when (tea.attributeType) {
TemporalEntityAttribute.AttributeType.Property ->
Triple(
valueToStringOrNull(attributePayload.getMemberValue(NGSILD_PROPERTY_VALUE)!!),
valueToDoubleOrNull(attributePayload.getMemberValue(NGSILD_PROPERTY_VALUE)!!),
valueToStringOrNull(attributePayload.getPropertyValue()!!),
valueToDoubleOrNull(attributePayload.getPropertyValue()!!),
null
)
TemporalEntityAttribute.AttributeType.Relationship ->
Expand All @@ -856,7 +855,7 @@ class TemporalEntityAttributeService(
Triple(
null,
null,
WKTCoordinates(attributePayload.getMemberValue(NGSILD_PROPERTY_VALUE)!! as String)
WKTCoordinates(attributePayload.getPropertyValue()!! as String)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fun guessAttributeValueType(
): AttributeValueType =
when (attributeType) {
TemporalEntityAttribute.AttributeType.Property ->
guessPropertyValueType(expandedAttributeInstance.getPropertyValue()).first
guessPropertyValueType(expandedAttributeInstance.getPropertyValue()!!).first
TemporalEntityAttribute.AttributeType.Relationship -> AttributeValueType.URI
TemporalEntityAttribute.AttributeType.GeoProperty -> AttributeValueType.GEOMETRY
}
Expand All @@ -106,6 +106,5 @@ fun guessPropertyValueType(
is LocalDate -> Pair(AttributeValueType.DATE, Triple(value.toString(), null, null))
is ZonedDateTime -> Pair(AttributeValueType.DATETIME, Triple(value.toString(), null, null))
is LocalTime -> Pair(AttributeValueType.TIME, Triple(value.toString(), null, null))
is URI -> Pair(AttributeValueType.URI, Triple(value.toString(), null, null))
else -> Pair(AttributeValueType.STRING, Triple(value.toString(), null, null))
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class AttributeInstanceUtilsTests {
fun `it should guess the value type of a time property`() = runTest {
val expandedTimeProperty = expandAttribute(
"property",
mapOf("type" to "Property", "value" to LocalTime.now()),
mapOf("type" to "Property", "value" to mapOf("@type" to "Time", "@value" to LocalTime.now())),
NGSILD_TEST_CORE_CONTEXTS
)
assertEquals(
Expand All @@ -118,19 +118,6 @@ class AttributeInstanceUtilsTests {
)
}

@Test
fun `it should guess the value type of an URI property`() = runTest {
val expandedTimeProperty = expandAttribute(
"property",
mapOf("type" to "Property", "value" to "urn:ngsi-ld:uri"),
NGSILD_TEST_CORE_CONTEXTS
)
assertEquals(
TemporalEntityAttribute.AttributeValueType.URI,
guessAttributeValueType(TemporalEntityAttribute.AttributeType.Property, expandedTimeProperty.second[0])
)
}

@Test
fun `it should guess the value type of a geo-property`() = runTest {
val expandedGeoProperty = expandAttribute(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import arrow.core.Either
import arrow.core.flatMap
import arrow.core.left
import arrow.core.right
import com.egm.stellio.shared.util.*
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_ID
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_TYPE
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_VALUE
Expand All @@ -17,7 +16,9 @@ import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_PROPERTY_VALUE
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_RELATIONSHIP_OBJECT
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_SCOPE_PROPERTY
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_TIME_TYPE
import com.egm.stellio.shared.util.JsonLdUtils.buildNonReifiedPropertyValue
import com.egm.stellio.shared.util.JsonLdUtils.buildNonReifiedTemporalValue
import com.egm.stellio.shared.util.toUri
import java.net.URI
import java.time.LocalDate
import java.time.LocalTime
Expand Down Expand Up @@ -47,7 +48,7 @@ fun ExpandedAttributes.getAttributeFromExpandedAttributes(
if (datasetId == null)
!expandedAttributeInstance.containsKey(NGSILD_DATASET_ID_PROPERTY)
else
expandedAttributeInstance.getMemberValue(NGSILD_DATASET_ID_PROPERTY) == datasetId.toString()
expandedAttributeInstance.getDatasetId() == datasetId
}
}

Expand All @@ -69,7 +70,7 @@ fun ExpandedAttributeInstances.addNonReifiedProperty(
): ExpandedAttributeInstances {
if (this.isEmpty() || this.size > 1)
throw BadRequestDataException("Cannot add a sub-attribute into empty or multi-instance attribute: $this")
return listOf(this[0].plus(subAttributeName to JsonLdUtils.buildNonReifiedPropertyValue(subAttributeValue)))
return listOf(this[0].plus(subAttributeName to buildNonReifiedPropertyValue(subAttributeValue)))
}

fun ExpandedAttributeInstances.addNonReifiedTemporalProperty(
Expand Down Expand Up @@ -161,6 +162,9 @@ fun ExpandedAttributeInstance.getMemberValue(memberName: ExpandedTerm): Any? {
}
}

fun ExpandedAttributeInstance.getPropertyValue(): Any? =
getMemberValue(NGSILD_PROPERTY_VALUE)

fun ExpandedAttributeInstance.getMemberValueAsDateTime(memberName: ExpandedTerm): ZonedDateTime? =
ZonedDateTime::class.safeCast(this.getMemberValue(memberName))

Expand Down Expand Up @@ -200,24 +204,5 @@ fun ExpandedAttributeInstance.getScopes(): List<String>? =
else -> null
}

fun ExpandedAttributeInstance.getPropertyValue(): Any {
val hasValueEntry = this[NGSILD_PROPERTY_VALUE]!!

return if (hasValueEntry.size == 1 && (hasValueEntry[0] as Map<String, Any>).containsKey(JSONLD_VALUE)) {
val rawValue = (hasValueEntry[0] as Map<String, Any>)[JSONLD_VALUE]!!
if (rawValue is String) {
when {
rawValue.isURI() -> rawValue.toUri()
rawValue.isTime() -> LocalTime.parse(rawValue)
rawValue.isDate() -> LocalDate.parse(rawValue)
rawValue.isDateTime() -> ZonedDateTime.parse(rawValue)
else -> rawValue
}
} else rawValue
} else if (hasValueEntry.size == 1)
hasValueEntry[0] as Map<String, Any>
else hasValueEntry.map { (it as Map<String, Any>)[JSONLD_VALUE]!! }
}

fun castAttributeValue(value: Any): ExpandedAttributeInstances =
value as List<Map<String, List<Any>>>
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class NgsiLdPropertyInstance private constructor(
name: String,
values: ExpandedAttributeInstance
): Either<APIException, NgsiLdPropertyInstance> = either {
val value = values.getMemberValue(NGSILD_PROPERTY_VALUE)
val value = values.getPropertyValue()
ensureNotNull(value) {
BadRequestDataException("Property $name has an instance without a value")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.egm.stellio.shared.model

import com.egm.stellio.shared.util.JsonLdUtils
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_ID
import com.egm.stellio.shared.util.*
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_CREATED_AT_PROPERTY
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_MODIFIED_AT_PROPERTY
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_RELATIONSHIP_OBJECT
import com.egm.stellio.shared.util.JsonLdUtils.buildExpandedPropertyValue
import com.egm.stellio.shared.util.NGSILD_TEST_CORE_CONTEXTS
import com.egm.stellio.shared.util.ngsiLdDateTime
import com.egm.stellio.shared.util.toUri
import com.egm.stellio.shared.util.JsonLdUtils.buildExpandedRelationshipValue
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.*
Expand Down Expand Up @@ -163,26 +160,20 @@ class ExpandedMembersTests {
)
)

val result = relationshipValues.extractRelationshipObject("isARelationship")
assertTrue(result.isLeft())
result.mapLeft {
assertEquals("Relationship isARelationship has an invalid or no object id: null", it.message)
}
relationshipValues.extractRelationshipObject("isARelationship")
.shouldFail {
assertEquals("Relationship isARelationship has an invalid or no object id: null", it.message)
}
}

@Test
fun `it should extract the target object of a relationship`() {
val relationshipObjectId = "urn:ngsi-ld:T:1"
val relationshipValues = mapOf(
NGSILD_RELATIONSHIP_OBJECT to listOf(
mapOf(JSONLD_ID to relationshipObjectId)
)
)
val relationshipValues = buildExpandedRelationshipValue(relationshipObjectId.toUri())

val result = relationshipValues.extractRelationshipObject("isARelationship")
assertTrue(result.isRight())
result.map {
assertEquals(relationshipObjectId.toUri(), it)
}
relationshipValues[0].extractRelationshipObject("isARelationship")
.shouldSucceedWith {
assertEquals(relationshipObjectId.toUri(), it)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.egm.stellio.shared.util

import com.egm.stellio.shared.model.BadRequestDataException
import com.egm.stellio.shared.model.ExpandedEntity
import com.egm.stellio.shared.model.GeoQuery
import com.egm.stellio.shared.model.GeoQuery.GeometryType
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_LOCATION_PROPERTY
Expand Down Expand Up @@ -210,4 +211,30 @@ class GeoQueryUtilsTests {
"coordinates" to "[57.5522,%20-20.3484]",
"geoproperty" to geoproperty
)

private suspend fun gimmeSimpleEntityWithGeoProperty(
propertyKey: String,
longitude: Double,
latitude: Double
): ExpandedEntity {
val entityWithLocation =
"""
{
"id": "urn:ngsi-ld:Entity:01",
"type": "Entity",
"$propertyKey": {
"type": "GeoProperty",
"value": {
"type": "Point",
"coordinates": [
$longitude,
$latitude
]
}
}
}
""".trimIndent()

return JsonLdUtils.expandJsonLdEntity(entityWithLocation, NGSILD_TEST_CORE_CONTEXTS)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ fun loadMinimalEntityWithSap(
}
""".trimIndent()

suspend fun loadAndExpandMinimalEntity(
id: String,
type: String,
contexts: List<String> = APIC_COMPOUND_CONTEXTS
): ExpandedEntity =
loadAndExpandMinimalEntity(id, listOf(type), contexts)

suspend fun loadAndExpandMinimalEntity(
id: String,
types: List<String>,
contexts: List<String> = APIC_COMPOUND_CONTEXTS
): ExpandedEntity {
val entity = mapOf(
"id" to id,
"type" to types
)

return expandJsonLdEntity(entity, contexts)
}

suspend fun String.sampleDataToNgsiLdEntity(): Either<APIException, Pair<ExpandedEntity, NgsiLdEntity>> {
val jsonLdEntity = expandJsonLdEntity(this)
return when (val ngsiLdEntity = jsonLdEntity.toNgsiLdEntity()) {
Expand Down
Loading

0 comments on commit df15c3b

Please sign in to comment.