|
| 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 | +} |
0 commit comments