|
| 1 | +package com.featurevisor.sdk; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.featurevisor.types.DatafileContent; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 14 | + |
| 15 | +public class JsonNodeVariableMethodsTest { |
| 16 | + |
| 17 | + private static final String DATAFILE_JSON = """ |
| 18 | + { |
| 19 | + "schemaVersion": "2", |
| 20 | + "revision": "json-node-test", |
| 21 | + "segments": {}, |
| 22 | + "features": { |
| 23 | + "json_feature": { |
| 24 | + "key": "json_feature", |
| 25 | + "bucketBy": "userId", |
| 26 | + "variablesSchema": { |
| 27 | + "jsonObject": { |
| 28 | + "type": "json", |
| 29 | + "defaultValue": {} |
| 30 | + }, |
| 31 | + "jsonArray": { |
| 32 | + "type": "json", |
| 33 | + "defaultValue": [] |
| 34 | + }, |
| 35 | + "malformedJson": { |
| 36 | + "type": "json", |
| 37 | + "defaultValue": {} |
| 38 | + }, |
| 39 | + "regularString": { |
| 40 | + "type": "string", |
| 41 | + "defaultValue": "default-value" |
| 42 | + } |
| 43 | + }, |
| 44 | + "traffic": [ |
| 45 | + { |
| 46 | + "key": "rule-1", |
| 47 | + "segments": "*", |
| 48 | + "percentage": 100000, |
| 49 | + "variables": { |
| 50 | + "jsonObject": "{\\"a\\":1,\\"nested\\":{\\"b\\":\\"x\\"}}", |
| 51 | + "jsonArray": "[{\\"id\\":1},2,\\"x\\"]", |
| 52 | + "malformedJson": "{ invalid json", |
| 53 | + "regularString": "plain-value" |
| 54 | + } |
| 55 | + } |
| 56 | + ] |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + """; |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testJsonNodeMethodsAndMalformedJsonHandling() throws Exception { |
| 64 | + Featurevisor sdk = Featurevisor.createInstance( |
| 65 | + new Featurevisor.Options().datafile(DatafileContent.fromJson(DATAFILE_JSON)) |
| 66 | + ); |
| 67 | + Map<String, Object> context = Map.of("userId", "123"); |
| 68 | + |
| 69 | + JsonNode objectNode = sdk.getVariableJSONNode("json_feature", "jsonObject", context); |
| 70 | + assertNotNull(objectNode); |
| 71 | + assertTrue(objectNode.isObject()); |
| 72 | + assertEquals(1, objectNode.get("a").asInt()); |
| 73 | + assertEquals("x", objectNode.get("nested").get("b").asText()); |
| 74 | + |
| 75 | + JsonNode arrayNode = sdk.getVariableJSONNode("json_feature", "jsonArray", context); |
| 76 | + assertNotNull(arrayNode); |
| 77 | + assertTrue(arrayNode.isArray()); |
| 78 | + assertEquals(1, arrayNode.get(0).get("id").asInt()); |
| 79 | + assertEquals(2, arrayNode.get(1).asInt()); |
| 80 | + assertEquals("x", arrayNode.get(2).asText()); |
| 81 | + |
| 82 | + Map<String, Object> jsonAsMap = sdk.getVariableJSON("json_feature", "jsonObject", context); |
| 83 | + assertNotNull(jsonAsMap); |
| 84 | + assertEquals(1, ((Number) jsonAsMap.get("a")).intValue()); |
| 85 | + assertEquals("x", ((Map<String, Object>) jsonAsMap.get("nested")).get("b")); |
| 86 | + |
| 87 | + assertNull(sdk.getVariable("json_feature", "malformedJson", context)); |
| 88 | + assertNull(sdk.getVariableJSONNode("json_feature", "malformedJson", context)); |
| 89 | + assertNull(sdk.getVariableJSON("json_feature", "malformedJson", context)); |
| 90 | + |
| 91 | + assertEquals("plain-value", sdk.getVariable("json_feature", "regularString", context)); |
| 92 | + assertEquals("plain-value", sdk.getVariableString("json_feature", "regularString", context)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testChildInstanceJsonNodeParity() throws Exception { |
| 97 | + Featurevisor sdk = Featurevisor.createInstance( |
| 98 | + new Featurevisor.Options().datafile(DatafileContent.fromJson(DATAFILE_JSON)) |
| 99 | + ); |
| 100 | + ChildInstance child = sdk.spawn(Map.of("userId", "123")); |
| 101 | + |
| 102 | + JsonNode objectNode = child.getVariableJSONNode("json_feature", "jsonObject"); |
| 103 | + assertNotNull(objectNode); |
| 104 | + assertEquals("x", objectNode.get("nested").get("b").asText()); |
| 105 | + |
| 106 | + JsonNode arrayNode = child.getVariableJSONNode("json_feature", "jsonArray"); |
| 107 | + assertNotNull(arrayNode); |
| 108 | + assertEquals(3, arrayNode.size()); |
| 109 | + |
| 110 | + assertNull(child.getVariable("json_feature", "malformedJson")); |
| 111 | + assertNull(child.getVariableJSONNode("json_feature", "malformedJson")); |
| 112 | + assertNull(child.getVariableJSON("json_feature", "malformedJson")); |
| 113 | + |
| 114 | + List<String> none = child.getVariableArray("json_feature", "jsonObject"); |
| 115 | + assertNull(none); |
| 116 | + } |
| 117 | +} |
0 commit comments