@Test public void get_profile_by_project_and_language() { ComponentDto project = new ComponentDto() .setId(1L) .setUuid("ABCD") .setKey("org.codehaus.sonar:sonar") .setName("SonarQube") .setLongName("SonarQube") .setQualifier("TRK") .setScope("TRK") .setEnabled(true); db.componentDao().insert(dbSession, project); QualityProfileDto profileDto = QProfileTesting.newXooP1(); db.qualityProfileDao().insert(dbSession, profileDto); dbSession.commit(); dbSession.clearCache(); assertThat(factory.getByProjectAndLanguage("org.codehaus.sonar:sonar", "xoo")).isNull(); tester .get(QProfileProjectOperations.class) .addProject( profileDto.getKey(), project.uuid(), new MockUserSession("me").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN), dbSession); dbSession.commit(); dbSession.clearCache(); assertThat(factory.getByProjectAndLanguage("org.codehaus.sonar:sonar", "xoo").getKey()) .isEqualTo(XOO_P1_KEY); }
@Test public void get_profile_by_name_and_language() { QualityProfileDto profileDto = QProfileTesting.newQProfileDto(new QProfileName("xoo", "SonarQube way"), "abcd"); db.qualityProfileDao().insert(dbSession, profileDto); dbSession.commit(); dbSession.clearCache(); assertThat(factory.getByNameAndLanguage("SonarQube way", "xoo").getKey()).isEqualTo("abcd"); assertThat(factory.getByNameAndLanguage("SonarQube way", "java")).isNull(); assertThat(factory.getByNameAndLanguage("Unfound", "xoo")).isNull(); }
@Test public void fail_to_create_if_already_exists() { QProfileName name = new QProfileName("xoo", "P1"); factory.create(dbSession, name); dbSession.commit(); dbSession.clearCache(); try { factory.create(dbSession, name); fail(); } catch (BadRequestException e) { assertThat(e).hasMessage("Quality profile already exists: {lang=xoo, name=P1}"); } }
@Test public void fail_renaming_if_name_already_exists() { QualityProfileDto p1 = factory.create(dbSession, new QProfileName("xoo", "P1")); QualityProfileDto p2 = factory.create(dbSession, new QProfileName("xoo", "P2")); dbSession.commit(); dbSession.clearCache(); try { factory.rename(p1.getKey(), "P2"); fail(); } catch (BadRequestException e) { assertThat(e).hasMessage("Quality profile already exists: P2"); } }
@Test public void fail_if_blank_renaming() { QualityProfileDto dto = factory.create(dbSession, new QProfileName("xoo", "P1")); dbSession.commit(); dbSession.clearCache(); String key = dto.getKey(); try { factory.rename(key, " "); fail(); } catch (BadRequestException e) { assertThat(e).hasMessage("Name must be set"); } }
@Test public void ignore_renaming_if_same_name() { QualityProfileDto dto = factory.create(dbSession, new QProfileName("xoo", "P1")); dbSession.commit(); dbSession.clearCache(); String key = dto.getKey(); assertThat(factory.rename(key, "P1")).isFalse(); dbSession.clearCache(); QualityProfileDto reloaded = db.qualityProfileDao().selectByKey(dbSession, dto.getKee()); assertThat(reloaded.getKey()).isEqualTo(key); assertThat(reloaded.getName()).isEqualTo("P1"); }
@Test public void do_not_delete_default_profile() { db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1()); factory.setDefault(dbSession, XOO_P1_KEY); dbSession.commit(); dbSession.clearCache(); try { factory.delete(XOO_P1_KEY); fail(); } catch (BadRequestException e) { assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P1"); assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(1); } }
@Test public void delete_descendants() { initRules(); // create parent and child profiles db.qualityProfileDao() .insert( dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2(), QProfileTesting.newXooP3()); tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY); tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY); tester .get(RuleActivator.class) .activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY); dbSession.commit(); dbSession.clearCache(); assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(3); assertThat(db.activeRuleDao().selectAll(dbSession)).hasSize(3); factory.delete(XOO_P1_KEY); dbSession.clearCache(); assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty(); assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty(); assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty(); assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P1_KEY)).isEmpty(); assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P2_KEY)).isEmpty(); assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P3_KEY)).isEmpty(); }
@Test public void fail_renaming_if_profile_not_found() { thrown.expect(NotFoundException.class); thrown.expectMessage("Quality profile not found: unknown"); factory.rename("unknown", "the new name"); }
@Test public void fail_if_unknown_profile_to_be_deleted() { thrown.expect(RowNotFoundException.class); thrown.expectMessage("Quality profile not found: XOO_P1"); factory.delete(XOO_P1_KEY); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn().checkPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN); String name = request.mandatoryParam(PARAM_PROFILE_NAME); String language = request.mandatoryParam(PARAM_LANGUAGE); DbSession dbSession = dbClient.openSession(false); try { QProfileResult result = new QProfileResult(); QualityProfileDto profile = profileFactory.create(dbSession, QProfileName.createFor(language, name)); result.setProfile(profile); for (ProfileImporter importer : importers) { InputStream contentToImport = request.paramAsInputStream(getBackupParamName(importer.getKey())); if (contentToImport != null) { result.add(exporters.importXml(profile, importer.getKey(), contentToImport, dbSession)); } } dbSession.commit(); response.stream().setMediaType(request.getMediaType()); JsonWriter jsonWriter = JsonWriter.of(new OutputStreamWriter(response.stream().output(), StandardCharsets.UTF_8)); writeResult(jsonWriter, result); } finally { dbSession.close(); } }
@Test public void fail_if_unknown_profile_to_be_set_as_default() { thrown.expect(NotFoundException.class); thrown.expectMessage("Quality profile not found: " + XOO_P1_KEY); factory.setDefault(XOO_P1_KEY); }
@Test public void fail_to_create_if_name_empty() { QProfileName name = new QProfileName("xoo", null); try { factory.create(dbSession, name); fail(); } catch (BadRequestException e) { assertThat(e).hasMessage("quality_profiles.profile_name_cant_be_blank"); } name = new QProfileName("xoo", ""); try { factory.create(dbSession, name); fail(); } catch (BadRequestException e) { assertThat(e).hasMessage("quality_profiles.profile_name_cant_be_blank"); } }
@Test public void do_not_delete_if_default_descendant() { db.qualityProfileDao() .insert( dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2(), QProfileTesting.newXooP3()); tester.get(RuleActivator.class).setParent(dbSession, XOO_P2_KEY, XOO_P1_KEY); tester.get(RuleActivator.class).setParent(dbSession, XOO_P3_KEY, XOO_P1_KEY); factory.setDefault(dbSession, XOO_P3_KEY); dbSession.commit(); dbSession.clearCache(); try { factory.delete(XOO_P1_KEY); fail(); } catch (BadRequestException e) { assertThat(e).hasMessage("The profile marked as default can not be deleted: XOO_P3"); assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(3); } }
@Test public void set_default_profile() { db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1()); dbSession.commit(); dbSession.clearCache(); assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P1_KEY).isDefault()).isFalse(); factory.setDefault(XOO_P1_KEY); dbSession.clearCache(); assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P1_KEY).isDefault()).isTrue(); }
@Test public void delete() { initRules(); db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1()); tester .get(RuleActivator.class) .activate(dbSession, new RuleActivation(RuleTesting.XOO_X1), XOO_P1_KEY); dbSession.commit(); dbSession.clearCache(); factory.delete(XOO_P1_KEY); dbSession.clearCache(); assertThat(db.qualityProfileDao().selectAll(dbSession)).isEmpty(); assertThat(db.activeRuleDao().selectAll(dbSession)).isEmpty(); assertThat(db.activeRuleDao().selectAllParams(dbSession)).isEmpty(); assertThat(index.get(ActiveRuleIndex.class).findByProfile(XOO_P1_KEY)).isEmpty(); }
@Test public void create() { QualityProfileDto dto = factory.create(dbSession, new QProfileName("xoo", "P1")); dbSession.commit(); dbSession.clearCache(); assertThat(dto.getKey()).startsWith("xoo-p1-"); assertThat(dto.getName()).isEqualTo("P1"); assertThat(dto.getLanguage()).isEqualTo("xoo"); assertThat(dto.getId()).isNotNull(); // reload the dto dto = db.qualityProfileDao().selectByNameAndLanguage("P1", "xoo", dbSession); assertThat(dto.getLanguage()).isEqualTo("xoo"); assertThat(dto.getName()).isEqualTo("P1"); assertThat(dto.getKey()).startsWith("xoo-p1"); assertThat(dto.getId()).isNotNull(); assertThat(dto.getParentKee()).isNull(); assertThat(db.qualityProfileDao().selectAll(dbSession)).hasSize(1); }