@Test public void fail_to_update_custom_when_description_is_empty() { // Template rule RuleDto templateRule = ruleDao.insert(session, RuleTesting.newTemplateRule(RuleKey.of("java", "S001"))); // Custom rule RuleDto customRule = RuleTesting.newCustomRule(templateRule); ruleDao.insert(session, customRule); session.commit(); session.clearCache(); WsTester.TestRequest request = wsTester .newPostRequest("api/rules", "update") .setParam("key", customRule.getKey().toString()) .setParam("name", "My custom rule") .setParam("markdown_description", ""); try { request.execute(); fail(); } catch (Exception e) { assertThat(e) .isInstanceOf(IllegalArgumentException.class) .hasMessage("The description is missing"); } }
@Test public void does_not_return_warnings_when_bulk_activate_on_profile_and_rules_exist_on_another_language_than_profile() throws Exception { QualityProfileDto javaProfile = createProfile("java"); createRule(javaProfile.getLanguage(), "toto"); createRule(javaProfile.getLanguage(), "tata"); QualityProfileDto phpProfile = createProfile("php"); createRule(phpProfile.getLanguage(), "hello"); createRule(phpProfile.getLanguage(), "world"); session.commit(); ruIndexer.index(); // 1. Activate Rule WsTester.TestRequest request = wsTester.newPostRequest( QProfilesWs.API_ENDPOINT, BulkRuleActivationActions.BULK_ACTIVATE_ACTION); request.setParam(RuleActivationActions.PROFILE_KEY, javaProfile.getKey()); request.setParam(PARAM_QPROFILE, javaProfile.getKey()); request.setParam("activation", "false"); request .execute() .assertJson( getClass(), "does_not_return_warnings_when_bulk_activate_on_profile_and_rules_exist_on_another_language_than_profile.json"); session.clearCache(); // 2. Assert ActiveRule in DAO assertThat(db.activeRuleDao().selectByProfileKey(session, javaProfile.getKey())).hasSize(2); }
@Test public void activate_rule_override_severity() throws Exception { QualityProfileDto profile = createProfile("java"); RuleDto rule = createRule(profile.getLanguage(), "toto"); session.commit(); ruIndexer.index(); // 0. Assert No Active Rule for profile assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).isEmpty(); // 1. Activate Rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION); request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey()); request.setParam(RuleActivationActions.RULE_KEY, rule.getKey().toString()); request.setParam(RuleActivationActions.SEVERITY, "MINOR"); WsTester.Result result = request.execute(); session.clearCache(); // 2. Assert ActiveRule in DAO ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), rule.getKey()); assertThat(db.activeRuleDao().selectOrFailByKey(session, activeRuleKey).getSeverityString()) .isEqualTo("MINOR"); }
@Test public void bulk_activate_rule_not_all() throws Exception { QualityProfileDto java = createProfile("java"); QualityProfileDto php = createProfile("php"); createRule(java.getLanguage(), "toto"); createRule(java.getLanguage(), "tata"); createRule(php.getLanguage(), "hello"); createRule(php.getLanguage(), "world"); session.commit(); ruIndexer.index(); // 0. Assert No Active Rule for profile assertThat(db.activeRuleDao().selectByProfileKey(session, php.getKey())).isEmpty(); // 1. Activate Rule WsTester.TestRequest request = wsTester.newPostRequest( QProfilesWs.API_ENDPOINT, BulkRuleActivationActions.BULK_ACTIVATE_ACTION); request.setParam(RuleActivationActions.PROFILE_KEY, php.getKey()); request.setParam(PARAM_LANGUAGES, "php"); request.execute().assertJson(getClass(), "bulk_activate_rule_not_all.json"); session.clearCache(); // 2. Assert ActiveRule in DAO assertThat(db.activeRuleDao().selectByProfileKey(session, php.getKey())).hasSize(2); }
@Test public void returns_full_object_in_response() throws Exception { MetricDto metric = MetricTesting.newMetricDto() .setEnabled(true) .setValueType(ValueType.STRING.name()) .setKey("metric-key"); dbClient.metricDao().insert(dbSession, metric); ComponentDto component = ComponentTesting.newProjectDto("project-uuid").setKey("project-key"); dbClient.componentDao().insert(dbSession, component); CustomMeasureDto customMeasure = newCustomMeasure(component, metric) .setCreatedAt(100_000_000L) .setDescription("custom-measure-description") .setTextValue("text-measure-value"); dbClient.customMeasureDao().insert(dbSession, customMeasure); dbSession.commit(); when(system.now()).thenReturn(123_456_789L); WsTester.Result response = ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION) .setParam(PARAM_ID, String.valueOf(customMeasure.getId())) .setParam(PARAM_DESCRIPTION, "new-custom-measure-description") .setParam(PARAM_VALUE, "new-text-measure-value") .execute(); response.assertJson(getClass(), "custom-measure.json"); String responseAsString = response.outputAsString(); assertThat(responseAsString) .matches(String.format(".*\"id\"\\s*:\\s*\"%s\".*", customMeasure.getId())); assertThat(responseAsString) .matches(String.format(".*\"id\"\\s*:\\s*\"%s\".*", metric.getId())); assertThat(responseAsString).matches(".*createdAt.*updatedAt.*"); }
@Test public void change_project_association_with_key_and_uuid() throws Exception { ComponentDto project = ComponentTesting.newProjectDto("ABCD").setId(1L); db.componentDao().insert(session, project); QualityProfileDto profile1 = QProfileTesting.newXooP1(); QualityProfileDto profile2 = QProfileTesting.newXooP2(); db.qualityProfileDao().insert(session, profile1, profile2); db.qualityProfileDao() .insertProjectProfileAssociation(project.uuid(), profile1.getKey(), session); session.commit(); wsTester .newPostRequest(QProfilesWs.API_ENDPOINT, "add_project") .setParam("profileKey", profile2.getKee()) .setParam("projectUuid", project.uuid()) .execute() .assertNoContent(); assertThat( tester .get(QProfileFactory.class) .getByProjectAndLanguage(session, project.getKey(), "xoo") .getKee()) .isEqualTo(profile2.getKee()); }
@Test public void update_description_only() throws Exception { MetricDto metric = insertNewMetric(ValueType.STRING); ComponentDto component = insertNewProject("project-uuid"); CustomMeasureDto customMeasure = newCustomMeasure(component, metric) .setMetricId(metric.getId()) .setComponentUuid(component.uuid()) .setCreatedAt(system.now()) .setDescription("custom-measure-description") .setTextValue("text-measure-value"); dbClient.customMeasureDao().insert(dbSession, customMeasure); dbSession.commit(); when(system.now()).thenReturn(123_456_789L); ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION) .setParam(PARAM_ID, String.valueOf(customMeasure.getId())) .setParam(PARAM_VALUE, "new-text-measure-value") .execute(); CustomMeasureDto updatedCustomMeasure = dbClient.customMeasureDao().selectOrFail(dbSession, customMeasure.getId()); assertThat(updatedCustomMeasure.getTextValue()).isEqualTo("new-text-measure-value"); assertThat(updatedCustomMeasure.getDescription()).isEqualTo("custom-measure-description"); assertThat(updatedCustomMeasure.getUpdatedAt()).isEqualTo(123_456_789L); assertThat(customMeasure.getCreatedAt()).isEqualTo(updatedCustomMeasure.getCreatedAt()); }
@Test public void fail_if_not_logged_in() throws Exception { userSessionRule.anonymous(); expectedException.expect(UnauthorizedException.class); MetricDto metric = MetricTesting.newMetricDto().setEnabled(true).setValueType(ValueType.STRING.name()); dbClient.metricDao().insert(dbSession, metric); ComponentDto component = ComponentTesting.newProjectDto("project-uuid"); dbClient.componentDao().insert(dbSession, component); CustomMeasureDto customMeasure = newCustomMeasureDto() .setMetricId(metric.getId()) .setComponentUuid(component.uuid()) .setCreatedAt(system.now()) .setDescription("custom-measure-description") .setTextValue("text-measure-value"); dbClient.customMeasureDao().insert(dbSession, customMeasure); dbSession.commit(); ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION) .setParam(PARAM_ID, String.valueOf(customMeasure.getId())) .setParam(PARAM_DESCRIPTION, "new-custom-measure-description") .setParam(PARAM_VALUE, "1984") .execute(); }
@Test(expected = IllegalArgumentException.class) public void add_project_missing_project() throws Exception { wsTester .newPostRequest(QProfilesWs.API_ENDPOINT, "add_project") .setParam("profileKey", "plouf") .execute(); }
@Test public void reset() throws Exception { QualityProfileDto profile = QProfileTesting.newXooP1(); QualityProfileDto subProfile = QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY); db.qualityProfileDao().insert(session, profile, subProfile); RuleDto rule = createRule(profile.getLanguage(), "rule"); ActiveRuleDto active1 = ActiveRuleDto.createFor(profile, rule).setSeverity(rule.getSeverityString()); ActiveRuleDto active2 = ActiveRuleDto.createFor(subProfile, rule).setSeverity("MINOR"); db.activeRuleDao().insert(session, active1); db.activeRuleDao().insert(session, active2); session.commit(); ruIndexer.index(); activeRuIndexer.index(); // 0. assert rule child rule is minor assertThat(db.activeRuleDao().selectOrFailByKey(session, active2.getKey()).getSeverityString()) .isEqualTo("MINOR"); // 1. reset child rule WsTester.TestRequest request = wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, RuleActivationActions.ACTIVATE_ACTION); request.setParam("profile_key", subProfile.getKey()); request.setParam("rule_key", rule.getKey().toString()); request.setParam("reset", "true"); request.execute(); session.clearCache(); // 2. assert rule child rule is NOT minor assertThat(db.activeRuleDao().selectOrFailByKey(session, active2.getKey()).getSeverityString()) .isNotEqualTo("MINOR"); }
@Test public void bulk_deactivate_rule_by_profile() throws Exception { QualityProfileDto profile = createProfile("java"); RuleDto rule0 = createRule(profile.getLanguage(), "hello"); RuleDto rule1 = createRule(profile.getLanguage(), "world"); createActiveRule(rule0, profile); createActiveRule(rule1, profile); session.commit(); ruIndexer.index(); activeRuIndexer.index(); // 0. Assert No Active Rule for profile assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).hasSize(2); // 1. Deactivate Rule WsTester.TestRequest request = wsTester.newPostRequest( QProfilesWs.API_ENDPOINT, BulkRuleActivationActions.BULK_DEACTIVATE_ACTION); request.setParam(RuleActivationActions.PROFILE_KEY, profile.getKey()); request.setParam(WebService.Param.TEXT_QUERY, "hello"); WsTester.Result result = request.execute(); session.clearCache(); // 2. Assert ActiveRule in DAO assertThat(db.activeRuleDao().selectByProfileKey(session, profile.getKey())).hasSize(1); }
@Test public void fail_when_user_login_is_missing() throws Exception { expectedException.expect(IllegalArgumentException.class); ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION) .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) .execute(); }
@Test public void fail_when_permission_is_missing() throws Exception { expectedException.expect(IllegalArgumentException.class); ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION) .setParam(PARAM_USER_LOGIN, "jrr.tolkien") .execute(); }
@Test public void fail_when_project_permission_without_permission() throws Exception { expectedException.expect(BadRequestException.class); ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION) .setParam(PARAM_USER_LOGIN, "ray.bradbury") .setParam(PARAM_PERMISSION, UserRole.ISSUE_ADMIN) .execute(); }
@Test(expected = IllegalArgumentException.class) public void fail_on_missing_name() throws Exception { userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); tester .newPostRequest("api/qualityprofiles", "rename") .setParam("key", "sonar-way-xoo1-13245") .execute(); }
@Test public void fail_if_custom_measure_id_is_missing_in_request() throws Exception { expectedException.expect(IllegalArgumentException.class); ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION) .setParam(PARAM_DESCRIPTION, "new-custom-measure-description") .setParam(PARAM_VALUE, "1984") .execute(); }
@Test public void fail_if_custom_measure_value_and_description_are_missing_in_request() throws Exception { expectedException.expect(IllegalArgumentException.class); ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION) .setParam(PARAM_ID, "42") .execute(); }
@Test(expected = NotFoundException.class) public void add_project_unknown_profile() throws Exception { wsTester .newPostRequest(QProfilesWs.API_ENDPOINT, "add_project") .setParam("projectUuid", "plouf") .setParam("profileName", "polop") .setParam("language", "xoo") .execute(); }
@Test(expected = IllegalArgumentException.class) public void add_project_too_many_project_parameters() throws Exception { wsTester .newPostRequest(QProfilesWs.API_ENDPOINT, "add_project") .setParam("profileKey", "plouf") .setParam("projectUuid", "polop") .setParam("projectKey", "palap") .execute(); }
@Test(expected = BadRequestException.class) public void do_nothing_on_conflict() throws Exception { userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); tester .newPostRequest("api/qualityprofiles", "rename") .setParam("key", "sonar-way-xoo2-23456") .setParam("name", "My Sonar way") .execute(); }
@Test(expected = ForbiddenException.class) public void fail_on_missing_permission() throws Exception { userSessionRule.login("obiwan"); tester .newPostRequest("api/qualityprofiles", "rename") .setParam("key", "sonar-way-xoo1-13245") .setParam("name", "Hey look I am not quality profile admin!") .execute(); }
@Test(expected = NotFoundException.class) public void fail_on_unknown_profile() throws Exception { userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); tester .newPostRequest("api/qualityprofiles", "rename") .setParam("key", "polop") .setParam("name", "Uh oh, I don't know this profile") .execute(); }
@Test public void fail_when_project_does_not_exist() throws Exception { expectedException.expect(NotFoundException.class); ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION) .setParam(PARAM_USER_LOGIN, "ray.bradbury") .setParam(PARAM_PROJECT_UUID, "unknown-project-uuid") .setParam(PARAM_PERMISSION, UserRole.ISSUE_ADMIN) .execute(); }
@Test public void fail_when_component_is_not_a_project() throws Exception { expectedException.expect(BadRequestException.class); insertComponent(newFileDto(newProjectDto(), "file-uuid")); ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION) .setParam(PARAM_USER_LOGIN, "ray.bradbury") .setParam(PARAM_PROJECT_UUID, "file-uuid") .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) .execute(); }
@Test public void fail_if_not_in_db() throws Exception { expectedException.expect(RowNotFoundException.class); expectedException.expectMessage("Custom measure '42' not found."); ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION) .setParam(PARAM_ID, "42") .setParam(PARAM_DESCRIPTION, "new-custom-measure-description") .setParam(PARAM_VALUE, "1984") .execute(); }
@Test public void call_permission_service_with_right_data() throws Exception { ws.newPostRequest(PermissionsWs.ENDPOINT, ACTION) .setParam(PARAM_USER_LOGIN, "ray.bradbury") .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) .execute(); verify(permissionUpdater).removePermission(permissionChangeCaptor.capture()); PermissionChange permissionChange = permissionChangeCaptor.getValue(); assertThat(permissionChange.userLogin()).isEqualTo("ray.bradbury"); assertThat(permissionChange.permission()).isEqualTo(SYSTEM_ADMIN); }
@Test public void fail_when_project_uuid_and_project_key_are_provided() throws Exception { expectedException.expect(BadRequestException.class); expectedException.expectMessage("Project id or project key can be provided, not both."); insertComponent(newProjectDto("project-uuid").setKey("project-key")); ws.newPostRequest(ENDPOINT, ACTION) .setParam(PARAM_PERMISSION, SYSTEM_ADMIN) .setParam(PARAM_USER_LOGIN, "ray.bradbury") .setParam(PARAM_PROJECT_UUID, "project-uuid") .setParam(PARAM_PROJECT_KEY, "project-key") .execute(); }
@Test public void unknown_user() throws Exception { GroupDto group = insertGroup("admins"); session.commit(); expectedException.expect(NotFoundException.class); userSession.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); tester .newPostRequest("api/usergroups", "add_user") .setParam("id", group.getId().toString()) .setParam("login", "my-admin") .execute(); }
@Test public void unknown_group() throws Exception { UserDto user = insertUser("my-admin"); session.commit(); expectedException.expect(NotFoundException.class); userSession.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); tester .newPostRequest("api/usergroups", "add_user") .setParam("id", "42") .setParam("login", user.getLogin()) .execute(); }
@Test public void rename_nominal() throws Exception { userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); tester .newPostRequest("api/qualityprofiles", "rename") .setParam("key", "sonar-way-xoo2-23456") .setParam("name", "Other Sonar Way") .execute() .assertNoContent(); assertThat(qualityProfileDao.selectOrFailByKey(session, "sonar-way-xoo2-23456").getName()) .isEqualTo("Other Sonar Way"); }