From 5a2ce47eefb8bd680c48862e8d995f97001600ca Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 13 Apr 2015 15:15:21 +0200 Subject: [PATCH] SONAR-6308 Duplicate code to simplify tests --- .../qualityprofile/QProfileActivity.java | 11 ++- .../ws/QProfileChangelogAction.java | 37 +++++++- ....java => QProfileChangelogActionTest.java} | 93 +++++++++++++------ .../changelog_empty.json | 0 .../changelog_nominal.json | 0 .../changelog_page1.json | 0 .../changelog_page2.json | 0 .../changelog_page3.json | 0 8 files changed, 106 insertions(+), 35 deletions(-) rename server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/{QProfileChangelogActionMediumTest.java => QProfileChangelogActionTest.java} (55%) rename server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/{QProfileChangelogActionMediumTest => QProfileChangelogActionTest}/changelog_empty.json (100%) rename server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/{QProfileChangelogActionMediumTest => QProfileChangelogActionTest}/changelog_nominal.json (100%) rename server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/{QProfileChangelogActionMediumTest => QProfileChangelogActionTest}/changelog_page1.json (100%) rename server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/{QProfileChangelogActionMediumTest => QProfileChangelogActionTest}/changelog_page2.json (100%) rename server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/{QProfileChangelogActionMediumTest => QProfileChangelogActionTest}/changelog_page3.json (100%) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java index fa49d3695e4..acf03166504 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java @@ -26,6 +26,7 @@ import org.sonar.server.activity.index.ActivityIndexDefinition; import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import java.util.Map; /** @@ -33,17 +34,19 @@ import java.util.Map; */ public class QProfileActivity extends ActivityDoc { + private static final String FIELD_SEVERITY = "severity"; + private String ruleName = null; private String authorName = null; - protected QProfileActivity(Map fields) { + public QProfileActivity(Map fields) { super(fields); Map details = getField("details"); for (Map.Entry detail : details.entrySet()) { fields.put((String) detail.getKey(), detail.getValue()); } - if (!fields.containsKey("severity")) { - fields.put("severity", null); + if (!fields.containsKey(FIELD_SEVERITY)) { + fields.put(FIELD_SEVERITY, null); } } @@ -81,7 +84,7 @@ public class QProfileActivity extends ActivityDoc { @CheckForNull public String severity(){ - return (String) getNullableField("severity"); + return (String) getNullableField(FIELD_SEVERITY); } public Map parameters() { diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangelogAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangelogAction.java index 6869e8273a6..fa3cbc08187 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangelogAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangelogAction.java @@ -19,6 +19,8 @@ */ package org.sonar.server.qualityprofile.ws; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.SearchHit; import org.sonar.api.resources.Languages; import org.sonar.api.server.ws.*; import org.sonar.api.server.ws.WebService.NewAction; @@ -28,13 +30,15 @@ import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.Paging; import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.persistence.DbSession; +import org.sonar.core.rule.RuleDto; +import org.sonar.core.user.UserDto; +import org.sonar.server.activity.index.ActivityIndex; import org.sonar.server.db.DbClient; import org.sonar.server.es.SearchOptions; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.qualityprofile.QProfileActivity; import org.sonar.server.qualityprofile.QProfileActivityQuery; import org.sonar.server.qualityprofile.QProfileFactory; -import org.sonar.server.qualityprofile.QProfileService; import org.sonar.server.search.Result; import java.util.Date; @@ -45,14 +49,14 @@ public class QProfileChangelogAction implements BaseQProfileWsAction { private static final String PARAM_SINCE = "since"; private static final String PARAM_TO = "to"; - private QProfileService service; private DbClient dbClient; + private ActivityIndex activityIndex; private QProfileFactory profileFactory; private Languages languages; - public QProfileChangelogAction(QProfileService service, DbClient dbClient, QProfileFactory profileFactory, Languages languages) { - this.service = service; + public QProfileChangelogAction(DbClient dbClient, ActivityIndex activityIndex, QProfileFactory profileFactory, Languages languages) { this.dbClient = dbClient; + this.activityIndex = activityIndex; this.profileFactory = profileFactory; this.languages = languages; } @@ -102,13 +106,36 @@ public class QProfileChangelogAction implements BaseQProfileWsAction { int page = request.mandatoryParamAsInt(Param.PAGE); options.setPage(page, request.mandatoryParamAsInt(Param.PAGE_SIZE)); - Result result = service.searchActivities(query, options); + Result result = searchActivities(query, options); writeResponse(response.newJsonWriter(), result, Paging.create(options.getLimit(), page, (int) result.getTotal())); } finally { session.close(); } } + private Result searchActivities(QProfileActivityQuery query, SearchOptions options) { + DbSession session = dbClient.openSession(false); + try { + SearchResponse response = activityIndex.doSearch(query, options); + Result result = new Result(response); + for (SearchHit hit : response.getHits().getHits()) { + QProfileActivity profileActivity = new QProfileActivity(hit.getSource()); + RuleDto ruleDto = dbClient.ruleDao().getNullableByKey(session, profileActivity.ruleKey()); + profileActivity.ruleName(ruleDto != null ? ruleDto.getName() : null); + + String login = profileActivity.getLogin(); + if (login != null) { + UserDto user = dbClient.userDao().selectActiveUserByLogin(session, login); + profileActivity.authorName(user != null ? user.getName() : null); + } + result.getHits().add(profileActivity); + } + return result; + } finally { + session.close(); + } + } + private void writeResponse(JsonWriter json, Result result, Paging paging) { json.beginObject(); json.prop("total", result.getTotal()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest.java similarity index 55% rename from server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest.java rename to server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest.java index d7d7b43898a..613f48b1d66 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest.java @@ -19,45 +19,69 @@ */ package org.sonar.server.qualityprofile.ws; +import com.google.common.collect.Maps; import org.joda.time.DateTime; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; import org.junit.Test; +import org.sonar.api.config.Settings; import org.sonar.api.rule.Severity; import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.internal.Uuids; import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.persistence.DbSession; +import org.sonar.core.persistence.DbTester; import org.sonar.core.qualityprofile.db.ActiveRuleKey; +import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.rule.RuleDto; import org.sonar.core.user.UserDto; -import org.sonar.server.activity.ActivityService; +import org.sonar.server.activity.Activity; +import org.sonar.server.activity.index.ActivityDoc; +import org.sonar.server.activity.index.ActivityIndex; +import org.sonar.server.activity.index.ActivityIndexDefinition; import org.sonar.server.db.DbClient; +import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.NotFoundException; +import org.sonar.server.language.LanguageTesting; import org.sonar.server.qualityprofile.ActiveRuleChange; +import org.sonar.server.qualityprofile.ActiveRuleChange.Type; +import org.sonar.server.qualityprofile.QProfileFactory; import org.sonar.server.qualityprofile.QProfileTesting; import org.sonar.server.rule.RuleTesting; -import org.sonar.server.tester.ServerTester; +import org.sonar.server.rule.db.RuleDao; import org.sonar.server.user.MockUserSession; +import org.sonar.server.user.db.UserDao; import org.sonar.server.ws.WsTester; import java.util.Date; +import java.util.Map; +import static org.mockito.Mockito.mock; import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_KEY; -public class QProfileChangelogActionMediumTest { +public class QProfileChangelogActionTest { @ClassRule - public static ServerTester tester = new ServerTester(); + public static DbTester dbTester = new DbTester(); + + @ClassRule + public static EsTester esTester = new EsTester().addDefinitions(new ActivityIndexDefinition(new Settings())); private DbClient db; private DbSession dbSession; private WsTester wsTester; + private String login; @Before public void before() { - tester.clearDbAndIndexes(); - db = tester.get(DbClient.class); + dbTester.truncateTables(); + esTester.truncateIndices(); + + System2 system = mock(System2.class); + + db = new DbClient(dbTester.database(), dbTester.myBatis(), new RuleDao(system), new QualityProfileDao(dbTester.myBatis(), system), new UserDao(dbTester.myBatis(), system)); dbSession = db.openSession(false); // create pre-defined rules @@ -67,14 +91,15 @@ public class QProfileChangelogActionMediumTest { // create pre-defined profiles P1 and P2 db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1(), QProfileTesting.newXooP2()); - // create a user for activity author - UserDto user = new UserDto().setLogin("david").setName("David").setEmail("dav@id.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis()); + login = "david"; + UserDto user = new UserDto().setLogin(login).setName("David").setEmail("dav@id.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis()); db.userDao().insert(dbSession, user); dbSession.commit(); dbSession.clearCache(); - wsTester = new WsTester(tester.get(QProfilesWs.class)); + wsTester = new WsTester(new QProfilesWs(mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class), mock(ProjectAssociationActions.class), + new QProfileChangelogAction(db, new ActivityIndex(esTester.client()), new QProfileFactory(db), LanguageTesting.newLanguages("xoo")))); } @After @@ -84,10 +109,8 @@ public class QProfileChangelogActionMediumTest { @Test public void changelog_nominal() throws Exception { - MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("david"); - tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)) - .setSeverity(Severity.MAJOR) - .setParameter("max", "10").toActivity()); + MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin(login); + createActivity(login, ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1), Severity.MAJOR, "max", "10"); wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY) .execute().assertJson(getClass(), "changelog_nominal.json"); @@ -99,32 +122,30 @@ public class QProfileChangelogActionMediumTest { Date tomorrow = DateTime.now().plusDays(1).toDate(); MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("david"); - tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)) - .setSeverity(Severity.MAJOR) - .setParameter("max", "10").toActivity()); + createActivity(login, ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1), Severity.MAJOR, "max", "10"); // Tests with "since" - wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("since", DateUtils.formatDate(yesterday)) + wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("since", + DateUtils.formatDate(yesterday)) .execute().assertJson(getClass(), "changelog_nominal.json"); - wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("since", DateUtils.formatDate(tomorrow)) + wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("since", + DateUtils.formatDate(tomorrow)) .execute().assertJson(getClass(), "changelog_empty.json"); // Tests with "to" - wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("to", DateUtils.formatDate(yesterday)) + wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("to", + DateUtils.formatDate(yesterday)) .execute().assertJson(getClass(), "changelog_empty.json"); - wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("to", DateUtils.formatDate(tomorrow)) + wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("to", + DateUtils.formatDate(tomorrow)) .execute().assertJson(getClass(), "changelog_nominal.json"); } @Test public void changelog_with_pagination() throws Exception { MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("david"); - tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)) - .setSeverity(Severity.MAJOR) - .setParameter("max", "10").toActivity()); - tester.get(ActivityService.class).save(ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1)) - .setSeverity(Severity.CRITICAL) - .setParameter("max", "20").toActivity()); + createActivity(login, ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1), Severity.MAJOR, "max", "10"); + createActivity(login, ActiveRuleChange.Type.ACTIVATED, ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1), Severity.CRITICAL, "max", "20"); wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", XOO_P1_KEY).setParam("ps", "1") .execute().assertJson(getClass(), "changelog_page1.json"); @@ -138,4 +159,24 @@ public class QProfileChangelogActionMediumTest { public void fail_on_unknown_profile() throws Exception { wsTester.newGetRequest(QProfilesWs.API_ENDPOINT, "changelog").setParam("profileKey", "unknown-profile").execute(); } + + private void createActivity(String login, Type type, ActiveRuleKey activeRuleKey, String severity, String... params) throws Exception { + Map details = Maps.newHashMap(); + details.put("key", activeRuleKey.toString()); + details.put("ruleKey", activeRuleKey.ruleKey().toString()); + details.put("profileKey", activeRuleKey.qProfile()); + details.put("severity", severity); + for (int i = 0; i < params.length; i += 2) { + details.put("param_" + params[i], params[i + 1]); + } + ActivityDoc doc = new ActivityDoc(Maps.newHashMap()); + doc.setAction(type.toString()); + doc.setCreatedAt(new Date()); + doc.setDetails(details); + doc.setKey(Uuids.create()); + doc.setLogin(login); + doc.setType(Activity.Type.QPROFILE.toString()); + + esTester.putDocuments(ActivityIndexDefinition.INDEX, ActivityIndexDefinition.TYPE, doc); + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_empty.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_empty.json similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_empty.json rename to server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_empty.json diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_nominal.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_nominal.json similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_nominal.json rename to server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_nominal.json diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_page1.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_page1.json similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_page1.json rename to server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_page1.json diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_page2.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_page2.json similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_page2.json rename to server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_page2.json diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_page3.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_page3.json similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionMediumTest/changelog_page3.json rename to server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileChangelogActionTest/changelog_page3.json -- 2.39.5