]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 mergeBranch returns default main branch when empty
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 22 Aug 2017 15:10:04 +0000 (17:10 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 09:34:49 +0000 (11:34 +0200)
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchDtoToWsBranch.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchesWs.java
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ListAction.java
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ListActionTest.java
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchDtoToWsBranch.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/BranchDtoToWsBranch.java
new file mode 100644 (file)
index 0000000..fa08135
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.projectbranch.ws;
+
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.sonar.db.component.BranchDto;
+import org.sonar.db.measure.MeasureDto;
+import org.sonarqube.ws.Common;
+import org.sonarqube.ws.WsBranches;
+
+import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
+import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
+import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
+import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
+import static org.sonar.core.util.Protobuf.setNullable;
+import static org.sonar.db.component.BranchType.LONG;
+import static org.sonar.db.component.BranchType.SHORT;
+import static org.sonar.server.projectbranch.ws.BranchesWs.DEFAULT_MAIN_BRANCH_NAME;
+
+public class BranchDtoToWsBranch {
+
+  private BranchDtoToWsBranch() {
+    // static methods only
+  }
+
+  public static WsBranches.Branch.Builder toBranchBuilder(BranchDto branch, @Nullable BranchDto mergeBranch, Map<String, MeasureDto> measuresByMetricKey) {
+    WsBranches.Branch.Builder builder = WsBranches.Branch.newBuilder();
+    String branchKey = branch.getKey();
+    String effectiveBranchKey = branchKey == null && branch.isMain() ? DEFAULT_MAIN_BRANCH_NAME : branchKey;
+    setNullable(effectiveBranchKey, builder::setName);
+    builder.setIsMain(branch.isMain());
+    builder.setType(Common.BranchType.valueOf(branch.getBranchType().name()));
+    if (mergeBranch != null) {
+      String mergeBranchKey = mergeBranch.getKey();
+      if (mergeBranchKey == null && branch.getBranchType().equals(SHORT)) {
+        builder.setMergeBranch(DEFAULT_MAIN_BRANCH_NAME);
+      } else {
+        setNullable(mergeBranchKey, builder::setMergeBranch);
+      }
+    }
+
+    if (branch.getBranchType().equals(LONG)) {
+      WsBranches.Branch.Status.Builder statusBuilder = WsBranches.Branch.Status.newBuilder();
+      MeasureDto measure = measuresByMetricKey.get(ALERT_STATUS_KEY);
+      setNullable(measure, m -> builder.setStatus(statusBuilder.setQualityGateStatus(m.getData())));
+    }
+
+    if (branch.getBranchType().equals(SHORT)) {
+      WsBranches.Branch.Status.Builder statusBuilder = WsBranches.Branch.Status.newBuilder();
+      MeasureDto bugs = measuresByMetricKey.get(BUGS_KEY);
+      setNullable(bugs, m -> builder.setStatus(statusBuilder.setBugs(m.getValue().intValue())));
+
+      MeasureDto vulnerabilities = measuresByMetricKey.get(VULNERABILITIES_KEY);
+      setNullable(vulnerabilities, m -> builder.setStatus(statusBuilder.setVulnerabilities(m.getValue().intValue())));
+
+      MeasureDto codeSmells = measuresByMetricKey.get(CODE_SMELLS_KEY);
+      setNullable(codeSmells, m -> builder.setStatus(statusBuilder.setCodeSmells(m.getValue().intValue())));
+      builder.setStatus(statusBuilder);
+    }
+    return builder;
+  }
+}
index ceb76423220457c4037ce2a66fc0de71a69c9cc2..0fc3ce92a8ba12c07025357c735c8eb46d297eb9 100644 (file)
@@ -26,6 +26,8 @@ import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.
 
 public class BranchesWs implements WebService {
 
+  public static final String DEFAULT_MAIN_BRANCH_NAME = "master";
+
   private final BranchWsAction[] actions;
 
   public BranchesWs(BranchWsAction... actions) {
index 38b902660a2e7e93cded01bef6baaa56530231ec..1291dba44b94c96573f5de22fabe4bb185a9fc08 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.function.Function;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -40,7 +41,6 @@ import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.ws.WsUtils;
 import org.sonarqube.ws.WsBranches;
-import org.sonarqube.ws.WsBranches.Branch.Status;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
@@ -50,14 +50,11 @@ import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
 import static org.sonar.api.resources.Qualifiers.PROJECT;
-import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.core.util.stream.MoreCollectors.index;
 import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
-import static org.sonar.db.component.BranchType.LONG;
-import static org.sonar.db.component.BranchType.SHORT;
+import static org.sonar.server.projectbranch.ws.BranchDtoToWsBranch.toBranchBuilder;
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
-import static org.sonarqube.ws.Common.BranchType;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
 
@@ -100,7 +97,6 @@ public class ListAction implements BranchWsAction {
 
       List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, asList(ALERT_STATUS_KEY, BUGS_KEY, VULNERABILITIES_KEY, CODE_SMELLS_KEY));
       Map<Integer, MetricDto> metricsById = metrics.stream().collect(uniqueIndex(MetricDto::getId));
-      Map<String, Integer> metricIdsByKey = metrics.stream().collect(uniqueIndex(MetricDto::getKey, MetricDto::getId));
 
       Collection<BranchDto> branches = dbClient.branchDao().selectByComponent(dbSession, project);
       Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao()
@@ -113,48 +109,23 @@ public class ListAction implements BranchWsAction {
       WsBranches.ListWsResponse.Builder protobufResponse = WsBranches.ListWsResponse.newBuilder();
       branches.stream()
         .filter(b -> b.getKeeType().equals(BranchKeyType.BRANCH))
-        .forEach(b -> addToProtobuf(protobufResponse, b, mergeBranchesByUuid, metricIdsByKey, measuresByComponentUuids));
+        .forEach(b -> addBranch(protobufResponse, b, mergeBranchesByUuid, metricsById, measuresByComponentUuids.get(b.getUuid())));
       WsUtils.writeProtobuf(protobufResponse.build(), request, response);
     }
   }
 
-  private static void addToProtobuf(WsBranches.ListWsResponse.Builder response, BranchDto branch, Map<String, BranchDto> mergeBranchesByUuid,
-    Map<String, Integer> metricIdsByKey, Multimap<String, MeasureDto> measuresByComponentUuids) {
-    WsBranches.Branch.Builder builder = response.addBranchesBuilder();
-    setNullable(branch.getKey(), builder::setName);
-    builder.setIsMain(branch.isMain());
-    builder.setType(BranchType.valueOf(branch.getBranchType().name()));
+  private static void addBranch(WsBranches.ListWsResponse.Builder response, BranchDto branch, Map<String, BranchDto> mergeBranchesByUuid,
+    Map<Integer, MetricDto> metricsById, Collection<MeasureDto> measures) {
+
+    BranchDto mergeBranch = null;
     String mergeBranchUuid = branch.getMergeBranchUuid();
     if (mergeBranchUuid != null) {
-      BranchDto mergeBranch = mergeBranchesByUuid.get(mergeBranchUuid);
+      mergeBranch = mergeBranchesByUuid.get(mergeBranchUuid);
       checkState(mergeBranch != null, "Component uuid '%s' cannot be found", mergeBranch);
-      setNullable(mergeBranch.getKey(), builder::setMergeBranch);
-    }
-
-    Collection<MeasureDto> componentMeasures = measuresByComponentUuids.get(branch.getUuid());
-    if (branch.getBranchType().equals(LONG)) {
-      Status.Builder statusBuilder = Status.newBuilder();
-      int qualityGateStatusMetricId = metricIdsByKey.get(ALERT_STATUS_KEY);
-      componentMeasures.stream().filter(m -> m.getMetricId() == qualityGateStatusMetricId).findAny()
-        .ifPresent(measure -> builder.setStatus(statusBuilder.setQualityGateStatus(measure.getData())));
-    }
-
-    if (branch.getBranchType().equals(SHORT)) {
-      Status.Builder statusBuilder = Status.newBuilder();
-      int bugsMetricId = metricIdsByKey.get(BUGS_KEY);
-      componentMeasures.stream().filter(m -> m.getMetricId() == bugsMetricId).findAny()
-        .ifPresent(measure -> statusBuilder.setBugs(measure.getValue().intValue()));
-
-      int vulnerabilitiesMetricId = metricIdsByKey.get(VULNERABILITIES_KEY);
-      componentMeasures.stream().filter(m -> m.getMetricId() == vulnerabilitiesMetricId).findAny()
-        .ifPresent(measure -> statusBuilder.setVulnerabilities(measure.getValue().intValue()));
-
-      int codeSmellMetricId = metricIdsByKey.get(CODE_SMELLS_KEY);
-      componentMeasures.stream().filter(m -> m.getMetricId() == codeSmellMetricId).findAny()
-        .ifPresent(measure -> statusBuilder.setCodeSmells(measure.getValue().intValue()));
-      builder.setStatus(statusBuilder);
     }
-    builder.build();
+    response.addBranches(
+      toBranchBuilder(branch, mergeBranch,
+        measures.stream().collect(uniqueIndex(m -> metricsById.get(m.getMetricId()).getKey(), Function.identity()))));
   }
 
 }
index 1f3e1bffa52ae127f546728a416376c599606ea4..041611379c8467707735e415f6591a83efc1f774 100644 (file)
  */
 package org.sonar.server.projectbranch.ws;
 
-import com.google.common.collect.Multimap;
 import com.google.common.io.Resources;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import java.util.function.Function;
 import javax.annotation.Nullable;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
@@ -40,9 +40,7 @@ import org.sonar.db.metric.MetricDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
 import org.sonar.server.user.UserSession;
-import org.sonar.server.ws.WsUtils;
 import org.sonarqube.ws.WsBranches;
-import org.sonarqube.ws.WsBranches.Branch.Status;
 import org.sonarqube.ws.WsBranches.ShowWsResponse;
 
 import static com.google.common.base.Preconditions.checkState;
@@ -51,14 +49,12 @@ import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
 import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
-import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.core.util.stream.MoreCollectors.index;
 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
-import static org.sonar.db.component.BranchType.LONG;
-import static org.sonar.db.component.BranchType.SHORT;
+import static org.sonar.server.projectbranch.ws.BranchDtoToWsBranch.toBranchBuilder;
 import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
 import static org.sonar.server.ws.KeyExamples.KEY_FILE_EXAMPLE_001;
-import static org.sonarqube.ws.Common.BranchType;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_SHOW;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_BRANCH;
 import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_COMPONENT;
@@ -105,7 +101,7 @@ public class ShowAction implements BranchWsAction {
   @Override
   public void handle(Request request, Response response) throws Exception {
     if (!isBranchFeatureSupported) {
-      WsUtils.writeProtobuf(WsBranches.Branch.newBuilder().build(), request, response);
+      writeProtobuf(WsBranches.Branch.newBuilder().build(), request, response);
       return;
     }
     String projectKey = request.mandatoryParam(PARAM_COMPONENT);
@@ -117,17 +113,20 @@ public class ShowAction implements BranchWsAction {
 
       List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, asList(ALERT_STATUS_KEY, BUGS_KEY, VULNERABILITIES_KEY, CODE_SMELLS_KEY));
       Map<Integer, MetricDto> metricsById = metrics.stream().collect(uniqueIndex(MetricDto::getId));
-      Map<String, Integer> metricIdsByKey = metrics.stream().collect(uniqueIndex(MetricDto::getKey, MetricDto::getId));
 
       BranchDto branch = getBranch(dbSession, component.projectUuid());
       String mergeBranchUuid = branch.getMergeBranchUuid();
       BranchDto mergeBranch = mergeBranchUuid == null ? null : getBranch(dbSession, mergeBranchUuid);
 
-      Multimap<String, MeasureDto> measuresByComponentUuids = dbClient.measureDao()
+      Collection<MeasureDto> measures = dbClient.measureDao()
         .selectByComponentsAndMetrics(dbSession, Collections.singletonList(branch.getUuid()), metricsById.keySet())
-        .stream().collect(index(MeasureDto::getComponentUuid));
+        .stream().collect(index(MeasureDto::getComponentUuid))
+        .get(branch.getUuid());
 
-      WsUtils.writeProtobuf(buildResponse(branch, mergeBranch, metricIdsByKey, measuresByComponentUuids), request, response);
+      writeProtobuf(ShowWsResponse.newBuilder()
+        .setBranch(
+          toBranchBuilder(branch, mergeBranch, measures.stream().collect(uniqueIndex(m -> metricsById.get(m.getMetricId()).getKey(), Function.identity()))))
+        .build(), request, response);
     }
   }
 
@@ -144,40 +143,4 @@ public class ShowAction implements BranchWsAction {
     return branch.get();
   }
 
-  private static ShowWsResponse buildResponse(BranchDto branch, @Nullable BranchDto mergeBranch,
-    Map<String, Integer> metricIdsByKey, Multimap<String, MeasureDto> measuresByComponentUuids) {
-    WsBranches.Branch.Builder builder = WsBranches.Branch.newBuilder();
-    setNullable(branch.getKey(), builder::setName);
-    builder.setIsMain(branch.isMain());
-    builder.setType(BranchType.valueOf(branch.getBranchType().name()));
-    if (mergeBranch != null) {
-      setNullable(mergeBranch.getKey(), builder::setMergeBranch);
-    }
-
-    Status.Builder statusBuilder = Status.newBuilder();
-    Collection<MeasureDto> componentMeasures = measuresByComponentUuids.get(branch.getUuid());
-    if (branch.getBranchType().equals(LONG)) {
-      int qualityGateStatusMetricId = metricIdsByKey.get(ALERT_STATUS_KEY);
-      componentMeasures.stream().filter(m -> m.getMetricId() == qualityGateStatusMetricId).findAny()
-        .ifPresent(measure -> statusBuilder.setQualityGateStatus(measure.getData()));
-    }
-
-    if (branch.getBranchType().equals(SHORT)) {
-      int bugsMetricId = metricIdsByKey.get(BUGS_KEY);
-      componentMeasures.stream().filter(m -> m.getMetricId() == bugsMetricId).findAny()
-        .ifPresent(measure -> statusBuilder.setBugs(measure.getValue().intValue()));
-
-      int vulnerabilitiesMetricId = metricIdsByKey.get(VULNERABILITIES_KEY);
-      componentMeasures.stream().filter(m -> m.getMetricId() == vulnerabilitiesMetricId).findAny()
-        .ifPresent(measure -> statusBuilder.setVulnerabilities(measure.getValue().intValue()));
-
-      int codeSmellMetricId = metricIdsByKey.get(CODE_SMELLS_KEY);
-      componentMeasures.stream().filter(m -> m.getMetricId() == codeSmellMetricId).findAny()
-        .ifPresent(measure -> statusBuilder.setCodeSmells(measure.getValue().intValue()));
-    }
-
-    builder.setStatus(statusBuilder);
-    return ShowWsResponse.newBuilder().setBranch(builder).build();
-  }
-
 }
index 5193cd898d9b2f4fcab95ae9c06840e1c3e1b7a4..7aea4ab3e059ac999a88f8726326c363d0abe75d 100644 (file)
@@ -74,7 +74,7 @@ public class ListActionTest {
   private MetricDto vulnerabilities;
   private MetricDto codeSmells;
 
-  public WsActionTester tester = new WsActionTester(new ListAction(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), resourceTypes)));
+  public WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), resourceTypes)));
 
   @Before
   public void setUp() throws Exception {
@@ -86,7 +86,7 @@ public class ListActionTest {
 
   @Test
   public void test_definition() {
-    WebService.Action definition = tester.getDef();
+    WebService.Action definition = ws.getDef();
     assertThat(definition.key()).isEqualTo("list");
     assertThat(definition.isPost()).isFalse();
     assertThat(definition.isInternal()).isTrue();
@@ -99,7 +99,7 @@ public class ListActionTest {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("The 'project' parameter is missing");
 
-    tester.newRequest().execute();
+    ws.newRequest().execute();
   }
 
   @Test
@@ -111,7 +111,7 @@ public class ListActionTest {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Invalid project key");
 
-    tester.newRequest()
+    ws.newRequest()
       .setParam("project", file.getDbKey())
       .execute();
   }
@@ -121,7 +121,7 @@ public class ListActionTest {
     expectedException.expect(NotFoundException.class);
     expectedException.expectMessage("Component key 'foo' not found");
 
-    tester.newRequest()
+    ws.newRequest()
       .setParam("project", "foo")
       .execute();
   }
@@ -131,13 +131,28 @@ public class ListActionTest {
     ComponentDto project = db.components().insertMainBranch();
     userSession.logIn().addProjectPermission(UserRole.USER, project);
 
-    ListWsResponse response = tester.newRequest()
+    ListWsResponse response = ws.newRequest()
       .setParam("project", project.getDbKey())
       .executeProtobuf(ListWsResponse.class);
 
     assertThat(response.getBranchesList())
-      .extracting(Branch::hasName, Branch::getIsMain, Branch::getType)
-      .containsExactlyInAnyOrder(tuple(false, true, Common.BranchType.LONG));
+      .extracting(Branch::getName, Branch::getIsMain, Branch::getType)
+      .containsExactlyInAnyOrder(tuple("master", true, Common.BranchType.LONG));
+  }
+
+  @Test
+  public void main_branch_with_specified_name() {
+    OrganizationDto organization = db.organizations().insert();
+    ComponentDto project = db.components().insertMainBranch(organization, "head");
+    userSession.logIn().addProjectPermission(UserRole.USER, project);
+
+    ListWsResponse response = ws.newRequest()
+      .setParam("project", project.getDbKey())
+      .executeProtobuf(ListWsResponse.class);
+
+    assertThat(response.getBranchesList())
+      .extracting(Branch::getName, Branch::getIsMain, Branch::getType)
+      .containsExactlyInAnyOrder(tuple("head", true, Common.BranchType.LONG));
   }
 
   @Test
@@ -145,7 +160,7 @@ public class ListActionTest {
     ComponentDto project = db.components().insertPrivateProject();
     userSession.logIn().addProjectPermission(UserRole.USER, project);
 
-    String json = tester.newRequest()
+    String json = ws.newRequest()
       .setParam("project", project.getDbKey())
       .setMediaType(MediaTypes.JSON)
       .execute()
@@ -160,14 +175,14 @@ public class ListActionTest {
     db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
     userSession.logIn().addProjectPermission(UserRole.USER, project);
 
-    ListWsResponse response = tester.newRequest()
+    ListWsResponse response = ws.newRequest()
       .setParam("project", project.getDbKey())
       .executeProtobuf(ListWsResponse.class);
 
     assertThat(response.getBranchesList())
       .extracting(Branch::getName, Branch::getType)
       .containsExactlyInAnyOrder(
-        tuple("", Common.BranchType.LONG),
+        tuple("master", Common.BranchType.LONG),
         tuple("feature/foo", Common.BranchType.LONG),
         tuple("feature/bar", Common.BranchType.LONG));
   }
@@ -183,17 +198,33 @@ public class ListActionTest {
     ComponentDto shortLivingBranchOnMaster = db.components().insertProjectBranch(project,
       b -> b.setKey("short_on_master").setBranchType(BranchType.SHORT).setMergeBranchUuid(project.uuid()));
 
-    ListWsResponse response = tester.newRequest()
+    ListWsResponse response = ws.newRequest()
       .setParam("project", project.getKey())
       .executeProtobuf(ListWsResponse.class);
 
     assertThat(response.getBranchesList())
       .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch)
       .containsExactlyInAnyOrder(
-        tuple("", Common.BranchType.LONG, ""),
+        tuple("master", Common.BranchType.LONG, ""),
         tuple(longLivingBranch.getBranch(), Common.BranchType.LONG, ""),
         tuple(shortLivingBranch.getBranch(), Common.BranchType.SHORT, longLivingBranch.getBranch()),
-        tuple(shortLivingBranchOnMaster.getBranch(), Common.BranchType.SHORT, ""));
+        tuple(shortLivingBranchOnMaster.getBranch(), Common.BranchType.SHORT, "master"));
+  }
+
+  @Test
+  public void mergeBranch_is_using_default_main_name_when_main_branch_has_no_name() {
+    ComponentDto project = db.components().insertMainBranch();
+    userSession.logIn().addProjectPermission(UserRole.USER, project);
+    ComponentDto shortLivingBranch = db.components().insertProjectBranch(project,
+      b -> b.setKey("short").setBranchType(BranchType.SHORT).setMergeBranchUuid(project.uuid()));
+
+    WsBranches.ShowWsResponse response = ws.newRequest()
+      .setParam("project", shortLivingBranch.getKey())
+      .executeProtobuf(WsBranches.ShowWsResponse.class);
+
+    assertThat(response.getBranch())
+      .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch)
+      .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), Common.BranchType.SHORT, "master");
   }
 
   @Test
