Skip to content

Commit

Permalink
fix: bug on create endpoint and PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasBousselin committed Sep 3, 2024
1 parent 314cd5c commit 9497e26
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ import arrow.core.right
import com.egm.stellio.shared.model.*
import com.egm.stellio.shared.util.JSON_LD_MEDIA_TYPE
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_CONTEXT
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_CSR_TERM
import com.egm.stellio.shared.util.JsonLdUtils.compactTerm
import com.egm.stellio.shared.util.JsonLdUtils.expandJsonLdTerm
import com.egm.stellio.shared.util.JsonUtils.deserializeAs
import com.egm.stellio.shared.util.JsonUtils.serializeObject
import com.egm.stellio.shared.util.mapper
import com.egm.stellio.shared.util.dataTypeMapper
import com.egm.stellio.shared.util.ngsiLdDateTime
import com.egm.stellio.shared.util.toUri
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.module.kotlin.convertValue
import org.springframework.data.annotation.Id
import org.springframework.http.MediaType
import java.net.URI
import java.time.Instant
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.*

data class ContextSourceRegistration(
@Id val id: URI = "urn:ngsi-ld:ContextSourceRegistration:${UUID.randomUUID()}".toUri(),
val id: URI = "urn:ngsi-ld:ContextSourceRegistration:${UUID.randomUUID()}".toUri(),
val endpoint: URI,
val type: String = TYPE,
val type: String = NGSILD_CSR_TERM,
val mode: Mode = Mode.INCLUSIVE,
val information: List<RegistrationInfo>,
val operations: List<Operation> = Operation.FEDERATION_OPS,
val createdAt: ZonedDateTime = Instant.now().atZone(ZoneOffset.UTC),
val operations: List<Operation> = listOf(Operation.FEDERATION_OPS),
val createdAt: ZonedDateTime = ngsiLdDateTime(),
val modifiedAt: ZonedDateTime? = null,
val observationInterval: TimeInterval?,
val managementInterval: TimeInterval?
Expand Down Expand Up @@ -60,9 +60,15 @@ data class ContextSourceRegistration(
}

data class EntityInfo(
val id: URI?,
val idPattern: String?,
val type: List<String> // todo can be String
val id: URI? = null,
val idPattern: String? = null,
@JsonFormat(
with = [
JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED
]
)
val type: List<String>
) {
fun expand(contexts: List<String>): EntityInfo =
this.copy(
Expand Down Expand Up @@ -95,14 +101,16 @@ data class ContextSourceRegistration(
includeSysAttrs = includeSysAttrs,
attributeRepresentation = AttributeRepresentation.NORMALIZED
)
return mapper.writeValueAsString(
mapper.convertValue<Map<String, Any>>(
return dataTypeMapper.writeValueAsString(
dataTypeMapper.convertValue<Map<String, Any>>(
(this.compact(contexts))
).toFinalRepresentation(representation) // todo dangerous?
).plus(JSONLD_CONTEXT to contexts)
.toFinalRepresentation(representation) // todo dangerous?
)
}

companion object {

fun deserialize(
input: Map<String, Any>,
contexts: List<String>
Expand All @@ -112,14 +120,13 @@ data class ContextSourceRegistration(
.expand(contexts)
}.fold(
{ it.right() },
{ it.toAPIException("Failed to parse context_source_registration").left() }
{ it.toAPIException("Failed to parse CSourceRegistration caused by :\n ${it.message}").left() }
)

fun contextSourceRegistrationNotFoundMessage(id: URI) = "Could not find a CSourceRegistration with id $id"
fun contextSourceRegistrationAlreadyExistsMessage(id: URI) = "A CSourceRegistration with id $id already exists"
fun contextSourceRegistrationUnauthorizedMessage(id: URI) =
"User is not authorized to access CSourceRegistration $id"
const val TYPE = "ContextSourceRegistration"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ enum class Operation(val string: String) {
RETRIEVE_ATTR_TYPE_DETAILS("retrieveAttrTypeDetails"),

@JsonProperty("retrieveAttrTypeInfo")
RETRIEVE_ATTRTYPE_INFO("retrieveAttrTypeInfo"),
RETRIEVE_ATTR_TYPE_INFO("retrieveAttrTypeInfo"),

// SUBSCRIPTION
@JsonProperty("createSubscription")
Expand All @@ -111,24 +111,68 @@ enum class Operation(val string: String) {
QUERY_SUBSCRIPTION("querySubscription"),

@JsonProperty("deleteSubscription")
DELETE_SUBSCRIPTION("deleteSubscription");
DELETE_SUBSCRIPTION("deleteSubscription"),

// OPERATION GROUPS
@JsonProperty("federationOps")
FEDERATION_OPS("federationOps"),

@JsonProperty("updateOps")
UPDATE_OPS("updateOps"),

@JsonProperty("retrieveOps")
RETRIEVE_OPS("retrieveOps"),

@JsonProperty("redirectionOps")
REDIRECTION_OPS("redirectionOps");

companion object {
val FEDERATION_OPS = listOf(
RETRIEVE_ENTITY,
QUERY_ENTITY,
QUERY_BATCH,
RETRIEVE_ENTITY_TYPES,
RETRIEVE_ENTITY_TYPE_DETAILS,
RETRIEVE_ENTITY_TYPE_INFO,
RETRIEVE_ATTR_TYPES,
RETRIEVE_ATTR_TYPE_DETAILS,
RETRIEVE_ATTRTYPE_INFO,
CREATE_SUBSCRIPTION,
UPDATE_SUBSCRIPTION,
RETRIEVE_SUBSCRIPTION,
QUERY_SUBSCRIPTION,
DELETE_SUBSCRIPTION,
val operationGroups = mapOf(
"federationOps" to listOf(
RETRIEVE_ENTITY,
QUERY_ENTITY,
QUERY_BATCH,
RETRIEVE_ENTITY_TYPES,
RETRIEVE_ENTITY_TYPE_DETAILS,
RETRIEVE_ENTITY_TYPE_INFO,
RETRIEVE_ATTR_TYPES,
RETRIEVE_ATTR_TYPE_DETAILS,
RETRIEVE_ATTR_TYPE_INFO,
CREATE_SUBSCRIPTION,
UPDATE_SUBSCRIPTION,
RETRIEVE_SUBSCRIPTION,
QUERY_SUBSCRIPTION,
DELETE_SUBSCRIPTION,
),
"updateOps" to listOf(
UPDATE_ENTITY,
UPDATE_ATTRS,
REPLACE_ENTITY,
REPLACE_ATTRS
),
"retrieveOps" to listOf(
RETRIEVE_ENTITY,
QUERY_ENTITY
),
"redirectionOps" to listOf(
CREATE_ENTITY,
UPDATE_ENTITY,
APPEND_ATTRS,
UPDATE_ATTRS,
DELETE_ATTRS,
DELETE_ENTITY,
MERGE_ENTITY,
REPLACE_ENTITY,
REPLACE_ATTRS,
RETRIEVE_ENTITY,
QUERY_ENTITY,
RETRIEVE_ENTITY_TYPES,
RETRIEVE_ENTITY_TYPE_DETAILS,
RETRIEVE_ENTITY_TYPE_INFO,
RETRIEVE_ATTR_TYPES,
RETRIEVE_ATTR_TYPE_DETAILS,
RETRIEVE_ATTR_TYPE_INFO
)
)
fun fromString(operation: String): Operation? =
Operation.entries.find { it.string == operation }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ class ContextSourceRegistrationService(
mode = Mode.fromString(row["mode"] as? String),
information = mapper.readerForListOf(RegistrationInfo::class.java)
.readValue((row["information"] as Json).asString()),
operations = (row["operations"] as? String)?.split(",")?.mapNotNull { Operation.fromString(it) }
?: Operation.FEDERATION_OPS,
operations = (row["operations"] as Array<String>).mapNotNull { Operation.fromString(it) },
createdAt = toZonedDateTime(row["created_at"]),
modifiedAt = toOptionalZonedDateTime(row["modified_at"]),
observationInterval = row["observation_interval_start"]?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
CREATE TABLE public.context_source_registration
CREATE TABLE context_source_registration
(
id text,
endpoint text,
mode text,
information jsonb,
operations text[],
endpoint text NOT NULL,
mode text NOT NULL,
information jsonb NOT NULL,
operations text[] NOT NULL,
observation_interval_start timestamp with time zone,
observation_interval_end timestamp with time zone,
management_interval_start timestamp with time zone,
management_interval_end timestamp with time zone,
sub text,
created_at timestamp with time zone NOT NULL DEFAULT '1970-01-01 00:00:00'::timestamp without time zone,
created_at timestamp with time zone NOT NULL,
modified_at timestamp with time zone,
status text,
times_sent integer,
Expand Down

0 comments on commit 9497e26

Please sign in to comment.