-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add putOrRemove to DefaultManagedDependentResourceContext
The javadoc of the existing put method states that it returns Optional. The implementation also returns Optional, but the method signature declares it returns T. Meaning the caller has to cast it to Optional to get at the return value. This new method will supercede the existing put method Signed-off-by: Robert Young <robeyoun@redhat.com>
- Loading branch information
Showing
3 changed files
with
129 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 105 additions & 72 deletions
177
...operator/api/reconciler/dependent/managed/DefaultManagedDependentResourceContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,117 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler.dependent.managed; | ||
|
||
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowReconcileResult; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowReconcileResult; | ||
|
||
import static io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DefaultManagedDependentResourceContext.RECONCILE_RESULT_KEY; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class DefaultManagedDependentResourceContextTest { | ||
|
||
private ManagedDependentResourceContext context = new DefaultManagedDependentResourceContext(); | ||
|
||
@Test | ||
void getWhenEmpty() { | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).isEmpty(); | ||
} | ||
|
||
@Test | ||
void get() { | ||
context.put("key", "value"); | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).contains("value"); | ||
} | ||
|
||
@Test | ||
void putNewValueOverwrites() { | ||
context.put("key", "value"); | ||
context.put("key", "valueB"); | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).contains("valueB"); | ||
} | ||
|
||
@Test | ||
void putNewValueReturnsPriorValue() { | ||
context.put("key", "value"); | ||
Optional<String> actual = (Optional<String>) (Object)context.put("key", "valueB"); | ||
assertThat(actual).contains("value"); | ||
} | ||
|
||
@Test | ||
void putNullRemoves() { | ||
context.put("key", "value"); | ||
context.put("key", null); | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).isEmpty(); | ||
} | ||
|
||
@Test | ||
void putNullReturnsPriorValue() { | ||
context.put("key", "value"); | ||
Optional<String> actual = context.put("key", null); | ||
assertThat(actual).contains("value"); | ||
} | ||
|
||
@Test | ||
void getMandatory() { | ||
context.put("key", "value"); | ||
String actual = context.getMandatory("key", String.class); | ||
assertThat(actual).isEqualTo("value"); | ||
} | ||
|
||
@Test | ||
void getMandatoryWhenEmpty() { | ||
assertThatThrownBy(() -> { | ||
context.getMandatory("key", String.class); | ||
}).isInstanceOf(IllegalStateException.class) | ||
.hasMessage("Mandatory attribute (key: key, type: java.lang.String) is missing or not of the expected type"); | ||
} | ||
|
||
@Test | ||
void getWorkflowReconcileResult() { | ||
WorkflowReconcileResult result = new WorkflowReconcileResult(List.of(), List.of(), Map.of(), Map.of()); | ||
context.put(RECONCILE_RESULT_KEY, result); | ||
Optional<WorkflowReconcileResult> actual = context.getWorkflowReconcileResult(); | ||
assertThat(actual).containsSame(result); | ||
} | ||
|
||
} | ||
private ManagedDependentResourceContext context = new DefaultManagedDependentResourceContext(); | ||
|
||
@Test | ||
void getWhenEmpty() { | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).isEmpty(); | ||
} | ||
|
||
@Test | ||
void get() { | ||
context.put("key", "value"); | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).contains("value"); | ||
} | ||
|
||
@Test | ||
void putNewValueOverwrites() { | ||
context.put("key", "value"); | ||
context.put("key", "valueB"); | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).contains("valueB"); | ||
} | ||
|
||
@Test | ||
void putOrRemoveNewValueOverwrites() { | ||
context.putOrRemove("key", "value"); | ||
context.putOrRemove("key", "valueB"); | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).contains("valueB"); | ||
} | ||
|
||
@Test | ||
void putNewValueReturnsPriorValue() { | ||
context.put("key", "value"); | ||
Optional<String> actual = (Optional<String>) (Object) context.put("key", "valueB"); | ||
assertThat(actual).contains("value"); | ||
} | ||
|
||
@Test | ||
void putOrRemoveNewValueReturnsPriorValue() { | ||
context.put("key", "value"); | ||
Optional<String> actual = context.putOrRemove("key", "valueB"); | ||
assertThat(actual).contains("value"); | ||
} | ||
|
||
@Test | ||
void putNullRemoves() { | ||
context.put("key", "value"); | ||
context.put("key", null); | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).isEmpty(); | ||
} | ||
|
||
@Test | ||
void putOrRemoveNullRemoves() { | ||
context.putOrRemove("key", "value"); | ||
context.putOrRemove("key", null); | ||
Optional<String> actual = context.get("key", String.class); | ||
assertThat(actual).isEmpty(); | ||
} | ||
|
||
@Test | ||
void putNullReturnsPriorValue() { | ||
context.put("key", "value"); | ||
Optional<String> actual = context.put("key", null); | ||
assertThat(actual).contains("value"); | ||
} | ||
|
||
@Test | ||
void putOrRemoveNullReturnsPriorValue() { | ||
context.putOrRemove("key", "value"); | ||
Optional<String> actual = context.putOrRemove("key", null); | ||
assertThat(actual).contains("value"); | ||
} | ||
|
||
@Test | ||
void getMandatory() { | ||
context.put("key", "value"); | ||
String actual = context.getMandatory("key", String.class); | ||
assertThat(actual).isEqualTo("value"); | ||
} | ||
|
||
@Test | ||
void getMandatoryWhenEmpty() { | ||
assertThatThrownBy(() -> { | ||
context.getMandatory("key", String.class); | ||
}).isInstanceOf(IllegalStateException.class) | ||
.hasMessage( | ||
"Mandatory attribute (key: key, type: java.lang.String) is missing or not of the expected type"); | ||
} | ||
|
||
@Test | ||
void getWorkflowReconcileResult() { | ||
WorkflowReconcileResult result = | ||
new WorkflowReconcileResult(List.of(), List.of(), Map.of(), Map.of()); | ||
context.put(RECONCILE_RESULT_KEY, result); | ||
Optional<WorkflowReconcileResult> actual = context.getWorkflowReconcileResult(); | ||
assertThat(actual).containsSame(result); | ||
} | ||
|
||
} |