From 155fc16bdb8bd4bf1c2b3f412bf807655787ac91 Mon Sep 17 00:00:00 2001 From: Jacek Date: Thu, 17 Dec 2020 08:59:16 +0100 Subject: [PATCH] SONAR-13999 drop organization usage from UI WS --- .../org/sonar/server/ui/PageRepository.java | 4 - .../sonar/server/ui/ws/ComponentAction.java | 4 +- .../org/sonar/server/ui/ws/GlobalAction.java | 12 +- .../server/ui/ws/NavigationWsModule.java | 1 - .../server/ui/ws/OrganizationAction.java | 162 ----------- .../sonar/server/ui/ws/global-example.json | 2 - .../sonar/server/ui/PageRepositoryTest.java | 47 +-- .../sonar/server/ui/ws/GlobalActionTest.java | 27 +- .../server/ui/ws/MarketplaceActionTest.java | 10 +- .../server/ui/ws/NavigationWsModuleTest.java | 2 +- .../server/ui/ws/OrganizationActionTest.java | 272 ------------------ 11 files changed, 29 insertions(+), 514 deletions(-) delete mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java delete mode 100644 server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/PageRepository.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/PageRepository.java index 070acdd6cda..f8cb576ed99 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/PageRepository.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/PageRepository.java @@ -42,7 +42,6 @@ import static java.util.Comparator.comparing; import static java.util.Objects.requireNonNull; import static org.sonar.api.web.page.Page.Scope.COMPONENT; import static org.sonar.api.web.page.Page.Scope.GLOBAL; -import static org.sonar.api.web.page.Page.Scope.ORGANIZATION; import static org.sonar.core.util.stream.MoreCollectors.toList; @ServerSide @@ -122,9 +121,6 @@ public class PageRepository implements Startable { return getPages(GLOBAL, isAdmin, null); } - public List getOrganizationPages(boolean isAdmin) { - return getPages(ORGANIZATION, isAdmin, null); - } public List getComponentPages(boolean isAdmin, String qualifierKey) { Qualifier qualifier = Qualifier.fromKey(qualifierKey); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java index 36607577f49..8504bfa7068 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java @@ -285,7 +285,7 @@ public class ComponentAction implements NavigationWsAction { boolean showBackgroundTasks = isProjectAdmin && (isProject || Qualifiers.VIEW.equals(component.qualifier()) || Qualifiers.APP.equals(component.qualifier())); boolean isQualityProfileAdmin = userSession.hasPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES); boolean isQualityGateAdmin = userSession.hasPermission(GlobalPermission.ADMINISTER_QUALITY_GATES); - boolean isOrganizationAdmin = userSession.hasPermission(GlobalPermission.ADMINISTER); + boolean isGlobalAdmin = userSession.hasPermission(GlobalPermission.ADMINISTER); boolean canBrowseProject = userSession.hasComponentPermission(USER, component); json.prop("showSettings", isProjectAdmin && componentTypeHasProperty(component, PROPERTY_CONFIGURABLE)); @@ -297,7 +297,7 @@ public class ComponentAction implements NavigationWsAction { json.prop("showHistory", isProjectAdmin && componentTypeHasProperty(component, PROPERTY_MODIFIABLE_HISTORY)); json.prop("showUpdateKey", isProjectAdmin && componentTypeHasProperty(component, PROPERTY_UPDATABLE_KEY)); json.prop("showBackgroundTasks", showBackgroundTasks); - json.prop("canApplyPermissionTemplate", isOrganizationAdmin); + json.prop("canApplyPermissionTemplate", isGlobalAdmin); json.prop("canBrowseProject", canBrowseProject); json.prop("canUpdateProjectVisibilityToPrivate", isProjectAdmin); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/GlobalAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/GlobalAction.java index f9a5a75d15c..c61985fdf43 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/GlobalAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/GlobalAction.java @@ -42,7 +42,6 @@ import org.sonar.server.almsettings.MultipleAlmFeatureProvider; import org.sonar.server.branch.BranchFeatureProxy; import org.sonar.server.issue.index.IssueIndexSyncProgressChecker; import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.OrganizationFlags; import org.sonar.server.platform.WebServer; import org.sonar.server.ui.PageRepository; import org.sonar.server.ui.VersionFormatter; @@ -79,7 +78,6 @@ public class GlobalAction implements NavigationWsAction, Startable { private final Server server; private final WebServer webServer; private final DbClient dbClient; - private final OrganizationFlags organizationFlags; private final DefaultOrganizationProvider defaultOrganizationProvider; private final BranchFeatureProxy branchFeature; private final UserSession userSession; @@ -89,7 +87,7 @@ public class GlobalAction implements NavigationWsAction, Startable { private final IssueIndexSyncProgressChecker issueIndexSyncChecker; public GlobalAction(PageRepository pageRepository, Configuration config, ResourceTypes resourceTypes, Server server, - WebServer webServer, DbClient dbClient, OrganizationFlags organizationFlags, + WebServer webServer, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider, BranchFeatureProxy branchFeature, UserSession userSession, PlatformEditionProvider editionProvider, MultipleAlmFeatureProvider multipleAlmFeatureProvider, WebAnalyticsLoader webAnalyticsLoader, IssueIndexSyncProgressChecker issueIndexSyncChecker) { this.pageRepository = pageRepository; @@ -98,7 +96,6 @@ public class GlobalAction implements NavigationWsAction, Startable { this.server = server; this.webServer = webServer; this.dbClient = dbClient; - this.organizationFlags = organizationFlags; this.defaultOrganizationProvider = defaultOrganizationProvider; this.branchFeature = branchFeature; this.userSession = userSession; @@ -201,11 +198,10 @@ public class GlobalAction implements NavigationWsAction, Startable { json.prop("productionDatabase", !dbClient.getDatabase().getDialect().getId().equals(H2.ID)); } + // TODO:: drop private void writeOrganizationSupport(JsonWriter json) { - try (DbSession dbSession = dbClient.openSession(false)) { - json.prop("organizationsEnabled", organizationFlags.isEnabled(dbSession)); - json.prop("defaultOrganization", defaultOrganizationProvider.get().getKey()); - } + json.prop("organizationsEnabled", false); + json.prop("defaultOrganization", defaultOrganizationProvider.get().getKey()); } private void writeBranchSupport(JsonWriter json) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/NavigationWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/NavigationWsModule.java index e4168dba9ed..03f791ec1af 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/NavigationWsModule.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/NavigationWsModule.java @@ -26,7 +26,6 @@ public class NavigationWsModule extends Module { protected void configureModule() { add( NavigationWs.class, - OrganizationAction.class, ComponentAction.class, GlobalAction.class, MarketplaceAction.class, diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java deleted file mode 100644 index 18f1be6c7dd..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/OrganizationAction.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2020 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.ui.ws; - -import com.google.common.base.Preconditions; -import java.util.List; -import java.util.Optional; -import javax.annotation.Nullable; -import org.sonar.api.server.ws.Change; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.text.JsonWriter; -import org.sonar.api.web.page.Page; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.alm.AlmAppInstallDto; -import org.sonar.db.alm.OrganizationAlmBindingDto; -import org.sonar.db.organization.OrganizationDto; -import org.sonar.server.organization.BillingValidations; -import org.sonar.server.organization.BillingValidationsProxy; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.project.ProjectDefaultVisibility; -import org.sonar.server.project.Visibility; -import org.sonar.server.ui.PageRepository; -import org.sonar.server.user.UserSession; - -import static org.sonar.db.permission.GlobalPermission.ADMINISTER; -import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional; -import static org.sonar.server.ws.KeyExamples.KEY_ORG_EXAMPLE_001; - -public class OrganizationAction implements NavigationWsAction { - - private static final String ACTION_NAME = "organization"; - private static final String PARAM_ORGANIZATION = "organization"; - - private final DbClient dbClient; - private final DefaultOrganizationProvider defaultOrganizationProvider; - private final UserSession userSession; - private final PageRepository pageRepository; - private final BillingValidationsProxy billingValidations; - private final ProjectDefaultVisibility projectDefaultVisibility; - - public OrganizationAction(DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider, UserSession userSession, PageRepository pageRepository, - BillingValidationsProxy billingValidations, ProjectDefaultVisibility projectDefaultVisibility) { - this.dbClient = dbClient; - this.defaultOrganizationProvider = defaultOrganizationProvider; - this.userSession = userSession; - this.pageRepository = pageRepository; - this.billingValidations = billingValidations; - this.projectDefaultVisibility = projectDefaultVisibility; - } - - @Override - public void define(WebService.NewController context) { - WebService.NewAction projectNavigation = context.createAction(ACTION_NAME) - .setDescription("Get information concerning organization navigation for the current user") - .setHandler(this) - .setInternal(true) - .setResponseExample(getClass().getResource("organization-example.json")) - .setSince("6.3") - .setChangelog(new Change("6.4", "The field 'projectVisibility' is added")); - - projectNavigation.createParam(PARAM_ORGANIZATION) - .setRequired(true) - .setDescription("the organization key") - .setExampleValue(KEY_ORG_EXAMPLE_001); - } - - @Override - public void handle(Request request, Response response) throws Exception { - String organizationKey = request.mandatoryParam(PARAM_ORGANIZATION); - try (DbSession dbSession = dbClient.openSession(false)) { - OrganizationDto organization = checkFoundWithOptional( - dbClient.organizationDao().selectByKey(dbSession, organizationKey), - "No organization with key '%s'", organizationKey); - - boolean newProjectPrivate = projectDefaultVisibility.get(dbSession).isPrivate(); - Optional optOrganizationAlmBinding = dbClient.organizationAlmBindingDao().selectByOrganization(dbSession, organization); - - JsonWriter json = response.newJsonWriter(); - json.beginObject(); - if (optOrganizationAlmBinding.isPresent()) { - OrganizationAlmBindingDto organizationAlmBinding = optOrganizationAlmBinding.get(); - Optional almAppInstall = dbClient.almAppInstallDao().selectByUuid(dbSession, organizationAlmBinding.getAlmAppInstallUuid()); - Preconditions.checkState(almAppInstall.isPresent(), "Failed to get ALM information for organization '{}': '{}' application is not installed.", - organization.getName(), organizationAlmBinding.getAlm().getId()); - writeOrganization(json, organization, newProjectPrivate, organizationAlmBinding, almAppInstall.get()); - } else { - writeOrganization(json, organization, newProjectPrivate, null, null); - } - json.endObject() - .close(); - } - } - - private void writeOrganization(JsonWriter json, OrganizationDto organization, boolean newProjectPrivate, @Nullable OrganizationAlmBindingDto organizationAlmBinding, - @Nullable AlmAppInstallDto almAppInstall) { - json.name("organization") - .beginObject() - .prop("isDefault", organization.getKey().equals(defaultOrganizationProvider.get().getKey())) - .prop("projectVisibility", Visibility.getLabel(newProjectPrivate)) - .prop("subscription", organization.getSubscription().name()) - .prop("canUpdateProjectsVisibilityToPrivate", - userSession.hasPermission(ADMINISTER) && - billingValidations.canUpdateProjectVisibilityToPrivate(new BillingValidations.Organization(organization.getKey(), organization.getUuid(), organization.getName()))); - - if (organizationAlmBinding != null && almAppInstall != null) { - writeAlm(json, organizationAlmBinding, almAppInstall); - } - - writeOrganizationPages(json, organization); - } - - private static void writeAlm(JsonWriter json, OrganizationAlmBindingDto organizationAlmBinding, AlmAppInstallDto almAppInstall) { - json - .name("alm") - .beginObject() - .prop("key", organizationAlmBinding.getAlm().getId()) - .prop("url", organizationAlmBinding.getUrl()) - .prop("membersSync", organizationAlmBinding.isMembersSyncEnable()) - .prop("personal", almAppInstall.isOwnerUser()) - .endObject(); - } - - private void writeOrganizationPages(JsonWriter json, OrganizationDto organization) { - json.name("pages"); - writePages(json, pageRepository.getOrganizationPages(false)); - if (userSession.hasPermission(ADMINISTER)) { - json.name("adminPages"); - writePages(json, pageRepository.getOrganizationPages(true)); - } - json.endObject(); - } - - private static void writePages(JsonWriter json, List pages) { - json.beginArray(); - pages.forEach(p -> json.beginObject() - .prop("key", p.getKey()) - .prop("name", p.getName()) - .endObject()); - json.endArray(); - } - -} diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/ui/ws/global-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/ui/ws/global-example.json index 97dd61de33f..700eee264bc 100644 --- a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/ui/ws/global-example.json +++ b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/ui/ws/global-example.json @@ -25,9 +25,7 @@ ], "version": "6.2", "productionDatabase": true, - "organizationsEnabled": false, "branchesEnabled": false, - "defaultOrganization": "key_foo", "canAdmin": false, "standalone": true, "edition": "community" diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/PageRepositoryTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/PageRepositoryTest.java index 5891857473f..c7ee2f8a6b9 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/PageRepositoryTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/PageRepositoryTest.java @@ -29,9 +29,9 @@ import org.sonar.api.utils.log.LogTester; import org.sonar.api.web.page.Page; import org.sonar.api.web.page.Page.Qualifier; import org.sonar.api.web.page.PageDefinition; +import org.sonar.core.extension.CoreExtensionRepository; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginRepository; -import org.sonar.core.extension.CoreExtensionRepository; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; @@ -40,7 +40,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.api.web.page.Page.Scope.COMPONENT; import static org.sonar.api.web.page.Page.Scope.GLOBAL; -import static org.sonar.api.web.page.Page.Scope.ORGANIZATION; public class PageRepositoryTest { @@ -50,7 +49,7 @@ public class PageRepositoryTest { public LogTester logTester = new LogTester(); private PluginRepository pluginRepository = mock(PluginRepository.class); - private CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class); + private final CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class); private PageRepository underTest = new PageRepository(pluginRepository, coreExtensionRepository); @@ -66,7 +65,7 @@ public class PageRepositoryTest { .addPage(Page.builder("my_plugin/K1").setName("N1").build()) .addPage(Page.builder("my_plugin/K3").setName("N3").build()); PageDefinition secondPlugin = context -> context.addPage(Page.builder("my_plugin/K2").setName("N2").build()); - underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{firstPlugin, secondPlugin}); + underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[] {firstPlugin, secondPlugin}); underTest.start(); List result = underTest.getAllPages(); @@ -89,7 +88,7 @@ public class PageRepositoryTest { .addPage(Page.builder("my_plugin/K4").setName("K4").setScope(GLOBAL).build()) .addPage(Page.builder("my_plugin/K5").setName("K5").setScope(COMPONENT).setComponentQualifiers(Qualifier.VIEW).build()) .addPage(Page.builder("my_plugin/K6").setName("K6").setScope(COMPONENT).setComponentQualifiers(Qualifier.APP).build()); - underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{plugin}); + underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[] {plugin}); underTest.start(); List result = underTest.getComponentPages(false, Qualifiers.PROJECT); @@ -114,7 +113,7 @@ public class PageRepositoryTest { .addPage(Page.builder("my_plugin/K1").setName("N1").build()) .addPage(Page.builder("my_plugin/K2").setName("N2").build()) .addPage(Page.builder("my_plugin/K3").setName("N3").build()); - underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{plugin}); + underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[] {plugin}); underTest.start(); List result = underTest.getGlobalPages(false); @@ -124,40 +123,6 @@ public class PageRepositoryTest { .containsExactly("my_plugin/K1", "my_plugin/K2", "my_plugin/K3"); } - @Test - public void get_organization_pages() { - PageDefinition plugin = context -> context - .addPage(Page.builder("my_plugin/G1").setName("G1").setScope(GLOBAL).build()) - .addPage(Page.builder("my_plugin/C1").setName("C1").setScope(COMPONENT).build()) - .addPage(Page.builder("my_plugin/O1").setName("O1").setScope(ORGANIZATION).build()) - .addPage(Page.builder("my_plugin/O2").setName("O2").setScope(ORGANIZATION).build()) - .addPage(Page.builder("my_plugin/O3").setName("O3").setScope(ORGANIZATION).build()) - .addPage(Page.builder("my_plugin/OA1").setName("OA1").setScope(ORGANIZATION).setAdmin(true).build()); - underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{plugin}); - underTest.start(); - - List result = underTest.getOrganizationPages(false); - - assertThat(result) - .extracting(Page::getKey) - .containsExactly("my_plugin/O1", "my_plugin/O2", "my_plugin/O3"); - } - - @Test - public void get_organization_admin_pages() { - PageDefinition plugin = context -> context - .addPage(Page.builder("my_plugin/O1").setName("O1").setScope(ORGANIZATION).build()) - .addPage(Page.builder("my_plugin/O2").setName("O2").setScope(ORGANIZATION).setAdmin(true).build()); - underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{plugin}); - underTest.start(); - - List result = underTest.getOrganizationPages(true); - - assertThat(result) - .extracting(Page::getKey) - .containsExactly("my_plugin/O2"); - } - @Test public void fail_if_pages_called_before_server_startup() { expectedException.expect(NullPointerException.class); @@ -172,7 +137,7 @@ public class PageRepositoryTest { PageDefinition plugin42 = context -> context.addPage(Page.builder("plugin_42/my_key").setName("N2").build()); pluginRepository = mock(PluginRepository.class); when(pluginRepository.hasPlugin("governance")).thenReturn(true); - underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[]{governance, plugin42}); + underTest = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[] {governance, plugin42}); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Page 'N2' references plugin 'plugin_42' that does not exist"); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java index 19ce056cb69..8f932035ead 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java @@ -41,7 +41,6 @@ import org.sonar.server.almsettings.MultipleAlmFeatureProvider; import org.sonar.server.issue.index.IssueIndexSyncProgressChecker; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; -import org.sonar.server.organization.TestOrganizationFlags; import org.sonar.server.platform.WebServer; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ui.PageRepository; @@ -61,18 +60,17 @@ public class GlobalActionTest { @Rule public UserSessionRule userSession = UserSessionRule.standalone(); - private MapSettings settings = new MapSettings(); + private final MapSettings settings = new MapSettings(); - private Server server = mock(Server.class); - private WebServer webServer = mock(WebServer.class); - private DbClient dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS); - private IssueIndexSyncProgressChecker indexSyncProgressChecker = mock(IssueIndexSyncProgressChecker.class); - private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone(); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.fromUuid("foo"); - private BranchFeatureRule branchFeature = new BranchFeatureRule(); - private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); - private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class); - private WebAnalyticsLoader webAnalyticsLoader = mock(WebAnalyticsLoader.class); + private final Server server = mock(Server.class); + private final WebServer webServer = mock(WebServer.class); + private final DbClient dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS); + private final IssueIndexSyncProgressChecker indexSyncProgressChecker = mock(IssueIndexSyncProgressChecker.class); + private final DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.fromUuid("foo"); + private final BranchFeatureRule branchFeature = new BranchFeatureRule(); + private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); + private final MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class); + private final WebAnalyticsLoader webAnalyticsLoader = mock(WebAnalyticsLoader.class); private WsActionTester ws; @@ -239,10 +237,9 @@ public class GlobalActionTest { @Test public void organization_support() { init(); - organizationFlags.setEnabled(true); assertJson(call()).isSimilarTo("{" + - " \"organizationsEnabled\": true," + + " \"organizationsEnabled\": false," + " \"defaultOrganization\": \"key_foo\"" + "}"); } @@ -389,7 +386,7 @@ public class GlobalActionTest { }}); pageRepository.start(); GlobalAction wsAction = new GlobalAction(pageRepository, settings.asConfig(), new ResourceTypes(resourceTypeTrees), server, - webServer, dbClient, organizationFlags, defaultOrganizationProvider, branchFeature, userSession, editionProvider, multipleAlmFeatureProvider, webAnalyticsLoader, + webServer, dbClient, defaultOrganizationProvider, branchFeature, userSession, editionProvider, multipleAlmFeatureProvider, webAnalyticsLoader, indexSyncProgressChecker); ws = new WsActionTester(wsAction); wsAction.start(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java index 6dca76a8206..7908fc2dbed 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/MarketplaceActionTest.java @@ -32,7 +32,6 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.metric.MetricDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; -import org.sonar.server.organization.DefaultOrganizationProviderImpl; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; @@ -45,7 +44,6 @@ import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; import static org.sonar.api.measures.Metric.ValueType.INT; import static org.sonar.test.JsonAssert.assertJson; - @RunWith(DataProviderRunner.class) public class MarketplaceActionTest { @@ -56,11 +54,11 @@ public class MarketplaceActionTest { @Rule public DbTester db = DbTester.create(); - private Server server = mock(Server.class); - private DbClient dbClient = db.getDbClient(); - private MarketplaceAction underTest = new MarketplaceAction(userSessionRule, server, dbClient); + private final Server server = mock(Server.class); + private final DbClient dbClient = db.getDbClient(); + private final MarketplaceAction underTest = new MarketplaceAction(userSessionRule, server, dbClient); - private WsActionTester ws = new WsActionTester(underTest); + private final WsActionTester ws = new WsActionTester(underTest); @Test public void definition() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/NavigationWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/NavigationWsModuleTest.java index af9c1d7c1b6..d85aa50fc9a 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/NavigationWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/NavigationWsModuleTest.java @@ -30,6 +30,6 @@ public class NavigationWsModuleTest { public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new NavigationWsModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 6); + assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 5); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java deleted file mode 100644 index e9cfbbea51d..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/OrganizationActionTest.java +++ /dev/null @@ -1,272 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2020 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.ui.ws; - -import javax.annotation.Nullable; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.server.ws.Change; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; -import org.sonar.api.web.page.Page; -import org.sonar.api.web.page.PageDefinition; -import org.sonar.core.extension.CoreExtensionRepository; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.PluginRepository; -import org.sonar.db.DbClient; -import org.sonar.db.DbTester; -import org.sonar.db.alm.AlmAppInstallDto; -import org.sonar.db.alm.OrganizationAlmBindingDto; -import org.sonar.db.organization.OrganizationDto; -import org.sonar.server.organization.BillingValidations; -import org.sonar.server.organization.BillingValidationsProxy; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.TestDefaultOrganizationProvider; -import org.sonar.server.project.ProjectDefaultVisibility; -import org.sonar.server.project.Visibility; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ui.PageRepository; -import org.sonar.server.ws.TestRequest; -import org.sonar.server.ws.TestResponse; -import org.sonar.server.ws.WsActionTester; -import org.sonar.updatecenter.common.Version; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.tuple; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.sonar.api.web.page.Page.Scope.ORGANIZATION; -import static org.sonar.db.organization.OrganizationDto.Subscription.FREE; -import static org.sonar.db.organization.OrganizationDto.Subscription.PAID; -import static org.sonar.db.organization.OrganizationDto.Subscription.SONARQUBE; -import static org.sonar.db.permission.GlobalPermission.ADMINISTER; -import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS; -import static org.sonar.test.JsonAssert.assertJson; - -public class OrganizationActionTest { - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private final DbClient dbClient = db.getDbClient(); - private final DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private final PageRepository pageRepository = mock(PageRepository.class); - private final BillingValidationsProxy billingValidations = mock(BillingValidationsProxy.class); - private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class); - - private WsActionTester ws = new WsActionTester( - new OrganizationAction(dbClient, defaultOrganizationProvider, userSession, pageRepository, billingValidations, projectDefaultVisibility)); - - @Before - public void before() { - when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE); - } - - @Test - public void filter_out_admin_pages_when_user_is_not_admin() { - initWithPages( - Page.builder("my-plugin/org-page").setName("Organization page").setScope(ORGANIZATION).build(), - Page.builder("my-plugin/org-admin-page").setName("Organization admin page").setScope(ORGANIZATION).setAdmin(true).build()); - OrganizationDto organization = db.organizations().insert(); - userSession.logIn() - .addPermission(PROVISION_PROJECTS); - - TestResponse response = executeRequest(organization); - - assertThat(response.getInput()) - .contains("my-plugin/org-page") - .doesNotContain("my-plugin/org-admin-page"); - } - - @Test - public void returns_project_visibility_private() { - OrganizationDto organization = db.organizations().insert(); - userSession.logIn().addPermission(PROVISION_PROJECTS); - - when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE); - TestResponse response = executeRequest(organization); - - assertJson(response.getInput()).isSimilarTo("{\"organization\": {\"projectVisibility\": \"private\"}}"); - } - - @Test - public void returns_project_visibility_public() { - when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PUBLIC); - userSession.logIn().addPermission(PROVISION_PROJECTS); - - OrganizationDto organization = db.organizations().insert(); - TestResponse response = executeRequest(organization); - - assertJson(response.getInput()).isSimilarTo("{\"organization\": {\"projectVisibility\": \"public\"}}"); - } - - @Test - public void returns_non_admin_and_canUpdateProjectsVisibilityToPrivate_false_when_user_logged_in_but_not_admin_and_extension_returns_true() { - OrganizationDto defaultOrganization = db.getDefaultOrganization(); - - userSession.logIn(); - when(billingValidations.canUpdateProjectVisibilityToPrivate(any(BillingValidations.Organization.class))).thenReturn(true); - verifyCanUpdateProjectsVisibilityToPrivateResponse(executeRequest(db.getDefaultOrganization()), false); - - userSession.logIn().addPermission(ADMINISTER); - when(billingValidations.canUpdateProjectVisibilityToPrivate(any(BillingValidations.Organization.class))).thenReturn(false); - verifyCanUpdateProjectsVisibilityToPrivateResponse(executeRequest(db.getDefaultOrganization()), false); - - userSession.logIn().addPermission(ADMINISTER); - when(billingValidations.canUpdateProjectVisibilityToPrivate(any(BillingValidations.Organization.class))).thenReturn(true); - verifyCanUpdateProjectsVisibilityToPrivateResponse(executeRequest(db.getDefaultOrganization()), true); - } - - @Test - public void return_FREE_subscription_flag() { - OrganizationDto freeOrganization = db.organizations().insert(o -> o.setSubscription(FREE)); - - TestResponse response = executeRequest(freeOrganization); - - assertJson(response.getInput()).isSimilarTo("{\"organization\": {\"subscription\": \"FREE\"}}"); - } - - @Test - public void return_SONARQUBE_subscription_flag() { - OrganizationDto sonarQubeOrganization = db.organizations().insert(o -> o.setSubscription(SONARQUBE)); - - TestResponse response = executeRequest(sonarQubeOrganization); - - assertJson(response.getInput()).isSimilarTo("{\"organization\": {\"subscription\": \"SONARQUBE\"}}"); - } - - @Test - public void return_PAID_subscription_flag() { - OrganizationDto paidOrganization = db.organizations().insert(o -> o.setSubscription(PAID)); - - TestResponse response = executeRequest(paidOrganization); - - assertJson(response.getInput()).isSimilarTo("{\"organization\": {\"subscription\": \"PAID\"}}"); - } - - @Test - public void return_PAID_subscription_flag_when_not_member_on_private_organization_with_public_project() { - OrganizationDto paidOrganization = db.organizations().insert(o -> o.setSubscription(PAID)); - db.components().insertPublicProject(paidOrganization); - userSession.anonymous(); - - TestResponse response = executeRequest(paidOrganization); - - assertJson(response.getInput()).isSimilarTo("{\"organization\": {\"subscription\": \"PAID\"}}"); - } - - @Test - public void return_alm_binding() { - OrganizationDto organization = db.organizations().insert(); - AlmAppInstallDto almAppInstall = db.alm().insertAlmAppInstall(); - OrganizationAlmBindingDto organizationAlmBinding = db.alm().insertOrganizationAlmBinding(organization, almAppInstall, true); - - TestResponse response = executeRequest(organization); - - assertJson(response.getInput()).isSimilarTo("{\"organization\": " + - " {" + - " \"alm\": {" + - " \"key\": \"" + organizationAlmBinding.getAlm().getId() + "\"," + - " \"url\": \"" + organizationAlmBinding.getUrl() + "\"," + - " \"membersSync\": " + organizationAlmBinding.isMembersSyncEnable() + "," + - " \"personal\": " + almAppInstall.isOwnerUser() + - " }" + - " }" + - "}"); - } - - @Test - public void fail_with_IAE_if_parameter_organization_is_not_specified() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("The 'organization' parameter is missing"); - - executeRequest(null); - } - - @Test - public void json_example() { - initWithPages( - Page.builder("my-plugin/org-page").setName("Organization page").setScope(ORGANIZATION).build(), - Page.builder("my-plugin/org-admin-page").setName("Organization admin page").setScope(ORGANIZATION).setAdmin(true).build()); - OrganizationDto organization = db.organizations().insert(); - userSession.logIn() - .addPermission(ADMINISTER) - .addPermission(PROVISION_PROJECTS); - - TestResponse response = executeRequest(organization); - - assertJson(response.getInput()).isSimilarTo(ws.getDef().responseExampleAsString()); - } - - @Test - public void verify_definition() { - WebService.Action def = ws.getDef(); - - assertThat(def.isInternal()).isTrue(); - assertThat(def.description()).isEqualTo("Get information concerning organization navigation for the current user"); - assertThat(def.since()).isEqualTo("6.3"); - assertThat(def.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactlyInAnyOrder( - tuple("6.4", "The field 'projectVisibility' is added")); - - assertThat(def.params()).hasSize(1); - WebService.Param organization = def.param("organization"); - assertThat(organization.description()).isEqualTo("the organization key"); - assertThat(organization.isRequired()).isTrue(); - assertThat(organization.exampleValue()).isEqualTo("my-org"); - } - - private void initWithPages(Page... pages) { - PluginRepository pluginRepository = mock(PluginRepository.class); - when(pluginRepository.hasPlugin(any())).thenReturn(true); - when(pluginRepository.getPluginInfo(any())).thenReturn(new PluginInfo("unused").setVersion(Version.create("1.0"))); - CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class); - when(coreExtensionRepository.isInstalled(any())).thenReturn(false); - PageRepository pageRepository = new PageRepository(pluginRepository, coreExtensionRepository, new PageDefinition[] {context -> { - for (Page page : pages) { - context.addPage(page); - } - }}); - pageRepository.start(); - ws = new WsActionTester(new OrganizationAction(dbClient, defaultOrganizationProvider, userSession, pageRepository, billingValidations, projectDefaultVisibility)); - } - - private TestResponse executeRequest(@Nullable OrganizationDto organization) { - TestRequest request = ws.newRequest(); - if (organization != null) { - request.setParam("organization", organization.getKey()); - } - return request.execute(); - } - - private static void verifyCanUpdateProjectsVisibilityToPrivateResponse(TestResponse response, boolean canUpdateProjectsVisibilityToPrivate) { - assertJson(response.getInput()) - .isSimilarTo("{" + - " \"organization\": {" + - " \"canUpdateProjectsVisibilityToPrivate\": " + canUpdateProjectsVisibilityToPrivate + "," + - " }" + - "}"); - } -} -- 2.39.5