assertThat(underTest.getDescription()).isEqualTo("Export ad-hoc rules");
}
+ @Test
+ public void execute_shouldMapFieldWithEmptyString_whenNameOrPluginKeyAreNull() {
+ RuleKey ruleKey = RuleKey.of("plugin1", "partiallyInit");
+ RuleDto partiallyInitRuleDto = insertAdHocRuleWithoutNameAndPluginKeyAndAdHocInformations(ruleKey);
+ insertIssue(partiallyInitRuleDto, mainBranch.uuid(), mainBranch.uuid());
+
+ underTest.execute(new TestComputationStepContext());
+
+ List<ProjectDump.AdHocRule> exportedRules = dumpWriter.getWrittenMessagesOf(DumpElement.AD_HOC_RULES);
+ assertThat(exportedRules).hasSize(1);
+ ProjectDump.AdHocRule adHocRule = exportedRules.iterator().next();
+ assertThat(adHocRule.getName()).isEmpty();
+ assertThat(adHocRule.getPluginKey()).isEmpty();
+ ProjectDump.AdHocRule.RuleMetadata adHocRuleMetadata = adHocRule.getMetadata();
+ assertThat(adHocRuleMetadata.getAdHocDescription()).isEmpty();
+ assertThat(adHocRuleMetadata.getAdHocName()).isEmpty();
+ assertThat(adHocRuleMetadata.getAdHocSeverity()).isEmpty();
+ assertThat(adHocRuleMetadata.getAdHocType()).isZero();
+
+ }
+
+ private RuleDto insertAdHocRuleWithoutNameAndPluginKeyAndAdHocInformations(RuleKey ruleKey) {
+ RuleDto partiallyInitRuleDto = new RuleDto()
+ .setIsExternal(false)
+ .setIsAdHoc(true)
+ .setCleanCodeAttribute(CleanCodeAttribute.CONVENTIONAL)
+ .addDefaultImpact(new ImpactDto().setUuid(Uuids.createFast()).setSoftwareQuality(SoftwareQuality.MAINTAINABILITY).setSeverity(org.sonar.api.issue.impact.Severity.MEDIUM))
+ .addDefaultImpact(new ImpactDto().setUuid(Uuids.createFast()).setSoftwareQuality(SoftwareQuality.RELIABILITY).setSeverity(org.sonar.api.issue.impact.Severity.HIGH))
+ .setRuleKey(ruleKey)
+ .setScope(RuleDto.Scope.ALL)
+ .setStatus(RuleStatus.READY);
+
+ dbTester.rules().insert(partiallyInitRuleDto);
+ dbTester.commit();
+
+ return dbTester.getDbClient().ruleDao().selectByKey(dbTester.getSession(), ruleKey)
+ .orElseThrow(() -> new RuntimeException("insertAdHocRule failed"));
+ }
+
private ProjectDto createProject() {
Date createdAt = new Date();
ProjectData projectData = dbTester.components().insertPublicProject(PROJECT_UUID);
i -> org.sonar.api.issue.impact.Severity.valueOf(i.getSeverity().name())));
}
-
private static void assertProtobufAdHocRuleIsCorrectlyBuilt(ProjectDump.AdHocRule.RuleMetadata metadata, RuleDto expected) {
assertThat(metadata.getAdHocName()).isEqualTo(expected.getAdHocName());
assertThat(metadata.getAdHocDescription()).isEqualTo(expected.getAdHocDescription());
import com.hazelcast.internal.util.MutableLong;
import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
import java.util.List;
+import java.util.Optional;
import java.util.Set;
import org.apache.ibatis.cursor.Cursor;
import org.slf4j.LoggerFactory;
try (
StreamWriter<ProjectDump.AdHocRule> output = dumpWriter.newStreamWriter(DumpElement.AD_HOC_RULES);
DbSession dbSession = dbClient.openSession(false);
- Cursor<RuleDto> ruleDtoCursor = dbSession.getMapper(ProjectExportMapper.class).scrollAdhocRulesForExport(projectHolder.projectDto().getUuid())
- ) {
+ Cursor<RuleDto> ruleDtoCursor = dbSession.getMapper(ProjectExportMapper.class).scrollAdhocRulesForExport(projectHolder.projectDto().getUuid())) {
ProjectDump.AdHocRule.Builder adHocRuleBuilder = ProjectDump.AdHocRule.newBuilder();
ruleDtoCursor
.forEach(ruleDto -> {
return builder
.clear()
.setRef(ruleDto.getUuid())
- .setPluginKey(ruleDto.getPluginKey())
+ .setPluginKey(Optional.of(ruleDto).map(RuleDto::getPluginKey).orElse(""))
.setPluginRuleKey(ruleDto.getKey().rule())
.setPluginName(ruleDto.getRepositoryKey())
- .setName(ruleDto.getName())
- .setStatus(ruleDto.getStatus().name())
+ .setName(Optional.of(ruleDto).map(RuleDto::getName).orElse(""))
+ .setStatus(Optional.of(ruleDto).map(RuleDto::getStatus).map(Enum::name).orElse(""))
.setType(ruleDto.getType())
.setScope(ruleDto.getScope().name())
.setMetadata(buildMetadata(ruleDto))
}
private static ProjectDump.AdHocRule.RuleMetadata buildMetadata(RuleDto ruleDto) {
+ Optional<RuleDto> rule = Optional.of(ruleDto);
return ProjectDump.AdHocRule.RuleMetadata.newBuilder()
- .setAdHocName(ruleDto.getAdHocName())
- .setAdHocDescription(ruleDto.getAdHocDescription())
- .setAdHocSeverity(ruleDto.getAdHocSeverity())
- .setAdHocType(ruleDto.getAdHocType())
+ .setAdHocName(rule.map(RuleDto::getAdHocName).orElse(""))
+ .setAdHocDescription(rule.map(RuleDto::getAdHocDescription).orElse(""))
+ .setAdHocSeverity(rule.map(RuleDto::getAdHocSeverity).orElse(""))
+ .setAdHocType(rule.map(RuleDto::getAdHocType).orElse(0))
.build();
}