From b17407e8979a340367238a8118af702687b9892b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 30 Aug 2017 15:46:03 +0200 Subject: [PATCH] SONAR-9616 Remove Optional from Branch#getName as it always set --- .../org/sonar/db/component/BranchDao.java | 3 +- .../org/sonar/db/component/BranchDto.java | 38 ++++--------------- .../org/sonar/db/component/BranchDaoTest.java | 18 +-------- .../settings/ProjectConfigurationFactory.java | 2 +- .../task/projectanalysis/analysis/Branch.java | 5 +-- .../PostProjectAnalysisTasksExecutor.java | 2 +- .../component/BranchPersister.java | 2 +- .../component/DefaultBranchImpl.java | 4 +- .../step/BuildComponentTreeStep.java | 2 +- .../step/QualityGateEventsStep.java | 2 +- .../step/SendIssueNotificationsStep.java | 2 +- .../ProjectConfigurationFactoryTest.java | 34 ++++++----------- .../AnalysisMetadataHolderImplTest.java | 2 +- .../PostProjectAnalysisTasksExecutorTest.java | 4 +- .../component/BranchLoaderTest.java | 6 +-- .../component/BranchPersisterTest.java | 21 +++++----- .../ConfigurationRepositoryTest.java | 3 +- .../component/DefaultBranchImplTest.java | 6 +-- .../step/BuildComponentTreeStepTest.java | 4 +- .../step/ReportPersistComponentsStepTest.java | 6 +-- 20 files changed, 54 insertions(+), 112 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java index 2e4fcb111d4..ec66130acdd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java @@ -55,8 +55,7 @@ public class BranchDao implements Dao { } public Optional selectByKey(DbSession dbSession, String projectUuid, BranchKeyType keyType, @Nullable String key) { - String keyInDb = BranchDto.convertKeyToDb(key); - return Optional.ofNullable(mapper(dbSession).selectByKey(projectUuid, keyType, keyInDb)); + return Optional.ofNullable(mapper(dbSession).selectByKey(projectUuid, keyType, key)); } public Collection selectByComponent(DbSession dbSession, ComponentDto component) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java index e756bd04b88..655260116e4 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java @@ -19,11 +19,9 @@ */ package org.sonar.db.component; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import static com.google.common.base.Preconditions.checkArgument; -import static org.apache.commons.lang.StringUtils.repeat; public class BranchDto { public static final String DEFAULT_MAIN_BRANCH_NAME = "master"; @@ -33,14 +31,6 @@ public class BranchDto { */ public static final int KEE_MAX_LENGTH = 255; - /** - * Value of {@link #kee} when the name of main branch is not known. - * Used only if {@link #keeType} is {@link BranchKeyType#BRANCH}. - * It does not conflict with names of real branches because the term ':BRANCH:' - * is not accepted. - */ - public static final String NULL_KEY = repeat("_", KEE_MAX_LENGTH); - /** * Branch UUID is the projects.uuid that reference projects, branches or pull requests * (projects.qualifier="TRK"). @@ -64,7 +54,7 @@ public class BranchDto { /** * If {@link #keeType} is {@link BranchKeyType#BRANCH}, then name of branch, for example - * "feature/foo". Can be {@link #NULL_KEY} is the name is not known. + * "feature/foo". * * If {@link #keeType} is {@link BranchKeyType#PR}, then id of the pull request, for * example "1204". @@ -126,30 +116,26 @@ public class BranchDto { } /** - * This is the getter used by MyBatis mapper. It does - * not handle the special value used to map null field. + * This is the getter used by MyBatis mapper. */ private String getKee() { return kee; } - @CheckForNull public String getKey() { - return convertKeyFromDb(getKee()); + return kee; } /** - * This is the setter used by MyBatis mapper. It does - * not handle the special value used to map null field. + * This is the setter used by MyBatis mapper. */ private void setKee(String s) { this.kee = s; } - public BranchDto setKey(@Nullable String s) { - checkArgument(s == null || s.length() <= KEE_MAX_LENGTH, "Maximum length of branch name or pull request id is %s: %s", KEE_MAX_LENGTH, s); - checkArgument(!NULL_KEY.equals(s), "Branch name is not allowed: %s", s); - setKee(convertKeyToDb(s)); + public BranchDto setKey(String s) { + checkArgument(s.length() <= KEE_MAX_LENGTH, "Maximum length of branch name or pull request id is %s: %s", KEE_MAX_LENGTH, s); + setKee(s); return this; } @@ -197,14 +183,4 @@ public class BranchDto { sb.append('}'); return sb.toString(); } - - static String convertKeyToDb(@Nullable String s) { - return s == null ? NULL_KEY : s; - } - - @CheckForNull - static String convertKeyFromDb(String s) { - return NULL_KEY.equals(s) ? null : s; - } - } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java index 43b6f6aa14b..ab5049dca3f 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java @@ -79,7 +79,7 @@ public class BranchDaoTest { dto.setUuid("U1"); dto.setBranchType(BranchType.LONG); dto.setKeeType(BranchKeyType.BRANCH); - dto.setKey(null); + dto.setKey("feature"); underTest.insert(dbSession, dto); BranchDto dto2 = new BranchDto(); @@ -120,22 +120,6 @@ public class BranchDaoTest { assertThat((String) map.get("pullRequestTitle")).contains("e").isEqualTo(dto.getPullRequestTitle()); } - @Test - public void null_value_of_kee_column_is_converted_in_db() { - BranchDto dto = new BranchDto(); - dto.setProjectUuid("u1"); - dto.setUuid("u2"); - dto.setKeeType(BranchKeyType.BRANCH); - dto.setKey(null); - - underTest.insert(dbSession, dto); - - Map map = db.selectFirst(dbSession, SELECT_FROM + " where uuid='" + dto.getUuid() + "'"); - assertThat(map).contains(entry("kee", BranchDto.NULL_KEY)); - BranchDto loaded = underTest.selectByKey(dbSession, dto.getProjectUuid(), dto.getKeeType(), null).get(); - assertThat(loaded.getKey()).isNull(); - } - @Test public void upsert() { BranchDto dto = new BranchDto(); diff --git a/server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java b/server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java index 8ba4009542c..16f6506ed77 100644 --- a/server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java +++ b/server/sonar-server/src/main/java/org/sonar/ce/settings/ProjectConfigurationFactory.java @@ -60,7 +60,7 @@ public class ProjectConfigurationFactory { } Branch branch = branchOpt.get(); if (!branch.isLegacyFeature() && !branch.isMain()) { - return branch.getName(); + return Optional.of(branch.getName()); } return Optional.empty(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java index 352dfa1ee18..7cbc50bac6a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/Branch.java @@ -38,10 +38,9 @@ public interface Branch extends ComponentKeyGenerator { boolean isLegacyFeature(); /** - * Name can be empty when it's not known on the main branch - * (regular analysis without the branch parameters) + * Name of the branch */ - Optional getName(); + String getName(); /** * For short living branches, indicates the branch from which it was forked. diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java index 254547e3063..08f532c8cd7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java @@ -158,7 +158,7 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor java.util.Optional analysisBranchOpt = analysisMetadataHolder.getBranch(); if (analysisBranchOpt.isPresent() && !analysisBranchOpt.get().isLegacyFeature()) { org.sonar.server.computation.task.projectanalysis.analysis.Branch branch = analysisBranchOpt.get(); - return new BranchImpl(branch.isMain(), branch.getName().orElse(null), Branch.Type.valueOf(branch.getType().name())); + return new BranchImpl(branch.isMain(), branch.getName(), Branch.Type.valueOf(branch.getType().name())); } return null; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java index 00ec46e39b6..48f1435d5b8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersister.java @@ -88,7 +88,7 @@ public class BranchPersister { // MainBranchProjectUuid will be null if it's a main branch dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid())); dto.setKeeType(BranchKeyType.BRANCH); - dto.setKey(branch.getName().orElse(null)); + dto.setKey(branch.getName()); dto.setBranchType(branch.getType()); // merge branch is only present if it's a short living branch dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null)); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java index 42bb0f1fdd2..3f4461ba006 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImpl.java @@ -76,8 +76,8 @@ public class DefaultBranchImpl implements Branch { } @Override - public Optional getName() { - return Optional.ofNullable(branchName); + public String getName() { + return branchName; } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java index 6a029818e4f..425cfb9de3a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStep.java @@ -107,7 +107,7 @@ public class BuildComponentTreeStep implements ComputationStep { return new DefaultBranchImpl(); } return branch.filter(Branch::isLegacyFeature) - .map(b -> new DefaultBranchImpl(b.getName().orElse(null))) + .map(b -> new DefaultBranchImpl(b.getName())) .orElseGet(DefaultBranchImpl::new); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java index 6de9d37adf4..64310f4b8f6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/QualityGateEventsStep.java @@ -132,7 +132,7 @@ public class QualityGateEventsStep implements ComputationStep { .setFieldValue("alertText", rawStatus.getText()) .setFieldValue("alertLevel", rawStatus.getStatus().toString()) .setFieldValue("isNewAlert", Boolean.toString(isNewAlert)); - analysisMetadataHolder.getBranch().ifPresent(branch -> notification.setFieldValue("branch", branch.getName().orElse(null))); + analysisMetadataHolder.getBranch().ifPresent(branch -> notification.setFieldValue("branch", branch.getName())); notificationService.deliver(notification); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java index b0e752ec7d4..fbfb35060ee 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/SendIssueNotificationsStep.java @@ -145,7 +145,7 @@ public class SendIssueNotificationsStep implements ComputationStep { } private String getBranchName() { - return analysisMetadataHolder.getBranch().filter(b -> !b.isMain()).flatMap(Branch::getName).orElse(null); + return analysisMetadataHolder.getBranch().filter(b -> !b.isMain()).map(Branch::getName).orElse(null); } private String getMainBranchProjectKey() { diff --git a/server/sonar-server/src/test/java/org/sonar/ce/settings/ProjectConfigurationFactoryTest.java b/server/sonar-server/src/test/java/org/sonar/ce/settings/ProjectConfigurationFactoryTest.java index b2b5bc52559..1388284bec0 100644 --- a/server/sonar-server/src/test/java/org/sonar/ce/settings/ProjectConfigurationFactoryTest.java +++ b/server/sonar-server/src/test/java/org/sonar/ce/settings/ProjectConfigurationFactoryTest.java @@ -84,7 +84,7 @@ public class ProjectConfigurationFactoryTest { ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1")); - Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch()))); + Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch(), false))); assertThat(config.get("sonar.leak.period")).hasValue("1"); } @@ -96,7 +96,7 @@ public class ProjectConfigurationFactoryTest { ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1")); - Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch()))); + Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch(), false))); assertThat(config.get("global")).hasValue("global_value"); assertThat(config.get("sonar.leak.period")).hasValue("1"); @@ -109,7 +109,7 @@ public class ProjectConfigurationFactoryTest { ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1")); - Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch()))); + Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch(), false))); assertThat(config.get("key")).hasValue("value"); assertThat(config.get("sonar.leak.period")).hasValue("1"); @@ -122,7 +122,7 @@ public class ProjectConfigurationFactoryTest { ComponentDto branch = db.components().insertProjectBranch(project); db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("2")); - Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch()))); + Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getBranch(), false))); assertThat(config.get("sonar.leak.period")).hasValue("2"); } @@ -131,10 +131,10 @@ public class ProjectConfigurationFactoryTest { public void main_branch() { ComponentDto project = db.components().insertMainBranch(); db.properties().insertProperties(newComponentPropertyDto(project).setKey("sonar.leak.period").setValue("1")); - Branch branch = createBranch(project.getBranch()); + Branch branch = createBranch("master", true); when(branch.isMain()).thenReturn(true); - Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(project.getBranch()))); + Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getName(), true))); assertThat(config.get("sonar.leak.period")).hasValue("1"); } @@ -143,30 +143,18 @@ public class ProjectConfigurationFactoryTest { public void legacy_branch() { ComponentDto project = db.components().insertMainBranch(); db.properties().insertProperties(newComponentPropertyDto(project).setKey("sonar.leak.period").setValue("1")); - Branch branch = createBranch(project.getBranch()); + Branch branch = createBranch("legacy", true); when(branch.isLegacyFeature()).thenReturn(true); - Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(project.getBranch()))); + Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(branch.getName(), true))); assertThat(config.get("sonar.leak.period")).hasValue("1"); } - @Test - public void empty_branch_name() { - ComponentDto project = db.components().insertMainBranch(); - db.properties().insertProperties(newComponentPropertyDto(project).setKey("sonar.leak.period").setValue("1")); - Branch branch = mock(Branch.class); - when(branch.getName()).thenReturn(Optional.empty()); - - Configuration config = underTest.newProjectConfiguration(project.getKey(), Optional.of(createBranch(project.getBranch()))); - - assertThat(config.get("sonar.leak.period")).hasValue("1"); - } - - private static Branch createBranch(String name) { + private static Branch createBranch(String name, boolean isMain) { Branch branch = mock(Branch.class); - when(branch.getName()).thenReturn(Optional.ofNullable(name)); - when(branch.isMain()).thenReturn(false); + when(branch.getName()).thenReturn(name); + when(branch.isMain()).thenReturn(isMain); return branch; } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java index b3f2b9a0236..d27897d7523 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java @@ -266,7 +266,7 @@ public class AnalysisMetadataHolderImplTest { underTest.setBranch(new DefaultBranchImpl("master")); - assertThat(underTest.getBranch().get().getName()).hasValue("master"); + assertThat(underTest.getBranch().get().getName()).isEqualTo("master"); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java index 4e96fd24484..e0f2d7c8a6d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java @@ -254,8 +254,8 @@ public class PostProjectAnalysisTasksExecutorTest { } @Override - public Optional getName() { - return Optional.of("feature/foo"); + public String getName() { + return "feature/foo"; } @Override diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java index 1eb210257a9..3ed00537c4d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java @@ -62,7 +62,7 @@ public class BranchLoaderTest { Branch branch = metadataHolder.getBranch().get(); assertThat(branch.isMain()).isTrue(); - assertThat(branch.getName().get()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME); + assertThat(branch.getName()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME); } @Test @@ -77,7 +77,7 @@ public class BranchLoaderTest { Branch branch = metadataHolder.getBranch().get(); assertThat(branch.isMain()).isTrue(); - assertThat(branch.getName()).hasValue("foo"); + assertThat(branch.getName()).isEqualTo("foo"); } @Test @@ -93,7 +93,7 @@ public class BranchLoaderTest { Branch branch = metadataHolder.getBranch().get(); assertThat(branch.isMain()).isTrue(); - assertThat(branch.getName()).hasValue("foo"); + assertThat(branch.getName()).isEqualTo("foo"); } private class FakeDelegate implements BranchLoaderDelegate { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java index 90c192496ee..46e56b62973 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchPersisterTest.java @@ -19,14 +19,7 @@ */ package org.sonar.server.computation.task.projectanalysis.component; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT; -import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder; - import java.util.Optional; -import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -38,8 +31,12 @@ import org.sonar.db.component.ComponentTesting; import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule; import org.sonar.server.computation.task.projectanalysis.analysis.Branch; import org.sonar.server.computation.task.projectanalysis.analysis.Project; -import org.sonar.server.computation.task.projectanalysis.component.Component; -import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT; +import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder; public class BranchPersisterTest { private final static Component MAIN = builder(PROJECT, 1).setUuid("PROJECT_UUID").setKey("PROJECT_KEY").build(); @@ -58,7 +55,7 @@ public class BranchPersisterTest { @Test public void fail_if_no_component_for_main_branches() { - analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, true, null)); + analysisMetadataHolder.setBranch(createBranch(BranchType.LONG, true, "master")); treeRootHolder.setRoot(MAIN); exception.expect(IllegalStateException.class); @@ -94,10 +91,10 @@ public class BranchPersisterTest { } - private static Branch createBranch(BranchType type, boolean isMain, @Nullable String name) { + private static Branch createBranch(BranchType type, boolean isMain, String name) { Branch branch = mock(Branch.class); when(branch.getType()).thenReturn(type); - when(branch.getName()).thenReturn(Optional.ofNullable(name)); + when(branch.getName()).thenReturn(name); when(branch.isMain()).thenReturn(isMain); when(branch.getMergeBranchUuid()).thenReturn(Optional.empty()); return branch; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ConfigurationRepositoryTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ConfigurationRepositoryTest.java index c86aa2e4d60..139407dd6f5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ConfigurationRepositoryTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/ConfigurationRepositoryTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.computation.task.projectanalysis.component; -import java.util.Optional; import org.junit.Rule; import org.junit.Test; import org.sonar.api.config.Configuration; @@ -114,7 +113,7 @@ public class ConfigurationRepositoryTest { ComponentDto project = db.components().insertMainBranch(); ComponentDto branchDto = db.components().insertProjectBranch(project); Branch branch = mock(Branch.class); - when(branch.getName()).thenReturn(Optional.of(branchDto.getBranch())); + when(branch.getName()).thenReturn(branchDto.getBranch()); analysisMetadataHolder.setProject(Project.copyOf(project)).setBranch(branch); globalSettings.setProperty("global", "global value"); insertProjectProperty(project, "project", "project value"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImplTest.java index 31d72a3f1f8..67f7385140e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/DefaultBranchImplTest.java @@ -56,7 +56,7 @@ public class DefaultBranchImplTest { assertThat(branch.isMain()).isTrue(); assertThat(branch.getType()).isEqualTo(BranchType.LONG); - assertThat(branch.getName().get()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME); + assertThat(branch.getName()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME); assertThat(branch.supportsCrossProjectCpd()).isTrue(); assertThat(branch.generateKey(PROJECT, null)).isEqualTo("P"); @@ -71,7 +71,7 @@ public class DefaultBranchImplTest { // not a real branch. Parameter sonar.branch forks project. assertThat(branch.isMain()).isTrue(); assertThat(branch.getType()).isEqualTo(BranchType.LONG); - assertThat(branch.getName()).hasValue("bar"); + assertThat(branch.getName()).isEqualTo("bar"); assertThat(branch.supportsCrossProjectCpd()).isFalse(); assertThat(branch.generateKey(PROJECT, null)).isEqualTo("P:bar"); @@ -81,7 +81,7 @@ public class DefaultBranchImplTest { private void assertThatNameIsCorrect(@Nullable String name) { DefaultBranchImpl branch = new DefaultBranchImpl(name); - assertThat(branch.getName()).hasValue(name); + assertThat(branch.getName()).isEqualTo(name); } private void assertThatNameIsNotCorrect(String name) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStepTest.java index dc330355cd2..11ad345a608 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/BuildComponentTreeStepTest.java @@ -205,7 +205,7 @@ public class BuildComponentTreeStepTest { @Test public void generate_keys_when_using_branch() { Branch branch = mock(Branch.class); - when(branch.getName()).thenReturn(Optional.of("origin/feature")); + when(branch.getName()).thenReturn("origin/feature"); when(branch.isMain()).thenReturn(false); when(branch.isLegacyFeature()).thenReturn(false); when(branch.generateKey(any(ScannerReport.Component.class), any(ScannerReport.Component.class))).thenReturn("generated"); @@ -230,7 +230,7 @@ public class BuildComponentTreeStepTest { @Test public void generate_keys_when_using_main_branch() { Branch branch = mock(Branch.class); - when(branch.getName()).thenReturn(Optional.of("origin/master")); + when(branch.getName()).thenReturn("origin/master"); when(branch.isMain()).thenReturn(true); when(branch.isLegacyFeature()).thenReturn(false); when(branch.generateKey(any(ScannerReport.Component.class), any(ScannerReport.Component.class))).thenReturn("generated"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java index 39e30067fd6..c03e8826dc9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/ReportPersistComponentsStepTest.java @@ -42,8 +42,8 @@ import org.sonar.server.computation.task.projectanalysis.analysis.Branch; import org.sonar.server.computation.task.projectanalysis.analysis.Project; import org.sonar.server.computation.task.projectanalysis.component.BranchPersister; import org.sonar.server.computation.task.projectanalysis.component.Component; -import org.sonar.server.computation.task.projectanalysis.component.FileAttributes; import org.sonar.server.computation.task.projectanalysis.component.DefaultBranchImpl; +import org.sonar.server.computation.task.projectanalysis.component.FileAttributes; import org.sonar.server.computation.task.projectanalysis.component.MutableDbIdsRepositoryRule; import org.sonar.server.computation.task.projectanalysis.component.MutableDisabledComponentsHolder; import org.sonar.server.computation.task.projectanalysis.component.ReportComponent; @@ -933,8 +933,8 @@ public class ReportPersistComponentsStepTest extends BaseStepTest { } @Override - public java.util.Optional getName() { - return java.util.Optional.ofNullable(name); + public String getName() { + return name; } @Override -- 2.39.5