Skip to content

Commit d1a7b34

Browse files
committed
Add unit tests for WatchBuilder that do not require Kubernetes
1 parent d73d9a8 commit d1a7b34

File tree

8 files changed

+505
-118
lines changed

8 files changed

+505
-118
lines changed

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,17 +445,36 @@
445445
<version>2.3.0</version>
446446
</dependency>
447447
<!-- test dependencies -->
448+
<dependency>
449+
<groupId>org.hamcrest</groupId>
450+
<artifactId>hamcrest-junit</artifactId>
451+
<version>2.0.0.0</version>
452+
<scope>test</scope>
453+
</dependency>
448454
<dependency>
449455
<groupId>junit</groupId>
450456
<artifactId>junit</artifactId>
451457
<version>${junit-version}</version>
458+
<scope>test</scope>
452459
</dependency>
453460
<dependency>
454461
<groupId>org.apache.commons</groupId>
455462
<artifactId>commons-exec</artifactId>
456463
<version>1.3</version>
457464
<scope>test</scope>
458465
</dependency>
466+
<dependency>
467+
<groupId>org.httpunit</groupId>
468+
<artifactId>httpunit</artifactId>
469+
<version>1.7.2</version>
470+
<scope>test</scope>
471+
</dependency>
472+
<dependency>
473+
<groupId>com.meterware.simplestub</groupId>
474+
<artifactId>simplestub</artifactId>
475+
<version>1.2.9</version>
476+
<scope>test</scope>
477+
</dependency>
459478

