3 * Copyright (C) 2009-2023 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.v2.controller;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.sonar.server.platform.ws.LivenessChecker;
29 import org.sonar.server.user.SystemPasscode;
30 import org.sonar.server.user.UserSession;
31 import org.sonar.server.v2.common.RestResponseEntityExceptionHandler;
32 import org.springframework.test.web.servlet.MockMvc;
33 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
35 import static org.mockito.Mockito.when;
36 import static org.sonar.server.v2.WebApiEndpoints.LIVENESS_ENDPOINT;
37 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
38 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40 @RunWith(MockitoJUnitRunner.class)
41 public class DefautLivenessControllerTest {
43 private static final String PASSCODE = "1234";
45 private LivenessChecker livenessChecker;
47 private UserSession userSession;
49 private SystemPasscode systemPasscode;
51 private DefautLivenessController defautLivenessController;
57 this.mockMvc = MockMvcBuilders.standaloneSetup(defautLivenessController)
58 .setControllerAdvice(RestResponseEntityExceptionHandler.class)
63 public void livenessCheck_should_returnForbiddenWithNoCredentials() throws Exception {
64 mockMvc.perform(get(LIVENESS_ENDPOINT))
65 .andExpect(status().isForbidden());
69 public void livenessCheck_should_returnForbiddenWithWrongPasscodeAndNoAdminCredentials() throws Exception {
70 when(systemPasscode.isValidPasscode(PASSCODE)).thenReturn(false);
71 when(userSession.isSystemAdministrator()).thenReturn(false);
72 mockMvc.perform(get(LIVENESS_ENDPOINT).header("X-Sonar-Passcode", PASSCODE))
73 .andExpect(status().isForbidden());
77 public void livenessCheck_should_returnNoContentWithSystemPasscode() throws Exception {
78 when(systemPasscode.isValidPasscode(PASSCODE)).thenReturn(true);
79 when(livenessChecker.liveness()).thenReturn(true);
80 mockMvc.perform(get(LIVENESS_ENDPOINT).header("X-Sonar-Passcode", PASSCODE))
81 .andExpect(status().isNoContent());
85 public void livenessCheck_should_returnNoContentWithWhenUserIsAdmin() throws Exception {
86 when(userSession.isSystemAdministrator()).thenReturn(true);
87 when(livenessChecker.liveness()).thenReturn(true);
88 mockMvc.perform(get(LIVENESS_ENDPOINT).header("X-Sonar-Passcode", PASSCODE))
89 .andExpect(status().isNoContent());
93 public void livenessCheck_should_returnServerErrorWhenLivenessCheckFails() throws Exception {
94 when(systemPasscode.isValidPasscode(PASSCODE)).thenReturn(true);
95 when(livenessChecker.liveness()).thenReturn(false);
96 mockMvc.perform(get(LIVENESS_ENDPOINT).header("X-Sonar-Passcode", PASSCODE))
97 .andExpect(status().isInternalServerError());