Skip to content

Commit

Permalink
Include audit fields in db info
Browse files Browse the repository at this point in the history
  • Loading branch information
insyncoss committed Feb 20, 2024
1 parent 216fac7 commit 2c9b8d4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
public abstract class BaseInfo implements Serializable {
private static final long serialVersionUID = 284049639636194327L;
/* Name of the resource */
private QualifiedName name;
protected QualifiedName name;
/* Audit information of the resource */
private AuditInfo audit;
protected AuditInfo audit;
/* Metadata properties of the resource */
private Map<String, String> metadata;
protected Map<String, String> metadata;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(of = {"name", "uri"})
public final class DatabaseInfo extends BaseInfo {
/* location of the database */
private String uri;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.netflix.metacat.connector.polaris.mappers;

import com.netflix.metacat.common.server.connectors.model.AuditInfo;
import com.netflix.metacat.connector.polaris.store.entities.AuditEntity;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Date;


/**
* Database object mapper implementations.
*/
public class AuditMapper implements
EntityToInfoMapper<AuditEntity, AuditInfo>,
InfoToEntityMapper<AuditInfo, AuditEntity> {

/**
* {@inheritDoc}.
*/
@Override
public AuditInfo toInfo(final AuditEntity entity) {
AuditInfo auditInfo = AuditInfo.builder()
.createdBy(entity.getCreatedBy())
.createdDate(Date.from(entity.getCreatedDate()))
.lastModifiedBy(entity.getLastModifiedBy())
.lastModifiedDate(Date.from(entity.getLastModifiedDate()))
.build();
return auditInfo;
}

/**
* {@inheritDoc}.
*/
@Override
public AuditEntity toEntity(final AuditInfo info) {
final AuditEntity entity = AuditEntity.builder()
.createdBy(info.getCreatedBy())
.createdDate(ObjectUtils.defaultIfNull(info.getCreatedDate(), new Date()).toInstant())
.lastModifiedBy(info.getLastModifiedBy())
.lastModifiedDate(ObjectUtils.defaultIfNull(info.getLastModifiedDate(), new Date()).toInstant())
.build();
return entity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public PolarisDatabaseMapper(final String catalogName) {
*/
@Override
public DatabaseInfo toInfo(final PolarisDatabaseEntity entity) {
AuditMapper mapper = new AuditMapper();
final DatabaseInfo databaseInfo = DatabaseInfo.builder()
.name(QualifiedName.ofDatabase(catalogName, entity.getDbName()))
.auditInfo(mapper.toInfo(entity.getAudit()))
.uri(entity.getLocation())
.build();
return databaseInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.netflix.metacat.common.server.connectors.ConnectorRequestContext;
import com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException;
import com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException;
import com.netflix.metacat.common.server.connectors.model.AuditInfo;
import com.netflix.metacat.common.server.connectors.model.DatabaseInfo;
import com.netflix.metacat.common.server.properties.DefaultConfigImpl;
import com.netflix.metacat.common.server.properties.MetacatProperties;
Expand All @@ -27,6 +28,7 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import spock.lang.Shared;

import java.util.Date;
import java.util.List;


Expand Down Expand Up @@ -122,6 +124,24 @@ public void testCreateDbDefaultUri() {
Assert.assertEquals(infoExpected, result);
}

/**
* Test create database get audit info.
*/
@Test
public void testDbAudit() {
final DatabaseInfo info = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).build();
Date date = new Date();
polarisDBService.create(requestContext, info);
final DatabaseInfo infoExpected = DatabaseInfo.builder()
.name(DB1_QUALIFIED_NAME).uri(DB1_NAME + ".db").build();
final DatabaseInfo result = polarisDBService.get(requestContext, DB1_QUALIFIED_NAME);
Assert.assertEquals(infoExpected, result);
final AuditInfo auditInfo = result.getAudit();
Assert.assertNotNull(auditInfo);
Assert.assertTrue(auditInfo.getCreatedDate().after(date));
Assert.assertEquals(auditInfo.getCreatedDate(), auditInfo.getLastModifiedDate());
}

/**
* Test update database.
*/
Expand Down

0 comments on commit 2c9b8d4

Please sign in to comment.