Skip to content

Commit

Permalink
Adding EventUtilsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerem Sahin committed Feb 26, 2020
1 parent e69def9 commit 71f2d14
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.linkedin.metadata;

import com.linkedin.common.urn.CorpuserUrn;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.metadata.dao.utils.RecordUtils;
import com.linkedin.mxe.MetadataAuditEvent;
import com.linkedin.mxe.MetadataChangeEvent;
import java.io.IOException;
import java.io.InputStream;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.JsonDecoder;
import org.testng.annotations.Test;

import static com.linkedin.metadata.utils.TestUtils.*;
import static org.testng.Assert.*;


public class EventUtilsTests {

@Test
public void testAvroToPegasusMAE() throws IOException {
GenericRecord record = genericRecordFromResource("test-avro2pegasus-mae.json",
com.linkedin.pegasus2avro.mxe.MetadataAuditEvent.SCHEMA$);

MetadataAuditEvent mae = EventUtils.avroToPegasusMAE(record);

assertEquals(
mae.getNewSnapshot().getDatasetSnapshot().getAspects().get(0).getOwnership().getOwners().get(0).getOwner(),
new CorpuserUrn("foobar"));
}

@Test
public void testAvroToPegasusMCE() throws IOException {
GenericRecord record = genericRecordFromResource("test-avro2pegasus-mce.json",
com.linkedin.pegasus2avro.mxe.MetadataChangeEvent.SCHEMA$);

MetadataChangeEvent mce = EventUtils.avroToPegasusMCE(record);

assertEquals(
mce.getProposedSnapshot().getDatasetSnapshot().getAspects().get(0).getOwnership().getOwners().get(0).getOwner(),
new CorpuserUrn("foobar"));
}

@Test
public void testPegasusToAvroMAE() throws IOException {
MetadataAuditEvent event = recordTemplateFromResource("test-pegasus2avro-mae.json", MetadataAuditEvent.class);

GenericRecord record = EventUtils.pegasusToAvroMAE(event);

assertEquals(record.getSchema(), com.linkedin.pegasus2avro.mxe.MetadataAuditEvent.SCHEMA$);
assertNotNull(record.get("newSnapshot"));
}

@Test
public void testPegasusToAvroMCE() throws IOException {
MetadataChangeEvent event = recordTemplateFromResource("test-pegasus2avro-mce.json", MetadataChangeEvent.class);

GenericRecord record = EventUtils.pegasusToAvroMCE(event);

assertEquals(record.getSchema(), com.linkedin.pegasus2avro.mxe.MetadataChangeEvent.SCHEMA$);
assertNotNull(record.get("proposedSnapshot"));
}

private GenericRecord genericRecordFromResource(String resourcePath, Schema schema) throws IOException {
InputStream is = getClass().getClassLoader().getResourceAsStream(resourcePath);
JsonDecoder decoder = DecoderFactory.get().jsonDecoder(schema, is);
DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
return reader.read(null, decoder);
}

private <T extends RecordTemplate> T recordTemplateFromResource(String resourcePath,
Class<? extends RecordTemplate> clazz) throws IOException {
String json = loadJsonFromResource(resourcePath);
return (T) RecordUtils.toRecordTemplate(clazz, json);
}
}

This file was deleted.

67 changes: 0 additions & 67 deletions metadata-events/mxe-utils-avro-1.7/src/test/resources/old-mce.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
"auditHeader": null,
"oldSnapshot": null,
"newSnapshot": {
"com.linkedin.pegasus2avro.metadata.snapshot.MetricSnapshot": {
"urn": "urn:li:metric:(foo,bar)",
"com.linkedin.pegasus2avro.metadata.snapshot.DatasetSnapshot": {
"urn": "urn:li:dataset:(urn:li:dataPlatform:foo,bar,baz)",
"aspects": [
{
"com.linkedin.pegasus2avro.common.Ownership": {
"owners": [
{
"owner": "urn:li:corpuser:foo",
"owner": "urn:li:corpuser:foobar",
"type": "DEVELOPER",
"source": null
}
],
"lastModified": null
"lastModified": {
"time": 0,
"actor": "urn:li:corpuser:foobar",
"impersonator": null
}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"auditHeader": null,
"proposedSnapshot": {
"com.linkedin.pegasus2avro.metadata.snapshot.MetricSnapshot": {
"urn": "urn:li:metric:(foo,bar)",
"com.linkedin.pegasus2avro.metadata.snapshot.DatasetSnapshot": {
"urn": "urn:li:dataset:(urn:li:dataPlatform:foo,bar,baz)",
"aspects": [
{
"com.linkedin.pegasus2avro.common.Ownership": {
"owners": [
{
"owner": "urn:li:corpuser:foo",
"owner": "urn:li:corpuser:foobar",
"type": "DEVELOPER",
"source": null
}
],
"lastModified": null
"lastModified": {
"time": 0,
"actor": "urn:li:corpuser:foobar",
"impersonator": null
}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{
"newSnapshot": {
"com.linkedin.metadata.snapshot.MetricSnapshot": {
"urn": "urn:li:metric:(foo,bar)",
"com.linkedin.metadata.snapshot.DatasetSnapshot": {
"urn": "urn:li:dataset:(urn:li:dataPlatform:foo,bar,baz)",
"aspects": [
{
"com.linkedin.common.Ownership": {
"owners": [
{
"owner": "urn:li:corpuser:foo",
"owner": "urn:li:corpuser:foobar",
"type": "DEVELOPER"
}
]
],
"lastModified": {
"time": 0,
"actor": "urn:li:corpuser:foobar"
}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{
"proposedSnapshot": {
"com.linkedin.metadata.snapshot.MetricSnapshot": {
"urn": "urn:li:metric:(foo,bar)",
"com.linkedin.metadata.snapshot.DatasetSnapshot": {
"urn": "urn:li:dataset:(urn:li:dataPlatform:foo,bar,baz)",
"aspects": [
{
"com.linkedin.common.Ownership": {
"owners": [
{
"owner": "urn:li:corpuser:foo",
"owner": "urn:li:corpuser:foobar",
"type": "DEVELOPER"
}
]
],
"lastModified": {
"time": 0,
"actor": "urn:li:corpuser:foobar"
}
}
}
]
Expand Down

0 comments on commit 71f2d14

Please sign in to comment.