Skip to content

Commit 5849369

Browse files
committed
Add unit tests for DefaultManagedDependentResourceContext
Signed-off-by: Robert Young <[email protected]>
1 parent 41c8ee1 commit 5849369

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.javaoperatorsdk.operator.api.reconciler.dependent.managed;
2+
3+
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowReconcileResult;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Optional;
9+
10+
import static io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DefaultManagedDependentResourceContext.RECONCILE_RESULT_KEY;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
13+
14+
class DefaultManagedDependentResourceContextTest {
15+
16+
private ManagedDependentResourceContext context = new DefaultManagedDependentResourceContext();
17+
18+
@Test
19+
void getWhenEmpty() {
20+
Optional<String> actual = context.get("key", String.class);
21+
assertThat(actual).isEmpty();
22+
}
23+
24+
@Test
25+
void get() {
26+
context.put("key", "value");
27+
Optional<String> actual = context.get("key", String.class);
28+
assertThat(actual).contains("value");
29+
}
30+
31+
@Test
32+
void putNewValueOverwrites() {
33+
context.put("key", "value");
34+
context.put("key", "valueB");
35+
Optional<String> actual = context.get("key", String.class);
36+
assertThat(actual).contains("valueB");
37+
}
38+
39+
@Test
40+
void putNewValueReturnsPriorValue() {
41+
context.put("key", "value");
42+
Optional<String> actual = (Optional<String>) (Object)context.put("key", "valueB");
43+
assertThat(actual).contains("value");
44+
}
45+
46+
@Test
47+
void putNullRemoves() {
48+
context.put("key", "value");
49+
context.put("key", null);
50+
Optional<String> actual = context.get("key", String.class);
51+
assertThat(actual).isEmpty();
52+
}
53+
54+
@Test
55+
void putNullReturnsPriorValue() {
56+
context.put("key", "value");
57+
Optional<String> actual = context.put("key", null);
58+
assertThat(actual).contains("value");
59+
}
60+
61+
@Test
62+
void getMandatory() {
63+
context.put("key", "value");
64+
String actual = context.getMandatory("key", String.class);
65+
assertThat(actual).isEqualTo("value");
66+
}
67+
68+
@Test
69+
void getMandatoryWhenEmpty() {
70+
assertThatThrownBy(() -> {
71+
context.getMandatory("key", String.class);
72+
}).isInstanceOf(IllegalStateException.class)
73+
.hasMessage("Mandatory attribute (key: key, type: java.lang.String) is missing or not of the expected type");
74+
}
75+
76+
@Test
77+
void getWorkflowReconcileResult() {
78+
WorkflowReconcileResult result = new WorkflowReconcileResult(List.of(), List.of(), Map.of(), Map.of());
79+
context.put(RECONCILE_RESULT_KEY, result);
80+
Optional<WorkflowReconcileResult> actual = context.getWorkflowReconcileResult();
81+
assertThat(actual).containsSame(result);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)