From: Daniel Schwarz Date: Thu, 16 Mar 2017 11:20:03 +0000 (+0100) Subject: SONAR-8857 refactored inheritance action medium test to a regular test X-Git-Tag: 6.4-RC1~627 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6acd6d9c8af54e87f6bee7cd38af7572e8b8ac65;p=sonarqube.git SONAR-8857 refactored inheritance action medium test to a regular test --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest.java deleted file mode 100644 index dd167c80804..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.qualityprofile.ws; - -import java.util.Date; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rule.RuleStatus; -import org.sonar.api.rule.Severity; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.organization.OrganizationDto; -import org.sonar.db.organization.OrganizationTesting; -import org.sonar.db.qualityprofile.ActiveRuleDto; -import org.sonar.db.qualityprofile.QualityProfileDto; -import org.sonar.db.rule.RuleDto; -import org.sonar.db.rule.RuleTesting; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.qualityprofile.QProfileName; -import org.sonar.server.qualityprofile.QProfileTesting; -import org.sonar.server.qualityprofile.RuleActivation; -import org.sonar.server.qualityprofile.RuleActivator; -import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; -import org.sonar.server.rule.index.RuleIndexer; -import org.sonar.server.tester.ServerTester; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.WsTester; - -import static org.sonar.server.qualityprofile.QProfileTesting.getDefaultOrganization; - -public class InheritanceActionMediumTest { - - @ClassRule - public static final ServerTester tester = new ServerTester().withEsIndexes(); - - @Rule - public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester); - - WsTester wsTester; - - DbClient db; - DbSession session; - - RuleIndexer ruleIndexer; - ActiveRuleIndexer activeRuleIndexer; - - private OrganizationDto organization; - - @Before - public void setUp() { - tester.clearDbAndIndexes(); - db = tester.get(DbClient.class); - session = db.openSession(false); - ruleIndexer = tester.get(RuleIndexer.class); - activeRuleIndexer = tester.get(ActiveRuleIndexer.class); - - wsTester = new WsTester(tester.get(QProfilesWs.class)); - organization = getDefaultOrganization(tester, db, session); - } - - @After - public void tearDown() { - session.close(); - } - - @Test - public void inheritance_nominal() throws Exception { - RuleDto rule1 = createRule("xoo", "rule1"); - RuleDto rule2 = createRule("xoo", "rule2"); - RuleDto rule3 = createRule("xoo", "rule3"); - - /* - * groupWide (2) <- companyWide (2) <- buWide (2, 1 overriding) <- (forProject1 (2), forProject2 (2)) - */ - QualityProfileDto groupWide = createProfile("xoo", "My Group Profile", "xoo-my-group-profile-01234"); - createActiveRule(rule1, groupWide); - createActiveRule(rule2, groupWide); - - session.commit(); - ruleIndexer.index(); - activeRuleIndexer.index(); - - QualityProfileDto companyWide = createProfile("xoo", "My Company Profile", "xoo-my-company-profile-12345"); - setParent(groupWide, companyWide); - - QualityProfileDto buWide = createProfile("xoo", "My BU Profile", "xoo-my-bu-profile-23456"); - setParent(companyWide, buWide); - overrideActiveRuleSeverity(rule1, buWide, Severity.CRITICAL); - - QualityProfileDto forProject1 = createProfile("xoo", "For Project One", "xoo-for-project-one-34567"); - setParent(buWide, forProject1); - createActiveRule(rule3, forProject1); - session.commit(); - activeRuleIndexer.index(); - - QualityProfileDto forProject2 = createProfile("xoo", "For Project Two", "xoo-for-project-two-45678"); - setParent(buWide, forProject2); - overrideActiveRuleSeverity(rule2, forProject2, Severity.CRITICAL); - - wsTester.newGetRequest("api/qualityprofiles", "inheritance").setParam("profileKey", buWide.getKee()).setParam("organization", organization.getKey()) - .execute().assertJson(getClass(), "inheritance-buWide.json"); - } - - @Test - public void inheritance_no_family() throws Exception { - // Simple profile, no parent, no child - QualityProfileDto remi = createProfile("xoo", "Nobodys Boy", "xoo-nobody-s-boy-01234"); - - wsTester.newGetRequest("api/qualityprofiles", "inheritance").setParam("profileKey", remi.getKee()).execute().assertJson(getClass(), "inheritance-simple.json"); - } - - @Test(expected = NotFoundException.class) - public void fail_if_not_found() throws Exception { - wsTester.newGetRequest("api/qualityprofiles", "inheritance").setParam("profileKey", "polop").execute(); - } - - private QualityProfileDto createProfile(String lang, String name, String key) { - QualityProfileDto profile = QProfileTesting.newQProfileDto(organization, new QProfileName(lang, name), key); - db.qualityProfileDao().insert(session, profile); - session.commit(); - return profile; - } - - private void setParent(QualityProfileDto profile, QualityProfileDto parent) { - tester.get(RuleActivator.class).setParent(session, parent.getKey(), profile.getKey()); - } - - private RuleDto createRule(String lang, String id) { - long now = new Date().getTime(); - RuleDto rule = RuleTesting.newDto(RuleKey.of("blah", id)) - .setLanguage(lang) - .setSeverity(Severity.BLOCKER) - .setStatus(RuleStatus.READY) - .setUpdatedAt(now) - .setCreatedAt(now); - db.ruleDao().insert(session, rule); - return rule; - } - - private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) { - long now = new Date().getTime(); - ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule) - .setSeverity(rule.getSeverityString()) - .setUpdatedAt(now) - .setCreatedAt(now); - db.activeRuleDao().insert(session, activeRule); - return activeRule; - } - - private void overrideActiveRuleSeverity(RuleDto rule, QualityProfileDto profile, String severity) { - tester.get(RuleActivator.class).activate(session, new RuleActivation(rule.getKey()).setSeverity(severity), profile.getKey()); - session.commit(); - activeRuleIndexer.index(); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java new file mode 100644 index 00000000000..3838725a79a --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java @@ -0,0 +1,214 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.qualityprofile.ws; + +import java.util.ArrayList; +import java.util.Date; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.config.MapSettings; +import org.sonar.api.resources.Languages; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.RuleStatus; +import org.sonar.api.rule.Severity; +import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.DbTester; +import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.qualityprofile.ActiveRuleDto; +import org.sonar.db.qualityprofile.QualityProfileDto; +import org.sonar.db.rule.RuleDto; +import org.sonar.db.rule.RuleTesting; +import org.sonar.server.es.EsClient; +import org.sonar.server.es.EsTester; +import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.organization.TestDefaultOrganizationProvider; +import org.sonar.server.qualityprofile.QProfileFactory; +import org.sonar.server.qualityprofile.QProfileLoader; +import org.sonar.server.qualityprofile.QProfileLookup; +import org.sonar.server.qualityprofile.QProfileName; +import org.sonar.server.qualityprofile.QProfileTesting; +import org.sonar.server.qualityprofile.RuleActivation; +import org.sonar.server.qualityprofile.RuleActivator; +import org.sonar.server.qualityprofile.RuleActivatorContextFactory; +import org.sonar.server.qualityprofile.index.ActiveRuleIndex; +import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; +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.util.TypeValidations; +import org.sonar.server.ws.WsActionTester; +import org.sonar.test.JsonAssert; + +public class InheritanceActionTest { + + @Rule + public DbTester dbTester = DbTester.create(); + @Rule + public EsTester esTester = new EsTester(new RuleIndexDefinition(new MapSettings())); + @Rule + public UserSessionRule userSessionRule = UserSessionRule.standalone(); + + private DbClient dbClient; + private DbSession dbSession; + private EsClient esClient; + private RuleIndexer ruleIndexer; + private ActiveRuleIndexer activeRuleIndexer; + private WsActionTester wsActionTester; + private RuleActivator ruleActivator; + private OrganizationDto organization; + + @Before + public void setUp() { + dbClient = dbTester.getDbClient(); + dbSession = dbTester.getSession(); + esClient = esTester.client(); + ruleIndexer = new RuleIndexer(System2.INSTANCE, dbClient, esClient); + activeRuleIndexer = new ActiveRuleIndexer(System2.INSTANCE, dbClient, esClient); + wsActionTester = new WsActionTester( + new InheritanceAction( + dbClient, + new QProfileLookup(dbClient), + new QProfileLoader( + dbClient, + new ActiveRuleIndex(esClient), + TestDefaultOrganizationProvider.from(dbTester) + ), + new QProfileFactory(dbClient), + new Languages() + )); + ruleActivator = new RuleActivator( + System2.INSTANCE, + dbClient, + new RuleIndex(esClient), + new RuleActivatorContextFactory(dbClient), + new TypeValidations(new ArrayList<>()), + new ActiveRuleIndexer(System2.INSTANCE, dbClient, esClient), + userSessionRule + ); + organization = dbTester.getDefaultOrganization(); + } + + @Test + public void inheritance_nominal() throws Exception { + RuleDto rule1 = createRule("xoo", "rule1"); + RuleDto rule2 = createRule("xoo", "rule2"); + RuleDto rule3 = createRule("xoo", "rule3"); + + /* + * groupWide (2) <- companyWide (2) <- buWide (2, 1 overriding) <- (forProject1 (2), forProject2 (2)) + */ + QualityProfileDto groupWide = createProfile("xoo", "My Group Profile", "xoo-my-group-profile-01234"); + createActiveRule(rule1, groupWide); + createActiveRule(rule2, groupWide); + + dbSession.commit(); + ruleIndexer.index(); + activeRuleIndexer.index(); + + QualityProfileDto companyWide = createProfile("xoo", "My Company Profile", "xoo-my-company-profile-12345"); + setParent(groupWide, companyWide); + + QualityProfileDto buWide = createProfile("xoo", "My BU Profile", "xoo-my-bu-profile-23456"); + setParent(companyWide, buWide); + overrideActiveRuleSeverity(rule1, buWide, Severity.CRITICAL); + + QualityProfileDto forProject1 = createProfile("xoo", "For Project One", "xoo-for-project-one-34567"); + setParent(buWide, forProject1); + createActiveRule(rule3, forProject1); + dbSession.commit(); + activeRuleIndexer.index(); + + QualityProfileDto forProject2 = createProfile("xoo", "For Project Two", "xoo-for-project-two-45678"); + setParent(buWide, forProject2); + overrideActiveRuleSeverity(rule2, forProject2, Severity.CRITICAL); + + String response = wsActionTester.newRequest() + .setMethod("GET") + .setParam("profileKey", buWide.getKee()) + .setParam("organization", organization.getKey()) + .execute() + .getInput(); + + JsonAssert.assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-buWide.json")); + } + + @Test + public void inheritance_no_family() throws Exception { + // Simple profile, no parent, no child + QualityProfileDto remi = createProfile("xoo", "Nobodys Boy", "xoo-nobody-s-boy-01234"); + + String response = wsActionTester.newRequest() + .setMethod("GET") + .setParam("profileKey", remi.getKee()) + .execute() + .getInput(); + + JsonAssert.assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-simple.json")); + } + + @Test(expected = NotFoundException.class) + public void fail_if_not_found() throws Exception { + wsActionTester.newRequest() + .setMethod("GET").setParam("profileKey", "polop").execute(); + } + + private QualityProfileDto createProfile(String lang, String name, String key) { + QualityProfileDto profile = QProfileTesting.newQProfileDto(organization, new QProfileName(lang, name), key); + dbClient.qualityProfileDao().insert(dbSession, profile); + dbSession.commit(); + return profile; + } + + private void setParent(QualityProfileDto profile, QualityProfileDto parent) { + ruleActivator.setParent(dbSession, parent.getKey(), profile.getKey()); + } + + private RuleDto createRule(String lang, String id) { + long now = new Date().getTime(); + RuleDto rule = RuleTesting.newDto(RuleKey.of("blah", id)) + .setLanguage(lang) + .setSeverity(Severity.BLOCKER) + .setStatus(RuleStatus.READY) + .setUpdatedAt(now) + .setCreatedAt(now); + dbClient.ruleDao().insert(dbSession, rule); + return rule; + } + + private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) { + long now = new Date().getTime(); + ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule) + .setSeverity(rule.getSeverityString()) + .setUpdatedAt(now) + .setCreatedAt(now); + dbClient.activeRuleDao().insert(dbSession, activeRule); + return activeRule; + } + + private void overrideActiveRuleSeverity(RuleDto rule, QualityProfileDto profile, String severity) { + ruleActivator.activate(dbSession, new RuleActivation(rule.getKey()).setSeverity(severity), profile.getKey()); + dbSession.commit(); + activeRuleIndexer.index(); + } +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest/inheritance-buWide.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest/inheritance-buWide.json deleted file mode 100644 index 9e3707e5d3f..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest/inheritance-buWide.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "profile": { - "key": "xoo-my-bu-profile-23456", "name": "My BU Profile", "parent": "xoo-my-company-profile-12345", - "activeRuleCount": 2, "overridingRuleCount": 1 - }, - "ancestors": [ - {"key": "xoo-my-company-profile-12345", "name": "My Company Profile", "parent": "xoo-my-group-profile-01234", "activeRuleCount": 2}, - {"key": "xoo-my-group-profile-01234", "name": "My Group Profile", "activeRuleCount": 2} - ], - "children": [ - {"key": "xoo-for-project-one-34567", "name": "For Project One", "activeRuleCount": 3}, - {"key": "xoo-for-project-two-45678", "name": "For Project Two", "activeRuleCount": 2, "overridingRuleCount": 1} - ] -} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest/inheritance-simple.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest/inheritance-simple.json deleted file mode 100644 index b5168de4671..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionMediumTest/inheritance-simple.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "profile": { - "key": "xoo-nobody-s-boy-01234", "name": "Nobodys Boy", "activeRuleCount": 0 - }, - "ancestors": [], - "children": [] -} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionTest/inheritance-buWide.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionTest/inheritance-buWide.json new file mode 100644 index 00000000000..9e3707e5d3f --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionTest/inheritance-buWide.json @@ -0,0 +1,14 @@ +{ + "profile": { + "key": "xoo-my-bu-profile-23456", "name": "My BU Profile", "parent": "xoo-my-company-profile-12345", + "activeRuleCount": 2, "overridingRuleCount": 1 + }, + "ancestors": [ + {"key": "xoo-my-company-profile-12345", "name": "My Company Profile", "parent": "xoo-my-group-profile-01234", "activeRuleCount": 2}, + {"key": "xoo-my-group-profile-01234", "name": "My Group Profile", "activeRuleCount": 2} + ], + "children": [ + {"key": "xoo-for-project-one-34567", "name": "For Project One", "activeRuleCount": 3}, + {"key": "xoo-for-project-two-45678", "name": "For Project Two", "activeRuleCount": 2, "overridingRuleCount": 1} + ] +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionTest/inheritance-simple.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionTest/inheritance-simple.json new file mode 100644 index 00000000000..b5168de4671 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/InheritanceActionTest/inheritance-simple.json @@ -0,0 +1,7 @@ +{ + "profile": { + "key": "xoo-nobody-s-boy-01234", "name": "Nobodys Boy", "activeRuleCount": 0 + }, + "ancestors": [], + "children": [] +}