-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(businessAttribute): generate platform events on association/remo…
…val with schemaField (#12224)
- Loading branch information
1 parent
4e3103e
commit 6b6d820
Showing
7 changed files
with
279 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...nkedin/metadata/timeline/data/dataset/schema/SchemaFieldBusinessAttributeChangeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.linkedin.metadata.timeline.data.dataset.schema; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.linkedin.common.AuditStamp; | ||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.metadata.timeline.data.ChangeCategory; | ||
import com.linkedin.metadata.timeline.data.ChangeEvent; | ||
import com.linkedin.metadata.timeline.data.ChangeOperation; | ||
import com.linkedin.metadata.timeline.data.SemanticChangeType; | ||
import lombok.Builder; | ||
|
||
public class SchemaFieldBusinessAttributeChangeEvent extends ChangeEvent { | ||
@Builder(builderMethodName = "schemaFieldBusinessAttributeChangeEventBuilder") | ||
public SchemaFieldBusinessAttributeChangeEvent( | ||
String entityUrn, | ||
ChangeCategory category, | ||
ChangeOperation operation, | ||
String modifier, | ||
AuditStamp auditStamp, | ||
SemanticChangeType semVerChange, | ||
String description, | ||
Urn parentUrn, | ||
Urn businessAttributeUrn, | ||
Urn datasetUrn) { | ||
super( | ||
entityUrn, | ||
category, | ||
operation, | ||
modifier, | ||
ImmutableMap.of( | ||
"parentUrn", parentUrn.toString(), | ||
"businessAttributeUrn", businessAttributeUrn.toString(), | ||
"datasetUrn", datasetUrn.toString()), | ||
auditStamp, | ||
semVerChange, | ||
description); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...com/linkedin/metadata/timeline/eventgenerator/BusinessAttributesChangeEventGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.linkedin.metadata.timeline.eventgenerator; | ||
|
||
import com.linkedin.businessattribute.BusinessAttributeAssociation; | ||
import com.linkedin.businessattribute.BusinessAttributes; | ||
import com.linkedin.common.AuditStamp; | ||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.metadata.timeline.data.ChangeCategory; | ||
import com.linkedin.metadata.timeline.data.ChangeEvent; | ||
import com.linkedin.metadata.timeline.data.ChangeOperation; | ||
import com.linkedin.metadata.timeline.data.SemanticChangeType; | ||
import com.linkedin.metadata.timeline.data.dataset.schema.SchemaFieldBusinessAttributeChangeEvent; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.annotation.Nonnull; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class BusinessAttributesChangeEventGenerator | ||
extends EntityChangeEventGenerator<BusinessAttributes> { | ||
|
||
private static final String BUSINESS_ATTRIBUTE_ADDED_FORMAT = | ||
"BusinessAttribute '%s' added to entity '%s'."; | ||
private static final String BUSINESS_ATTRIBUTE_REMOVED_FORMAT = | ||
"BusinessAttribute '%s' removed from entity '%s'."; | ||
|
||
@Override | ||
public List<ChangeEvent> getChangeEvents( | ||
@Nonnull Urn urn, | ||
@Nonnull String entityName, | ||
@Nonnull String aspectName, | ||
@Nonnull Aspect<BusinessAttributes> from, | ||
@Nonnull Aspect<BusinessAttributes> to, | ||
@Nonnull AuditStamp auditStamp) { | ||
log.debug( | ||
"Calling BusinessAttributesChangeEventGenerator for entity {} and aspect {}", | ||
entityName, | ||
aspectName); | ||
return computeDiff(urn, entityName, aspectName, from.getValue(), to.getValue(), auditStamp); | ||
} | ||
|
||
private List<ChangeEvent> computeDiff( | ||
Urn urn, | ||
String entityName, | ||
String aspectName, | ||
BusinessAttributes previousValue, | ||
BusinessAttributes newValue, | ||
AuditStamp auditStamp) { | ||
List<ChangeEvent> changeEvents = new ArrayList<>(); | ||
|
||
BusinessAttributeAssociation previousAssociation = | ||
previousValue != null ? previousValue.getBusinessAttribute() : null; | ||
BusinessAttributeAssociation newAssociation = | ||
newValue != null ? newValue.getBusinessAttribute() : null; | ||
|
||
if (Objects.nonNull(previousAssociation) && Objects.isNull(newAssociation)) { | ||
changeEvents.add( | ||
createChangeEvent( | ||
previousAssociation, | ||
urn, | ||
ChangeOperation.REMOVE, | ||
BUSINESS_ATTRIBUTE_REMOVED_FORMAT, | ||
auditStamp)); | ||
|
||
} else if (Objects.isNull(previousAssociation) && Objects.nonNull(newAssociation)) { | ||
changeEvents.add( | ||
createChangeEvent( | ||
newAssociation, | ||
urn, | ||
ChangeOperation.ADD, | ||
BUSINESS_ATTRIBUTE_ADDED_FORMAT, | ||
auditStamp)); | ||
} | ||
return changeEvents; | ||
} | ||
|
||
private ChangeEvent createChangeEvent( | ||
BusinessAttributeAssociation businessAttributeAssociation, | ||
Urn entityUrn, | ||
ChangeOperation changeOperation, | ||
String format, | ||
AuditStamp auditStamp) { | ||
return SchemaFieldBusinessAttributeChangeEvent.schemaFieldBusinessAttributeChangeEventBuilder() | ||
.entityUrn(entityUrn.toString()) | ||
.category(ChangeCategory.BUSINESS_ATTRIBUTE) | ||
.operation(changeOperation) | ||
.modifier(businessAttributeAssociation.getBusinessAttributeUrn().toString()) | ||
.auditStamp(auditStamp) | ||
.semVerChange(SemanticChangeType.MINOR) | ||
.description( | ||
String.format( | ||
format, businessAttributeAssociation.getBusinessAttributeUrn().getId(), entityUrn)) | ||
.parentUrn(entityUrn) | ||
.businessAttributeUrn(businessAttributeAssociation.getBusinessAttributeUrn()) | ||
.datasetUrn(entityUrn.getIdAsUrn()) | ||
.build(); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
...linkedin/metadata/timeline/eventgenerator/BusinessAttributesChangeEventGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.linkedin.metadata.timeline.eventgenerator; | ||
|
||
import static org.testng.AssertJUnit.assertEquals; | ||
|
||
import com.linkedin.businessattribute.BusinessAttributeAssociation; | ||
import com.linkedin.businessattribute.BusinessAttributes; | ||
import com.linkedin.common.AuditStamp; | ||
import com.linkedin.common.urn.BusinessAttributeUrn; | ||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.data.ByteString; | ||
import com.linkedin.data.template.RecordTemplate; | ||
import com.linkedin.metadata.Constants; | ||
import com.linkedin.metadata.models.AspectSpec; | ||
import com.linkedin.metadata.timeline.data.ChangeEvent; | ||
import com.linkedin.metadata.timeline.data.ChangeOperation; | ||
import com.linkedin.metadata.utils.GenericRecordUtils; | ||
import com.linkedin.mxe.SystemMetadata; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import mock.MockEntitySpec; | ||
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; | ||
import org.testng.annotations.Test; | ||
|
||
public class BusinessAttributesChangeEventGeneratorTest extends AbstractTestNGSpringContextTests { | ||
|
||
private static Urn getSchemaFieldUrn() throws URISyntaxException { | ||
return Urn.createFromString( | ||
"urn:li:schemaField:(urn:li:dataset:(urn:li:dataPlatform:hdfs,SampleHdfsDataset,PROD),user_id)"); | ||
} | ||
|
||
private static final String BUSINESS_ATTRIBUTE_URN = | ||
"urn:li:businessAttribute:cypressTestAttribute"; | ||
|
||
private static AuditStamp getTestAuditStamp() throws URISyntaxException { | ||
return new AuditStamp() | ||
.setActor(Urn.createFromString("urn:li:corpuser:__datahub_system")) | ||
.setTime(1683829509553L); | ||
} | ||
|
||
private static Aspect<BusinessAttributes> getBusinessAttributes( | ||
BusinessAttributeAssociation association) { | ||
return new Aspect<>( | ||
new BusinessAttributes().setBusinessAttribute(association), new SystemMetadata()); | ||
} | ||
|
||
private static Aspect<BusinessAttributes> getNullBusinessAttributes() { | ||
MockEntitySpec mockEntitySpec = new MockEntitySpec("schemaField"); | ||
BusinessAttributes businessAttributes = new BusinessAttributes(); | ||
final AspectSpec aspectSpec = | ||
mockEntitySpec.createAspectSpec(businessAttributes, Constants.BUSINESS_ATTRIBUTE_ASPECT); | ||
final RecordTemplate nullAspect = | ||
GenericRecordUtils.deserializeAspect( | ||
ByteString.copyString("{}", StandardCharsets.UTF_8), "application/json", aspectSpec); | ||
return new Aspect(nullAspect, new SystemMetadata()); | ||
} | ||
|
||
@Test | ||
public void testBusinessAttributeAddition() throws Exception { | ||
BusinessAttributesChangeEventGenerator businessAttributesChangeEventGenerator = | ||
new BusinessAttributesChangeEventGenerator(); | ||
|
||
Urn urn = getSchemaFieldUrn(); | ||
String entity = "schemaField"; | ||
String aspect = "businessAttributes"; | ||
AuditStamp auditStamp = getTestAuditStamp(); | ||
|
||
Aspect<BusinessAttributes> from = getNullBusinessAttributes(); | ||
Aspect<BusinessAttributes> to = | ||
getBusinessAttributes( | ||
new BusinessAttributeAssociation() | ||
.setBusinessAttributeUrn(new BusinessAttributeUrn(BUSINESS_ATTRIBUTE_URN))); | ||
|
||
List<ChangeEvent> actual = | ||
businessAttributesChangeEventGenerator.getChangeEvents( | ||
urn, entity, aspect, from, to, auditStamp); | ||
assertEquals(1, actual.size()); | ||
assertEquals(ChangeOperation.ADD.name(), actual.get(0).getOperation().name()); | ||
assertEquals(getSchemaFieldUrn(), Urn.createFromString(actual.get(0).getEntityUrn())); | ||
} | ||
|
||
@Test | ||
public void testBusinessAttributeRemoval() throws Exception { | ||
BusinessAttributesChangeEventGenerator test = new BusinessAttributesChangeEventGenerator(); | ||
|
||
Urn urn = getSchemaFieldUrn(); | ||
String entity = "schemaField"; | ||
String aspect = "businessAttributes"; | ||
AuditStamp auditStamp = getTestAuditStamp(); | ||
|
||
Aspect<BusinessAttributes> from = | ||
getBusinessAttributes( | ||
new BusinessAttributeAssociation() | ||
.setBusinessAttributeUrn(new BusinessAttributeUrn(BUSINESS_ATTRIBUTE_URN))); | ||
Aspect<BusinessAttributes> to = getNullBusinessAttributes(); | ||
|
||
List<ChangeEvent> actual = test.getChangeEvents(urn, entity, aspect, from, to, auditStamp); | ||
assertEquals(1, actual.size()); | ||
assertEquals(ChangeOperation.REMOVE.name(), actual.get(0).getOperation().name()); | ||
assertEquals(getSchemaFieldUrn(), Urn.createFromString(actual.get(0).getEntityUrn())); | ||
} | ||
|
||
@Test | ||
public void testNoChange() throws Exception { | ||
BusinessAttributesChangeEventGenerator test = new BusinessAttributesChangeEventGenerator(); | ||
|
||
Urn urn = getSchemaFieldUrn(); | ||
String entity = "schemaField"; | ||
String aspect = "businessAttributes"; | ||
AuditStamp auditStamp = getTestAuditStamp(); | ||
|
||
Aspect<BusinessAttributes> from = | ||
getBusinessAttributes( | ||
new BusinessAttributeAssociation() | ||
.setBusinessAttributeUrn(new BusinessAttributeUrn(BUSINESS_ATTRIBUTE_URN))); | ||
Aspect<BusinessAttributes> to = | ||
getBusinessAttributes( | ||
new BusinessAttributeAssociation() | ||
.setBusinessAttributeUrn(new BusinessAttributeUrn(BUSINESS_ATTRIBUTE_URN))); | ||
|
||
List<ChangeEvent> actual = test.getChangeEvents(urn, entity, aspect, from, to, auditStamp); | ||
assertEquals(0, actual.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters