Skip to content

Commit 233cb9f

Browse files
committed
test: address review comments and fix format
Signed-off-by: Dennis-Mircea Ciupitu <dennis.mircea.ciupitu@gmail.com>
1 parent b6bf6de commit 233cb9f

4 files changed

Lines changed: 41 additions & 14 deletions

File tree

operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/externalstate/ExternalStateTestBase.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515
*/
1616
package io.javaoperatorsdk.operator.dependent.externalstate;
1717

18-
import org.junit.jupiter.api.BeforeEach;
1918
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.ExtendWith;
2020

2121
import io.fabric8.kubernetes.api.model.ConfigMap;
2222
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
2323
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
2424
import io.javaoperatorsdk.operator.support.ExternalIDGenServiceMock;
25+
import io.javaoperatorsdk.operator.support.ExternalServiceResetExtension;
2526

2627
import static io.javaoperatorsdk.operator.dependent.externalstate.ExternalStateReconciler.ID_KEY;
2728
import static org.assertj.core.api.Assertions.assertThat;
2829
import static org.awaitility.Awaitility.await;
2930

31+
@ExtendWith(ExternalServiceResetExtension.class)
3032
public abstract class ExternalStateTestBase {
3133

3234
private static final String TEST_RESOURCE_NAME = "test1";
@@ -36,11 +38,6 @@ public abstract class ExternalStateTestBase {
3638

3739
private final ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock.getInstance();
3840

39-
@BeforeEach
40-
void resetExternalService() {
41-
externalService.reset();
42-
}
43-
4441
@Test
4542
public void reconcilesResourceWithPersistentState() {
4643
var resource = extension().create(testResource());

operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/externalstate/externalstatebulkdependent/ExternalStateBulkIT.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
import java.time.Duration;
1919

20-
import org.junit.jupiter.api.BeforeEach;
2120
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
2222
import org.junit.jupiter.api.extension.RegisterExtension;
2323

2424
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
2525
import io.javaoperatorsdk.annotation.Sample;
2626
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
2727
import io.javaoperatorsdk.operator.support.ExternalIDGenServiceMock;
28+
import io.javaoperatorsdk.operator.support.ExternalServiceResetExtension;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
3031
import static org.awaitility.Awaitility.await;
@@ -38,6 +39,7 @@
3839
allowing operators to track and reconcile a variable number of external resources with \
3940
persistent state that survives operator restarts.
4041
""")
42+
@ExtendWith(ExternalServiceResetExtension.class)
4143
class ExternalStateBulkIT {
4244

4345
private static final String TEST_RESOURCE_NAME = "test1";
@@ -50,11 +52,6 @@ class ExternalStateBulkIT {
5052

5153
private final ExternalIDGenServiceMock externalService = ExternalIDGenServiceMock.getInstance();
5254

53-
@BeforeEach
54-
void resetExternalService() {
55-
externalService.reset();
56-
}
57-
5855
@RegisterExtension
5956
LocallyRunOperatorExtension operator =
6057
LocallyRunOperatorExtension.builder()

operator-framework/src/test/java/io/javaoperatorsdk/operator/support/ExternalIDGenServiceMock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public List<ExternalResource> listResources() {
5151
}
5252

5353
/**
54-
* Clears the internal state. Intended to be called between tests so that state from one test
55-
* does not leak into the next via this JVM-lifetime singleton.
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.
5656
*/
5757
public void reset() {
5858
resourceMap.clear();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
 (0)