2121import org .junit .jupiter .api .Test ;
2222
2323import io .fabric8 .kubernetes .api .model .ConfigMap ;
24+ import io .fabric8 .kubernetes .api .model .ConfigMapBuilder ;
2425import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
2526import io .javaoperatorsdk .operator .api .reconciler .Context ;
2627import io .javaoperatorsdk .operator .api .reconciler .DefaultContext ;
3334class AbstractDependentResourceTest {
3435
3536 private static final TestCustomResource PRIMARY = new TestCustomResource ();
36- private static final DefaultContext <TestCustomResource > CONTEXT =
37- new DefaultContext <>(mock (), mock (), PRIMARY , false , false );
37+ private static final DefaultContext <TestCustomResource > CONTEXT = createContext (PRIMARY );
38+
39+ private static DefaultContext <TestCustomResource > createContext (TestCustomResource primary ) {
40+ return new DefaultContext <>(mock (), mock (), primary , false , false );
41+ }
3842
3943 @ Test
4044 void throwsExceptionIfDesiredIsNullOnCreate () {
@@ -76,6 +80,27 @@ void throwsExceptionIfUpdateReturnsNull() {
7680 DependentResourceException .class , () -> testDependentResource .reconcile (PRIMARY , CONTEXT ));
7781 }
7882
83+ @ Test
84+ void checkThatDesiredIsOnlyCalledOnce () {
85+ final var testDependentResource = new DesiredCallCountCheckingDR ();
86+ final var primary = new TestCustomResource ();
87+ final var spec = primary .getSpec ();
88+ spec .setConfigMapName ("foo" );
89+ spec .setKey ("key" );
90+ spec .setValue ("value" );
91+ final var context = createContext (primary );
92+ testDependentResource .reconcile (primary , context );
93+
94+ spec .setValue ("value2" );
95+ testDependentResource .reconcile (primary , context );
96+
97+ assertEquals (1 , testDependentResource .desiredCallCount );
98+
99+ context .getOrComputeDesiredStateFor (
100+ testDependentResource , p -> testDependentResource .desired (p , context ));
101+ assertEquals (1 , testDependentResource .desiredCallCount );
102+ }
103+
79104 private ConfigMap configMap () {
80105 ConfigMap configMap = new ConfigMap ();
81106 configMap .setMetadata (
@@ -131,22 +156,12 @@ protected ConfigMap desired(TestCustomResource primary, Context<TestCustomResour
131156 return desired ;
132157 }
133158
134- public ConfigMap getSecondary () {
135- return secondary ;
136- }
137-
138- public TestDependentResource setSecondary (ConfigMap secondary ) {
159+ public void setSecondary (ConfigMap secondary ) {
139160 this .secondary = secondary ;
140- return this ;
141- }
142-
143- public ConfigMap getDesired () {
144- return desired ;
145161 }
146162
147- public TestDependentResource setDesired (ConfigMap desired ) {
163+ public void setDesired (ConfigMap desired ) {
148164 this .desired = desired ;
149- return this ;
150165 }
151166
152167 @ Override
@@ -173,4 +188,35 @@ public Matcher.Result<ConfigMap> match(
173188 return result ;
174189 }
175190 }
191+
192+ private static class DesiredCallCountCheckingDR extends TestDependentResource {
193+ private short desiredCallCount ;
194+
195+ @ Override
196+ public ConfigMap update (
197+ ConfigMap actual ,
198+ ConfigMap desired ,
199+ TestCustomResource primary ,
200+ Context <TestCustomResource > context ) {
201+ return desired ;
202+ }
203+
204+ @ Override
205+ public ConfigMap create (
206+ ConfigMap desired , TestCustomResource primary , Context <TestCustomResource > context ) {
207+ return desired ;
208+ }
209+
210+ @ Override
211+ protected ConfigMap desired (TestCustomResource primary , Context <TestCustomResource > context ) {
212+ final var spec = primary .getSpec ();
213+ desiredCallCount ++;
214+ return new ConfigMapBuilder ()
215+ .editOrNewMetadata ()
216+ .withName (spec .getConfigMapName ())
217+ .endMetadata ()
218+ .addToData (spec .getKey (), spec .getValue ())
219+ .build ();
220+ }
221+ }
176222}
0 commit comments