Skip to content

Commit d492ff6

Browse files
committed
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>
1 parent 204ac34 commit d492ff6

File tree

3 files changed

+129
-72
lines changed

3 files changed

+129
-72
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/managed/DefaultManagedDependentResourceContext.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ public <T> T put(Object key, T value) {
2828
return (T) Optional.ofNullable(attributes.put(key, value));
2929
}
3030

31+
@Override
32+
@SuppressWarnings("unchecked")
33+
public <T> Optional<T> putOrRemove(Object key, T value) {
34+
if (value == null) {
35+
return (Optional<T>) Optional.ofNullable(attributes.remove(key));
36+
}
37+
return (Optional<T>) Optional.ofNullable(attributes.put(key, value));
38+
}
39+
3140
@Override
3241
@SuppressWarnings("unused")
3342
public <T> T getMandatory(Object key, Class<T> expectedType) {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/managed/ManagedDependentResourceContext.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ public interface ManagedDependentResourceContext {
4040
@SuppressWarnings("unchecked")
4141
<T> T put(Object key, T value);
4242

43+
/**
44+
* Associates the specified contextual value to the specified key. If the value is {@code null},
45+
* the semantics of this operation is defined as removing the mapping associated with the
46+
* specified key.
47+
*
48+
* @param <T> object type
49+
* @param key the key identifying which contextual object to add or remove from the context
50+
* @param value the value to add to the context or {@code null} to remove an existing entry
51+
* associated with the specified key
52+
* @return an Optional containing the previous value associated with the key or
53+
* {@link Optional#empty()} if none existed
54+
*/
55+
@SuppressWarnings("unchecked")
56+
<T> Optional<T> putOrRemove(Object key, T value);
57+
4358
/**
4459
* Retrieves the value associated with the key or fail with an exception if none exists.
4560
*
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,117 @@
11
package io.javaoperatorsdk.operator.api.reconciler.dependent.managed;
22

3-
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowReconcileResult;
4-
import org.junit.jupiter.api.Test;
5-
63
import java.util.List;
74
import java.util.Map;
85
import java.util.Optional;
96

7+
import org.junit.jupiter.api.Test;
8+
9+
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowReconcileResult;
10+
1011
import static io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DefaultManagedDependentResourceContext.RECONCILE_RESULT_KEY;
1112
import static org.assertj.core.api.Assertions.assertThat;
1213
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1314

1415
class DefaultManagedDependentResourceContextTest {
1516

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-
}
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 putOrRemoveNewValueOverwrites() {
42+
context.putOrRemove("key", "value");
43+
context.putOrRemove("key", "valueB");
44+
Optional<String> actual = context.get("key", String.class);
45+
assertThat(actual).contains("valueB");
46+
}
47+
48+
@Test
49+
void putNewValueReturnsPriorValue() {
50+
context.put("key", "value");
51+
Optional<String> actual = (Optional<String>) (Object) context.put("key", "valueB");
52+
assertThat(actual).contains("value");
53+
}
54+
55+
@Test
56+
void putOrRemoveNewValueReturnsPriorValue() {
57+
context.put("key", "value");
58+
Optional<String> actual = context.putOrRemove("key", "valueB");
59+
assertThat(actual).contains("value");
60+
}
61+
62+
@Test
63+
void putNullRemoves() {
64+
context.put("key", "value");
65+
context.put("key", null);
66+
Optional<String> actual = context.get("key", String.class);
67+
assertThat(actual).isEmpty();
68+
}
69+
70+
@Test
71+
void putOrRemoveNullRemoves() {
72+
context.putOrRemove("key", "value");
73+
context.putOrRemove("key", null);
74+
Optional<String> actual = context.get("key", String.class);
75+
assertThat(actual).isEmpty();
76+
}
77+
78+
@Test
79+
void putNullReturnsPriorValue() {
80+
context.put("key", "value");
81+
Optional<String> actual = context.put("key", null);
82+
assertThat(actual).contains("value");
83+
}
84+
85+
@Test
86+
void putOrRemoveNullReturnsPriorValue() {
87+
context.putOrRemove("key", "value");
88+
Optional<String> actual = context.putOrRemove("key", null);
89+
assertThat(actual).contains("value");
90+
}
91+
92+
@Test
93+
void getMandatory() {
94+
context.put("key", "value");
95+
String actual = context.getMandatory("key", String.class);
96+
assertThat(actual).isEqualTo("value");
97+
}
98+
99+
@Test
100+
void getMandatoryWhenEmpty() {
101+
assertThatThrownBy(() -> {
102+
context.getMandatory("key", String.class);
103+
}).isInstanceOf(IllegalStateException.class)
104+
.hasMessage(
105+
"Mandatory attribute (key: key, type: java.lang.String) is missing or not of the expected type");
106+
}
107+
108+
@Test
109+
void getWorkflowReconcileResult() {
110+
WorkflowReconcileResult result =
111+
new WorkflowReconcileResult(List.of(), List.of(), Map.of(), Map.of());
112+
context.put(RECONCILE_RESULT_KEY, result);
113+
Optional<WorkflowReconcileResult> actual = context.getWorkflowReconcileResult();
114+
assertThat(actual).containsSame(result);
115+
}
116+
117+
}

0 commit comments

Comments
 (0)