-
Notifications
You must be signed in to change notification settings - Fork 6
SDK-Issues : Refactoring the code to render as CloudEvent and schema validation #35
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
45b2d84
Refactoring the code to render as cloudevent and schema validation
03a2f8f
removing empty and commented lines
9eded93
Merge branch 'main' into sdk_issues
rjalander e14cff2
fixing linter checks
4c21e5f
addressing review comments
4a60f32
adding missing file CDEventsException
ea72d93
comiting test file
4a7b872
adding Missing JavadocMethod
0e15d00
Merge branch 'main' into sdk_issues
rjalander a94c0b7
merge from main
e60ba0c
merge from main
b247dc6
removed unused helper
ad28140
removing unused imports
2ebbc17
schema offline validation using json schema
6548a32
adding test for Invalid event schema exception
9605f56
OperatorWrap checkstyle config
a4aeb14
adding log4j to print the logs on the console
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package dev.cdevents; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.networknt.schema.JsonSchema; | ||
import com.networknt.schema.JsonSchemaFactory; | ||
import com.networknt.schema.SpecVersion; | ||
import com.networknt.schema.ValidationMessage; | ||
import dev.cdevents.config.CustomObjectMapper; | ||
import dev.cdevents.exception.CDEventsException; | ||
import dev.cdevents.models.CDEvent; | ||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.core.v03.CloudEventBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.OffsetDateTime; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
public final class CDEvents { | ||
|
||
private CDEvents() { | ||
} | ||
|
||
private static ObjectMapper objectMapper = new CustomObjectMapper().customConfiguration(); | ||
private static Logger log = LoggerFactory.getLogger(CDEvents.class); | ||
|
||
|
||
/** | ||
* @param cdEvent | ||
* @return json string of a cdEvent | ||
*/ | ||
public static String cdEventAsJson(CDEvent cdEvent) { | ||
try { | ||
return objectMapper.writeValueAsString(cdEvent); | ||
} catch (JsonProcessingException e) { | ||
log.error("Error while mapping cdEvent as Json {}", e.getMessage()); | ||
throw new CDEventsException("Error while mapping cdEvent as Json {}", e); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Creates a CloudEvent from the cdEvent. | ||
* @param cdEvent | ||
* @return CloudEvent | ||
*/ | ||
public static CloudEvent cdEventAsCloudEvent(CDEvent cdEvent) { | ||
|
||
String cdEventJson = cdEventAsJson(cdEvent); | ||
if (!validateCDEvent(cdEvent)) { | ||
log.error("CDEvent validation failed against schema URL - {}", cdEvent.schemaURL()); | ||
throw new CDEventsException("CDEvent validation failed against schema URL - " + cdEvent.schemaURL()); | ||
} | ||
CloudEvent ceToSend = new CloudEventBuilder() | ||
.withId(UUID.randomUUID().toString()) | ||
.withSource(cdEvent.getContext().getSource()) | ||
.withType(cdEvent.getContext().getType()) | ||
.withDataContentType("application/json") | ||
.withData(cdEventJson.getBytes(StandardCharsets.UTF_8)) | ||
.withTime(OffsetDateTime.now()) | ||
.build(); | ||
|
||
return ceToSend; | ||
} | ||
|
||
/** | ||
* Validates the cdEvent against the Schema URL. | ||
* @param cdEvent | ||
* @return valid cdEvent | ||
*/ | ||
public static boolean validateCDEvent(CDEvent cdEvent) { | ||
String cdEventJson = cdEventAsJson(cdEvent); | ||
|
||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); | ||
JsonSchema jsonSchema = factory.getSchema(cdEvent.eventSchema()); | ||
|
||
JsonNode jsonNode = objectMapper.convertValue(cdEvent, ObjectNode.class); | ||
Set<ValidationMessage> errors = jsonSchema.validate(jsonNode); | ||
|
||
if (!errors.isEmpty()) { | ||
log.error("CDEvent validation failed with errors {}", errors); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.cdevents.config; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
public class CustomObjectMapper extends ObjectMapper { | ||
|
||
/** | ||
* @return customized ObjectMapper | ||
*/ | ||
public ObjectMapper customConfiguration() { | ||
return this | ||
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) | ||
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING) | ||
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) | ||
.registerModule(new JavaTimeModule()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Copyright 2023-Present https://cdevents.dev/ | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* | ||
*/ | ||
|
||
package dev.cdevents.config; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.