aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre <pierre.guillot@sonarsource.com>2022-07-12 17:49:25 +0200
committersonartech <sonartech@sonarsource.com>2022-07-14 20:03:47 +0000
commit2fd25861cb2fc208a7404d1d29e637a984e37569 (patch)
tree84c4dd7a1d19eae590b1942500e8d288caac0de3
parent3a491da6237e599188660550256673112a679eed (diff)
downloadsonarqube-2fd25861cb2fc208a7404d1d29e637a984e37569.tar.gz
sonarqube-2fd25861cb2fc208a7404d1d29e637a984e37569.zip
SONAR-16642 fix race condition on HealthReducer
-rw-r--r--server/sonar-webserver-api/src/main/java/org/sonar/server/health/Health.java10
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java2
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClusterSystemInfoWriterTest.java2
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/AppNodeClusterCheck.java9
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/CeStatusNodeCheck.java4
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/DbConnectionNodeCheck.java4
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusCheck.java8
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusClusterCheck.java4
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java4
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthReducer.java25
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerSafemodeNodeCheck.java4
-rw-r--r--server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerStatusNodeCheck.java4
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/ClusterHealthTest.java5
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthCheckerImplTest.java9
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthTest.java27
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthProviderImplTest.java12
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java9
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java2
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java9
-rw-r--r--server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessCheckerImplTest.java2
20 files changed, 61 insertions, 94 deletions
diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/health/Health.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/health/Health.java
index 881c4ecb860..2d01efe055b 100644
--- a/server/sonar-webserver-api/src/main/java/org/sonar/server/health/Health.java
+++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/health/Health.java
@@ -31,7 +31,7 @@ public class Health {
/**
* The GREEN status without any cause as a constant, for convenience and optimisation.
*/
- public static final Health GREEN = newHealthCheckBuilder()
+ public static final Health GREEN = builder()
.setStatus(Status.GREEN)
.build();
@@ -51,7 +51,7 @@ public class Health {
return causes;
}
- public static Builder newHealthCheckBuilder() {
+ public static Builder builder() {
return new Builder();
}
@@ -91,12 +91,6 @@ public class Health {
// use static factory method
}
- public Builder clear() {
- this.status = null;
- this.causes.clear();
- return this;
- }
-
public Builder setStatus(Status status) {
this.status = checkStatus(status);
return this;
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java
index ab6b062fa44..cbbd50ecefb 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/health/TestStandaloneHealthChecker.java
@@ -21,7 +21,7 @@ package org.sonar.server.health;
public class TestStandaloneHealthChecker implements HealthChecker {
- private Health health = Health.newHealthCheckBuilder().setStatus(Health.Status.GREEN).build();
+ private Health health = Health.builder().setStatus(Health.Status.GREEN).build();
public void setHealth(Health h) {
this.health = h;
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClusterSystemInfoWriterTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClusterSystemInfoWriterTest.java
index 621f947dbba..e93104388e9 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClusterSystemInfoWriterTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClusterSystemInfoWriterTest.java
@@ -56,7 +56,7 @@ public class ClusterSystemInfoWriterTest {
when(globalInfoLoader.load()).thenReturn(Collections.singletonList(createSection("globalInfo")));
when(appNodesInfoLoader.load()).thenReturn(Collections.singletonList(createNodeInfo("appNodes")));
when(searchNodesInfoLoader.load()).thenReturn(Collections.singletonList(createNodeInfo("searchNodes")));
- Health health = Health.newHealthCheckBuilder().setStatus(Health.Status.GREEN).build();
+ Health health = Health.builder().setStatus(Health.Status.GREEN).build();
when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(health, Collections.emptySet()));
}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/AppNodeClusterCheck.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/AppNodeClusterCheck.java
index 3f1bace89da..88e24c3185d 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/AppNodeClusterCheck.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/AppNodeClusterCheck.java
@@ -28,7 +28,6 @@ import static org.sonar.core.util.stream.MoreCollectors.toSet;
import static org.sonar.process.cluster.health.NodeHealth.Status.GREEN;
import static org.sonar.process.cluster.health.NodeHealth.Status.RED;
import static org.sonar.process.cluster.health.NodeHealth.Status.YELLOW;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
public class AppNodeClusterCheck implements ClusterHealthCheck {
@@ -40,7 +39,7 @@ public class AppNodeClusterCheck implements ClusterHealthCheck {
return Arrays.stream(AppNodeClusterHealthSubChecks.values())
.map(s -> s.check(appNodes))
- .reduce(Health.GREEN, HealthReducer.INSTANCE);
+ .reduce(Health.GREEN, HealthReducer::merge);
}
private enum AppNodeClusterHealthSubChecks implements ClusterHealthSubCheck {
@@ -49,7 +48,7 @@ public class AppNodeClusterCheck implements ClusterHealthCheck {
public Health check(Set<NodeHealth> appNodes) {
int appNodeCount = appNodes.size();
if (appNodeCount == 0) {
- return newHealthCheckBuilder()
+ return Health.builder()
.setStatus(Health.Status.RED)
.addCause("No application node")
.build();
@@ -62,7 +61,7 @@ public class AppNodeClusterCheck implements ClusterHealthCheck {
public Health check(Set<NodeHealth> appNodes) {
int appNodeCount = appNodes.size();
if (appNodeCount == 1) {
- return newHealthCheckBuilder()
+ return Health.builder()
.setStatus(Health.Status.YELLOW)
.addCause("There should be at least two application nodes")
.build();
@@ -85,7 +84,7 @@ public class AppNodeClusterCheck implements ClusterHealthCheck {
return Health.GREEN;
}
- Health.Builder builder = newHealthCheckBuilder();
+ Health.Builder builder = Health.builder();
if (redNodesCount == appNodeCount) {
return builder
.setStatus(Health.Status.RED)
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/CeStatusNodeCheck.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/CeStatusNodeCheck.java
index 09e4d1dc6d6..5825cca9a8a 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/CeStatusNodeCheck.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/CeStatusNodeCheck.java
@@ -21,10 +21,8 @@ package org.sonar.server.health;
import org.sonar.server.app.ProcessCommandWrapper;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
-
public class CeStatusNodeCheck implements NodeHealthCheck {
- private static final Health RED_HEALTH = newHealthCheckBuilder()
+ private static final Health RED_HEALTH = Health.builder()
.setStatus(Health.Status.RED)
.addCause("Compute Engine is not operational")
.build();
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/DbConnectionNodeCheck.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/DbConnectionNodeCheck.java
index d63414e4806..4eccf4ce798 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/DbConnectionNodeCheck.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/DbConnectionNodeCheck.java
@@ -25,14 +25,12 @@ import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.IsAliveMapper;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
-
/**
* Checks Web Server can connect to the Database.
*/
public class DbConnectionNodeCheck implements NodeHealthCheck {
private static final Logger LOGGER = Loggers.get(DbConnectionNodeCheck.class);
- private static final Health RED_HEALTH = newHealthCheckBuilder().setStatus(Health.Status.RED).addCause("Can't connect to DB").build();
+ private static final Health RED_HEALTH = Health.builder().setStatus(Health.Status.RED).addCause("Can't connect to DB").build();
private final DbClient dbClient;
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusCheck.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusCheck.java
index 28eae7cfb4e..d00f78c6c1f 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusCheck.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusCheck.java
@@ -26,20 +26,18 @@ import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.server.es.EsClient;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
-
abstract class EsStatusCheck {
private static final Logger LOG = Loggers.get(EsStatusCheck.class);
- private static final Health YELLOW_HEALTH = newHealthCheckBuilder()
+ private static final Health YELLOW_HEALTH = Health.builder()
.setStatus(Health.Status.YELLOW)
.addCause("Elasticsearch status is YELLOW")
.build();
- private static final Health RED_HEALTH = newHealthCheckBuilder()
+ private static final Health RED_HEALTH = Health.builder()
.setStatus(Health.Status.RED)
.addCause("Elasticsearch status is RED")
.build();
- protected static final Health RED_HEALTH_UNAVAILABLE = newHealthCheckBuilder()
+ protected static final Health RED_HEALTH_UNAVAILABLE = Health.builder()
.setStatus(Health.Status.RED)
.addCause("Elasticsearch status is RED (unavailable)")
.build();
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusClusterCheck.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusClusterCheck.java
index a0028c655b8..36871a71438 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusClusterCheck.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/EsStatusClusterCheck.java
@@ -38,7 +38,7 @@ public class EsStatusClusterCheck extends EsStatusCheck implements ClusterHealth
if (esClusterHealth != null) {
Health minimumNodes = checkMinimumNodes(esClusterHealth);
Health clusterStatus = extractStatusHealth(esClusterHealth);
- return HealthReducer.INSTANCE.apply(minimumNodes, clusterStatus);
+ return HealthReducer.merge(minimumNodes, clusterStatus);
}
return RED_HEALTH_UNAVAILABLE;
}
@@ -46,7 +46,7 @@ public class EsStatusClusterCheck extends EsStatusCheck implements ClusterHealth
private static Health checkMinimumNodes(ClusterHealthResponse esClusterHealth) {
int nodeCount = esClusterHealth.getNumberOfNodes();
if (nodeCount < RECOMMENDED_MIN_NUMBER_OF_ES_NODES) {
- return Health.newHealthCheckBuilder()
+ return Health.builder()
.setStatus(Health.Status.YELLOW)
.addCause(MINIMUM_NODE_MESSAGE)
.build();
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java
index 96547b3ed53..44b24091b9f 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthCheckerImpl.java
@@ -66,7 +66,7 @@ public class HealthCheckerImpl implements HealthChecker {
public Health checkNode() {
return nodeHealthChecks.stream()
.map(NodeHealthCheck::check)
- .reduce(Health.GREEN, HealthReducer.INSTANCE);
+ .reduce(Health.GREEN, HealthReducer::merge);
}
@Override
@@ -77,7 +77,7 @@ public class HealthCheckerImpl implements HealthChecker {
Set<NodeHealth> nodeHealths = sharedHealthState.readAll();
Health health = clusterHealthChecks.stream()
.map(clusterHealthCheck -> clusterHealthCheck.check(nodeHealths))
- .reduce(Health.GREEN, HealthReducer.INSTANCE);
+ .reduce(Health.GREEN, HealthReducer::merge);
return new ClusterHealth(health, nodeHealths);
}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthReducer.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthReducer.java
index 8e8c957bd49..2d03a95011b 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthReducer.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/HealthReducer.java
@@ -19,27 +19,16 @@
*/
package org.sonar.server.health;
-import java.util.function.BinaryOperator;
-import java.util.stream.Stream;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
+public class HealthReducer {
-public enum HealthReducer implements BinaryOperator<Health> {
- INSTANCE;
-
- /**
- * According to Javadoc, {@link BinaryOperator} used in method
- * {@link java.util.stream.Stream#reduce(Object, BinaryOperator)} is supposed to be stateless.
- *
- * But as we are sure this {@link BinaryOperator} won't be used on a Stream with {@link Stream#parallel()}
- * feature on, we allow ourselves this optimisation.
- */
- private final Health.Builder builder = newHealthCheckBuilder();
+ private HealthReducer() {
+ // no public constructor
+ }
- @Override
- public Health apply(Health left, Health right) {
- builder.clear();
- builder.setStatus(worseOf(left.getStatus(), right.getStatus()));
+ public static Health merge(Health left, Health right) {
+ Health.Builder builder = Health.builder()
+ .setStatus(worseOf(left.getStatus(), right.getStatus()));
left.getCauses().forEach(builder::addCause);
right.getCauses().forEach(builder::addCause);
return builder.build();
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerSafemodeNodeCheck.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerSafemodeNodeCheck.java
index 3e8136be5d8..09f915f567b 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerSafemodeNodeCheck.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerSafemodeNodeCheck.java
@@ -19,15 +19,13 @@
*/
package org.sonar.server.health;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
-
/**
* Checks the running status of the WebServer when the WebServer is in safemode.
* Obviously, it statically returns a red health status.
*/
public class WebServerSafemodeNodeCheck implements NodeHealthCheck {
- private static final Health RED_HEALTH = newHealthCheckBuilder()
+ private static final Health RED_HEALTH = Health.builder()
.setStatus(Health.Status.RED)
.addCause("SonarQube webserver is not up")
.build();
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerStatusNodeCheck.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerStatusNodeCheck.java
index d0c730609b1..733f3cb7b94 100644
--- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerStatusNodeCheck.java
+++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/health/WebServerStatusNodeCheck.java
@@ -24,8 +24,6 @@ import org.sonar.server.app.RestartFlagHolder;
import org.sonar.server.platform.Platform;
import org.sonar.server.platform.db.migration.DatabaseMigrationState;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
-
/**
* Checks the running status of the WebServer when it is not anymore in safemode.
*/
@@ -51,7 +49,7 @@ public class WebServerStatusNodeCheck implements NodeHealthCheck {
&& !restartFlagHolder.isRestarting()) {
return Health.GREEN;
}
- return newHealthCheckBuilder()
+ return Health.builder()
.setStatus(Health.Status.RED)
.addCause("SonarQube webserver is not up")
.build();
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/ClusterHealthTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/ClusterHealthTest.java
index bb72dcc7cd6..0324f33cd4c 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/ClusterHealthTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/ClusterHealthTest.java
@@ -36,7 +36,6 @@ import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.process.cluster.health.NodeHealth.newNodeHealthBuilder;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
public class ClusterHealthTest {
@@ -78,7 +77,7 @@ public class ClusterHealthTest {
.isNotEqualTo(new Object())
.isNotNull()
.isNotEqualTo(new ClusterHealth(
- newHealthCheckBuilder()
+ Health.builder()
.setStatus(health.getStatus())
.addCause("foo_bar")
.build(),
@@ -119,7 +118,7 @@ public class ClusterHealthTest {
}
private Health randomHealth() {
- Health.Builder healthBuilder = newHealthCheckBuilder();
+ Health.Builder healthBuilder = Health.builder();
healthBuilder.setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]);
IntStream.range(0, random.nextInt(3)).mapToObj(i -> randomAlphanumeric(3)).forEach(healthBuilder::addCause);
return healthBuilder.build();
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthCheckerImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthCheckerImplTest.java
index e5205e2f6f3..127163463ce 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthCheckerImplTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthCheckerImplTest.java
@@ -46,7 +46,6 @@ import static org.sonar.process.cluster.health.NodeHealth.newNodeHealthBuilder;
import static org.sonar.server.health.Health.Status.GREEN;
import static org.sonar.server.health.Health.Status.RED;
import static org.sonar.server.health.Health.Status.YELLOW;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
public class HealthCheckerImplTest {
@@ -269,11 +268,11 @@ public class HealthCheckerImplTest {
private final Health health;
public HardcodedHealthNodeCheck(Health.Status status) {
- this.health = newHealthCheckBuilder().setStatus(status).build();
+ this.health = Health.builder().setStatus(status).build();
}
public HardcodedHealthNodeCheck(String... causes) {
- Health.Builder builder = newHealthCheckBuilder().setStatus(Health.Status.values()[random.nextInt(3)]);
+ Health.Builder builder = Health.builder().setStatus(Health.Status.values()[random.nextInt(3)]);
Stream.of(causes).forEach(builder::addCause);
this.health = builder.build();
}
@@ -288,11 +287,11 @@ public class HealthCheckerImplTest {
private final Health health;
public HardcodedHealthClusterCheck(Health.Status status) {
- this.health = newHealthCheckBuilder().setStatus(status).build();
+ this.health = Health.builder().setStatus(status).build();
}
public HardcodedHealthClusterCheck(String... causes) {
- Health.Builder builder = newHealthCheckBuilder().setStatus(Health.Status.values()[random.nextInt(3)]);
+ Health.Builder builder = Health.builder().setStatus(Health.Status.values()[random.nextInt(3)]);
Stream.of(causes).forEach(builder::addCause);
this.health = builder.build();
}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthTest.java
index 0c677076738..ccbbb859821 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/HealthTest.java
@@ -31,7 +31,6 @@ import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.ThrowableAssert.ThrowingCallable;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
public class HealthTest {
@@ -41,28 +40,28 @@ public class HealthTest {
@Test
public void build_throws_NPE_if_status_is_null() {
- Health.Builder builder = newHealthCheckBuilder();
+ Health.Builder builder = Health.builder();
expectStatusNotNullNPE(() -> builder.build());
}
@Test
public void setStatus_throws_NPE_if_status_is_null() {
- Health.Builder builder = newHealthCheckBuilder();
+ Health.Builder builder = Health.builder();
expectStatusNotNullNPE(() -> builder.setStatus(null));
}
@Test
public void getStatus_returns_status_from_builder() {
- Health underTest = newHealthCheckBuilder().setStatus(anyStatus).build();
+ Health underTest = Health.builder().setStatus(anyStatus).build();
assertThat(underTest.getStatus()).isEqualTo(anyStatus);
}
@Test
public void addCause_throws_NPE_if_arg_is_null() {
- Health.Builder builder = newHealthCheckBuilder();
+ Health.Builder builder = Health.builder();
assertThatThrownBy(() -> builder.addCause(null))
.isInstanceOf(NullPointerException.class)
@@ -71,21 +70,21 @@ public class HealthTest {
@Test
public void addCause_throws_IAE_if_arg_is_empty() {
- Health.Builder builder = newHealthCheckBuilder();
+ Health.Builder builder = Health.builder();
expectCauseCannotBeEmptyIAE(() -> builder.addCause(""));
}
@Test
public void addCause_throws_IAE_if_arg_contains_only_spaces() {
- Health.Builder builder = newHealthCheckBuilder();
+ Health.Builder builder = Health.builder();
expectCauseCannotBeEmptyIAE(() -> builder.addCause(Strings.repeat(" ", 1 + random.nextInt(5))));
}
@Test
public void getCause_returns_causes_from_builder() {
- Health.Builder builder = newHealthCheckBuilder().setStatus(anyStatus);
+ Health.Builder builder = Health.builder().setStatus(anyStatus);
randomCauses.forEach(builder::addCause);
Health underTest = builder.build();
@@ -95,13 +94,13 @@ public class HealthTest {
@Test
public void green_constant() {
- assertThat(Health.GREEN).isEqualTo(newHealthCheckBuilder().setStatus(Health.Status.GREEN).build());
+ assertThat(Health.builder().setStatus(Health.Status.GREEN).build()).isEqualTo(Health.GREEN);
}
@Test
public void equals_is_based_on_status_and_causes() {
- Health.Builder builder1 = newHealthCheckBuilder();
- Health.Builder builder2 = newHealthCheckBuilder();
+ Health.Builder builder1 = Health.builder();
+ Health.Builder builder2 = Health.builder();
builder1.setStatus(anyStatus);
builder2.setStatus(anyStatus);
@@ -126,8 +125,8 @@ public class HealthTest {
@Test
public void hashcode_is_based_on_status_and_causes() {
- Health.Builder builder1 = newHealthCheckBuilder();
- Health.Builder builder2 = newHealthCheckBuilder();
+ Health.Builder builder1 = Health.builder();
+ Health.Builder builder2 = Health.builder();
builder1.setStatus(anyStatus);
builder2.setStatus(anyStatus);
randomCauses.forEach(s -> {
@@ -144,7 +143,7 @@ public class HealthTest {
@Test
public void verify_toString() {
assertThat(Health.GREEN.toString()).isEqualTo("Health{GREEN, causes=[]}");
- Health.Builder builder = newHealthCheckBuilder().setStatus(anyStatus);
+ Health.Builder builder = Health.builder().setStatus(anyStatus);
randomCauses.forEach(builder::addCause);
String underTest = builder.build().toString();
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthProviderImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthProviderImplTest.java
index 3c3e7ad8cb7..7e7019cb735 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthProviderImplTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/health/NodeHealthProviderImplTest.java
@@ -88,7 +88,7 @@ public class NodeHealthProviderImplTest {
when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(4));
Health.Status randomStatus = Health.Status.values()[random.nextInt(Health.Status.values().length)];
String[] expected = IntStream.range(0, random.nextInt(4)).mapToObj(s -> randomAlphabetic(55)).toArray(String[]::new);
- Health.Builder healthBuilder = Health.newHealthCheckBuilder()
+ Health.Builder healthBuilder = Health.builder()
.setStatus(randomStatus);
Arrays.stream(expected).forEach(healthBuilder::addCause);
when(healthChecker.checkNode()).thenReturn(healthBuilder.build());
@@ -105,7 +105,7 @@ public class NodeHealthProviderImplTest {
setRequiredPropertiesForConstructor();
setStartedAt();
when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(23));
- when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder()
+ when(healthChecker.checkNode()).thenReturn(Health.builder()
.setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)])
.build());
NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils);
@@ -122,7 +122,7 @@ public class NodeHealthProviderImplTest {
mapSettings.setProperty(CLUSTER_NODE_NAME.getKey(), name);
mapSettings.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), port);
setStartedAt();
- when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder()
+ when(healthChecker.checkNode()).thenReturn(Health.builder()
.setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)])
.build());
when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(3));
@@ -149,7 +149,7 @@ public class NodeHealthProviderImplTest {
mapSettings.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), 1 + random.nextInt(4));
mapSettings.setProperty(CLUSTER_NODE_HOST.getKey(), host);
setStartedAt();
- when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder()
+ when(healthChecker.checkNode()).thenReturn(Health.builder()
.setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)])
.build());
NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils);
@@ -183,7 +183,7 @@ public class NodeHealthProviderImplTest {
mapSettings.setProperty(CLUSTER_NODE_HOST.getKey(), hostPropertyValue);
}
setStartedAt();
- when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder()
+ when(healthChecker.checkNode()).thenReturn(Health.builder()
.setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)])
.build());
when(networkUtils.getHostname()).thenReturn(host);
@@ -207,7 +207,7 @@ public class NodeHealthProviderImplTest {
when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(4));
Date date = new Date();
when(server.getStartedAt()).thenReturn(date);
- when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder()
+ when(healthChecker.checkNode()).thenReturn(Health.builder()
.setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)])
.build());
NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils);
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java
index 1281d93cff7..ed79a2576b5 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/HealthActionTest.java
@@ -61,7 +61,6 @@ import static org.sonar.api.utils.DateUtils.parseDateTime;
import static org.sonar.process.cluster.health.NodeDetails.newNodeDetailsBuilder;
import static org.sonar.process.cluster.health.NodeHealth.newNodeHealthBuilder;
import static org.sonar.server.health.Health.GREEN;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
import static org.sonar.test.JsonAssert.assertJson;
public class HealthActionTest {
@@ -139,7 +138,7 @@ public class HealthActionTest {
long time = parseDateTime("2015-08-13T23:34:59+0200").getTime();
when(healthChecker.checkCluster())
.thenReturn(
- new ClusterHealth(newHealthCheckBuilder()
+ new ClusterHealth(Health.builder()
.setStatus(Health.Status.RED)
.addCause("Application node app-1 is RED")
.build(),
@@ -212,7 +211,7 @@ public class HealthActionTest {
public void request_returns_status_and_causes_from_HealthChecker_checkNode_method_when_standalone() {
authenticateWithRandomMethod();
Health.Status randomStatus = Health.Status.values()[new Random().nextInt(Health.Status.values().length)];
- Health.Builder builder = newHealthCheckBuilder()
+ Health.Builder builder = Health.builder()
.setStatus(randomStatus);
IntStream.range(0, new Random().nextInt(5)).mapToObj(i -> RandomStringUtils.randomAlphanumeric(3)).forEach(builder::addCause);
Health health = builder.build();
@@ -230,7 +229,7 @@ public class HealthActionTest {
authenticateWithRandomMethod();
Health.Status randomStatus = Health.Status.values()[random.nextInt(Health.Status.values().length)];
String[] causes = IntStream.range(0, random.nextInt(33)).mapToObj(i -> randomAlphanumeric(4)).toArray(String[]::new);
- Health.Builder healthBuilder = newHealthCheckBuilder()
+ Health.Builder healthBuilder = Health.builder()
.setStatus(randomStatus);
Arrays.stream(causes).forEach(healthBuilder::addCause);
when(webServer.isStandalone()).thenReturn(false);
@@ -330,7 +329,7 @@ public class HealthActionTest {
}
private ClusterHealth randomStatusMinimalClusterHealth() {
- return new ClusterHealth(newHealthCheckBuilder()
+ return new ClusterHealth(Health.builder()
.setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)])
.build(), emptySet());
}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java
index 76d605c6403..8f84f339588 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/LivenessCheckerImplTest.java
@@ -32,7 +32,7 @@ import static org.mockito.Mockito.when;
public class LivenessCheckerImplTest {
- public static final Health RED = Health.newHealthCheckBuilder().setStatus(Health.Status.RED).build();
+ public static final Health RED = Health.builder().setStatus(Health.Status.RED).build();
private final DbConnectionNodeCheck dbConnectionNodeCheck = mock(DbConnectionNodeCheck.class);
private final WebServerStatusNodeCheck webServerStatusNodeCheck = mock(WebServerStatusNodeCheck.class);
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java
index fac6bae92c4..5143d9a9f33 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeHealthActionTest.java
@@ -41,7 +41,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.sonar.server.health.Health.newHealthCheckBuilder;
import static org.sonar.test.JsonAssert.assertJson;
public class SafeModeHealthActionTest {
@@ -76,7 +75,7 @@ public class SafeModeHealthActionTest {
public void request_succeeds_when_valid_passcode() {
authenticateWithPasscode();
when(healthChecker.checkNode())
- .thenReturn(newHealthCheckBuilder()
+ .thenReturn(Health.builder()
.setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)])
.build());
TestRequest request = underTest.newRequest();
@@ -88,7 +87,7 @@ public class SafeModeHealthActionTest {
public void verify_response_example() {
authenticateWithPasscode();
when(healthChecker.checkNode())
- .thenReturn(newHealthCheckBuilder()
+ .thenReturn(Health.builder()
.setStatus(Health.Status.RED)
.addCause("Application node app-1 is RED")
.build());
@@ -104,7 +103,7 @@ public class SafeModeHealthActionTest {
public void request_returns_status_and_causes_from_HealthChecker_checkNode_method() {
authenticateWithPasscode();
Health.Status randomStatus = Health.Status.values()[new Random().nextInt(Health.Status.values().length)];
- Health.Builder builder = newHealthCheckBuilder()
+ Health.Builder builder = Health.builder()
.setStatus(randomStatus);
IntStream.range(0, new Random().nextInt(5)).mapToObj(i -> RandomStringUtils.randomAlphanumeric(3)).forEach(builder::addCause);
Health health = builder.build();
@@ -121,7 +120,7 @@ public class SafeModeHealthActionTest {
authenticateWithPasscode();
Health.Status randomStatus = Health.Status.values()[random.nextInt(Health.Status.values().length)];
String[] causes = IntStream.range(0, random.nextInt(33)).mapToObj(i -> randomAlphanumeric(4)).toArray(String[]::new);
- Health.Builder healthBuilder = newHealthCheckBuilder()
+ Health.Builder healthBuilder = Health.builder()
.setStatus(randomStatus);
Arrays.stream(causes).forEach(healthBuilder::addCause);
when(healthChecker.checkNode()).thenReturn(healthBuilder.build());
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessCheckerImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessCheckerImplTest.java
index e93c47e5ab0..2904936a3d7 100644
--- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessCheckerImplTest.java
+++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/platform/ws/SafeModeLivenessCheckerImplTest.java
@@ -29,7 +29,7 @@ import static org.mockito.Mockito.when;
public class SafeModeLivenessCheckerImplTest {
- public static final Health RED = Health.newHealthCheckBuilder().setStatus(Health.Status.RED).build();
+ public static final Health RED = Health.builder().setStatus(Health.Status.RED).build();
private final DbConnectionNodeCheck dbConnectionNodeCheck = mock(DbConnectionNodeCheck.class);
private final SafeModeLivenessCheckerImpl underTest = new SafeModeLivenessCheckerImpl(dbConnectionNodeCheck);