diff --git a/core/src/main/java/org/apache/gravitino/listener/TagEventDispatcher.java b/core/src/main/java/org/apache/gravitino/listener/TagEventDispatcher.java index 3f14f055ad6..da0052ea73e 100644 --- a/core/src/main/java/org/apache/gravitino/listener/TagEventDispatcher.java +++ b/core/src/main/java/org/apache/gravitino/listener/TagEventDispatcher.java @@ -22,27 +22,26 @@ import org.apache.gravitino.MetadataObject; import org.apache.gravitino.exceptions.NoSuchTagException; import org.apache.gravitino.listener.api.event.AlterTagEvent; +import org.apache.gravitino.listener.api.event.AlterTagFailureEvent; import org.apache.gravitino.listener.api.event.AssociateTagsForMetadataObjectEvent; +import org.apache.gravitino.listener.api.event.AssociateTagsForMetadataObjectFailureEvent; import org.apache.gravitino.listener.api.event.CreateTagEvent; +import org.apache.gravitino.listener.api.event.CreateTagFailureEvent; import org.apache.gravitino.listener.api.event.DeleteTagEvent; +import org.apache.gravitino.listener.api.event.DeleteTagFailureEvent; import org.apache.gravitino.listener.api.event.GetTagEvent; +import org.apache.gravitino.listener.api.event.GetTagFailureEvent; import org.apache.gravitino.listener.api.event.GetTagForMetadataObjectEvent; +import org.apache.gravitino.listener.api.event.GetTagForMetadataObjectFailureEvent; import org.apache.gravitino.listener.api.event.ListMetadataObjectsForTagEvent; +import org.apache.gravitino.listener.api.event.ListMetadataObjectsForTagFailureEvent; import org.apache.gravitino.listener.api.event.ListTagEvent; import org.apache.gravitino.listener.api.event.ListTagInfoEvent; -import org.apache.gravitino.listener.api.event.ListTagsForMetadataObjectEvent; -import org.apache.gravitino.listener.api.event.ListTagsInfoForMetadataObjectEvent; -import org.apache.gravitino.listener.api.info.TagInfo; -import org.apache.gravitino.listener.api.event.AlterTagFailureEvent; -import org.apache.gravitino.listener.api.event.AssociateTagsForMetadataObjectFailureEvent; -import org.apache.gravitino.listener.api.event.CreateTagFailureEvent; -import org.apache.gravitino.listener.api.event.DeleteTagFailureEvent; -import org.apache.gravitino.listener.api.event.GetTagFailureEvent; -import org.apache.gravitino.listener.api.event.GetTagForMetadataObjectFailureEvent; -import org.apache.gravitino.listener.api.event.ListMetadataObjectsForTagFailureEvent; import org.apache.gravitino.listener.api.event.ListTagsFailureEvent; +import org.apache.gravitino.listener.api.event.ListTagsForMetadataObjectEvent; import org.apache.gravitino.listener.api.event.ListTagsForMetadataObjectFailureEvent; import org.apache.gravitino.listener.api.event.ListTagsInfoFailureEvent; +import org.apache.gravitino.listener.api.event.ListTagsInfoForMetadataObjectEvent; import org.apache.gravitino.listener.api.event.ListTagsInfoForMetadataObjectFailureEvent; import org.apache.gravitino.listener.api.info.TagInfo; import org.apache.gravitino.tag.Tag; @@ -74,7 +73,7 @@ public String[] listTags(String metalake) { return tagNames; } catch (Exception e) { eventBus.dispatchEvent( - new ListTagsFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, e)); + new ListTagsFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, e)); throw e; } } @@ -85,11 +84,11 @@ public Tag[] listTagsInfo(String metalake) { try { Tag[] tags = dispatcher.listTagsInfo(metalake); eventBus.dispatchEvent( - new ListTagInfoEvent(PrincipalUtils.getCurrentUserName(), metalake, tags)); + new ListTagInfoEvent(PrincipalUtils.getCurrentUserName(), metalake, tags)); return tags; } catch (Exception e) { eventBus.dispatchEvent( - new ListTagsInfoFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, e)); + new ListTagsInfoFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, e)); throw e; } } @@ -100,31 +99,31 @@ public Tag getTag(String metalake, String name) throws NoSuchTagException { try { Tag tag = dispatcher.getTag(metalake, name); eventBus.dispatchEvent( - new GetTagEvent(PrincipalUtils.getCurrentUserName(), metalake, name, tag)); + new GetTagEvent(PrincipalUtils.getCurrentUserName(), metalake, name, tag)); return tag; } catch (Exception e) { eventBus.dispatchEvent( - new GetTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, name, e)); + new GetTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, name, e)); throw e; } } @Override public Tag createTag( - String metalake, String name, String comment, Map properties) { + String metalake, String name, String comment, Map properties) { TagInfo tagInfo = new TagInfo(name, comment, properties); // TODO: createTagPreEvent try { Tag tag = dispatcher.createTag(metalake, name, comment, properties); eventBus.dispatchEvent( - new CreateTagEvent( - PrincipalUtils.getCurrentUserName(), - metalake, - new TagInfo(tag.name(), tag.comment(), tag.properties()))); + new CreateTagEvent( + PrincipalUtils.getCurrentUserName(), + metalake, + new TagInfo(tag.name(), tag.comment(), tag.properties()))); return tag; } catch (Exception e) { eventBus.dispatchEvent( - new CreateTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, tagInfo, e)); + new CreateTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, tagInfo, e)); throw e; } } @@ -135,16 +134,16 @@ public Tag alterTag(String metalake, String name, TagChange... changes) { try { Tag tag = dispatcher.alterTag(metalake, name, changes); eventBus.dispatchEvent( - new AlterTagEvent( - PrincipalUtils.getCurrentUserName(), - metalake, - changes, - new TagInfo(tag.name(), tag.comment(), tag.properties()))); + new AlterTagEvent( + PrincipalUtils.getCurrentUserName(), + metalake, + changes, + new TagInfo(tag.name(), tag.comment(), tag.properties()))); return tag; } catch (Exception e) { eventBus.dispatchEvent( - new AlterTagFailureEvent( - PrincipalUtils.getCurrentUserName(), metalake, name, changes, e)); + new AlterTagFailureEvent( + PrincipalUtils.getCurrentUserName(), metalake, name, changes, e)); throw e; } } @@ -155,11 +154,11 @@ public boolean deleteTag(String metalake, String name) { try { boolean isExists = dispatcher.deleteTag(metalake, name); eventBus.dispatchEvent( - new DeleteTagEvent(PrincipalUtils.getCurrentUserName(), metalake, isExists)); + new DeleteTagEvent(PrincipalUtils.getCurrentUserName(), metalake, isExists)); return isExists; } catch (Exception e) { eventBus.dispatchEvent( - new DeleteTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, name, e)); + new DeleteTagFailureEvent(PrincipalUtils.getCurrentUserName(), metalake, name, e)); throw e; } } @@ -170,13 +169,13 @@ public MetadataObject[] listMetadataObjectsForTag(String metalake, String name) try { MetadataObject[] metadataObjects = dispatcher.listMetadataObjectsForTag(metalake, name); eventBus.dispatchEvent( - new ListMetadataObjectsForTagEvent( - PrincipalUtils.getCurrentUserName(), metalake, name, metadataObjects)); + new ListMetadataObjectsForTagEvent( + PrincipalUtils.getCurrentUserName(), metalake, name, metadataObjects)); return metadataObjects; } catch (Exception e) { eventBus.dispatchEvent( - new ListMetadataObjectsForTagFailureEvent( - PrincipalUtils.getCurrentUserName(), metalake, name, e)); + new ListMetadataObjectsForTagFailureEvent( + PrincipalUtils.getCurrentUserName(), metalake, name, e)); throw e; } } @@ -187,13 +186,13 @@ public String[] listTagsForMetadataObject(String metalake, MetadataObject metada try { String[] tags = dispatcher.listTagsForMetadataObject(metalake, metadataObject); eventBus.dispatchEvent( - new ListTagsForMetadataObjectEvent( - PrincipalUtils.getCurrentUserName(), metalake, metadataObject, tags)); + new ListTagsForMetadataObjectEvent( + PrincipalUtils.getCurrentUserName(), metalake, metadataObject, tags)); return tags; } catch (Exception e) { eventBus.dispatchEvent( - new ListTagsForMetadataObjectFailureEvent( - PrincipalUtils.getCurrentUserName(), metalake, metadataObject, e)); + new ListTagsForMetadataObjectFailureEvent( + PrincipalUtils.getCurrentUserName(), metalake, metadataObject, e)); throw e; } } @@ -204,43 +203,43 @@ public Tag[] listTagsInfoForMetadataObject(String metalake, MetadataObject metad try { Tag[] tags = dispatcher.listTagsInfoForMetadataObject(metalake, metadataObject); eventBus.dispatchEvent( - new ListTagsInfoForMetadataObjectEvent( - PrincipalUtils.getCurrentUserName(), metalake, metadataObject, tags)); + new ListTagsInfoForMetadataObjectEvent( + PrincipalUtils.getCurrentUserName(), metalake, metadataObject, tags)); return tags; } catch (Exception e) { eventBus.dispatchEvent( - new ListTagsInfoForMetadataObjectFailureEvent( - PrincipalUtils.getCurrentUserName(), metalake, metadataObject, e)); + new ListTagsInfoForMetadataObjectFailureEvent( + PrincipalUtils.getCurrentUserName(), metalake, metadataObject, e)); throw e; } } @Override public String[] associateTagsForMetadataObject( - String metalake, MetadataObject metadataObject, String[] tagsToAdd, String[] tagsToRemove) { + String metalake, MetadataObject metadataObject, String[] tagsToAdd, String[] tagsToRemove) { // TODO: associateTagsForMetadataObjectPreEvent try { String[] associatedTags = - dispatcher.associateTagsForMetadataObject( - metalake, metadataObject, tagsToAdd, tagsToRemove); + dispatcher.associateTagsForMetadataObject( + metalake, metadataObject, tagsToAdd, tagsToRemove); eventBus.dispatchEvent( - new AssociateTagsForMetadataObjectEvent( - PrincipalUtils.getCurrentUserName(), - metalake, - metadataObject, - tagsToAdd, - tagsToRemove, - associatedTags)); + new AssociateTagsForMetadataObjectEvent( + PrincipalUtils.getCurrentUserName(), + metalake, + metadataObject, + tagsToAdd, + tagsToRemove, + associatedTags)); return associatedTags; } catch (Exception e) { eventBus.dispatchEvent( - new AssociateTagsForMetadataObjectFailureEvent( - PrincipalUtils.getCurrentUserName(), - metalake, - metadataObject, - tagsToAdd, - tagsToRemove, - e)); + new AssociateTagsForMetadataObjectFailureEvent( + PrincipalUtils.getCurrentUserName(), + metalake, + metadataObject, + tagsToAdd, + tagsToRemove, + e)); throw e; } } @@ -251,14 +250,14 @@ public Tag getTagForMetadataObject(String metalake, MetadataObject metadataObjec try { Tag tag = dispatcher.getTagForMetadataObject(metalake, metadataObject, name); eventBus.dispatchEvent( - new GetTagForMetadataObjectEvent( - PrincipalUtils.getCurrentUserName(), metalake, metadataObject, name, tag)); + new GetTagForMetadataObjectEvent( + PrincipalUtils.getCurrentUserName(), metalake, metadataObject, name, tag)); return tag; } catch (Exception e) { eventBus.dispatchEvent( - new GetTagForMetadataObjectFailureEvent( - PrincipalUtils.getCurrentUserName(), metalake, metadataObject, name, e)); + new GetTagForMetadataObjectFailureEvent( + PrincipalUtils.getCurrentUserName(), metalake, metadataObject, name, e)); throw e; } } -} \ No newline at end of file +} diff --git a/core/src/test/java/org/apache/gravitino/listener/api/event/TestTagEvent.java b/core/src/test/java/org/apache/gravitino/listener/api/event/TestTagEvent.java index 16b781fbc33..ed982faa7b6 100644 --- a/core/src/test/java/org/apache/gravitino/listener/api/event/TestTagEvent.java +++ b/core/src/test/java/org/apache/gravitino/listener/api/event/TestTagEvent.java @@ -20,7 +20,6 @@ package org.apache.gravitino.listener.api.event; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -28,10 +27,8 @@ import com.google.common.collect.ImmutableMap; import java.util.Arrays; import java.util.Map; - import org.apache.gravitino.Entity; import org.apache.gravitino.MetadataObject; -import org.apache.gravitino.NameIdentifier; import org.apache.gravitino.exceptions.GravitinoRuntimeException; import org.apache.gravitino.listener.DummyEventListener; import org.apache.gravitino.listener.EventBus; @@ -49,307 +46,421 @@ @TestInstance(Lifecycle.PER_CLASS) public class TestTagEvent { - private TagEventDispatcher dispatcher; - private TagEventDispatcher failureDispatcher; - private DummyEventListener dummyEventListener; - private Tag tag; - - @BeforeAll - void init() { - this.tag = mockTag(); - this.dummyEventListener = new DummyEventListener(); - EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener)); - TagDispatcher tagDispatcher = mockTagDispatcher(); - this.dispatcher = new TagEventDispatcher(eventBus, tagDispatcher); - TagDispatcher tagExceptionDispatcher = mockExceptionTagDispatcher(); - this.failureDispatcher = new TagEventDispatcher(eventBus, tagExceptionDispatcher); - } - - @Test - void testCreateTagEvent() { - dispatcher.createTag("metalake", tag.name(), tag.comment(), tag.properties()); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(CreateTagEvent.class, event.getClass()); - TagInfo tagInfo = ((CreateTagEvent) event).createdTagInfo(); - checkTagInfo(tagInfo, tag); - Assertions.assertEquals(OperationType.CREATE_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); - } - @Test - void testGetTagEvent() { - String metalake = "metalake"; - - Tag result = dispatcher.getTag(metalake, tag.name()); - Event event = dummyEventListener.popPostEvent(); - - Assertions.assertEquals(GetTagEvent.class, event.getClass()); - Assertions.assertEquals(tag.name(), ((GetTagEvent) event).tagName()); - Assertions.assertEquals(OperationType.GET_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); - } - - @Test - void testListTagEvent() { - String[] result = dispatcher.listTags("metalake"); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(ListTagEvent.class, event.getClass()); - Assertions.assertEquals("metalake", ((ListTagEvent)event).metalake()); - Assertions.assertEquals(OperationType.LIST_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); - } - - @Test - void testListTagInfoEvent() { - Tag[] result = dispatcher.listTagsInfo("metalake"); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(ListTagInfoEvent.class, event.getClass()); - Assertions.assertEquals(OperationType.LISTINFO_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); - Assertions.assertEquals(result, ((ListTagInfoEvent)event).tags()); - } - - @Test - void testAlterTagEvent() { - TagChange change1 = TagChange.rename("newName"); - TagChange change2 = TagChange.updateComment("new comment"); - TagChange change3 = TagChange.setProperty("key", "value"); - TagChange change4 = TagChange.removeProperty("oldKey"); - TagChange[] changes = new TagChange[] {change1, change2, change3, change4}; - dispatcher.alterTag("metalake", tag.name(), changes); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(AlterTagEvent.class, event.getClass()); - TagInfo tagInfo = ((AlterTagEvent) event).updatedTagInfo(); - checkTagInfo(tagInfo, tag); - TagChange[] tagChanges = ((AlterTagEvent) event).tagChanges(); - Assertions.assertEquals(4, tagChanges.length); - Assertions.assertEquals(change1, tagChanges[0]); - Assertions.assertEquals(change2, tagChanges[1]); - Assertions.assertEquals(change3, tagChanges[2]); - Assertions.assertEquals(change4, tagChanges[3]); - Assertions.assertEquals(OperationType.ALTER_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); - } - @Test - void testDeleteTagEvent() { - boolean result = dispatcher.deleteTag("metalake", tag.name()); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(DeleteTagEvent.class, event.getClass()); - Assertions.assertEquals("metalake", ((DeleteTagEvent) event).metalake()); - Assertions.assertEquals(tag.name(), ((DeleteTagEvent) event).tagName()); - Assertions.assertEquals(OperationType.DELETE_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); - Assertions.assertTrue(result); - } - - - @Test - void testCreateTagFailureEvent() { - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, - () -> failureDispatcher.createTag("metalake", tag.name(), tag.comment(), tag.properties())); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(CreateTagFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, ((CreateTagFailureEvent) event).exception().getClass()); - Assertions.assertEquals(tag.name(), ((CreateTagFailureEvent) event).tagInfo().name()); - Assertions.assertEquals(tag.comment(), ((CreateTagFailureEvent) event).tagInfo().comment()); - Assertions.assertEquals( - tag.properties(), ((CreateTagFailureEvent) event).tagInfo().properties()); - Assertions.assertEquals(OperationType.CREATE_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testGetTagFailureEvent() { - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, () -> failureDispatcher.getTag("metalake", tag.name())); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(GetTagFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, ((GetTagFailureEvent) event).exception().getClass()); - Assertions.assertEquals(OperationType.GET_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testGetTagForMetadataObjectFailureEvent() { - MetadataObject metadataObject = - NameIdentifierUtil.toMetadataObject( - NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), - Entity.EntityType.CATALOG); - - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, - () -> failureDispatcher.getTagForMetadataObject("metalake", metadataObject, tag.name())); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(GetTagForMetadataObjectFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, - ((GetTagForMetadataObjectFailureEvent) event).exception().getClass()); - Assertions.assertEquals(OperationType.GET_TAG_FOR_METADATA_OBJECT, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testDeleteTagFailureEvent() { - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, () -> failureDispatcher.deleteTag("metalake", tag.name())); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(DeleteTagFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, ((DeleteTagFailureEvent) event).exception().getClass()); - Assertions.assertEquals(OperationType.DELETE_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testAlterTagFailureEvent() { - TagChange change1 = TagChange.rename("newName"); - TagChange change2 = TagChange.updateComment("new comment"); - TagChange change3 = TagChange.setProperty("key", "value"); - TagChange change4 = TagChange.removeProperty("oldKey"); - TagChange[] changes = new TagChange[] {change1, change2, change3, change4}; - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, - () -> failureDispatcher.alterTag("metalake", tag.name(), changes)); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(AlterTagFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, ((AlterTagFailureEvent) event).exception().getClass()); - Assertions.assertEquals(changes, ((AlterTagFailureEvent) event).changes()); - Assertions.assertEquals(OperationType.ALTER_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testListTagFailureEvent() { - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, () -> failureDispatcher.listTags("metalake")); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(ListTagsFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, ((ListTagsFailureEvent) event).exception().getClass()); - Assertions.assertEquals(OperationType.LIST_TAG, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testListTagsForMetadataObjectFailureEvent() { - MetadataObject metadataObject = - NameIdentifierUtil.toMetadataObject( - NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), - Entity.EntityType.CATALOG); - - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, - () -> failureDispatcher.listTagsForMetadataObject("metalake", metadataObject)); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(ListTagsForMetadataObjectFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, - ((ListTagsForMetadataObjectFailureEvent) event).exception().getClass()); - Assertions.assertEquals(OperationType.LIST_TAGS_FOR_METADATA_OBJECT, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testListTagsInfoFailureEvent() { - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, () -> failureDispatcher.listTagsInfo("metalake")); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(ListTagsInfoFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, ((ListTagsInfoFailureEvent) event).exception().getClass()); - Assertions.assertEquals(OperationType.LIST_TAGS_INFO, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testListTagsInfoForMetadataObjectFailureEvent() { - MetadataObject metadataObject = - NameIdentifierUtil.toMetadataObject( - NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), - Entity.EntityType.CATALOG); - - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, - () -> failureDispatcher.listTagsInfoForMetadataObject("metalake", metadataObject)); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(ListTagsInfoForMetadataObjectFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, - ((ListTagsInfoForMetadataObjectFailureEvent) event).exception().getClass()); - Assertions.assertEquals( - OperationType.LIST_TAGS_INFO_FOR_METADATA_OBJECT, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - @Test - void testAssociateTagsForMetadataObjectFailureEvent() { - MetadataObject metadataObject = - NameIdentifierUtil.toMetadataObject( - NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), - Entity.EntityType.CATALOG); - - String[] tagsToAssociate = new String[] {"tag1", "tag2"}; - String[] tagsToDisassociate = new String[] {"tag3", "tag4"}; - - Assertions.assertThrowsExactly( - GravitinoRuntimeException.class, - () -> - failureDispatcher.associateTagsForMetadataObject( - "metalake", metadataObject, tagsToAssociate, tagsToDisassociate)); - Event event = dummyEventListener.popPostEvent(); - Assertions.assertEquals(AssociateTagsForMetadataObjectFailureEvent.class, event.getClass()); - Assertions.assertEquals( - GravitinoRuntimeException.class, - ((AssociateTagsForMetadataObjectFailureEvent) event).exception().getClass()); - Assertions.assertEquals( - tagsToAssociate, ((AssociateTagsForMetadataObjectFailureEvent) event).tagsToAdd()); - Assertions.assertEquals( - tagsToDisassociate, ((AssociateTagsForMetadataObjectFailureEvent) event).tagsToRemove()); - Assertions.assertEquals( - OperationType.ASSOCIATE_TAGS_FOR_METADATA_OBJECT, event.operationType()); - Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); - } - - private void checkTagInfo(TagInfo actualTagInfo, Tag expectedTag) { - Assertions.assertEquals(expectedTag.name(), actualTagInfo.name()); - Assertions.assertEquals(expectedTag.comment(), actualTagInfo.comment()); - Assertions.assertEquals(expectedTag.properties(), actualTagInfo.properties()); - } - - - private Tag mockTag() { - Tag tag = mock(Tag.class); - when(tag.name()).thenReturn("tag"); - when(tag.comment()).thenReturn("comment"); - when(tag.properties()).thenReturn(ImmutableMap.of("color", "#FFFFFF")); - return tag; - } - - private TagDispatcher mockTagDispatcher() { - TagDispatcher dispatcher = mock(TagDispatcher.class); - String metalake = "metalake"; - String[] tagNames = new String[] {"tag1", "tag2"}; - Tag[] tags = new Tag[] {tag, tag}; - - when(dispatcher.createTag( - any(String.class), any(String.class), any(String.class), any(Map.class))) - .thenReturn(tag); - when(dispatcher.listTags(metalake)).thenReturn(tagNames); - when(dispatcher.listTagsInfo(metalake)).thenReturn(tags); - when(dispatcher.alterTag(any(String.class), any(String.class), any(TagChange[].class))).thenReturn(tag); - when(dispatcher.getTag(any(String.class), any(String.class))).thenReturn(tag); - when(dispatcher.deleteTag(metalake, tag.name())).thenReturn(true); - return dispatcher; - } - - private TagDispatcher mockExceptionTagDispatcher() { - return mock( - TagDispatcher.class, - invocation -> { - throw new GravitinoRuntimeException("Exception for all methods"); - }); - } -} \ No newline at end of file + private TagEventDispatcher dispatcher; + private TagEventDispatcher failureDispatcher; + private DummyEventListener dummyEventListener; + private Tag tag; + + @BeforeAll + void init() { + this.tag = mockTag(); + this.dummyEventListener = new DummyEventListener(); + EventBus eventBus = new EventBus(Arrays.asList(dummyEventListener)); + TagDispatcher tagDispatcher = mockTagDispatcher(); + this.dispatcher = new TagEventDispatcher(eventBus, tagDispatcher); + TagDispatcher tagExceptionDispatcher = mockExceptionTagDispatcher(); + this.failureDispatcher = new TagEventDispatcher(eventBus, tagExceptionDispatcher); + } + + @Test + void testCreateTagEvent() { + dispatcher.createTag("metalake", tag.name(), tag.comment(), tag.properties()); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(CreateTagEvent.class, event.getClass()); + TagInfo tagInfo = ((CreateTagEvent) event).createdTagInfo(); + checkTagInfo(tagInfo, tag); + Assertions.assertEquals(OperationType.CREATE_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testGetTagEvent() { + String metalake = "metalake"; + + dispatcher.getTag(metalake, tag.name()); + Event event = dummyEventListener.popPostEvent(); + + Assertions.assertEquals(GetTagEvent.class, event.getClass()); + Assertions.assertEquals(tag.name(), ((GetTagEvent) event).tagName()); + Assertions.assertEquals(OperationType.GET_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testListTagEvent() { + dispatcher.listTags("metalake"); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(ListTagEvent.class, event.getClass()); + Assertions.assertEquals("metalake", ((ListTagEvent) event).metalake()); + Assertions.assertEquals(OperationType.LIST_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testListTagInfoEvent() { + Tag[] result = dispatcher.listTagsInfo("metalake"); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(ListTagInfoEvent.class, event.getClass()); + Assertions.assertEquals(OperationType.LISTINFO_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + Assertions.assertEquals(result, ((ListTagInfoEvent) event).tags()); + } + + @Test + void testAlterTagEvent() { + TagChange change1 = TagChange.rename("newName"); + TagChange change2 = TagChange.updateComment("new comment"); + TagChange change3 = TagChange.setProperty("key", "value"); + TagChange change4 = TagChange.removeProperty("oldKey"); + TagChange[] changes = new TagChange[] {change1, change2, change3, change4}; + dispatcher.alterTag("metalake", tag.name(), changes); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(AlterTagEvent.class, event.getClass()); + TagInfo tagInfo = ((AlterTagEvent) event).updatedTagInfo(); + checkTagInfo(tagInfo, tag); + TagChange[] tagChanges = ((AlterTagEvent) event).tagChanges(); + Assertions.assertEquals(4, tagChanges.length); + Assertions.assertEquals(change1, tagChanges[0]); + Assertions.assertEquals(change2, tagChanges[1]); + Assertions.assertEquals(change3, tagChanges[2]); + Assertions.assertEquals(change4, tagChanges[3]); + Assertions.assertEquals(OperationType.ALTER_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testDeleteTagEvent() { + dispatcher.deleteTag("metalake", tag.name()); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(DeleteTagEvent.class, event.getClass()); + Assertions.assertEquals(OperationType.DELETE_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testGetTagForMetadataObjectEvent() { + MetadataObject metadataObject = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + + dispatcher.getTagForMetadataObject("metalake", metadataObject, tag.name()); + Event event = dummyEventListener.popPostEvent(); + + Assertions.assertEquals(GetTagForMetadataObjectEvent.class, event.getClass()); + Assertions.assertEquals("metalake", ((GetTagForMetadataObjectEvent) event).metalake()); + Assertions.assertEquals(tag.name(), ((GetTagForMetadataObjectEvent) event).tagName()); + Assertions.assertEquals( + metadataObject, ((GetTagForMetadataObjectEvent) event).metadataObject()); + Assertions.assertEquals(OperationType.GET_TAG_FOR_METADATA_OBJECT, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testListMetadataObjectsForTagEvent() { + dispatcher.listMetadataObjectsForTag("metalake", tag.name()); + Event event = dummyEventListener.popPostEvent(); + + Assertions.assertEquals(ListMetadataObjectsForTagEvent.class, event.getClass()); + Assertions.assertEquals("metalake", ((ListMetadataObjectsForTagEvent) event).metalake()); + Assertions.assertEquals(tag.name(), ((ListMetadataObjectsForTagEvent) event).tagName()); + Assertions.assertEquals(OperationType.LIST_METADATA_OBJECTS_FOR_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testAssociateTagsForMetadataObjectEvent() { + MetadataObject metadataObject = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + String[] tagsToAssociate = new String[] {"tag1", "tag2"}; + String[] tagsToDisassociate = new String[] {"tag3", "tag4"}; + + dispatcher.associateTagsForMetadataObject( + "metalake", metadataObject, tagsToAssociate, tagsToDisassociate); + Event event = dummyEventListener.popPostEvent(); + + Assertions.assertEquals(AssociateTagsForMetadataObjectEvent.class, event.getClass()); + Assertions.assertEquals("metalake", ((AssociateTagsForMetadataObjectEvent) event).metalake()); + Assertions.assertEquals( + metadataObject, ((AssociateTagsForMetadataObjectEvent) event).metadataObject()); + Assertions.assertArrayEquals( + tagsToAssociate, ((AssociateTagsForMetadataObjectEvent) event).tagsToAdd()); + Assertions.assertArrayEquals( + tagsToDisassociate, ((AssociateTagsForMetadataObjectEvent) event).tagsToRemove()); + Assertions.assertEquals( + OperationType.ASSOCIATE_TAGS_FOR_METADATA_OBJECT, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testListTagsForMetadataObjectEvent() { + MetadataObject metadataObject = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + + dispatcher.listTagsForMetadataObject("metalake", metadataObject); + Event event = dummyEventListener.popPostEvent(); + + Assertions.assertEquals(ListTagsForMetadataObjectEvent.class, event.getClass()); + Assertions.assertEquals("metalake", ((ListTagsForMetadataObjectEvent) event).metalake()); + Assertions.assertEquals( + metadataObject, ((ListTagsForMetadataObjectEvent) event).metadataObject()); + Assertions.assertEquals(OperationType.LIST_TAGS_FOR_METADATA_OBJECT, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testListTagsInfoForMetadataObjectEvent() { + MetadataObject metadataObject = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + + dispatcher.listTagsInfoForMetadataObject("metalake", metadataObject); + Event event = dummyEventListener.popPostEvent(); + + Assertions.assertEquals(ListTagsInfoForMetadataObjectEvent.class, event.getClass()); + Assertions.assertEquals("metalake", ((ListTagsInfoForMetadataObjectEvent) event).metalake()); + Assertions.assertEquals( + metadataObject, ((ListTagsInfoForMetadataObjectEvent) event).metadataObject()); + Assertions.assertEquals( + OperationType.LIST_TAGS_INFO_FOR_METADATA_OBJECT, event.operationType()); + Assertions.assertEquals(OperationStatus.SUCCESS, event.operationStatus()); + } + + @Test + void testCreateTagFailureEvent() { + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.createTag("metalake", tag.name(), tag.comment(), tag.properties())); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(CreateTagFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, ((CreateTagFailureEvent) event).exception().getClass()); + Assertions.assertEquals(tag.name(), ((CreateTagFailureEvent) event).tagInfo().name()); + Assertions.assertEquals(tag.comment(), ((CreateTagFailureEvent) event).tagInfo().comment()); + Assertions.assertEquals( + tag.properties(), ((CreateTagFailureEvent) event).tagInfo().properties()); + Assertions.assertEquals(OperationType.CREATE_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testGetTagFailureEvent() { + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, () -> failureDispatcher.getTag("metalake", tag.name())); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(GetTagFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, ((GetTagFailureEvent) event).exception().getClass()); + Assertions.assertEquals(OperationType.GET_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testGetTagForMetadataObjectFailureEvent() { + MetadataObject metadataObject = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.getTagForMetadataObject("metalake", metadataObject, tag.name())); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(GetTagForMetadataObjectFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((GetTagForMetadataObjectFailureEvent) event).exception().getClass()); + Assertions.assertEquals(OperationType.GET_TAG_FOR_METADATA_OBJECT, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testDeleteTagFailureEvent() { + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, () -> failureDispatcher.deleteTag("metalake", tag.name())); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(DeleteTagFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, ((DeleteTagFailureEvent) event).exception().getClass()); + Assertions.assertEquals(OperationType.DELETE_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testAlterTagFailureEvent() { + TagChange change1 = TagChange.rename("newName"); + TagChange change2 = TagChange.updateComment("new comment"); + TagChange change3 = TagChange.setProperty("key", "value"); + TagChange change4 = TagChange.removeProperty("oldKey"); + TagChange[] changes = new TagChange[] {change1, change2, change3, change4}; + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.alterTag("metalake", tag.name(), changes)); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(AlterTagFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, ((AlterTagFailureEvent) event).exception().getClass()); + Assertions.assertEquals(changes, ((AlterTagFailureEvent) event).changes()); + Assertions.assertEquals(OperationType.ALTER_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testListTagFailureEvent() { + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, () -> failureDispatcher.listTags("metalake")); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(ListTagsFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, ((ListTagsFailureEvent) event).exception().getClass()); + Assertions.assertEquals(OperationType.LIST_TAG, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testListTagsForMetadataObjectFailureEvent() { + MetadataObject metadataObject = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.listTagsForMetadataObject("metalake", metadataObject)); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(ListTagsForMetadataObjectFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((ListTagsForMetadataObjectFailureEvent) event).exception().getClass()); + Assertions.assertEquals(OperationType.LIST_TAGS_FOR_METADATA_OBJECT, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testListTagsInfoFailureEvent() { + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, () -> failureDispatcher.listTagsInfo("metalake")); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(ListTagsInfoFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, ((ListTagsInfoFailureEvent) event).exception().getClass()); + Assertions.assertEquals(OperationType.LIST_TAGS_INFO, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testListTagsInfoForMetadataObjectFailureEvent() { + MetadataObject metadataObject = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> failureDispatcher.listTagsInfoForMetadataObject("metalake", metadataObject)); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(ListTagsInfoForMetadataObjectFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((ListTagsInfoForMetadataObjectFailureEvent) event).exception().getClass()); + Assertions.assertEquals( + OperationType.LIST_TAGS_INFO_FOR_METADATA_OBJECT, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + @Test + void testAssociateTagsForMetadataObjectFailureEvent() { + MetadataObject metadataObject = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + + String[] tagsToAssociate = new String[] {"tag1", "tag2"}; + String[] tagsToDisassociate = new String[] {"tag3", "tag4"}; + + Assertions.assertThrowsExactly( + GravitinoRuntimeException.class, + () -> + failureDispatcher.associateTagsForMetadataObject( + "metalake", metadataObject, tagsToAssociate, tagsToDisassociate)); + Event event = dummyEventListener.popPostEvent(); + Assertions.assertEquals(AssociateTagsForMetadataObjectFailureEvent.class, event.getClass()); + Assertions.assertEquals( + GravitinoRuntimeException.class, + ((AssociateTagsForMetadataObjectFailureEvent) event).exception().getClass()); + Assertions.assertEquals( + tagsToAssociate, ((AssociateTagsForMetadataObjectFailureEvent) event).tagsToAdd()); + Assertions.assertEquals( + tagsToDisassociate, ((AssociateTagsForMetadataObjectFailureEvent) event).tagsToRemove()); + Assertions.assertEquals( + OperationType.ASSOCIATE_TAGS_FOR_METADATA_OBJECT, event.operationType()); + Assertions.assertEquals(OperationStatus.FAILURE, event.operationStatus()); + } + + private void checkTagInfo(TagInfo actualTagInfo, Tag expectedTag) { + Assertions.assertEquals(expectedTag.name(), actualTagInfo.name()); + Assertions.assertEquals(expectedTag.comment(), actualTagInfo.comment()); + Assertions.assertEquals(expectedTag.properties(), actualTagInfo.properties()); + } + + private Tag mockTag() { + Tag tag = mock(Tag.class); + when(tag.name()).thenReturn("tag"); + when(tag.comment()).thenReturn("comment"); + when(tag.properties()).thenReturn(ImmutableMap.of("color", "#FFFFFF")); + return tag; + } + + private TagDispatcher mockTagDispatcher() { + TagDispatcher dispatcher = mock(TagDispatcher.class); + String metalake = "metalake"; + String[] tagNames = new String[] {"tag1", "tag2"}; + Tag[] tags = new Tag[] {tag, tag}; + + when(dispatcher.createTag( + any(String.class), any(String.class), any(String.class), any(Map.class))) + .thenReturn(tag); + when(dispatcher.listTags(metalake)).thenReturn(tagNames); + when(dispatcher.listTagsInfo(metalake)).thenReturn(tags); + when(dispatcher.alterTag(any(String.class), any(String.class), any(TagChange[].class))) + .thenReturn(tag); + when(dispatcher.getTag(any(String.class), any(String.class))).thenReturn(tag); + when(dispatcher.deleteTag(metalake, tag.name())).thenReturn(true); + when(dispatcher.getTagForMetadataObject( + any(String.class), any(MetadataObject.class), any(String.class))) + .thenReturn(tag); + MetadataObject catalog = + NameIdentifierUtil.toMetadataObject( + NameIdentifierUtil.ofCatalog("metalake", "catalog_for_test"), + Entity.EntityType.CATALOG); + MetadataObject[] objects = new MetadataObject[] {catalog}; + + when(dispatcher.listMetadataObjectsForTag(any(String.class), any(String.class))) + .thenReturn(objects); + + when(dispatcher.associateTagsForMetadataObject( + any(String.class), any(MetadataObject.class), any(String[].class), any(String[].class))) + .thenReturn(new String[] {"tag1", "tag2"}); + + when(dispatcher.listTagsForMetadataObject(any(String.class), any(MetadataObject.class))) + .thenReturn(new String[] {"tag1", "tag2"}); + + when(dispatcher.listTagsInfoForMetadataObject(any(String.class), any(MetadataObject.class))) + .thenReturn(new Tag[] {tag, tag}); + + return dispatcher; + } + + private TagDispatcher mockExceptionTagDispatcher() { + return mock( + TagDispatcher.class, + invocation -> { + throw new GravitinoRuntimeException("Exception for all methods"); + }); + } +}