6
6
import java .util .HashMap ;
7
7
8
8
import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
9
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9
10
import static org .junit .jupiter .api .Assertions .assertThrows ;
10
11
11
12
public class ImmutableContextTest {
@@ -27,4 +28,40 @@ void shouldCreateCopyOfAttributesForImmutableContext() {
27
28
attributes .put ("key3" , new Value ("val3" ));
28
29
assertArrayEquals (ctx .keySet ().toArray (), new Object []{"key1" , "key2" });
29
30
}
31
+
32
+ @ DisplayName ("targeting key should be changed from the overriding context" )
33
+ @ Test
34
+ void shouldChangeTargetingKeyFromOverridingContext () {
35
+ HashMap <String , Value > attributes = new HashMap <>();
36
+ attributes .put ("key1" , new Value ("val1" ));
37
+ attributes .put ("key2" , new Value ("val2" ));
38
+ EvaluationContext ctx = new ImmutableContext ("targeting key" , attributes );
39
+ EvaluationContext overriding = new ImmutableContext ("overriding_key" );
40
+ EvaluationContext merge = ctx .merge (overriding );
41
+ assertEquals ("overriding_key" , merge .getTargetingKey ());
42
+ }
43
+
44
+ @ DisplayName ("targeting key should not changed from the overriding context if missing" )
45
+ @ Test
46
+ void shouldRetainTargetingKeyWhenOverridingContextTargetingKeyValueIsEmpty () {
47
+ HashMap <String , Value > attributes = new HashMap <>();
48
+ attributes .put ("key1" , new Value ("val1" ));
49
+ attributes .put ("key2" , new Value ("val2" ));
50
+ EvaluationContext ctx = new ImmutableContext ("targeting_key" , attributes );
51
+ EvaluationContext overriding = new ImmutableContext ("" );
52
+ EvaluationContext merge = ctx .merge (overriding );
53
+ assertEquals ("targeting_key" , merge .getTargetingKey ());
54
+ }
55
+
56
+ @ DisplayName ("Merge should retain all the attributes from the existing context when overriding context is null" )
57
+ @ Test
58
+ void mergeShouldReturnAllTheValuesFromTheContextWhenOverridingContextIsNull () {
59
+ HashMap <String , Value > attributes = new HashMap <>();
60
+ attributes .put ("key1" , new Value ("val1" ));
61
+ attributes .put ("key2" , new Value ("val2" ));
62
+ EvaluationContext ctx = new ImmutableContext ("targeting_key" , attributes );
63
+ EvaluationContext merge = ctx .merge (null );
64
+ assertEquals ("targeting_key" , merge .getTargetingKey ());
65
+ assertArrayEquals (merge .keySet ().toArray (), new Object []{"key1" , "key2" });
66
+ }
30
67
}
0 commit comments