Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#2980] test(core): add test for event listener (#2981)

FANNG4a09d11bd0d4
diff --git a/core/build.gradle.kts b/core/build.gradle.kts
index 6f6fb3b726..983bbb59c0 100644
--- a/core/build.gradle.kts
+++ b/core/build.gradle.kts
@@ -33,6 +33,7 @@ dependencies {
testAnnotationProcessor(libs.lombok)
testCompileOnly(libs.lombok)
+ testImplementation(libs.awaitility)
testImplementation(libs.h2db)
testImplementation(libs.junit.jupiter.api)
testImplementation(libs.junit.jupiter.params)
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListCatalogEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListCatalogEvent.java
index 37b4d666f9..2e1421ac09 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListCatalogEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListCatalogEvent.java
@@ -21,7 +21,7 @@ public final class ListCatalogEvent extends CatalogEvent {
* @param namespace The namespace from which catalogs were listed.
*/
public ListCatalogEvent(String user, Namespace namespace) {
- super(user, NameIdentifier.of(namespace.toString()));
+ super(user, NameIdentifier.of(namespace.levels()));
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListCatalogFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListCatalogFailureEvent.java
index 30e4b24e3e..203310fde7 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListCatalogFailureEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListCatalogFailureEvent.java
@@ -25,7 +25,7 @@ public final class ListCatalogFailureEvent extends CatalogFailureEvent {
* @param exception The exception encountered during the attempt to list catalogs.
*/
public ListCatalogFailureEvent(String user, Exception exception, Namespace namespace) {
- super(user, NameIdentifier.of(namespace.toString()), exception);
+ super(user, NameIdentifier.of(namespace.levels()), exception);
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListFilesetEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListFilesetEvent.java
index c5e99d6a70..5d18863146 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListFilesetEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListFilesetEvent.java
@@ -23,7 +23,7 @@ public final class ListFilesetEvent extends FilesetEvent {
* contextual information, identifying the scope and boundaries of the listing operation.
*/
public ListFilesetEvent(String user, Namespace namespace) {
- super(user, NameIdentifier.of(namespace.toString()));
+ super(user, NameIdentifier.of(namespace.levels()));
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListFilesetFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListFilesetFailureEvent.java
index bf063fd891..745945b384 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListFilesetFailureEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListFilesetFailureEvent.java
@@ -26,7 +26,7 @@ public final class ListFilesetFailureEvent extends FilesetFailureEvent {
* an indicator of the issues that caused the failure.
*/
public ListFilesetFailureEvent(String user, Namespace namespace, Exception exception) {
- super(user, NameIdentifier.of(namespace.toString()), exception);
+ super(user, NameIdentifier.of(namespace.levels()), exception);
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListSchemaEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListSchemaEvent.java
index dd13768aab..08aa0b38a3 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListSchemaEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListSchemaEvent.java
@@ -15,7 +15,7 @@ public final class ListSchemaEvent extends SchemaEvent {
private final Namespace namespace;
public ListSchemaEvent(String user, Namespace namespace) {
- super(user, NameIdentifier.of(namespace.toString()));
+ super(user, NameIdentifier.of(namespace.levels()));
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListSchemaFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListSchemaFailureEvent.java
index cb233503db..b1c0a037ae 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListSchemaFailureEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListSchemaFailureEvent.java
@@ -18,7 +18,7 @@ public final class ListSchemaFailureEvent extends SchemaFailureEvent {
private final Namespace namespace;
public ListSchemaFailureEvent(String user, Namespace namespace, Exception exception) {
- super(user, NameIdentifier.of(namespace.toString()), exception);
+ super(user, NameIdentifier.of(namespace.levels()), exception);
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTableEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTableEvent.java
index 63eb0d410c..7a28ebe08e 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTableEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTableEvent.java
@@ -28,7 +28,7 @@ public final class ListTableEvent extends TableEvent {
* @param namespace The namespace from which tables were listed.
*/
public ListTableEvent(String user, Namespace namespace) {
- super(user, NameIdentifier.parse(namespace.toString()));
+ super(user, NameIdentifier.of(namespace.levels()));
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTableFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTableFailureEvent.java
index 706fc71d16..169b6ea80d 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTableFailureEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTableFailureEvent.java
@@ -25,7 +25,7 @@ public final class ListTableFailureEvent extends TableFailureEvent {
* @param exception The exception encountered during the attempt to list tables.
*/
public ListTableFailureEvent(String user, Namespace namespace, Exception exception) {
- super(user, NameIdentifier.of(namespace.toString()), exception);
+ super(user, NameIdentifier.of(namespace.levels()), exception);
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTopicEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTopicEvent.java
index 4eacc1fbf1..16297fe385 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTopicEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTopicEvent.java
@@ -21,7 +21,7 @@ public final class ListTopicEvent extends TopicEvent {
* @param namespace The namespace from which topics were listed.
*/
public ListTopicEvent(String user, Namespace namespace) {
- super(user, NameIdentifier.parse(namespace.toString()));
+ super(user, NameIdentifier.of(namespace.levels()));
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTopicFailureEvent.java b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTopicFailureEvent.java
index e98212fb74..a7c686f625 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTopicFailureEvent.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/event/ListTopicFailureEvent.java
@@ -25,7 +25,7 @@ public final class ListTopicFailureEvent extends TopicFailureEvent {
* @param exception The exception encountered during the attempt to list topics.
*/
public ListTopicFailureEvent(String user, Namespace namespace, Exception exception) {
- super(user, NameIdentifier.of(namespace.toString()), exception);
+ super(user, NameIdentifier.of(namespace.levels()), exception);
this.namespace = namespace;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/info/FilesetInfo.java b/core/src/main/java/com/datastrato/gravitino/listener/api/info/FilesetInfo.java
index eb81f86510..93b0f1e5ab 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/info/FilesetInfo.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/info/FilesetInfo.java
@@ -12,6 +12,7 @@ import com.google.common.collect.ImmutableMap;
import java.util.Map;
import javax.annotation.Nullable;
+/** Encapsulates read-only information about a fileset, intended for use in event listeners. */
@DeveloperApi
public final class FilesetInfo {
private final String name;
@@ -21,6 +22,11 @@ public final class FilesetInfo {
private final Map<String, String> properties;
@Nullable private final Audit audit;
+ /**
+ * Constructs a FilesetInfo object from a Fileset instance.
+ *
+ * @param fileset The source Fileset instance.
+ */
public FilesetInfo(Fileset fileset) {
this(
fileset.name(),
@@ -31,6 +37,16 @@ public final class FilesetInfo {
fileset.auditInfo());
}
+ /**
+ * Constructs a FilesetInfo object with specified details.
+ *
+ * @param name The name of the fileset.
+ * @param comment An optional comment about the fileset. Can be {@code null}.
+ * @param type The type of the fileset.
+ * @param storageLocation The storage location of the fileset.
+ * @param properties A map of properties associated with the fileset. Can be {@code null}.
+ * @param audit Optional audit information. Can be {@code null}.
+ */
public FilesetInfo(
String name,
String comment,
@@ -42,36 +58,62 @@ public final class FilesetInfo {
this.comment = comment;
this.type = type;
this.storageLocation = storageLocation;
- if (properties == null) {
- this.properties = ImmutableMap.of();
- } else {
- this.properties = ImmutableMap.<String, String>builder().putAll(properties).build();
- }
+ this.properties = properties == null ? ImmutableMap.of() : ImmutableMap.copyOf(properties);
this.audit = audit;
}
+ /**
+ * Returns the audit information.
+ *
+ * @return The audit information, or {@code null} if not available.
+ */
@Nullable
public Audit auditInfo() {
return audit;
}
+ /**
+ * Returns the name of the fileset.
+ *
+ * @return The fileset name.
+ */
public String name() {
return name;
}
- public Fileset.Type getType() {
+ /**
+ * Returns the type of the fileset.
+ *
+ * @return The fileset type.
+ */
+ public Fileset.Type type() {
return type;
}
- public String getStorageLocation() {
+ /**
+ * Returns the storage location of the fileset.
+ *
+ * @return The storage location.
+ */
+ public String storageLocation() {
return storageLocation;
}
+ /**
+ * Returns the optional comment about the fileset.
+ *
+ * @return The comment, or {@code null} if not provided.
+ */
@Nullable
public String comment() {
return comment;
}
+ /**
+ * Returns the properties associated with the fileset.
+ *
+ * @return The properties map.
+ */
public Map<String, String> properties() {
return properties;
}
diff --git a/core/src/main/java/com/datastrato/gravitino/listener/api/info/TableInfo.java b/core/src/main/java/com/datastrato/gravitino/listener/api/info/TableInfo.java
index 2f7329e050..a877c00af2 100644
--- a/core/src/main/java/com/datastrato/gravitino/listener/api/info/TableInfo.java
+++ b/core/src/main/java/com/datastrato/gravitino/listener/api/info/TableInfo.java
@@ -35,6 +35,11 @@ public final class TableInfo {
private final Index[] indexes;
@Nullable private final Audit auditInfo;
+ /**
+ * Constructs a TableInfo object from a Table instance.
+ *
+ * @param table The source Table instance.
+ */
public TableInfo(Table table) {
this(
table.name(),
@@ -48,6 +53,19 @@ public final class TableInfo {
table.auditInfo());
}
+ /**
+ * Constructs a TableInfo object with specified details.
+ *
+ * @param name Name of the table.
+ * @param columns Array of columns in the table.
+ * @param comment Optional comment about the table.
+ * @param properties Map of table properties.
+ * @param partitions Array of partition transforms.
+ * @param distribution Table distribution configuration.
+ * @param sortOrders Array of sort order configurations.
+ * @param indexes Array of indexes on the table.
+ * @param auditInfo Optional audit information.
+ */
public TableInfo(
String name,
Column[] columns,
@@ -61,69 +79,93 @@ public final class TableInfo {
this.name = name;
this.columns = columns.clone();
this.comment = comment;
- if (properties == null) {
- this.properties = ImmutableMap.of();
- } else {
- this.properties = ImmutableMap.<String, String>builder().putAll(properties).build();
- }
- if (partitions == null) {
- this.partitions = new Transform[0];
- } else {
- this.partitions = partitions.clone();
- }
- if (distribution == null) {
- this.distribution = Distributions.NONE;
- } else {
- this.distribution = distribution;
- }
- if (sortOrders == null) {
- this.sortOrders = new SortOrder[0];
- } else {
- this.sortOrders = sortOrders.clone();
- }
- if (indexes == null) {
- this.indexes = Indexes.EMPTY_INDEXES;
- } else {
- this.indexes = indexes.clone();
- }
+ this.properties = properties == null ? ImmutableMap.of() : ImmutableMap.copyOf(properties);
+ this.partitions = partitions == null ? new Transform[0] : partitions.clone();
+ this.distribution = distribution == null ? Distributions.NONE : distribution;
+ this.sortOrders = sortOrders == null ? new SortOrder[0] : sortOrders.clone();
+ this.indexes = indexes == null ? Indexes.EMPTY_INDEXES : indexes.clone();
this.auditInfo = auditInfo;
}
- /** Audit information is null when tableInfo is generated from create table request. */
+ /**
+ * Returns the audit information for the table.
+ *
+ * @return Audit information, or {@code null} if not available.
+ */
@Nullable
public Audit auditInfo() {
return this.auditInfo;
}
+ /**
+ * Returns the name of the table.
+ *
+ * @return Table name.
+ */
public String name() {
return name;
}
+ /**
+ * Returns the columns of the table.
+ *
+ * @return Array of table columns.
+ */
public Column[] columns() {
return columns;
}
+ /**
+ * Returns the partitioning transforms applied to the table.
+ *
+ * @return Array of partition transforms.
+ */
public Transform[] partitioning() {
return partitions;
}
+ /**
+ * Returns the sort order configurations for the table.
+ *
+ * @return Array of sort orders.
+ */
public SortOrder[] sortOrder() {
return sortOrders;
}
+ /**
+ * Returns the distribution configuration for the table.
+ *
+ * @return Distribution configuration.
+ */
public Distribution distribution() {
return distribution;
}
+ /**
+ * Returns the indexes applied to the table.
+ *
+ * @return Array of indexes.
+ */
public Index[] index() {
return indexes;
}
+ /**
+ * Returns the optional comment about the table.
+ *
+ * @return Table comment, or {@code null} if not provided.
+ */
@Nullable
public String comment() {
return comment;
}
+ /**
+ * Returns the properties associated with the table.
+ *
+ * @return Map of table properties.
+ */
public Map<String, String> properties() {
return properties;
}
diff --git a/core/src/test/java/com/datastrato/gravitino/listener/DummyEventListener.java b/core/src/test/java/com/datastrato/gravitino/listener/DummyEventListener.java
new file mode 100644
index 0000000000..a78a07349a
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/listener/DummyEventListener.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.listener;
+
+import com.datastrato.gravitino.listener.api.EventListenerPlugin;
+import com.datastrato.gravitino.listener.api.event.Event;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import lombok.Getter;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Assertions;
+
+public class DummyEventListener implements EventListenerPlugin {
+ Map<String, String> properties;
+ @Getter LinkedList<Event> events = new LinkedList<>();
+
+ @Override
+ public void init(Map<String, String> properties) {
+ this.properties = properties;
+ }
+
+ @Override
+ public void start() {}
+
+ @Override
+ public void stop() {}
+
+ @Override
+ public void onPostEvent(Event event) {
+ this.events.add(event);
+ }
+
+ @Override
+ public Mode mode() {
+ return Mode.SYNC;
+ }
+
+ public Event popEvent() {
+ Assertions.assertTrue(events.size() > 0, "No events to pop");
+ return events.removeLast();
+ }
+
+ public static class DummyAsyncEventListener extends DummyEventListener {
+ public List<Event> tryGetEvents() {
+ Awaitility.await()
+ .atMost(20, TimeUnit.SECONDS)
+ .pollInterval(10, TimeUnit.MILLISECONDS)
+ .until(() -> getEvents().size() > 0);
+ return getEvents();
+ }
+
+ @Override
+ public Mode mode() {
+ return Mode.ASYNC_SHARED;
+ }
+ }
+
+ public static class DummyAsyncIsolatedEventListener extends DummyAsyncEventListener {
+ @Override
+ public Mode mode() {
+ return Mode.ASYNC_ISOLATED;
+ }
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/listener/TestEventListenerManager.java b/core/src/test/java/com/datastrato/gravitino/listener/TestEventListenerManager.java
index f670a39404..fa1c15044e 100644
--- a/core/src/test/java/com/datastrato/gravitino/listener/TestEventListenerManager.java
+++ b/core/src/test/java/com/datastrato/gravitino/listener/TestEventListenerManager.java
@@ -6,17 +6,16 @@
package com.datastrato.gravitino.listener;
import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.listener.DummyEventListener.DummyAsyncEventListener;
+import com.datastrato.gravitino.listener.DummyEventListener.DummyAsyncIsolatedEventListener;
import com.datastrato.gravitino.listener.api.EventListenerPlugin;
import com.datastrato.gravitino.listener.api.event.Event;
import com.google.common.collect.ImmutableSet;
-import java.time.Instant;
-import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
-import lombok.Getter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -27,58 +26,6 @@ public class TestEventListenerManager {
}
}
- static class DummyEventListener implements EventListenerPlugin {
- Map<String, String> properties;
- @Getter List<Event> events = new ArrayList<>();
-
- @Override
- public void init(Map<String, String> properties) {
- this.properties = properties;
- }
-
- @Override
- public void start() {}
-
- @Override
- public void stop() {}
-
- @Override
- public void onPostEvent(Event event) {
- this.events.add(event);
- }
-
- @Override
- public Mode mode() {
- return Mode.SYNC;
- }
- }
-
- static class DummyAsyncEventListener extends DummyEventListener {
- List<Event> tryGetEvents() {
- Instant waitTime = Instant.now().plusSeconds(20);
- while (getEvents().size() == 0 && Instant.now().isBefore(waitTime)) {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- break;
- }
- }
- return getEvents();
- }
-
- @Override
- public Mode mode() {
- return Mode.ASYNC_SHARED;
- }
- }
-
- static class DummyAsyncIsolatedEventListener extends DummyAsyncEventListener {
- @Override
- public Mode mode() {
- return Mode.ASYNC_ISOLATED;
- }
- }
-
private static final DummyEvent DUMMY_EVENT_INSTANCE =
new DummyEvent("user", NameIdentifier.of("a", "b"));
diff --git a/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestCatalogEvent.java b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestCatalogEvent.java
new file mode 100644
index 0000000000..bccd7e705f
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestCatalogEvent.java
@@ -0,0 +1,248 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.listener.api.event;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.datastrato.gravitino.Catalog;
+import com.datastrato.gravitino.CatalogChange;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.catalog.CatalogDispatcher;
+import com.datastrato.gravitino.catalog.CatalogEventDispatcher;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.listener.DummyEventListener;
+import com.datastrato.gravitino.listener.EventBus;
+import com.datastrato.gravitino.listener.api.info.CatalogInfo;
+import com.google.common.collect.ImmutableMap;
+import java.util.Arrays;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+
+@TestInstance(Lifecycle.PER_CLASS)
+public class TestCatalogEvent {
+ private CatalogEventDispatcher dispatcher;
+ private CatalogEventDispatcher failureDispatcher;
+ private DummyEventListener dummyEventListener;
+ private Catalog catalog;
+
+ @BeforeAll
+ void init() {
+ this.catalog = mockCatalog();
+ this.dummyEventListener = new DummyEventListener();
+ EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener));
+ CatalogDispatcher catalogDispatcher = mockCatalogDispatcher();
+ this.dispatcher = new CatalogEventDispatcher(eventBus, catalogDispatcher);
+ CatalogDispatcher catalogExceptionDispatcher = mockExceptionCatalogDispatcher();
+ this.failureDispatcher = new CatalogEventDispatcher(eventBus, catalogExceptionDispatcher);
+ }
+
+ @Test
+ void testCreateCatalogEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", catalog.name());
+ dispatcher.createCatalog(
+ identifier, catalog.type(), catalog.provider(), catalog.comment(), catalog.properties());
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateCatalogEvent.class, event.getClass());
+ CatalogInfo catalogInfo = ((CreateCatalogEvent) event).createdCatalogInfo();
+ checkCatalogInfo(catalogInfo, catalog);
+ }
+
+ @Test
+ void testLoadCatalogEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", catalog.name());
+ dispatcher.loadCatalog(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadCatalogEvent.class, event.getClass());
+ CatalogInfo catalogInfo = ((LoadCatalogEvent) event).loadedCatalogInfo();
+ checkCatalogInfo(catalogInfo, catalog);
+ }
+
+ @Test
+ void testAlterCatalogEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", catalog.name());
+ CatalogChange catalogChange = CatalogChange.setProperty("a", "b");
+ dispatcher.alterCatalog(identifier, catalogChange);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterCatalogEvent.class, event.getClass());
+ CatalogInfo catalogInfo = ((AlterCatalogEvent) event).updatedCatalogInfo();
+ checkCatalogInfo(catalogInfo, catalog);
+ CatalogChange[] catalogChanges = ((AlterCatalogEvent) event).catalogChanges();
+ Assertions.assertEquals(1, catalogChanges.length);
+ Assertions.assertEquals(catalogChange, catalogChanges[0]);
+ }
+
+ @Test
+ void testDropCatalogEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", catalog.name());
+ dispatcher.dropCatalog(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropCatalogEvent.class, event.getClass());
+ Assertions.assertEquals(true, ((DropCatalogEvent) event).isExists());
+ }
+
+ @Test
+ void testListCatalogEvent() {
+ Namespace namespace = Namespace.of("metalake");
+ dispatcher.listCatalogs(namespace);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListCatalogEvent.class, event.getClass());
+ Assertions.assertEquals(namespace, ((ListCatalogEvent) event).namespace());
+ }
+
+ @Test
+ void testListCatalogInfoEvent() {
+ Namespace namespace = Namespace.of("metalake");
+ dispatcher.listCatalogsInfo(namespace);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListCatalogEvent.class, event.getClass());
+ Assertions.assertEquals(namespace, ((ListCatalogEvent) event).namespace());
+ }
+
+ @Test
+ void testCreateCatalogFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", catalog.name());
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () ->
+ failureDispatcher.createCatalog(
+ identifier,
+ catalog.type(),
+ catalog.provider(),
+ catalog.comment(),
+ catalog.properties()));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateCatalogFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ ((CreateCatalogFailureEvent) event).exception().getClass());
+ checkCatalogInfo(((CreateCatalogFailureEvent) event).createCatalogRequest(), catalog);
+ }
+
+ @Test
+ void testLoadCatalogFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.loadCatalog(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadCatalogFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((LoadCatalogFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testAlterCatalogFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog");
+ CatalogChange catalogChange = CatalogChange.setProperty("a", "b");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.alterCatalog(identifier, catalogChange));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterCatalogFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((AlterCatalogFailureEvent) event).exception().getClass());
+ CatalogChange[] catalogChanges = ((AlterCatalogFailureEvent) event).catalogChanges();
+ Assertions.assertEquals(1, catalogChanges.length);
+ Assertions.assertEquals(catalogChange, catalogChanges[0]);
+ }
+
+ @Test
+ void testDropCatalogFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.dropCatalog(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropCatalogFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((DropCatalogFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testListCatalogFailureEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.listCatalogs(namespace));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(ListCatalogFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((ListCatalogFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(namespace, ((ListCatalogFailureEvent) event).namespace());
+ }
+
+ @Test
+ void testListCatalogInfoFailureEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.listCatalogsInfo(namespace));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(ListCatalogFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((ListCatalogFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(namespace, ((ListCatalogFailureEvent) event).namespace());
+ }
+
+ private void checkCatalogInfo(CatalogInfo catalogInfo, Catalog catalog) {
+ Assertions.assertEquals(catalog.name(), catalogInfo.name());
+ Assertions.assertEquals(catalog.type(), catalogInfo.type());
+ Assertions.assertEquals(catalog.provider(), catalogInfo.provider());
+ Assertions.assertEquals(catalog.properties(), catalogInfo.properties());
+ Assertions.assertEquals(catalog.comment(), catalogInfo.comment());
+ }
+
+ private Catalog mockCatalog() {
+ Catalog catalog = mock(Catalog.class);
+ when(catalog.comment()).thenReturn("comment");
+ when(catalog.properties()).thenReturn(ImmutableMap.of("a", "b"));
+ when(catalog.name()).thenReturn("catalog");
+ when(catalog.provider()).thenReturn("hive");
+ when(catalog.type()).thenReturn(Catalog.Type.RELATIONAL);
+ when(catalog.auditInfo()).thenReturn(null);
+ return catalog;
+ }
+
+ private CatalogDispatcher mockCatalogDispatcher() {
+ CatalogDispatcher dispatcher = mock(CatalogDispatcher.class);
+ when(dispatcher.createCatalog(
+ any(NameIdentifier.class),
+ any(Catalog.Type.class),
+ any(String.class),
+ any(String.class),
+ any(Map.class)))
+ .thenReturn(catalog);
+ when(dispatcher.loadCatalog(any(NameIdentifier.class))).thenReturn(catalog);
+ when(dispatcher.dropCatalog(any(NameIdentifier.class))).thenReturn(true);
+ when(dispatcher.listCatalogs(any(Namespace.class))).thenReturn(null);
+ when(dispatcher.alterCatalog(any(NameIdentifier.class), any(CatalogChange.class)))
+ .thenReturn(catalog);
+ return dispatcher;
+ }
+
+ private CatalogDispatcher mockExceptionCatalogDispatcher() {
+ CatalogDispatcher dispatcher =
+ mock(
+ CatalogDispatcher.class,
+ invocation -> {
+ throw new GravitinoRuntimeException("Exception for all methods");
+ });
+ return dispatcher;
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestFilesetEvent.java b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestFilesetEvent.java
new file mode 100644
index 0000000000..7f5a679d5a
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestFilesetEvent.java
@@ -0,0 +1,228 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.listener.api.event;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.catalog.FilesetDispatcher;
+import com.datastrato.gravitino.catalog.FilesetEventDispatcher;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.file.Fileset;
+import com.datastrato.gravitino.file.FilesetChange;
+import com.datastrato.gravitino.listener.DummyEventListener;
+import com.datastrato.gravitino.listener.EventBus;
+import com.datastrato.gravitino.listener.api.info.FilesetInfo;
+import com.google.common.collect.ImmutableMap;
+import java.util.Arrays;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+
+@TestInstance(Lifecycle.PER_CLASS)
+public class TestFilesetEvent {
+ private FilesetEventDispatcher dispatcher;
+ private FilesetEventDispatcher failureDispatcher;
+ private DummyEventListener dummyEventListener;
+ private Fileset fileset;
+
+ @BeforeAll
+ void init() {
+ this.fileset = mockFileset();
+ this.dummyEventListener = new DummyEventListener();
+ EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener));
+ FilesetDispatcher filesetDispatcher = mockFilesetDispatcher();
+ this.dispatcher = new FilesetEventDispatcher(eventBus, filesetDispatcher);
+ FilesetDispatcher filesetExceptionDispatcher = mockExceptionFilesetDispatcher();
+ this.failureDispatcher = new FilesetEventDispatcher(eventBus, filesetExceptionDispatcher);
+ }
+
+ @Test
+ void testCreateFilesetEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", fileset.name());
+ dispatcher.createFileset(
+ identifier,
+ fileset.comment(),
+ fileset.type(),
+ fileset.storageLocation(),
+ fileset.properties());
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateFilesetEvent.class, event.getClass());
+ FilesetInfo filesetInfo = ((CreateFilesetEvent) event).createdFilesetInfo();
+ checkFilesetInfo(filesetInfo, fileset);
+ }
+
+ @Test
+ void testLoadFilesetEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", fileset.name());
+ dispatcher.loadFileset(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadFilesetEvent.class, event.getClass());
+ FilesetInfo filesetInfo = ((LoadFilesetEvent) event).loadedFilesetInfo();
+ checkFilesetInfo(filesetInfo, fileset);
+ }
+
+ @Test
+ void testAlterFilesetEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", fileset.name());
+ FilesetChange change = FilesetChange.setProperty("a", "b");
+ dispatcher.alterFileset(identifier, change);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterFilesetEvent.class, event.getClass());
+ FilesetInfo filesetInfo = ((AlterFilesetEvent) event).updatedFilesetInfo();
+ checkFilesetInfo(filesetInfo, fileset);
+ Assertions.assertEquals(1, ((AlterFilesetEvent) event).filesetChanges().length);
+ Assertions.assertEquals(change, ((AlterFilesetEvent) event).filesetChanges()[0]);
+ }
+
+ @Test
+ void testDropFilesetEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", fileset.name());
+ dispatcher.dropFileset(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropFilesetEvent.class, event.getClass());
+ Assertions.assertTrue(((DropFilesetEvent) event).isExists());
+ }
+
+ @Test
+ void testListFilesetEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ dispatcher.listFilesets(namespace);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListFilesetEvent.class, event.getClass());
+ Assertions.assertEquals(namespace, ((ListFilesetEvent) event).namespace());
+ }
+
+ @Test
+ void testCreateSchemaFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "fileset");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () ->
+ failureDispatcher.createFileset(
+ identifier,
+ fileset.comment(),
+ fileset.type(),
+ fileset.storageLocation(),
+ fileset.properties()));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateFilesetFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ ((CreateFilesetFailureEvent) event).exception().getClass());
+ checkFilesetInfo(((CreateFilesetFailureEvent) event).createFilesetRequest(), fileset);
+ }
+
+ @Test
+ void testLoadFilesetFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "fileset");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.loadFileset(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadFilesetFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((LoadFilesetFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testAlterFilesetFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "fileset");
+ FilesetChange change = FilesetChange.setProperty("a", "b");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.alterFileset(identifier, change));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterFilesetFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((AlterFilesetFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(1, ((AlterFilesetFailureEvent) event).filesetChanges().length);
+ Assertions.assertEquals(change, ((AlterFilesetFailureEvent) event).filesetChanges()[0]);
+ }
+
+ @Test
+ void testDropFilesetFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "fileset");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.dropFileset(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropFilesetFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((DropFilesetFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testListFilesetFailureEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.listFilesets(namespace));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListFilesetFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((ListFilesetFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(namespace, ((ListFilesetFailureEvent) event).namespace());
+ }
+
+ private void checkFilesetInfo(FilesetInfo filesetInfo, Fileset fileset) {
+ Assertions.assertEquals(fileset.name(), filesetInfo.name());
+ Assertions.assertEquals(fileset.type(), filesetInfo.type());
+ Assertions.assertEquals(fileset.storageLocation(), filesetInfo.storageLocation());
+ Assertions.assertEquals(fileset.properties(), filesetInfo.properties());
+ Assertions.assertEquals(fileset.comment(), filesetInfo.comment());
+ }
+
+ private Fileset mockFileset() {
+ Fileset fileset = mock(Fileset.class);
+ when(fileset.comment()).thenReturn("comment");
+ when(fileset.type()).thenReturn(Fileset.Type.MANAGED);
+ when(fileset.properties()).thenReturn(ImmutableMap.of("a", "b"));
+ when(fileset.name()).thenReturn("fileset");
+ when(fileset.auditInfo()).thenReturn(null);
+ when(fileset.storageLocation()).thenReturn("location");
+ return fileset;
+ }
+
+ private FilesetDispatcher mockFilesetDispatcher() {
+ FilesetDispatcher dispatcher = mock(FilesetDispatcher.class);
+ when(dispatcher.createFileset(
+ any(NameIdentifier.class),
+ any(String.class),
+ any(Fileset.Type.class),
+ any(String.class),
+ any(Map.class)))
+ .thenReturn(fileset);
+ when(dispatcher.loadFileset(any(NameIdentifier.class))).thenReturn(fileset);
+ when(dispatcher.dropFileset(any(NameIdentifier.class))).thenReturn(true);
+ when(dispatcher.listFilesets(any(Namespace.class))).thenReturn(null);
+ when(dispatcher.alterFileset(any(NameIdentifier.class), any(FilesetChange.class)))
+ .thenReturn(fileset);
+ return dispatcher;
+ }
+
+ private FilesetDispatcher mockExceptionFilesetDispatcher() {
+ FilesetDispatcher dispatcher =
+ mock(
+ FilesetDispatcher.class,
+ invocation -> {
+ throw new GravitinoRuntimeException("Exception for all methods");
+ });
+ return dispatcher;
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestMetalakeEvent.java b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestMetalakeEvent.java
new file mode 100644
index 0000000000..09fb7aab53
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestMetalakeEvent.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.listener.api.event;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.datastrato.gravitino.Metalake;
+import com.datastrato.gravitino.MetalakeChange;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.listener.DummyEventListener;
+import com.datastrato.gravitino.listener.EventBus;
+import com.datastrato.gravitino.listener.api.info.MetalakeInfo;
+import com.datastrato.gravitino.metalake.MetalakeDispatcher;
+import com.datastrato.gravitino.metalake.MetalakeEventDispatcher;
+import com.google.common.collect.ImmutableMap;
+import java.util.Arrays;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+
+@TestInstance(Lifecycle.PER_CLASS)
+public class TestMetalakeEvent {
+ private MetalakeEventDispatcher dispatcher;
+ private MetalakeEventDispatcher failureDispatcher;
+ private DummyEventListener dummyEventListener;
+ private Metalake metalake;
+
+ @BeforeAll
+ void init() {
+ this.metalake = mockMetalake();
+ this.dummyEventListener = new DummyEventListener();
+ EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener));
+ MetalakeDispatcher metalakeDispatcher = mockMetalakeDispatcher();
+ this.dispatcher = new MetalakeEventDispatcher(eventBus, metalakeDispatcher);
+ MetalakeDispatcher metalakeExceptionDispatcher = mockExceptionMetalakeDispatcher();
+ this.failureDispatcher = new MetalakeEventDispatcher(eventBus, metalakeExceptionDispatcher);
+ }
+
+ @Test
+ void testCreateMetalakeEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake");
+ dispatcher.createMetalake(identifier, metalake.comment(), metalake.properties());
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateMetalakeEvent.class, event.getClass());
+ MetalakeInfo metalakeInfo = ((CreateMetalakeEvent) event).createdMetalakeInfo();
+ checkMetalakeInfo(metalakeInfo, metalake);
+ }
+
+ @Test
+ void testLoadMetalakeEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake");
+ dispatcher.loadMetalake(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadMetalakeEvent.class, event.getClass());
+ MetalakeInfo metalakeInfo = ((LoadMetalakeEvent) event).loadedMetalakeInfo();
+ checkMetalakeInfo(metalakeInfo, metalake);
+ }
+
+ @Test
+ void testAlterMetalakeEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake");
+ MetalakeChange metalakeChange = MetalakeChange.setProperty("a", "b");
+ dispatcher.alterMetalake(identifier, metalakeChange);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterMetalakeEvent.class, event.getClass());
+ MetalakeInfo metalakeInfo = ((AlterMetalakeEvent) event).updatedMetalakeInfo();
+ checkMetalakeInfo(metalakeInfo, metalake);
+ MetalakeChange[] metalakeChanges = ((AlterMetalakeEvent) event).metalakeChanges();
+ Assertions.assertTrue(metalakeChanges.length == 1);
+ Assertions.assertEquals(metalakeChange, metalakeChanges[0]);
+ }
+
+ @Test
+ void testDropMetalakeEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake");
+ dispatcher.dropMetalake(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropMetalakeEvent.class, event.getClass());
+ Assertions.assertTrue(((DropMetalakeEvent) event).isExists());
+ }
+
+ @Test
+ void testListMetalakeEvent() {
+ dispatcher.listMetalakes();
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertNull(event.identifier());
+ Assertions.assertEquals(ListMetalakeEvent.class, event.getClass());
+ }
+
+ @Test
+ void testCreateMetalakeFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of(metalake.name());
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () ->
+ failureDispatcher.createMetalake(
+ identifier, metalake.comment(), metalake.properties()));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateMetalakeFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ ((CreateMetalakeFailureEvent) event).exception().getClass());
+ checkMetalakeInfo(((CreateMetalakeFailureEvent) event).createMetalakeRequest(), metalake);
+ }
+
+ @Test
+ void testLoadMetalakeFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of(metalake.name());
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.loadMetalake(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadMetalakeFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((LoadMetalakeFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testAlterMetalakeFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of(metalake.name());
+ MetalakeChange metalakeChange = MetalakeChange.setProperty("a", "b");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.alterMetalake(identifier, metalakeChange));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterMetalakeFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ ((AlterMetalakeFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(1, ((AlterMetalakeFailureEvent) event).metalakeChanges().length);
+ Assertions.assertEquals(
+ metalakeChange, ((AlterMetalakeFailureEvent) event).metalakeChanges()[0]);
+ }
+
+ @Test
+ void testDropMetalakeFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of(metalake.name());
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.dropMetalake(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropMetalakeFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((DropMetalakeFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testListMetalakeFailureEvent() {
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.listMetalakes());
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertNull(event.identifier());
+ Assertions.assertEquals(ListMetalakeFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((ListMetalakeFailureEvent) event).exception().getClass());
+ }
+
+ private void checkMetalakeInfo(MetalakeInfo metalakeInfo, Metalake metalake) {
+ Assertions.assertEquals(metalake.name(), metalakeInfo.name());
+ Assertions.assertEquals(metalake.properties(), metalakeInfo.properties());
+ Assertions.assertEquals(metalake.comment(), metalakeInfo.comment());
+ Assertions.assertEquals(metalake.auditInfo(), metalakeInfo.auditInfo());
+ }
+
+ private Metalake mockMetalake() {
+ Metalake metalake = mock(Metalake.class);
+ when(metalake.comment()).thenReturn("comment");
+ when(metalake.properties()).thenReturn(ImmutableMap.of("a", "b"));
+ when(metalake.name()).thenReturn("metalake");
+ when(metalake.auditInfo()).thenReturn(null);
+ return metalake;
+ }
+
+ private MetalakeDispatcher mockMetalakeDispatcher() {
+ MetalakeDispatcher dispatcher = mock(MetalakeDispatcher.class);
+ when(dispatcher.createMetalake(any(NameIdentifier.class), any(String.class), any(Map.class)))
+ .thenReturn(metalake);
+ when(dispatcher.loadMetalake(any(NameIdentifier.class))).thenReturn(metalake);
+ when(dispatcher.dropMetalake(any(NameIdentifier.class))).thenReturn(true);
+ when(dispatcher.listMetalakes()).thenReturn(null);
+ when(dispatcher.alterMetalake(any(NameIdentifier.class), any(MetalakeChange.class)))
+ .thenReturn(metalake);
+ return dispatcher;
+ }
+
+ private MetalakeDispatcher mockExceptionMetalakeDispatcher() {
+ MetalakeDispatcher dispatcher =
+ mock(
+ MetalakeDispatcher.class,
+ invocation -> {
+ throw new GravitinoRuntimeException("Exception for all methods");
+ });
+ return dispatcher;
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestSchemaEvent.java b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestSchemaEvent.java
new file mode 100644
index 0000000000..9bd7ef49ef
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestSchemaEvent.java
@@ -0,0 +1,215 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.listener.api.event;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.catalog.SchemaDispatcher;
+import com.datastrato.gravitino.catalog.SchemaEventDispatcher;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.listener.DummyEventListener;
+import com.datastrato.gravitino.listener.EventBus;
+import com.datastrato.gravitino.listener.api.info.SchemaInfo;
+import com.datastrato.gravitino.rel.Schema;
+import com.datastrato.gravitino.rel.SchemaChange;
+import com.google.common.collect.ImmutableMap;
+import java.util.Arrays;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+import org.mockito.stubbing.Answer;
+
+@TestInstance(Lifecycle.PER_CLASS)
+public class TestSchemaEvent {
+ private SchemaEventDispatcher dispatcher;
+ private SchemaEventDispatcher failureDispatcher;
+ private DummyEventListener dummyEventListener;
+ private Schema schema;
+
+ @BeforeAll
+ void init() {
+ this.schema = mockSchema();
+ this.dummyEventListener = new DummyEventListener();
+ EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener));
+ SchemaDispatcher schemaDispatcher = mockSchemaDispatcher();
+ this.dispatcher = new SchemaEventDispatcher(eventBus, schemaDispatcher);
+ SchemaDispatcher schemaExceptionDispatcher = mockExceptionSchemaDispatcher();
+ this.failureDispatcher = new SchemaEventDispatcher(eventBus, schemaExceptionDispatcher);
+ }
+
+ @Test
+ void testCreateSchemaEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");
+ dispatcher.createSchema(identifier, "", ImmutableMap.of());
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateSchemaEvent.class, event.getClass());
+ SchemaInfo schemaInfo = ((CreateSchemaEvent) event).createdSchemaInfo();
+ checkSchemaInfo(schemaInfo, schema);
+ }
+
+ @Test
+ void testLoadSchemaEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");
+ dispatcher.loadSchema(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadSchemaEvent.class, event.getClass());
+ SchemaInfo schemaInfo = ((LoadSchemaEvent) event).loadedSchemaInfo();
+ checkSchemaInfo(schemaInfo, schema);
+ }
+
+ @Test
+ void testListSchemaEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ dispatcher.listSchemas(namespace);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(ListSchemaEvent.class, event.getClass());
+ Assertions.assertEquals(namespace, ((ListSchemaEvent) event).namespace());
+ }
+
+ @Test
+ void testAlterSchemaEvent() {
+ SchemaChange schemaChange = SchemaChange.setProperty("a", "b");
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");
+ dispatcher.alterSchema(identifier, schemaChange);
+ Event event = dummyEventListener.popEvent();
+
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterSchemaEvent.class, event.getClass());
+ SchemaInfo schemaInfo = ((AlterSchemaEvent) event).updatedSchemaInfo();
+ checkSchemaInfo(schemaInfo, schema);
+
+ Assertions.assertEquals(1, ((AlterSchemaEvent) event).schemaChanges().length);
+ Assertions.assertEquals(schemaChange, ((AlterSchemaEvent) event).schemaChanges()[0]);
+ }
+
+ @Test
+ void testDropSchemaEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");
+ dispatcher.dropSchema(identifier, true);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropSchemaEvent.class, event.getClass());
+ Assertions.assertEquals(true, ((DropSchemaEvent) event).cascade());
+ Assertions.assertEquals(false, ((DropSchemaEvent) event).isExists());
+ }
+
+ @Test
+ void testCreateSchemaFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.createSchema(identifier, schema.comment(), schema.properties()));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateSchemaFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((CreateSchemaFailureEvent) event).exception().getClass());
+ checkSchemaInfo(((CreateSchemaFailureEvent) event).createSchemaRequest(), schema);
+ }
+
+ @Test
+ void testLoadSchemaFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.loadSchema(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadSchemaFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((LoadSchemaFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testAlterSchemaFailureEvent() {
+ // alter schema
+ SchemaChange schemaChange = SchemaChange.setProperty("a", "b");
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.alterSchema(identifier, schemaChange));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterSchemaFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((AlterSchemaFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(1, ((AlterSchemaFailureEvent) event).schemaChanges().length);
+ Assertions.assertEquals(schemaChange, ((AlterSchemaFailureEvent) event).schemaChanges()[0]);
+ }
+
+ @Test
+ void testDropSchemaFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "schema");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.dropSchema(identifier, true));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropSchemaFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((DropSchemaFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(true, ((DropSchemaFailureEvent) event).cascade());
+ }
+
+ @Test
+ void testListSchemaFailureEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.listSchemas(namespace));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListSchemaFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((ListSchemaFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(namespace, ((ListSchemaFailureEvent) event).namespace());
+ }
+
+ private void checkSchemaInfo(SchemaInfo schemaInfo, Schema schema) {
+ Assertions.assertEquals(schema.name(), schemaInfo.name());
+ Assertions.assertEquals(schema.properties(), schemaInfo.properties());
+ Assertions.assertEquals(schema.comment(), schemaInfo.comment());
+ }
+
+ private Schema mockSchema() {
+ Schema schema = mock(Schema.class);
+ when(schema.comment()).thenReturn("comment");
+ when(schema.properties()).thenReturn(ImmutableMap.of("a", "b"));
+ when(schema.name()).thenReturn("schema");
+ when(schema.auditInfo()).thenReturn(null);
+ return schema;
+ }
+
+ private SchemaDispatcher mockSchemaDispatcher() {
+ SchemaDispatcher dispatcher = mock(SchemaDispatcher.class);
+ when(dispatcher.createSchema(any(NameIdentifier.class), any(String.class), any(Map.class)))
+ .thenReturn(schema);
+ when(dispatcher.loadSchema(any(NameIdentifier.class))).thenReturn(schema);
+ when(dispatcher.dropSchema(any(NameIdentifier.class), eq(true))).thenReturn(false);
+ when(dispatcher.listSchemas(any(Namespace.class))).thenReturn(null);
+ when(dispatcher.alterSchema(any(NameIdentifier.class), any(SchemaChange.class)))
+ .thenReturn(schema);
+ return dispatcher;
+ }
+
+ private SchemaDispatcher mockExceptionSchemaDispatcher() {
+ SchemaDispatcher dispatcher =
+ mock(
+ SchemaDispatcher.class,
+ (Answer)
+ invocation -> {
+ throw new GravitinoRuntimeException("Exception for all methods");
+ });
+ return dispatcher;
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestTableEvent.java b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestTableEvent.java
new file mode 100644
index 0000000000..572cfa4746
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestTableEvent.java
@@ -0,0 +1,281 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.listener.api.event;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.catalog.TableDispatcher;
+import com.datastrato.gravitino.catalog.TableEventDispatcher;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.listener.DummyEventListener;
+import com.datastrato.gravitino.listener.EventBus;
+import com.datastrato.gravitino.listener.api.info.TableInfo;
+import com.datastrato.gravitino.rel.Column;
+import com.datastrato.gravitino.rel.Table;
+import com.datastrato.gravitino.rel.TableChange;
+import com.datastrato.gravitino.rel.expressions.NamedReference;
+import com.datastrato.gravitino.rel.expressions.distributions.Distribution;
+import com.datastrato.gravitino.rel.expressions.distributions.Distributions;
+import com.datastrato.gravitino.rel.expressions.distributions.Strategy;
+import com.datastrato.gravitino.rel.expressions.sorts.SortOrder;
+import com.datastrato.gravitino.rel.expressions.sorts.SortOrders;
+import com.datastrato.gravitino.rel.expressions.transforms.Transform;
+import com.datastrato.gravitino.rel.expressions.transforms.Transforms;
+import com.datastrato.gravitino.rel.indexes.Index;
+import com.datastrato.gravitino.rel.indexes.Indexes;
+import com.datastrato.gravitino.rel.types.Types;
+import com.google.common.collect.ImmutableMap;
+import java.util.Arrays;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+
+@TestInstance(Lifecycle.PER_CLASS)
+public class TestTableEvent {
+ private TableEventDispatcher dispatcher;
+ private TableEventDispatcher failureDispatcher;
+ private DummyEventListener dummyEventListener;
+ private Table table;
+
+ @BeforeAll
+ void init() {
+ this.table = mockTable();
+ this.dummyEventListener = new DummyEventListener();
+ EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener));
+ TableDispatcher tableDispatcher = mockTableDispatcher();
+ this.dispatcher = new TableEventDispatcher(eventBus, tableDispatcher);
+ TableDispatcher tableExceptionDispatcher = mockExceptionTableDispatcher();
+ this.failureDispatcher = new TableEventDispatcher(eventBus, tableExceptionDispatcher);
+ }
+
+ @Test
+ void testCreateTableEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", table.name());
+ dispatcher.createTable(
+ identifier,
+ table.columns(),
+ table.comment(),
+ table.properties(),
+ table.partitioning(),
+ table.distribution(),
+ table.sortOrder(),
+ table.index());
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateTableEvent.class, event.getClass());
+ TableInfo tableInfo = ((CreateTableEvent) event).createdTableInfo();
+ checkTableInfo(tableInfo, table);
+ }
+
+ @Test
+ void testLoadTableEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", table.name());
+ dispatcher.loadTable(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadTableEvent.class, event.getClass());
+ TableInfo tableInfo = ((LoadTableEvent) event).loadedTableInfo();
+ checkTableInfo(tableInfo, table);
+ }
+
+ @Test
+ void testAlterTableEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", table.name());
+ TableChange change = TableChange.setProperty("a", "b");
+ dispatcher.alterTable(identifier, change);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterTableEvent.class, event.getClass());
+ TableInfo tableInfo = ((AlterTableEvent) event).updatedTableInfo();
+ checkTableInfo(tableInfo, table);
+ Assertions.assertEquals(1, ((AlterTableEvent) event).tableChanges().length);
+ Assertions.assertEquals(change, ((AlterTableEvent) event).tableChanges()[0]);
+ }
+
+ @Test
+ void testDropTableEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", table.name());
+ dispatcher.dropTable(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropTableEvent.class, event.getClass());
+ Assertions.assertEquals(true, ((DropTableEvent) event).isExists());
+ }
+
+ @Test
+ void testPurgeTableEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", table.name());
+ dispatcher.purgeTable(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(PurgeTableEvent.class, event.getClass());
+ Assertions.assertEquals(true, ((PurgeTableEvent) event).isExists());
+ }
+
+ @Test
+ void testListTableEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ dispatcher.listTables(namespace);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListTableEvent.class, event.getClass());
+ Assertions.assertEquals(namespace, ((ListTableEvent) event).namespace());
+ }
+
+ @Test
+ void testCreateTableFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "table", table.name());
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () ->
+ failureDispatcher.createTable(
+ identifier,
+ table.columns(),
+ table.comment(),
+ table.properties(),
+ table.partitioning(),
+ table.distribution(),
+ table.sortOrder(),
+ table.index()));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateTableFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((CreateTableFailureEvent) event).exception().getClass());
+ checkTableInfo(((CreateTableFailureEvent) event).createTableRequest(), table);
+ }
+
+ @Test
+ void testLoadTableFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "table", table.name());
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.loadTable(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadTableFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((LoadTableFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testAlterTableFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "table", table.name());
+ TableChange change = TableChange.setProperty("a", "b");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.alterTable(identifier, change));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterTableFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((AlterTableFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(1, ((AlterTableFailureEvent) event).tableChanges().length);
+ Assertions.assertEquals(change, ((AlterTableFailureEvent) event).tableChanges()[0]);
+ }
+
+ @Test
+ void testDropTableFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "table", table.name());
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.dropTable(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropTableFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((DropTableFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testPurgeTableFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "table", table.name());
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.purgeTable(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(PurgeTableFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((PurgeTableFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testListTableFailureEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.listTables(namespace));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListTableFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((ListTableFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(namespace, ((ListTableFailureEvent) event).namespace());
+ }
+
+ private void checkTableInfo(TableInfo tableInfo, Table table) {
+ Assertions.assertEquals(table.name(), tableInfo.name());
+ Assertions.assertEquals(table.properties(), tableInfo.properties());
+ Assertions.assertEquals(table.comment(), tableInfo.comment());
+ Assertions.assertArrayEquals(table.columns(), tableInfo.columns());
+ Assertions.assertArrayEquals(table.partitioning(), tableInfo.partitioning());
+ Assertions.assertEquals(table.distribution(), tableInfo.distribution());
+ Assertions.assertArrayEquals(table.sortOrder(), tableInfo.sortOrder());
+ Assertions.assertArrayEquals(table.index(), tableInfo.index());
+ Assertions.assertEquals(table.auditInfo(), tableInfo.auditInfo());
+ }
+
+ private Table mockTable() {
+ Table table = mock(Table.class);
+ when(table.name()).thenReturn("table");
+ when(table.comment()).thenReturn("comment");
+ when(table.properties()).thenReturn(ImmutableMap.of("a", "b"));
+ when(table.columns()).thenReturn(new Column[] {Column.of("a", Types.IntegerType.get())});
+ when(table.distribution())
+ .thenReturn(Distributions.of(Strategy.HASH, 10, NamedReference.field("a")));
+ when(table.index())
+ .thenReturn(new Index[] {Indexes.primary("p", new String[][] {{"a"}, {"b"}})});
+ when(table.sortOrder())
+ .thenReturn(new SortOrder[] {SortOrders.ascending(NamedReference.field("a"))});
+ when(table.partitioning()).thenReturn(new Transform[] {Transforms.identity("a")});
+ when(table.auditInfo()).thenReturn(null);
+ return table;
+ }
+
+ private TableDispatcher mockTableDispatcher() {
+ TableDispatcher dispatcher = mock(TableDispatcher.class);
+ when(dispatcher.createTable(
+ any(NameIdentifier.class),
+ any(Column[].class),
+ any(String.class),
+ any(Map.class),
+ any(Transform[].class),
+ any(Distribution.class),
+ any(SortOrder[].class),
+ any(Index[].class)))
+ .thenReturn(table);
+ when(dispatcher.loadTable(any(NameIdentifier.class))).thenReturn(table);
+ when(dispatcher.dropTable(any(NameIdentifier.class))).thenReturn(true);
+ when(dispatcher.purgeTable(any(NameIdentifier.class))).thenReturn(true);
+ when(dispatcher.listTables(any(Namespace.class))).thenReturn(null);
+ when(dispatcher.alterTable(any(NameIdentifier.class), any(TableChange.class)))
+ .thenReturn(table);
+ return dispatcher;
+ }
+
+ private TableDispatcher mockExceptionTableDispatcher() {
+ TableDispatcher dispatcher =
+ mock(
+ TableDispatcher.class,
+ invocation -> {
+ throw new GravitinoRuntimeException("Exception for all methods");
+ });
+ return dispatcher;
+ }
+}
diff --git a/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestTopicEvent.java b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestTopicEvent.java
new file mode 100644
index 0000000000..557c52a45e
--- /dev/null
+++ b/core/src/test/java/com/datastrato/gravitino/listener/api/event/TestTopicEvent.java
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.listener.api.event;
+
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.catalog.TopicDispatcher;
+import com.datastrato.gravitino.catalog.TopicEventDispatcher;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.listener.DummyEventListener;
+import com.datastrato.gravitino.listener.EventBus;
+import com.datastrato.gravitino.listener.api.info.TopicInfo;
+import com.datastrato.gravitino.messaging.Topic;
+import com.datastrato.gravitino.messaging.TopicChange;
+import com.google.common.collect.ImmutableMap;
+import java.util.Arrays;
+import java.util.Map;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestInstance.Lifecycle;
+
+@TestInstance(Lifecycle.PER_CLASS)
+public class TestTopicEvent {
+ private TopicEventDispatcher dispatcher;
+ private TopicEventDispatcher failureDispatcher;
+ private DummyEventListener dummyEventListener;
+ private Topic topic;
+
+ @BeforeAll
+ void init() {
+ this.topic = mockTopic();
+ this.dummyEventListener = new DummyEventListener();
+ EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener));
+ TopicDispatcher topicDispatcher = mockTopicDispatcher();
+ this.dispatcher = new TopicEventDispatcher(eventBus, topicDispatcher);
+ TopicDispatcher topicExceptionDispatcher = mockExceptionTopicDispatcher();
+ this.failureDispatcher = new TopicEventDispatcher(eventBus, topicExceptionDispatcher);
+ }
+
+ @Test
+ void testCreateTopicEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "topic");
+ dispatcher.createTopic(identifier, topic.comment(), null, topic.properties());
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateTopicEvent.class, event.getClass());
+ TopicInfo topicInfo = ((CreateTopicEvent) event).createdTopicInfo();
+ checkTopicInfo(topicInfo, topic);
+ }
+
+ @Test
+ void testLoadTopicEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "topic");
+ dispatcher.loadTopic(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadTopicEvent.class, event.getClass());
+ TopicInfo topicInfo = ((LoadTopicEvent) event).loadedTopicInfo();
+ checkTopicInfo(topicInfo, topic);
+ }
+
+ @Test
+ void testAlterTopicEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "topic");
+ TopicChange topicChange = TopicChange.setProperty("a", "b");
+ dispatcher.alterTopic(identifier, topicChange);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterTopicEvent.class, event.getClass());
+ TopicInfo topicInfo = ((AlterTopicEvent) event).updatedTopicInfo();
+ checkTopicInfo(topicInfo, topic);
+ Assertions.assertEquals(1, ((AlterTopicEvent) event).topicChanges().length);
+ Assertions.assertEquals(topicChange, ((AlterTopicEvent) event).topicChanges()[0]);
+ }
+
+ @Test
+ void testDropTopicEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "topic");
+ dispatcher.dropTopic(identifier);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropTopicEvent.class, event.getClass());
+ Assertions.assertEquals(true, ((DropTopicEvent) event).isExists());
+ }
+
+ @Test
+ void testListTopicEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ dispatcher.listTopics(namespace);
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListTopicEvent.class, event.getClass());
+ Assertions.assertEquals(namespace, ((ListTopicEvent) event).namespace());
+ }
+
+ @Test
+ void testCreateTopicFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "topic");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.createTopic(identifier, topic.comment(), null, topic.properties()));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(CreateTopicFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((CreateTopicFailureEvent) event).exception().getClass());
+ checkTopicInfo(((CreateTopicFailureEvent) event).createTopicRequest(), topic);
+ }
+
+ @Test
+ void testLoadTopicFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "topic");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.loadTopic(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(LoadTopicFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((LoadTopicFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testAlterTopicFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "topic");
+ TopicChange topicChange = TopicChange.setProperty("a", "b");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.alterTopic(identifier, topicChange));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(AlterTopicFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((AlterTopicFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(1, ((AlterTopicFailureEvent) event).topicChanges().length);
+ Assertions.assertEquals(topicChange, ((AlterTopicFailureEvent) event).topicChanges()[0]);
+ }
+
+ @Test
+ void testDropTopicFailureEvent() {
+ NameIdentifier identifier = NameIdentifier.of("metalake", "catalog", "topic");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.dropTopic(identifier));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(identifier, event.identifier());
+ Assertions.assertEquals(DropTopicFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((DropTopicFailureEvent) event).exception().getClass());
+ }
+
+ @Test
+ void testListTopicFailureEvent() {
+ Namespace namespace = Namespace.of("metalake", "catalog");
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.listTopics(namespace));
+ Event event = dummyEventListener.popEvent();
+ Assertions.assertEquals(namespace.toString(), event.identifier().toString());
+ Assertions.assertEquals(ListTopicFailureEvent.class, event.getClass());
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class, ((ListTopicFailureEvent) event).exception().getClass());
+ Assertions.assertEquals(namespace, ((ListTopicFailureEvent) event).namespace());
+ }
+
+ private void checkTopicInfo(TopicInfo topicInfo, Topic topic) {
+ Assertions.assertEquals(topic.name(), topicInfo.name());
+ Assertions.assertEquals(topic.properties(), topicInfo.properties());
+ Assertions.assertEquals(topic.comment(), topicInfo.comment());
+ }
+
+ private Topic mockTopic() {
+ Topic topic = mock(Topic.class);
+ when(topic.comment()).thenReturn("comment");
+ when(topic.properties()).thenReturn(ImmutableMap.of("a", "b"));
+ when(topic.name()).thenReturn("topic");
+ when(topic.auditInfo()).thenReturn(null);
+ return topic;
+ }
+
+ private TopicDispatcher mockTopicDispatcher() {
+ TopicDispatcher dispatcher = mock(TopicDispatcher.class);
+ when(dispatcher.createTopic(
+ any(NameIdentifier.class), any(String.class), isNull(), any(Map.class)))
+ .thenReturn(topic);
+ when(dispatcher.loadTopic(any(NameIdentifier.class))).thenReturn(topic);
+ when(dispatcher.dropTopic(any(NameIdentifier.class))).thenReturn(true);
+ when(dispatcher.listTopics(any(Namespace.class))).thenReturn(null);
+ when(dispatcher.alterTopic(any(NameIdentifier.class), any(TopicChange.class)))
+ .thenReturn(topic);
+ return dispatcher;
+ }
+
+ private TopicDispatcher mockExceptionTopicDispatcher() {
+ TopicDispatcher dispatcher =
+ mock(
+ TopicDispatcher.class,
+ invocation -> {
+ throw new GravitinoRuntimeException("Exception for all methods");
+ });
+ return dispatcher;
+ }
+}