import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.IsAliveMapper;
+import org.sonar.server.db.DbClient;
import org.sonar.server.db.migrations.DatabaseMigration;
import org.sonar.server.platform.Platform;
*/
public class SystemStatusWsAction implements SystemWsAction {
+ private static final Logger LOGGER = Loggers.get(SystemStatusWsAction.class);
+
private final Server server;
private final DatabaseMigration databaseMigration;
private final Platform platform;
+ private final DbClient dbClient;
- public SystemStatusWsAction(Server server, DatabaseMigration databaseMigration, Platform platform) {
+ public SystemStatusWsAction(Server server, DatabaseMigration databaseMigration, Platform platform, DbClient dbClient) {
this.server = server;
this.databaseMigration = databaseMigration;
this.platform = platform;
+ this.dbClient = dbClient;
}
@Override
}
private boolean isConnectedToDB() {
- // TODO check DB connection is up
- return true;
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ return dbSession.getMapper(IsAliveMapper.class).isAlive() == IsAliveMapper.IS_ALIVE_RETURNED_VALUE;
+ } catch (RuntimeException e) {
+ LOGGER.error("DB connection is down", e);
+ return false;
+ }
}
private Status computeFromDbMigrationStatus() {
import java.io.File;
import java.util.Date;
import java.util.Set;
+import org.junit.Before;
import org.junit.Test;
import org.sonar.api.platform.Server;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.IsAliveMapper;
+import org.sonar.server.db.DbClient;
import org.sonar.server.db.migrations.DatabaseMigration;
import org.sonar.server.platform.Platform;
import org.sonar.server.ws.WsTester;
import static com.google.common.collect.Iterables.filter;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.test.JsonAssert.assertJson;
private static Server server = new Dummy51Server();
private DatabaseMigration databaseMigration = mock(DatabaseMigration.class);
private Platform platform = mock(Platform.class);
+ private DbClient dbClient = mock(DbClient.class);
+ private DbSession dbSession = mock(DbSession.class);
+ private IsAliveMapper isAliveMapper = mock(IsAliveMapper.class);
+ private SystemStatusWsAction underTest = new SystemStatusWsAction(server, databaseMigration, platform, dbClient);
+
private Request request = mock(Request.class);
- private SystemStatusWsAction underTest = new SystemStatusWsAction(server, databaseMigration, platform);
+
+ @Before
+ public void wireMocks() throws Exception {
+ when(dbClient.openSession(anyBoolean())).thenReturn(dbSession);
+ when(dbSession.getMapper(IsAliveMapper.class)).thenReturn(isAliveMapper);
+ }
@Test
public void action_status_is_defined() throws Exception {
@Test
public void verify_example() throws Exception {
+ when(isAliveMapper.isAlive()).thenReturn(IsAliveMapper.IS_ALIVE_RETURNED_VALUE);
when(platform.status()).thenReturn(Platform.Status.UP);
WsTester.TestResponse response = new WsTester.TestResponse();
verifyStatus(Platform.Status.SAFEMODE, DatabaseMigration.Status.FAILED, STATUS_DOWN);
}
+ @Test
+ public void status_is_DOWN_if_any_error_occurs_when_checking_DB() throws Exception {
+ when(isAliveMapper.isAlive()).thenThrow(new RuntimeException("simulated runtime exception when querying DB"));
+
+ WsTester.TestResponse response = new WsTester.TestResponse();
+ underTest.handle(request, response);
+
+ assertJson(response.outputAsString()).isSimilarTo("{" +
+ " \"status\": \"DOWN\"\n" +
+ "}");
+ }
+
+ /**
+ * By contract {@link IsAliveMapper#isAlive()} can not return anything but 1. Still we write this test as a
+ * protection against change in this contract.
+ */
+ @Test
+ public void status_is_DOWN_if_isAlive_does_not_return_1() throws Exception {
+ when(isAliveMapper.isAlive()).thenReturn(12);
+
+ WsTester.TestResponse response = new WsTester.TestResponse();
+ underTest.handle(request, response);
+
+ assertJson(response.outputAsString()).isSimilarTo("{" +
+ " \"status\": \"" + STATUS_DOWN + "\"\n" +
+ "}");
+ }
+
@Test
public void safety_test_for_new_platform_status() throws Exception {
for (Platform.Status platformStatus : filter(asList(Platform.Status.values()), not(in(SUPPORTED_PLATFORM_STATUSES)))) {
}
private void verifyStatus(Platform.Status platformStatus, DatabaseMigration.Status databaseMigrationStatus, String expectedStatus) throws Exception {
+ when(isAliveMapper.isAlive()).thenReturn(IsAliveMapper.IS_ALIVE_RETURNED_VALUE);
when(platform.status()).thenReturn(platformStatus);
when(databaseMigration.status()).thenReturn(databaseMigrationStatus);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.persistence;
+
+public interface IsAliveMapper {
+ static final int IS_ALIVE_RETURNED_VALUE = 1;
+ /**
+ * Always return {@link #IS_ALIVE_RETURNED_VALUE} unless a database or connection error occurs.
+ */
+ int isAlive();
+}
package org.sonar.core.persistence;
-import ch.qos.logback.classic.Level;
-import com.google.common.io.Closeables;
+import java.io.InputStream;
+
+import javax.annotation.Nullable;
+
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.mapping.Environment;
import org.sonar.core.user.UserMapper;
import org.sonar.core.user.UserRoleDto;
-import javax.annotation.Nullable;
+import ch.qos.logback.classic.Level;
-import java.io.InputStream;
+import com.google.common.io.Closeables;
public class MyBatis implements BatchComponent, ServerComponent {
Class<?>[] mappers = {ActivityMapper.class, ActiveDashboardMapper.class, AuthorMapper.class, DashboardMapper.class,
FileDependencyMapper.class, DuplicationMapper.class, GraphDtoMapper.class,
IssueMapper.class, IssueChangeMapper.class, IssueFilterMapper.class, IssueFilterFavouriteMapper.class,
+ IsAliveMapper.class,
LoadedTemplateMapper.class, MeasureFilterMapper.class, Migration44Mapper.class, PermissionTemplateMapper.class, PropertiesMapper.class, PurgeMapper.class,
ResourceKeyUpdaterMapper.class, ResourceIndexerMapper.class, RoleMapper.class, RuleMapper.class,
SchemaMigrationMapper.class, SemaphoreMapper.class, UserMapper.class, GroupMapper.class, UserGroupMapper.class, WidgetMapper.class, WidgetPropertyMapper.class,
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.persistence.IsAliveMapper">
+
+ <select id="isAlive" resultType="int" >
+ select 1
+ <choose>
+ <when test="_databaseId == 'oracle'">
+ from dual
+ </when>
+ </choose>
+ </select>
+
+</mapper>
+
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.persistence;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.sonar.test.DbTests;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@Category(DbTests.class)
+public class IsAliveMapperTest {
+
+ @ClassRule
+ public static DbTester dbTester = new DbTester();
+
+ DbSession session;
+ IsAliveMapper underTest;
+
+ @Before
+ public void setUp() throws Exception {
+ session = dbTester.myBatis().openSession(false);
+ underTest = session.getMapper(IsAliveMapper.class);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ session.close();
+ }
+
+ @Test
+ public void isAlive_works_for_current_vendors() throws Exception {
+ assertThat(underTest.isAlive()).isEqualTo(IsAliveMapper.IS_ALIVE_RETURNED_VALUE);
+ }
+}