|
8 | 8 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
9 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
10 | 10 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
11 | 12 |
|
12 | 13 | class ImmutableContextTest {
|
13 | 14 |
|
@@ -64,4 +65,53 @@ void mergeShouldReturnAllTheValuesFromTheContextWhenOverridingContextIsNull() {
|
64 | 65 | assertEquals("targeting_key", merge.getTargetingKey());
|
65 | 66 | assertArrayEquals(new Object[]{"key1", "key2"}, merge.keySet().toArray());
|
66 | 67 | }
|
| 68 | + |
| 69 | + @DisplayName("Merge should retain subkeys from the existing context when the overriding context has the same targeting key") |
| 70 | + @Test |
| 71 | + void mergeShouldRetainItsSubkeysWhenOverridingContextHasTheSameKey() { |
| 72 | + HashMap<String, Value> attributes = new HashMap<>(); |
| 73 | + HashMap<String, Value> overridingAttributes = new HashMap<>(); |
| 74 | + HashMap<String, Value> key1Attributes = new HashMap<>(); |
| 75 | + HashMap<String, Value> ovKey1Attributes = new HashMap<>(); |
| 76 | + |
| 77 | + key1Attributes.put("key1_1", new Value("val1_1")); |
| 78 | + attributes.put("key1", new Value(new ImmutableStructure(key1Attributes))); |
| 79 | + attributes.put("key2", new Value("val2")); |
| 80 | + ovKey1Attributes.put("overriding_key1_1", new Value("overriding_val_1_1")); |
| 81 | + overridingAttributes.put("key1", new Value(new ImmutableStructure(ovKey1Attributes))); |
| 82 | + |
| 83 | + EvaluationContext ctx = new ImmutableContext("targeting_key", attributes); |
| 84 | + EvaluationContext overriding = new ImmutableContext("targeting_key", overridingAttributes); |
| 85 | + EvaluationContext merge = ctx.merge(overriding); |
| 86 | + assertEquals("targeting_key", merge.getTargetingKey()); |
| 87 | + assertArrayEquals(new Object[]{"key1", "key2"}, merge.keySet().toArray()); |
| 88 | + |
| 89 | + Value key1 = merge.getValue("key1"); |
| 90 | + assertTrue(key1.isStructure()); |
| 91 | + |
| 92 | + Structure value = key1.asStructure(); |
| 93 | + assertArrayEquals(new Object[]{"key1_1","overriding_key1_1"}, value.keySet().toArray()); |
| 94 | + } |
| 95 | + |
| 96 | + @DisplayName("Merge should retain subkeys from the existing context when the overriding context doesn't have targeting key") |
| 97 | + @Test |
| 98 | + void mergeShouldRetainItsSubkeysWhenOverridingContextHasNoTargetingKey() { |
| 99 | + HashMap<String, Value> attributes = new HashMap<>(); |
| 100 | + HashMap<String, Value> key1Attributes = new HashMap<>(); |
| 101 | + |
| 102 | + key1Attributes.put("key1_1", new Value("val1_1")); |
| 103 | + attributes.put("key1", new Value(new ImmutableStructure(key1Attributes))); |
| 104 | + attributes.put("key2", new Value("val2")); |
| 105 | + |
| 106 | + EvaluationContext ctx = new ImmutableContext(attributes); |
| 107 | + EvaluationContext overriding = new ImmutableContext(); |
| 108 | + EvaluationContext merge = ctx.merge(overriding); |
| 109 | + assertArrayEquals(new Object[]{"key1", "key2"}, merge.keySet().toArray()); |
| 110 | + |
| 111 | + Value key1 = merge.getValue("key1"); |
| 112 | + assertTrue(key1.isStructure()); |
| 113 | + |
| 114 | + Structure value = key1.asStructure(); |
| 115 | + assertArrayEquals(new Object[]{"key1_1"}, value.keySet().toArray()); |
| 116 | + } |
67 | 117 | }
|
0 commit comments