Skip to content

Commit

Permalink
SONAR-12364 Fix update of values
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses authored and SonarTech committed Sep 24, 2019
1 parent 09f8f0f commit 14bd90f
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void do_not_fail_when_project_has_no_BranchDto(String leakPeriodSettingVa
public void fail_with_ISE_when_manual_baseline_is_set_but_does_not_exist_in_DB(String leakPeriodSettingValue) {
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto project = dbTester.components().insertMainBranch(organization);
dbTester.components().setManualBaseline(project, new SnapshotDto().setUuid("nonexistent"));
dbTester.components().setSpecificAnalysisNewCodePeriod(project, new SnapshotDto().setUuid("nonexistent"));
setBranchOf(project);
setupRoot(project);

Expand All @@ -281,7 +281,7 @@ public void fail_with_ISE_when_manual_baseline_is_set_but_does_not_belong_to_cur
ComponentDto project = dbTester.components().insertMainBranch(organization);
ComponentDto otherProject = dbTester.components().insertMainBranch(organization);
SnapshotDto otherProjectAnalysis = dbTester.components().insertSnapshot(otherProject);
dbTester.components().setManualBaseline(project, otherProjectAnalysis);
dbTester.components().setSpecificAnalysisNewCodePeriod(project, otherProjectAnalysis);
setBranchOf(project);
setupRoot(project);

Expand All @@ -305,7 +305,7 @@ public void feed_period_by_manual_baseline_ignores_leak_period_setting(String le
when(system2Mock.now()).thenReturn(november30th2008.getTime());
when(analysisMetadataHolder.getAnalysisDate()).thenReturn(november30th2008.getTime());
when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false);
dbTester.components().setManualBaseline(project, manualBaselineAnalysis);
dbTester.components().setSpecificAnalysisNewCodePeriod(project, manualBaselineAnalysis);
setBranchOf(project);
setupRoot(project);

Expand All @@ -330,7 +330,7 @@ public void feed_period_by_manual_baseline_on_long_living_branch(String leakPeri
when(system2Mock.now()).thenReturn(november30th2008.getTime());
when(analysisMetadataHolder.getAnalysisDate()).thenReturn(november30th2008.getTime());
when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false);
dbTester.components().setManualBaseline(branch, manualBaselineAnalysis);
dbTester.components().setSpecificAnalysisNewCodePeriod(branch, manualBaselineAnalysis);
setBranchOf(branch);
setupRoot(branch);

Expand Down Expand Up @@ -361,7 +361,7 @@ public void feed_period_parameter_as_null_when_manual_baseline_has_no_version()
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto project = dbTester.components().insertMainBranch(organization);
SnapshotDto manualBaselineAnalysis = dbTester.components().insertSnapshot(project);
dbTester.components().setManualBaseline(project, manualBaselineAnalysis);
dbTester.components().setSpecificAnalysisNewCodePeriod(project, manualBaselineAnalysis);
setBranchOf(project);
setupRoot(project);

Expand All @@ -377,7 +377,7 @@ public void feed_period_parameter_as_null_when_manual_baseline_has_same_project_
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto project = dbTester.components().insertMainBranch(organization);
SnapshotDto manualBaselineAnalysis = dbTester.components().insertSnapshot(project, t -> t.setProjectVersion(version).setProjectVersion(version));
dbTester.components().setManualBaseline(project, manualBaselineAnalysis);
dbTester.components().setSpecificAnalysisNewCodePeriod(project, manualBaselineAnalysis);
setBranchOf(project);
setupRoot(project);

Expand All @@ -393,7 +393,7 @@ public void feed_no_period_parameter_as_projectVersion_when_manual_baseline_has_
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto project = dbTester.components().insertMainBranch(organization);
SnapshotDto manualBaselineAnalysis = dbTester.components().insertSnapshot(project, t -> t.setProjectVersion(projectVersion));
dbTester.components().setManualBaseline(project, manualBaselineAnalysis);
dbTester.components().setSpecificAnalysisNewCodePeriod(project, manualBaselineAnalysis);
setBranchOf(project);
setupRoot(project);

Expand All @@ -411,7 +411,7 @@ public void feed_no_period_parameter_as_version_event_version_when_manual_baseli
ComponentDto project = dbTester.components().insertMainBranch(organization);
SnapshotDto manualBaselineAnalysis = dbTester.components().insertSnapshot(project, t -> t.setProjectVersion(projectVersion));
dbTester.events().insertEvent(EventTesting.newEvent(manualBaselineAnalysis).setCategory(CATEGORY_VERSION).setName(eventVersion));
dbTester.components().setManualBaseline(project, manualBaselineAnalysis);
dbTester.components().setSpecificAnalysisNewCodePeriod(project, manualBaselineAnalysis);
setBranchOf(project);
setupRoot(project);

Expand Down Expand Up @@ -829,7 +829,7 @@ private void setupRoot(ComponentDto project, String version) {
}

private void setupBranchWithNoManualBaseline(ComponentDto projectOrLongBranch) {
dbTester.components().unsetManualBaseline(projectOrLongBranch);
dbTester.components().unsetBranchNewCodePeriod(projectOrLongBranch);
setBranchOf(projectOrLongBranch);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void upsert(DbSession dbSession, NewCodePeriodDto dto) {
NewCodePeriodMapper mapper = mapper(dbSession);
long currentTime = system2.now();
dto.setUpdatedAt(currentTime);
if (updateInternal(dbSession, dto) == 0) {
if (mapper.update(dto) == 0) {
dto.setCreatedAt(currentTime);
dto.setUuid(uuidFactory.create());
mapper.insert(dto);
Expand All @@ -66,12 +66,7 @@ public void upsert(DbSession dbSession, NewCodePeriodDto dto) {

public void update(DbSession dbSession, NewCodePeriodDto dto) {
requireNonNull(dto.getUuid(), "Uuid of NewCodePeriod must be specified.");
updateInternal(dbSession, dto);
}

private int updateInternal(DbSession dbSession, NewCodePeriodDto dto) {
requireNonNull(dto.getType(), "Type of NewCodePeriod must be specified.");
return mapper(dbSession).update(dto.setUpdatedAt(system2.now()));
mapper(dbSession).update(dto.setUpdatedAt(system2.now()));
}

public Optional<NewCodePeriodDto> selectByProject(DbSession dbSession, String projectUuid) {
Expand All @@ -90,9 +85,10 @@ public boolean existsByProjectAnalysisUuid(DbSession dbSession, String projectAn
return mapper(dbSession).countByProjectAnalysis(projectAnalysisUuid) > 0;
}

public void deleteByProjectUuidAndBranchUuid(DbSession dbSession, String projectUuid, String branchUuid) {
public void deleteByBranch(DbSession dbSession, String projectUuid, String branchUuid) {
requireNonNull(projectUuid, "Project uuid must be specified.");
mapper(dbSession).deleteByProjectAndBranch(projectUuid, branchUuid);
requireNonNull(branchUuid, "Branch uuid must be specified.");
mapper(dbSession).deleteByBranch(projectUuid, branchUuid);
}

private static NewCodePeriodMapper mapper(DbSession session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface NewCodePeriodMapper {

NewCodePeriodDto selectByProject(String projectUuid);

void deleteByProjectAndBranch(@Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);
void deleteByBranch(@Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);

NewCodePeriodDto selectByBranch(@Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private ManualBaselineAnalysisFilter(PurgeMapper mapper, String componentUuid) {
@Override
public boolean test(PurgeableAnalysisDto purgeableAnalysisDto) {
if (manualBaselineAnalysisUuid == null) {
manualBaselineAnalysisUuid = Optional.ofNullable(mapper.selectManualBaseline(componentUuid))
manualBaselineAnalysisUuid = Optional.ofNullable(mapper.selectSpecificAnalysisNewCodePeriod(componentUuid))
.map(t -> new String[] {t})
.orElse(NO_BASELINE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public interface PurgeMapper {
List<String> selectStaleShortLivingBranchesAndPullRequests(@Param("projectUuid") String projectUuid, @Param("toDate") Long toDate);

@CheckForNull
String selectManualBaseline(@Param("projectUuid") String projectUuid);
String selectSpecificAnalysisNewCodePeriod(@Param("projectUuid") String projectUuid);

List<IdUuidPair> selectDisabledComponentsWithoutIssues(@Param("projectUuid") String projectUuid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,23 @@
value=#{value, jdbcType=VARCHAR},
updated_at=#{updatedAt, jdbcType=TIMESTAMP}
WHERE
uuid=#{uuid, jdbcType=VARCHAR}
OR
(project_uuid=#{projectUuid, jdbcType=VARCHAR}
<choose>
<when test="projectUuid != null">
project_uuid=#{projectUuid, jdbcType=VARCHAR}
</when>
<otherwise>
project_uuid IS NULL
</otherwise>
</choose>
AND
branch_uuid=#{branchUuid, jdbcType=VARCHAR})
<choose>
<when test="branchUuid != null">
branch_uuid=#{branchUuid, jdbcType=VARCHAR}
</when>
<otherwise>
branch_uuid IS NULL
</otherwise>
</choose>
</update>

<select id="selectByProject" parameterType="map" resultType="org.sonar.db.newcodeperiod.NewCodePeriodDto">
Expand All @@ -76,7 +88,7 @@
AND ncp.branch_uuid=#{branchUuid, jdbcType=VARCHAR}
</select>

<update id="deleteByProjectAndBranch" parameterType="map">
<update id="deleteByBranch" parameterType="map">
DELETE
FROM new_code_periods
WHERE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
and not exists(select e.id from events e where e.analysis_uuid=s.uuid)
</select>

<select id="selectManualBaseline" parameterType="String" resultType="String">
<select id="selectSpecificAnalysisNewCodePeriod" parameterType="String" resultType="String">
select
value
from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public void update_with_upsert() {

underTest.upsert(dbSession, new NewCodePeriodDto()
.setUuid(NEW_CODE_PERIOD_UUID)
.setProjectUuid("proj-uuid")
.setBranchUuid("branch-uuid")
.setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
.setValue("analysis-uuid"));

Expand Down Expand Up @@ -262,7 +264,7 @@ public void delete_by_project_uuid_and_branch_uuid() {
.setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
.setValue("analysis-uuid"));

underTest.deleteByProjectUuidAndBranchUuid(dbSession, "proj-uuid", "branch-uuid");
underTest.deleteByBranch(dbSession, "proj-uuid", "branch-uuid");
db.commit();
assertNewCodePeriodRowCount(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public void deleteAnalyses_deletes_rows_in_events_and_event_component_changes()

@Test
public void selectPurgeableAnalyses() {
SnapshotDto[] analyses = new SnapshotDto[]{
SnapshotDto[] analyses = new SnapshotDto[] {
newSnapshot()
.setUuid("u1")
.setComponentUuid(PROJECT_UUID)
Expand Down Expand Up @@ -1597,7 +1597,7 @@ private boolean scannerContextExists(String uuid) {

@SafeVarargs
private final void insertCeActivityAndChildDataWithDate(String ceActivityUuid, LocalDateTime dateTime,
Consumer<CeQueueDto>... queueDtoConsumers) {
Consumer<CeQueueDto>... queueDtoConsumers) {
long date = dateTime.toInstant(UTC).toEpochMilli();
CeQueueDto queueDto = new CeQueueDto();
queueDto.setUuid(ceActivityUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,27 +300,28 @@ public final ComponentDto insertProjectBranch(ComponentDto project, BranchDto br
return branch;
}

public final void setManualBaseline(ComponentDto longOrMainBranchOfProject, SnapshotDto analysis) {
public final void setSpecificAnalysisNewCodePeriod(ComponentDto longOrMainBranchOfProject, SnapshotDto analysis) {
checkArgument(longOrMainBranchOfProject.isRoot());

BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(dbSession, longOrMainBranchOfProject.uuid())
.orElseThrow(() -> new IllegalArgumentException("BranchDto not found for component " + longOrMainBranchOfProject));
checkArgument(branchDto.getBranchType() == LONG, "must be a main or a Long Living branch");

db.getDbClient().newCodePeriodDao().insert(dbSession, new NewCodePeriodDto()
.setProjectUuid(longOrMainBranchOfProject.uuid())
.setProjectUuid(branchDto.getProjectUuid())
.setBranchUuid(branchDto.getUuid())
.setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
.setValue(analysis.getUuid()));
db.commit();
}

public final void unsetManualBaseline(ComponentDto longOrMainBranchOfProject) {
public final void unsetBranchNewCodePeriod(ComponentDto longOrMainBranchOfProject) {
checkArgument(longOrMainBranchOfProject.isRoot());

BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(dbSession, longOrMainBranchOfProject.uuid())
.orElseThrow(() -> new IllegalArgumentException("BranchDto not found for component " + longOrMainBranchOfProject));
checkArgument(branchDto.getBranchType() == LONG, "must be a main or a Long Living branch");
db.getDbClient().newCodePeriodDao().deleteByProjectUuidAndBranchUuid(dbSession, longOrMainBranchOfProject.uuid(), null);
db.getDbClient().newCodePeriodDao().deleteByBranch(dbSession, branchDto.getProjectUuid(), branchDto.getUuid());
db.commit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void define(WebService.NewController context) {
"<li>'Administer' rights on the specified component</li>" +
"</ul>")
.setSince("8.0")
.setResponseExample(getClass().getResource("generate_secret_key-example.json"))
.setResponseExample(getClass().getResource("show_new_code_period-example.json"))
.setHandler(this);

action.createParam(PARAM_PROJECT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void throw_IAE_if_branch_is_a_SLB() {
}

@Test
public void throw_NFE_if_no_project_permission() {
public void throw_FE_if_no_project_permission() {
ComponentDto project = componentDb.insertMainBranch();
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,24 @@ public void set_project_period_to_number_of_days() {
.setParam("value", "5")
.execute();
assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "5");
}

@Test
public void set_project_twice_period_to_number_of_days() {
ComponentDto project = componentDb.insertMainBranch();
logInAsProjectAdministrator(project);
ws.newRequest()
.setParam("project", project.getKey())
.setParam("type", "previous_version")
.execute();
assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.PREVIOUS_VERSION, null);

ws.newRequest()
.setParam("project", project.getKey())
.setParam("type", "number_of_days")
.setParam("value", "5")
.execute();
assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "5");
}

@Test
Expand All @@ -348,6 +365,32 @@ public void set_branch_period_to_analysis() {
assertTableContainsOnly(project.uuid(), branch.uuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, analysisBranch.getUuid());
}

@Test
public void set_branch_period_twice_to_analysis() {
ComponentDto project = componentDb.insertMainBranch();
ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch"));

SnapshotDto analysisMaster = db.components().insertSnapshot(project);
SnapshotDto analysisBranch = db.components().insertSnapshot(branch);

logInAsProjectAdministrator(project);

ws.newRequest()
.setParam("project", project.getKey())
.setParam("type", "specific_analysis")
.setParam("branch", "branch")
.setParam("value", analysisBranch.getUuid())
.execute();

ws.newRequest()
.setParam("project", project.getKey())
.setParam("type", "previous_version")
.setParam("branch", "branch")
.execute();

assertTableContainsOnly(project.uuid(), branch.uuid(), NewCodePeriodType.PREVIOUS_VERSION, null);
}

private void assertTableContainsOnly(@Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
assertThat(db.countRowsOfTable(dbSession, "new_code_periods")).isEqualTo(1);
assertThat(db.selectFirst(dbSession, "select project_uuid, branch_uuid, type, value from new_code_periods"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void handle(Request request, Response response) throws Exception {
userSession.checkComponentUuidPermission(UserRole.ADMIN, analysis.getComponentUuid());

checkArgument(!analysis.getLast(), "The last analysis '%s' cannot be deleted", analysisUuid);
checkNotBaseline(dbSession, analysis);
checkNotUsedInNewCodePeriod(dbSession, analysis);

analysis.setStatus(STATUS_UNPROCESSED);
dbClient.snapshotDao().update(dbSession, analysis);
Expand All @@ -85,10 +85,10 @@ public void handle(Request request, Response response) throws Exception {
response.noContent();
}

private void checkNotBaseline(DbSession dbSession, SnapshotDto analysis) {
private void checkNotUsedInNewCodePeriod(DbSession dbSession, SnapshotDto analysis) {
boolean isSetAsBaseline = dbClient.newCodePeriodDao().existsByProjectAnalysisUuid(dbSession, analysis.getUuid());
checkArgument(!isSetAsBaseline,
"The analysis '%s' can not be deleted because it is set as a manual new code period baseline", analysis.getUuid());
"The analysis '%s' can not be deleted because it is set as a new code period baseline", analysis.getUuid());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void doHandle(Request request) {

String projectUuid = projectBranch.getMainBranchProjectUuid() != null ? projectBranch.getMainBranchProjectUuid() : projectBranch.uuid();
String branchUuid = projectBranch.uuid();
dbClient.newCodePeriodDao().deleteByProjectUuidAndBranchUuid(dbSession, projectUuid, branchUuid);
dbClient.newCodePeriodDao().deleteByBranch(dbSession, projectUuid, branchUuid);
dbSession.commit();
}
}
Expand Down
Loading

0 comments on commit 14bd90f

Please sign in to comment.