Skip to content

Commit

Permalink
Add artifact created event based on the outbox pattern (#5274)
Browse files Browse the repository at this point in the history
* Add artifact created event based on the outbox pattern

* Fire artifact create event inside the db transaction

* Add artifact metadata update event and artifact delete event

* Initial implementation for kafkasql events

* Improve test assertions

* Add group events

* Only create the event in the table if the database supports events

* Implement artifact version events

* Add rules events

* Update events test with rules tests for modifications and deletions

* Add documentation to properties

* Remove property that enables events

* Ensure that any event is sent only once
  • Loading branch information
carlesarnal authored Oct 10, 2024
1 parent 26d9fa1 commit 5d756a9
Show file tree
Hide file tree
Showing 54 changed files with 1,920 additions and 170 deletions.
41 changes: 41 additions & 0 deletions app/src/main/java/io/apicurio/registry/events/ArtifactCreated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.ArtifactMetaDataDto;
import io.apicurio.registry.storage.dto.OutboxEvent;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.ARTIFACT_CREATED;

public class ArtifactCreated extends OutboxEvent {
private final JSONObject eventPayload;

private ArtifactCreated(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static ArtifactCreated of(ArtifactMetaDataDto artifactMetaDataDto) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("groupId", artifactMetaDataDto.getGroupId())
.put("artifactId", artifactMetaDataDto.getArtifactId())
.put("name", artifactMetaDataDto.getName())
.put("description", artifactMetaDataDto.getDescription())
.put("eventType", ARTIFACT_CREATED.name());

return new ArtifactCreated(id,
artifactMetaDataDto.getGroupId() + "-" + artifactMetaDataDto.getArtifactId(), jsonObject);
}

@Override
public String getType() {
return ARTIFACT_CREATED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/io/apicurio/registry/events/ArtifactDeleted.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.OutboxEvent;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.ARTIFACT_DELETED;

public class ArtifactDeleted extends OutboxEvent {

private final JSONObject eventPayload;

private ArtifactDeleted(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static ArtifactDeleted of(String groupId, String artifactId) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("groupId", groupId).put("artifactId", artifactId).put("eventType",
ARTIFACT_DELETED.name());

return new ArtifactDeleted(id, groupId + "-" + artifactId, jsonObject);
}

@Override
public String getType() {
return ARTIFACT_DELETED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.EditableArtifactMetaDataDto;
import io.apicurio.registry.storage.dto.OutboxEvent;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.ARTIFACT_METADATA_UPDATED;

public class ArtifactMetadataUpdated extends OutboxEvent {

private final JSONObject eventPayload;

private ArtifactMetadataUpdated(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static ArtifactMetadataUpdated of(String groupId, String artifactId,
EditableArtifactMetaDataDto artifactMetaDataDto) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("groupId", groupId).put("artifactId", artifactId)
.put("name", artifactMetaDataDto.getName()).put("owner", artifactMetaDataDto.getOwner())
.put("description", artifactMetaDataDto.getDescription())
.put("eventType", ARTIFACT_METADATA_UPDATED.name());

return new ArtifactMetadataUpdated(id, groupId + "-" + artifactId, jsonObject);
}

@Override
public String getType() {
return ARTIFACT_METADATA_UPDATED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.OutboxEvent;
import io.apicurio.registry.storage.dto.RuleConfigurationDto;
import io.apicurio.registry.types.RuleType;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.ARTIFACT_RULE_CONFIGURED;

public class ArtifactRuleConfigured extends OutboxEvent {
private final JSONObject eventPayload;

private ArtifactRuleConfigured(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static ArtifactRuleConfigured of(String groupId, String artifactId, RuleType ruleType,
RuleConfigurationDto rule) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("groupId", groupId).put("artifactId", artifactId)
.put("ruleType", ruleType.value()).put("rule", rule.getConfiguration())
.put("eventType", ARTIFACT_RULE_CONFIGURED.name());

return new ArtifactRuleConfigured(id, groupId + "-" + artifactId, jsonObject);
}

@Override
public String getType() {
return ARTIFACT_RULE_CONFIGURED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.ArtifactVersionMetaDataDto;
import io.apicurio.registry.storage.dto.OutboxEvent;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.ARTIFACT_VERSION_CREATED;

public class ArtifactVersionCreated extends OutboxEvent {
private final JSONObject eventPayload;

private ArtifactVersionCreated(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static ArtifactVersionCreated of(ArtifactVersionMetaDataDto versionMetaDataDto) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("groupId", versionMetaDataDto.getGroupId())
.put("artifactId", versionMetaDataDto.getArtifactId())
.put("version", versionMetaDataDto.getVersion()).put("name", versionMetaDataDto.getName())
.put("description", versionMetaDataDto.getDescription())
.put("eventType", ARTIFACT_VERSION_CREATED.name());

return new ArtifactVersionCreated(id, versionMetaDataDto.getGroupId() + "-"
+ versionMetaDataDto.getArtifactId() + "-" + versionMetaDataDto.getVersion(), jsonObject);
}

@Override
public String getType() {
return ARTIFACT_VERSION_CREATED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.OutboxEvent;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.ARTIFACT_VERSION_DELETED;

public class ArtifactVersionDeleted extends OutboxEvent {

private final JSONObject eventPayload;

private ArtifactVersionDeleted(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static ArtifactVersionDeleted of(String groupId, String artifactId, String version) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("groupId", groupId).put("artifactId", artifactId).put("version", version)
.put("eventType", ARTIFACT_VERSION_DELETED.name());

return new ArtifactVersionDeleted(id, groupId + "-" + artifactId + "-" + version, jsonObject);
}

@Override
public String getType() {
return ARTIFACT_VERSION_DELETED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.EditableVersionMetaDataDto;
import io.apicurio.registry.storage.dto.OutboxEvent;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.ARTIFACT_VERSION_METADATA_UPDATED;

public class ArtifactVersionMetadataUpdated extends OutboxEvent {

private final JSONObject eventPayload;

private ArtifactVersionMetadataUpdated(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static ArtifactVersionMetadataUpdated of(String groupId, String artifactId, String version,
EditableVersionMetaDataDto editableVersionMetaDataDto) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("groupId", groupId).put("artifactId", artifactId).put("version", version)
.put("name", editableVersionMetaDataDto.getName())
.put("description", editableVersionMetaDataDto.getDescription())
.put("eventType", ARTIFACT_VERSION_METADATA_UPDATED.name());

return new ArtifactVersionMetadataUpdated(id, groupId + "-" + artifactId + "-" + version, jsonObject);
}

@Override
public String getType() {
return ARTIFACT_VERSION_METADATA_UPDATED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.OutboxEvent;
import io.apicurio.registry.storage.dto.RuleConfigurationDto;
import io.apicurio.registry.types.RuleType;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.GLOBAL_RULE_CONFIGURED;

public class GlobalRuleConfigured extends OutboxEvent {
private final JSONObject eventPayload;

private GlobalRuleConfigured(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static GlobalRuleConfigured of(RuleType ruleType, RuleConfigurationDto rule) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("ruleType", ruleType.value()).put("rule", rule.getConfiguration())
.put("eventType", GLOBAL_RULE_CONFIGURED.name());

return new GlobalRuleConfigured(id, ruleType.value(), jsonObject);
}

@Override
public String getType() {
return GLOBAL_RULE_CONFIGURED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/io/apicurio/registry/events/GroupCreated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.apicurio.registry.events;

import io.apicurio.registry.storage.dto.GroupMetaDataDto;
import io.apicurio.registry.storage.dto.OutboxEvent;
import org.json.JSONObject;

import java.util.UUID;

import static io.apicurio.registry.storage.StorageEventType.GROUP_CREATED;

public class GroupCreated extends OutboxEvent {
private final JSONObject eventPayload;

private GroupCreated(String id, String aggregateId, JSONObject eventPayload) {
super(id, aggregateId);
this.eventPayload = eventPayload;
}

public static GroupCreated of(GroupMetaDataDto groupMetaDataDto) {
String id = UUID.randomUUID().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", id).put("groupId", groupMetaDataDto.getGroupId()).put("eventType",
GROUP_CREATED.name());

return new GroupCreated(id, groupMetaDataDto.getGroupId(), jsonObject);
}

@Override
public String getType() {
return GROUP_CREATED.name();
}

@Override
public JSONObject getPayload() {
return eventPayload;
}
}
Loading

0 comments on commit 5d756a9

Please sign in to comment.