460479
</dependencies>
461480

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected Watching<Domain> createWatching(ClientHolder client) {
7575
public Watch<Domain> initiateWatch(Object context, String resourceVersion) throws ApiException {
7676
return new WatchBuilder(client)
7777
.withResourceVersion(resourceVersion)
78-
.createDomainsInNamespaceWatch(ns);
78+
.createDomainWatch(ns);
7979
}
8080

8181
@Override

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

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
package oracle.kubernetes.operator.builders;
44

55
import com.squareup.okhttp.Call;
6-
import io.kubernetes.client.ApiClient;
76
import io.kubernetes.client.ApiException;
87
import io.kubernetes.client.ProgressRequestBody;
98
import io.kubernetes.client.ProgressResponseBody;
10-
import io.kubernetes.client.models.V1Namespace;
119
import io.kubernetes.client.models.V1Pod;
1210
import io.kubernetes.client.models.V1Service;
1311
import io.kubernetes.client.models.V1beta1Ingress;
@@ -19,7 +17,7 @@
1917
import java.lang.reflect.Type;
2018
import java.util.function.BiFunction;
2119

22-
@SuppressWarnings("unused")
20+
@SuppressWarnings("WeakerAccess")
2321
public class WatchBuilder {
2422

2523
/** Always true for watches. */
@@ -41,32 +39,7 @@ public WatchBuilder(ClientHolder clientHolder) {
4139
this.clientHolder = clientHolder;
4240
}
4341

44-
/**
45-
* Creates a web hook object to track changes to the namespaces.
46-
* @return the active web hook
47-
* @throws ApiException if there is an error on the call that sets up the web hook.
48-
*/
49-
public Watch<V1Namespace> createNamespaceWatch() throws ApiException {
50-
return FACTORY.createWatch(clientHolder, callParams, V1Namespace.class, WatchBuilder::listNamespaceCall);
51-
}
52-
53-
private ApiClient getApiClient() {
54-
return clientHolder.getApiClient();
55-
}
56-
57-
private static Call listNamespaceCall(ClientHolder clientHolder, CallParams callParams) {
58-
try {
59-
return clientHolder.getCoreApiClient().listNamespaceCall(
60-
callParams.getPretty(), START_LIST, callParams.getFieldSelector(),
61-
callParams.getIncludeUninitialized(), callParams.getLabelSelector(), callParams.getLimit(),
62-
callParams.getResourceVersion(), callParams.getTimeoutSeconds(), WATCH,
63-
callParams.getProgressListener(), callParams.getProgressRequestListener());
64-
} catch (ApiException e) {
65-
throw new UncheckedApiException(e);
66-
}
67-
}
68-
69-
private static <T> Type getType(Class<?> responseBodyType) {
42+
private static Type getType(Class<?> responseBodyType) {
7043
return new ParameterizedType() {
7144
@Override
7245
public Type[] getActualTypeArguments() {
@@ -172,40 +145,20 @@ public Call apply(ClientHolder clientHolder, CallParams callParams) {
172145
}
173146
}
174147

175-
/**
176-
* Creates a web hook object to track changes to weblogic domains in all namespaces
177-
* @return the active web hook
178-
* @throws ApiException if there is an error on the call that sets up the web hook.
179-
*/
180-
public Watch<Domain> createDomainsInAllNamespacesWatch() throws ApiException {
181-
return FACTORY.createWatch(clientHolder, callParams, Domain.class, WatchBuilder::listDomainsForAllNamespacesCall);
182-
}
183-
184-
private static Call listDomainsForAllNamespacesCall(ClientHolder clientHolder, CallParams callParams) {
185-
try {
186-
return clientHolder.getWeblogicApiClient().listWebLogicOracleV1DomainForAllNamespacesCall(START_LIST,
187-
callParams.getFieldSelector(), callParams.getIncludeUninitialized(), callParams.getLabelSelector(),
188-
callParams.getLimit(), callParams.getPretty(), callParams.getResourceVersion(),
189-
callParams.getTimeoutSeconds(), WATCH, callParams.getProgressListener(), callParams.getProgressRequestListener());
190-
} catch (ApiException e) {
191-
throw new UncheckedApiException(e);
192-
}
193-
}
194-
195148
/**
196149
* Creates a web hook object to track changes to weblogic domains in one namespaces
197150
* @param namespace the namespace in which to track domains
198151
* @return the active web hook
199152
* @throws ApiException if there is an error on the call that sets up the web hook.
200153
*/
201-
public Watch<Domain> createDomainsInNamespaceWatch(String namespace) throws ApiException {
202-
return FACTORY.createWatch(clientHolder, callParams, Domain.class, new ListDomainsInNamespaceCall(namespace));
154+
public Watch<Domain> createDomainWatch(String namespace) throws ApiException {
155+
return FACTORY.createWatch(clientHolder, callParams, Domain.class, new ListDomainsCall(namespace));
203156
}
204157

205-
private class ListDomainsInNamespaceCall implements BiFunction<ClientHolder, CallParams, Call> {
158+
private class ListDomainsCall implements BiFunction<ClientHolder, CallParams, Call> {
206159
private String namespace;
207160

208-
ListDomainsInNamespaceCall(String namespace) {
161+
ListDomainsCall(String namespace) {
209162
this.namespace = namespace;
210163
}
211164

@@ -301,11 +254,13 @@ public WatchBuilder withTimeoutSeconds(Integer timeoutSeconds) {
301254
return this;
302255
}
303256

257+
@SuppressWarnings("unused")
304258
public WatchBuilder withProgressListener(ProgressResponseBody.ProgressListener progressListener) {
305259
callParams.setProgressListener(progressListener);
306260
return this;
307261
}
308262

263+
@SuppressWarnings("unused")
309264
public WatchBuilder withProgressRequestListener(ProgressRequestBody.ProgressRequestListener progressRequestListener) {
310265
callParams.setProgressRequestListener(progressRequestListener);
311266
return this;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
4+
/**
5+
* Builders for complex objects that interact with Kubernetes.
6+
*/
7+
package oracle.kubernetes.operator.builders;

src/test/java/oracle/kubernetes/TestUtils.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
33
package oracle.kubernetes;
44

5+
import com.meterware.simplestub.Memento;
6+
import oracle.kubernetes.operator.logging.LoggingFactory;
57
import org.apache.commons.exec.CommandLine;
68
import org.apache.commons.exec.DefaultExecutor;
79

@@ -49,6 +51,26 @@ public static boolean isLinux() {
4951
return System.getProperty("os.name").toLowerCase().contains("linux");
5052
}
5153

54+
/**
55+
* Removes the console handlers from the specified logger, in order to silence them during a test.
56+
*
57+
* @return a collection of the removed handlers
58+
*/
59+
public static Memento silenceOperatorLogger() {
60+
Logger logger = LoggingFactory.getLogger("Operator", "Operator").getUnderlyingLogger();
61+
List<Handler> savedHandlers = new ArrayList<>();
62+
for (Handler handler : logger.getHandlers()) {
63+
if (handler instanceof ConsoleHandler) {
64+
savedHandlers.add(handler);
65+
}
66+
}
67+
68+
for (Handler handler : savedHandlers)
69+
logger.removeHandler(handler);
70+
71+
return new ConsoleHandlerMemento(logger, savedHandlers);
72+
}
73+
5274
/**
5375
* Removes the console handlers from the specified logger, in order to silence them during a test.
5476
*
@@ -78,4 +100,24 @@ public static void restoreConsoleHandlers(Logger logger, List<Handler> savedHand
78100
logger.addHandler(handler);
79101
}
80102
}
103+
104+
private static class ConsoleHandlerMemento implements Memento {
105+
private Logger logger;
106+
private List<Handler> savedHandlers;
107+
108+
ConsoleHandlerMemento(Logger logger, List<Handler> savedHandlers) {
109+
this.logger = logger;
110+
this.savedHandlers = savedHandlers;
111+
}
112+
113+
@Override
114+
public void revert() {
115+
restoreConsoleHandlers(logger, savedHandlers);
116+
}
117+
118+
@Override
119+
public <T> T getOriginalValue() {
120+
throw new UnsupportedOperationException();
121+
}
122+
}
81123
}

src/test/java/oracle/kubernetes/operator/WatchTest.java

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)