From ce7e18ffa4eb1a90faf560e3b8c9fb54edaa696b Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 19 Oct 2016 16:58:02 +0200 Subject: [PATCH] SONAR-8278 clean-up SearchTemplateAction and its test --- .../ws/template/SearchTemplatesAction.java | 5 +- .../template/SearchTemplatesActionTest.java | 49 +++++++++---------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java index 1fde8f12702..1a9de3dabc5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java @@ -26,7 +26,6 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; import org.sonar.core.permission.ProjectPermissions; -import org.sonar.db.DbClient; import org.sonar.db.permission.template.PermissionTemplateDto; import org.sonar.server.permission.ws.PermissionsWsAction; import org.sonar.server.user.UserSession; @@ -44,13 +43,11 @@ public class SearchTemplatesAction implements PermissionsWsAction { private static final String PROPERTY_PREFIX = "projects_role."; private static final String DESCRIPTION_SUFFIX = ".desc"; - private final DbClient dbClient; private final UserSession userSession; private final I18n i18n; private final SearchTemplatesDataLoader dataLoader; - public SearchTemplatesAction(DbClient dbClient, UserSession userSession, I18n i18n, SearchTemplatesDataLoader dataLoader) { - this.dbClient = dbClient; + public SearchTemplatesAction(UserSession userSession, I18n i18n, SearchTemplatesDataLoader dataLoader) { this.userSession = userSession; this.i18n = i18n; this.dataLoader = dataLoader; diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java index 68e95989fe7..6bec7b28b36 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java @@ -40,8 +40,9 @@ import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.i18n.I18nRule; +import org.sonar.server.permission.ws.BasePermissionWsTest; +import org.sonar.server.permission.ws.PermissionsWsAction; import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.WsActionTester; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY; @@ -54,7 +55,7 @@ import static org.sonar.db.user.UserTesting.newUserDto; import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty; import static org.sonar.test.JsonAssert.assertJson; -public class SearchTemplatesActionTest { +public class SearchTemplatesActionTest extends BasePermissionWsTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); @@ -64,17 +65,13 @@ public class SearchTemplatesActionTest { @Rule public UserSessionRule userSession = UserSessionRule.standalone(); - WsActionTester ws; - I18nRule i18n = new I18nRule(); - DbClient dbClient = db.getDbClient(); - DbSession dbSession = db.getSession(); - ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); - SearchTemplatesDataLoader dataLoader; - SearchTemplatesAction underTest; + private I18nRule i18n = new I18nRule(); + private DbClient dbClient = db.getDbClient(); + private DbSession dbSession = db.getSession(); + private ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV"); - @Before - public void setUp() { - i18n.setProjectPermissions(); + @Override + protected PermissionsWsAction buildWsAction() { Settings settings = new MapSettings(); settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), UUID_EXAMPLE_01); @@ -82,12 +79,13 @@ public class SearchTemplatesActionTest { settings.setProperty(defaultRootQualifierTemplateProperty("DEV"), UUID_EXAMPLE_03); DefaultPermissionTemplateFinder defaultPermissionTemplateFinder = new DefaultPermissionTemplateFinder(settings, resourceTypes); + SearchTemplatesDataLoader dataLoader = new SearchTemplatesDataLoader(dbClient, defaultPermissionTemplateFinder); + return new SearchTemplatesAction(userSession, i18n, dataLoader); + } - dataLoader = new SearchTemplatesDataLoader(dbClient, defaultPermissionTemplateFinder); - underTest = new SearchTemplatesAction(dbClient, userSession, i18n, dataLoader); - - ws = new WsActionTester(underTest); - + @Before + public void setUp() { + i18n.setProjectPermissions(); userSession.login(); } @@ -122,7 +120,7 @@ public class SearchTemplatesActionTest { db.commit(); - String result = newRequest(); + String result = newRequest().execute().getInput(); assertJson(result) .withStrictArrayOrder() @@ -131,7 +129,7 @@ public class SearchTemplatesActionTest { @Test public void empty_result() { - String result = newRequest(); + String result = newRequest().execute().getInput(); assertJson(result) .withStrictArrayOrder() @@ -146,9 +144,10 @@ public class SearchTemplatesActionTest { insertDeveloperTemplate(); db.commit(); - String result = ws.newRequest() + String result = newRequest() .setParam(TEXT_QUERY, "views") - .execute().getInput(); + .execute() + .getInput(); assertThat(result).contains("Default template for Views") .doesNotContain("projects") @@ -160,12 +159,12 @@ public class SearchTemplatesActionTest { expectedException.expect(UnauthorizedException.class); userSession.anonymous(); - ws.newRequest().execute(); + newRequest().execute(); } @Test public void display_all_project_permissions() { - String result = newRequest(); + String result = newRequest().execute().getInput(); assertJson(result) .withStrictArrayOrder() @@ -173,10 +172,6 @@ public class SearchTemplatesActionTest { .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/display_all_project_permissions.json")); } - private String newRequest() { - return ws.newRequest().execute().getInput(); - } - private PermissionTemplateDto insertProjectTemplate() { return insertTemplate(newPermissionTemplateDto() .setOrganizationUuid(db.getDefaultOrganization().getUuid()) -- 2.39.5