3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.ws;
22 import java.util.List;
23 import java.util.Random;
24 import java.util.stream.Collectors;
25 import org.junit.Test;
26 import org.sonar.server.health.AppNodeClusterCheck;
27 import org.sonar.server.health.CeStatusNodeCheck;
28 import org.sonar.server.health.ClusterHealthCheck;
29 import org.sonar.server.health.DbConnectionNodeCheck;
30 import org.sonar.server.health.EsStatusClusterCheck;
31 import org.sonar.server.health.EsStatusNodeCheck;
32 import org.sonar.server.health.HealthCheckerImpl;
33 import org.sonar.server.health.NodeHealthCheck;
34 import org.sonar.server.health.WebServerStatusNodeCheck;
35 import org.sonar.core.platform.ListContainer;
36 import org.sonar.server.platform.WebServer;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
42 public class HealthCheckerModuleTest {
43 private final WebServer webServer = mock(WebServer.class);
44 private final HealthCheckerModule underTest = new HealthCheckerModule(webServer);
47 public void verify_HealthChecker() {
48 boolean standalone = new Random().nextBoolean();
49 when(webServer.isStandalone()).thenReturn(standalone);
50 ListContainer container = new ListContainer();
52 underTest.configure(container);
54 assertThat(container.getAddedObjects())
55 .describedAs("Verifying action and HealthChecker with standalone=%s", standalone)
56 .contains(HealthCheckerImpl.class)
57 .doesNotContain(HealthActionSupport.class)
58 .doesNotContain(HealthAction.class)
59 .doesNotContain(SafeModeHealthAction.class);
63 public void verify_installed_NodeHealthChecks_implementations_when_standalone() {
64 when(webServer.isStandalone()).thenReturn(true);
65 ListContainer container = new ListContainer();
67 underTest.configure(container);
69 List<Class<?>> checks = container.getAddedObjects().stream()
70 .filter(o -> o instanceof Class)
71 .map(o -> (Class<?>) o)
72 .filter(NodeHealthCheck.class::isAssignableFrom).collect(Collectors.toList());
73 assertThat(checks).containsOnly(WebServerStatusNodeCheck.class, DbConnectionNodeCheck.class, EsStatusNodeCheck.class, CeStatusNodeCheck.class);
77 public void verify_installed_NodeHealthChecks_implementations_when_clustered() {
78 when(webServer.isStandalone()).thenReturn(false);
79 ListContainer container = new ListContainer();
81 underTest.configure(container);
83 List<Class<?>> checks = container.getAddedObjects().stream()
84 .filter(o -> o instanceof Class)
85 .map(o -> (Class<?>) o)
86 .filter(NodeHealthCheck.class::isAssignableFrom).collect(Collectors.toList());
87 assertThat(checks).containsOnly(WebServerStatusNodeCheck.class, DbConnectionNodeCheck.class, CeStatusNodeCheck.class);
91 public void verify_installed_ClusterHealthChecks_implementations_in_standalone() {
92 when(webServer.isStandalone()).thenReturn(true);
93 ListContainer container = new ListContainer();
95 underTest.configure(container);
97 List<Class<?>> checks = container.getAddedObjects().stream()
98 .filter(o -> o instanceof Class<?>)
99 .map(o -> (Class<?>) o)
100 .filter(ClusterHealthCheck.class::isAssignableFrom).collect(Collectors.toList());
101 assertThat(checks).isEmpty();
105 public void verify_installed_ClusterHealthChecks_implementations_in_clustering() {
106 when(webServer.isStandalone()).thenReturn(false);
107 ListContainer container = new ListContainer();
109 underTest.configure(container);
111 List<Class<?>> checks = container.getAddedObjects().stream()
112 .filter(o -> o instanceof Class<?>)
113 .map(o -> (Class<?>) o)
114 .filter(ClusterHealthCheck.class::isAssignableFrom).collect(Collectors.toList());
115 assertThat(checks).containsOnly(EsStatusClusterCheck.class, AppNodeClusterCheck.class);