]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11626 Add BranchDao.updateManualBaseline
authorJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 15 Jan 2019 09:25:10 +0000 (10:25 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 11 Feb 2019 08:11:46 +0000 (09:11 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java

index 6a9150d5196b5983469154b30d13fb0bed7b58d8..d2deefe5a5903e5ba5e6b1fc76d2921f639c4da6 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.db.component;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
+import javax.annotation.Nullable;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
@@ -64,17 +65,16 @@ public class BranchDao implements Dao {
   }
 
   /**
-   * Update the baseline_analysis_uuid and baseline_manual columns, if:
+   * Set or unset the uuid of the manual baseline analysis by updating the manual_baseline_analysis_uuid column, if:
    *
    * - the specified uuid exists
-   * - and the specified uuid  corresponds to a long-living branch (including the main branch)
-   * - and the existing baseline_analysis_uuid and baseline_manual values are different
+   * - and the specified uuid corresponds to a long-living branch (including the main branch)
    *
    * @return the number of rows that were updated
    */
-  public int updateBaseline(DbSession dbSession, String uuid, String baselineAnalysisUuid, boolean baselineManual) {
+  public int updateManualBaseline(DbSession dbSession, String uuid, @Nullable String analysisUuid) {
     long now = system2.now();
-    return mapper(dbSession).updateBaseline(uuid, baselineAnalysisUuid, baselineManual, now);
+    return mapper(dbSession).updateManualBaseline(uuid, analysisUuid, now);
   }
 
   public Optional<BranchDto> selectByBranchKey(DbSession dbSession, String projectUuid, String key) {
index 50313de242cb8cd20e4cef1daeb1378456833ff8..411f113855484ef6ecace82a6044042d695859e2 100644 (file)
@@ -89,21 +89,10 @@ public class BranchDto {
   private byte[] pullRequestBinary;
 
   /**
-   * The baseline analysis that marks the start of New Code Period.
-   * Either manually set by user, or else automatically set to the value computed during analysis.
-   *
-   * It may be null for projects which have never been analyzed since the column was introduced.
+   * The UUID of the analysis set by user as manual baseline for computation of the New Code Period, if any.
    */
   @Nullable
-  private String baselineAnalysisUuid;
-
-  /**
-   * Set to true if the user has manually specified a baseline analysis for the branch.
-   *
-   * It may be null for projects which have never been analyzed since the column was introduced.
-   */
-  @Nullable
-  private Boolean baselineManual;
+  private String manualBaseline;
 
   public String getUuid() {
     return uuid;
@@ -207,27 +196,12 @@ public class BranchDto {
   }
 
   @CheckForNull
-  public String getBaselineAnalysisUuid() {
-    return baselineAnalysisUuid;
-  }
-
-  private BranchDto setBaselineAnalysisUuid(@Nullable String baselineAnalysisUuid) {
-    this.baselineAnalysisUuid = baselineAnalysisUuid;
-    return this;
-  }
-
-  public boolean isBaselineManual() {
-    return this.baselineManual != null && this.baselineManual;
-  }
-
-  private BranchDto setBaselineManual(@Nullable Boolean baselineManual) {
-    this.baselineManual = baselineManual != null && baselineManual;
-    return this;
+  public String getManualBaseline() {
+    return manualBaseline;
   }
 
-  public BranchDto setBaseline(@Nullable String baselineAnalysisUuid, @Nullable Boolean baselineManual) {
-    this.baselineAnalysisUuid = baselineAnalysisUuid;
-    this.baselineManual = baselineManual;
+  public BranchDto setManualBaseline(@Nullable String manualBaseline) {
+    this.manualBaseline = manualBaseline;
     return this;
   }
 
@@ -245,8 +219,7 @@ public class BranchDto {
         Objects.equals(kee, branchDto.kee) &&
         branchType == branchDto.branchType &&
         Objects.equals(mergeBranchUuid, branchDto.mergeBranchUuid) &&
-        Objects.equals(baselineAnalysisUuid, branchDto.baselineAnalysisUuid) &&
-        baselineManual == branchDto.baselineManual;
+        Objects.equals(manualBaseline, branchDto.manualBaseline);
   }
 
   @Override
@@ -263,8 +236,7 @@ public class BranchDto {
     sb.append(", keyType=").append(keyType);
     sb.append(", branchType=").append(branchType);
     sb.append(", mergeBranchUuid='").append(mergeBranchUuid).append('\'');
-    sb.append(", baselineAnalysisUuid='").append(baselineAnalysisUuid).append('\'');
-    sb.append(", baselineManual=").append(baselineManual);
+    sb.append(", manualBaseline='").append(manualBaseline).append('\'');
     sb.append('}');
     return sb.toString();
   }
index 330e62c817a51a418f4125cd608476eda87dfd65..10e501b5f5d50f9179d8e1955973035740f0b33c 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.db.component;
 
 import java.util.Collection;
 import java.util.List;
+import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
 
 public interface BranchMapper {
@@ -31,8 +32,7 @@ public interface BranchMapper {
 
   int updateMainBranchName(@Param("projectUuid") String projectUuid, @Param("newBranchName") String newBranchName, @Param("now") long now);
 
-  int updateBaseline(@Param("uuid") String uuid, @Param("baselineAnalysisUuid") String baselineAnalysisUuid,
-    @Param("baselineManual") boolean baselineManual, @Param("now") long now);
+  int updateManualBaseline(@Param("uuid") String uuid, @Nullable @Param("analysisUuid") String analysisUuid, @Param("now") long now);
 
   BranchDto selectByKey(@Param("projectUuid") String projectUuid, @Param("key") String key, @Param("keyType") KeyType keyType);
 
index 1db234d143b9e73c577f847f154821537ed156c0..a7bdecd5d84130870ccce93f8e5af1a51aa5e3b8 100644 (file)
@@ -10,8 +10,7 @@
     pb.branch_type as branchType,
     pb.merge_branch_uuid as mergeBranchUuid,
     pb.pull_request_binary as pullRequestBinary,
-    pb.baseline_analysis_uuid as baselineAnalysisUuid,
-    pb.baseline_manual as baselineManual
+    pb.manual_baseline_analysis_uuid as manualBaseline
   </sql>
 
   <insert id="insert" parameterType="map" useGeneratedKeys="false">
@@ -23,8 +22,7 @@
       branch_type,
       merge_branch_uuid,
       pull_request_binary,
-      baseline_analysis_uuid,
-      baseline_manual,
+      manual_baseline_analysis_uuid,
       created_at,
       updated_at
     ) values (
@@ -35,8 +33,7 @@
       #{dto.branchType, jdbcType=VARCHAR},
       #{dto.mergeBranchUuid, jdbcType=VARCHAR},
       #{dto.pullRequestBinary, jdbcType=BINARY},
-      #{dto.baselineAnalysisUuid, jdbcType=VARCHAR},
-      #{dto.baselineManual, jdbcType=BOOLEAN},
+      #{dto.manualBaseline, jdbcType=VARCHAR},
       #{now, jdbcType=BIGINT},
       #{now, jdbcType=BIGINT}
     )
       uuid = #{projectUuid, jdbcType=VARCHAR}
   </update>
 
-  <update id="updateBaseline" parameterType="map" useGeneratedKeys="false">
+  <update id="updateManualBaseline" parameterType="map" useGeneratedKeys="false">
     update project_branches
     set
-      baseline_analysis_uuid = #{baselineAnalysisUuid, jdbcType=VARCHAR},
-      baseline_manual = #{baselineManual, jdbcType=BOOLEAN},
+      manual_baseline_analysis_uuid = #{analysisUuid, jdbcType=VARCHAR},
       updated_at = #{now, jdbcType=BIGINT}
     where
       uuid = #{uuid, jdbcType=VARCHAR}
       and branch_type = 'LONG'
-      and (baseline_analysis_uuid is null
-        or baseline_analysis_uuid != #{baselineAnalysisUuid, jdbcType=VARCHAR}
-        or baseline_manual != #{baselineManual, jdbcType=BOOLEAN})
   </update>
 
   <update id="update" parameterType="map" useGeneratedKeys="false">
index b25a63f133f4f3d3067c45e04950b69f284dd15f..aab5f139966b730013ba9c37c9daf0bba10ad10e 100644 (file)
@@ -23,6 +23,7 @@ import com.tngtech.java.junit.dataprovider.DataProvider;
 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
 import com.tngtech.java.junit.dataprovider.UseDataProvider;
 import java.util.Map;
+import javax.annotation.Nullable;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -34,6 +35,7 @@ import org.sonar.db.protobuf.DbProjectBranches;
 
 import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.apache.commons.lang.StringUtils.repeat;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.entry;
@@ -99,25 +101,38 @@ public class BranchDaoTest {
   }
 
   @Test
-  public void insert_baseline_analysis() {
+  @UseDataProvider("nullOrEmptyOrValue")
+  public void insert_manual_baseline_analysis_uuid(@Nullable String manualBaselineAnalysisUuid) {
     BranchDto dto = new BranchDto();
     dto.setProjectUuid("U1");
     dto.setUuid("U1");
     dto.setBranchType(BranchType.LONG);
     dto.setKey("foo");
+    dto.setManualBaseline(manualBaselineAnalysisUuid);
+
     underTest.insert(dbSession, dto);
 
-    assertThat(underTest.updateBaseline(dbSession, dto.getUuid(), "dfsf", false)).isEqualTo(1);
+    assertThat(underTest.selectByUuid(dbSession, dto.getUuid()).get().getManualBaseline()).isEqualTo(manualBaselineAnalysisUuid);
+  }
+
+  @DataProvider
+  public static Object[][] nullOrEmptyOrValue() {
+    return new Object[][] {
+      {null},
+      {""},
+      {randomAlphabetic(12)}
+    };
   }
 
   @Test
-  public void update_baseline_on_analysis_uuid_change() {
+  @UseDataProvider("oldAndNewValuesCombinations")
+  public void update_manualBaselineAnalysisUuid_if_new_value_is_different(@Nullable String oldValue, @Nullable String newValue) {
     BranchDto dto = new BranchDto();
     dto.setProjectUuid("U1");
     dto.setUuid("U1");
     dto.setBranchType(BranchType.LONG);
     dto.setKey("foo");
-    dto.setBaseline("a-uuid", false);
+    dto.setManualBaseline(oldValue);
     underTest.insert(dbSession, dto);
 
     BranchDto otherDtoThatShouldNotChange = new BranchDto();
@@ -125,97 +140,55 @@ public class BranchDaoTest {
     otherDtoThatShouldNotChange.setUuid("U2");
     otherDtoThatShouldNotChange.setBranchType(BranchType.LONG);
     otherDtoThatShouldNotChange.setKey("branch");
+    otherDtoThatShouldNotChange.setManualBaseline(oldValue);
     underTest.insert(dbSession, otherDtoThatShouldNotChange);
 
-    String newBaselineAnalysisUuid = dto.getBaselineAnalysisUuid() + "-mod";
-    int updated = underTest.updateBaseline(dbSession, dto.getUuid(), newBaselineAnalysisUuid, dto.isBaselineManual());
-    assertThat(updated).isEqualTo(1);
-    BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get();
-    assertThat(loaded.getBaselineAnalysisUuid()).isEqualTo(newBaselineAnalysisUuid);
-  }
-
-  @Test
-  @UseDataProvider("trueAndFalse")
-  public void update_baseline_on_mode_change(boolean initialMode) {
-    BranchDto dto = new BranchDto();
-    dto.setProjectUuid("U1");
-    dto.setUuid("U1");
-    dto.setBranchType(BranchType.LONG);
-    dto.setKey("foo");
-    dto.setBaseline("a-uuid", initialMode);
-    underTest.insert(dbSession, dto);
-
-    int updated = underTest.updateBaseline(dbSession, dto.getUuid(), dto.getBaselineAnalysisUuid(), !initialMode);
-    assertThat(updated).isEqualTo(1);
-    BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get();
-    assertThat(loaded.isBaselineManual()).isEqualTo(!initialMode);
-  }
-
-  @Test
-  @UseDataProvider("trueAndFalse")
-  public void do_not_update_baseline_when_unchanged(boolean initialMode) {
-    BranchDto dto = new BranchDto();
-    dto.setProjectUuid("U1");
-    dto.setUuid("U1");
-    dto.setBranchType(BranchType.LONG);
-    dto.setKey("foo");
-    dto.setBaseline("a-uuid", initialMode);
-    underTest.insert(dbSession, dto);
+    assertThat(underTest.updateManualBaseline(dbSession, dto.getUuid(), newValue)).isEqualTo(1);
 
-    int updated = underTest.updateBaseline(dbSession, dto.getUuid(), dto.getBaselineAnalysisUuid(), initialMode);
-    assertThat(updated).isZero();
+    assertThat(underTest.selectByUuid(dbSession, dto.getUuid()).get().getManualBaseline())
+      .isEqualTo(newValue);
+    assertThat(underTest.selectByUuid(dbSession, otherDtoThatShouldNotChange.getUuid()).get().getManualBaseline())
+      .isEqualTo(oldValue);
   }
 
   @DataProvider
-  public static Object[][] trueAndFalse() {
+  public static Object[][] oldAndNewValuesCombinations() {
+    String value1 = randomAlphabetic(10);
+    String value2 = randomAlphabetic(20);
     return new Object[][] {
-      {true},
-      {false},
+      {null, value1},
+      {value1, null},
+      {value1, value2},
+      {null, null},
+      {value1, value1}
     };
   }
 
   @Test
-  @UseDataProvider("modeAndNonLongBranchType")
-  public void do_not_update_baseline_of_non_long_branches(boolean initialMode, BranchType branchType) {
-    BranchDto dto = new BranchDto();
-    dto.setProjectUuid("U1");
-    dto.setUuid("U1");
-    dto.setBranchType(branchType);
-    dto.setKey("foo");
-    dto.setBaseline("a-uuid", initialMode);
-    underTest.insert(dbSession, dto);
-
-    int updated = underTest.updateBaseline(dbSession, dto.getUuid(), dto.getBaselineAnalysisUuid() + "-mod", !initialMode);
-    assertThat(updated).isZero();
+  @UseDataProvider("nonLongBranchType")
+  public void do_not_update_manual_baseline_of_non_long_branches(BranchType branchType) {
+    String analysisUuid = randomAlphabetic(12);
+    String otherAnalysisUuid = randomAlphabetic(12);
+    BranchDto noManualBaselineDto = ComponentTesting.newBranchDto(db.components().insertPrivateProject(), branchType);
+    BranchDto withManualBaselineDto = ComponentTesting.newBranchDto(db.components().insertPrivateProject(), branchType).setManualBaseline(analysisUuid);
+    underTest.insert(dbSession, noManualBaselineDto);
+    underTest.insert(dbSession, withManualBaselineDto);
+    dbSession.commit();
+
+    assertThat(underTest.updateManualBaseline(dbSession, noManualBaselineDto.getUuid(), null)).isZero();
+    assertThat(underTest.updateManualBaseline(dbSession, noManualBaselineDto.getUuid(), otherAnalysisUuid)).isZero();
+    assertThat(underTest.updateManualBaseline(dbSession, withManualBaselineDto.getUuid(), null)).isZero();
+    assertThat(underTest.updateManualBaseline(dbSession, withManualBaselineDto.getUuid(), otherAnalysisUuid)).isZero();
   }
 
   @DataProvider
-  public static Object[][] modeAndNonLongBranchType() {
+  public static Object[][] nonLongBranchType() {
     return new Object[][] {
-      {true, BranchType.SHORT},
-      {true, BranchType.PULL_REQUEST},
-      {false, BranchType.SHORT},
-      {false, BranchType.PULL_REQUEST},
+      {BranchType.SHORT},
+      {BranchType.PULL_REQUEST}
     };
   }
 
-  @Test
-  public void isBaselineManual_returns_false_when_column_is_null() {
-    BranchDto dto = new BranchDto();
-    dto.setProjectUuid("U1");
-    dto.setUuid("U1");
-    dto.setBranchType(BranchType.LONG);
-    dto.setKey("foo");
-    dto.setBaseline("a-uuid", true);
-    underTest.insert(dbSession, dto);
-    db.commit();
-
-    db.executeUpdateSql("update project_branches set baseline_manual = null");
-
-    BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get();
-    assertThat(loaded.isBaselineManual()).isEqualTo(false);
-  }
-
   @Test
   public void insert_branch_with_all_fields_and_max_length_values() {
     BranchDto dto = new BranchDto();
@@ -437,6 +410,7 @@ public class BranchDaoTest {
     featureBranch.setBranchType(BranchType.SHORT);
     featureBranch.setKey("feature/foo");
     featureBranch.setMergeBranchUuid("U3");
+    featureBranch.setManualBaseline("analysisUUID");
     underTest.insert(dbSession, featureBranch);
 
     // select the feature branch
@@ -446,6 +420,7 @@ public class BranchDaoTest {
     assertThat(loaded.getProjectUuid()).isEqualTo(featureBranch.getProjectUuid());
     assertThat(loaded.getBranchType()).isEqualTo(featureBranch.getBranchType());
     assertThat(loaded.getMergeBranchUuid()).isEqualTo(featureBranch.getMergeBranchUuid());
+    assertThat(loaded.getManualBaseline()).isEqualTo(featureBranch.getManualBaseline());
 
     // select a branch on another project with same branch name
     assertThat(underTest.selectByBranchKey(dbSession, "U3", "feature/foo")).isEmpty();
@@ -467,6 +442,7 @@ public class BranchDaoTest {
     pullRequest.setBranchType(BranchType.PULL_REQUEST);
     pullRequest.setKey(pullRequestId);
     pullRequest.setMergeBranchUuid("U3");
+    pullRequest.setManualBaseline("analysisUUID");
     underTest.insert(dbSession, pullRequest);
 
     // select the feature branch
@@ -476,6 +452,7 @@ public class BranchDaoTest {
     assertThat(loaded.getProjectUuid()).isEqualTo(pullRequest.getProjectUuid());
     assertThat(loaded.getBranchType()).isEqualTo(pullRequest.getBranchType());
     assertThat(loaded.getMergeBranchUuid()).isEqualTo(pullRequest.getMergeBranchUuid());
+    assertThat(loaded.getManualBaseline()).isEqualTo(pullRequest.getManualBaseline());
 
     // select a branch on another project with same branch name
     assertThat(underTest.selectByPullRequestKey(dbSession, "U3", pullRequestId)).isEmpty();