Skip to content

Commit 3d7eafd

Browse files
committed
Add unit tests for Watcher, some code cleanup
1 parent 7a5e495 commit 3d7eafd

File tree

16 files changed

+605
-721
lines changed

16 files changed

+605
-721
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@
463463
<version>1.3</version>
464464
<scope>test</scope>
465465
</dependency>
466+
<dependency>
467+
<groupId>com.google.guava</groupId>
468+
<artifactId>guava</artifactId>
469+
<version>18.0</version>
470+
<scope>test</scope>
471+
</dependency>
466472
<dependency>
467473
<groupId>org.httpunit</groupId>
468474
<artifactId>httpunit</artifactId>

src/main/java/oracle/kubernetes/operator/DomainWatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
import io.kubernetes.client.ApiException;
77
import io.kubernetes.client.util.Watch;
8+
import oracle.kubernetes.operator.builders.WatchBuilder;
9+
import oracle.kubernetes.operator.builders.WatchI;
810
import oracle.kubernetes.operator.domain.model.oracle.kubernetes.weblogic.domain.v1.Domain;
911
import oracle.kubernetes.operator.helpers.ClientHelper;
1012
import oracle.kubernetes.operator.helpers.ClientHolder;
11-
import oracle.kubernetes.operator.builders.WatchBuilder;
1213
import oracle.kubernetes.operator.watcher.Watcher;
1314
import oracle.kubernetes.operator.watcher.Watching;
1415
import oracle.kubernetes.operator.watcher.WatchingEventDestination;
@@ -50,7 +51,7 @@ public void run() {
5051
ClientHolder client = helper.take();
5152
try {
5253
Watching<Domain> w = createWatching(client);
53-
Watcher<Domain> watcher = new Watcher<Domain>(w, null, initialResourceVersion);
54+
Watcher<Domain> watcher = new Watcher<>(w, initialResourceVersion);
5455

5556
// invoke watch on current Thread. Won't return until watch stops
5657
watcher.doWatch();
@@ -66,13 +67,12 @@ protected Watching<Domain> createWatching(ClientHolder client) {
6667
/**
6768
* Watcher callback to issue the list Domain changes. It is driven by the
6869
* Watcher wrapper to issue repeated watch requests.
69-
* @param context user defined contact object or null
7070
* @param resourceVersion resource version to omit older events
7171
* @return Watch object or null if the operation should end
7272
* @throws ApiException if there is an API error.
7373
*/
7474
@Override
75-
public Watch<Domain> initiateWatch(Object context, String resourceVersion) throws ApiException {
75+
public WatchI<Domain> initiateWatch(String resourceVersion) throws ApiException {
7676
return new WatchBuilder(client)
7777
.withResourceVersion(resourceVersion)
7878
.createDomainWatch(ns);

src/main/java/oracle/kubernetes/operator/IngressWatcher.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.kubernetes.client.models.V1ObjectMeta;
88
import io.kubernetes.client.models.V1beta1Ingress;
99
import io.kubernetes.client.util.Watch;
10+
import oracle.kubernetes.operator.builders.WatchI;
1011
import oracle.kubernetes.operator.helpers.ClientHelper;
1112
import oracle.kubernetes.operator.helpers.ClientHolder;
1213
import oracle.kubernetes.operator.builders.WatchBuilder;
@@ -52,7 +53,7 @@ public void run() {
5253
ClientHolder client = helper.take();
5354
try {
5455
Watching<V1beta1Ingress> w = createWatching(client);
55-
Watcher<V1beta1Ingress> watcher = new Watcher<V1beta1Ingress>(w, null, initialResourceVersion);
56+
Watcher<V1beta1Ingress> watcher = new Watcher<>(w, initialResourceVersion);
5657

5758
// invoke watch on current Thread. Won't return until watch stops
5859
watcher.doWatch();
@@ -68,13 +69,12 @@ protected Watching<V1beta1Ingress> createWatching(ClientHolder client) {
6869
/**
6970
* Watcher callback to issue the list Ingress changes. It is driven by the
7071
* Watcher wrapper to issue repeated watch requests.
71-
* @param context user defined contact object or null
7272
* @param resourceVersion resource version to omit older events
7373
* @return Watch object or null if the operation should end
7474
* @throws ApiException if there is an API error.
7575
*/
7676
@Override
77-
public Watch<V1beta1Ingress> initiateWatch(Object context, String resourceVersion) throws ApiException {
77+
public WatchI<V1beta1Ingress> initiateWatch(String resourceVersion) throws ApiException {
7878
return new WatchBuilder(client)
7979
.withResourceVersion(resourceVersion)
8080
.withLabelSelector(LabelConstants.DOMAINUID_LABEL) // Any Ingress with a domainUID label

src/main/java/oracle/kubernetes/operator/PodWatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import io.kubernetes.client.models.V1PodCondition;
99
import io.kubernetes.client.models.V1PodStatus;
1010
import io.kubernetes.client.util.Watch;
11+
import oracle.kubernetes.operator.builders.WatchBuilder;
12+
import oracle.kubernetes.operator.builders.WatchI;
1113
import oracle.kubernetes.operator.helpers.CallBuilder;
1214
import oracle.kubernetes.operator.helpers.ClientHelper;
1315
import oracle.kubernetes.operator.helpers.ClientHolder;
1416
import oracle.kubernetes.operator.helpers.ResponseStep;
1517
import oracle.kubernetes.operator.logging.LoggingFacade;
1618
import oracle.kubernetes.operator.logging.LoggingFactory;
1719
import oracle.kubernetes.operator.logging.MessageKeys;
18-
import oracle.kubernetes.operator.builders.WatchBuilder;
1920
import oracle.kubernetes.operator.watcher.Watcher;
2021
import oracle.kubernetes.operator.watcher.Watching;
2122
import oracle.kubernetes.operator.watcher.WatchingEventDestination;
@@ -77,7 +78,7 @@ public void run() {
7778
ClientHolder client = helper.take();
7879
try {
7980
Watching<V1Pod> w = createWatching(client);
80-
Watcher<V1Pod> watcher = new Watcher<V1Pod>(w, null, initialResourceVersion);
81+
Watcher<V1Pod> watcher = new Watcher<>(w, initialResourceVersion);
8182

8283
// invoke watch on current Thread. Won't return until watch stops
8384
watcher.doWatch();
@@ -93,13 +94,12 @@ private Watching<V1Pod> createWatching(ClientHolder client) {
9394
/**
9495
* Watcher callback to issue the list Pod changes. It is driven by the
9596
* Watcher wrapper to issue repeated watch requests.
96-
* @param context user defined contact object or null
9797
* @param resourceVersion resource version to omit older events
9898
* @return Watch object or null if the operation should end
9999
* @throws ApiException if there is an API error.
100100
*/
101101
@Override
102-
public Watch<V1Pod> initiateWatch(Object context, String resourceVersion) throws ApiException {
102+
public WatchI<V1Pod> initiateWatch(String resourceVersion) throws ApiException {
103103
return new WatchBuilder(client)
104104
.withResourceVersion(resourceVersion)
105105
.withLabelSelector(LabelConstants.DOMAINUID_LABEL) // Any Pod with a domainUID label

src/main/java/oracle/kubernetes/operator/ServiceWatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import io.kubernetes.client.models.V1ObjectMeta;
88
import io.kubernetes.client.models.V1Service;
99
import io.kubernetes.client.util.Watch;
10+
import oracle.kubernetes.operator.builders.WatchBuilder;
11+
import oracle.kubernetes.operator.builders.WatchI;
1012
import oracle.kubernetes.operator.helpers.ClientHelper;
1113
import oracle.kubernetes.operator.helpers.ClientHolder;
12-
import oracle.kubernetes.operator.builders.WatchBuilder;
1314
import oracle.kubernetes.operator.watcher.Watcher;
1415
import oracle.kubernetes.operator.watcher.Watching;
1516
import oracle.kubernetes.operator.watcher.WatchingEventDestination;
@@ -52,7 +53,7 @@ public void run() {
5253
ClientHolder client = helper.take();
5354
try {
5455
Watching<V1Service> w = createWatching(client);
55-
Watcher<V1Service> watcher = new Watcher<V1Service>(w, null, initialResourceVersion);
56+
Watcher<V1Service> watcher = new Watcher<>(w, initialResourceVersion);
5657

5758
// invoke watch on current Thread. Won't return until watch stops
5859
watcher.doWatch();
@@ -68,13 +69,12 @@ protected Watching<V1Service> createWatching(ClientHolder client) {
6869
/**
6970
* Watcher callback to issue the list Service changes. It is driven by the
7071
* Watcher wrapper to issue repeated watch requests.
71-
* @param context user defined contact object or null
7272
* @param resourceVersion resource version to omit older events
7373
* @return Watch object or null if the operation should end
7474
* @throws ApiException if there is an API error.
7575
*/
7676
@Override
77-
public Watch<V1Service> initiateWatch(Object context, String resourceVersion) throws ApiException {
77+
public WatchI<V1Service> initiateWatch(String resourceVersion) throws ApiException {
7878
return new WatchBuilder(client)
7979
.withResourceVersion(resourceVersion)
8080
.withLabelSelector(LabelConstants.DOMAINUID_LABEL) // Any Service with a domainUID label

src/main/java/oracle/kubernetes/operator/builders/WatchBuilder.java

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class WatchBuilder {
3232
private CallParamsImpl callParams = new CallParamsImpl();
3333

3434
public interface WatchFactory {
35-
<T> Watch<T> createWatch(ClientHolder clientHolder, CallParams callParams, Class<?> responseBodyType, BiFunction<ClientHolder, CallParams, Call> function) throws ApiException;
35+
<T> WatchI<T> createWatch(ClientHolder clientHolder, CallParams callParams, Class<?> responseBodyType, BiFunction<ClientHolder, CallParams, Call> function) throws ApiException;
3636
}
3737

3838
public WatchBuilder(ClientHolder clientHolder) {
@@ -63,7 +63,7 @@ public Type getOwnerType() {
6363
* @return the active web hook
6464
* @throws ApiException if there is an error on the call that sets up the web hook.
6565
*/
66-
public Watch<V1Service> createServiceWatch(String namespace) throws ApiException {
66+
public WatchI<V1Service> createServiceWatch(String namespace) throws ApiException {
6767
return FACTORY.createWatch(clientHolder, callParams, V1Service.class, new ListNamespacedServiceCall(namespace));
6868
}
6969

@@ -92,7 +92,7 @@ public Call apply(ClientHolder clientHolder, CallParams callParams) {
9292
* @return the active web hook
9393
* @throws ApiException if there is an error on the call that sets up the web hook.
9494
*/
95-
public Watch<V1Pod> createPodWatch(String namespace) throws ApiException {
95+
public WatchI<V1Pod> createPodWatch(String namespace) throws ApiException {
9696
return FACTORY.createWatch(clientHolder, callParams, V1Pod.class, new ListPodCall(namespace));
9797
}
9898

@@ -121,7 +121,7 @@ public Call apply(ClientHolder clientHolder, CallParams callParams) {
121121
* @return the active web hook
122122
* @throws ApiException if there is an error on the call that sets up the web hook.
123123
*/
124-
public Watch<V1beta1Ingress> createIngressWatch(String namespace) throws ApiException {
124+
public WatchI<V1beta1Ingress> createIngressWatch(String namespace) throws ApiException {
125125
return FACTORY.createWatch(clientHolder, callParams, V1beta1Ingress.class, new ListIngressCall(namespace));
126126
}
127127

@@ -151,7 +151,7 @@ public Call apply(ClientHolder clientHolder, CallParams callParams) {
151151
* @return the active web hook
152152
* @throws ApiException if there is an error on the call that sets up the web hook.
153153
*/
154-
public Watch<Domain> createDomainWatch(String namespace) throws ApiException {
154+
public WatchI<Domain> createDomainWatch(String namespace) throws ApiException {
155155
return FACTORY.createWatch(clientHolder, callParams, Domain.class, new ListDomainsCall(namespace));
156156
}
157157

@@ -175,45 +175,6 @@ public Call apply(ClientHolder clientHolder, CallParams callParams) {
175175
}
176176
}
177177

178-
/**
179-
* Creates a web hook object to track changes to custom objects
180-
* @param namespace the namespace in which to track custom objects.
181-
* @param responseBodyType the type of objects returned in the events
182-
* @param group the custom resource group name
183-
* @param version the custom resource version
184-
* @param plural the custom resource plural name @return the active web hook
185-
* @throws ApiException if there is an error on the call that sets up the web hook.
186-
*/
187-
public <T> Watch<T> createCustomObjectWatch(String namespace, Class<?> responseBodyType, String group, String version, String plural) throws ApiException {
188-
return FACTORY.createWatch(clientHolder, callParams, responseBodyType, new ListCustomObjectsInNamespaceCall(namespace, group, version, plural));
189-
}
190-
191-
private class ListCustomObjectsInNamespaceCall implements BiFunction<ClientHolder, CallParams, Call> {
192-
private String namespace;
193-
private String group;
194-
private String version;
195-
private String plural;
196-
197-
ListCustomObjectsInNamespaceCall(String namespace, String group, String version, String plural) {
198-
this.namespace = namespace;
199-
this.group = group;
200-
this.version = version;
201-
this.plural = plural;
202-
}
203-
204-
@Override
205-
public Call apply(ClientHolder clientHolder, CallParams callParams) {
206-
try {
207-
return clientHolder.getCustomObjectsApiClient().listNamespacedCustomObjectCall(
208-
group, version, namespace, plural,
209-
callParams.getPretty(), callParams.getLabelSelector(), callParams.getResourceVersion(),
210-
WATCH, null, null);
211-
} catch (ApiException e) {
212-
throw new UncheckedApiException(e);
213-
}
214-
}
215-
}
216-
217178
/**
218179
* Sets a value for the fieldSelector parameter for the call that will set up this watch. Defaults to null.
219180
* @param fieldSelector the desired value
@@ -268,9 +229,9 @@ public WatchBuilder withProgressRequestListener(ProgressRequestBody.ProgressRequ
268229

269230
static class WatchFactoryImpl implements WatchFactory {
270231
@Override
271-
public <T> Watch<T> createWatch(ClientHolder clientHolder, CallParams callParams, Class<?> responseBodyType, BiFunction<ClientHolder, CallParams, Call> function) throws ApiException {
232+
public <T> WatchI<T> createWatch(ClientHolder clientHolder, CallParams callParams, Class<?> responseBodyType, BiFunction<ClientHolder, CallParams, Call> function) throws ApiException {
272233
try {
273-
return Watch.createWatch(clientHolder.getApiClient(), function.apply(clientHolder, callParams), getType(responseBodyType));
234+
return new WatchImpl<T>(Watch.createWatch(clientHolder.getApiClient(), function.apply(clientHolder, callParams), getType(responseBodyType)));
274235
} catch (UncheckedApiException e) {
275236
throw e.getCause();
276237
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
package oracle.kubernetes.operator.builders;
4+
5+
import io.kubernetes.client.util.Watch;
6+
7+
import java.util.Iterator;
8+
9+
/**
10+
* An interface that allows test-stubbing of the Kubernetes Watch class.
11+
* @param <T> the generic object type
12+
*/
13+
public interface WatchI<T>
14+
extends Iterable<Watch.Response<T>>, Iterator<Watch.Response<T>>,
15+
java.io.Closeable {
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2018 Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
package oracle.kubernetes.operator.builders;
4+
5+
import io.kubernetes.client.util.Watch;
6+
7+
import java.io.IOException;
8+
import java.util.Iterator;
9+
10+
/**
11+
* A pass-through implementation of the Kubernetes Watch class which implements a facade
12+
* interface.
13+
*/
14+
public class WatchImpl<T> implements WatchI<T> {
15+
private Watch<T> impl;
16+
17+
WatchImpl(Watch<T> impl) {
18+
this.impl = impl;
19+
}
20+
21+
@Override
22+
public void close() throws IOException {
23+
impl.close();
24+
}
25+
26+
@Override
27+
public Iterator<Watch.Response<T>> iterator() {
28+
return impl.iterator();
29+
}
30+
31+
@Override
32+
public boolean hasNext() {
33+
return impl.hasNext();
34+
}
35+
36+
@Override
37+
public Watch.Response<T> next() {
38+
return impl.next();
39+
}
40+
}

0 commit comments

Comments
 (0)