@Test public void indexOnStartup_indexes_all_data() { ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile1, rule1); underTest.indexOnStartup(emptySet()); List<ActiveRuleDoc> docs = es.getDocuments(INDEX_TYPE_ACTIVE_RULE, ActiveRuleDoc.class); assertThat(docs).hasSize(1); verify(docs.get(0), profile1, activeRule); assertThatEsQueueTableIsEmpty(); }
@Test public void isDefault_returns_true_if_profile_is_marked_as_default() { OrganizationDto org = dbTester.organizations().insert(); QProfileDto profile1 = dbTester.qualityProfiles().insert(org); QProfileDto profile2 = dbTester.qualityProfiles().insert(org); dbTester.qualityProfiles().setAsDefault(profile1); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile1.getKee())).isTrue(); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile2.getKee())).isFalse(); assertThat(underTest.isDefault(dbSession, org.getUuid(), "does_not_exist")).isFalse(); }
@Test public void commitAndIndex_empty_list() { ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1); underTest.commitAndIndex(db.getSession(), Collections.emptyList()); assertThat(es.countDocuments(INDEX_TYPE_ACTIVE_RULE)).isEqualTo(0); assertThatEsQueueTableIsEmpty(); }
@Test public void renameAndCommit_updates_name_of_specified_profiles() { OrganizationDto org1 = db.organizations().insert(); OrganizationDto org2 = db.organizations().insert(); QProfileDto fooInOrg1 = db.qualityProfiles().insert(org1, p -> p.setName("foo")); QProfileDto fooInOrg2 = db.qualityProfiles().insert(org2, p -> p.setName("foo")); QProfileDto bar = db.qualityProfiles().insert(org1, p -> p.setName("bar")); underTest.renameRulesProfilesAndCommit(dbSession, asList(fooInOrg1.getRulesProfileUuid(), fooInOrg2.getRulesProfileUuid()), "foo (copy)"); assertThat(underTest.selectOrFailByUuid(dbSession, fooInOrg1.getKee()).getName()).isEqualTo("foo (copy)"); assertThat(underTest.selectOrFailByUuid(dbSession, fooInOrg2.getKee()).getName()).isEqualTo("foo (copy)"); assertThat(underTest.selectOrFailByUuid(dbSession, bar.getKee()).getName()).isEqualTo("bar"); }
@Test public void renameAndCommit_does_nothing_if_empty_keys() { OrganizationDto org = db.organizations().insert(); QProfileDto profile = db.qualityProfiles().insert(org, p -> p.setName("foo")); underTest.renameRulesProfilesAndCommit(dbSession, Collections.emptyList(), "foo (copy)"); assertThat(underTest.selectOrFailByUuid(dbSession, profile.getKee()).getName()).isEqualTo("foo"); }
@Test public void deactivate_user_deletes_his_qprofiles_permissions() { logInAsSystemAdministrator(); UserDto user = db.users().insertUser(); QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); db.qualityProfiles().addUserPermission(profile, user); deactivate(user.getLogin()); assertThat(db.getDbClient().qProfileEditUsersDao().exists(dbSession, profile, user)).isFalse(); }
@Test public void insertOrUpdate_inserts_row_when_does_not_exist() { OrganizationDto org = dbTester.organizations().insert(); QProfileDto profile = dbTester.qualityProfiles().insert(org); DefaultQProfileDto dto = DefaultQProfileDto.from(profile); underTest.insertOrUpdate(dbSession, dto); dbSession.commit(); assertThat(countRows()).isEqualTo(1); assertThatIsDefault(org, profile); }
@Test public void add_project_on_profile_of_specified_organization() { OrganizationDto org1 = db.organizations().insert(); logInAsProfileAdmin(org1); ComponentDto project = db.components().insertPrivateProject(org1); QProfileDto profile = db.qualityProfiles().insert(org1, p -> p.setLanguage(LANGUAGE_1)); TestResponse response = call(org1, project, profile); assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT); assertProjectIsAssociatedToProfile(project, profile); }
@Test public void no_comparison_when_param_is_false() { QProfileDto sonarWayProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey())); QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(XOO1.getKey())); ShowResponse result = call(ws.newRequest() .setParam(PARAM_KEY, profile.getKee()) .setParam(PARAM_COMPARE_TO_SONAR_WAY, "false")); assertThat(result.hasCompareToSonarWay()).isFalse(); }
@Test public void project_administrator_can_remove_profile() { ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization()); QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); db.qualityProfiles().associateWithProject(project, profile); userSession.logIn(db.users().insertUser()).addProjectPermission(UserRole.ADMIN, project); call(project, profile); assertProjectIsNotAssociatedToProfile(project, profile); }
@Test public void commitAndIndex_keeps_elements_to_recover_in_ES_QUEUE_on_errors() { ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1); es.lockWrites(INDEX_TYPE_ACTIVE_RULE); commitAndIndex(rule1, ar); EsQueueDto expectedItem = EsQueueDto.create(INDEX_TYPE_ACTIVE_RULE.format(), "" + ar.getId(), "activeRuleId", valueOf(ar.getRuleId())); assertThatEsQueueContainsExactly(expectedItem); es.unlockWrites(INDEX_TYPE_ACTIVE_RULE); }
@Test public void do_not_fail_to_compare_on_paid_organization() { OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID)); QProfileDto left = db.qualityProfiles().insert(organization); QProfileDto right = db.qualityProfiles().insert(organization); UserDto user = db.users().insertUser(); userSession.logIn(user).addMembership(organization); ws.newRequest() .setParam("leftKey", left.getKee()) .setParam("rightKey", right.getKee()) .execute(); }
@Test public void fail_when_profile_is_built_in() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE).setIsBuiltIn(true)); RuleDefinitionDto defaultRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(String.format("Operation forbidden for built-in Quality Profile '%s'", profile.getKee())); underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getId()))); }
@Test public void fail_on_paid_organization_when_not_member() { OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID)); QProfileDto qualityProfile = db.qualityProfiles().insert(organization); expectedException.expect(ForbiddenException.class); expectedException.expectMessage(format("You're not member of organization '%s'", organization.getKey())); ws.newRequest() .setParam(PARAM_KEY, qualityProfile.getKee()) .execute(); }
@Test public void activate_one_rule() { OrganizationDto organization = db.organizations().insert(); QProfileDto qProfile = db.qualityProfiles().insert(organization); RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(qProfile.getLanguage())); RuleActivation ruleActivation = RuleActivation.create(rule.getId(), Severity.CRITICAL, Collections.emptyMap()); qProfileRules.activateAndCommit(db.getSession(), qProfile, singleton(ruleActivation)); assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), qProfile)) .extracting(ActiveRuleDto::getRuleKey, ActiveRuleDto::getSeverityString) .containsExactlyInAnyOrder(tuple(rule.getKey(), Severity.CRITICAL)); }
@Test public void fail_if_profile_language_is_not_supported() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setKee("unknown-profile").setLanguage("kotlin")); expectedException.expect(NotFoundException.class); expectedException.expectMessage("Quality Profile with key 'unknown-profile' does not exist"); call(ws.newRequest().setParam(PARAM_KEY, profile.getKee())); }
@Test public void show_on_paid_organization() { OrganizationDto organization = db.organizations().insert(); QProfileDto qualityProfile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO1.getKey())); UserDto user = db.users().insertUser(); db.organizations().addMember(organization, user); userSession.logIn(user); ShowResponse result = call(ws.newRequest().setParam(PARAM_KEY, qualityProfile.getKee())); assertThat(result.getProfile()) .extracting(QualityProfile::getKey) .containsExactly(qualityProfile.getKee()); }
@Test public void return_qprofile_on_paid_organization_when_user_is_member() { OrganizationDto organization = db.organizations().insert(o -> o.setSubscription(PAID)); QProfileDto qProfile = db.qualityProfiles().insert(organization, p -> p.setLanguage(XOO1.getKey())); UserDto user = db.users().insertUser(); userSession.logIn(user).addMembership(organization); SearchWsResponse result = call(ws.newRequest().setParam(PARAM_ORGANIZATION, organization.getKey())); assertThat(result.getProfilesList()).extracting(QualityProfile::getKey) .containsExactlyInAnyOrder(qProfile.getKee()); }
@Test public void fail_if_profile_is_not_found_in_specified_organization() { OrganizationDto org1 = db.organizations().insert(); OrganizationDto org2 = db.organizations().insert(); logInAsProfileAdmin(org1); ComponentDto project = db.components().insertPrivateProject(org1); QProfileDto profileInOrg2 = db.qualityProfiles().insert(org2, p -> p.setLanguage(LANGUAGE_1)); expectedException.expect(NotFoundException.class); expectedException .expectMessage("Quality Profile for language '" + LANGUAGE_1 + "' and name '" + profileInOrg2.getName() + "' does not exist in organization '" + org1.getKey() + "'"); call(org1, project, profileInOrg2); }
@Test public void selectByQuery_returns_changes_after_given_date() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee()); query.setFromIncluded(change1.getCreatedAt() + 1); assertThat(underTest.selectByQuery(dbSession, query)) .extracting(QProfileChangeDto::getUuid) .containsExactly(change3.getUuid(), change2.getUuid()); }