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

add support for recursively set default value in test mode #464

Merged
merged 4 commits into from
Nov 1, 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
1 change: 1 addition & 0 deletions dao-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
compile externalDependency.reflections
compile externalDependency.commonsLang
implementation 'com.google.protobuf:protobuf-java:3.21.1'
implementation spec.product.pegasus.restliServer
dataModel project(':core-models')
dataModel project(':validators')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.linkedin.data.template.UnionTemplate;
import com.linkedin.metadata.dao.exception.ModelConversionException;
import com.linkedin.metadata.validator.InvalidSchemaException;
import com.linkedin.restli.internal.server.response.ResponseUtils;
import com.linkedin.restli.server.RestLiServiceException;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -77,6 +79,27 @@ public static String toJsonString(@Nonnull RecordTemplate recordTemplate) {
}
}

/**
* Serializes a {@link RecordTemplate} to JSON string.
* Also take test mode as input to control the default value fill in strategy
* @param recordTemplate the record template to serialize
* @return the JSON string serialized using {@link JacksonDataTemplateCodec}.
*/
//Todo: we will remove this method once we verify it works and does not bring too much degrade in test mode.
@Nonnull
public static String toJsonString(@Nonnull RecordTemplate recordTemplate, boolean isTestMode) {
if (isTestMode) {
try {
DataMap dataWithDefaultValue = (DataMap) ResponseUtils.fillInDataDefault(recordTemplate.schema(), recordTemplate.data());
return DATA_TEMPLATE_CODEC.mapToString(dataWithDefaultValue);
} catch (RestLiServiceException | IOException e) {
throw new ModelConversionException("Failed to serialize RecordTemplate: " + recordTemplate.toString(), e);
}
} else {
return toJsonString(recordTemplate);
}
}

/**
* Creates a {@link RecordTemplate} object from a serialized JSON string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.linkedin.testing.AspectFoo;
import com.linkedin.testing.AspectBarArray;
import com.linkedin.testing.AspectFooArray;
import com.linkedin.testing.AspectWithDefaultValue;
import com.linkedin.testing.EntityAspectUnion;
import com.linkedin.testing.EntityAspectUnionAlias;
import com.linkedin.testing.EntityAspectUnionComplex;
Expand All @@ -22,6 +23,7 @@
import com.linkedin.testing.PizzaInfo;
import com.linkedin.testing.StringUnion;
import com.linkedin.testing.StringUnionArray;
import com.linkedin.testing.MapValueRecord;
import com.linkedin.testing.singleaspectentity.EntityValue;
import com.linkedin.testing.urn.FooUrn;
import java.io.IOException;
Expand Down Expand Up @@ -53,6 +55,17 @@ public void testToJsonString() throws IOException {
assertEquals(actual, expected);
}

@Test
public void testToJsonStringWithDefault() throws IOException {
AspectWithDefaultValue defaultValueAspect = new AspectWithDefaultValue().setNestedValueWithDefault(new MapValueRecord());
String expected =
loadJsonFromResource("defaultValueAspect.json").replaceAll("\\s+", "").replaceAll("\\n", "").replaceAll("\\r", "");

String actual = RecordUtils.toJsonString(defaultValueAspect, true);

assertEquals(actual, expected);
}

@Test
public void testToRecordTemplate() throws IOException {
AspectFoo expected = new AspectFoo().setValue("foo");
Expand Down
6 changes: 6 additions & 0 deletions dao-api/src/test/resources/defaultValueAspect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"nestedValueWithDefault":{
"mapValueWithDefaultmap":{}
},
"valueWithDefault": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public <ASPECT extends RecordTemplate> int addWithOptimisticLocking(
}

AuditedAspect auditedAspect = new AuditedAspect()
.setAspect(RecordUtils.toJsonString(newValue))
.setAspect(RecordUtils.toJsonString(newValue, isTestMode))
.setCanonicalName(aspectClass.getCanonicalName())
.setLastmodifiedby(actor)
.setLastmodifiedon(new Timestamp(timestamp).toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace com.linkedin.testing

record AspectWithDefaultValue {

/**
* For unit tests
*/
valueWithDefault: string = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to add other/complex default data structure in test model? such as nested and map {}?

nestedValueWithDefault: record MapValueRecord {mapValueWithDefaultmap: map[string, string] = { }}
}
Loading