]> source.dussan.org Git - sonarqube.git/commitdiff
Add unit test
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 10 Apr 2014 16:35:28 +0000 (18:35 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 10 Apr 2014 16:35:28 +0000 (18:35 +0200)
sonar-server/src/main/java/org/sonar/server/db/migrations/MassUpdater.java
sonar-server/src/test/java/org/sonar/server/db/migrations/MassUpdaterTest.java
sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data_result.xml [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/schema.sql [new file with mode: 0644]

index 0a603ab3a4a81c2d5898a7448ae1a6c9cf2c0876..8ebb52612f61099421a808a252721452caf6924e 100644 (file)
@@ -37,7 +37,6 @@ import java.sql.*;
 public class MassUpdater {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(MassUpdater.class);
-  private static final String FAILURE_MESSAGE = "Fail to migrate data";
   private static final int GROUP_SIZE = 1000;
   private final Database db;
 
@@ -104,14 +103,10 @@ public class MassUpdater {
       }
 
     } catch (SQLException e) {
-      LOGGER.error(FAILURE_MESSAGE, e);
       SqlUtil.log(LOGGER, e);
-      throw MessageException.of(FAILURE_MESSAGE);
-
+      throw processError(e);
     } catch (Exception e) {
-      LOGGER.error(FAILURE_MESSAGE, e);
-      throw MessageException.of(FAILURE_MESSAGE);
-
+      throw processError(e);
     } finally {
       DbUtils.closeQuietly(writeStatement);
       DbUtils.closeQuietly(writeConnection);
@@ -121,6 +116,12 @@ public class MassUpdater {
     }
   }
 
+  private static MessageException processError(Exception e) {
+    String message = String.format("Fail to migrate data, error is : %s", e.getMessage());
+    LOGGER.error(message, e);
+    throw MessageException.of(message);
+  }
+
   @VisibleForTesting
   static String convertSelectSql(String selectSql, Database db){
     String newSelectSql = selectSql;
index 208c0d50687611402efc40569d658a5039d605f4..f5aaeb81aed9d32e69a40acee6e9e3d8167e72fc 100644 (file)
 
 package org.sonar.server.db.migrations;
 
+import org.junit.ClassRule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.api.utils.MessageException;
 import org.sonar.core.persistence.Database;
+import org.sonar.core.persistence.TestDatabase;
 import org.sonar.core.persistence.dialect.Dialect;
 
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
 import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public class MassUpdaterTest {
 
-  @Mock
-  Database db;
+  @ClassRule
+  public static TestDatabase db = new TestDatabase().schema(MassUpdaterTest.class, "schema.sql");
+
+  static class Row {
+    private Long id;
+  }
+
+  @Test
+  public void execute() throws Exception {
+    db.prepareDbUnit(getClass(), "migrate_data.xml");
+
+    new MassUpdater(db.database()).execute(
+      new MassUpdater.InputLoader<Row>() {
+        @Override
+        public String selectSql() {
+          return "SELECT i.id FROM issues i";
+        }
+
+        @Override
+        public Row load(ResultSet rs) throws SQLException {
+          Row row = new Row();
+          row.id = SqlUtil.getLong(rs, 1);
+          return row;
+        }
+      },
+      new MassUpdater.InputConverter<Row>() {
+        @Override
+        public String updateSql() {
+          return "UPDATE issues SET severity=? WHERE id=?";
+        }
+
+        @Override
+        public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException {
+          updateStatement.setString(1, "MAJOR");
+          updateStatement.setLong(2, row.id);
+          return true;
+        }
+      }
+    );
+
+    db.assertDbUnit(getClass(), "migrate_data_result.xml", "issues");
+  }
+
+  @Test
+  public void fail_on_bad_sql_request() throws Exception {
+    db.prepareDbUnit(getClass(), "migrate_data.xml");
+
+    try {
+      new MassUpdater(db.database()).execute(
+        new MassUpdater.InputLoader<Row>() {
+          @Override
+          public String selectSql() {
+            return "<INVALID QUERY>";
+          }
+
+          @Override
+          public Row load(ResultSet rs) throws SQLException {
+            return new Row();
+          }
+        },
+        new MassUpdater.InputConverter<Row>() {
+          @Override
+          public String updateSql() {
+            return "<INVALID QUERY>";
+          }
+
+          @Override
+          public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException {
+            return true;
+          }
+        }
+      );
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(MessageException.class);
+    }
+  }
+
+  @Test
+  public void fail_on_unknown_error() throws Exception {
+    db.prepareDbUnit(getClass(), "migrate_data.xml");
+
+    try {
+      new MassUpdater(db.database()).execute(
+        new MassUpdater.InputLoader<Row>() {
+          @Override
+          public String selectSql() {
+            return "SELECT i.id FROM issues i";
+          }
+
+          @Override
+          public Row load(ResultSet rs) throws SQLException {
+            Row row = new Row();
+            row.id = SqlUtil.getLong(rs, 1);
+            return row;
+          }
+        },
+        new MassUpdater.InputConverter<Row>() {
+          @Override
+          public String updateSql() {
+            throw new RuntimeException("Unknown error");
+          }
+
+          @Override
+          public boolean convert(Row row, PreparedStatement updateStatement) throws SQLException {
+            return true;
+          }
+        }
+      );
+      fail();
+    } catch (Exception e) {
+      assertThat(e).isInstanceOf(MessageException.class);
+    }
+  }
 
   @Test
   public void convert_select_sql() throws Exception {
+    Database db = mock(Database.class);
+
     Dialect dialect = mock(Dialect.class);
     when(dialect.getTrueSqlValue()).thenReturn("true");
     when(dialect.getFalseSqlValue()).thenReturn("false");
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data.xml
new file mode 100644 (file)
index 0000000..5eb657b
--- /dev/null
@@ -0,0 +1,10 @@
+<dataset>
+
+  <issues ID="1" COMPONENT_ID="11" ROOT_COMPONENT_ID="10" RULE_ID="20" SEVERITY="MINOR" KEE="1"
+          ACTION_PLAN_KEY="[null]" ASSIGNEE="[null]" AUTHOR_LOGIN="[null]" CHECKSUM="ABCDE"
+          EFFORT_TO_FIX="3.14" ISSUE_ATTRIBUTES="[null]" ISSUE_CLOSE_DATE="[null]" ISSUE_CREATION_DATE="2012-01-05"
+          ISSUE_UPDATE_DATE="2012-01-05" LINE="1234" MANUAL_SEVERITY="[false]" MESSAGE="the message" REPORTER="[null]"
+          RESOLUTION="[null]" STATUS="OPEN" CREATED_AT="2012-01-05" UPDATED_AT="2012-01-05"
+          TECHNICAL_DEBT="10"/>
+
+</dataset>
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data_result.xml b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/migrate_data_result.xml
new file mode 100644 (file)
index 0000000..ec1ac69
--- /dev/null
@@ -0,0 +1,10 @@
+<dataset>
+
+  <issues ID="1" COMPONENT_ID="11" ROOT_COMPONENT_ID="10" RULE_ID="20" SEVERITY="MAJOR" KEE="1"
+          ACTION_PLAN_KEY="[null]" ASSIGNEE="[null]" AUTHOR_LOGIN="[null]" CHECKSUM="ABCDE"
+          EFFORT_TO_FIX="3.14" ISSUE_ATTRIBUTES="[null]" ISSUE_CLOSE_DATE="[null]" ISSUE_CREATION_DATE="2012-01-05"
+          ISSUE_UPDATE_DATE="2012-01-05" LINE="1234" MANUAL_SEVERITY="[false]" MESSAGE="the message" REPORTER="[null]"
+          RESOLUTION="[null]" STATUS="OPEN" CREATED_AT="2012-01-05" UPDATED_AT="2012-01-05"
+          TECHNICAL_DEBT="10"/>
+
+</dataset>
diff --git a/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/schema.sql b/sonar-server/src/test/resources/org/sonar/server/db/migrations/MassUpdaterTest/schema.sql
new file mode 100644 (file)
index 0000000..f3f71cf
--- /dev/null
@@ -0,0 +1,28 @@
+-- 4.3
+
+CREATE TABLE "ISSUES" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "KEE" VARCHAR(50) UNIQUE NOT NULL,
+  "COMPONENT_ID" INTEGER NOT NULL,
+  "ROOT_COMPONENT_ID" INTEGER,
+  "RULE_ID" INTEGER,
+  "SEVERITY" VARCHAR(10),
+  "MANUAL_SEVERITY" BOOLEAN NOT NULL,
+  "MESSAGE" VARCHAR(4000),
+  "LINE" INTEGER,
+  "EFFORT_TO_FIX" DOUBLE,
+  "STATUS" VARCHAR(20),
+  "RESOLUTION" VARCHAR(20),
+  "CHECKSUM" VARCHAR(1000),
+  "REPORTER" VARCHAR(40),
+  "ASSIGNEE" VARCHAR(40),
+  "AUTHOR_LOGIN" VARCHAR(100),
+  "ACTION_PLAN_KEY" VARCHAR(50) NULL,
+  "ISSUE_ATTRIBUTES" VARCHAR(4000),
+  "ISSUE_CREATION_DATE" TIMESTAMP,
+  "ISSUE_CLOSE_DATE" TIMESTAMP,
+  "ISSUE_UPDATE_DATE" TIMESTAMP,
+  "CREATED_AT" TIMESTAMP,
+  "UPDATED_AT" TIMESTAMP,
+  "TECHNICAL_DEBT" INTEGER
+);