Skip to content

Commit

Permalink
add more test cases and address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdonn committed Oct 15, 2024
1 parent 5433dcd commit d1dc935
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,17 @@ public static <RELATIONSHIP extends RecordTemplate, ASPECT extends RecordTemplat
String fieldName = field.getName();
Class<RecordTemplate> clazz = (Class<RecordTemplate>) aspect.getClass();
try {
Method getMethod = clazz.getMethod("get" + StringUtils.capitalize(fieldName));
Object obj = getMethod.invoke(aspect);
Method getMethod = clazz.getMethod("get" + StringUtils.capitalize(fieldName)); // getFieldName
Object obj = getMethod.invoke(aspect); // invoke getFieldName
// all relationship fields will be represented as Arrays so filter out any non-lists, empty lists, and lists that don't contain RecordTemplates
if (!(obj instanceof List) || ((List) obj).isEmpty() || !(((List) obj).get(0) instanceof RecordTemplate)) {
return null;
}
List<RecordTemplate> relationshipsList = (List<RecordTemplate>) obj;
ModelType modelType = parseModelTypeFromGmaAnnotation(relationshipsList.get(0));
if (modelType == ModelType.RELATIONSHIP) {
log.debug(String.format("Found {%d} relationships of type {%s} for field {%s} of aspect class {%s}.",
relationshipsList.size(), relationshipsList.get(0).getClass(), fieldName, clazz.getName()));
return (List<RELATIONSHIP>) relationshipsList;
}
} catch (ReflectiveOperationException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.linkedin.metadata.dao.utils;

import com.google.common.io.Resources;
import com.linkedin.data.template.IntegerArray;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.data.template.StringArray;
import com.linkedin.metadata.aspect.AuditedAspect;
Expand All @@ -21,6 +22,8 @@
import com.linkedin.testing.AnnotatedRelationshipFooArray;
import com.linkedin.testing.AspectFoo;
import com.linkedin.testing.AnnotatedAspectFooWithRelationshipField;
import com.linkedin.testing.CommonAspect;
import com.linkedin.testing.CommonAspectArray;
import com.linkedin.testing.urn.BurgerUrn;
import com.linkedin.testing.urn.FooUrn;
import io.ebean.Ebean;
Expand All @@ -40,7 +43,6 @@
import org.testng.annotations.Test;

import static com.linkedin.testing.TestUtils.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -600,13 +602,16 @@ public void testExtractRelationshipsFromAspect() {
AnnotatedAspectFooWithRelationshipField fooWithNullRelationshipField = new AnnotatedAspectFooWithRelationshipField();
assertTrue(EBeanDAOUtils.extractRelationshipsFromAspect(fooWithNullRelationshipField).isEmpty());

// case 4: aspect model contains multiple relationship fields, some null and some non-null
// case 4: aspect model contains multiple relationship fields, some null and some non-null, as well as array fields
// containing non-Relationship objects
// expected: return only the non-null relationships
relationshipFoos = new AnnotatedRelationshipFooArray(new AnnotatedRelationshipFoo(), new AnnotatedRelationshipFoo());
AnnotatedRelationshipBarArray relationshipBars = new AnnotatedRelationshipBarArray(new AnnotatedRelationshipBar());
// given:
// aspect = {
// value -> "abc"
// integers -> [1]
// nonRelationshipStructs -> [commonAspect1]
// relationshipFoos -> [foo1, foo2]
// relationshipBars -> [bar1]
// moreRelationshipFoos -> not present
Expand All @@ -615,6 +620,8 @@ public void testExtractRelationshipsFromAspect() {
// [[foo1, foo2], [bar1]]
AnnotatedAspectBarWithRelationshipFields barWithRelationshipFields = new AnnotatedAspectBarWithRelationshipFields()
.setValue("abc")
.setIntegers(new IntegerArray(1))
.setNonRelationshipStructs(new CommonAspectArray(new CommonAspect()))
.setRelationshipFoos(relationshipFoos)
.setRelationshipBars(relationshipBars); // don't set moreRelationshipFoos field

Expand All @@ -626,4 +633,6 @@ public void testExtractRelationshipsFromAspect() {
assertEquals(new AnnotatedRelationshipFoo(), results.get(0).get(1));
assertEquals(new AnnotatedRelationshipBar(), results.get(1).get(0));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ record AnnotatedAspectBarWithRelationshipFields {
*/
value: string

/**
* For unit tests
*/
integers: optional array[int]

/**
* For unit tests
*/
nonRelationshipStructs: optional array[CommonAspect]

/**
* For unit tests
*/
Expand Down

0 comments on commit d1dc935

Please sign in to comment.