Skip to content

Commit

Permalink
[apache#5746] feat(CLI): Support table format output for Audit command
Browse files Browse the repository at this point in the history
Support table format output for Audit command.
  • Loading branch information
Abyss-lord committed Feb 24, 2025
1 parent e8ac483 commit 98f7647
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -665,9 +665,10 @@ public String getOutput(Audit audit) {
Column columnModifyTime = new Column(context, "modify time");

columnCreator.addCell(audit.creator());
columnCreateTime.addCell(audit.createTime());
columnModified.addCell(audit.lastModifier());
columnModifyTime.addCell(audit.lastModifiedTime());
columnCreateTime.addCell(audit.createTime() == null ? "N/A" : audit.createTime().toString());
columnModified.addCell(audit.lastModifier() == null ? "N/A" : audit.lastModifier());
columnModifyTime.addCell(
audit.lastModifiedTime() == null ? "N/A" : audit.lastModifiedTime().toString());

return getTableFormat(columnCreator, columnCreateTime, columnModified, columnModifyTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,14 @@ void testListTableWithTableFormat() {
@Test
void testAuditWithTableFormat() {
CommandContext mockContext = getMockContext();
Audit mockAudit = getMockAudit();
Audit mockAudit = mock(Audit.class);
when(mockAudit.creator()).thenReturn("demo_user");
when(mockAudit.createTime()).thenReturn(Instant.ofEpochMilli(1611111111111L));
when(mockAudit.lastModifier()).thenReturn("demo_user");
when(mockAudit.lastModifiedTime()).thenReturn(Instant.ofEpochMilli(1611111111111L));

TableFormat.output(mockAudit, mockContext);

String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+-----------+--------------------------+-----------+--------------------------+\n"
Expand All @@ -390,6 +396,27 @@ void testAuditWithTableFormat() {
output);
}

@Test
void testAuditWithTableFormatWithNullValues() {
CommandContext mockContext = getMockContext();
Audit mockAudit = mock(Audit.class);
when(mockAudit.creator()).thenReturn("demo_user");
when(mockAudit.createTime()).thenReturn(null);
when(mockAudit.lastModifier()).thenReturn(null);
when(mockAudit.lastModifiedTime()).thenReturn(null);

TableFormat.output(mockAudit, mockContext);

String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+-----------+-------------+----------+-------------+\n"
+ "| Creator | Create time | Modified | Modify time |\n"
+ "+-----------+-------------+----------+-------------+\n"
+ "| demo_user | N/A | N/A | N/A |\n"
+ "+-----------+-------------+----------+-------------+",
output);
}

@Test
void testOutputWithUnsupportType() {
CommandContext mockContext = getMockContext();
Expand Down Expand Up @@ -478,14 +505,4 @@ private org.apache.gravitino.rel.Column getMockColumn(

return mockColumn;
}

private Audit getMockAudit() {
Audit mockAudit = mock(Audit.class);
when(mockAudit.creator()).thenReturn("demo_user");
when(mockAudit.createTime()).thenReturn(Instant.ofEpochMilli(1611111111111L));
when(mockAudit.lastModifier()).thenReturn("demo_user");
when(mockAudit.lastModifiedTime()).thenReturn(Instant.ofEpochMilli(1611111111111L));

return mockAudit;
}
}

0 comments on commit 98f7647

Please sign in to comment.