File tree Expand file tree Collapse file tree
operator-framework/src/test/java/io/javaoperatorsdk/operator
externalstatebulkdependent Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616package io .javaoperatorsdk .operator .dependent .externalstate ;
1717
1818import org .junit .jupiter .api .Test ;
19+ import org .junit .jupiter .api .extension .ExtendWith ;
1920
2021import io .fabric8 .kubernetes .api .model .ConfigMap ;
2122import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
2223import io .javaoperatorsdk .operator .junit .LocallyRunOperatorExtension ;
2324import io .javaoperatorsdk .operator .support .ExternalIDGenServiceMock ;
25+ import io .javaoperatorsdk .operator .support .ExternalServiceResetExtension ;
2426
2527import static io .javaoperatorsdk .operator .dependent .externalstate .ExternalStateReconciler .ID_KEY ;
2628import static org .assertj .core .api .Assertions .assertThat ;
2729import static org .awaitility .Awaitility .await ;
2830
31+ @ ExtendWith (ExternalServiceResetExtension .class )
2932public abstract class ExternalStateTestBase {
3033
3134 private static final String TEST_RESOURCE_NAME = "test1" ;
Original file line number Diff line number Diff line change 1818import java .time .Duration ;
1919
2020import org .junit .jupiter .api .Test ;
21+ import org .junit .jupiter .api .extension .ExtendWith ;
2122import org .junit .jupiter .api .extension .RegisterExtension ;
2223
2324import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
2425import io .javaoperatorsdk .annotation .Sample ;
2526import io .javaoperatorsdk .operator .junit .LocallyRunOperatorExtension ;
2627import io .javaoperatorsdk .operator .support .ExternalIDGenServiceMock ;
28+ import io .javaoperatorsdk .operator .support .ExternalServiceResetExtension ;
2729
2830import static org .assertj .core .api .Assertions .assertThat ;
2931import static org .awaitility .Awaitility .await ;
3739 allowing operators to track and reconcile a variable number of external resources with \
3840 persistent state that survives operator restarts.
3941 """ )
42+ @ ExtendWith (ExternalServiceResetExtension .class )
4043class ExternalStateBulkIT {
4144
4245 private static final String TEST_RESOURCE_NAME = "test1" ;
Original file line number Diff line number Diff line change @@ -50,6 +50,14 @@ public List<ExternalResource> listResources() {
5050 return new ArrayList <>(resourceMap .values ());
5151 }
5252
53+ /**
54+ * Clears the internal state. Intended to be called between tests so that state from one test does
55+ * not leak into the next via this JVM-lifetime singleton.
56+ */
57+ public void reset () {
58+ resourceMap .clear ();
59+ }
60+
5361 public static ExternalIDGenServiceMock getInstance () {
5462 return serviceMock ;
5563 }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright Java Operator SDK Authors
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package io .javaoperatorsdk .operator .support ;
17+
18+ import org .jspecify .annotations .NonNull ;
19+ import org .junit .jupiter .api .extension .BeforeEachCallback ;
20+ import org .junit .jupiter .api .extension .ExtensionContext ;
21+
22+ /**
23+ * Resets the {@link ExternalIDGenServiceMock} singleton before each test method so that state from
24+ * one test does not leak into the next. Apply via
25+ * {@code @ExtendWith(ExternalServiceResetExtension.class)}.
26+ */
27+ public class ExternalServiceResetExtension implements BeforeEachCallback {
28+
29+ @ Override
30+ public void beforeEach (@ NonNull ExtensionContext context ) {
31+ ExternalIDGenServiceMock .getInstance ().reset ();
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments