Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(urn): add test case #10112

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import com.linkedin.datahub.graphql.generated.SearchSuggestion;
import com.linkedin.datahub.graphql.types.common.mappers.UrnToEntityMapper;
import com.linkedin.datahub.graphql.types.entitytype.EntityTypeMapper;
import com.linkedin.metadata.entity.validation.ValidationUtils;
import com.linkedin.metadata.models.registry.EntityRegistry;
import com.linkedin.metadata.search.SearchEntity;
import com.linkedin.metadata.search.utils.SearchUtils;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.annotation.Nonnull;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -70,6 +73,7 @@ public static String convertFilterValue(String filterValue, List<Boolean> isEnti
.collect(Collectors.joining(AGGREGATION_SEPARATOR_CHAR));
}

@Deprecated
public static List<MatchedField> getMatchedFieldEntry(
List<com.linkedin.metadata.search.MatchedField> highlightMetadata) {
return highlightMetadata.stream()
Expand All @@ -91,6 +95,29 @@ public static List<MatchedField> getMatchedFieldEntry(
.collect(Collectors.toList());
}

public static List<MatchedField> getMatchedFieldEntry(
@Nonnull EntityRegistry entityRegistry,
List<com.linkedin.metadata.search.MatchedField> highlightMetadata) {
return highlightMetadata.stream()
.map(
field -> {
MatchedField matchedField = new MatchedField();
matchedField.setName(field.getName());
matchedField.setValue(field.getValue());
if (SearchUtils.isUrn(field.getValue())) {
try {
Urn urn = Urn.createFromString(field.getValue());
ValidationUtils.validateUrn(entityRegistry, urn);
matchedField.setEntity(UrnToEntityMapper.map(urn));
} catch (IllegalArgumentException | URISyntaxException e) {
log.debug("Failed to create urn from MatchedField value: {}", field.getValue());
}
}
return matchedField;
})
.collect(Collectors.toList());
}

public static SearchSuggestion mapSearchSuggestion(
com.linkedin.metadata.search.SearchSuggestion suggestion) {
return new SearchSuggestion(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.linkedin.datahub.graphql.types.mappers;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;

import com.linkedin.common.urn.Urn;
import com.linkedin.data.schema.annotation.PathSpecBasedSchemaAnnotationVisitor;
import com.linkedin.datahub.graphql.generated.MatchedField;
import com.linkedin.metadata.entity.validation.ValidationUtils;
import com.linkedin.metadata.models.registry.ConfigEntityRegistry;
import com.linkedin.metadata.models.registry.EntityRegistry;
import com.linkedin.metadata.snapshot.Snapshot;
import java.net.URISyntaxException;
import java.util.List;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class MapperUtilsTest {
private EntityRegistry entityRegistry;

@BeforeTest
public void setup() {
PathSpecBasedSchemaAnnotationVisitor.class
.getClassLoader()
.setClassAssertionStatus(PathSpecBasedSchemaAnnotationVisitor.class.getName(), false);
entityRegistry =
new ConfigEntityRegistry(
Snapshot.class.getClassLoader().getResourceAsStream("entity-registry.yml"));
}

@Test
public void testMatchedFieldValidation() throws URISyntaxException {
final Urn urn =
Urn.createFromString(
"urn:li:dataset:(urn:li:dataPlatform:s3,urn:li:dataset:%28urn:li:dataPlatform:s3%2Ctest-datalake-concepts/prog_maintenance%2CPROD%29,PROD)");
final Urn invalidUrn =
Urn.createFromString(
"urn:li:dataset:%28urn:li:dataPlatform:s3%2Ctest-datalake-concepts/prog_maintenance%2CPROD%29");
assertThrows(
IllegalArgumentException.class,
() -> ValidationUtils.validateUrn(entityRegistry, invalidUrn));

List<MatchedField> actualMatched =
MapperUtils.getMatchedFieldEntry(
entityRegistry,
List.of(
buildSearchMatchField(urn.toString()),
buildSearchMatchField(invalidUrn.toString())));

assertEquals(actualMatched.size(), 2, "Matched fields should be 2");
assertEquals(
actualMatched.stream().filter(matchedField -> matchedField.getEntity() != null).count(),
1,
"With urn should be 1");
}

private static com.linkedin.metadata.search.MatchedField buildSearchMatchField(
String highlightValue) {
com.linkedin.metadata.search.MatchedField field =
new com.linkedin.metadata.search.MatchedField();
field.setName("testField");
field.setValue(highlightValue);
return field;
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
package com.linkedin.metadata.entity;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;

import com.linkedin.common.BrowsePath;
import com.linkedin.common.FabricType;
import com.linkedin.common.Owner;
import com.linkedin.common.OwnershipType;
import com.linkedin.common.Status;
import com.linkedin.common.urn.Urn;
import com.linkedin.data.DataMap;
import com.linkedin.data.schema.annotation.PathSpecBasedSchemaAnnotationVisitor;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.metadata.entity.validation.ValidationException;
import com.linkedin.metadata.entity.validation.ValidationUtils;
import org.testng.Assert;
import com.linkedin.metadata.key.DatasetKey;
import com.linkedin.metadata.models.AspectSpec;
import com.linkedin.metadata.models.registry.ConfigEntityRegistry;
import com.linkedin.metadata.models.registry.EntityRegistry;
import com.linkedin.metadata.snapshot.Snapshot;
import com.linkedin.metadata.utils.EntityKeyUtils;
import java.net.URISyntaxException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ValidationUtilsTest {
private EntityRegistry entityRegistry;

@BeforeTest
public void setup() {
PathSpecBasedSchemaAnnotationVisitor.class
.getClassLoader()
.setClassAssertionStatus(PathSpecBasedSchemaAnnotationVisitor.class.getName(), false);
entityRegistry =
new ConfigEntityRegistry(
Snapshot.class.getClassLoader().getResourceAsStream("entity-registry.yml"));
}

@Test
public void testValidateOrThrowThrowsOnMissingUnrecognizedField() {
DataMap rawMap = new DataMap();
rawMap.put("removed", true);
rawMap.put("extraField", 1);
Status status = new Status(rawMap);
Assert.assertThrows(ValidationException.class, () -> ValidationUtils.validateOrThrow(status));
assertThrows(ValidationException.class, () -> ValidationUtils.validateOrThrow(status));
}

@Test
public void testValidateOrThrowThrowsOnMissingRequiredField() {
DataMap rawMap = new DataMap();
BrowsePath status = new BrowsePath(rawMap);
Assert.assertThrows(ValidationException.class, () -> ValidationUtils.validateOrThrow(status));
assertThrows(ValidationException.class, () -> ValidationUtils.validateOrThrow(status));
}

@Test
Expand All @@ -43,4 +68,32 @@ public void testValidateOrThrowDoesNotThrowOnMissingDefaultField() {
Status status = new Status(rawMap);
ValidationUtils.validateOrThrow(status);
}

@Test
public void testConvertEntityUrnToKeyUrlEncoded() throws URISyntaxException {
final Urn urn =
Urn.createFromString(
"urn:li:dataset:(urn:li:dataPlatform:s3,urn:li:dataset:%28urn:li:dataPlatform:s3%2Ctest-datalake-concepts/prog_maintenance%2CPROD%29,PROD)");

ValidationUtils.validateUrn(entityRegistry, urn);

final AspectSpec keyAspectSpec =
entityRegistry.getEntitySpec(urn.getEntityType()).getKeyAspectSpec();
final RecordTemplate actualKey = EntityKeyUtils.convertUrnToEntityKey(urn, keyAspectSpec);

final DatasetKey expectedKey = new DatasetKey();
expectedKey.setPlatform(Urn.createFromString("urn:li:dataPlatform:s3"));
expectedKey.setName(
"urn:li:dataset:%28urn:li:dataPlatform:s3%2Ctest-datalake-concepts/prog_maintenance%2CPROD%29");
expectedKey.setOrigin(FabricType.PROD);

assertEquals(actualKey, expectedKey);

final Urn invalidUrn =
Urn.createFromString(
"urn:li:dataset:%28urn:li:dataPlatform:s3%2Ctest-datalake-concepts/prog_maintenance%2CPROD%29");
assertThrows(
IllegalArgumentException.class,
() -> ValidationUtils.validateUrn(entityRegistry, invalidUrn));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@

import com.datahub.test.KeyPartEnum;
import com.datahub.test.TestEntityKey;
import com.linkedin.common.FabricType;
import com.linkedin.common.urn.Urn;
import com.linkedin.data.schema.annotation.PathSpecBasedSchemaAnnotationVisitor;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.metadata.key.DatasetKey;
import com.linkedin.metadata.models.AspectSpec;
import com.linkedin.metadata.models.EntitySpec;
import com.linkedin.metadata.models.registry.ConfigEntityRegistry;
import com.linkedin.metadata.models.registry.EntityRegistry;
import com.linkedin.metadata.snapshot.Snapshot;
import java.net.URISyntaxException;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/** Tests the capabilities of {@link EntityKeyUtils} */
public class EntityKeyUtilsTest {

private EntityRegistry entityRegistry;

@BeforeTest
public void setup() {
PathSpecBasedSchemaAnnotationVisitor.class
.getClassLoader()
.setClassAssertionStatus(PathSpecBasedSchemaAnnotationVisitor.class.getName(), false);
entityRegistry =
new ConfigEntityRegistry(
Snapshot.class.getClassLoader().getResourceAsStream("entity-registry.yml"));
}

@Test
public void testConvertEntityKeyToUrn() throws Exception {
final TestEntityKey key = new TestEntityKey();
Expand Down Expand Up @@ -59,4 +79,22 @@ public void testConvertEntityUrnToKey() throws Exception {
EntityKeyUtils.convertUrnToEntityKey(urn, entitySpec.getKeyAspectSpec());
Assert.assertEquals(actualKey.data(), expectedKey.data());
}

@Test
public void testConvertEntityUrnToKeyUrlEncoded() throws URISyntaxException {
final Urn urn =
Urn.createFromString(
"urn:li:dataset:(urn:li:dataPlatform:s3,urn:li:dataset:%28urn:li:dataPlatform:s3%2Ctest-datalake-concepts/prog_maintenance%2CPROD%29,PROD)");
final AspectSpec keyAspectSpec =
entityRegistry.getEntitySpec(urn.getEntityType()).getKeyAspectSpec();
final RecordTemplate actualKey = EntityKeyUtils.convertUrnToEntityKey(urn, keyAspectSpec);

final DatasetKey expectedKey = new DatasetKey();
expectedKey.setPlatform(Urn.createFromString("urn:li:dataPlatform:s3"));
expectedKey.setName(
"urn:li:dataset:%28urn:li:dataPlatform:s3%2Ctest-datalake-concepts/prog_maintenance%2CPROD%29");
expectedKey.setOrigin(FabricType.PROD);

assertEquals(actualKey, expectedKey);
}
}
Loading