Skip to content
This repository has been archived by the owner on Aug 25, 2024. It is now read-only.

Commit

Permalink
spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoloboschi committed Aug 23, 2024
1 parent 3eb3cb1 commit 7df0a9b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private static String getValueByDotNotation(final Map<String, Object> map, Strin
}
}

return currentMap.get(words.get(words.size() - 1)).toString();
Object value = currentMap.get(words.get(words.size() - 1));
return value == null ? "<null>" : value.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package ai.langstream.deployer.k8s.util;

import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -53,7 +54,8 @@ public List<JSONComparator.FieldComparisonDiff> diffs() {

private SpecDiffer() {}

public static JSONComparator.Result generateDiff(String expectedJson, String actualJson) {
private static JSONComparator.Result generateDiffFromStrings(
String expectedJson, String actualJson) {
if (expectedJson == null && actualJson == null) {
return JSONComparator.RESULT_EQUALS;
}
Expand All @@ -66,36 +68,11 @@ public static JSONComparator.Result generateDiff(String expectedJson, String act
return new JSONAssertComparator().compare(expectedJson, actualJson);
}

public static JSONComparator.Result generateDiff(Object expectedSpec, Object actualSpec) {
if (expectedSpec == null && actualSpec == null) {
return JSONComparator.RESULT_EQUALS;
}
if (expectedSpec == null) {
return EXPECTED_WAS_NULL_RESULT;
}
if (actualSpec == null) {
return ACTUAL_WAS_NULL_RESULT;
}
final String expectedStr = SerializationUtil.writeAsJson(expectedSpec);
final String actualStr = SerializationUtil.writeAsJson(actualSpec);
return generateDiff(expectedStr, actualStr);
}

public static JSONComparator.Result generateDiff(Object expectedSpec, String actualJson) {
if (expectedSpec == null && actualJson == null) {
return JSONComparator.RESULT_EQUALS;
}
if (expectedSpec == null) {
return EXPECTED_WAS_NULL_RESULT;
}
if (actualJson == null) {
return ACTUAL_WAS_NULL_RESULT;
}
final String expectedStr = SerializationUtil.writeAsJson(expectedSpec);
return generateDiff(expectedStr, actualJson);
}

public static JSONComparator.Result generateDiff(String expectedJson, Object actualSpec) {
if (actualSpec instanceof String) {
throw new IllegalArgumentException(
"actualSpec should be a parsed object, not a string");
}
if (expectedJson == null && actualSpec == null) {
return JSONComparator.RESULT_EQUALS;
}
Expand All @@ -106,7 +83,9 @@ public static JSONComparator.Result generateDiff(String expectedJson, Object act
return EXPECTED_WAS_NULL_RESULT;
}
final String actualStr = SerializationUtil.writeAsJson(actualSpec);
return generateDiff(expectedJson, actualStr);
final String expectedJsonStrSameSerialization =
SerializationUtil.writeAsJson(SerializationUtil.readJson(expectedJson, Map.class));
return generateDiffFromStrings(expectedJsonStrSameSerialization, actualStr);
}

public static void logDetailedSpecDiff(JSONComparator.Result diff) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ai.langstream.deployer.k8s.util;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Map;
import org.junit.jupiter.api.Test;

class SpecDifferTest {

@Test
void testJsonOrderAndNulls() throws Exception {
String json1 =
"""
{
"c": 3,
"a": 1,
"b": null
}""";

JSONComparator.Result result = SpecDiffer.generateDiff(json1, Map.of("a", 1, "c", 3));
SpecDiffer.logDetailedSpecDiff(result);
assertTrue(result.areEquals());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ void testApp() {
assertNull(createdCr.getSpec().getImage());
assertNull(createdCr.getSpec().getImagePullPolicy());
assertEquals(
"{\"resources\":{},\"modules\":{},\"instance\":null,\"gateways\":null,\"agentRunners\":{}}",
"{\"resources\":{},\"modules\":{},\"agentRunners\":{}}",
createdCr.getSpec().getApplication());
assertEquals(tenant, createdCr.getSpec().getTenant());
assertEquals(
"{\"deleteMode\":\"CLEANUP_REQUIRED\",\"markedForDeletion\":false,\"seed\":0,\"runtimeVersion\":null,\"autoUpgradeRuntimeImagePullPolicy\":false,\"autoUpgradeAgentResources\":false,\"autoUpgradeAgentPodTemplate\":false}",
"{\"deleteMode\":\"CLEANUP_REQUIRED\",\"markedForDeletion\":false,\"seed\":0,\"autoUpgradeRuntimeImagePullPolicy\":false,\"autoUpgradeAgentResources\":false,\"autoUpgradeAgentPodTemplate\":false}",
createdCr.getSpec().getOptions());

final Secret createdSecret =
Expand Down

0 comments on commit 7df0a9b

Please sign in to comment.