3 * Copyright (C) 2009-2024 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.Arrays;
23 import java.util.Random;
24 import java.util.stream.IntStream;
25 import org.apache.commons.lang3.RandomStringUtils;
26 import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
27 import org.junit.Test;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.server.exceptions.ForbiddenException;
30 import org.sonar.server.health.Health;
31 import org.sonar.server.health.HealthChecker;
32 import org.sonar.server.user.SystemPasscode;
33 import org.sonar.server.ws.TestRequest;
34 import org.sonar.server.ws.TestResponse;
35 import org.sonar.server.ws.WsActionTester;
36 import org.sonarqube.ws.System;
38 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.core.api.Assertions.assertThatThrownBy;
41 import static org.mockito.ArgumentMatchers.any;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.test.JsonAssert.assertJson;
46 public class SafeModeHealthActionTest {
48 private final Random random = new Random();
49 private HealthChecker healthChecker = mock(HealthChecker.class);
50 private SystemPasscode systemPasscode = mock(SystemPasscode.class);
51 private WsActionTester underTest = new WsActionTester(new SafeModeHealthAction(new HealthActionSupport(healthChecker), systemPasscode));
54 public void verify_definition() {
55 WebService.Action definition = underTest.getDef();
57 assertThat(definition.key()).isEqualTo("health");
58 assertThat(definition.isPost()).isFalse();
59 assertThat(definition.description()).isNotEmpty();
60 assertThat(definition.since()).isEqualTo("6.6");
61 assertThat(definition.isInternal()).isFalse();
62 assertThat(definition.responseExample()).isNotNull();
63 assertThat(definition.params()).isEmpty();
67 public void request_fails_with_ForbiddenException_when_PassCode_disabled_or_incorrect() {
68 when(systemPasscode.isValid(any())).thenReturn(false);
69 TestRequest request = underTest.newRequest();
71 expectForbiddenException(() -> request.execute());
75 public void request_succeeds_when_valid_passcode() {
76 authenticateWithPasscode();
77 when(healthChecker.checkNode())
78 .thenReturn(Health.builder()
79 .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)])
81 TestRequest request = underTest.newRequest();
87 public void verify_response_example() {
88 authenticateWithPasscode();
89 when(healthChecker.checkNode())
90 .thenReturn(Health.builder()
91 .setStatus(Health.Status.RED)
92 .addCause("Application node app-1 is RED")
95 TestResponse response = underTest.newRequest().execute();
97 assertJson(response.getInput())
98 .ignoreFields("nodes")
99 .isSimilarTo(underTest.getDef().responseExampleAsString());
103 public void request_returns_status_and_causes_from_HealthChecker_checkNode_method() {
104 authenticateWithPasscode();
105 Health.Status randomStatus = Health.Status.values()[new Random().nextInt(Health.Status.values().length)];
106 Health.Builder builder = Health.builder()
107 .setStatus(randomStatus);
108 IntStream.range(0, new Random().nextInt(5)).mapToObj(i -> RandomStringUtils.randomAlphanumeric(3)).forEach(builder::addCause);
109 Health health = builder.build();
110 when(healthChecker.checkNode()).thenReturn(health);
111 TestRequest request = underTest.newRequest();
113 System.HealthResponse healthResponse = request.executeProtobuf(System.HealthResponse.class);
114 assertThat(healthResponse.getHealth().name()).isEqualTo(randomStatus.name());
115 assertThat(health.getCauses()).isEqualTo(health.getCauses());
119 public void response_contains_status_and_causes_from_HealthChecker_checkCluster() {
120 authenticateWithPasscode();
121 Health.Status randomStatus = Health.Status.values()[random.nextInt(Health.Status.values().length)];
122 String[] causes = IntStream.range(0, random.nextInt(33)).mapToObj(i -> randomAlphanumeric(4)).toArray(String[]::new);
123 Health.Builder healthBuilder = Health.builder()
124 .setStatus(randomStatus);
125 Arrays.stream(causes).forEach(healthBuilder::addCause);
126 when(healthChecker.checkNode()).thenReturn(healthBuilder.build());
128 System.HealthResponse clusterHealthResponse = underTest.newRequest().executeProtobuf(System.HealthResponse.class);
129 assertThat(clusterHealthResponse.getHealth().name()).isEqualTo(randomStatus.name());
130 assertThat(clusterHealthResponse.getCausesList())
131 .extracting(System.Cause::getMessage)
132 .containsOnly(causes);
135 private void expectForbiddenException(ThrowingCallable shouldRaiseThrowable) {
136 assertThatThrownBy(shouldRaiseThrowable)
137 .isInstanceOf(ForbiddenException.class)
138 .hasMessageContaining("Insufficient privileges");
141 private void authenticateWithPasscode() {
142 when(systemPasscode.isValid(any())).thenReturn(true);