]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5626 get next report mecanism dao implemented and interfaces defined. Thread...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 1 Oct 2014 16:23:52 +0000 (18:23 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 2 Oct 2014 16:11:07 +0000 (18:11 +0200)
18 files changed:
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/JavaCpdEngine.java
server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTask.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTaskCleaner.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTaskLauncher.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTasksCleaner.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/ComputationService.java
server/sonar-server/src/main/java/org/sonar/server/computation/db/AnalysisReportDao.java
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTaskCleanerTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTasksCleanerTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/ComputationServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/db/AnalysisReportDaoTest.java
server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select-with-no-available-report.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select_oldest_available_report.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select_oldest_available_report_with_working_reports_older.xml [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/computation/db/AnalysisReportDto.java
sonar-core/src/main/java/org/sonar/core/computation/db/AnalysisReportMapper.java
sonar-core/src/main/resources/org/sonar/core/computation/db/AnalysisReportMapper.xml

index d7d94be4c57c5f6ef3e7796cc91e74a97d0adc2f..fb128eddcc96f9bbd32d7453eb7b547b390dd4bb 100644 (file)
@@ -65,12 +65,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
+import java.util.concurrent.*;
 
 public class JavaCpdEngine extends CpdEngine {
 
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTask.java b/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTask.java
new file mode 100644 (file)
index 0000000..083fa36
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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.server.computation;
+
+import org.sonar.core.computation.db.AnalysisReportDto;
+
+import java.util.concurrent.TimeUnit;
+
+public class AnalysisReportTask extends Thread {
+  private static final String TASK_NAME = "AnalysisReportTask";
+  private static final int SLEEP_DURATION_IN_SECONDS = 10;
+
+  private final ComputationService service;
+
+  // TODO to improve – the computationService singleton should be retrieved directly in the pico container
+  public AnalysisReportTask(ComputationService service) {
+    super(TASK_NAME);
+    this.service = service;
+  }
+
+  @Override
+  public void run() {
+    while (!this.isInterrupted()) {
+      AnalysisReportDto report = service.findAndBookNextAnalysisReport();
+      if (report == null) {
+        sleepBeforeNextAttempt();
+      } else {
+        service.analyzeReport(report);
+      }
+    }
+  }
+
+  private void sleepBeforeNextAttempt() {
+    try {
+      TimeUnit.SECONDS.sleep(SLEEP_DURATION_IN_SECONDS);
+    } catch (InterruptedException e) {
+      // thread interrupted while sleeping, no action needed
+    }
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTaskCleaner.java b/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTaskCleaner.java
new file mode 100644 (file)
index 0000000..cbed337
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.server.computation;
+
+import org.picocontainer.Startable;
+import org.sonar.api.ServerComponent;
+import org.sonar.api.platform.ServerUpgradeStatus;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.server.computation.db.AnalysisReportDao;
+import org.sonar.server.db.DbClient;
+
+public class AnalysisReportTaskCleaner implements Startable, ServerComponent {
+  private final ServerUpgradeStatus serverUpgradeStatus;
+  private final DbClient dbClient;
+
+  public AnalysisReportTaskCleaner(ServerUpgradeStatus serverUpgradeStatus, DbClient dbClient) {
+    this.serverUpgradeStatus = serverUpgradeStatus;
+    this.dbClient = dbClient;
+  }
+
+  @Override
+  public void start() {
+    DbSession session = dbClient.openSession(false);
+    AnalysisReportDao dao = dbClient.analysisReportDao();
+
+    try {
+      if (serverUpgradeStatus.isUpgraded()) {
+        dao.cleanWithTruncate(session);
+      } else {
+        dao.cleanWithUpdateAllToPendingStatus(session);
+      }
+
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  @Override
+  public void stop() {
+    // do nothing
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTaskLauncher.java b/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTaskLauncher.java
new file mode 100644 (file)
index 0000000..06f07ed
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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.server.computation;
+
+import org.picocontainer.Startable;
+import org.sonar.api.ServerComponent;
+
+public class AnalysisReportTaskLauncher implements Startable, ServerComponent {
+
+  private final ComputationService service;
+
+  private AnalysisReportTask task;
+
+  public AnalysisReportTaskLauncher(ComputationService service) {
+    this.service = service;
+  }
+
+  @Override
+  public void start() {
+    task = new AnalysisReportTask(service);
+    task.start();
+  }
+
+  @Override
+  public void stop() {
+    task.interrupt();
+  }
+
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTasksCleaner.java b/server/sonar-server/src/main/java/org/sonar/server/computation/AnalysisReportTasksCleaner.java
deleted file mode 100644 (file)
index 77929d4..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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.server.computation;
-
-import org.picocontainer.Startable;
-import org.sonar.api.ServerComponent;
-import org.sonar.api.platform.ServerUpgradeStatus;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.server.computation.db.AnalysisReportDao;
-import org.sonar.server.db.DbClient;
-
-public class AnalysisReportTasksCleaner implements Startable, ServerComponent {
-  private final ServerUpgradeStatus serverUpgradeStatus;
-  private final DbClient dbClient;
-
-  public AnalysisReportTasksCleaner(ServerUpgradeStatus serverUpgradeStatus, DbClient dbClient) {
-    this.serverUpgradeStatus = serverUpgradeStatus;
-    this.dbClient = dbClient;
-  }
-
-  @Override
-  public void start() {
-    DbSession session = dbClient.openSession(false);
-    AnalysisReportDao dao = dbClient.analysisReportDao();
-
-    try {
-      if (serverUpgradeStatus.isUpgraded()) {
-        dao.cleanWithTruncate(session);
-      } else {
-        dao.cleanWithUpdateAllToPendingStatus(session);
-      }
-
-      session.commit();
-    } finally {
-      MyBatis.closeQuietly(session);
-    }
-  }
-
-  @Override
-  public void stop() {
-    // do nothing
-  }
-}
index 74112ddd24ed2f50d7f973b26301b5e5f67de85b..23acf45b2db5bf6db966608b21418283883ea027 100644 (file)
@@ -35,10 +35,12 @@ import static org.sonar.core.computation.db.AnalysisReportDto.Status.PENDING;
  * since 5.0
  */
 public class ComputationService implements ServerComponent {
-  private DbClient dbClient;
+  private final DbClient dbClient;
+  private final AnalysisReportDao dao;
 
   public ComputationService(DbClient dbClient) {
     this.dbClient = dbClient;
+    dao = this.dbClient.analysisReportDao();
   }
 
   public void create(String projectKey) {
@@ -46,8 +48,6 @@ public class ComputationService implements ServerComponent {
       .setProjectKey(projectKey)
       .setStatus(PENDING);
 
-    AnalysisReportDao dao = dbClient.analysisReportDao();
-
     DbSession session = dbClient.openSession(false);
     try {
       dao.insert(session, report);
@@ -58,8 +58,6 @@ public class ComputationService implements ServerComponent {
   }
 
   public List<AnalysisReportDto> findByProjectKey(String projectKey) {
-    AnalysisReportDao dao = dbClient.analysisReportDao();
-
     DbSession session = dbClient.openSession(false);
     try {
       return dao.findByProjectKey(session, projectKey);
@@ -67,4 +65,13 @@ public class ComputationService implements ServerComponent {
       MyBatis.closeQuietly(session);
     }
   }
+
+  public AnalysisReportDto findAndBookNextAnalysisReport() {
+    // TODO TBE – implementation needed
+    return null;
+  }
+
+  public void analyzeReport(AnalysisReportDto report) {
+    // TODO TBE – implementation needed
+  }
 }
index 726eebd22a074af746bb94e91ca527969f0c8b1e..a0f32d8d96559f817872a66e2bdfd1db13dd2061 100644 (file)
@@ -23,7 +23,6 @@ package org.sonar.server.computation.db;
 import com.google.common.annotations.VisibleForTesting;
 import org.sonar.api.utils.System2;
 import org.sonar.core.computation.db.AnalysisReportDto;
-import org.sonar.core.computation.db.AnalysisReportDto.Status;
 import org.sonar.core.computation.db.AnalysisReportMapper;
 import org.sonar.core.persistence.DaoComponent;
 import org.sonar.core.persistence.DbSession;
@@ -33,6 +32,9 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
+import static org.sonar.core.computation.db.AnalysisReportDto.Status.PENDING;
+import static org.sonar.core.computation.db.AnalysisReportDto.Status.WORKING;
+
 public class AnalysisReportDao extends BaseDao<AnalysisReportMapper, AnalysisReportDto, String> implements DaoComponent {
 
   private System2 system2;
@@ -51,7 +53,7 @@ public class AnalysisReportDao extends BaseDao<AnalysisReportMapper, AnalysisRep
    * startup task use only
    */
   public void cleanWithUpdateAllToPendingStatus(DbSession session) {
-    mapper(session).cleanWithUpdateAllToPendingStatus(Status.PENDING, new Date(system2.now()));
+    mapper(session).cleanWithUpdateAllToPendingStatus(PENDING, new Date(system2.now()));
   }
 
   /**
@@ -65,13 +67,41 @@ public class AnalysisReportDao extends BaseDao<AnalysisReportMapper, AnalysisRep
     return mapper(session).selectByProjectKey(projectKey);
   }
 
+  public AnalysisReportDto getNextAvailableReport(DbSession session) {
+    // TODO to improve – the query should return one element or null
+    List<AnalysisReportDto> reports = mapper(session).selectNextAvailableReport(PENDING, WORKING);
+
+    if (reports.isEmpty()) {
+      return null;
+    }
+
+    return reports.get(0);
+  }
+
+  public int tryToBookReport(DbSession session, AnalysisReportDto report) {
+    // checkArgument(report.getId() != null);
+    //
+    // report.setStatus(WORKING);
+    //
+    // return mapper(session).update(report);
+    throw new UnsupportedOperationException();
+  }
+
   @Override
   protected AnalysisReportDto doGetNullableByKey(DbSession session, String projectKey) {
     throw new UnsupportedOperationException();
   }
 
   @Override
-  protected AnalysisReportDto doUpdate(DbSession session, AnalysisReportDto issue) {
+  protected AnalysisReportDto doUpdate(DbSession session, AnalysisReportDto report) {
+    // int nbOfReportsChanged = mapper(session).update(report);
+    // checkState(nbOfReportsChanged < 2);
+    //
+    // if (nbOfReportsChanged == 0) {
+    // return null;
+    // }
+    //
+    // return report;
     throw new UnsupportedOperationException();
   }
 
index b55ff6507879fea54a4334ae82e5223fccf0a78a..032091704300523c763d04e9cb5d118755ed3d9d 100644 (file)
@@ -85,7 +85,7 @@ import org.sonar.server.component.DefaultRubyComponentService;
 import org.sonar.server.component.db.ComponentDao;
 import org.sonar.server.component.db.SnapshotDao;
 import org.sonar.server.component.ws.*;
-import org.sonar.server.computation.AnalysisReportTasksCleaner;
+import org.sonar.server.computation.AnalysisReportTaskCleaner;
 import org.sonar.server.computation.ComputationService;
 import org.sonar.server.computation.db.AnalysisReportDao;
 import org.sonar.server.config.ws.PropertiesWs;
@@ -627,7 +627,7 @@ class ServerComponents {
     startupContainer.addSingleton(RegisterServletFilters.class);
     startupContainer.addSingleton(CleanPreviewAnalysisCache.class);
     startupContainer.addSingleton(CopyRequirementsFromCharacteristicsToRules.class);
-    startupContainer.addSingleton(AnalysisReportTasksCleaner.class);
+    startupContainer.addSingleton(AnalysisReportTaskCleaner.class);
 
     DoPrivileged.execute(new DoPrivileged.Task() {
       @Override
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTaskCleanerTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTaskCleanerTest.java
new file mode 100644 (file)
index 0000000..6ee7c7e
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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.server.computation;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.platform.ServerUpgradeStatus;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.server.computation.db.AnalysisReportDao;
+import org.sonar.server.db.DbClient;
+
+import static org.mockito.Mockito.*;
+
+public class AnalysisReportTaskCleanerTest {
+
+  AnalysisReportTaskCleaner sut;
+  ServerUpgradeStatus serverUpgradeStatus;
+  DbClient dbClient;
+  AnalysisReportDao analysisReportDao;
+  DbSession session;
+
+  @Before
+  public void before() {
+    analysisReportDao = mock(AnalysisReportDao.class);
+    serverUpgradeStatus = mock(ServerUpgradeStatus.class);
+    dbClient = mock(DbClient.class);
+    session = mock(DbSession.class);
+
+    when(dbClient.analysisReportDao()).thenReturn(analysisReportDao);
+    when(dbClient.openSession(false)).thenReturn(session);
+
+    sut = new AnalysisReportTaskCleaner(serverUpgradeStatus, dbClient);
+  }
+
+  @Test
+  public void start_must_call_dao_clean_update_to_pending_by_default() {
+    sut.start();
+    verify(analysisReportDao).cleanWithUpdateAllToPendingStatus(any(DbSession.class));
+    sut.stop();
+  }
+
+  @Test
+  public void start_must_call_dao_truncate_when_upgrading() {
+    when(serverUpgradeStatus.isUpgraded()).thenReturn(Boolean.TRUE);
+    sut.start();
+    verify(analysisReportDao).cleanWithTruncate(any(DbSession.class));
+    sut.stop();
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTasksCleanerTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/AnalysisReportTasksCleanerTest.java
deleted file mode 100644 (file)
index e9ee75f..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * 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.server.computation;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.platform.ServerUpgradeStatus;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.server.computation.db.AnalysisReportDao;
-import org.sonar.server.db.DbClient;
-
-import static org.mockito.Mockito.*;
-
-public class AnalysisReportTasksCleanerTest {
-
-  AnalysisReportTasksCleaner sut;
-  ServerUpgradeStatus serverUpgradeStatus;
-  DbClient dbClient;
-  AnalysisReportDao analysisReportDao;
-  DbSession session;
-
-  @Before
-  public void before() {
-    analysisReportDao = mock(AnalysisReportDao.class);
-    serverUpgradeStatus = mock(ServerUpgradeStatus.class);
-    dbClient = mock(DbClient.class);
-    session = mock(DbSession.class);
-
-    when(dbClient.analysisReportDao()).thenReturn(analysisReportDao);
-    when(dbClient.openSession(false)).thenReturn(session);
-
-    sut = new AnalysisReportTasksCleaner(serverUpgradeStatus, dbClient);
-  }
-
-  @Test
-  public void start_must_call_dao_clean_update_to_pending_by_default() {
-    sut.start();
-    verify(analysisReportDao).cleanWithUpdateAllToPendingStatus(any(DbSession.class));
-    sut.stop();
-  }
-
-  @Test
-  public void start_must_call_dao_truncate_when_upgrading() {
-    when(serverUpgradeStatus.isUpgraded()).thenReturn(Boolean.TRUE);
-    sut.start();
-    verify(analysisReportDao).cleanWithTruncate(any(DbSession.class));
-    sut.stop();
-  }
-}
index cacc48e9483d1399d55679fb8868e0b51bac0c74..11913814f7379dbce08e77bc455ce0fd9c7c8197 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.server.computation.db.AnalysisReportDao;
 import org.sonar.server.db.DbClient;
 
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -52,8 +53,21 @@ public class ComputationServiceTest {
   }
 
   @Test
-  public void create_must_call_dao_insert() throws Exception {
+  public void create_must_call_dao_insert() {
     sut.create("ANY-KEY");
     verify(analysisReportDao).insert(any(DbSession.class), any(AnalysisReportDto.class));
   }
+
+  @Test
+  public void findByProjectKey_call_corresponding_dao() {
+    sut.findByProjectKey("ANY_STRING");
+    verify(analysisReportDao).findByProjectKey(any(DbSession.class), anyString());
+  }
+
+  @Test
+  public void test_unimplemented_methods_for_code_coverage_purpose_only() {
+    sut.analyzeReport(new AnalysisReportDto());
+    sut.findAndBookNextAnalysisReport();
+  }
+
 }
\ No newline at end of file
index b11cb7cf4784dc596e626baaba07de45b1dffdec..8fa3fd396605bc5fa4c090ec40ebe22a381bc918 100644 (file)
@@ -30,6 +30,8 @@ import org.sonar.core.computation.db.AnalysisReportDto;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.persistence.TestDatabase;
 
+import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -98,7 +100,7 @@ public class AnalysisReportDaoTest {
   }
 
   @Test
-  public void select_one_report_by_project_key() {
+  public void find_one_report_by_project_key() {
     db.prepareDbUnit(getClass(), "select.xml");
 
     final String projectKey = "123456789-987654321";
@@ -111,7 +113,7 @@ public class AnalysisReportDaoTest {
   }
 
   @Test
-  public void select_several_reports_by_project_key() {
+  public void find_several_reports_by_project_key() {
     db.prepareDbUnit(getClass(), "select.xml");
 
     final String projectKey = "987654321-123456789";
@@ -119,4 +121,56 @@ public class AnalysisReportDaoTest {
 
     assertThat(reports).hasSize(2);
   }
+
+  @Test
+  public void get_oldest_available_report() {
+    db.prepareDbUnit(getClass(), "select_oldest_available_report.xml");
+
+    final String projectKey = "123456789-987654321";
+    AnalysisReportDto nextAvailableReport = dao.getNextAvailableReport(session);
+
+    assertThat(nextAvailableReport.getId()).isEqualTo(2);
+    assertThat(nextAvailableReport.getProjectKey()).isEqualTo(projectKey);
+  }
+
+  @Test
+  public void get_oldest_available_report_with_working_reports_older() {
+    db.prepareDbUnit(getClass(), "select_oldest_available_report_with_working_reports_older.xml");
+
+    final String projectKey = "123456789-987654321";
+    AnalysisReportDto nextAvailableReport = dao.getNextAvailableReport(session);
+
+    assertThat(nextAvailableReport.getId()).isEqualTo(2);
+    assertThat(nextAvailableReport.getProjectKey()).isEqualTo(projectKey);
+  }
+
+  @Test
+  public void null_when_no_available_pending_report_because_working_report_on_the_same_project() {
+    db.prepareDbUnit(getClass(), "select-with-no-available-report.xml");
+
+    AnalysisReportDto nextAvailableReport = dao.getNextAvailableReport(session);
+
+    assertThat(nextAvailableReport).isNull();
+  }
+
+  @Test(expected = UnsupportedOperationException.class)
+  public void doGetNullableByKey_is_not_implemented_yet() {
+    dao.doGetNullableByKey(session, "ANY_STRING");
+  }
+
+  @Test(expected = UnsupportedOperationException.class)
+  public void getSynchronizationParams_is_not_implemented_yet() {
+    dao.getSynchronizationParams(new Date(), new HashMap<String, String>());
+  }
+
+  @Test(expected = UnsupportedOperationException.class)
+  public void doUpdate_is_not_implemented_yet() {
+    dao.doUpdate(session, new AnalysisReportDto());
+  }
+
+  @Test(expected = UnsupportedOperationException.class)
+  public void tryToBookReport_is_not_implemented_yet() {
+    dao.tryToBookReport(session, new AnalysisReportDto());
+  }
+
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select-with-no-available-report.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select-with-no-available-report.xml
new file mode 100644 (file)
index 0000000..8cb1fa0
--- /dev/null
@@ -0,0 +1,27 @@
+<dataset>
+  <analysis_reports
+      id="1"
+      project_key="111111111-987654321"
+      report_data="data-project"
+      report_status="WORKING"
+      created_at="2014-09-25"
+      updated_at="2014-09-26"
+      />
+  <analysis_reports
+      id="2"
+      project_key="123456789-987654321"
+      report_data="data-project"
+      report_status="WORKING"
+      created_at="2014-09-24"
+      updated_at="2014-09-26"
+      />
+  <!-- not select as the previous report which is working is on the same project -->
+  <analysis_reports
+      id="3"
+      project_key="123456789-987654321"
+      report_data="data-project"
+      report_status="PENDING"
+      created_at="2014-09-25"
+      updated_at="2014-09-26"
+      />
+</dataset>
\ No newline at end of file
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select_oldest_available_report.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select_oldest_available_report.xml
new file mode 100644 (file)
index 0000000..64e74fc
--- /dev/null
@@ -0,0 +1,26 @@
+<dataset>
+  <analysis_reports
+      id="1"
+      project_key="111111111-987654321"
+      report_data="data-project"
+      report_status="PENDING"
+      created_at="2014-09-25"
+      updated_at="2014-09-26"
+      />
+  <analysis_reports
+      id="2"
+      project_key="123456789-987654321"
+      report_data="data-project"
+      report_status="PENDING"
+      created_at="2014-09-24"
+      updated_at="2014-09-26"
+      />
+  <analysis_reports
+      id="3"
+      project_key="333333333-123456789"
+      report_data="data-project"
+      report_status="PENDING"
+      created_at="2014-09-25"
+      updated_at="2014-09-26"
+      />
+</dataset>
\ No newline at end of file
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select_oldest_available_report_with_working_reports_older.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/db/AnalysisReportDaoTest/select_oldest_available_report_with_working_reports_older.xml
new file mode 100644 (file)
index 0000000..d0c7f42
--- /dev/null
@@ -0,0 +1,26 @@
+<dataset>
+  <analysis_reports
+      id="1"
+      project_key="111111111-987654321"
+      report_data="data-project"
+      report_status="WORKING"
+      created_at="2014-09-23"
+      updated_at="2014-09-26"
+      />
+  <analysis_reports
+      id="2"
+      project_key="123456789-987654321"
+      report_data="data-project"
+      report_status="PENDING"
+      created_at="2014-09-24"
+      updated_at="2014-09-26"
+      />
+  <analysis_reports
+      id="3"
+      project_key="333333333-123456789"
+      report_data="data-project"
+      report_status="WORKING"
+      created_at="2014-09-25"
+      updated_at="2014-09-26"
+      />
+</dataset>
\ No newline at end of file
index 82ed36befae55b64095e2719d345fcd47ba400c9..f54ff8e63a88fa18bdbefc5109de1a2f43fe2394 100644 (file)
@@ -23,9 +23,6 @@ import org.sonar.core.persistence.Dto;
 
 import javax.annotation.Nullable;
 
-/**
- * since 5.0
- */
 public class AnalysisReportDto extends Dto<String> {
 
   private Long id;
@@ -33,10 +30,6 @@ public class AnalysisReportDto extends Dto<String> {
   private Status status;
   private String data;
 
-  public enum Status {
-    PENDING, WORKING
-  }
-
   public String getProjectKey() {
     return projectKey;
   }
@@ -73,8 +66,7 @@ public class AnalysisReportDto extends Dto<String> {
     return id;
   }
 
-  public AnalysisReportDto setId(Long id) {
-    this.id = id;
-    return this;
+  public enum Status {
+    PENDING, WORKING
   }
 }
index c32f06100709b908cc5842ec9b935fc7fb8354b9..e777a3ba5f714c468095ce9dd70723a832ea575e 100644 (file)
@@ -22,14 +22,14 @@ package org.sonar.core.computation.db;
 import org.apache.ibatis.annotations.Param;
 
 import java.util.Date;
+import java.util.List;
 
-/**
- * since 5.0
- */
 public interface AnalysisReportMapper {
   void insert(AnalysisReportDto report);
 
-  java.util.List<AnalysisReportDto> selectByProjectKey(String projectKey);
+  List<AnalysisReportDto> selectByProjectKey(String projectKey);
+
+  List<AnalysisReportDto> selectNextAvailableReport(@Param("availableStatus") AnalysisReportDto.Status availableStatus, @Param("busyStatus") AnalysisReportDto.Status busyStatus);
 
   /**
    * startup task use only
@@ -40,4 +40,6 @@ public interface AnalysisReportMapper {
    * startup task use only
    */
   void cleanWithTruncate();
+
+  int update(AnalysisReportDto report);
 }
index 785e71a775e631ad00f1d43e64d2fcaef1f612e7..b0b437b8ff6060af27330de1ee2703ceaec4bd9c 100644 (file)
@@ -3,12 +3,12 @@
 
 <mapper namespace="org.sonar.core.computation.db.AnalysisReportMapper">
   <sql id="reportColumns">
-    id,
-    project_key as projectKey,
-    report_status as status,
-    report_data as data,
-    created_at as createdAt,
-    updated_at as updatedAt
+    ar.id,
+    ar.project_key as projectKey,
+    ar.report_status as status,
+    ar.report_data as data,
+    ar.created_at as createdAt,
+    ar.updated_at as updatedAt
   </sql>
 
   <insert id="insert" parameterType="AnalysisReport" useGeneratedKeys="true">
   <select id="selectByProjectKey" parameterType="String" resultType="AnalysisReport">
     select
     <include refid="reportColumns"/>
-    from analysis_reports
+    from analysis_reports ar
     where project_key = #{projectKey}
   </select>
+
+  <select id="selectNextAvailableReport" parameterType="map" resultType="AnalysisReport">
+    select
+    <include refid="reportColumns"/>
+    from analysis_reports ar
+    where ar.report_status=#{availableStatus}
+    and not exists(
+    select 1
+    from analysis_reports ar2
+    where ar.project_key = ar2.project_key
+    and ar2.report_status=#{busyStatus}
+    )
+    order by created_at asc
+  </select>
 </mapper>
\ No newline at end of file