Skip to content

Commit 00932c1

Browse files
committed
more unit tests
1 parent f82a627 commit 00932c1

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2019 Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator.http;
6+
7+
public abstract class HttpClientStub extends HttpClient {
8+
9+
final String RESPONSE = "{}";
10+
11+
public HttpClientStub() {
12+
super(null, null);
13+
}
14+
15+
@Override
16+
public Result executePostUrlOnServiceClusterIP(
17+
String requestUrl, String serviceURL, String payload, boolean throwOnFailure)
18+
throws HTTPException {
19+
return new Result(RESPONSE, 200, true);
20+
}
21+
}

operator/src/test/java/oracle/kubernetes/operator/steps/ReadHealthStepTest.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1+
// Copyright 2018, 2019 Oracle Corporation and/or its affiliates. All rights reserved.
22
// Licensed under the Universal Permissive License v 1.0 as shown at
33
// http://oss.oracle.com/licenses/upl.
44

@@ -8,22 +8,28 @@
88
import static oracle.kubernetes.operator.logging.MessageKeys.WLS_HEALTH_READ_FAILED;
99
import static oracle.kubernetes.operator.logging.MessageKeys.WLS_HEALTH_READ_FAILED_NO_HTTPCLIENT;
1010
import static org.hamcrest.MatcherAssert.assertThat;
11-
import static org.junit.Assert.*;
11+
import static org.hamcrest.Matchers.is;
12+
import static org.hamcrest.core.IsNull.nullValue;
1213

1314
import com.meterware.simplestub.Memento;
1415
import com.meterware.simplestub.Stub;
1516
import io.kubernetes.client.models.V1Service;
17+
import io.kubernetes.client.models.V1ServicePort;
18+
import io.kubernetes.client.models.V1ServiceSpec;
1619
import java.util.ArrayList;
1720
import java.util.List;
21+
import java.util.concurrent.ConcurrentHashMap;
1822
import java.util.logging.Level;
1923
import java.util.logging.LogRecord;
2024
import oracle.kubernetes.TestUtils;
2125
import oracle.kubernetes.operator.ProcessingConstants;
2226
import oracle.kubernetes.operator.http.HttpClient;
27+
import oracle.kubernetes.operator.http.HttpClientStub;
2328
import oracle.kubernetes.operator.steps.ReadHealthStep.ReadHealthWithHttpClientStep;
2429
import oracle.kubernetes.operator.work.NextAction;
2530
import oracle.kubernetes.operator.work.Packet;
2631
import oracle.kubernetes.operator.work.Step;
32+
import oracle.kubernetes.weblogic.domain.model.ServerHealth;
2733
import org.junit.After;
2834
import org.junit.Before;
2935
import org.junit.Test;
@@ -74,25 +80,55 @@ public void withHttpClientStep_logIfMissingHTTPClient() {
7480
V1Service service = Stub.createStub(V1ServiceStub.class);
7581
Step next = new MockStep(null);
7682
final String SERVER_NAME = "admin-server";
77-
Packet packet = Stub.createStub(PacketStub.class).withServerName(SERVER_NAME);
83+
Packet packet =
84+
Stub.createStub(PacketStub.class).withServerName(SERVER_NAME).withGetKeyReturnValue(null);
7885

7986
ReadHealthWithHttpClientStep withHttpClientStep =
8087
new ReadHealthWithHttpClientStep(service, next);
8188
withHttpClientStep.apply(packet);
8289

8390
assertThat(logRecords, containsInfo(WLS_HEALTH_READ_FAILED_NO_HTTPCLIENT, SERVER_NAME));
91+
assertThat(packet.get(ProcessingConstants.SERVER_HEALTH_READ), nullValue());
92+
}
93+
94+
@Test
95+
public void withHttpClientStep_putServerHealthReadToPacketIfSucceeded() {
96+
V1Service service = Stub.createStub(V1ServiceStub.class);
97+
Step next = new MockStep(null);
98+
final String SERVER_NAME = "admin-server";
99+
100+
HttpClientStub httpClientStub = Stub.createStub(HttpClientStub.class);
101+
102+
Packet packet =
103+
Stub.createStub(PacketStub.class)
104+
.withServerName(SERVER_NAME)
105+
.withGetKeyReturnValue(httpClientStub);
106+
packet.put(
107+
ProcessingConstants.SERVER_HEALTH_MAP, new ConcurrentHashMap<String, ServerHealth>());
108+
109+
ReadHealthWithHttpClientStep withHttpClientStep =
110+
new ReadHealthWithHttpClientStep(service, next);
111+
withHttpClientStep.apply(packet);
112+
113+
assertThat(packet.get(ProcessingConstants.SERVER_HEALTH_READ), is(Boolean.TRUE));
84114
}
85115

86116
abstract static class PacketStub extends Packet {
87117

88118
String serverName;
89119
boolean getKeyThrowsException;
120+
HttpClient getKeyReturnValue;
90121

91122
PacketStub withGetKeyThrowsException(boolean getKeyThrowsException) {
92123
this.getKeyThrowsException = getKeyThrowsException;
93124
return this;
94125
}
95126

127+
PacketStub withGetKeyReturnValue(HttpClient getKeyReturnValue) {
128+
this.getKeyReturnValue = getKeyReturnValue;
129+
return this;
130+
}
131+
96132
PacketStub withServerName(String serverName) {
97133
this.serverName = serverName;
98134
return this;
@@ -104,15 +140,24 @@ public Object get(Object key) {
104140
if (getKeyThrowsException) {
105141
throw CLASSCAST_EXCEPTION; // to go to catch clause in WithHttpClientStep.apply() method
106142
}
107-
return null;
143+
return getKeyReturnValue;
108144
} else if (ProcessingConstants.SERVER_NAME.equals(key)) {
109145
return serverName;
110146
}
111147
return super.get(key);
112148
}
113149
}
114150

115-
public abstract static class V1ServiceStub extends V1Service {}
151+
public abstract static class V1ServiceStub extends V1Service {
152+
153+
@Override
154+
public V1ServiceSpec getSpec() {
155+
List<V1ServicePort> ports = new ArrayList<>();
156+
ports.add(new V1ServicePort().port(7001));
157+
V1ServiceSpec v1ServiceSpec = new V1ServiceSpec().clusterIP("127.0.0.1").ports(ports);
158+
return v1ServiceSpec;
159+
}
160+
}
116161

117162
static class MockStep extends Step {
118163
public MockStep(Step next) {

0 commit comments

Comments
 (0)