@@ -204,7 +235,7 @@ public class ListActionTest {
     SnapshotDto branchAnalysis = db.components().insertSnapshot(branch);
     db.measures().insertMeasure(branch, branchAnalysis, qualityGateStatus, m -> m.setData("OK"));
 
-    ListWsResponse response = tester.newRequest()
+    ListWsResponse response = ws.newRequest()
       .setParam("project", project.getKey())
       .executeProtobuf(ListWsResponse.class);
 
@@ -225,7 +256,7 @@ public class ListActionTest {
     db.measures().insertMeasure(shortLivingBranch, branchAnalysis, vulnerabilities, m -> m.setValue(2d));
     db.measures().insertMeasure(shortLivingBranch, branchAnalysis, codeSmells, m -> m.setValue(3d));
 
-    ListWsResponse response = tester.newRequest()
+    ListWsResponse response = ws.newRequest()
       .setParam("project", project.getKey())
       .executeProtobuf(ListWsResponse.class);
 
@@ -247,7 +278,7 @@ public class ListActionTest {
     expectedException.expect(NotFoundException.class);
     expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
 
-    tester.newRequest()
+    ws.newRequest()
       .setParam("project", branch.getDbKey())
       .execute();
   }
@@ -259,12 +290,12 @@ public class ListActionTest {
     db.components().insertProjectBranch(project, b -> b.setKey("feature/foo").setBranchType(BranchType.SHORT).setMergeBranchUuid(longLivingBranch.uuid()));
     userSession.logIn().addProjectPermission(UserRole.USER, project);
 
-    String json = tester.newRequest()
+    String json = ws.newRequest()
       .setParam("project", project.getDbKey())
       .execute()
       .getInput();
 
-    assertJson(json).isSimilarTo(tester.getDef().responseExampleAsString());
+    assertJson(json).isSimilarTo(ws.getDef().responseExampleAsString());
   }
 
 }
index 388377741eae155563e0b23c45b5d6f80d384c1f..05c6acc3dbdc7517f603a6ee0b369029ad31dc87 100644 (file)
@@ -101,8 +101,8 @@ public class ShowActionTest {
       .executeProtobuf(ShowWsResponse.class);
 
     assertThat(response.getBranch())
-      .extracting(Branch::hasName, Branch::getType, Branch::getIsMain, Branch::hasMergeBranch)
-      .containsExactlyInAnyOrder(false, Common.BranchType.LONG, true, false);
+      .extracting(Branch::getName, Branch::getType, Branch::getIsMain, Branch::hasMergeBranch)
+      .containsExactlyInAnyOrder("master", Common.BranchType.LONG, true, false);
   }
 
   @Test
@@ -134,8 +134,8 @@ public class ShowActionTest {
       .executeProtobuf(ShowWsResponse.class);
 
     assertThat(response.getBranch())
-      .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch)
-      .containsExactlyInAnyOrder(longLivingBranch.getBranch(), Common.BranchType.LONG, "");
+      .extracting(Branch::getName, Branch::getType, Branch::hasMergeBranch)
+      .containsExactlyInAnyOrder(longLivingBranch.getBranch(), Common.BranchType.LONG, false);
   }
 
   @Test
