-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Method signature mismatched with javadoc and implementation (#2587)
* refactor: add unit tests for DefaultManagedDependentResourceContext Signed-off-by: Robert Young <[email protected]> * fix: implementation of put should return value instead of Optional Signed-off-by: Chris Laprun <[email protected]> * fix: update test name Signed-off-by: Robert Young <[email protected]> --------- Signed-off-by: Robert Young <[email protected]> Signed-off-by: Chris Laprun <[email protected]> Co-authored-by: Chris Laprun <[email protected]>
- Loading branch information
Showing
3 changed files
with
140 additions
and
5 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
105 changes: 105 additions & 0 deletions
105
...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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler.dependent.managed; | ||
|
||
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 final 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() { | ||
final var prior = "value"; | ||
context.put("key", prior); | ||
String actual = context.put("key", "valueB"); | ||
assertThat(actual).isEqualTo(prior); | ||
} | ||
|
||
@Test | ||
void putNewValueLogsWarningIfTypesDiffer() { | ||
// to check that we properly log things without setting up a complex fixture | ||
final String[] messages = new String[1]; | ||
var context = new DefaultManagedDependentResourceContext() { | ||
@Override | ||
void logWarning(String message) { | ||
messages[0] = message; | ||
} | ||
}; | ||
final var prior = "value"; | ||
final var key = "key"; | ||
context.put(key, prior); | ||
context.put(key, 10); | ||
assertThat(messages[0]).contains(key).contains(prior).contains("put(" + key + ", null)"); | ||
} | ||
|
||
@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"); | ||
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); | ||
} | ||
} |