Skip to content

Commit 085cdfc

Browse files
committed
Add initial unit test for DomainStatusUpdater
1 parent a8df423 commit 085cdfc

File tree

7 files changed

+964
-18
lines changed

7 files changed

+964
-18
lines changed

model/src/main/java/oracle/kubernetes/weblogic/domain/v2/DomainStatus.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.gson.annotations.SerializedName;
99
import java.util.ArrayList;
1010
import java.util.List;
11+
import javax.annotation.Nonnull;
1112
import javax.validation.Valid;
1213
import oracle.kubernetes.json.Description;
1314
import oracle.kubernetes.json.Range;
@@ -69,7 +70,7 @@ public class DomainStatus {
6970
*
7071
* @return conditions
7172
*/
72-
public List<DomainCondition> getConditions() {
73+
public @Nonnull List<DomainCondition> getConditions() {
7374
return conditions;
7475
}
7576

@@ -78,7 +79,7 @@ public List<DomainCondition> getConditions() {
7879
*
7980
* @param conditions conditions
8081
*/
81-
public void setConditions(List<DomainCondition> conditions) {
82+
public void setConditions(@Nonnull List<DomainCondition> conditions) {
8283
this.conditions = conditions;
8384
}
8485

@@ -93,6 +94,11 @@ public DomainStatus withConditions(List<DomainCondition> conditions) {
9394
return this;
9495
}
9596

97+
public DomainStatus addCondition(DomainCondition condition) {
98+
conditions.add(condition);
99+
return this;
100+
}
101+
96102
/**
97103
* A human readable message indicating details about why the domain is in this condition.
98104
*

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

Lines changed: 8 additions & 12 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

@@ -13,7 +13,6 @@
1313
import java.util.ListIterator;
1414
import java.util.Map;
1515
import java.util.TreeMap;
16-
import java.util.concurrent.ConcurrentMap;
1716
import oracle.kubernetes.operator.calls.CallResponse;
1817
import oracle.kubernetes.operator.helpers.CallBuilder;
1918
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
@@ -93,7 +92,7 @@ info, timeoutSeconds, new StatusUpdateStep(getNext())),
9392
}
9493
}
9594

96-
private static class StatusUpdateStep extends Step {
95+
static class StatusUpdateStep extends Step {
9796
public StatusUpdateStep(Step next) {
9897
super(next);
9998
}
@@ -125,12 +124,12 @@ public NextAction apply(Packet packet) {
125124

126125
// Acquire current state
127126
@SuppressWarnings("unchecked")
128-
ConcurrentMap<String, String> serverState =
129-
(ConcurrentMap<String, String>) packet.get(ProcessingConstants.SERVER_STATE_MAP);
127+
Map<String, String> serverState =
128+
(Map<String, String>) packet.get(ProcessingConstants.SERVER_STATE_MAP);
130129

131130
@SuppressWarnings("unchecked")
132-
ConcurrentMap<String, ServerHealth> serverHealth =
133-
(ConcurrentMap<String, ServerHealth>) packet.get(ProcessingConstants.SERVER_HEALTH_MAP);
131+
Map<String, ServerHealth> serverHealth =
132+
(Map<String, ServerHealth>) packet.get(ProcessingConstants.SERVER_HEALTH_MAP);
134133

135134
Map<String, ServerStatus> serverStatuses = new TreeMap<>();
136135
WlsDomainConfig config = (WlsDomainConfig) packet.get(ProcessingConstants.DOMAIN_TOPOLOGY);
@@ -164,6 +163,7 @@ public NextAction apply(Packet packet) {
164163
}
165164
}
166165

166+
// num servers in named cluster, using labels
167167
Map<String, Integer> clusterCounts = new HashMap<>();
168168
for (Map.Entry<String, ServerKubernetesObjects> entry : info.getServers().entrySet()) {
169169
String serverName = entry.getKey();
@@ -173,11 +173,7 @@ public NextAction apply(Packet packet) {
173173
String clusterName =
174174
pod.getMetadata().getLabels().get(LabelConstants.CLUSTERNAME_LABEL);
175175
if (clusterName != null) {
176-
clusterCounts.compute(
177-
clusterName,
178-
(key, value) -> {
179-
return (value == null) ? 1 : value + 1;
180-
});
176+
clusterCounts.compute(clusterName, (key, value) -> (value == null) ? 1 : value + 1);
181177
}
182178
ServerStatus ss =
183179
new ServerStatus()

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
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

55
package oracle.kubernetes;
66

7-
import static com.meterware.simplestub.Stub.createStub;
8-
97
import com.meterware.simplestub.Memento;
108
import java.util.ArrayList;
119
import java.util.Arrays;
@@ -19,6 +17,8 @@
1917
import java.util.logging.SimpleFormatter;
2018
import oracle.kubernetes.operator.logging.LoggingFactory;
2119

20+
import static com.meterware.simplestub.Stub.createStub;
21+
2222
public class TestUtils {
2323
/**
2424
* Removes the console handlers from the specified logger, in order to silence them during a test.
@@ -42,6 +42,20 @@ public static ConsoleHandlerMemento silenceOperatorLogger() {
4242
return new ConsoleHandlerMemento(logger, testHandler, savedHandlers);
4343
}
4444

45+
/**
46+
* Converts a list of strings to a comma-separated list, using "and" for the last item.
47+
*
48+
* @param list the list to convert
49+
* @return the resultant string
50+
*/
51+
public static String joinListGrammatically(final List<String> list) {
52+
return list.size() > 1
53+
? String.join(", ", list.subList(0, list.size() - 1))
54+
.concat(String.format("%s and ", list.size() > 2 ? "," : ""))
55+
.concat(list.get(list.size() - 1))
56+
: list.get(0);
57+
}
58+
4559
abstract static class TestLogHandler extends Handler {
4660
private Throwable throwable;
4761
private List<Throwable> ignoredExceptions = new ArrayList<>();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Optional;
10+
import oracle.kubernetes.TestUtils;
11+
import oracle.kubernetes.weblogic.domain.v2.Domain;
12+
import oracle.kubernetes.weblogic.domain.v2.DomainCondition;
13+
import oracle.kubernetes.weblogic.domain.v2.DomainStatus;
14+
import org.hamcrest.Description;
15+
import org.hamcrest.TypeSafeDiagnosingMatcher;
16+
17+
@SuppressWarnings("unused")
18+
class DomainConditionMatcher extends TypeSafeDiagnosingMatcher<Domain> {
19+
private String expectedType;
20+
private String expectedStatus;
21+
private String expectedReason;
22+
private String expectedMessage;
23+
24+
static DomainConditionMatcher hasCondition(String type) {
25+
return new DomainConditionMatcher(type);
26+
}
27+
28+
private DomainConditionMatcher(String expectedType) {
29+
this.expectedType = expectedType;
30+
}
31+
32+
DomainConditionMatcher withStatus(String status) {
33+
expectedStatus = status;
34+
return this;
35+
}
36+
37+
DomainConditionMatcher withReason(String reason) {
38+
expectedReason = reason;
39+
return this;
40+
}
41+
42+
DomainConditionMatcher withMessage(String message) {
43+
expectedMessage = message;
44+
return this;
45+
}
46+
47+
@Override
48+
protected boolean matchesSafely(Domain item, Description mismatchDescription) {
49+
for (DomainCondition condition : getStatus(item).getConditions())
50+
if (matches(condition)) return true;
51+
52+
mismatchDescription.appendValueList(
53+
"found domain with conditions ", ", ", ".", getStatus(item).getConditions());
54+
return false;
55+
}
56+
57+
private boolean matches(DomainCondition condition) {
58+
if (expectedType != null && !expectedType.equals(condition.getType())) return false;
59+
if (expectedStatus != null && !expectedStatus.equals(condition.getStatus())) return false;
60+
if (expectedMessage != null && !expectedMessage.equals(condition.getMessage())) return false;
61+
return expectedReason == null || expectedReason.equals(condition.getReason());
62+
}
63+
64+
private DomainStatus getStatus(Domain domain) {
65+
return Optional.ofNullable(domain.getStatus()).orElse(new DomainStatus());
66+
}
67+
68+
@Override
69+
public void describeTo(Description description) {
70+
List<String> expectations = new ArrayList<>();
71+
if (expectedType != null) expectations.add(expectation("type", expectedType));
72+
if (expectedStatus != null) expectations.add(expectation("status", expectedStatus));
73+
if (expectedReason != null) expectations.add(expectation("reason", expectedReason));
74+
if (expectedMessage != null) expectations.add(expectation("reason", expectedMessage));
75+
description
76+
.appendText("domain containing condition: ")
77+
.appendText(TestUtils.joinListGrammatically(expectations));
78+
}
79+
80+
private String expectation(String description, String value) {
81+
return description + " = '" + value + "'";
82+
}
83+
}

0 commit comments

Comments
 (0)