Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for jsonldContext member in subscriptions #1230

Merged
merged 7 commits into from
Sep 9, 2024

Conversation

ranim-n
Copy link
Contributor

@ranim-n ranim-n commented Aug 28, 2024

No description provided.

@ranim-n ranim-n added feature New feature or request subscription Relates to subscription API ngsild-1.7.1 labels Aug 28, 2024
@ranim-n ranim-n self-assigned this Aug 28, 2024
@ranim-n ranim-n linked an issue Aug 28, 2024 that may be closed by this pull request
@bobeal
Copy link
Member

bobeal commented Aug 28, 2024

some points are missing in the implementation.

for instance, in 5.8.1.4, it is said: The @context to be used for sending Notifications related to this Subscription shall be the one specified in the jsonldContext field. If not present, the jsonldContext field shall be initialized with the @context applicable for the Subscription (if @context is also not present in the Subscription, see clause 5.5.5). When the remote JSON-LD @context referenced by the jsonldContext field is not available implementations shall raise an error of type LdContextNotAvailable. If the remote JSON-LD @context referenced by the jsonldContext field is invalid, implementations shall raise an error of type BadRequestData.

(search for jsonldContext in the specification)

Copy link
Contributor

github-actions bot commented Aug 28, 2024

Test Results

   62 files  ±0     62 suites  ±0   1m 23s ⏱️ +2s
1 014 tests +4  1 014 ✅ +4  0 💤 ±0  0 ❌ ±0 
1 053 runs  +4  1 053 ✅ +4  0 💤 ±0  0 ❌ ±0 

Results for commit d928b62. ± Comparison against base commit 2d2e0ff.

