11package io .javaoperatorsdk .operator .junit ;
22
3- import java .io .InputStream ;
4- import java .util .ArrayList ;
5- import java .util .List ;
6- import java .util .Locale ;
7- import java .util .concurrent .TimeUnit ;
8- import java .util .stream .Collectors ;
9-
10- import org .awaitility .Awaitility ;
11- import org .junit .jupiter .api .extension .AfterAllCallback ;
12- import org .junit .jupiter .api .extension .AfterEachCallback ;
13- import org .junit .jupiter .api .extension .BeforeAllCallback ;
14- import org .junit .jupiter .api .extension .BeforeEachCallback ;
15- import org .junit .jupiter .api .extension .ExtensionContext ;
16- import org .slf4j .Logger ;
17- import org .slf4j .LoggerFactory ;
3+ import static io .javaoperatorsdk .operator .api .config .ControllerConfigurationOverrider .override ;
184
195import io .fabric8 .kubernetes .api .model .HasMetadata ;
206import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
3218import io .javaoperatorsdk .operator .api .config .Version ;
3319import io .javaoperatorsdk .operator .processing .ConfiguredController ;
3420import io .javaoperatorsdk .operator .processing .retry .Retry ;
35-
36- import static io .javaoperatorsdk .operator .api .config .ControllerConfigurationOverrider .override ;
21+ import java .io .InputStream ;
22+ import java .util .ArrayList ;
23+ import java .util .List ;
24+ import java .util .Locale ;
25+ import java .util .concurrent .TimeUnit ;
26+ import java .util .stream .Collectors ;
27+ import org .awaitility .Awaitility ;
28+ import org .junit .jupiter .api .extension .AfterAllCallback ;
29+ import org .junit .jupiter .api .extension .AfterEachCallback ;
30+ import org .junit .jupiter .api .extension .BeforeAllCallback ;
31+ import org .junit .jupiter .api .extension .BeforeEachCallback ;
32+ import org .junit .jupiter .api .extension .ExtensionContext ;
33+ import org .slf4j .Logger ;
34+ import org .slf4j .LoggerFactory ;
3735
3836public class OperatorExtension
3937 implements HasKubernetesClient ,
@@ -67,6 +65,11 @@ private OperatorExtension(
6765 this .waitForNamespaceDeletion = waitForNamespaceDeletion ;
6866 }
6967
68+ /**
69+ * Creates a {@link Builder} to set up an {@link OperatorExtension} instance.
70+ *
71+ * @return the builder.
72+ */
7073 public static Builder builder () {
7174 return new Builder ();
7275 }
@@ -96,17 +99,32 @@ public KubernetesClient getKubernetesClient() {
9699 return kubernetesClient ;
97100 }
98101
102+ /**
103+ * Returns the test namespace.
104+ *
105+ * @return the namespace name.
106+ */
99107 public String getNamespace () {
100108 return namespace ;
101109 }
102110
111+ /**
112+ * The list of controllers known by the operator.
113+ *
114+ * @return the list of {@link ResourceController}.
115+ */
103116 @ SuppressWarnings ({"rawtypes" })
104117 public List <ResourceController > getControllers () {
105118 return operator .getControllers ().stream ()
106119 .map (ConfiguredController ::getController )
107120 .collect (Collectors .toUnmodifiableList ());
108121 }
109122
123+ /**
124+ * The list of controllers of the give {@code type} known by the operator.
125+ *
126+ * @return the list of {@link ResourceController} matching the required type.
127+ */
110128 @ SuppressWarnings ({"rawtypes" })
111129 public <T extends ResourceController > T getControllerOfType (Class <T > type ) {
112130 return operator .getControllers ().stream ()
@@ -123,22 +141,42 @@ public <T extends HasMetadata> NonNamespaceOperation<T, KubernetesResourceList<T
123141 return kubernetesClient .resources (type ).inNamespace (namespace );
124142 }
125143
126- public <T extends HasMetadata > T getNamedResource (Class <T > type , String name ) {
144+ /**
145+ * Lookup a resource given its {@code type} and {@code name} in the current test namespace.
146+ *
147+ * @param type Class for resource
148+ * @param name the name of the resource
149+ * @param <T> T type represents resource type
150+ * @return The resource or null if it does not exist
151+ */
152+ public <T extends HasMetadata > T get (Class <T > type , String name ) {
127153 return kubernetesClient .resources (type ).inNamespace (namespace ).withName (name ).get ();
128154 }
129155
156+ /**
157+ * Creates a resource in the current test namespace.
158+ *
159+ * @param type Class for resource
160+ * @param resource the resource to create
161+ * @param <T> T type represents resource type
162+ * @return The resource or null if it does not exist
163+ */
130164 public <T extends HasMetadata > T create (Class <T > type , T resource ) {
131165 return kubernetesClient .resources (type ).inNamespace (namespace ).create (resource );
132166 }
133167
168+ /**
169+ * Replaces a resource in the current test namespace.
170+ *
171+ * @param type Class for resource
172+ * @param resource the resource to create
173+ * @param <T> T type represents resource type
174+ * @return The resource or null if it does not exist
175+ */
134176 public <T extends HasMetadata > T replace (Class <T > type , T resource ) {
135177 return kubernetesClient .resources (type ).inNamespace (namespace ).replace (resource );
136178 }
137179
138- public <T extends HasMetadata > T get (Class <T > type , String name ) {
139- return kubernetesClient .resources (type ).inNamespace (namespace ).withName (name ).get ();
140- }
141-
142180
143181 @ SuppressWarnings ("unchecked" )
144182 protected void before (ExtensionContext context ) {
@@ -225,33 +263,71 @@ protected Builder() {
225263 true );
226264 }
227265
266+ /**
267+ * Configures if the test namespace should be preserved in case of error for troubleshooting.
268+ *
269+ * @param value true if the namespace should be preserved.
270+ * @return this builder
271+ */
228272 public Builder preserveNamespaceOnError (boolean value ) {
229273 this .preserveNamespaceOnError = value ;
230274 return this ;
231275 }
232276
277+ /**
278+ * Configures if the extension should wait for the test namespace deletion.
279+ *
280+ * @param value true if the waiting for namespace deletion is required.
281+ * @return this builder
282+ */
233283 public Builder waitForNamespaceDeletion (boolean value ) {
234284 this .waitForNamespaceDeletion = value ;
235285 return this ;
236286 }
237287
288+ /**
289+ * Defines the {@link ConfigurationService} the operator should use.
290+ *
291+ * @param value the {@link ConfigurationService}.
292+ * @return this builder
293+ */
238294 public Builder withConfigurationService (ConfigurationService value ) {
239295 configurationService = value ;
240296 return this ;
241297 }
242298
299+ /**
300+ * Add a {@link ResourceController} to the operator.
301+ *
302+ * @param value the {@link ResourceController}
303+ * @return this builder
304+ */
243305 @ SuppressWarnings ("rawtypes" )
244306 public Builder withController (ResourceController value ) {
245307 controllers .add (new ControllerSpec (value , null ));
246308 return this ;
247309 }
248310
311+ /**
312+ * Add a {@link ResourceController} to the operator with a {@link Retry} policy.
313+ *
314+ * @param value the {@link ResourceController}
315+ * @param retry the {@link Retry} policy.
316+ * @return this builder
317+ */
249318 @ SuppressWarnings ("rawtypes" )
250319 public Builder withController (ResourceController value , Retry retry ) {
251320 controllers .add (new ControllerSpec (value , retry ));
252321 return this ;
253322 }
254323
324+ /**
325+ * Add a {@link ResourceController} to the operator by providing its class name. The controller
326+ * is instantiated using the default empty constructor.
327+ *
328+ * @param value the {@link ResourceController} type.
329+ * @return this builder
330+ */
255331 @ SuppressWarnings ("rawtypes" )
256332 public Builder withController (Class <? extends ResourceController > value ) {
257333 try {
@@ -262,6 +338,11 @@ public Builder withController(Class<? extends ResourceController> value) {
262338 return this ;
263339 }
264340
341+ /**
342+ * Build a new {@link OperatorExtension} instance.
343+ *
344+ * @return a new {@link OperatorExtension} instance.
345+ */
265346 public OperatorExtension build () {
266347 return new OperatorExtension (
267348 configurationService ,
0 commit comments