Skip to content

Commit 5810080

Browse files
committed
unit tests
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 9d82591 commit 5810080

File tree

4 files changed

+400
-0
lines changed

4 files changed

+400
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package io.javaoperatorsdk.operator.processing.expectation;
2+
3+
import java.time.Duration;
4+
import java.util.Optional;
5+
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import io.fabric8.kubernetes.api.model.ConfigMap;
10+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
11+
import io.javaoperatorsdk.operator.api.reconciler.Context;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.mockito.Mockito.mock;
15+
import static org.mockito.Mockito.when;
16+
17+
class ExpectationManagerTest {
18+
19+
private ExpectationManager<ConfigMap> expectationManager;
20+
private ConfigMap configMap;
21+
private Context<ConfigMap> context;
22+
23+
@BeforeEach
24+
void setUp() {
25+
expectationManager = new ExpectationManager<>();
26+
configMap = new ConfigMap();
27+
configMap.setMetadata(
28+
new ObjectMetaBuilder().withName("test-configmap").withNamespace("test-namespace").build());
29+
context = mock(Context.class);
30+
}
31+
32+
@Test
33+
void setExpectationShouldStoreExpectation() {
34+
Expectation<ConfigMap> expectation = mock(Expectation.class);
35+
Duration timeout = Duration.ofMinutes(5);
36+
37+
expectationManager.setExpectation(configMap, expectation, timeout);
38+
39+
assertThat(expectationManager.isExpectationPresent(configMap)).isTrue();
40+
assertThat(expectationManager.getExpectation(configMap)).contains(expectation);
41+
}
42+
43+
@Test
44+
void checkOnExpectationShouldReturnEmptyWhenNoExpectation() {
45+
Optional<ExpectationResult<ConfigMap>> result =
46+
expectationManager.checkOnExpectation(configMap, context);
47+
48+
assertThat(result).isEmpty();
49+
}
50+
51+
@Test
52+
void checkOnExpectationShouldReturnFulfilledWhenExpectationMet() {
53+
Expectation<ConfigMap> expectation = mock(Expectation.class);
54+
when(expectation.isFulfilled(configMap, context)).thenReturn(true);
55+
56+
expectationManager.setExpectation(configMap, expectation, Duration.ofMinutes(5));
57+
Optional<ExpectationResult<ConfigMap>> result =
58+
expectationManager.checkOnExpectation(configMap, context);
59+
60+
assertThat(result).isPresent();
61+
assertThat(result.get().status()).isEqualTo(ExpectationStatus.FULFILLED);
62+
assertThat(result.get().expectation()).isEqualTo(expectation);
63+
assertThat(expectationManager.isExpectationPresent(configMap)).isFalse();
64+
}
65+
66+
@Test
67+
void checkOnExpectationShouldReturnNotFulfilledWhenExpectationNotMet() {
68+
Expectation<ConfigMap> expectation = mock(Expectation.class);
69+
when(expectation.isFulfilled(configMap, context)).thenReturn(false);
70+
71+
expectationManager.setExpectation(configMap, expectation, Duration.ofMinutes(5));
72+
Optional<ExpectationResult<ConfigMap>> result =
73+
expectationManager.checkOnExpectation(configMap, context);
74+
75+
assertThat(result).isPresent();
76+
assertThat(result.get().status()).isEqualTo(ExpectationStatus.NOT_FULFILLED);
77+
assertThat(result.get().expectation()).isEqualTo(expectation);
78+
assertThat(expectationManager.isExpectationPresent(configMap)).isTrue();
79+
}
80+
81+
@Test
82+
void checkOnExpectationShouldReturnTimedOutWhenExpectationExpired() throws InterruptedException {
83+
Expectation<ConfigMap> expectation = mock(Expectation.class);
84+
when(expectation.isFulfilled(configMap, context)).thenReturn(false);
85+
86+
expectationManager.setExpectation(configMap, expectation, Duration.ofMillis(1));
87+
Thread.sleep(10);
88+
Optional<ExpectationResult<ConfigMap>> result =
89+
expectationManager.checkOnExpectation(configMap, context);
90+
91+
assertThat(result).isPresent();
92+
assertThat(result.get().status()).isEqualTo(ExpectationStatus.TIMED_OUT);
93+
assertThat(result.get().expectation()).isEqualTo(expectation);
94+
assertThat(expectationManager.isExpectationPresent(configMap)).isFalse();
95+
}
96+
97+
@Test
98+
void getExpectationNameShouldReturnExpectationName() {
99+
String expectedName = "test-expectation";
100+
Expectation<ConfigMap> expectation = mock(Expectation.class);
101+
when(expectation.name()).thenReturn(expectedName);
102+
103+
expectationManager.setExpectation(configMap, expectation, Duration.ofMinutes(5));
104+
Optional<String> name = expectationManager.getExpectationName(configMap);
105+
106+
assertThat(name).contains(expectedName);
107+
}
108+
109+
@Test
110+
void getExpectationNameShouldReturnEmptyWhenNoExpectation() {
111+
Optional<String> name = expectationManager.getExpectationName(configMap);
112+
113+
assertThat(name).isEmpty();
114+
}
115+
116+
@Test
117+
void cleanupShouldRemoveExpectation() {
118+
Expectation<ConfigMap> expectation = mock(Expectation.class);
119+
120+
expectationManager.setExpectation(configMap, expectation, Duration.ofMinutes(5));
121+
assertThat(expectationManager.isExpectationPresent(configMap)).isTrue();
122+
123+
expectationManager.cleanup(configMap);
124+
assertThat(expectationManager.isExpectationPresent(configMap)).isFalse();
125+
}
126+
127+
@Test
128+
void shouldHandleMultipleExpectationsForDifferentResources() {
129+
ConfigMap configMap2 = new ConfigMap();
130+
configMap2.setMetadata(
131+
new ObjectMetaBuilder()
132+
.withName("test-configmap-2")
133+
.withNamespace("test-namespace")
134+
.build());
135+
136+
Expectation<ConfigMap> expectation1 = mock(Expectation.class);
137+
Expectation<ConfigMap> expectation2 = mock(Expectation.class);
138+
139+
expectationManager.setExpectation(configMap, expectation1, Duration.ofMinutes(5));
140+
expectationManager.setExpectation(configMap2, expectation2, Duration.ofMinutes(5));
141+
142+
assertThat(expectationManager.isExpectationPresent(configMap)).isTrue();
143+
assertThat(expectationManager.isExpectationPresent(configMap2)).isTrue();
144+
assertThat(expectationManager.getExpectation(configMap)).contains(expectation1);
145+
assertThat(expectationManager.getExpectation(configMap2)).contains(expectation2);
146+
}
147+
148+
@Test
149+
void setExpectationShouldReplaceExistingExpectation() {
150+
Expectation<ConfigMap> expectation1 = mock(Expectation.class);
151+
Expectation<ConfigMap> expectation2 = mock(Expectation.class);
152+
153+
expectationManager.setExpectation(configMap, expectation1, Duration.ofMinutes(5));
154+
expectationManager.setExpectation(configMap, expectation2, Duration.ofMinutes(5));
155+
156+
assertThat(expectationManager.getExpectation(configMap)).contains(expectation2);
157+
}
158+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.javaoperatorsdk.operator.processing.expectation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
class ExpectationStatusTest {
8+
9+
@Test
10+
void shouldHaveThreeStatuses() {
11+
ExpectationStatus[] values = ExpectationStatus.values();
12+
13+
assertThat(values).hasSize(3);
14+
assertThat(values)
15+
.containsExactlyInAnyOrder(
16+
ExpectationStatus.FULFILLED,
17+
ExpectationStatus.NOT_FULFILLED,
18+
ExpectationStatus.TIMED_OUT);
19+
}
20+
21+
@Test
22+
void shouldHaveCorrectNames() {
23+
assertThat(ExpectationStatus.FULFILLED.name()).isEqualTo("FULFILLED");
24+
assertThat(ExpectationStatus.NOT_FULFILLED.name()).isEqualTo("NOT_FULFILLED");
25+
assertThat(ExpectationStatus.TIMED_OUT.name()).isEqualTo("TIMED_OUT");
26+
}
27+
28+
@Test
29+
void shouldSupportValueOf() {
30+
assertThat(ExpectationStatus.valueOf("FULFILLED")).isEqualTo(ExpectationStatus.FULFILLED);
31+
assertThat(ExpectationStatus.valueOf("NOT_FULFILLED"))
32+
.isEqualTo(ExpectationStatus.NOT_FULFILLED);
33+
assertThat(ExpectationStatus.valueOf("TIMED_OUT")).isEqualTo(ExpectationStatus.TIMED_OUT);
34+
}
35+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.javaoperatorsdk.operator.processing.expectation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import io.fabric8.kubernetes.api.model.ConfigMap;
6+
import io.javaoperatorsdk.operator.api.reconciler.Context;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.mockito.Mockito.mock;
10+
11+
class ExpectationTest {
12+
13+
@Test
14+
void createExpectationWithCustomName() {
15+
String customName = "test-expectation";
16+
Expectation<ConfigMap> expectation =
17+
Expectation.createExpectation(customName, (primary, context) -> true);
18+
19+
assertThat(expectation.name()).isEqualTo(customName);
20+
}
21+
22+
@Test
23+
void createExpectationWithPredicate() {
24+
ConfigMap configMap = new ConfigMap();
25+
Context<ConfigMap> context = mock(Context.class);
26+
27+
Expectation<ConfigMap> trueExpectation =
28+
Expectation.createExpectation("always-true", (primary, ctx) -> true);
29+
Expectation<ConfigMap> falseExpectation =
30+
Expectation.createExpectation("always-false", (primary, ctx) -> false);
31+
32+
assertThat(trueExpectation.isFulfilled(configMap, context)).isTrue();
33+
assertThat(falseExpectation.isFulfilled(configMap, context)).isFalse();
34+
}
35+
36+
@Test
37+
void expectationShouldWorkWithGenericTypes() {
38+
ConfigMap configMap = new ConfigMap();
39+
Context<ConfigMap> context = mock(Context.class);
40+
41+
Expectation<ConfigMap> expectation =
42+
new Expectation<>() {
43+
@Override
44+
public String name() {
45+
return "custom-expectation";
46+
}
47+
48+
@Override
49+
public boolean isFulfilled(ConfigMap primary, Context<ConfigMap> context) {
50+
return primary != null;
51+
}
52+
};
53+
54+
assertThat(expectation.name()).isEqualTo("custom-expectation");
55+
assertThat(expectation.isFulfilled(configMap, context)).isTrue();
56+
assertThat(expectation.isFulfilled(null, context)).isFalse();
57+
}
58+
}

0 commit comments

Comments
 (0)