public void insert(DbSession session, List<AnalysisPropertyDto> analysisPropertyDto) { analysisPropertyDto.forEach(a -> insert(session, a)); }
private void compareFirstValueWith(AnalysisPropertyDto analysisPropertyDto) { AnalysisPropertyDto dtoFromDatabase = underTest.selectBySnapshotUuid(dbSession, analysisPropertyDto.getSnapshotUuid()).get(0); assertThat(dtoFromDatabase).isEqualTo(analysisPropertyDto); } }
public void insert(DbSession session, AnalysisPropertyDto analysisPropertyDto) { requireNonNull(analysisPropertyDto.getUuid(), "uuid cannot be null"); requireNonNull(analysisPropertyDto.getKey(), "key cannot be null"); requireNonNull(analysisPropertyDto.getSnapshotUuid(), "snapshot uuid cannot be null"); requireNonNull(analysisPropertyDto.getValue(), "value cannot be null"); String value = analysisPropertyDto.getValue(); long now = system2.now(); if (isEmpty(value)) { getMapper(session).insertAsEmpty(analysisPropertyDto, now); } else if (mustBeStoredInClob(value)) { getMapper(session).insertAsClob(analysisPropertyDto, now); } else { getMapper(session).insertAsText(analysisPropertyDto, now); } }
@Test public void selectByAnalysisUuid_should_return_correct_values() { String snapshotUuid = randomAlphanumeric(40); List<AnalysisPropertyDto> propertyDtos = Arrays.asList( newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid), newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid), newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid), newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid), newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid), newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid), newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid), newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid)); underTest.insert(dbSession, propertyDtos); assertThat(dbTester.countRowsOfTable(dbSession, "ANALYSIS_PROPERTIES")).isEqualTo(propertyDtos.size()); List<AnalysisPropertyDto> result = underTest.selectBySnapshotUuid(dbSession, snapshotUuid); assertThat(result).containsExactlyInAnyOrder(propertyDtos.toArray(new AnalysisPropertyDto[0])); }
public List<AnalysisPropertyDto> selectBySnapshotUuid(DbSession session, String snapshotUuid) { requireNonNull(snapshotUuid); return getMapper(session).selectBySnapshotUuid(snapshotUuid); }
private AnalysisPropertyDto insertAnalysisPropertyDto(int valueLength) { AnalysisPropertyDto analysisPropertyDto = newAnalysisPropertyDto(valueLength, randomAlphanumeric(40)); underTest.insert(dbSession, analysisPropertyDto); return analysisPropertyDto; }
@Test public void persist_should_only_store_scmRevisionId_if_there_is_no_context_properties() { when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID)); when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator()); when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID); underTest.execute(new TestComputationStepContext()); assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(1); List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient() .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID); assertThat(propertyDtos) .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue) .containsExactlyInAnyOrder(tuple(SNAPSHOT_UUID, "sonar.analysis.scm_revision_id", SCM_REV_ID)); }
private void insertPropertiesFor(String snapshotUuid, Map<String, String> properties) { List<AnalysisPropertyDto> analysisProperties = properties.entrySet().stream() .map(entry -> new AnalysisPropertyDto() .setUuid(UuidFactoryFast.getInstance().create()) .setSnapshotUuid(snapshotUuid) .setKey(entry.getKey()) .setValue(entry.getValue())) .collect(toArrayList(properties.size())); dbTester.getDbClient().analysisPropertiesDao().insert(dbTester.getSession(), analysisProperties); dbTester.getSession().commit(); }
private WebhookPayload buildWebHookPayload(DbSession dbSession, QGChangeEvent event, @Nullable EvaluatedQualityGate evaluatedQualityGate) { ComponentDto project = event.getProject(); BranchDto branch = event.getBranch(); SnapshotDto analysis = event.getAnalysis(); Map<String, String> analysisProperties = dbClient.analysisPropertiesDao().selectBySnapshotUuid(dbSession, analysis.getUuid()) .stream() .collect(Collectors.toMap(AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)); String projectUuid = StringUtils.defaultString(project.getMainBranchProjectUuid(), project.projectUuid()); ProjectAnalysis projectAnalysis = new ProjectAnalysis( new Project(projectUuid, project.getKey(), project.name()), null, new Analysis(analysis.getUuid(), analysis.getCreatedAt()), new Branch(branch.isMain(), branch.getKey(), Type.valueOf(branch.getBranchType().name())), evaluatedQualityGate, null, analysisProperties); return webhookPayloadFactory.create(projectAnalysis); }
@Override public void execute(ComputationStep.Context context) { List<AnalysisPropertyDto> analysisPropertyDtos = new ArrayList<>(); try (CloseableIterator<ScannerReport.ContextProperty> it = reportReader.readContextProperties()) { it.forEachRemaining( contextProperty -> { String propertyKey = contextProperty.getKey(); if (propertyKey.startsWith(SONAR_ANALYSIS) || propertyKey.startsWith(SONAR_PULL_REQUEST)) { analysisPropertyDtos.add(new AnalysisPropertyDto() .setUuid(uuidFactory.create()) .setKey(propertyKey) .setValue(contextProperty.getValue()) .setSnapshotUuid(analysisMetadataHolder.getUuid())); } }); } analysisMetadataHolder.getScmRevisionId().ifPresent(scmRevisionId -> analysisPropertyDtos.add(new AnalysisPropertyDto() .setUuid(uuidFactory.create()) .setKey(SCM_REVISION_ID) .setValue(scmRevisionId) .setSnapshotUuid(analysisMetadataHolder.getUuid()))); if (analysisPropertyDtos.isEmpty()) { return; } try (DbSession dbSession = dbClient.openSession(false)) { dbClient.analysisPropertiesDao().insert(dbSession, analysisPropertyDtos); dbSession.commit(); } }
@Test public void persist_should_stores_sonarDotAnalysisDot_and_sonarDotPullRequestDot_properties() { when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator())); when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID); when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID)); underTest.execute(new TestComputationStepContext()); assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(9); List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient() .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID); assertThat(propertyDtos) .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue) .containsExactlyInAnyOrder( tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE2), tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""), tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE), tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE3), tuple(SNAPSHOT_UUID, "sonar.analysis.scm_revision_id", SCM_REV_ID), tuple(SNAPSHOT_UUID, "sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2), tuple(SNAPSHOT_UUID, "sonar.pullrequest.empty_string", ""), tuple(SNAPSHOT_UUID, "sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE), tuple(SNAPSHOT_UUID, "sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3)); }
@Test public void insert_a_list() { List<AnalysisPropertyDto> propertyDtos = Arrays.asList( newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)), newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)), newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)), newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)), newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)), newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)), newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)), newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40))); underTest.insert(dbSession, propertyDtos); assertThat(dbTester.countRowsOfTable(dbSession, "ANALYSIS_PROPERTIES")).isEqualTo(propertyDtos.size()); }
@Test public void persist_should_not_stores_sonarDotAnalysisDotscm_revision_id_properties_when_its_not_available_in_report_metada() { when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator())); when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID); when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.empty()); underTest.execute(new TestComputationStepContext()); assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(8); List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient() .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID); assertThat(propertyDtos) .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue) .containsExactlyInAnyOrder( tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE2), tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""), tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE), tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE3), tuple(SNAPSHOT_UUID, "sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2), tuple(SNAPSHOT_UUID, "sonar.pullrequest.empty_string", ""), tuple(SNAPSHOT_UUID, "sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE), tuple(SNAPSHOT_UUID, "sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3)); }
@Test public void insert_with_null_uuid_throws_NPE() { AnalysisPropertyDto analysisPropertyDto = new AnalysisPropertyDto() .setSnapshotUuid(randomAlphanumeric(10)) .setKey(randomAlphanumeric(10)) .setValue(randomAlphanumeric(10)); expectedException.expect(NullPointerException.class); expectedException.expectMessage("uuid cannot be null"); underTest.insert(dbSession, analysisPropertyDto); }
private WebhookPayload buildWebHookPayload(DbSession dbSession, QGChangeEvent event, @Nullable EvaluatedQualityGate evaluatedQualityGate) { ComponentDto project = event.getProject(); BranchDto branch = event.getBranch(); SnapshotDto analysis = event.getAnalysis(); Map<String, String> analysisProperties = dbClient.analysisPropertiesDao().selectBySnapshotUuid(dbSession, analysis.getUuid()) .stream() .collect(Collectors.toMap(AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)); String projectUuid = StringUtils.defaultString(project.getMainBranchProjectUuid(), project.projectUuid()); ProjectAnalysis projectAnalysis = new ProjectAnalysis( new Project(projectUuid, project.getKey(), project.name()), null, new Analysis(analysis.getUuid(), analysis.getCreatedAt()), new Branch(branch.isMain(), branch.getKey(), Type.valueOf(branch.getBranchType().name())), evaluatedQualityGate, null, analysisProperties); return webhookPayloadFactory.create(projectAnalysis); }
@Test public void insert_with_null_snapshot_uuid_throws_NPE() { AnalysisPropertyDto analysisPropertyDto = new AnalysisPropertyDto() .setUuid(randomAlphanumeric(10)) .setKey(randomAlphanumeric(10)) .setValue(randomAlphanumeric(10)); expectedException.expect(NullPointerException.class); expectedException.expectMessage("snapshot uuid cannot be null"); underTest.insert(dbSession, analysisPropertyDto); }
@Test public void insert_with_null_key_throws_NPE() { AnalysisPropertyDto analysisPropertyDto = new AnalysisPropertyDto() .setSnapshotUuid(randomAlphanumeric(10)) .setUuid(randomAlphanumeric(10)) .setValue(randomAlphanumeric(10)); expectedException.expect(NullPointerException.class); expectedException.expectMessage("key cannot be null"); underTest.insert(dbSession, analysisPropertyDto); }
@Test public void insert_with_null_value_throws_NPE() { AnalysisPropertyDto analysisPropertyDto = new AnalysisPropertyDto() .setSnapshotUuid(randomAlphanumeric(10)) .setUuid(randomAlphanumeric(10)) .setKey(randomAlphanumeric(10)); expectedException.expect(NullPointerException.class); expectedException.expectMessage("value cannot be null"); underTest.insert(dbSession, analysisPropertyDto); }
@Override public void execute() { final List<AnalysisPropertyDto> analysisPropertyDtos = new ArrayList<>(); reportReader.readContextProperties().forEachRemaining( contextProperty -> { String propertyKey = contextProperty.getKey(); if (propertyKey.startsWith(SONAR_ANALYSIS) || propertyKey.startsWith(SONAR_PULL_REQUEST)) { analysisPropertyDtos.add(new AnalysisPropertyDto() .setUuid(uuidFactory.create()) .setKey(propertyKey) .setValue(contextProperty.getValue()) .setSnapshotUuid(analysisMetadataHolder.getUuid())); } }); if (analysisPropertyDtos.isEmpty()) { return; } try (DbSession dbSession = dbClient.openSession(false)) { dbClient.analysisPropertiesDao().insert(dbSession, analysisPropertyDtos); dbSession.commit(); } }