Skip to content

Commit c273dfc

Browse files
authored
Continuous Deployment CDEvents to render as CloudEvent and schema validation (#44)
* Adding first continuous deployment event
1 parent 102f761 commit c273dfc

File tree

4 files changed

+302
-0
lines changed

4 files changed

+302
-0
lines changed

src/main/java/dev/cdevents/constants/CDEventConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public enum SubjectType {
2727
*/
2828
PIPELINERUN("pipelineRun"),
2929

30+
/**
31+
* Subject Type Environment.
32+
*/
33+
ENVIRONMENT("environment"),
34+
3035
/**
3136
* Subject type branch.
3237
*/
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package dev.cdevents.events;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import dev.cdevents.constants.CDEventConstants;
5+
import dev.cdevents.models.CDEvent;
6+
import dev.cdevents.models.EnvironmentCreatedSubject;
7+
8+
import java.net.URI;
9+
10+
public class EnvironmentCreatedCDEvent extends CDEvent {
11+
12+
private static final String CDEVENT_VERSION = "0.1.0";
13+
@JsonProperty(required = true)
14+
private EnvironmentCreatedSubject subject;
15+
16+
/**
17+
* Constructor to init CDEvent and set the Subject for {@link EnvironmentCreatedCDEvent}.
18+
*/
19+
public EnvironmentCreatedCDEvent() {
20+
initCDEvent(currentCDEventType());
21+
setSubject(new EnvironmentCreatedSubject(CDEventConstants.SubjectType.ENVIRONMENT));
22+
}
23+
24+
/**
25+
* @return subject
26+
*/
27+
public EnvironmentCreatedSubject getSubject() {
28+
return subject;
29+
}
30+
31+
/**
32+
* @param subject
33+
*/
34+
public void setSubject(EnvironmentCreatedSubject subject) {
35+
this.subject = subject;
36+
}
37+
38+
/**
39+
* @return the current CDEvent type
40+
*/
41+
@Override
42+
public String currentCDEventType() {
43+
return CDEventConstants.CDEventTypes.EnvironmentCreatedEvent.getEventType().concat(CDEVENT_VERSION);
44+
}
45+
46+
/**
47+
* @return the environment-created-event schema URL
48+
*/
49+
@Override
50+
public String schemaURL() {
51+
return String.format("https://cdevents.dev/%s/schema/environment-created-event", CDEventConstants.CDEVENTS_SPEC_VERSION);
52+
}
53+
54+
/**
55+
* @return the environment-created-event schema Json
56+
*/
57+
@Override
58+
public String eventSchema() {
59+
return "{\n" +
60+
" \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" +
61+
" \"$id\": \"https://cdevents.dev/0.1.2/schema/environment-created-event\",\n" +
62+
" \"properties\": {\n" +
63+
" \"context\": {\n" +
64+
" \"properties\": {\n" +
65+
" \"version\": {\n" +
66+
" \"type\": \"string\",\n" +
67+
" \"minLength\": 1\n" +
68+
" },\n" +
69+
" \"id\": {\n" +
70+
" \"type\": \"string\",\n" +
71+
" \"minLength\": 1\n" +
72+
" },\n" +
73+
" \"source\": {\n" +
74+
" \"type\": \"string\",\n" +
75+
" \"minLength\": 1\n" +
76+
" },\n" +
77+
" \"type\": {\n" +
78+
" \"type\": \"string\",\n" +
79+
" \"enum\": [\n" +
80+
" \"dev.cdevents.environment.created.0.1.0\"\n" +
81+
" ],\n" +
82+
" \"default\": \"dev.cdevents.environment.created.0.1.0\"\n" +
83+
" },\n" +
84+
" \"timestamp\": {\n" +
85+
" \"type\": \"string\",\n" +
86+
" \"format\": \"date-time\"\n" +
87+
" }\n" +
88+
" },\n" +
89+
" \"additionalProperties\": false,\n" +
90+
" \"type\": \"object\",\n" +
91+
" \"required\": [\n" +
92+
" \"version\",\n" +
93+
" \"id\",\n" +
94+
" \"source\",\n" +
95+
" \"type\",\n" +
96+
" \"timestamp\"\n" +
97+
" ]\n" +
98+
" },\n" +
99+
" \"subject\": {\n" +
100+
" \"properties\": {\n" +
101+
" \"id\": {\n" +
102+
" \"type\": \"string\",\n" +
103+
" \"minLength\": 1\n" +
104+
" },\n" +
105+
" \"source\": {\n" +
106+
" \"type\": \"string\"\n" +
107+
" },\n" +
108+
" \"type\": {\n" +
109+
" \"type\": \"string\",\n" +
110+
" \"minLength\": 1\n" +
111+
" },\n" +
112+
" \"content\": {\n" +
113+
" \"properties\": {\n" +
114+
" \"name\": {\n" +
115+
" \"type\": \"string\"\n" +
116+
" },\n" +
117+
" \"url\": {\n" +
118+
" \"type\": \"string\"\n" +
119+
" }\n" +
120+
" },\n" +
121+
" \"additionalProperties\": false,\n" +
122+
" \"type\": \"object\"\n" +
123+
" }\n" +
124+
" },\n" +
125+
" \"additionalProperties\": false,\n" +
126+
" \"type\": \"object\",\n" +
127+
" \"required\": [\n" +
128+
" \"id\",\n" +
129+
" \"type\",\n" +
130+
" \"content\"\n" +
131+
" ]\n" +
132+
" },\n" +
133+
" \"customData\": {\n" +
134+
" \"oneOf\": [\n" +
135+
" {\n" +
136+
" \"type\": \"object\"\n" +
137+
" },\n" +
138+
" {\n" +
139+
" \"type\": \"string\",\n" +
140+
" \"contentEncoding\": \"base64\"\n" +
141+
" }\n" +
142+
" ]\n" +
143+
" },\n" +
144+
" \"customDataContentType\": {\n" +
145+
" \"type\": \"string\"\n" +
146+
" }\n" +
147+
" },\n" +
148+
" \"additionalProperties\": false,\n" +
149+
" \"type\": \"object\",\n" +
150+
" \"required\": [\n" +
151+
" \"context\",\n" +
152+
" \"subject\"\n" +
153+
" ]\n" +
154+
"}";
155+
}
156+
157+
/**
158+
* @param subjectId
159+
* sets the subject Id
160+
*/
161+
public void setSubjectId(String subjectId) {
162+
getSubject().setId(subjectId);
163+
}
164+
165+
/**
166+
* @param subjectSource
167+
* sets the subject source
168+
*/
169+
public void setSubjectSource(URI subjectSource) {
170+
getSubject().setSource(subjectSource);
171+
}
172+
173+
/**
174+
* @param subjectName
175+
* sets the subject name
176+
*/
177+
public void setSubjectName(String subjectName) {
178+
getSubject().getContent().setName(subjectName);
179+
}
180+
181+
/**
182+
* @param subjectUrl
183+
* sets the subject URL
184+
*/
185+
public void setSubjectUrl(String subjectUrl) {
186+
getSubject().getContent().setUrl(subjectUrl);
187+
}
188+
189+
190+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dev.cdevents.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import dev.cdevents.constants.CDEventConstants;
5+
6+
public class EnvironmentCreatedSubject extends Subject {
7+
8+
@JsonProperty(required = true)
9+
private EnvironmentCreatedSubjectContent content;
10+
11+
12+
13+
/**
14+
* @return the EnvironmentCreated subject's content
15+
*/
16+
public EnvironmentCreatedSubjectContent getContent() {
17+
return content;
18+
}
19+
20+
/**
21+
* @param content
22+
*/
23+
public void setContent(EnvironmentCreatedSubjectContent content) {
24+
this.content = content;
25+
}
26+
27+
/**
28+
* Constructor to set the Subject Type.
29+
*
30+
* @param subjectType
31+
*/
32+
public EnvironmentCreatedSubject(CDEventConstants.SubjectType subjectType) {
33+
super(subjectType);
34+
setContent(new EnvironmentCreatedSubjectContent());
35+
}
36+
37+
38+
public class EnvironmentCreatedSubjectContent {
39+
@JsonProperty
40+
private String name;
41+
42+
@JsonProperty
43+
private String url;
44+
45+
/**
46+
* @return name
47+
*/
48+
public String getName() {
49+
return name;
50+
}
51+
52+
/**
53+
* @param name
54+
*/
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
/**
60+
* @return url
61+
*/
62+
public String getUrl() {
63+
return url;
64+
}
65+
66+
/**
67+
* @param url
68+
*/
69+
public void setUrl(String url) {
70+
this.url = url;
71+
}
72+
}
73+
}

src/test/java/dev/cdevents/CDEventsTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,38 @@ void testInvalidChangeUpdatedEventWithNoSubject() {
580580

581581
assertThat(exception.getMessage()).isEqualTo(expectedError);
582582
}
583+
584+
@Test
585+
void createEnvironmentCreatedEventAsCloudEvent(){
586+
EnvironmentCreatedCDEvent cdEvent = new EnvironmentCreatedCDEvent();
587+
cdEvent.setSource(URI.create("http://dev.cdevents"));
588+
589+
cdEvent.setSubjectId("/dev/pipeline/run/subject");
590+
cdEvent.setSubjectSource(URI.create("/dev/pipeline/run/subject"));
591+
cdEvent.setSubjectName("Name");
592+
cdEvent.setSubjectUrl("http://dev/pipeline/url");
593+
594+
String cdEventJson = CDEvents.cdEventAsJson(cdEvent);
595+
596+
CloudEvent ceEvent = CDEvents.cdEventAsCloudEvent(cdEvent);
597+
598+
String ceDataJson = new String(ceEvent.getData().toBytes(), StandardCharsets.UTF_8);
599+
600+
assertThat(ceEvent.getType()).isEqualTo(cdEvent.getContext().getType());
601+
assertThat(ceEvent.getSource()).isEqualTo(cdEvent.getContext().getSource());
602+
assertThat(ceDataJson).isEqualTo(cdEventJson);
603+
}
604+
605+
@Test
606+
void testInvalidEnvironmentCreatedEventWithNoSubject(){
607+
EnvironmentCreatedCDEvent cdEvent = new EnvironmentCreatedCDEvent();
608+
cdEvent.setSource(URI.create("http://dev.cdevents"));
609+
610+
Exception exception = assertThrows(CDEventsException.class, () -> {
611+
CDEvents.cdEventAsCloudEvent(cdEvent);
612+
});
613+
String expectedError = "CDEvent validation failed against schema URL - " + cdEvent.schemaURL();
614+
615+
assertThat(exception.getMessage()).isEqualTo(expectedError);
616+
}
583617
}

0 commit comments

Comments
 (0)