Skip to content

Commit 47c32f6

Browse files
committed
Update declaration to return Optional
It was declared as returning T, but javadocced and implemented returning Optional<T> Signed-off-by: Robert Young <[email protected]>
1 parent 5849369 commit 47c32f6

File tree

3 files changed

+87
-76
lines changed

3 files changed

+87
-76
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public <T> Optional<T> get(Object key, Class<T> expectedType) {
2121

2222
@Override
2323
@SuppressWarnings("unchecked")
24-
public <T> T put(Object key, T value) {
24+
public <T> Optional<T> put(Object key, T value) {
2525
if (value == null) {
26-
return (T) Optional.ofNullable(attributes.remove(key));
26+
return (Optional<T>) Optional.ofNullable(attributes.remove(key));
2727
}
28-
return (T) Optional.ofNullable(attributes.put(key, value));
28+
return (Optional<T>) Optional.ofNullable(attributes.put(key, value));
2929
}
3030

3131
@Override

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public interface ManagedDependentResourceContext {
3838
* {@link Optional#empty()} if none existed
3939
*/
4040
@SuppressWarnings("unchecked")
41-
<T> T put(Object key, T value);
41+
<T> Optional<T> put(Object key, T value);
4242

4343
/**
4444
* Retrieves the value associated with the key or fail with an exception if none exists.
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,95 @@
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+
// a bit scary that it could mask a typing issue by returning empty
33+
@Test
34+
void getTypeMismatch() {
35+
context.put("key", 1L);
36+
Optional<String> actual = context.get("key", String.class);
37+
assertThat(actual).isEmpty();
38+
}
39+
40+
@Test
41+
void putNewValueOverwrites() {
42+
context.put("key", "value");
43+
context.put("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 = context.put("key", "valueB");
52+
assertThat(actual).contains("value");
53+
}
54+
55+
@Test
56+
void putNullRemoves() {
57+
context.put("key", "value");
58+
context.put("key", null);
59+
Optional<String> actual = context.get("key", String.class);
60+
assertThat(actual).isEmpty();
61+
}
62+
63+
@Test
64+
void putNullReturnsPriorValue() {
65+
context.put("key", "value");
66+
Optional<String> actual = context.put("key", null);
67+
assertThat(actual).contains("value");
68+
}
69+
70+
@Test
71+
void getMandatory() {
72+
context.put("key", "value");
73+
String actual = context.getMandatory("key", String.class);
74+
assertThat(actual).isEqualTo("value");
75+
}
76+
77+
@Test
78+
void getMandatoryWhenEmpty() {
79+
assertThatThrownBy(() -> {
80+
context.getMandatory("key", String.class);
81+
}).isInstanceOf(IllegalStateException.class)
82+
.hasMessage(
83+
"Mandatory attribute (key: key, type: java.lang.String) is missing or not of the expected type");
84+
}
85+
86+
@Test
87+
void getWorkflowReconcileResult() {
88+
WorkflowReconcileResult result =
89+
new WorkflowReconcileResult(List.of(), List.of(), Map.of(), Map.of());
90+
context.put(RECONCILE_RESULT_KEY, result);
91+
Optional<WorkflowReconcileResult> actual = context.getWorkflowReconcileResult();
92+
assertThat(actual).containsSame(result);
93+
}
94+
95+
}

0 commit comments

Comments
 (0)