Skip to content

Commit 758569f

Browse files
authored
SDK-Issues : Refactoring the code to render as CloudEvent and schema validation (#35)
This comment has the Refactoring code changes to create CDEvents using an object model, Creating a smaller PR to have this for only one CDEvent - PipelineRunFinishedCDEvent and the similar pattern can be extended to all other events from the specification. Included fixes: #26, #29, #30, #31
1 parent 096635c commit 758569f

17 files changed

+1490
-603
lines changed

.github/linters/sun_checks.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@
9292
<module name="InvalidJavadocPosition"/>
9393
<module name="JavadocMethod"/>
9494
<module name="JavadocType"/>
95-
<module name="JavadocVariable"/>
95+
<module name="JavadocVariable">
96+
<property name="scope" value="public"/>
97+
</module>
9698
<module name="JavadocStyle"/>
9799
<module name="MissingJavadocMethod"/>
98100
<!-- Enables @SuppressWarnings Support -->
@@ -135,7 +137,12 @@
135137
<module name="MethodParamPad"/>
136138
<module name="NoWhitespaceAfter"/>
137139
<module name="NoWhitespaceBefore"/>
138-
<module name="OperatorWrap"/>
140+
<module name="OperatorWrap">
141+
<property name="tokens"
142+
value="ASSIGN,DIV_ASSIGN,PLUS_ASSIGN,MINUS_ASSIGN,STAR_ASSIGN,MOD_ASSIGN,
143+
SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,BXOR_ASSIGN,BOR_ASSIGN,BAND_ASSIGN"/>
144+
<property name="option" value="eol"/>
145+
</module>
139146
<module name="ParenPad"/>
140147
<module name="TypecastParenPad"/>
141148
<module name="WhitespaceAfter"/>

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@
117117
<version>2.0.6</version>
118118
</dependency>
119119

120+
<dependency>
121+
<groupId>com.fasterxml.jackson.datatype</groupId>
122+
<artifactId>jackson-datatype-jsr310</artifactId>
123+
<version>2.14.2</version>
124+
</dependency>
125+
126+
<dependency>
127+
<groupId>com.networknt</groupId>
128+
<artifactId>json-schema-validator</artifactId>
129+
<version>1.0.78</version>
130+
</dependency>
131+
132+
120133
</dependencies>
121134
<build>
122135
<pluginManagement>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package dev.cdevents;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
7+
import com.networknt.schema.JsonSchema;
8+
import com.networknt.schema.JsonSchemaFactory;
9+
import com.networknt.schema.SpecVersion;
10+
import com.networknt.schema.ValidationMessage;
11+
import dev.cdevents.config.CustomObjectMapper;
12+
import dev.cdevents.exception.CDEventsException;
13+
import dev.cdevents.models.CDEvent;
14+
import io.cloudevents.CloudEvent;
15+
import io.cloudevents.core.v03.CloudEventBuilder;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
import java.nio.charset.StandardCharsets;
20+
import java.time.OffsetDateTime;
21+
import java.util.Set;
22+
import java.util.UUID;
23+
24+
public final class CDEvents {
25+
26+
private CDEvents() {
27+
}
28+
29+
private static ObjectMapper objectMapper = new CustomObjectMapper().customConfiguration();
30+
private static Logger log = LoggerFactory.getLogger(CDEvents.class);
31+
32+
33+
/**
34+
* @param cdEvent
35+
* @return json string of a cdEvent
36+
*/
37+
public static String cdEventAsJson(CDEvent cdEvent) {
38+
try {
39+
return objectMapper.writeValueAsString(cdEvent);
40+
} catch (JsonProcessingException e) {
41+
log.error("Error while mapping cdEvent as Json {}", e.getMessage());
42+
throw new CDEventsException("Error while mapping cdEvent as Json {}", e);
43+
}
44+
}
45+
46+
47+
/**
48+
* Creates a CloudEvent from the cdEvent.
49+
* @param cdEvent
50+
* @return CloudEvent
51+
*/
52+
public static CloudEvent cdEventAsCloudEvent(CDEvent cdEvent) {
53+
54+
String cdEventJson = cdEventAsJson(cdEvent);
55+
if (!validateCDEvent(cdEvent)) {
56+
log.error("CDEvent validation failed against schema URL - {}", cdEvent.schemaURL());
57+
throw new CDEventsException("CDEvent validation failed against schema URL - " + cdEvent.schemaURL());
58+
}
59+
CloudEvent ceToSend = new CloudEventBuilder()
60+
.withId(UUID.randomUUID().toString())
61+
.withSource(cdEvent.getContext().getSource())
62+
.withType(cdEvent.getContext().getType())
63+
.withDataContentType("application/json")
64+
.withData(cdEventJson.getBytes(StandardCharsets.UTF_8))
65+
.withTime(OffsetDateTime.now())
66+
.build();
67+
68+
return ceToSend;
69+
}
70+
71+
/**
72+
* Validates the cdEvent against the Schema URL.
73+
* @param cdEvent
74+
* @return valid cdEvent
75+
*/
76+
public static boolean validateCDEvent(CDEvent cdEvent) {
77+
String cdEventJson = cdEventAsJson(cdEvent);
78+
79+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
80+
JsonSchema jsonSchema = factory.getSchema(cdEvent.eventSchema());
81+
82+
JsonNode jsonNode = objectMapper.convertValue(cdEvent, ObjectNode.class);
83+
Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
84+
85+
if (!errors.isEmpty()) {
86+
log.error("CDEvent validation failed with errors {}", errors);
87+
return false;
88+
}
89+
return true;
90+
}
91+
92+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.cdevents.config;
2+
3+
import com.fasterxml.jackson.databind.DeserializationFeature;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.SerializationFeature;
6+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
7+
8+
public class CustomObjectMapper extends ObjectMapper {
9+
10+
/**
11+
* @return customized ObjectMapper
12+
*/
13+
public ObjectMapper customConfiguration() {
14+
return this
15+
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
16+
.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
17+
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
18+
.registerModule(new JavaTimeModule());
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright 2023-Present https://cdevents.dev/
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
*
15+
*/
16+
17+
package dev.cdevents.config;

0 commit comments

Comments
 (0)