]> source.dussan.org Git - sonarqube.git/blob
283dd04fb5c62647adfd69485a373743ea943b15
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.ws;
21
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.jupiter.api.Test;
28 import org.junit.jupiter.params.ParameterizedTest;
29 import org.junit.jupiter.params.provider.EnumSource;
30 import org.sonar.api.server.ws.WebService;
31 import org.sonar.server.exceptions.ForbiddenException;
32 import org.sonar.server.health.Health;
33 import org.sonar.server.health.HealthChecker;
34 import org.sonar.server.user.SystemPasscode;
35 import org.sonar.server.ws.TestRequest;
36 import org.sonar.server.ws.TestResponse;
37 import org.sonar.server.ws.WsActionTester;
38 import org.sonarqube.ws.System;
39
40 import static org.apache.commons.lang3.RandomStringUtils.secure;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.assertj.core.api.Assertions.assertThatThrownBy;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46 import static org.sonar.test.JsonAssert.assertJson;
47
48 public class SafeModeHealthActionTest {
49
50   private final HealthChecker healthChecker = mock(HealthChecker.class);
51   private final SystemPasscode systemPasscode = mock(SystemPasscode.class);
52   private final WsActionTester underTest = new WsActionTester(new SafeModeHealthAction(new HealthActionSupport(healthChecker), systemPasscode));
53
54   @Test
55   void verify_definition() {
56     WebService.Action definition = underTest.getDef();
57
58     assertThat(definition.key()).isEqualTo("health");
59     assertThat(definition.isPost()).isFalse();
60     assertThat(definition.description()).isNotEmpty();
61     assertThat(definition.since()).isEqualTo("6.6");
62     assertThat(definition.isInternal()).isFalse();
63     assertThat(definition.responseExample()).isNotNull();
64     assertThat(definition.params()).isEmpty();
65   }
66
67   @Test
68   void request_fails_with_ForbiddenException_when_PassCode_disabled_or_incorrect() {
69     when(systemPasscode.isValid(any())).thenReturn(false);
70     TestRequest request = underTest.newRequest();
71
72     expectForbiddenException(() -> request.execute());
73   }
74
75   @ParameterizedTest
76   @EnumSource(Health.Status.class)
77   void request_succeeds_when_valid_passcode(Health.Status healthStatus) {
78     authenticateWithPasscode();
79     when(healthChecker.checkNode())
80       .thenReturn(Health.builder()
81         .setStatus(healthStatus)
82         .build());
83     TestRequest request = underTest.newRequest();
84
85     request.execute();
86   }
87
88   @Test
89   void verify_response_example() {
90     authenticateWithPasscode();
91     when(healthChecker.checkNode())
92       .thenReturn(Health.builder()
93         .setStatus(Health.Status.RED)
94         .addCause("Application node app-1 is RED")
95         .build());
96
97     TestResponse response = underTest.newRequest().execute();
98
99     assertJson(response.getInput())
100       .ignoreFields("nodes")
101       .isSimilarTo(underTest.getDef().responseExampleAsString());
102   }
103
104   @ParameterizedTest
105   @EnumSource(Health.Status.class)
106   void request_returns_status_and_causes_from_HealthChecker_checkNode_method(Health.Status healthStatus) {
107     authenticateWithPasscode();
108     Health.Builder builder = Health.builder()
109       .setStatus(healthStatus);
110     IntStream.range(0, new Random().nextInt(5)).mapToObj(i -> RandomStringUtils.secure().nextAlphanumeric(3)).forEach(builder::addCause);
111     Health health = builder.build();
112     when(healthChecker.checkNode()).thenReturn(health);
113     TestRequest request = underTest.newRequest();
114
115     System.HealthResponse healthResponse = request.executeProtobuf(System.HealthResponse.class);
116     assertThat(healthResponse.getHealth().name()).isEqualTo(healthStatus.name());
117     assertThat(health.getCauses()).isEqualTo(health.getCauses());
118   }
119
120   @ParameterizedTest
121   @EnumSource(Health.Status.class)
122   void response_contains_status_and_causes_from_HealthChecker_checkCluster(Health.Status healthStatus) {
123     authenticateWithPasscode();
124     String[] causes = IntStream.range(0, new Random().nextInt(33)).mapToObj(i -> secure().nextAlphanumeric(4)).toArray(String[]::new);
125     Health.Builder healthBuilder = Health.builder()
126       .setStatus(healthStatus);
127     Arrays.stream(causes).forEach(healthBuilder::addCause);
128     when(healthChecker.checkNode()).thenReturn(healthBuilder.build());
129
130     System.HealthResponse clusterHealthResponse = underTest.newRequest().executeProtobuf(System.HealthResponse.class);
131     assertThat(clusterHealthResponse.getHealth().name()).isEqualTo(healthStatus.name());
132     assertThat(clusterHealthResponse.getCausesList())
133       .extracting(System.Cause::getMessage)
134       .containsOnly(causes);
135   }
136
137   private void expectForbiddenException(ThrowingCallable shouldRaiseThrowable) {
138     assertThatThrownBy(shouldRaiseThrowable)
139       .isInstanceOf(ForbiddenException.class)
140       .hasMessageContaining("Insufficient privileges");
141   }
142
143   private void authenticateWithPasscode() {
144     when(systemPasscode.isValid(any())).thenReturn(true);
145   }
146
147 }