@@ -157,6 +157,23 @@ public class ShowActionTest {
       .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), Common.BranchType.SHORT, longLivingBranch.getBranch());
   }
 
+  @Test
+  public void mergeBranch_is_using_default_main_name_when_main_branch_has_no_name() {
+    ComponentDto project = db.components().insertMainBranch();
+    userSession.logIn().addProjectPermission(UserRole.USER, project);
+    ComponentDto shortLivingBranch = db.components().insertProjectBranch(project,
+      b -> b.setKey("short").setBranchType(BranchType.SHORT).setMergeBranchUuid(project.uuid()));
+
+    ShowWsResponse response = ws.newRequest()
+      .setParam("component", shortLivingBranch.getKey())
+      .setParam("branch", shortLivingBranch.getBranch())
+      .executeProtobuf(ShowWsResponse.class);
+
+    assertThat(response.getBranch())
+      .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch)
+      .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), Common.BranchType.SHORT, "master");
+  }
+
   @Test
   public void quality_gate_status_on_long_living_branch() {
     ComponentDto project = db.components().insertMainBranch();
@@ -211,8 +228,8 @@ public class ShowActionTest {
       .executeProtobuf(ShowWsResponse.class);
 
     assertThat(response.getBranch())
-      .extracting(Branch::getName, Branch::getType, Branch::getMergeBranch)
-      .containsExactlyInAnyOrder(file.getBranch(), Common.BranchType.LONG, "");
+      .extracting(Branch::getName, Branch::getType, Branch::hasMergeBranch)
+      .containsExactlyInAnyOrder(file.getBranch(), Common.BranchType.LONG, false);
   }
 
   @Test