Skip to content

Commit bd5cf38

Browse files
committed
chore: polish and add some minimal javadoc to the junit5 extension
1 parent 98e3def commit bd5cf38

File tree

7 files changed

+96
-13
lines changed

7 files changed

+96
-13
lines changed

Diff for: operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/OperatorExtension.java

+88-5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ private OperatorExtension(
6767
this.waitForNamespaceDeletion = waitForNamespaceDeletion;
6868
}
6969

70+
/**
71+
* Creates a {@link Builder} to set up an {@link OperatorExtension} instance.
72+
*
73+
* @return the builder.
74+
*/
7075
public static Builder builder() {
7176
return new Builder();
7277
}
@@ -96,17 +101,32 @@ public KubernetesClient getKubernetesClient() {
96101
return kubernetesClient;
97102
}
98103

104+
/**
105+
* Returns the test namespace.
106+
*
107+
* @return the namespace name.
108+
*/
99109
public String getNamespace() {
100110
return namespace;
101111
}
102112

113+
/**
114+
* The list of controllers known by the operator.
115+
*
116+
* @return the list of {@link ResourceController}.
117+
*/
103118
@SuppressWarnings({"rawtypes"})
104119
public List<ResourceController> getControllers() {
105120
return operator.getControllers().stream()
106121
.map(ConfiguredController::getController)
107122
.collect(Collectors.toUnmodifiableList());
108123
}
109124

125+
/**
126+
* The list of controllers of the give {@code type} known by the operator.
127+
*
128+
* @return the list of {@link ResourceController} matching the required type.
129+
*/
110130
@SuppressWarnings({"rawtypes"})
111131
public <T extends ResourceController> T getControllerOfType(Class<T> type) {
112132
return operator.getControllers().stream()
@@ -123,22 +143,42 @@ public <T extends HasMetadata> NonNamespaceOperation<T, KubernetesResourceList<T
123143
return kubernetesClient.resources(type).inNamespace(namespace);
124144
}
125145

