public int getLineHashesVersion() { return lineHashesVersion != null ? lineHashesVersion : LineHashVersion.WITHOUT_SIGNIFICANT_CODE.getDbValue(); }
@Override public int getLineHashesVersion(Component component) { if (significantCodeRepository.getRangesPerLine(component).isPresent()) { return LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue(); } else { return LineHashVersion.WITHOUT_SIGNIFICANT_CODE.getDbValue(); } }
@Test public void should_return_version_of_line_hashes_without_significant_code_in_the_report() { when(significantCodeRepository.getRangesPerLine(file)).thenReturn(Optional.empty()); assertThat(underTest.getLineHashesVersion(file)).isEqualTo(LineHashVersion.WITHOUT_SIGNIFICANT_CODE.getDbValue()); verify(significantCodeRepository).getRangesPerLine(file); verifyNoMoreInteractions(significantCodeRepository); verifyZeroInteractions(dbLineHashVersion); }
@Test public void should_return_with_significant_code_if_report_contains_it() { when(significantCodeRepository.getRangesPerLine(file)).thenReturn(Optional.of(new LineRange[0])); assertThat(underTest.getLineHashesVersion(file)).isEqualTo(LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue()); verify(significantCodeRepository).getRangesPerLine(file); verifyNoMoreInteractions(significantCodeRepository); verifyZeroInteractions(dbLineHashVersion); }
@Test public void should_return_without_significant_code_if_report_does_not_contain_it() { when(significantCodeRepository.getRangesPerLine(file)).thenReturn(Optional.empty()); assertThat(underTest.getLineHashesVersion(file)).isEqualTo(LineHashVersion.WITHOUT_SIGNIFICANT_CODE.getDbValue()); verify(significantCodeRepository).getRangesPerLine(file); verifyNoMoreInteractions(significantCodeRepository); verifyZeroInteractions(dbLineHashVersion); }
@Test public void should_return_version_of_line_hashes_with_significant_code_in_the_report() { LineRange[] lineRanges = {new LineRange(0, 1), null, new LineRange(1, 5)}; when(significantCodeRepository.getRangesPerLine(file)).thenReturn(Optional.of(lineRanges)); assertThat(underTest.getLineHashesVersion(file)).isEqualTo(LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue()); verify(significantCodeRepository).getRangesPerLine(file); verifyNoMoreInteractions(significantCodeRepository); verifyZeroInteractions(dbLineHashVersion); }
@Test public void persist_sources() { List<String> lineHashes = Arrays.asList("137f72c3708c6bd0de00a0e5a69c699b", "e6251bcf1a7dc3ba5e7933e325bbe605"); String sourceHash = "ee5a58024a155466b43bc559d953e018"; DbFileSources.Data fileSourceData = DbFileSources.Data.newBuilder() .addAllLines(Arrays.asList( DbFileSources.Line.newBuilder().setSource("line1").setLine(1).build(), DbFileSources.Line.newBuilder().setSource("line2").setLine(2).build())) .build(); when(fileSourceDataComputer.compute(fileComponent().build(), fileSourceDataWarnings)) .thenReturn(new FileSourceDataComputer.Data(fileSourceData, lineHashes, sourceHash, null)); underTest.execute(new TestComputationStepContext()); assertThat(dbTester.countRowsOfTable("file_sources")).isEqualTo(1); FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectByFileUuid(session, FILE1_UUID); assertThat(fileSourceDto.getProjectUuid()).isEqualTo(PROJECT_UUID); assertThat(fileSourceDto.getFileUuid()).isEqualTo(FILE1_UUID); assertThat(fileSourceDto.getBinaryData()).isNotEmpty(); assertThat(fileSourceDto.getDataHash()).isNotEmpty(); assertThat(fileSourceDto.getLineHashesVersion()).isEqualTo(LineHashVersion.WITHOUT_SIGNIFICANT_CODE.getDbValue()); assertThat(fileSourceDto.getLineHashes()).isNotEmpty(); assertThat(fileSourceDto.getCreatedAt()).isEqualTo(NOW); assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(NOW); DbFileSources.Data data = fileSourceDto.getSourceData(); assertThat(data.getLinesCount()).isEqualTo(2); assertThat(data.getLines(0).getLine()).isEqualTo(1); assertThat(data.getLines(0).getSource()).isEqualTo("line1"); assertThat(data.getLines(1).getLine()).isEqualTo(2); assertThat(data.getLines(1).getSource()).isEqualTo("line2"); verify(fileSourceDataWarnings).commitWarnings(); }
@Test public void should_cache_line_hash_version_from_db() { ComponentDto project = db.components().insertPublicProject(); ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project)); db.fileSources().insertFileSource(file, dto -> dto.setLineHashesVersion(LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue())); Component component = ReportComponent.builder(Component.Type.FILE, 1).setKey("key").setUuid(file.uuid()).build(); assertThat(underTest.hasLineHashesWithSignificantCode(component)).isTrue(); assertThat(db.countRowsOfTable("file_sources")).isOne(); db.executeUpdateSql("delete from file_sources"); db.commit(); assertThat(db.countRowsOfTable("file_sources")).isZero(); // still true because it uses cache assertThat(underTest.hasLineHashesWithSignificantCode(component)).isTrue(); } }
@Test public void hasLineHashWithSignificantCode_should_return_true() { ComponentDto project = db.components().insertPublicProject(); ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project)); db.fileSources().insertFileSource(file, dto -> dto.setLineHashesVersion(LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue())); Component component = ReportComponent.builder(Component.Type.FILE, 1).setKey("key").setUuid(file.uuid()).build(); assertThat(underTest.hasLineHashesWithSignificantCode(component)).isTrue(); }
@Override public int getLineHashesVersion(Component component) { if (significantCodeRepository.getRangesPerLine(component).isPresent()) { return LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue(); } else { return LineHashVersion.WITHOUT_SIGNIFICANT_CODE.getDbValue(); } }