]> source.dussan.org Git - sonarqube.git/blob
091bcd835d60355aada21cf2ae898d2e24f0ec3a
[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 org.junit.Test;
23 import org.sonar.api.server.ws.WebService;
24 import org.sonar.server.common.platform.LivenessChecker;
25 import org.sonar.server.exceptions.ForbiddenException;
26 import org.sonar.server.user.SystemPasscode;
27 import org.sonar.server.ws.TestRequest;
28 import org.sonar.server.ws.WsActionTester;
29
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.core.api.Assertions.assertThatThrownBy;
32 import static org.mockito.ArgumentMatchers.any;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35
36 public class SafeModeLivenessActionTest {
37
38   private final SystemPasscode systemPasscode = mock(SystemPasscode.class);
39   private final LivenessChecker livenessChecker = mock(LivenessChecker.class);
40   private final LivenessActionSupport livenessActionSupport = new LivenessActionSupport(livenessChecker);
41   private final WsActionTester underTest = new WsActionTester(new SafeModeLivenessAction(livenessActionSupport, systemPasscode));
42
43   @Test
44   public void verify_definition() {
45     WebService.Action definition = underTest.getDef();
46
47     assertThat(definition.key()).isEqualTo("liveness");
48     assertThat(definition.isPost()).isFalse();
49     assertThat(definition.description()).isNotEmpty();
50     assertThat(definition.since()).isEqualTo("9.1");
51     assertThat(definition.isInternal()).isTrue();
52     assertThat(definition.responseExample()).isNull();
53     assertThat(definition.params()).isEmpty();
54   }
55
56   @Test
57   public void fail_when_system_passcode_is_invalid() {
58     when(systemPasscode.isValid(any())).thenReturn(false);
59
60     TestRequest request = underTest.newRequest();
61     assertThatThrownBy(request::execute)
62       .isInstanceOf(ForbiddenException.class)
63       .hasMessage("Insufficient privileges");
64   }
65
66   @Test
67   public void liveness_check_failed_expect_500() {
68     when(systemPasscode.isValid(any())).thenReturn(true);
69     when(livenessChecker.liveness()).thenReturn(false);
70
71     TestRequest request = underTest.newRequest();
72     assertThatThrownBy(request::execute)
73       .isInstanceOf(IllegalStateException.class)
74       .hasMessage("Liveness check failed");
75   }
76
77   @Test
78   public void liveness_check_success_expect_204() {
79     when(systemPasscode.isValid(any())).thenReturn(true);
80     when(livenessChecker.liveness()).thenReturn(true);
81
82     assertThat(underTest.newRequest().execute().getStatus()).isEqualTo(204);
83   }
84
85 }