From: Simon Brandhof Date: Thu, 15 Jun 2017 09:43:35 +0000 (+0200) Subject: Complete SearchActionTest X-Git-Tag: 6.5-M2~137 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=562b101e487da9b9e646f37fad33222b6456f592;p=sonarqube.git Complete SearchActionTest --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java index 3ac1a3ce177..f0de64894ce 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java @@ -21,581 +21,224 @@ package org.sonar.server.rule.ws; public class SearchActionMediumTest { -// @ClassRule -// public static ServerTester tester = new ServerTester().withEsIndexes().addXoo(); -// @Rule -// public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); -// @Rule -// public ExpectedException thrown = ExpectedException.none(); -// -// private static final String API_ENDPOINT = "api/rules"; -// private static final String API_SEARCH_METHOD = "search"; -// -// private DbClient db; -// private RuleDao ruleDao; -// private DbSession dbSession; -// private RuleIndexer ruleIndexer; -// private ActiveRuleIndexer activeRuleIndexer; -// private OrganizationDto defaultOrganizationDto; -// -// @Before -// public void setUp() { -// tester.clearDbAndIndexes(); -// db = tester.get(DbClient.class); -// ruleDao = tester.get(RuleDao.class); -// dbSession = tester.get(DbClient.class).openSession(false); -// ruleIndexer = tester.get(RuleIndexer.class); -// activeRuleIndexer = tester.get(ActiveRuleIndexer.class); -// DefaultOrganization defaultOrganization = tester.get(DefaultOrganizationProvider.class).get(); -// this.defaultOrganizationDto = db.organizationDao().selectByUuid(dbSession, defaultOrganization.getUuid()).get(); -// } -// -// @After -// public void after() { -// dbSession.close(); -// } -// -// @Test -// public void search_no_rules() throws Exception { -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, "actives"); -// WsTester.Result result = request.execute(); -// -// result.assertJson(this.getClass(), "search_no_rules.json"); -// } -// -// @Test -// public void filter_by_key_rules() throws Exception { -// insertRule(RuleTesting.newXooX1().getDefinition()); -// insertRule(RuleTesting.newXooX2().getDefinition()); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(PARAM_RULE_KEY, RuleTesting.XOO_X1.toString()); -// request.setParam(WebService.Param.FIELDS, "actives"); -// WsTester.Result result = request.execute(); -// result.assertJson("{\"total\":1,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x1\"}]}"); -// -// request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(PARAM_RULE_KEY, RuleKey.of("xoo", "unknown").toString()); -// request.setParam(WebService.Param.FIELDS, "actives"); -// result = request.execute(); -// result.assertJson("{\"total\":0,\"p\":1,\"ps\":100,\"rules\":[],\"actives\":{}}"); -// } -// -// @Test -// public void search_2_rules() throws Exception { -// RuleDto rule1 = RuleTesting.newXooX1(defaultOrganizationDto) -// .setType(RuleType.BUG); -// insertRule(rule1.getDefinition()); -// ruleDao.insertOrUpdate(dbSession, rule1.getMetadata().setRuleId(rule1.getId())); -// RuleDto rule2 = RuleTesting.newXooX2(defaultOrganizationDto) -// .setType(RuleType.VULNERABILITY); -// insertRule(rule2.getDefinition()); -// ruleDao.insertOrUpdate(dbSession, rule2.getMetadata().setRuleId(rule2.getId())); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// WsTester.Result result = request.execute(); -// -// result.assertJson(getClass(), "search_2_rules.json"); -// } -// -// @Test -// public void search_2_rules_with_fields_selection() throws Exception { -// insertRule(RuleTesting.newXooX1() -// .setType(RuleType.CODE_SMELL) -// .getDefinition()); -// insertRule(RuleTesting.newXooX2() -// .setType(RuleType.BUG) -// .setDescription("A *Xoo* rule") -// .setDescriptionFormat(RuleDto.Format.MARKDOWN) -// .getDefinition()); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "name,htmlDesc,mdDesc"); -// WsTester.Result result = request.execute(); -// -// result.assertJson(getClass(), "search_2_rules_fields.json"); -// } -// -// @Test -// public void return_mandatory_fields_even_when_setting_f_param() throws Exception { -// insertRule(RuleTesting.newXooX1() -// .setName("Rule x1") -// .setType(RuleType.CODE_SMELL) -// .getDefinition()); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "name"); -// WsTester.Result result = request.execute(); -// -// result.assertJson(getClass(), "return_mandatory_fields_even_when_setting_f_param.json"); -// } -// -// @Test -// public void return_lang_field() throws Exception { -// insertRule(RuleTesting.newXooX1().getDefinition()); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "lang"); -// WsTester.Result result = request.execute(); -// -// result.assertJson("{\"total\":1,\"p\":1,\"ps\":100," + -// "\"rules\":[{\"key\":\"xoo:x1\",\"lang\":\"xoo\"}]}"); -// assertThat(result.outputAsString()).doesNotContain("\"langName\""); -// assertThat(result.outputAsString()).doesNotContain("\"name\""); -// } -// -// @Test -// public void return_lang_name_field() throws Exception { -// insertRule(RuleTesting.newXooX1().getDefinition()); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "langName"); -// WsTester.Result result = request.execute(); -// -// result.assertJson("{\"total\":1,\"p\":1,\"ps\":100," + -// "\"rules\":[{\"key\":\"xoo:x1\",\"langName\":\"Xoo\"}]}"); -// assertThat(result.outputAsString()).doesNotContain("\"lang\""); -// assertThat(result.outputAsString()).doesNotContain("\"name\""); -// } -// -// @Test -// public void return_lang_key_field_when_language_name_is_not_available() throws Exception { -// insertRule(RuleTesting.newRule(RuleKey.of("other", "rule")).setLanguage("unknown")); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "langName"); -// WsTester.Result result = request.execute(); -// -// result.assertJson("{\"total\":1,\"p\":1,\"ps\":100," + -// "\"rules\":[{\"key\":\"other:rule\",\"langName\":\"unknown\"}]}"); -// } -// -// @Test -// public void search_debt_rules_with_default_and_overridden_debt_values() throws Exception { -// RuleDto ruleDto = RuleTesting.newXooX1(defaultOrganizationDto) -// .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) -// .setDefRemediationGapMultiplier("1h") -// .setDefRemediationBaseEffort("15min") -// .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) -// .setRemediationGapMultiplier("2h") -// .setRemediationBaseEffort("25min"); -// insertRule(ruleDto.getDefinition()); -// ruleDao.insertOrUpdate(dbSession, ruleDto.getMetadata().setRuleId(ruleDto.getId())); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, "debtRemFn,debtOverloaded,defaultDebtRemFn"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "search_debt_rules_with_default_and_overridden_debt_values.json"); -// } -// -// @Test -// public void search_debt_rules_with_default_linear_offset_and_overridden_constant_debt() throws Exception { -// RuleDto ruleDto = RuleTesting.newXooX1(defaultOrganizationDto) -// .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) -// .setDefRemediationGapMultiplier("1h") -// .setDefRemediationBaseEffort("15min") -// .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()) -// .setRemediationGapMultiplier(null) -// .setRemediationBaseEffort("5min"); -// insertRule(ruleDto.getDefinition()); -// ruleDao.insertOrUpdate(dbSession, ruleDto.getMetadata().setRuleId(ruleDto.getId())); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, "debtRemFn,debtOverloaded,defaultDebtRemFn"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json"); -// } -// -// @Test -// public void search_debt_rules_with_default_linear_offset_and_overridden_linear_debt() throws Exception { -// RuleDto ruleDto = RuleTesting.newXooX1(defaultOrganizationDto) -// .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) -// .setDefRemediationGapMultiplier("1h") -// .setDefRemediationBaseEffort("15min") -// .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) -// .setRemediationGapMultiplier("1h") -// .setRemediationBaseEffort(null); -// insertRule(ruleDto.getDefinition()); -// ruleDao.insertOrUpdate(dbSession, ruleDto.getMetadata().setRuleId(ruleDto.getId())); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, "debtRemFn,debtOverloaded,defaultDebtRemFn"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json"); -// } -// -// @Test -// public void search_template_rules() throws Exception { -// RuleDto templateRule = RuleTesting.newXooX1().setIsTemplate(true); -// insertRule(templateRule.getDefinition()); -// RuleDto rule = RuleTesting.newXooX2(); -// rule.setTemplateId(templateRule.getId()); -// insertRule(rule.getDefinition()); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, "isTemplate"); -// request.setParam(PARAM_IS_TEMPLATE, "true"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "search_template_rules.json"); -// } -// -// @Test -// public void search_custom_rules_from_template_key() throws Exception { -// RuleDto templateRule = RuleTesting.newXooX1().setIsTemplate(true); -// insertRule(templateRule.getDefinition()); -// insertRule(RuleTesting.newXooX2().setTemplateId(templateRule.getId()).getDefinition()); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, "templateKey"); -// request.setParam(PARAM_TEMPLATE_KEY, "xoo:x1"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "search_rules_from_template_key.json"); -// } -// -// @Test -// public void search_all_active_rules() throws Exception { -// QProfileDto profile = QProfileTesting.newXooP1(defaultOrganizationDto); -// tester.get(QualityProfileDao.class).insert(dbSession, profile); -// -// RuleDefinitionDto rule = RuleTesting.newXooX1().getDefinition(); -// insertRule(rule); -// -// ActiveRuleDto activeRule = newActiveRule(profile, rule); -// tester.get(ActiveRuleDao.class).insert(dbSession, activeRule); -// -// dbSession.commit(); -// -// activeRuleIndexer.index(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.TEXT_QUERY, "x1"); -// request.setParam(PARAM_ACTIVATION, "true"); -// request.setParam(WebService.Param.FIELDS, ""); -// request.setParam("organization", defaultOrganizationDto.getKey()); -// WsTester.Result result = request.execute(); -// -// result.assertJson(this.getClass(), "search_active_rules.json"); -// } -// -// @Test -// public void search_profile_active_rules() throws Exception { -// QProfileDto profile = QProfileTesting.newXooP1(defaultOrganizationDto); -// tester.get(QualityProfileDao.class).insert(dbSession, profile); -// -// QProfileDto profile2 = QProfileTesting.newXooP2(defaultOrganizationDto); -// tester.get(QualityProfileDao.class).insert(dbSession, profile2); -// -// dbSession.commit(); -// -// RuleDefinitionDto rule = RuleTesting.newXooX1().getDefinition(); -// insertRule(rule); -// -// RuleParamDto param = RuleParamDto.createFor(rule) -// .setDefaultValue("some value") -// .setType("string") -// .setDescription("My small description") -// .setName("my_var"); -// ruleDao.insertRuleParam(dbSession, rule, param); -// -// RuleParamDto param2 = RuleParamDto.createFor(rule) -// .setDefaultValue("other value") -// .setType("integer") -// .setDescription("My small description") -// .setName("the_var"); -// ruleDao.insertRuleParam(dbSession, rule, param2); -// -// // SONAR-7083 -// RuleParamDto param3 = RuleParamDto.createFor(rule) -// .setDefaultValue(null) -// .setType("string") -// .setDescription("Empty Param") -// .setName("empty_var"); -// ruleDao.insertRuleParam(dbSession, rule, param3); -// -// ActiveRuleDto activeRule = newActiveRule(profile, rule); -// tester.get(ActiveRuleDao.class).insert(dbSession, activeRule); -// ActiveRuleDto activeRule2 = newActiveRule(profile2, rule); -// tester.get(ActiveRuleDao.class).insert(dbSession, activeRule2); -// -// ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param) -// .setValue("The VALUE"); -// tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule2, activeRuleParam); -// -// ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2) -// .setValue("The Other Value"); -// tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule2, activeRuleParam2); -// -// ActiveRuleParamDto activeRuleParam3 = ActiveRuleParamDto.createFor(param3) -// .setValue(null); -// tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule2, activeRuleParam3); -// -// dbSession.commit(); -// -// activeRuleIndexer.index(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.TEXT_QUERY, "x1"); -// request.setParam(PARAM_ACTIVATION, "true"); -// request.setParam(PARAM_QPROFILE, profile2.getKee()); -// request.setParam(WebService.Param.FIELDS, "actives"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "search_profile_active_rules.json"); -// -// WsTester.TestRequest request2 = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD) -// .setParam(PARAM_ACTIVATION, "true") -// .setParam(PARAM_QPROFILE, "unknown_profile") -// .setParam(WebService.Param.FIELDS, "actives"); -// -// thrown.expect(NotFoundException.class); -// thrown.expectMessage("The specified qualityProfile 'unknown_profile' does not exist"); -// -// request2.execute(); -// } -// -// @Test -// public void search_profile_active_rules_with_inheritance() throws Exception { -// QProfileDto profile = QProfileTesting.newXooP1(defaultOrganizationDto); -// tester.get(QualityProfileDao.class).insert(dbSession, profile); -// -// QProfileDto profile2 = QProfileTesting.newXooP2(defaultOrganizationDto).setParentKee(profile.getKee()); -// tester.get(QualityProfileDao.class).insert(dbSession, profile2); -// -// dbSession.commit(); -// -// RuleDefinitionDto rule = RuleTesting.newXooX1().getDefinition(); -// insertRule(rule); -// -// ActiveRuleDto activeRule = newActiveRule(profile, rule); -// tester.get(ActiveRuleDao.class).insert(dbSession, activeRule); -// ActiveRuleDto activeRule2 = newActiveRule(profile2, rule).setInheritance(ActiveRuleDto.OVERRIDES).setSeverity(Severity.CRITICAL); -// tester.get(ActiveRuleDao.class).insert(dbSession, activeRule2); -// -// dbSession.commit(); -// -// activeRuleIndexer.index(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.TEXT_QUERY, "x1"); -// request.setParam(PARAM_ACTIVATION, "true"); -// request.setParam(PARAM_QPROFILE, profile2.getKee()); -// request.setParam(WebService.Param.FIELDS, "actives"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "search_profile_active_rules_inheritance.json"); -// } -// -// @Test -// public void search_all_active_rules_params() throws Exception { -// QProfileDto profile = QProfileTesting.newXooP1(defaultOrganizationDto); -// tester.get(QualityProfileDao.class).insert(dbSession, profile); -// RuleDefinitionDto rule = RuleTesting.newXooX1().getDefinition(); -// insertRule(rule); -// dbSession.commit(); -// -// RuleParamDto param = RuleParamDto.createFor(rule) -// .setDefaultValue("some value") -// .setType("string") -// .setDescription("My small description") -// .setName("my_var"); -// ruleDao.insertRuleParam(dbSession, rule, param); -// -// RuleParamDto param2 = RuleParamDto.createFor(rule) -// .setDefaultValue("other value") -// .setType("integer") -// .setDescription("My small description") -// .setName("the_var"); -// ruleDao.insertRuleParam(dbSession, rule, param2); -// -// ActiveRuleDto activeRule = newActiveRule(profile, rule); -// tester.get(ActiveRuleDao.class).insert(dbSession, activeRule); -// -// ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param) -// .setValue("The VALUE"); -// tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam); -// -// ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2) -// .setValue("The Other Value"); -// tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam2); -// -// dbSession.commit(); -// -// activeRuleIndexer.index(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.TEXT_QUERY, "x1"); -// request.setParam(PARAM_ACTIVATION, "true"); -// request.setParam(WebService.Param.FIELDS, "params"); -// WsTester.Result result = request.execute(); -// -// result.assertJson(this.getClass(), "search_active_rules_params.json"); -// } -// -// @Test -// public void get_note_as_markdown_and_html() throws Exception { -// QProfileDto profile = QProfileTesting.newXooP1("org-123"); -// tester.get(QualityProfileDao.class).insert(dbSession, profile); -// RuleDto rule = RuleTesting.newXooX1(defaultOrganizationDto).setNoteData("this is *bold*"); -// insertRule(rule.getDefinition()); -// ruleDao.insertOrUpdate(dbSession, rule.getMetadata().setRuleId(rule.getId())); -// -// dbSession.commit(); -// -// activeRuleIndexer.index(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, "htmlNote, mdNote"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "get_note_as_markdown_and_html.json"); -// } -// -// @Test -// public void filter_by_tags() throws Exception { -// insertRule(RuleTesting.newRule() -// .setRepositoryKey("xoo").setRuleKey("x1") -// .setSystemTags(ImmutableSet.of("tag1"))); -// insertRule(RuleTesting.newRule() -// .setSystemTags(ImmutableSet.of("tag2"))); -// -// activeRuleIndexer.index(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(PARAM_TAGS, "tag1"); -// request.setParam(WebService.Param.FIELDS, "sysTags, tags"); -// request.setParam(WebService.Param.FACETS, "tags"); -// WsTester.Result result = request.execute(); -// result.assertJson(this.getClass(), "filter_by_tags.json"); -// } -// -// @Test -// public void severities_facet_should_have_all_severities() throws Exception { -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FACETS, "severities"); -// request.execute().assertJson(this.getClass(), "severities_facet.json"); -// } -// -// @Test -// public void statuses_facet_should_have_all_statuses_except_removed() throws Exception { -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FACETS, "statuses"); -// request.execute().assertJson(this.getClass(), "statuses_facet.json"); -// } -// -// @Test -// public void statuses_facet_should_be_sticky() throws Exception { -// RuleDefinitionDto definition = RuleTesting.newXooX1().getDefinition(); -// insertRule(definition); -// insertRule(RuleTesting.newXooX2().setStatus(RuleStatus.BETA).getDefinition()); -// insertRule(RuleTesting.newXooX3().setStatus(RuleStatus.DEPRECATED).getDefinition()); -// -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(PARAM_STATUSES, "DEPRECATED"); -// request.setParam(WebService.Param.FACETS, "statuses"); -// request.execute().assertJson(this.getClass(), "statuses_facet_sticky.json"); -// } -// -// @Test -// public void sort_by_name() throws Exception { -// insertRule(RuleTesting.newXooX1() -// .setName("Dodgy - Consider returning a zero length array rather than null ") -// .getDefinition()); -// insertRule(RuleTesting.newXooX2() -// .setName("Bad practice - Creates an empty zip file entry") -// .getDefinition()); -// insertRule(RuleTesting.newXooX3() -// .setName("XPath rule") -// .getDefinition()); -// -// dbSession.commit(); -// -// // 1. Sort Name Asc -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, ""); -// request.setParam(WebService.Param.SORT, "name"); -// request.setParam(WebService.Param.ASCENDING, "true"); -// -// WsTester.Result result = request.execute(); -// result.assertJson("{\"total\":3,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x2\"},{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x3\"}]}"); -// -// // 2. Sort Name DESC -// request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, ""); -// request.setParam(WebService.Param.SORT, RuleIndexDefinition.FIELD_RULE_NAME); -// request.setParam(WebService.Param.ASCENDING, "false"); -// -// result = request.execute(); -// result.assertJson("{\"total\":3,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x3\"},{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x2\"}]}"); -// } -// -// @Test -// public void available_since() throws Exception { -// Date since = new Date(); -// insertRule(RuleTesting.newXooX1() -// .setUpdatedAt(since.getTime()) -// .setCreatedAt(since.getTime()) -// .getDefinition()); -// insertRule(RuleTesting.newXooX2() -// .setUpdatedAt(since.getTime()) -// .setCreatedAt(since.getTime()) -// .getDefinition()); -// -// dbSession.commit(); -// dbSession.clearCache(); -// -// // 1. find today's rules -// WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, ""); -// request.setParam(PARAM_AVAILABLE_SINCE, DateUtils.formatDate(since)); -// request.setParam(WebService.Param.SORT, RuleIndexDefinition.FIELD_RULE_KEY); -// WsTester.Result result = request.execute(); -// result.assertJson("{\"total\":2,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x2\"}]}"); -// -// // 2. no rules since tomorrow -// request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); -// request.setParam(WebService.Param.FIELDS, ""); -// request.setParam(PARAM_AVAILABLE_SINCE, DateUtils.formatDate(DateUtils.addDays(since, 1))); -// result = request.execute(); -// result.assertJson("{\"total\":0,\"p\":1,\"ps\":100,\"rules\":[]}"); -// } -// -// @Test -// public void search_rules_with_deprecated_fields() throws Exception { -// RuleDto ruleDto = RuleTesting.newXooX1(defaultOrganizationDto) -// .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) -// .setDefRemediationGapMultiplier("1h") -// .setDefRemediationBaseEffort("15min") -// .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) -// .setRemediationGapMultiplier("2h") -// .setRemediationBaseEffort("25min"); -// insertRule(ruleDto.getDefinition()); -// ruleDao.insertOrUpdate(dbSession, ruleDto.getMetadata().setRuleId(ruleDto.getId())); -// dbSession.commit(); -// -// WsTester.TestRequest request = tester.wsTester() -// .newGetRequest(API_ENDPOINT, API_SEARCH_METHOD) -// .setParam(WebService.Param.FIELDS, "name,defaultDebtRemFn,debtRemFn,effortToFixDescription,debtOverloaded"); -// WsTester.Result result = request.execute(); -// -// result.assertJson(getClass(), "search_rules_with_deprecated_fields.json"); -// } -// -// private ActiveRuleDto newActiveRule(QProfileDto profile, RuleDefinitionDto rule) { -// return ActiveRuleDto.createFor(profile, rule) -// .setInheritance(null) -// .setSeverity("BLOCKER"); -// } -// -// private void insertRule(RuleDefinitionDto definition) { -// ruleDao.insert(dbSession, definition); -// dbSession.commit(); -// ruleIndexer.indexRuleDefinition(definition.getKey()); -// } + + // + // @Test + // public void search_profile_active_rules_with_inheritance() throws Exception { + // QProfileDto profile = QProfileTesting.newXooP1(defaultOrganizationDto); + // tester.get(QualityProfileDao.class).insert(dbSession, profile); + // + // QProfileDto profile2 = QProfileTesting.newXooP2(defaultOrganizationDto).setParentKee(profile.getKee()); + // tester.get(QualityProfileDao.class).insert(dbSession, profile2); + // + // dbSession.commit(); + // + // RuleDefinitionDto rule = RuleTesting.newXooX1().getDefinition(); + // insertRule(rule); + // + // ActiveRuleDto activeRule = newActiveRule(profile, rule); + // tester.get(ActiveRuleDao.class).insert(dbSession, activeRule); + // ActiveRuleDto activeRule2 = newActiveRule(profile2, rule).setInheritance(ActiveRuleDto.OVERRIDES).setSeverity(Severity.CRITICAL); + // tester.get(ActiveRuleDao.class).insert(dbSession, activeRule2); + // + // dbSession.commit(); + // + // activeRuleIndexer.index(); + // + // WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(WebService.Param.TEXT_QUERY, "x1"); + // request.setParam(PARAM_ACTIVATION, "true"); + // request.setParam(PARAM_QPROFILE, profile2.getKee()); + // request.setParam(WebService.Param.FIELDS, "actives"); + // WsTester.Result result = request.execute(); + // result.assertJson(this.getClass(), "search_profile_active_rules_inheritance.json"); + // } + // + // @Test + // public void search_all_active_rules_params() throws Exception { + // QProfileDto profile = QProfileTesting.newXooP1(defaultOrganizationDto); + // tester.get(QualityProfileDao.class).insert(dbSession, profile); + // RuleDefinitionDto rule = RuleTesting.newXooX1().getDefinition(); + // insertRule(rule); + // dbSession.commit(); + // + // RuleParamDto param = RuleParamDto.createFor(rule) + // .setDefaultValue("some value") + // .setType("string") + // .setDescription("My small description") + // .setName("my_var"); + // ruleDao.insertRuleParam(dbSession, rule, param); + // + // RuleParamDto param2 = RuleParamDto.createFor(rule) + // .setDefaultValue("other value") + // .setType("integer") + // .setDescription("My small description") + // .setName("the_var"); + // ruleDao.insertRuleParam(dbSession, rule, param2); + // + // ActiveRuleDto activeRule = newActiveRule(profile, rule); + // tester.get(ActiveRuleDao.class).insert(dbSession, activeRule); + // + // ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(param) + // .setValue("The VALUE"); + // tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam); + // + // ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(param2) + // .setValue("The Other Value"); + // tester.get(ActiveRuleDao.class).insertParam(dbSession, activeRule, activeRuleParam2); + // + // dbSession.commit(); + // + // activeRuleIndexer.index(); + // + // WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(WebService.Param.TEXT_QUERY, "x1"); + // request.setParam(PARAM_ACTIVATION, "true"); + // request.setParam(WebService.Param.FIELDS, "params"); + // WsTester.Result result = request.execute(); + // + // result.assertJson(this.getClass(), "search_active_rules_params.json"); + // } + // + // @Test + // public void get_note_as_markdown_and_html() throws Exception { + // QProfileDto profile = QProfileTesting.newXooP1("org-123"); + // tester.get(QualityProfileDao.class).insert(dbSession, profile); + // RuleDto rule = RuleTesting.newXooX1(defaultOrganizationDto).setNoteData("this is *bold*"); + // insertRule(rule.getDefinition()); + // ruleDao.insertOrUpdate(dbSession, rule.getMetadata().setRuleId(rule.getId())); + // + // dbSession.commit(); + // + // activeRuleIndexer.index(); + // + // WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(WebService.Param.FIELDS, "htmlNote, mdNote"); + // WsTester.Result result = request.execute(); + // result.assertJson(this.getClass(), "get_note_as_markdown_and_html.json"); + // } + // + // @Test + // public void filter_by_tags() throws Exception { + // insertRule(RuleTesting.newRule() + // .setRepositoryKey("xoo").setRuleKey("x1") + // .setSystemTags(ImmutableSet.of("tag1"))); + // insertRule(RuleTesting.newRule() + // .setSystemTags(ImmutableSet.of("tag2"))); + // + // activeRuleIndexer.index(); + // + // WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(PARAM_TAGS, "tag1"); + // request.setParam(WebService.Param.FIELDS, "sysTags, tags"); + // request.setParam(WebService.Param.FACETS, "tags"); + // WsTester.Result result = request.execute(); + // result.assertJson(this.getClass(), "filter_by_tags.json"); + // } + // + // @Test + // public void severities_facet_should_have_all_severities() throws Exception { + // WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(WebService.Param.FACETS, "severities"); + // request.execute().assertJson(this.getClass(), "severities_facet.json"); + // } + // + // + // + // @Test + // public void sort_by_name() throws Exception { + // insertRule(RuleTesting.newXooX1() + // .setName("Dodgy - Consider returning a zero length array rather than null ") + // .getDefinition()); + // insertRule(RuleTesting.newXooX2() + // .setName("Bad practice - Creates an empty zip file entry") + // .getDefinition()); + // insertRule(RuleTesting.newXooX3() + // .setName("XPath rule") + // .getDefinition()); + // + // dbSession.commit(); + // + // // 1. Sort Name Asc + // WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(WebService.Param.FIELDS, ""); + // request.setParam(WebService.Param.SORT, "name"); + // request.setParam(WebService.Param.ASCENDING, "true"); + // + // WsTester.Result result = request.execute(); + // result.assertJson("{\"total\":3,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x2\"},{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x3\"}]}"); + // + // // 2. Sort Name DESC + // request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(WebService.Param.FIELDS, ""); + // request.setParam(WebService.Param.SORT, RuleIndexDefinition.FIELD_RULE_NAME); + // request.setParam(WebService.Param.ASCENDING, "false"); + // + // result = request.execute(); + // result.assertJson("{\"total\":3,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x3\"},{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x2\"}]}"); + // } + // + // @Test + // public void available_since() throws Exception { + // Date since = new Date(); + // insertRule(RuleTesting.newXooX1() + // .setUpdatedAt(since.getTime()) + // .setCreatedAt(since.getTime()) + // .getDefinition()); + // insertRule(RuleTesting.newXooX2() + // .setUpdatedAt(since.getTime()) + // .setCreatedAt(since.getTime()) + // .getDefinition()); + // + // dbSession.commit(); + // dbSession.clearCache(); + // + // // 1. find today's rules + // WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(WebService.Param.FIELDS, ""); + // request.setParam(PARAM_AVAILABLE_SINCE, DateUtils.formatDate(since)); + // request.setParam(WebService.Param.SORT, RuleIndexDefinition.FIELD_RULE_KEY); + // WsTester.Result result = request.execute(); + // result.assertJson("{\"total\":2,\"p\":1,\"ps\":100,\"rules\":[{\"key\":\"xoo:x1\"},{\"key\":\"xoo:x2\"}]}"); + // + // // 2. no rules since tomorrow + // request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD); + // request.setParam(WebService.Param.FIELDS, ""); + // request.setParam(PARAM_AVAILABLE_SINCE, DateUtils.formatDate(DateUtils.addDays(since, 1))); + // result = request.execute(); + // result.assertJson("{\"total\":0,\"p\":1,\"ps\":100,\"rules\":[]}"); + // } + // + // @Test + // public void search_rules_with_deprecated_fields() throws Exception { + // RuleDto ruleDto = RuleTesting.newXooX1(defaultOrganizationDto) + // .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + // .setDefRemediationGapMultiplier("1h") + // .setDefRemediationBaseEffort("15min") + // .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + // .setRemediationGapMultiplier("2h") + // .setRemediationBaseEffort("25min"); + // insertRule(ruleDto.getDefinition()); + // ruleDao.insertOrUpdate(dbSession, ruleDto.getMetadata().setRuleId(ruleDto.getId())); + // dbSession.commit(); + // + // WsTester.TestRequest request = tester.wsTester() + // .newGetRequest(API_ENDPOINT, API_SEARCH_METHOD) + // .setParam(WebService.Param.FIELDS, "name,defaultDebtRemFn,debtRemFn,effortToFixDescription,debtOverloaded"); + // WsTester.Result result = request.execute(); + // + // result.assertJson(getClass(), "search_rules_with_deprecated_fields.json"); + // } + // + // private ActiveRuleDto newActiveRule(QProfileDto profile, RuleDefinitionDto rule) { + // return ActiveRuleDto.createFor(profile, rule) + // .setInheritance(null) + // .setSeverity("BLOCKER"); + // } + // + // private void insertRule(RuleDefinitionDto definition) { + // ruleDao.insert(dbSession, definition); + // dbSession.commit(); + // ruleIndexer.indexRuleDefinition(definition.getKey()); + // } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java index 7b51abbf0cd..8b2c610a6dd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java @@ -21,62 +21,99 @@ package org.sonar.server.rule.ws; import java.io.IOException; +import java.util.List; import java.util.function.Consumer; import java.util.stream.Collectors; import org.assertj.core.api.iterable.Extractor; import org.junit.Before; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.config.MapSettings; import org.sonar.api.resources.Languages; -import org.sonar.db.DbClient; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.RuleStatus; +import org.sonar.api.rules.RuleType; +import org.sonar.api.server.debt.DebtRemediationFunction; +import org.sonar.api.server.ws.WebService; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.internal.AlwaysIncreasingSystem2; +import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbTester; import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.qualityprofile.ActiveRuleParamDto; +import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.rule.RuleDefinitionDto; import org.sonar.db.rule.RuleMetadataDto; -import org.sonar.server.es.EsClient; +import org.sonar.db.rule.RuleParamDto; import org.sonar.server.es.EsTester; +import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.language.LanguageTesting; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; +import org.sonar.server.qualityprofile.ActiveRuleChange; +import org.sonar.server.qualityprofile.RuleActivation; +import org.sonar.server.qualityprofile.RuleActivator; +import org.sonar.server.qualityprofile.RuleActivatorContextFactory; +import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; +import org.sonar.server.qualityprofile.index.ActiveRuleIteratorFactory; import org.sonar.server.rule.index.RuleIndex; import org.sonar.server.rule.index.RuleIndexDefinition; import org.sonar.server.rule.index.RuleIndexer; +import org.sonar.server.tester.UserSessionRule; import org.sonar.server.text.MacroInterpreter; -import org.sonar.server.ws.WsAction; +import org.sonar.server.util.IntegerTypeValidation; +import org.sonar.server.util.StringTypeValidation; +import org.sonar.server.util.TypeValidations; +import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Rules; import org.sonarqube.ws.Rules.Rule; import org.sonarqube.ws.Rules.SearchResponse; +import static java.util.Arrays.asList; +import static java.util.Arrays.stream; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.assertj.guava.api.Assertions.entry; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.sonar.api.rule.Severity.BLOCKER; import static org.sonar.db.rule.RuleTesting.setSystemTags; import static org.sonar.db.rule.RuleTesting.setTags; +import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_RULE_KEY; public class SearchActionTest { @org.junit.Rule - public DbTester dbTester = DbTester.create(); + public UserSessionRule userSession = UserSessionRule.standalone(); @org.junit.Rule - public EsTester esTester = new EsTester( - new RuleIndexDefinition(new MapSettings())); + public ExpectedException thrown = ExpectedException.none(); - private DbClient dbClient = dbTester.getDbClient(); - private EsClient esClient = esTester.client(); + private System2 system2 = new AlwaysIncreasingSystem2(); + @org.junit.Rule + public DbTester dbTester = DbTester.create(system2); + @org.junit.Rule + public EsTester es = new EsTester(new RuleIndexDefinition(new MapSettings())); private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester); + private RuleIndex ruleIndex = new RuleIndex(es.client()); + private RuleIndexer ruleIndexer = new RuleIndexer(es.client(), dbTester.getDbClient()); + private ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(dbTester.getDbClient(), es.client(), new ActiveRuleIteratorFactory(dbTester.getDbClient())); + private Languages languages = LanguageTesting.newLanguages("java", "js"); + private ActiveRuleCompleter activeRuleCompleter = new ActiveRuleCompleter(dbTester.getDbClient(), languages); + private RuleWsSupport wsSupport = new RuleWsSupport(dbTester.getDbClient(), userSession, defaultOrganizationProvider); + private RuleQueryFactory ruleQueryFactory = new RuleQueryFactory(dbTester.getDbClient(), wsSupport); private MacroInterpreter macroInterpreter = mock(MacroInterpreter.class); - private Languages languages = new Languages(); - private RuleMapper mapper = new RuleMapper(languages, macroInterpreter); - private RuleQueryFactory ruleQueryFactory = new RuleQueryFactory(dbClient, new RuleWsSupport(dbClient, null, defaultOrganizationProvider)); - private ActiveRuleCompleter activeRuleCompleter = mock(ActiveRuleCompleter.class); - private RuleIndex ruleIndex = new RuleIndex(esClient); - private WsAction underTest = new SearchAction(ruleIndex, activeRuleCompleter, ruleQueryFactory, dbClient, mapper); - private WsActionTester actionTester = new WsActionTester(underTest); + private RuleMapper ruleMapper = new RuleMapper(languages, macroInterpreter); + private SearchAction underTest = new SearchAction(ruleIndex, activeRuleCompleter, ruleQueryFactory, dbTester.getDbClient(), ruleMapper); - private RuleIndexer ruleIndexer = new RuleIndexer(esClient, dbClient); + private RuleActivatorContextFactory contextFactory = new RuleActivatorContextFactory(dbTester.getDbClient()); + private TypeValidations typeValidations = new TypeValidations(asList(new StringTypeValidation(), new IntegerTypeValidation())); + private RuleActivator ruleActivator = new RuleActivator(system2, dbTester.getDbClient(), ruleIndex, contextFactory, typeValidations, activeRuleIndexer, + userSession); + private WsActionTester ws = new WsActionTester(underTest); @Before public void before() { @@ -84,39 +121,115 @@ public class SearchActionTest { } @Test - public void should_find_rule() throws IOException { - RuleDefinitionDto rule = insertRuleDefinition(); + public void test_definition() { + assertThat(ws.getDef().isPost()).isFalse(); + assertThat(ws.getDef().since()).isEqualTo("4.4"); + assertThat(ws.getDef().isInternal()).isFalse(); + assertThat(ws.getDef().params()).hasSize(22); + } - doReturn("interpreted").when(macroInterpreter).interpret(anyString()); + @Test + public void return_empty_result() { + Rules.SearchResponse response = ws.newRequest() + .setParam(WebService.Param.FIELDS, "actives") + .executeProtobuf(Rules.SearchResponse.class); - SearchResponse result = actionTester.newRequest() - .executeProtobuf(SearchResponse.class); - assertThat(result.getRulesList()).extracting(Rule::getKey).containsExactly(rule.getKey().toString()); + assertThat(response.getTotal()).isEqualTo(0L); + assertThat(response.getP()).isEqualTo(1); + assertThat(response.getRulesCount()).isEqualTo(0); + } + + @Test + public void return_all_rules() { + RuleDefinitionDto rule1 = createJavaRule(); + RuleDefinitionDto rule2 = createJavaRule(); + indexRules(); + + verify(r -> { + }, rule1, rule2); + } + + @Test + public void filter_by_rule_key() { + RuleDefinitionDto rule1 = createJavaRule(); + RuleDefinitionDto rule2 = createJavaRule(); + indexRules(); + + verify(r -> r.setParam(PARAM_RULE_KEY, rule1.getKey().toString()), rule1); + verifyNoResults(r -> r.setParam(PARAM_RULE_KEY, "missing")); + } + + @Test + public void return_all_rule_fields_by_default() { + RuleDefinitionDto rule = createJavaRule(); + indexRules(); + + Rules.SearchResponse response = ws.newRequest().executeProtobuf(Rules.SearchResponse.class); + Rules.Rule result = response.getRules(0); + assertThat(result.getCreatedAt()).isNotEmpty(); + assertThat(result.getEffortToFixDescription()).isNotEmpty(); + assertThat(result.getHtmlDesc()).isNotEmpty(); + assertThat(result.hasIsTemplate()).isTrue(); + assertThat(result.getLang()).isEqualTo(rule.getLanguage()); + assertThat(result.getLangName()).isEqualTo(languages.get(rule.getLanguage()).getName()); + assertThat(result.getName()).isNotEmpty(); + assertThat(result.getRepo()).isNotEmpty(); + assertThat(result.getSeverity()).isNotEmpty(); + assertThat(result.getType().name()).isEqualTo(RuleType.valueOf(rule.getType()).name()); + } + + @Test + public void return_subset_of_fields() { + RuleDefinitionDto rule = createJavaRule(); + indexRules(); + + Rules.SearchResponse response = ws.newRequest() + .setParam(WebService.Param.FIELDS, "createdAt,langName") + .executeProtobuf(Rules.SearchResponse.class); + Rules.Rule result = response.getRules(0); + + // mandatory fields + assertThat(result.getKey()).isEqualTo(rule.getKey().toString()); + assertThat(result.getType().getNumber()).isEqualTo(rule.getType()); + + // selected fields + assertThat(result.getCreatedAt()).isNotEmpty(); + assertThat(result.getLangName()).isNotEmpty(); + + // not returned fields + assertThat(result.hasEffortToFixDescription()).isFalse(); + assertThat(result.hasHtmlDesc()).isFalse(); + assertThat(result.hasIsTemplate()).isFalse(); + assertThat(result.hasLang()).isFalse(); + assertThat(result.hasName()).isFalse(); + assertThat(result.hasSeverity()).isFalse(); + assertThat(result.hasRepo()).isFalse(); } @Test public void should_filter_on_organization_specific_tags() throws IOException { OrganizationDto organization = dbTester.organizations().insert(); - RuleDefinitionDto rule1 = insertRuleDefinition(); + RuleDefinitionDto rule1 = createJavaRule(); RuleMetadataDto metadata1 = insertMetadata(organization, rule1, setTags("tag1", "tag2")); - RuleDefinitionDto rule2 = insertRuleDefinition(); + RuleDefinitionDto rule2 = createJavaRule(); RuleMetadataDto metadata2 = insertMetadata(organization, rule2); + indexRules(); - SearchResponse result = actionTester.newRequest() + Consumer request = r -> r .setParam("f", "repo,name") .setParam("tags", metadata1.getTags().stream().collect(Collectors.joining(","))) - .setParam("organization", organization.getKey()) - .executeProtobuf(SearchResponse.class); - assertThat(result.getRulesList()).extracting(Rule::getKey).containsExactly(rule1.getKey().toString()); + .setParam("organization", organization.getKey()); + verify(request, rule1); } @Test public void should_list_tags_in_tags_facet() throws IOException { OrganizationDto organization = dbTester.organizations().insert(); - RuleDefinitionDto rule = insertRuleDefinition(setSystemTags("tag1", "tag3", "tag5", "tag7", "tag9", "x")); + RuleDefinitionDto rule = dbTester.rules().insert(setSystemTags("tag1", "tag3", "tag5", "tag7", "tag9", "x")); RuleMetadataDto metadata = insertMetadata(organization, rule, setTags("tag2", "tag4", "tag6", "tag8", "tagA")); + indexRules(); - SearchResponse result = actionTester.newRequest() + SearchResponse result = ws.newRequest() .setParam("facets", "tags") .setParam("organization", organization.getKey()) .executeProtobuf(SearchResponse.class); @@ -127,9 +240,10 @@ public class SearchActionTest { @Test public void should_include_selected_matching_tag_in_facet() throws IOException { - insertRuleDefinition(setSystemTags("tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tagA", "x")); + RuleDefinitionDto rule = dbTester.rules().insert(setSystemTags("tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tagA", "x")); + indexRules(); - SearchResponse result = actionTester.newRequest() + SearchResponse result = ws.newRequest() .setParam("facets", "tags") .setParam("tags", "x") .executeProtobuf(SearchResponse.class); @@ -138,9 +252,10 @@ public class SearchActionTest { @Test public void should_included_selected_non_matching_tag_in_facet() throws IOException { - insertRuleDefinition(setSystemTags("tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tagA")); + RuleDefinitionDto rule = dbTester.rules().insert(setSystemTags("tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tagA")); + indexRules(); - SearchResponse result = actionTester.newRequest() + SearchResponse result = ws.newRequest() .setParam("facets", "tags") .setParam("tags", "x") .executeProtobuf(SearchResponse.class); @@ -150,10 +265,11 @@ public class SearchActionTest { @Test public void should_return_organization_specific_tags() throws IOException { OrganizationDto organization = dbTester.organizations().insert(); - RuleDefinitionDto rule = insertRuleDefinition(); + RuleDefinitionDto rule = createJavaRule(); RuleMetadataDto metadata = insertMetadata(organization, rule, setTags("tag1", "tag2")); + indexRules(); - SearchResponse result = actionTester.newRequest() + SearchResponse result = ws.newRequest() .setParam("f", "tags") .setParam("organization", organization.getKey()) .executeProtobuf(SearchResponse.class); @@ -164,8 +280,9 @@ public class SearchActionTest { } @Test - public void should_return_specified_fields() throws IOException { - RuleDefinitionDto rule = insertRuleDefinition(); + public void should_return_specified_fields() throws Exception { + RuleDefinitionDto rule = createJavaRule(); + indexRules(); checkField(rule, "repo", Rule::getRepo, rule.getRepositoryKey()); checkField(rule, "name", Rule::getName, rule.getName()); @@ -177,31 +294,392 @@ public class SearchActionTest { r -> r.getSysTags().getSysTagsList().stream().collect(Collectors.joining(",")), rule.getSystemTags().stream().collect(Collectors.joining(","))); checkField(rule, "lang", Rule::getLang, rule.getLanguage()); - checkField(rule, "langName", Rule::getLangName, rule.getLanguage()); + checkField(rule, "langName", Rule::getLangName, languages.get(rule.getLanguage()).getName()); checkField(rule, "gapDescription", Rule::getGapDescription, rule.getGapDescription()); // to be continued... } + @Test + public void return_lang_key_field_when_language_name_is_not_available() throws Exception { + OrganizationDto organization = dbTester.organizations().insert(); + String unknownLanguage = "unknown_" + randomAlphanumeric(5); + RuleDefinitionDto rule = dbTester.rules().insert(r -> r.setLanguage(unknownLanguage)); + + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "langName") + .setParam("organization", organization.getKey()) + .executeProtobuf(SearchResponse.class); + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + + Rule searchedRule = result.getRules(0); + assertThat(searchedRule).isNotNull(); + assertThat(searchedRule.getLangName()).isEqualTo(unknownLanguage); + } + + @Test + public void search_debt_rules_with_default_and_overridden_debt_values() throws Exception { + RuleDefinitionDto rule = dbTester.rules().insert(r -> + r.setLanguage("java") + .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefRemediationGapMultiplier("1h") + .setDefRemediationBaseEffort("15min")); + + RuleMetadataDto metadata = insertMetadata(dbTester.getDefaultOrganization(), rule, + r -> r.setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setRemediationGapMultiplier("2h") + .setRemediationBaseEffort("25min")); + + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "debtRemFn,debtOverloaded,defaultDebtRemFn") + .executeProtobuf(SearchResponse.class); + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + + Rule searchedRule = result.getRules(0); + assertThat(searchedRule).isNotNull(); + assertThat(searchedRule.getDefaultDebtRemFnCoeff()).isEqualTo("1h"); + assertThat(searchedRule.getDefaultDebtRemFnOffset()).isEqualTo("15min"); + assertThat(searchedRule.getDefaultDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); + assertThat(searchedRule.getDefaultRemFnBaseEffort()).isEqualTo("15min"); + assertThat(searchedRule.getDefaultRemFnGapMultiplier()).isEqualTo("1h"); + assertThat(searchedRule.getDefaultRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); + assertThat(searchedRule.getDebtOverloaded()).isTrue(); + assertThat(searchedRule.getDebtRemFnCoeff()).isEqualTo("2h"); + assertThat(searchedRule.getDebtRemFnOffset()).isEqualTo("25min"); + assertThat(searchedRule.getDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); + } + + @Test + public void search_debt_rules_with_default_linear_offset_and_overridden_constant_debt() throws Exception { + RuleDefinitionDto rule = dbTester.rules().insert(r -> + r.setLanguage("java") + .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefRemediationGapMultiplier("1h") + .setDefRemediationBaseEffort("15min")); + + RuleMetadataDto metadata = insertMetadata(dbTester.getDefaultOrganization(), rule, + r -> r.setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()) + .setRemediationGapMultiplier(null) + .setRemediationBaseEffort("5min")); + + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "debtRemFn,debtOverloaded,defaultDebtRemFn") + .executeProtobuf(SearchResponse.class); + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + + Rule searchedRule = result.getRules(0); + assertThat(searchedRule).isNotNull(); + assertThat(searchedRule.getDefaultDebtRemFnCoeff()).isEqualTo("1h"); + assertThat(searchedRule.getDefaultDebtRemFnOffset()).isEqualTo("15min"); + assertThat(searchedRule.getDefaultDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); + assertThat(searchedRule.getDefaultRemFnBaseEffort()).isEqualTo("15min"); + assertThat(searchedRule.getDefaultRemFnGapMultiplier()).isEqualTo("1h"); + assertThat(searchedRule.getDefaultRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); + assertThat(searchedRule.getDebtOverloaded()).isTrue(); + assertThat(searchedRule.getDebtRemFnCoeff()).isEmpty(); + ; + assertThat(searchedRule.getDebtRemFnOffset()).isEqualTo("5min"); + assertThat(searchedRule.getDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()); + } + + @Test + public void search_debt_rules_with_default_linear_offset_and_overridden_linear_debt() throws Exception { + RuleDefinitionDto rule = dbTester.rules().insert(r -> + r.setLanguage("java") + .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setDefRemediationGapMultiplier("1h") + .setDefRemediationBaseEffort("15min")); + + RuleMetadataDto metadata = insertMetadata(dbTester.getDefaultOrganization(), rule, + r -> r.setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) + .setRemediationGapMultiplier("1h") + .setRemediationBaseEffort(null)); + + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "debtRemFn,debtOverloaded,defaultDebtRemFn") + .executeProtobuf(SearchResponse.class); + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + + Rule searchedRule = result.getRules(0); + assertThat(searchedRule).isNotNull(); + assertThat(searchedRule.getDefaultDebtRemFnCoeff()).isEqualTo("1h"); + assertThat(searchedRule.getDefaultDebtRemFnOffset()).isEqualTo("15min"); + assertThat(searchedRule.getDefaultDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); + assertThat(searchedRule.getDefaultRemFnBaseEffort()).isEqualTo("15min"); + assertThat(searchedRule.getDefaultRemFnGapMultiplier()).isEqualTo("1h"); + assertThat(searchedRule.getDefaultRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); + assertThat(searchedRule.getDebtOverloaded()).isTrue(); + assertThat(searchedRule.getDebtRemFnCoeff()).isEqualTo("1h"); + assertThat(searchedRule.getDebtRemFnOffset()).isEmpty(); + assertThat(searchedRule.getDebtRemFnType()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name()); + } + + @Test + public void search_template_rules() throws Exception { + RuleDefinitionDto templateRule = dbTester.rules().insert(r -> + r.setLanguage("java") + .setIsTemplate(true)); + RuleDefinitionDto rule = dbTester.rules().insert(r -> + r.setLanguage("java") + .setTemplateId(templateRule.getId())); + + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "isTemplate") + .setParam("is_template", "true") + .executeProtobuf(SearchResponse.class); + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + + Rule searchedRule = result.getRules(0); + assertThat(searchedRule).isNotNull(); + assertThat(searchedRule.getIsTemplate()).isTrue(); + assertThat(searchedRule.getKey()).isEqualTo(templateRule.getRepositoryKey() + ":" + templateRule.getRuleKey()); + } + + @Test + public void search_custom_rules_from_template_key() throws Exception { + RuleDefinitionDto templateRule = dbTester.rules().insert(r -> + r.setLanguage("java") + .setIsTemplate(true)); + RuleDefinitionDto rule = dbTester.rules().insert(r -> + r.setLanguage("java") + .setTemplateId(templateRule.getId())); + + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "templateKey") + .setParam("template_key", templateRule.getRepositoryKey() + ":" + templateRule.getRuleKey()) + .executeProtobuf(SearchResponse.class); + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + + Rule searchedRule = result.getRules(0); + assertThat(searchedRule).isNotNull(); + assertThat(searchedRule.getKey()).isEqualTo(rule.getRepositoryKey() + ":" + rule.getRuleKey()); + assertThat(searchedRule.getTemplateKey()).isEqualTo(templateRule.getRepositoryKey() + ":" + templateRule.getRuleKey()); + } + + @Test + public void search_all_active_rules() throws Exception { + OrganizationDto organization = dbTester.organizations().insert(); + QProfileDto profile = dbTester.qualityProfiles().insert(organization, p -> p.setLanguage("java")); + RuleDefinitionDto rule = createJavaRule(); + RuleActivation activation = RuleActivation.create(rule.getKey(), BLOCKER, null); + ruleActivator.activate(dbTester.getSession(), activation, profile); + + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "") + .setParam("q", rule.getName()) + .setParam("activation", "true") + .setParam("organization", organization.getKey()) + .executeProtobuf(SearchResponse.class); + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + + Rule searchedRule = result.getRules(0); + assertThat(searchedRule).isNotNull(); + assertThat(searchedRule.getKey()).isEqualTo(rule.getRepositoryKey() + ":" + rule.getRuleKey()); + assertThat(searchedRule.getName()).isEqualTo(rule.getName()); + } + + @Test + public void search_profile_active_rules() throws Exception { + OrganizationDto organization = dbTester.organizations().insert(); + QProfileDto profile = dbTester.qualityProfiles().insert(organization, p -> p.setLanguage("java")); + QProfileDto waterproofProfile = dbTester.qualityProfiles().insert(organization, p -> p.setLanguage("java")); + + RuleDefinitionDto rule = createJavaRule(); + + RuleParamDto ruleParam1 = dbTester.rules().insertRuleParam(rule, p -> + p.setDefaultValue("some value") + .setType("STRING") + .setDescription("My small description") + .setName("my_var")); + + RuleParamDto ruleParam2 = dbTester.rules().insertRuleParam(rule, p -> + p.setDefaultValue("1") + .setType("INTEGER") + .setDescription("My small description") + .setName("the_var")); + + // SONAR-7083 + RuleParamDto ruleParam3 = dbTester.rules().insertRuleParam(rule, p -> + p.setDefaultValue(null) + .setType("STRING") + .setDescription("Empty Param") + .setName("empty_var")); + + RuleActivation activation = RuleActivation.create(rule.getKey()); + List activeRuleChanges1 = ruleActivator.activate(dbTester.getSession(), activation, profile); + ruleActivator.activate(dbTester.getSession(), activation, waterproofProfile); + + assertThat(activeRuleChanges1).hasSize(1); + + dbTester.commit(); + + indexRules(); + indexActiveRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "actives") + .setParam("q", rule.getName()) + .setParam("organization", organization.getKey()) + .setParam("activation", "true") + .setParam("qprofile", profile.getKee()) + .executeProtobuf(SearchResponse.class); + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + assertThat(result.getActives()).isNotNull(); + assertThat(result.getActives().getActives().get(rule.getKey().toString())).isNotNull(); + assertThat(result.getActives().getActives().get(rule.getKey().toString()).getActiveListList()).hasSize(1); + + Rules.Active activeList = result.getActives().getActives().get(rule.getKey().toString()).getActiveList(0); + + // The rule without value is not inserted + assertThat(activeList.getParamsCount()).isEqualTo(2); + assertThat(activeList.getParamsList()).extracting("key", "value").containsExactlyInAnyOrder( + tuple(ruleParam1.getName(), ruleParam1.getDefaultValue()), + tuple(ruleParam2.getName(), ruleParam2.getDefaultValue()) + ); + + String unknownProfile = "unknown_profile" + randomAlphanumeric(5); + thrown.expect(NotFoundException.class); + thrown.expectMessage("The specified qualityProfile '" + unknownProfile + "' does not exist"); + + ws.newRequest() + .setParam("activation", "true") + .setParam("qprofile", unknownProfile) + .executeProtobuf(SearchResponse.class); + } + + @Test + public void test_SONAR7083() { + OrganizationDto organization = dbTester.organizations().insert(); + QProfileDto profile = dbTester.qualityProfiles().insert(organization, p -> p.setLanguage("java")); + + RuleDefinitionDto rule = createJavaRule(); + + RuleParamDto ruleParam = dbTester.rules().insertRuleParam(rule, p -> + p.setDefaultValue("some value") + .setType("STRING") + .setDescription("My small description") + .setName("my_var")); + + RuleActivation activation = RuleActivation.create(rule.getKey()); + List activeRuleChanges = ruleActivator.activate(dbTester.getSession(), activation, profile); + + // Insert directly in database a rule parameter with a null value + ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(ruleParam).setValue(null); + dbTester.getDbClient().activeRuleDao().insertParam(dbTester.getSession(), activeRuleChanges.get(0).getActiveRule(), activeRuleParam); + + dbTester.commit(); + + indexRules(); + indexActiveRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "actives") + .setParam("q", rule.getName()) + .setParam("organization", organization.getKey()) + .setParam("activation", "true") + .setParam("qprofile", profile.getKee()) + .executeProtobuf(SearchResponse.class); + + assertThat(result.getTotal()).isEqualTo(1); + assertThat(result.getRulesCount()).isEqualTo(1); + assertThat(result.getActives()).isNotNull(); + assertThat(result.getActives().getActives().get(rule.getKey().toString())).isNotNull(); + assertThat(result.getActives().getActives().get(rule.getKey().toString()).getActiveListList()).hasSize(1); + + Rules.Active activeList = result.getActives().getActives().get(rule.getKey().toString()).getActiveList(0); + assertThat(activeList.getParamsCount()).isEqualTo(2); + assertThat(activeList.getParamsList()).extracting("key", "value").containsExactlyInAnyOrder( + tuple(ruleParam.getName(), ruleParam.getDefaultValue()), + tuple(activeRuleParam.getKey(), "") + ); + } + + @Test + public void statuses_facet_should_be_sticky() throws Exception { + RuleDefinitionDto rule1 = dbTester.rules().insert(r -> r.setLanguage("java")); + RuleDefinitionDto rule2 = dbTester.rules().insert(r -> r.setLanguage("java").setStatus(RuleStatus.BETA)); + RuleDefinitionDto rule3 = dbTester.rules().insert(r -> r.setLanguage("java").setStatus(RuleStatus.DEPRECATED)); + + indexRules(); + + SearchResponse result = ws.newRequest() + .setParam("f", "status") + .setParam("status", "DEPRECATED") + .executeProtobuf(SearchResponse.class); + + assertThat(result.getRulesCount()).isEqualTo(3); + assertThat(result.getRulesList()).extracting("key", "status.name").containsExactlyInAnyOrder( + tuple(rule1.getKey().toString(), rule1.getStatus().name()), + tuple(rule2.getKey().toString(), rule2.getStatus().name()), + tuple(rule3.getKey().toString(), rule3.getStatus().name()) + ); + } + @SafeVarargs private final void checkField(RuleDefinitionDto rule, String fieldName, Extractor responseExtractor, T... expected) throws IOException { - SearchResponse result = actionTester.newRequest() + SearchResponse result = ws.newRequest() .setParam("f", fieldName) .executeProtobuf(SearchResponse.class); assertThat(result.getRulesList()).extracting(Rule::getKey).containsExactly(rule.getKey().toString()); assertThat(result.getRulesList()).extracting(responseExtractor).containsExactly(expected); } - @SafeVarargs - private final RuleDefinitionDto insertRuleDefinition(Consumer... populaters) { - RuleDefinitionDto ruleDefinitionDto = dbTester.rules().insert(populaters); - ruleIndexer.indexRuleDefinition(ruleDefinitionDto.getKey()); - return ruleDefinitionDto; - } - @SafeVarargs private final RuleMetadataDto insertMetadata(OrganizationDto organization, RuleDefinitionDto rule, Consumer... populaters) { RuleMetadataDto metadata = dbTester.rules().insertOrUpdateMetadata(rule, organization, populaters); ruleIndexer.indexRuleExtension(organization, rule.getKey()); return metadata; } + + private void verifyNoResults(Consumer requestPopulator) { + verify(requestPopulator); + } + + private void verify(Consumer requestPopulator, RuleDefinitionDto... expectedRules) { + TestRequest request = ws.newRequest(); + requestPopulator.accept(request); + Rules.SearchResponse response = request + .executeProtobuf(Rules.SearchResponse.class); + + assertThat(response.getP()).isEqualTo(1); + assertThat(response.getTotal()).isEqualTo(expectedRules.length); + assertThat(response.getRulesCount()).isEqualTo(expectedRules.length); + RuleKey[] expectedRuleKeys = stream(expectedRules).map(RuleDefinitionDto::getKey).collect(MoreCollectors.toList()).toArray(new RuleKey[0]); + assertThat(response.getRulesList()) + .extracting(r -> RuleKey.parse(r.getKey())) + .containsExactlyInAnyOrder(expectedRuleKeys); + } + + private void indexRules() { + ruleIndexer.indexOnStartup(ruleIndexer.getIndexTypes()); + } + + private void indexActiveRules() { + activeRuleIndexer.indexOnStartup(activeRuleIndexer.getIndexTypes()); + } + + private RuleDefinitionDto createJavaRule() { + return dbTester.rules().insert(r -> r.setLanguage("java")); + } }