This pull request removes 190 and adds 38 tests. Note that renamed tests count towards both.
                                    { "id":…, withTemporalValues=true, withAudit=false, expectation={
                      "@id": "https://uri…
                      "@type": "@json",
                      …
                    "@value": "/A/B"
                    "@value": "/C/D"
                    "@value": 20
                    "…
                    {
                  "@type": "https://uri.etsi.org/ngsi-ld/DateTime",
…
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [1] source={
    "attribute": {
        "type": "Property",
        "value": 12.0,
        "observedAt": "2024-04-14T12:34:56Z"
    }
}, target={
    "attribute": {
        "type": "Property",
        "value": 12.2,
        "unitCode": "GRM"
    }
}, expected={
    "attribute": {
        "type": "Property",
        "value": 12.2,
        "unitCode": "GRM",
        "observedAt": "2024-04-14T12:34:56Z"
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [1] source={
    "attribute": {
        "type": "Property",
        "value": 12.0,
        "observedAt": "2024-04-14T12:34:56Z",
        "subAttribute": {
            "type": "Property",
            "value": "subAttribute"
        }
    }
}, target={
    "attribute": {
        "type": "Property",
        "value": 12.2,
        "unitCode": "GRM",
        "subAttribute": {
            "type": "Property",
            "value": "newSubAttributeValue"
        }
    }
}, expected={
    "attribute": {
        "type": "Property",
        "value": 12.2,
        "unitCode": "GRM",
        "observedAt": "2024-04-14T12:34:56Z",
        "subAttribute": {
            "type": "Property",
            "value": "newSubAttributeValue"
        }
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [2] source={
    "attribute": {
        "type": "Property",
        "value": { "en": "car", "fr": "voiture" }
    }
}, target={
    "attribute": {
        "type": "Property",
        "value": { "fr": "vélo", "es": "bicicleta" }
    }
}, expected={
    "attribute": {
        "type": "Property",
        "value": { "en": "car", "fr": "vélo", "es": "bicicleta" }
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [2] source={
    "incoming": {
        "type": "JsonProperty",
        "json": { "id": 1, "b": null, "c": 12.4 },
        "observedAt": "2022-12-24T14:01:22.066Z",
        "subAttribute": {
            "type": "Property",
            "value": "subAttribute"
        }
    }
}, target={
    "incoming": {
        "type": "JsonProperty",
        "json": { "id": 2, "b": "something" },
        "observedAt": "2023-12-24T14:01:22.066Z"
    }
}, expected={
    "incoming": {
        "type": "JsonProperty",
        "json": { "id": 2, "b": "something" },
        "observedAt": "2023-12-24T14:01:22.066Z",
        "subAttribute": {
            "type": "Property",
            "value": "subAttribute"
        }
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [3] source={
    "attribute": {
        "type": "Property",
        "value": [ "car", "voiture" ]
    }
}, target={
    "attribute": {
        "type": "Property",
        "value": [ "vélo", "bicicleta" ]
    }
}, expected={
    "attribute": {
        "type": "Property",
        "value": [ "vélo", "bicicleta" ]
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [3] source={
    "attribute": {
        "type": "VocabProperty",
        "vocab": "stellio"
    }
}, target={
    "attribute": {
        "type": "VocabProperty",
        "vocab": "egm"
    }
}, expected={
    "attribute": {
        "type": "VocabProperty",
        "vocab": "egm"
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [4] source={
    "attribute": {
        "type": "Relationship",
        "object": "urn:ngsi-ld:Entity:01"
    }
}, target={
    "attribute": {
        "type": "Relationship",
        "object": "urn:ngsi-ld:Entity:02"
    }
}, expected={
    "attribute": {
        "type": "Relationship",
        "object": "urn:ngsi-ld:Entity:02"
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [5] source={
    "attribute": {
        "type": "LanguageProperty",
        "languageMap": { "en": "train", "fr": "train" }
    }
}, target={
    "attribute": {
        "type": "LanguageProperty",
        "languageMap": { "fr": "TGV", "es": "tren" }
    }
}, expected={
    "attribute": {
        "type": "LanguageProperty",
        "languageMap": { "en": "train", "fr": "TGV", "es": "tren" }
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [6] source={
    "incoming": {
        "type": "JsonProperty",
        "json": { "a": 1, "b": "thing" }
    }
}, target={
    "incoming": {
        "type": "JsonProperty",
        "json": { "a": 2, "c": "other thing" }
    }
}, expected={
    "incoming": {
        "type": "JsonProperty",
        "json": { "a": 2, "b": "thing", "c": "other thing" }
    }
}
com.egm.stellio.search.entity.util.PatchAttributeTests ‑ [7] source={
    "attribute": {
        "type": "VocabProperty",
        "vocab": "stellio"
    }
}, target={
    "attribute": {
        "type": "VocabProperty",
        "vocab": "egm"
    }
}, expected={
    "attribute": {
        "type": "VocabProperty",
        "vocab": "egm"
    }
}
…

♻️ This comment has been updated with latest results.

@ranim-n
Copy link
Contributor Author

ranim-n commented Aug 29, 2024

some points are missing in the implementation.

for instance, in 5.8.1.4, it is said: The @context to be used for sending Notifications related to this Subscription shall be the one specified in the jsonldContext field. If not present, the jsonldContext field shall be initialized with the @context applicable for the Subscription (if @context is also not present in the Subscription, see clause 5.5.5). When the remote JSON-LD @context referenced by the jsonldContext field is not available implementations shall raise an error of type LdContextNotAvailable. If the remote JSON-LD @context referenced by the jsonldContext field is invalid, implementations shall raise an error of type BadRequestData.

(search for jsonldContext in the specification)

to check if a jsonld context is available or valid do I make an http request to it to make sure it is reachable or do I use it in compaction/expansion to test it... ?

@bobeal
Copy link
Member

bobeal commented Aug 29, 2024

to check if a jsonld context is available or valid do I make an http request to it to make sure it is reachable or do I use it in compaction/expansion to test it... ?

Doing a compaction or expansion is too expensive. So option 1.

The JSON-LD library should already have some functions to do it (this is something it is doing every time we expand / compact). Maybe in https://github.com/filip26/titanium-json-ld/blob/main/src/main/java/com/apicatalog/jsonld/loader/DefaultHttpLoader.java?

@ranim-n
Copy link
Contributor Author

ranim-n commented Aug 30, 2024

to check if a jsonld context is available or valid do I make an http request to it to make sure it is reachable or do I use it in compaction/expansion to test it... ?

Doing a compaction or expansion is too expensive. So option 1.

The JSON-LD library should already have some functions to do it (this is something it is doing every time we expand / compact). Maybe in https://github.com/filip26/titanium-json-ld/blob/main/src/main/java/com/apicatalog/jsonld/loader/DefaultHttpLoader.java?

when updating a subscription there are no checks (see #1090 ) should I do a check for the jsonldContext as stated in 5.8.2.4 anyway ?

@bobeal
Copy link
Member

bobeal commented Aug 30, 2024

to check if a jsonld context is available or valid do I make an http request to it to make sure it is reachable or do I use it in compaction/expansion to test it... ?

Doing a compaction or expansion is too expensive. So option 1.
The JSON-LD library should already have some functions to do it (this is something it is doing every time we expand / compact). Maybe in https://github.com/filip26/titanium-json-ld/blob/main/src/main/java/com/apicatalog/jsonld/loader/DefaultHttpLoader.java?

when updating a subscription there are no checks (see #1090 ) should I do a check for the jsonldContext as stated in 5.8.2.4 anyway ?

Start with the creation only. Will see later.

Base automatically changed from refactor/prepare-handlers-for-csr to develop August 30, 2024 13:33
@bobeal bobeal force-pushed the feature/support-for-jsonldContext-in-subscriptions branch from 4254e8b to ba3e0e9 Compare August 31, 2024 06:31
@thomasBousselin
Copy link
Contributor

did you look at the sonar issues?

@@ -49,9 +49,11 @@ class NotificationService(
AttributeRepresentation.SIMPLIFIED
else AttributeRepresentation.NORMALIZED

val context = it.jsonldContext?.toString()?.let { listOf(it) } ?: it.contexts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • since context is a list, call it contexts
  • a bit easier to read: it.jsonldContext?.let { listOf(it.toString()) } ?: it.contexts

Comment on lines 248 to 255
val notificationResult = notificationResults[0]
assertEquals(subscription.id, notificationResult.first.id)
assertEquals(subscription.id, notificationResult.second.subscriptionId)
assertEquals(1, notificationResult.second.data.size)
assertTrue(notificationResult.second.data[0].containsKey(NGSILD_NAME_TERM))
assertTrue(notificationResult.second.data[0].containsKey(MANAGED_BY_COMPACT_RELATIONSHIP))
assertEquals(APIC_COMPOUND_CONTEXT, notificationResult.second.data[0][JsonLdUtils.JSONLD_CONTEXT])
assertTrue(notificationResult.third)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to repeat the assertions of the previous test. here, you want to check what should be different when you have a specific jsonldContext.

I am also a bit surprised that the entity in the notification has the same keys than the entity in the previous test, whereas they are not compacted with the same contexts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keys are not the same. In the previous entity they are NGSILD_NAME_PROPERTY and MANAGED_BY_RELATIONSHIP) . In this entity they are NGSILD_NAME_TERM and MANAGED_BY_COMPACT_RELATIONSHIP

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, my bad

Comment on lines 252 to 261
fun checkJsonldContext(context: URI): Either<APIException, Unit> = either {
return try {
context.toString().toUri()
val options = DocumentLoaderOptions()
loader.loadDocument(context, options)
Unit.right()
} catch (e: JsonLdError) {
e.toAPIException(e.cause?.cause?.message).left()
} catch (e: BadRequestDataException) {
e.left()
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about:

    fun checkJsonldContext(context: URI): Either<APIException, Unit> = either {
        return try {
            loader.loadDocument(context, DocumentLoaderOptions())
            Unit.right()
        } catch (e: JsonLdError) {
            e.toAPIException(e.cause?.cause?.message).left()
        } catch (e: IllegalArgumentException) {
            BadRequestDataException(e.cause?.message ?: "Provided context is invalid: $context").left()
        }
    }

@bobeal
Copy link
Member

bobeal commented Sep 6, 2024

@ranim-n don't forget the last comments in this PR

@thomasBousselin
Copy link
Contributor

have you check getContextsForSubscription() function

@bobeal
Copy link
Member

bobeal commented Sep 6, 2024

LGTM

@ranim-n
Copy link
Contributor Author

ranim-n commented Sep 9, 2024

@thomasBousselin

@ranim-n ranim-n force-pushed the feature/support-for-jsonldContext-in-subscriptions branch from fcd1ce8 to d928b62 Compare September 9, 2024 08:12
Copy link

sonarcloud bot commented Sep 9, 2024

@ranim-n ranim-n merged commit cf5b4f7 into develop Sep 9, 2024
10 checks passed
@ranim-n ranim-n deleted the feature/support-for-jsonldContext-in-subscriptions branch September 9, 2024 08:21
@github-actions github-actions bot locked and limited conversation to collaborators Sep 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New feature or request ngsild-1.7.1 subscription Relates to subscription API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for jsonldContext member in subscriptions
3 participants