|
| 1 | +package io.javaoperatorsdk.operator; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 5 | + |
| 6 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 7 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 8 | +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; |
| 9 | +import io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.CustomMappingConfigMapDependentResource; |
| 10 | +import io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.DependentCustomMappingCustomResource; |
| 11 | +import io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.DependentCustomMappingReconciler; |
| 12 | +import io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.DependentCustomMappingSpec; |
| 13 | + |
| 14 | +import static io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.CustomMappingConfigMapDependentResource.CUSTOM_NAMESPACE_KEY; |
| 15 | +import static io.javaoperatorsdk.operator.sample.dependentcustommappingannotation.CustomMappingConfigMapDependentResource.CUSTOM_NAME_KEY; |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.awaitility.Awaitility.await; |
| 18 | + |
| 19 | +class DependentCustomMappingAnnotationIT { |
| 20 | + |
| 21 | + public static final String INITIAL_VALUE = "initial value"; |
| 22 | + public static final String CHANGED_VALUE = "changed value"; |
| 23 | + public static final String TEST_RESOURCE_NAME = "test1"; |
| 24 | + |
| 25 | + @RegisterExtension |
| 26 | + LocallyRunOperatorExtension extension = |
| 27 | + LocallyRunOperatorExtension.builder() |
| 28 | + .withReconciler(DependentCustomMappingReconciler.class) |
| 29 | + .build(); |
| 30 | + |
| 31 | + |
| 32 | + @Test |
| 33 | + void testCustomMappingAnnotationForDependent() { |
| 34 | + var cr = extension.create(testResource()); |
| 35 | + assertConfigMapData(INITIAL_VALUE); |
| 36 | + |
| 37 | + cr.getSpec().setValue(CHANGED_VALUE); |
| 38 | + cr = extension.replace(cr); |
| 39 | + assertConfigMapData(CHANGED_VALUE); |
| 40 | + |
| 41 | + extension.delete(cr); |
| 42 | + |
| 43 | + await().untilAsserted(() -> { |
| 44 | + var resource = extension.get(ConfigMap.class, TEST_RESOURCE_NAME); |
| 45 | + assertThat(resource).isNull(); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + private void assertConfigMapData(String val) { |
| 50 | + await().untilAsserted(() -> { |
| 51 | + var resource = extension.get(ConfigMap.class, TEST_RESOURCE_NAME); |
| 52 | + assertThat(resource).isNotNull(); |
| 53 | + assertThat(resource.getMetadata().getAnnotations()) |
| 54 | + .containsKey(CUSTOM_NAME_KEY) |
| 55 | + .containsKey(CUSTOM_NAMESPACE_KEY); |
| 56 | + assertThat(resource.getData()).containsEntry(CustomMappingConfigMapDependentResource.KEY, |
| 57 | + val); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + DependentCustomMappingCustomResource testResource() { |
| 63 | + var dr = new DependentCustomMappingCustomResource(); |
| 64 | + dr.setMetadata(new ObjectMetaBuilder().withName(TEST_RESOURCE_NAME).build()); |
| 65 | + dr.setSpec(new DependentCustomMappingSpec()); |
| 66 | + dr.getSpec().setValue(INITIAL_VALUE); |
| 67 | + |
| 68 | + return dr; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +} |
0 commit comments