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