diff --git a/src/main/java/dev/cdevents/CDEvents.java b/src/main/java/dev/cdevents/CDEvents.java index f497adc..1d837f0 100644 --- a/src/main/java/dev/cdevents/CDEvents.java +++ b/src/main/java/dev/cdevents/CDEvents.java @@ -50,12 +50,12 @@ public static String cdEventAsJson(CDEvent 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()); } + String cdEventJson = cdEventAsJson(cdEvent); + log.info("CDEvent with type {} as json - {}", cdEvent.getContext().getType(), cdEventJson); CloudEvent ceToSend = new CloudEventBuilder() .withId(UUID.randomUUID().toString()) .withSource(cdEvent.getContext().getSource()) diff --git a/src/main/java/dev/cdevents/constants/CDEventConstants.java b/src/main/java/dev/cdevents/constants/CDEventConstants.java index 59e2a46..fa9962d 100644 --- a/src/main/java/dev/cdevents/constants/CDEventConstants.java +++ b/src/main/java/dev/cdevents/constants/CDEventConstants.java @@ -8,9 +8,14 @@ private CDEventConstants() { /** * CDEvents Version. */ - public static final String CDEVENTS_SPEC_VERSION = "0.1.0"; + public static final String CDEVENTS_SPEC_VERSION = "0.1.2"; public enum SubjectType { + + /** + * Subject Type taskRun. + */ + TASKRUN("taskRun"), /** * Subject Type pipelineRun. */ diff --git a/src/main/java/dev/cdevents/events/PipelineRunFinishedCDEvent.java b/src/main/java/dev/cdevents/events/PipelineRunFinishedCDEvent.java index d73dde8..03d68c0 100644 --- a/src/main/java/dev/cdevents/events/PipelineRunFinishedCDEvent.java +++ b/src/main/java/dev/cdevents/events/PipelineRunFinishedCDEvent.java @@ -58,16 +58,13 @@ public String schemaURL() { public String eventSchema() { return "{\n" + " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" + - " \"$id\": \"https://cdevents.dev/0.1.0/schema/pipeline-run-finished-event\",\n" + + " \"$id\": \"https://cdevents.dev/0.1.2/schema/pipeline-run-finished-event\",\n" + " \"properties\": {\n" + " \"context\": {\n" + " \"properties\": {\n" + " \"version\": {\n" + " \"type\": \"string\",\n" + - " \"enum\": [\n" + - " \"0.1.0\"\n" + - " ],\n" + - " \"default\": \"0.1.0\"\n" + + " \"minLength\": 1\n" + " },\n" + " \"id\": {\n" + " \"type\": \"string\",\n" + @@ -79,7 +76,10 @@ public String eventSchema() { " },\n" + " \"type\": {\n" + " \"type\": \"string\",\n" + - " \"minLength\": 1\n" + + " \"enum\": [\n" + + " \"dev.cdevents.pipelinerun.finished.0.1.0\"\n" + + " ],\n" + + " \"default\": \"dev.cdevents.pipelinerun.finished.0.1.0\"\n" + " },\n" + " \"timestamp\": {\n" + " \"type\": \"string\",\n" + diff --git a/src/main/java/dev/cdevents/events/PipelineRunQueuedCDEvent.java b/src/main/java/dev/cdevents/events/PipelineRunQueuedCDEvent.java new file mode 100644 index 0000000..dce77b6 --- /dev/null +++ b/src/main/java/dev/cdevents/events/PipelineRunQueuedCDEvent.java @@ -0,0 +1,188 @@ +package dev.cdevents.events; + +import com.fasterxml.jackson.annotation.JsonProperty; +import dev.cdevents.constants.CDEventConstants; +import dev.cdevents.models.CDEvent; +import dev.cdevents.models.PipelineRunQueuedSubject; + +import java.net.URI; + +public class PipelineRunQueuedCDEvent extends CDEvent { + + private static final String CDEVENT_VERSION = "0.1.0"; + @JsonProperty(required = true) + private PipelineRunQueuedSubject subject; + + /** + * Constructor to init CDEvent and set the Subject for {@link PipelineRunQueuedCDEvent}. + */ + public PipelineRunQueuedCDEvent() { + initCDEvent(currentCDEventType()); + setSubject(new PipelineRunQueuedSubject(CDEventConstants.SubjectType.PIPELINERUN)); + } + + /** + * @return subject + */ + public PipelineRunQueuedSubject getSubject() { + return subject; + } + + /** + * @param subject + */ + public void setSubject(PipelineRunQueuedSubject subject) { + this.subject = subject; + } + + /** + * @return the PipelineRunQueuedEvent type + */ + @Override + public String currentCDEventType() { + return CDEventConstants.CDEventTypes.PipelineRunQueuedEvent.getEventType().concat(CDEVENT_VERSION); + } + + /** + * @return the pipeline-run-queued-event schema URL + */ + @Override + public String schemaURL() { + return String.format("https://cdevents.dev/%s/schema/pipeline-run-queued-event", CDEventConstants.CDEVENTS_SPEC_VERSION); + } + + /** + * @return the pipeline-run-queued-event schema Json + */ + @Override + public String eventSchema() { + return "{\n" + + " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" + + " \"$id\": \"https://cdevents.dev/0.1.2/schema/pipeline-run-queued-event\",\n" + + " \"properties\": {\n" + + " \"context\": {\n" + + " \"properties\": {\n" + + " \"version\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"string\",\n" + + " \"enum\": [\n" + + " \"dev.cdevents.pipelinerun.queued.0.1.0\"\n" + + " ],\n" + + " \"default\": \"dev.cdevents.pipelinerun.queued.0.1.0\"\n" + + " },\n" + + " \"timestamp\": {\n" + + " \"type\": \"string\",\n" + + " \"format\": \"date-time\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"version\",\n" + + " \"id\",\n" + + " \"source\",\n" + + " \"type\",\n" + + " \"timestamp\"\n" + + " ]\n" + + " },\n" + + " \"subject\": {\n" + + " \"properties\": {\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"content\": {\n" + + " \"properties\": {\n" + + " \"pipelineName\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"url\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"id\",\n" + + " \"type\",\n" + + " \"content\"\n" + + " ]\n" + + " },\n" + + " \"customData\": {\n" + + " \"oneOf\": [\n" + + " {\n" + + " \"type\": \"object\"\n" + + " },\n" + + " {\n" + + " \"type\": \"string\",\n" + + " \"contentEncoding\": \"base64\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"customDataContentType\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"context\",\n" + + " \"subject\"\n" + + " ]\n" + + "}"; + } + + /** + * @param subjectId + * sets the subject Id + */ + public void setSubjectId(String subjectId) { + getSubject().setId(subjectId); + } + + /** + * @param subjectSource + * sets the pipeline source + */ + public void setSubjectSource(URI subjectSource) { + getSubject().setSource(subjectSource); + } + + /** + * @param pipelineName + * sets the pipeline name + */ + public void setSubjectPipelineName(String pipelineName) { + getSubject().getContent().setPipelineName(pipelineName); + } + + /** + * @param subjectUrl + * sets the pipeline URL + */ + public void setSubjectUrl(URI subjectUrl) { + getSubject().getContent().setUrl(subjectUrl); + } +} diff --git a/src/main/java/dev/cdevents/events/PipelineRunStartedCDEvent.java b/src/main/java/dev/cdevents/events/PipelineRunStartedCDEvent.java new file mode 100644 index 0000000..4e71966 --- /dev/null +++ b/src/main/java/dev/cdevents/events/PipelineRunStartedCDEvent.java @@ -0,0 +1,192 @@ +package dev.cdevents.events; + +import com.fasterxml.jackson.annotation.JsonProperty; +import dev.cdevents.constants.CDEventConstants; +import dev.cdevents.models.CDEvent; +import dev.cdevents.models.PipelineRunStartedSubject; + +import java.net.URI; + +public class PipelineRunStartedCDEvent extends CDEvent { + + private static final String CDEVENT_VERSION = "0.1.0"; + @JsonProperty(required = true) + private PipelineRunStartedSubject subject; + + /** + * Constructor to init CDEvent and set the Subject for {@link PipelineRunStartedCDEvent}. + */ + public PipelineRunStartedCDEvent() { + initCDEvent(currentCDEventType()); + setSubject(new PipelineRunStartedSubject(CDEventConstants.SubjectType.PIPELINERUN)); + } + + /** + * @return subject + */ + public PipelineRunStartedSubject getSubject() { + return subject; + } + + /** + * @param subject + */ + public void setSubject(PipelineRunStartedSubject subject) { + this.subject = subject; + } + + /** + * @return the PipelineRunStartedEvent type + */ + @Override + public String currentCDEventType() { + return CDEventConstants.CDEventTypes.PipelineRunStartedEvent.getEventType().concat(CDEVENT_VERSION); + } + + /** + * @return the pipeline-run-started-event schema URL + */ + @Override + public String schemaURL() { + return String.format("https://cdevents.dev/%s/schema/pipeline-run-started-event", CDEventConstants.CDEVENTS_SPEC_VERSION); + } + + /** + * @return the pipeline-run-started-event schema Json + */ + @Override + public String eventSchema() { + return "{\n" + + " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" + + " \"$id\": \"https://cdevents.dev/0.1.2/schema/pipeline-run-started-event\",\n" + + " \"properties\": {\n" + + " \"context\": {\n" + + " \"properties\": {\n" + + " \"version\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"string\",\n" + + " \"enum\": [\n" + + " \"dev.cdevents.pipelinerun.started.0.1.0\"\n" + + " ],\n" + + " \"default\": \"dev.cdevents.pipelinerun.started.0.1.0\"\n" + + " },\n" + + " \"timestamp\": {\n" + + " \"type\": \"string\",\n" + + " \"format\": \"date-time\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"version\",\n" + + " \"id\",\n" + + " \"source\",\n" + + " \"type\",\n" + + " \"timestamp\"\n" + + " ]\n" + + " },\n" + + " \"subject\": {\n" + + " \"properties\": {\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"content\": {\n" + + " \"properties\": {\n" + + " \"pipelineName\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"url\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"pipelineName\",\n" + + " \"url\"\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"id\",\n" + + " \"type\",\n" + + " \"content\"\n" + + " ]\n" + + " },\n" + + " \"customData\": {\n" + + " \"oneOf\": [\n" + + " {\n" + + " \"type\": \"object\"\n" + + " },\n" + + " {\n" + + " \"type\": \"string\",\n" + + " \"contentEncoding\": \"base64\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"customDataContentType\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"context\",\n" + + " \"subject\"\n" + + " ]\n" + + "}"; + } + + /** + * @param subjectId + * sets the subject Id + */ + public void setSubjectId(String subjectId) { + getSubject().setId(subjectId); + } + + /** + * @param subjectSource + * sets the pipeline source + */ + public void setSubjectSource(URI subjectSource) { + getSubject().setSource(subjectSource); + } + + /** + * @param pipelineName + * sets the pipeline name + */ + public void setSubjectPipelineName(String pipelineName) { + getSubject().getContent().setPipelineName(pipelineName); + } + + /** + * @param subjectUrl + * sets the pipeline URL + */ + public void setSubjectUrl(URI subjectUrl) { + getSubject().getContent().setUrl(subjectUrl); + } +} diff --git a/src/main/java/dev/cdevents/events/TaskRunFinishedCDEvent.java b/src/main/java/dev/cdevents/events/TaskRunFinishedCDEvent.java new file mode 100644 index 0000000..38cf58c --- /dev/null +++ b/src/main/java/dev/cdevents/events/TaskRunFinishedCDEvent.java @@ -0,0 +1,243 @@ +package dev.cdevents.events; + +import com.fasterxml.jackson.annotation.JsonProperty; +import dev.cdevents.constants.CDEventConstants; +import dev.cdevents.models.CDEvent; +import dev.cdevents.models.TaskRunFinishedSubject; + +import java.net.URI; + +public class TaskRunFinishedCDEvent extends CDEvent { + + private static final String CDEVENT_VERSION = "0.1.0"; + @JsonProperty(required = true) + private TaskRunFinishedSubject subject; + + /** + * Constructor to init CDEvent and set the Subject for {@link TaskRunFinishedCDEvent}. + */ + public TaskRunFinishedCDEvent() { + initCDEvent(currentCDEventType()); + setSubject(new TaskRunFinishedSubject(CDEventConstants.SubjectType.TASKRUN)); + } + + /** + * @return subject + */ + public TaskRunFinishedSubject getSubject() { + return subject; + } + + /** + * @param subject + */ + public void setSubject(TaskRunFinishedSubject subject) { + this.subject = subject; + } + + /** + * @return the current CDEvent type + */ + @Override + public String currentCDEventType() { + return CDEventConstants.CDEventTypes.TaskRunFinishedEvent.getEventType().concat(CDEVENT_VERSION); + } + + /** + * @return the task-run-finished-event schema URL + */ + @Override + public String schemaURL() { + return String.format("https://cdevents.dev/%s/schema/task-run-finished-event", CDEventConstants.CDEVENTS_SPEC_VERSION); + } + + /** + * @return the task-run-finished-event schema Json + */ + @Override + public String eventSchema() { + return "{\n" + + " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" + + " \"$id\": \"https://cdevents.dev/0.1.2/schema/task-run-finished-event\",\n" + + " \"properties\": {\n" + + " \"context\": {\n" + + " \"properties\": {\n" + + " \"version\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"string\",\n" + + " \"enum\": [\n" + + " \"dev.cdevents.taskrun.finished.0.1.0\"\n" + + " ],\n" + + " \"default\": \"dev.cdevents.taskrun.finished.0.1.0\"\n" + + " },\n" + + " \"timestamp\": {\n" + + " \"type\": \"string\",\n" + + " \"format\": \"date-time\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"version\",\n" + + " \"id\",\n" + + " \"source\",\n" + + " \"type\",\n" + + " \"timestamp\"\n" + + " ]\n" + + " },\n" + + " \"subject\": {\n" + + " \"properties\": {\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"content\": {\n" + + " \"properties\": {\n" + + " \"taskName\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"url\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"pipelineRun\": {\n" + + " \"properties\": {\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"id\"\n" + + " ]\n" + + " },\n" + + " \"outcome\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"errors\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"id\",\n" + + " \"type\",\n" + + " \"content\"\n" + + " ]\n" + + " },\n" + + " \"customData\": {\n" + + " \"oneOf\": [\n" + + " {\n" + + " \"type\": \"object\"\n" + + " },\n" + + " {\n" + + " \"type\": \"string\",\n" + + " \"contentEncoding\": \"base64\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"customDataContentType\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"context\",\n" + + " \"subject\"\n" + + " ]\n" + + "}"; + } + + /** + * @param subjectId + * sets the taskRun subject Id + */ + public void setSubjectId(String subjectId) { + getSubject().setId(subjectId); + } + + /** + * @param subjectSource + * sets the taskRun subject source + */ + public void setSubjectSource(URI subjectSource) { + getSubject().setSource(subjectSource); + } + + /** + * @param taskName + * sets the taskName + */ + public void setSubjectTaskName(String taskName) { + getSubject().getContent().setTaskName(taskName); + } + + /** + * @param subjectUrl + * sets the taskRun URL + */ + public void setSubjectUrl(URI subjectUrl) { + getSubject().getContent().setUrl(subjectUrl); + } + + /** + * @param subjectOutcome + * sets the {@link TaskRunFinishedCDEvent} outcome + */ + public void setSubjectOutcome(CDEventConstants.Outcome subjectOutcome) { + getSubject().getContent().setOutcome(subjectOutcome); + } + + /** + * @param subjectErrors + * sets the {@link TaskRunFinishedCDEvent} errors + */ + public void setSubjectErrors(String subjectErrors) { + getSubject().getContent().setErrors(subjectErrors); + } + + /** + * @param pipelineRunId + * sets The pipelineRunId that this taskRun belongs to + */ + public void setSubjectPipelineRunId(String pipelineRunId) { + getSubject().getContent().getPipelineRun().setId(pipelineRunId); + + } + + /** + * @param pipelineRunSource + * sets The pipelineRunSource that this taskRun belongs to + */ + public void setSubjectPipelineRunSource(URI pipelineRunSource) { + getSubject().getContent().getPipelineRun().setSource(pipelineRunSource); + } +} diff --git a/src/main/java/dev/cdevents/events/TaskRunStartedCDEvent.java b/src/main/java/dev/cdevents/events/TaskRunStartedCDEvent.java new file mode 100644 index 0000000..b32f7a4 --- /dev/null +++ b/src/main/java/dev/cdevents/events/TaskRunStartedCDEvent.java @@ -0,0 +1,221 @@ +package dev.cdevents.events; + +import com.fasterxml.jackson.annotation.JsonProperty; +import dev.cdevents.constants.CDEventConstants; +import dev.cdevents.models.CDEvent; +import dev.cdevents.models.TaskRunStartedSubject; + +import java.net.URI; + +public class TaskRunStartedCDEvent extends CDEvent { + + private static final String CDEVENT_VERSION = "0.1.0"; + @JsonProperty(required = true) + private TaskRunStartedSubject subject; + + /** + * Constructor to init CDEvent and set the Subject for {@link TaskRunStartedCDEvent}. + */ + public TaskRunStartedCDEvent() { + initCDEvent(currentCDEventType()); + setSubject(new TaskRunStartedSubject(CDEventConstants.SubjectType.TASKRUN)); + } + + /** + * @return subject + */ + public TaskRunStartedSubject getSubject() { + return subject; + } + + /** + * @param subject + */ + public void setSubject(TaskRunStartedSubject subject) { + this.subject = subject; + } + + /** + * @return the TaskRunStartedEvent type + */ + @Override + public String currentCDEventType() { + return CDEventConstants.CDEventTypes.TaskRunStartedEvent.getEventType().concat(CDEVENT_VERSION); + } + + /** + * @return the task-run-started-event schema URL + */ + @Override + public String schemaURL() { + return String.format("https://cdevents.dev/%s/schema/task-run-started-event", CDEventConstants.CDEVENTS_SPEC_VERSION); + } + + /** + * @return the task-run-started-event schema Json + */ + @Override + public String eventSchema() { + return "{\n" + + " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n" + + " \"$id\": \"https://cdevents.dev/0.1.2/schema/task-run-started-event\",\n" + + " \"properties\": {\n" + + " \"context\": {\n" + + " \"properties\": {\n" + + " \"version\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"string\",\n" + + " \"enum\": [\n" + + " \"dev.cdevents.taskrun.started.0.1.0\"\n" + + " ],\n" + + " \"default\": \"dev.cdevents.taskrun.started.0.1.0\"\n" + + " },\n" + + " \"timestamp\": {\n" + + " \"type\": \"string\",\n" + + " \"format\": \"date-time\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"version\",\n" + + " \"id\",\n" + + " \"source\",\n" + + " \"type\",\n" + + " \"timestamp\"\n" + + " ]\n" + + " },\n" + + " \"subject\": {\n" + + " \"properties\": {\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"type\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"content\": {\n" + + " \"properties\": {\n" + + " \"taskName\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"url\": {\n" + + " \"type\": \"string\"\n" + + " },\n" + + " \"pipelineRun\": {\n" + + " \"properties\": {\n" + + " \"id\": {\n" + + " \"type\": \"string\",\n" + + " \"minLength\": 1\n" + + " },\n" + + " \"source\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"id\"\n" + + " ]\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"id\",\n" + + " \"type\",\n" + + " \"content\"\n" + + " ]\n" + + " },\n" + + " \"customData\": {\n" + + " \"oneOf\": [\n" + + " {\n" + + " \"type\": \"object\"\n" + + " },\n" + + " {\n" + + " \"type\": \"string\",\n" + + " \"contentEncoding\": \"base64\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"customDataContentType\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " },\n" + + " \"additionalProperties\": false,\n" + + " \"type\": \"object\",\n" + + " \"required\": [\n" + + " \"context\",\n" + + " \"subject\"\n" + + " ]\n" + + "}"; + } + + /** + * @param subjectId + * sets the subject Id + */ + public void setSubjectId(String subjectId) { + getSubject().setId(subjectId); + } + + /** + * @param subjectSource + * sets the taskRun source + */ + public void setSubjectSource(URI subjectSource) { + getSubject().setSource(subjectSource); + } + + /** + * @param taskName + * sets the taskName + */ + public void setSubjectTaskName(String taskName) { + getSubject().getContent().setTaskName(taskName); + } + + /** + * @param subjectUrl + * sets the taskRun URL + */ + public void setSubjectUrl(URI subjectUrl) { + getSubject().getContent().setUrl(subjectUrl); + } + + /** + * @param pipelineRunId + * sets The pipelineRunId that this taskRun belongs to + */ + public void setSubjectPipelineRunId(String pipelineRunId) { + getSubject().getContent().getPipelineRun().setId(pipelineRunId); + + } + + /** + * @param pipelineRunSource + * sets The pipelineRunSource that this taskRun belongs to + */ + public void setSubjectPipelineRunSource(URI pipelineRunSource) { + getSubject().getContent().getPipelineRun().setSource(pipelineRunSource); + } +} diff --git a/src/main/java/dev/cdevents/models/PipelineRunQueuedSubject.java b/src/main/java/dev/cdevents/models/PipelineRunQueuedSubject.java new file mode 100644 index 0000000..ca48a14 --- /dev/null +++ b/src/main/java/dev/cdevents/models/PipelineRunQueuedSubject.java @@ -0,0 +1,75 @@ +package dev.cdevents.models; + +import com.fasterxml.jackson.annotation.JsonProperty; +import dev.cdevents.constants.CDEventConstants; + +import java.net.URI; + +public class PipelineRunQueuedSubject extends Subject { + + @JsonProperty(required = true) + private PipelineRunQueuedSubjectContent content; + + + /** + * @return the PipelineRunQueued subject's Content + */ + public PipelineRunQueuedSubjectContent getContent() { + return content; + } + + /** + * @param content + */ + public void setContent(PipelineRunQueuedSubjectContent content) { + this.content = content; + } + + /** + * @param subjectType + */ + public PipelineRunQueuedSubject(CDEventConstants.SubjectType subjectType) { + super(subjectType); + setContent(new PipelineRunQueuedSubjectContent()); + + } + + public class PipelineRunQueuedSubjectContent { + + @JsonProperty + private String pipelineName; + + @JsonProperty + private URI url; + + + /** + * @return pipelineName + */ + public String getPipelineName() { + return pipelineName; + } + + /** + * @param pipelineName + */ + public void setPipelineName(String pipelineName) { + this.pipelineName = pipelineName; + } + + /** + * @return URL + */ + public URI getUrl() { + return url; + } + + /** + * @param url + */ + public void setUrl(URI url) { + this.url = url; + } + + } +} diff --git a/src/main/java/dev/cdevents/models/PipelineRunStartedSubject.java b/src/main/java/dev/cdevents/models/PipelineRunStartedSubject.java new file mode 100644 index 0000000..a9d76af --- /dev/null +++ b/src/main/java/dev/cdevents/models/PipelineRunStartedSubject.java @@ -0,0 +1,75 @@ +package dev.cdevents.models; + +import com.fasterxml.jackson.annotation.JsonProperty; +import dev.cdevents.constants.CDEventConstants; + +import java.net.URI; + +public class PipelineRunStartedSubject extends Subject { + + @JsonProperty(required = true) + private PipelineRunStartedSubjectContent content; + + + /** + * @return the PipelineRunStarted subject's Content + */ + public PipelineRunStartedSubjectContent getContent() { + return content; + } + + /** + * @param content + */ + public void setContent(PipelineRunStartedSubjectContent content) { + this.content = content; + } + + /** + * @param subjectType + */ + public PipelineRunStartedSubject(CDEventConstants.SubjectType subjectType) { + super(subjectType); + setContent(new PipelineRunStartedSubjectContent()); + + } + + public class PipelineRunStartedSubjectContent { + + @JsonProperty + private String pipelineName; + + @JsonProperty + private URI url; + + + /** + * @return pipelineName + */ + public String getPipelineName() { + return pipelineName; + } + + /** + * @param pipelineName + */ + public void setPipelineName(String pipelineName) { + this.pipelineName = pipelineName; + } + + /** + * @return URL + */ + public URI getUrl() { + return url; + } + + /** + * @param url + */ + public void setUrl(URI url) { + this.url = url; + } + + } +} diff --git a/src/main/java/dev/cdevents/models/TaskRunFinishedSubject.java b/src/main/java/dev/cdevents/models/TaskRunFinishedSubject.java new file mode 100644 index 0000000..f584586 --- /dev/null +++ b/src/main/java/dev/cdevents/models/TaskRunFinishedSubject.java @@ -0,0 +1,160 @@ +package dev.cdevents.models; + +import com.fasterxml.jackson.annotation.JsonProperty; +import dev.cdevents.constants.CDEventConstants; + +import java.net.URI; + +public class TaskRunFinishedSubject extends Subject { + + @JsonProperty(required = true) + private TaskRunFinishedSubjectContent content; + + + /** + * @return the TaskRunFinished subject's Content + */ + public TaskRunFinishedSubjectContent getContent() { + return content; + } + + /** + * @param content + */ + public void setContent(TaskRunFinishedSubjectContent content) { + this.content = content; + } + + /** + * @param subjectType + */ + public TaskRunFinishedSubject(CDEventConstants.SubjectType subjectType) { + super(subjectType); + setContent(new TaskRunFinishedSubjectContent()); + + } + + public class TaskRunFinishedSubjectContent { + + @JsonProperty + private String taskName; + + @JsonProperty + private URI url; + + @JsonProperty + private CDEventConstants.Outcome outcome; + + @JsonProperty + private String errors; + + @JsonProperty + private PipelineRun pipelineRun = new PipelineRun(); + + /** + * @return taskName + */ + public String getTaskName() { + return taskName; + } + + /** + * @param taskName + */ + public void setTaskName(String taskName) { + this.taskName = taskName; + } + + /** + * @return URL + */ + public URI getUrl() { + return url; + } + + /** + * @param url + */ + public void setUrl(URI url) { + this.url = url; + } + + /** + * @return outcome + */ + public CDEventConstants.Outcome getOutcome() { + return outcome; + } + + /** + * @param outcome + */ + public void setOutcome(CDEventConstants.Outcome outcome) { + this.outcome = outcome; + } + + + /** + * @return errors + */ + public String getErrors() { + return errors; + } + + + /** + * @param errors + */ + public void setErrors(String errors) { + this.errors = errors; + } + + /** + * @return pipelineRun + */ + public PipelineRun getPipelineRun() { + return pipelineRun; + } + + /** + * @param pipelineRun + */ + public void setPipelineRun(PipelineRun pipelineRun) { + this.pipelineRun = pipelineRun; + } + + public class PipelineRun { + private String id; + + private URI source; + + /** + * @return id + */ + public String getId() { + return id; + } + + /** + * @param id + */ + public void setId(String id) { + this.id = id; + } + + /** + * @return source + */ + public URI getSource() { + return source; + } + + /** + * @param source + */ + public void setSource(URI source) { + this.source = source; + } + } + } +} diff --git a/src/main/java/dev/cdevents/models/TaskRunStartedSubject.java b/src/main/java/dev/cdevents/models/TaskRunStartedSubject.java new file mode 100644 index 0000000..a820c4a --- /dev/null +++ b/src/main/java/dev/cdevents/models/TaskRunStartedSubject.java @@ -0,0 +1,125 @@ +package dev.cdevents.models; + +import com.fasterxml.jackson.annotation.JsonProperty; +import dev.cdevents.constants.CDEventConstants; + +import java.net.URI; + +public class TaskRunStartedSubject extends Subject { + + @JsonProperty(required = true) + private TaskRunStartedSubjectContent content; + + + /** + * @return the TaskRunStarted subject's Content + */ + public TaskRunStartedSubjectContent getContent() { + return content; + } + + /** + * @param content + */ + public void setContent(TaskRunStartedSubjectContent content) { + this.content = content; + } + + /** + * @param subjectType + */ + public TaskRunStartedSubject(CDEventConstants.SubjectType subjectType) { + super(subjectType); + setContent(new TaskRunStartedSubjectContent()); + + } + + public class TaskRunStartedSubjectContent { + + @JsonProperty + private String taskName; + + @JsonProperty + private URI url; + + @JsonProperty + private PipelineRun pipelineRun = new PipelineRun(); + + + /** + * @return taskName + */ + public String getTaskName() { + return taskName; + } + + /** + * @param taskName + */ + public void setTaskName(String taskName) { + this.taskName = taskName; + } + + /** + * @return URL + */ + public URI getUrl() { + return url; + } + + /** + * @param url + */ + public void setUrl(URI url) { + this.url = url; + } + + /** + * @return pipelineRun + */ + public PipelineRun getPipelineRun() { + return pipelineRun; + } + + /** + * @param pipelineRun + */ + public void setPipelineRun(PipelineRun pipelineRun) { + this.pipelineRun = pipelineRun; + } + + public class PipelineRun { + private String id; + + private URI source; + + /** + * @return id + */ + public String getId() { + return id; + } + + /** + * @param id + */ + public void setId(String id) { + this.id = id; + } + + /** + * @return source + */ + public URI getSource() { + return source; + } + + /** + * @param source + */ + public void setSource(URI source) { + this.source = source; + } + } + } +} diff --git a/src/test/java/dev/cdevents/CDEventsTest.java b/src/test/java/dev/cdevents/CDEventsTest.java index a9e53e5..9d09f04 100644 --- a/src/test/java/dev/cdevents/CDEventsTest.java +++ b/src/test/java/dev/cdevents/CDEventsTest.java @@ -1,7 +1,8 @@ package dev.cdevents; +import com.fasterxml.jackson.core.JsonProcessingException; import dev.cdevents.constants.CDEventConstants; -import dev.cdevents.events.PipelineRunFinishedCDEvent; +import dev.cdevents.events.*; import dev.cdevents.exception.CDEventsException; import io.cloudevents.CloudEvent; import org.junit.jupiter.api.Test; @@ -11,7 +12,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; public class CDEventsTest { @@ -49,7 +49,180 @@ void testInvalidPipelineRunFinishedEventWithNoSubject() { CDEvents.cdEventAsCloudEvent(cdEvent); }); String expectedError = "CDEvent validation failed against schema URL - " + cdEvent.schemaURL(); - + assertThat(exception.getMessage()).isEqualTo(expectedError); + } + + @Test + void createPipelineRunQueuedEventAsCloudEvent() { + + PipelineRunQueuedCDEvent cdEvent = new PipelineRunQueuedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + cdEvent.setSubjectId("/dev/pipeline/run/subject"); + cdEvent.setSubjectSource(URI.create("/dev/pipeline/run/subject")); + cdEvent.setSubjectPipelineName("test-pipeline-queued"); + cdEvent.setSubjectUrl(URI.create("http://dev/pipeline/url")); + + String cdEventJson = CDEvents.cdEventAsJson(cdEvent); + + CloudEvent ceEvent = CDEvents.cdEventAsCloudEvent(cdEvent); + + String ceDataJson = new String(ceEvent.getData().toBytes(), StandardCharsets.UTF_8); + + assertThat(ceEvent.getType()).isEqualTo(cdEvent.getContext().getType()); + assertThat(ceEvent.getSource()).isEqualTo(cdEvent.getContext().getSource()); + assertThat(ceDataJson).isEqualTo(cdEventJson); + + } + + @Test + void testInvalidPipelineRunQueuedEventWithNoSubject() { + PipelineRunQueuedCDEvent cdEvent = new PipelineRunQueuedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + Exception exception = assertThrows(CDEventsException.class, () -> { + CDEvents.cdEventAsCloudEvent(cdEvent); + }); + String expectedError = "CDEvent validation failed against schema URL - " + cdEvent.schemaURL(); + + assertThat(exception.getMessage()).isEqualTo(expectedError); + } + + @Test + void testPipelineRunQueuedEventWithCustomData() throws JsonProcessingException { + PipelineRunQueuedCDEvent cdEvent = new PipelineRunQueuedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + cdEvent.setSubjectId("/dev/pipeline/run/subject"); + cdEvent.setSubjectSource(URI.create("/dev/pipeline/run/subject")); + cdEvent.setSubjectPipelineName("test-pipeline-queued"); + cdEvent.setSubjectUrl(URI.create("http://dev/pipeline/url")); + + String customDataJson = "{\"key1\": \"value1\",\"key2\" : {\"test\": \"customData\" }}"; + cdEvent.setCustomData(customDataJson); + + String cdEventJson = CDEvents.cdEventAsJson(cdEvent); + + CloudEvent ceEvent = CDEvents.cdEventAsCloudEvent(cdEvent); + + String ceDataJson = new String(ceEvent.getData().toBytes(), StandardCharsets.UTF_8); + + assertThat(ceEvent.getType()).isEqualTo(cdEvent.getContext().getType()); + assertThat(ceEvent.getSource()).isEqualTo(cdEvent.getContext().getSource()); + assertThat(ceDataJson).isEqualTo(cdEventJson); + assertThat(ceDataJson).isEqualTo(cdEventJson); + } + + @Test + void createPipelineRunStartedEventAsCloudEvent() { + + PipelineRunStartedCDEvent cdEvent = new PipelineRunStartedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + cdEvent.setSubjectId("/dev/pipeline/run/subject"); + cdEvent.setSubjectSource(URI.create("/dev/pipeline/run/subject")); + cdEvent.setSubjectPipelineName("test-pipeline-started"); + cdEvent.setSubjectUrl(URI.create("http://dev/pipeline/url")); + + String cdEventJson = CDEvents.cdEventAsJson(cdEvent); + + CloudEvent ceEvent = CDEvents.cdEventAsCloudEvent(cdEvent); + + String ceDataJson = new String(ceEvent.getData().toBytes(), StandardCharsets.UTF_8); + + assertThat(ceEvent.getType()).isEqualTo(cdEvent.getContext().getType()); + assertThat(ceEvent.getSource()).isEqualTo(cdEvent.getContext().getSource()); + assertThat(ceDataJson).isEqualTo(cdEventJson); + + } + + @Test + void testInvalidPipelineRunStartedEventWithNoSubject() { + PipelineRunStartedCDEvent cdEvent = new PipelineRunStartedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + Exception exception = assertThrows(CDEventsException.class, () -> { + CDEvents.cdEventAsCloudEvent(cdEvent); + }); + String expectedError = "CDEvent validation failed against schema URL - " + cdEvent.schemaURL(); + + assertThat(exception.getMessage()).isEqualTo(expectedError); + } + + + @Test + void createTaskRunStartedEventAsCloudEvent() { + + TaskRunStartedCDEvent cdEvent = new TaskRunStartedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + cdEvent.setSubjectId("/dev/task/run/subject"); + cdEvent.setSubjectSource(URI.create("/dev/task/run/subject")); + cdEvent.setSubjectTaskName("test-task-started"); + cdEvent.setSubjectUrl(URI.create("http://dev/task/url")); + cdEvent.setSubjectPipelineRunId("/dev/pipeline/run/subject"); + cdEvent.setSubjectPipelineRunSource(URI.create("http://dev/pipeline/run/subject")); + + String cdEventJson = CDEvents.cdEventAsJson(cdEvent); + + CloudEvent ceEvent = CDEvents.cdEventAsCloudEvent(cdEvent); + + String ceDataJson = new String(ceEvent.getData().toBytes(), StandardCharsets.UTF_8); + + assertThat(ceEvent.getType()).isEqualTo(cdEvent.getContext().getType()); + assertThat(ceEvent.getSource()).isEqualTo(cdEvent.getContext().getSource()); + assertThat(ceDataJson).isEqualTo(cdEventJson); + } + + @Test + void testInvalidTaskRunStartedEventWithNoSubject() { + TaskRunStartedCDEvent cdEvent = new TaskRunStartedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + Exception exception = assertThrows(CDEventsException.class, () -> { + CDEvents.cdEventAsCloudEvent(cdEvent); + }); + String expectedError = "CDEvent validation failed against schema URL - " + cdEvent.schemaURL(); + + assertThat(exception.getMessage()).isEqualTo(expectedError); + } + + @Test + void createTaskRunFinishedEventAsCloudEvent() { + + TaskRunFinishedCDEvent cdEvent = new TaskRunFinishedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + cdEvent.setSubjectId("/dev/task/run/subject"); + cdEvent.setSubjectSource(URI.create("/dev/task/run/subject")); + cdEvent.setSubjectTaskName("test-task-finished"); + cdEvent.setSubjectUrl(URI.create("http://dev/task/url")); + cdEvent.setSubjectErrors("errors to place"); + cdEvent.setSubjectOutcome(CDEventConstants.Outcome.SUCCESS); + cdEvent.setSubjectPipelineRunId("/dev/pipeline/run/subject"); + cdEvent.setSubjectPipelineRunSource(URI.create("http://dev/pipeline/run/subject")); + + String cdEventJson = CDEvents.cdEventAsJson(cdEvent); + + CloudEvent ceEvent = CDEvents.cdEventAsCloudEvent(cdEvent); + + String ceDataJson = new String(ceEvent.getData().toBytes(), StandardCharsets.UTF_8); + + assertThat(ceEvent.getType()).isEqualTo(cdEvent.getContext().getType()); + assertThat(ceEvent.getSource()).isEqualTo(cdEvent.getContext().getSource()); + assertThat(ceDataJson).isEqualTo(cdEventJson); + } + + @Test + void testInvalidTaskRunFinishedEventWithNoSubject() { + TaskRunFinishedCDEvent cdEvent = new TaskRunFinishedCDEvent(); + cdEvent.setSource(URI.create("http://dev.cdevents")); + + Exception exception = assertThrows(CDEventsException.class, () -> { + CDEvents.cdEventAsCloudEvent(cdEvent); + }); + String expectedError = "CDEvent validation failed against schema URL - " + cdEvent.schemaURL(); + assertThat(exception.getMessage()).isEqualTo(expectedError); } }