/** * Required because there is no autogenerated keys for analysis_properties */ @After public void resetAnalysisProperties() { dbTester.executeUpdateSql("DELETE FROM analysis_properties"); }
private void manuallySetToNullFromHotpotsColumn(IssueDto fromHostSpotIssue) { dbTester.executeUpdateSql("update issues set from_hotspot = null where kee = '" + fromHostSpotIssue.getKey() + "'"); dbTester.commit(); }
@Test public void snashotDto_can_hold_version_larger_than_100_read_from_database() { SnapshotDto analysis = insertAnalysis("P1", "A1", STATUS_PROCESSED, false); db.commit(); String tooLongVersion = StringUtils.repeat("d", 200); db.executeUpdateSql("update snapshots set version='" + tooLongVersion + "' where uuid='" + analysis.getUuid() + "'"); db.commit(); assertThat(underTest.selectByUuid(dbSession, analysis.getUuid()) .map(SnapshotDto::getVersion)) .contains(tooLongVersion); }
@Test public void cancelWornOuts_does_not_update_queueStatus() { CEQueueStatus queueStatusMock = mock(CEQueueStatus.class); CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1")); db.executeUpdateSql("update ce_queue set status = 'PENDING', started_at = 123 where uuid = '" + task.getUuid() + "'"); db.commit(); InternalCeQueueImpl underTest = new InternalCeQueueImpl(system2, db.getDbClient(), null, queueStatusMock, null, null); underTest.cancelWornOuts(); assertThat(db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid())).isPresent(); verifyZeroInteractions(queueStatusMock); }
@Test public void project_settings_are_cached_to_avoid_db_access() { ComponentDto project = db.components().insertPrivateProject(); insertProjectProperty(project, "key", "value"); analysisMetadataHolder.setProject(Project.from(project)); Configuration config = underTest.getConfiguration(); assertThat(config.get("key")).hasValue("value"); db.executeUpdateSql("delete from properties"); db.commit(); assertThat(config.get("key")).hasValue("value"); }
@Test public void update() { ComponentDto project = db.components().insertPrivateProject(); ProjectLinkDto link = db.componentLinks().insertProvidedLink(project, c -> c .setUuid("ABCD") .setType("ci") .setName("Gihub") .setHref("http://www.github.org")); // Force dates to be in the past db.executeUpdateSql("UPDATE project_links SET created_at=" + PAST + " ,updated_at=" + PAST); ComponentDto project2 = db.components().insertPrivateProject(); underTest.update(db.getSession(), link .setProjectUuid(project2.uuid()) .setType("homepage") .setName("Home") .setHref("http://www.sonarqube.org")); db.getSession().commit(); ProjectLinkDto reloaded = underTest.selectByUuid(db.getSession(), link.getUuid()); assertThat(reloaded.getUuid()).isEqualTo("ABCD"); assertThat(reloaded.getProjectUuid()).isEqualTo(project2.uuid()); assertThat(reloaded.getType()).isEqualTo("homepage"); assertThat(reloaded.getName()).isEqualTo("Home"); assertThat(reloaded.getHref()).isEqualTo("http://www.sonarqube.org"); assertThat(reloaded.getCreatedAt()).isEqualTo(PAST); assertThat(reloaded.getUpdatedAt()).isEqualTo(NOW); }
@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(); } }
@Before() public void setUp() throws Exception { Consumer<RuleDefinitionDto> setUpdatedAt = rule -> rule.setUpdatedAt(system2.now()); this.ruleDefinitions = new RuleDefinitionDto[] { dbTester.rules().insert(setUpdatedAt), dbTester.rules().insert(setUpdatedAt), dbTester.rules().insert(setUpdatedAt), dbTester.rules().insert(setUpdatedAt), dbTester.rules().insert(setUpdatedAt), dbTester.rules().insert(setUpdatedAt) }; this.ruleParams = Arrays.stream(ruleDefinitions) .map(rule -> dbTester.rules().insertRuleParam(rule)) .toArray(RuleParamDto[]::new); underTest = new CachingRuleFinder(dbClient); // delete all data from DB to ensure tests rely on cache exclusively dbTester.executeUpdateSql("delete from rules"); dbTester.executeUpdateSql("delete from rules_parameters"); assertThat(dbTester.countRowsOfTable("rules")).isZero(); assertThat(dbTester.countRowsOfTable("rules_parameters")).isZero(); }
@Test public void fail_if_branch_is_main() { ComponentDto project = db.components().insertMainBranch(); db.executeUpdateSql("UPDATE project_branches set KEE = 'main'"); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); // not found because the DB keys don't contain the name expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Only non-main branches can be deleted"); tester.newRequest() .setParam("project", project.getKey()) .setParam("branch", "main") .execute(); }
@Test public void does_not_return_issues_from_external_rules() { RuleDefinitionDto rule = db.rules().insert(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(newModuleDto(project)); ComponentDto file = db.components().insertComponent(newFileDto(module, null)); IssueDto issueOnProject = db.issues().insert(rule, project, project, i -> i.setKee("ON_PROJECT").setType(randomRuleTypeExceptHotspot())); IssueDto issueOnModule = db.issues().insert(rule, project, module, i -> i.setKee("ON_MODULE").setType(randomRuleTypeExceptHotspot())); IssueDto issueOnFile = db.issues().insert(rule, project, file, i -> i.setKee("ON_FILE").setType(randomRuleTypeExceptHotspot())); RuleDefinitionDto external = db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto.setIsExternal(true)); IssueDto issueFromExteralruleOnFile = db.issues().insert(external, project, file, i -> i.setKee("ON_FILE_FROM_EXTERNAL").setType(randomRuleTypeExceptHotspot())); RuleDefinitionDto migrated = db.rules().insert(); db.executeUpdateSql("update rules set is_external=? where rules.id = ?", false, migrated.getId()); IssueDto issueFromMigratedRule = db.issues().insert(migrated, project, file, i -> i.setKee("MIGRATED").setType(randomRuleTypeExceptHotspot())); addPermissionTo(project); try (CloseableIterator<ServerIssue> result = callStream(project.getKey(), null)) { assertThat(result) .extracting(ServerIssue::getKey, ServerIssue::getModuleKey) .containsExactlyInAnyOrder( tuple(issueOnFile.getKey(), module.getKey()), tuple(issueOnModule.getKey(), module.getKey()), tuple(issueOnProject.getKey(), project.getKey()), tuple(issueFromMigratedRule.getKey(), module.getKey())); } }