126-
public <T extends HasMetadata> T getNamedResource(Class<T> type, String name) {
146+
/**
147+
* Lookup a resource given its {@code type} and {@code name} in the current test namespace.
148+
*
149+
* @param type Class for resource
150+
* @param name the name of the resource
151+
* @param <T> T type represents resource type
152+
* @return The resource or null if it does not exist
153+
*/
154+
public <T extends HasMetadata> T get(Class<T> type, String name) {
127155
return kubernetesClient.resources(type).inNamespace(namespace).withName(name).get();
128156
}
129157

158+
/**
159+
* Creates a resource in the current test namespace.
160+
*
161+
* @param type Class for resource
162+
* @param resource the resource to create
163+
* @param <T> T type represents resource type
164+
* @return The resource or null if it does not exist
165+
*/
130166
public <T extends HasMetadata> T create(Class<T> type, T resource) {
131167
return kubernetesClient.resources(type).inNamespace(namespace).create(resource);
132168
}
133169

170+
/**
171+
* Replaces a resource in the current test namespace.
172+
*
173+
* @param type Class for resource
174+
* @param resource the resource to create
175+
* @param <T> T type represents resource type
176+
* @return The resource or null if it does not exist
177+
*/
134178
public <T extends HasMetadata> T replace(Class<T> type, T resource) {
135179
return kubernetesClient.resources(type).inNamespace(namespace).replace(resource);
136180
}
137181

138-
public <T extends HasMetadata> T get(Class<T> type, String name) {
139-
return kubernetesClient.resources(type).inNamespace(namespace).withName(name).get();
140-
}
141-
142182

143183
@SuppressWarnings("unchecked")
144184
protected void before(ExtensionContext context) {
@@ -225,33 +265,71 @@ protected Builder() {
225265
true);
226266
}
227267

268+
/**
269+
* Configures if the test namespace should be preserved in case of error for troubleshooting.
270+
*
271+
* @param value true if the namespace should be preserved.
272+
* @return this builder
273+
*/
228274
public Builder preserveNamespaceOnError(boolean value) {
229275
this.preserveNamespaceOnError = value;
230276
return this;
231277
}
232278

279+
/**
280+
* Configures if the extension should wait for the test namespace deletion.
281+
*
282+
* @param value true if the waiting for namespace deletion is required.
283+
* @return this builder
284+
*/
233285
public Builder waitForNamespaceDeletion(boolean value) {
234286
this.waitForNamespaceDeletion = value;
235287
return this;
236288
}
237289

290+
/**
291+
* Defines the {@link ConfigurationService} the operator should use.
292+
*
293+
* @param value the {@link ConfigurationService}.
294+
* @return this builder
295+
*/
238296
public Builder withConfigurationService(ConfigurationService value) {
239297
configurationService = value;
240298
return this;
241299
}
242300

301+
/**
302+
* Add a {@link ResourceController} to the operator.
303+
*
304+
* @param value the {@link ResourceController}
305+
* @return this builder
306+
*/
243307
@SuppressWarnings("rawtypes")
244308
public Builder withController(ResourceController value) {
245309
controllers.add(new ControllerSpec(value, null));
246310
return this;
247311
}
248312

313+
/**
314+
* Add a {@link ResourceController} to the operator with a {@link Retry} policy.
315+
*
316+
* @param value the {@link ResourceController}
317+
* @param retry the {@link Retry} policy.
318+
* @return this builder
319+
*/
249320
@SuppressWarnings("rawtypes")
250321
public Builder withController(ResourceController value, Retry retry) {
251322
controllers.add(new ControllerSpec(value, retry));
252323
return this;
253324
}
254325

326+
/**
327+
* Add a {@link ResourceController} to the operator by providing its class name. The controller
328+
* is instantiated using the default empty constructor.
329+
*
330+
* @param value the {@link ResourceController} type.
331+
* @return this builder
332+
*/
255333
@SuppressWarnings("rawtypes")
256334
public Builder withController(Class<? extends ResourceController> value) {
257335
try {
@@ -262,6 +340,11 @@ public Builder withController(Class<? extends ResourceController> value) {
262340
return this;
263341
}
264342

343+
/**
344+
* Build a new {@link OperatorExtension} instance.
345+
*
346+
* @return a new {@link OperatorExtension} instance.
347+
*/
265348
public OperatorExtension build() {
266349
return new OperatorExtension(
267350
configurationService,

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/ConcurrencyIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void manyResourcesGetCreatedUpdatedAndDeleted() throws InterruptedExcepti
5858
// update some resources
5959
for (int i = 0; i < NUMBER_OF_RESOURCES_UPDATED; i++) {
6060
TestCustomResource tcr =
61-
operator.getNamedResource(TestCustomResource.class,
61+
operator.get(TestCustomResource.class,
6262
TestUtils.TEST_CUSTOM_RESOURCE_PREFIX + i);
6363
tcr.getSpec().setValue(i + UPDATED_SUFFIX);
6464
operator.resources(TestCustomResource.class)

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/ControllerExecutionIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void awaitResourcesCreatedOrUpdated() {
5252
.untilAsserted(
5353
() -> {
5454
ConfigMap configMap =
55-
operator.getNamedResource(ConfigMap.class, "test-config-map");
55+
operator.get(ConfigMap.class, "test-config-map");
5656
assertThat(configMap).isNotNull();
5757
assertThat(configMap.getData().get("test-key")).isEqualTo("test-value");
5858
});
@@ -68,7 +68,7 @@ void awaitStatusUpdated(int timeout) {
6868
.untilAsserted(
6969
() -> {
7070
TestCustomResource cr =
71-
operator.getNamedResource(TestCustomResource.class,
71+
operator.get(TestCustomResource.class,
7272
TestUtils.TEST_CUSTOM_RESOURCE_NAME);
7373
assertThat(cr).isNotNull();
7474
assertThat(cr.getStatus()).isNotNull();

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/InformerEventSourceIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private InformerEventSourceTestCustomResource initialCustomResource() {
7070
private void waitForCRStatusValue(String value) {
7171
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> {
7272
var cr =
73-
operator.getNamedResource(InformerEventSourceTestCustomResource.class, RESOURCE_NAME);
73+
operator.get(InformerEventSourceTestCustomResource.class, RESOURCE_NAME);
7474
assertThat(cr.getStatus()).isNotNull();
7575
assertThat(cr.getStatus().getConfigMapValue()).isEqualTo(value);
7676
});

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/RetryIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void retryFailedExecution() {
5252
.isEqualTo(RetryTestCustomResourceController.NUMBER_FAILED_EXECUTIONS + 1);
5353

5454
RetryTestCustomResource finalResource =
55-
operator.getNamedResource(RetryTestCustomResource.class,
55+
operator.get(RetryTestCustomResource.class,
5656
resource.getMetadata().getName());
5757
assertThat(finalResource.getStatus().getState())
5858
.isEqualTo(RetryTestCustomResourceStatus.State.SUCCESS);

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/SubResourceUpdateIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void awaitStatusUpdated(String name) {
9999
.untilAsserted(
100100
() -> {
101101
SubResourceTestCustomResource cr =
102-
operator.getNamedResource(SubResourceTestCustomResource.class, name);
102+
operator.get(SubResourceTestCustomResource.class, name);
103103
assertThat(cr.getMetadata().getFinalizers()).hasSize(1);
104104
assertThat(cr).isNotNull();
105105
assertThat(cr.getStatus()).isNotNull();

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/UpdatingResAndSubResIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void updatesSubResourceStatus() {
3636

3737
DoubleUpdateTestCustomResource customResource =
3838
operator
39-
.getNamedResource(DoubleUpdateTestCustomResource.class,
39+
.get(DoubleUpdateTestCustomResource.class,
4040
resource.getMetadata().getName());
4141

4242
assertThat(TestUtils.getNumberOfExecutions(operator))
@@ -57,7 +57,7 @@ void awaitStatusUpdated(String name) {
5757
.untilAsserted(
5858
() -> {
5959
DoubleUpdateTestCustomResource cr =
60-
operator.getNamedResource(DoubleUpdateTestCustomResource.class, name);
60+
operator.get(DoubleUpdateTestCustomResource.class, name);
6161

6262
assertThat(cr)
6363
.isNotNull();

0 commit comments

Comments
 (0)