|
| 1 | +package io.javaoperatorsdk.operator.processing.dependent; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 8 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 9 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 10 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
| 11 | +import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.*; |
| 14 | +import static org.mockito.Mockito.mock; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | + |
| 17 | +class AbstractDependentResourceTest { |
| 18 | + |
| 19 | + |
| 20 | + @Test |
| 21 | + void throwsExceptionIfDesiredIsNullOnCreate() { |
| 22 | + TestDependentResource testDependentResource = new TestDependentResource(); |
| 23 | + testDependentResource.setSecondary(null); |
| 24 | + testDependentResource.setDesired(null); |
| 25 | + |
| 26 | + assertThrows(DependentResourceException.class, |
| 27 | + () -> testDependentResource.reconcile(new TestCustomResource(), null)); |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void throwsExceptionIfDesiredIsNullOnUpdate() { |
| 33 | + TestDependentResource testDependentResource = new TestDependentResource(); |
| 34 | + testDependentResource.setSecondary(configMap()); |
| 35 | + testDependentResource.setDesired(null); |
| 36 | + |
| 37 | + assertThrows(DependentResourceException.class, |
| 38 | + () -> testDependentResource.reconcile(new TestCustomResource(), null)); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void throwsExceptionIfCreateReturnsNull() { |
| 43 | + TestDependentResource testDependentResource = new TestDependentResource(); |
| 44 | + testDependentResource.setSecondary(null); |
| 45 | + testDependentResource.setDesired(configMap()); |
| 46 | + |
| 47 | + assertThrows(DependentResourceException.class, |
| 48 | + () -> testDependentResource.reconcile(new TestCustomResource(), null)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void throwsExceptionIfUpdateReturnsNull() { |
| 53 | + TestDependentResource testDependentResource = new TestDependentResource(); |
| 54 | + testDependentResource.setSecondary(configMap()); |
| 55 | + testDependentResource.setDesired(configMap()); |
| 56 | + |
| 57 | + assertThrows(DependentResourceException.class, |
| 58 | + () -> testDependentResource.reconcile(new TestCustomResource(), null)); |
| 59 | + } |
| 60 | + |
| 61 | + private ConfigMap configMap() { |
| 62 | + ConfigMap configMap = new ConfigMap(); |
| 63 | + configMap.setMetadata(new ObjectMetaBuilder() |
| 64 | + .withName("test") |
| 65 | + .withNamespace("default") |
| 66 | + .build()); |
| 67 | + return configMap; |
| 68 | + } |
| 69 | + |
| 70 | + private static class TestDependentResource |
| 71 | + extends AbstractDependentResource<ConfigMap, TestCustomResource> |
| 72 | + implements Creator<ConfigMap, TestCustomResource>, Updater<ConfigMap, TestCustomResource> { |
| 73 | + |
| 74 | + private ConfigMap secondary; |
| 75 | + private ConfigMap desired; |
| 76 | + |
| 77 | + @Override |
| 78 | + public Class<ConfigMap> resourceType() { |
| 79 | + return ConfigMap.class; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Optional<ConfigMap> getSecondaryResource(TestCustomResource primary) { |
| 84 | + return Optional.ofNullable(secondary); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void onCreated(ResourceID primaryResourceId, ConfigMap created) {} |
| 89 | + |
| 90 | + @Override |
| 91 | + protected void onUpdated(ResourceID primaryResourceId, ConfigMap updated, ConfigMap actual) {} |
| 92 | + |
| 93 | + @Override |
| 94 | + protected ConfigMap desired(TestCustomResource primary, Context<TestCustomResource> context) { |
| 95 | + return desired; |
| 96 | + } |
| 97 | + |
| 98 | + public ConfigMap getSecondary() { |
| 99 | + return secondary; |
| 100 | + } |
| 101 | + |
| 102 | + public TestDependentResource setSecondary(ConfigMap secondary) { |
| 103 | + this.secondary = secondary; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + public ConfigMap getDesired() { |
| 108 | + return desired; |
| 109 | + } |
| 110 | + |
| 111 | + public TestDependentResource setDesired(ConfigMap desired) { |
| 112 | + this.desired = desired; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public ConfigMap create(ConfigMap desired, TestCustomResource primary, |
| 118 | + Context<TestCustomResource> context) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public ConfigMap update(ConfigMap actual, ConfigMap desired, TestCustomResource primary, |
| 124 | + Context<TestCustomResource> context) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Matcher.Result<ConfigMap> match(ConfigMap actualResource, TestCustomResource primary, |
| 130 | + Context<TestCustomResource> context) { |
| 131 | + var result = mock(Matcher.Result.class); |
| 132 | + when(result.matched()).thenReturn(false); |
| 133 | + return result; |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments