return Optional.ofNullable(mapper(dbSession).selectByBranch(projectUuid, branchUuid));
}
+ public boolean existsByProjectAnalysisUuid(DbSession dbSession, String projectAnalysisUuid) {
+ requireNonNull(projectAnalysisUuid, "Project analysis uuid must be specified.");
+ return mapper(dbSession).countByProjectAnalysis(projectAnalysisUuid) > 0;
+ }
+
public void deleteByProjectUuidAndBranchUuid(DbSession dbSession, String projectUuid, String branchUuid) {
requireNonNull(projectUuid, "Project uuid must be specified.");
mapper(dbSession).deleteByProjectAndBranch(projectUuid, branchUuid);
void deleteByProjectAndBranch(@Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);
NewCodePeriodDto selectByBranch(@Param("projectUuid") String projectUuid, @Param("branchUuid") String branchUuid);
+
+ long countByProjectAnalysis(String projectAnalysisUuid);
}
AND branch_uuid=#{branchUuid, jdbcType=VARCHAR}
</update>
+ <select id="countByProjectAnalysis" parameterType="map" resultType="java.lang.Long">
+ SELECT
+ count(1)
+ FROM new_code_periods ncp
+ WHERE
+ ncp.type='SPECIFIC_ANALYSIS'
+ AND ncp.value=#{projectAnalysisUuid, jdbcType=VARCHAR}
+ </select>
+
</mapper>
assertThat(result.getUpdatedAt()).isNotEqualTo(0);
}
+ @Test
+ public void exists_by_project_analysis_is_true() {
+ when(uuidFactory.create()).thenReturn(NEW_CODE_PERIOD_UUID);
+
+ underTest.insert(dbSession, new NewCodePeriodDto()
+ .setProjectUuid("proj-uuid")
+ .setBranchUuid("branch-uuid")
+ .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
+ .setValue("analysis-uuid"));
+
+ boolean exists = underTest.existsByProjectAnalysisUuid(dbSession, "analysis-uuid");
+ assertThat(exists).isTrue();
+ }
+
+ @Test
+ public void exists_by_project_analysis_is_false() {
+ boolean exists = underTest.existsByProjectAnalysisUuid(dbSession, "analysis-uuid");
+ assertThat(exists).isFalse();
+ }
+
@Test
public void fail_select_by_project_and_branch_uuids_if_project_uuid_not_provided() {
expectedException.expect(NullPointerException.class);
}
private void checkNotBaseline(DbSession dbSession, SnapshotDto analysis) {
- dbClient.branchDao().selectByUuid(dbSession, analysis.getComponentUuid())
- .ifPresent(branchDto -> {
- dbClient.newCodePeriodDao().selectByBranch(dbSession, branchDto.getProjectUuid(), branchDto.getUuid())
- .ifPresent(newCodePeriodDto ->
- checkArgument(!newCodePeriodDto.getValue().equals(analysis.getUuid()),
- "The analysis '%s' can not be deleted because it is set as a manual new code period baseline", analysis.getUuid())
- );
- });
+ 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());
+
}
private static NotFoundException analysisNotFoundException(String analysis) {