import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.NewAction;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
import org.sonar.server.qualityprofile.QProfile;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.util.LanguageParamUtils;
import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
import static java.lang.String.format;
import static java.util.function.Function.identity;
import static org.sonar.api.utils.DateUtils.formatDateTime;
+import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
-import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.*;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SEARCH;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_NAME;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_KEY;
public class SearchAction implements QProfileWsAction {
private final SearchDataLoader dataLoader;
private final Languages languages;
+ private final ActiveRuleIndex activeRuleIndex;
+ private final DbClient dbClient;
- public SearchAction(SearchDataLoader dataLoader, Languages languages) {
+ public SearchAction(SearchDataLoader dataLoader, Languages languages, ActiveRuleIndex activeRuleIndex, DbClient dbClient) {
this.dataLoader = dataLoader;
this.languages = languages;
+ this.activeRuleIndex = activeRuleIndex;
+ this.dbClient = dbClient;
}
@Override
}
private static SearchWsRequest toSearchWsRequest(Request request) {
- return new SearchWsRequest()
+ return new SearchWsRequest()
.setOrganizationKey(request.param(PARAM_ORGANIZATION))
.setProjectKey(request.param(PARAM_PROJECT_KEY))
.setProfileName(request.param(PARAM_PROFILE_NAME))
@VisibleForTesting
SearchWsResponse doHandle(SearchWsRequest request) {
- SearchData data = dataLoader.load(request);
+ validateRequest(request);
+ SearchData data = load(request);
return buildResponse(data);
}
+ private SearchData load(SearchWsRequest request) {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ return new SearchData()
+ .setProfiles(dataLoader.findProfiles(dbSession, request))
+ .setActiveRuleCountByProfileKey(activeRuleIndex.countAllByQualityProfileKey())
+ .setActiveDeprecatedRuleCountByProfileKey(activeRuleIndex.countAllDeprecatedByQualityProfileKey())
+ .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileKey(dbSession));
+ }
+ }
+
+ private static void validateRequest(SearchWsRequest request) {
+ boolean hasLanguage = request.getLanguage() != null;
+ boolean isDefault = request.getDefaults();
+ boolean hasComponentKey = request.getProjectKey() != null;
+ boolean hasProfileName = request.getProfileName() != null;
+
+ checkRequest(!hasLanguage || (!hasComponentKey && !hasProfileName && !isDefault),
+ "The language parameter cannot be provided at the same time than the component key or profile name.");
+ checkRequest(!isDefault || !hasComponentKey, "The default parameter cannot be provided at the same time than the component key.");
+ checkRequest(!hasProfileName || hasComponentKey || isDefault, "The name parameter requires either projectKey or defaults to be set.");
+ }
+
private SearchWsResponse buildResponse(SearchData data) {
List<QProfile> profiles = data.getProfiles();
Map<String, QProfile> profilesByKey = profiles.stream().collect(Collectors.toMap(QProfile::key, identity()));
import org.sonar.server.qualityprofile.QProfile;
import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileLookup;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
import static java.lang.String.format;
-import static org.sonar.server.ws.WsUtils.checkRequest;
public class SearchDataLoader {
private final QProfileFactory profileFactory;
private final DbClient dbClient;
private final ComponentFinder componentFinder;
- private final ActiveRuleIndex activeRuleIndex;
-
private final QProfileWsSupport qProfileWsSupport;
public SearchDataLoader(Languages languages, QProfileLookup profileLookup, QProfileFactory profileFactory, DbClient dbClient,
- ComponentFinder componentFinder, ActiveRuleIndex activeRuleIndex, QProfileWsSupport qProfileWsSupport) {
+ ComponentFinder componentFinder, QProfileWsSupport qProfileWsSupport) {
this.languages = languages;
this.profileLookup = profileLookup;
this.profileFactory = profileFactory;
this.dbClient = dbClient;
this.componentFinder = componentFinder;
- this.activeRuleIndex = activeRuleIndex;
this.qProfileWsSupport = qProfileWsSupport;
}
- SearchData load(SearchWsRequest request) {
- validateRequest(request);
-
- try (DbSession dbSession = dbClient.openSession(false)) {
- return new SearchData()
- .setProfiles(findProfiles(dbSession, request))
- .setActiveRuleCountByProfileKey(activeRuleIndex.countAllByQualityProfileKey())
- .setActiveDeprecatedRuleCountByProfileKey(activeRuleIndex.countAllDeprecatedByQualityProfileKey())
- .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileKey(dbSession));
- }
- }
-
@VisibleForTesting
List<QProfile> findProfiles(DbSession dbSession, SearchWsRequest request) {
OrganizationDto organization = qProfileWsSupport.getOrganizationByKey(dbSession, request.getOrganizationKey());
.collect(Collectors.toList());
}
- @VisibleForTesting
- static void validateRequest(SearchWsRequest request) {
- boolean hasLanguage = hasLanguage(request);
- boolean isDefault = askDefaultProfiles(request);
- boolean hasComponentKey = hasComponentKey(request);
- boolean hasProfileName = hasProfileName(request);
-
- checkRequest(!hasLanguage || (!hasComponentKey && !hasProfileName && !isDefault),
- "The language parameter cannot be provided at the same time than the component key or profile name.");
- checkRequest(!isDefault || !hasComponentKey, "The default parameter cannot be provided at the same time than the component key.");
- checkRequest(!hasProfileName || hasComponentKey || isDefault, "The name parameter requires either projectKey or defaults to be set.");
- }
-
private static boolean askDefaultProfiles(SearchWsRequest request) {
return request.getDefaults();
}
- private static boolean hasProfileName(SearchWsRequest request) {
- return request.getProfileName() != null;
- }
-
private static boolean hasComponentKey(SearchWsRequest request) {
return request.getProjectKey() != null;
}
-
- private static boolean hasLanguage(SearchWsRequest request) {
- return request.getLanguage() != null;
- }
}
import org.sonar.server.language.LanguageTesting;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.qualityprofile.QProfileExporters;
import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileService;
import org.sonar.server.tester.UserSessionRule;
new RemoveProjectAction(projectAssociationParameters, null, null, dbClient),
new CreateAction(null, null, null, languages, wsSupport, userSessionRule, null, importers),
new ImportersAction(importers),
- new SearchAction(null, languages),
+ new SearchAction(null, languages, null, dbClient),
new SetDefaultAction(languages, null, null, wsSupport),
new ProjectsAction(null, userSessionRule),
new ChangelogAction(null, mock(QProfileFactory.class), languages, dbClient),
import org.sonar.db.qualityprofile.QualityProfileDao;
import org.sonar.db.qualityprofile.QualityProfileDbTester;
import org.sonar.db.qualityprofile.QualityProfileDto;
+import org.sonar.db.qualityprofile.QualityProfileTesting;
import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.language.LanguageTesting;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
public class SearchActionTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
- QualityProfileDbTester qualityProfileDb = new QualityProfileDbTester(db);
- ComponentDbTester componentDb = new ComponentDbTester(db);
- DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
- QualityProfileDao qualityProfileDao = dbClient.qualityProfileDao();
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+ private QualityProfileDbTester qualityProfileDb = new QualityProfileDbTester(db);
+ private ComponentDbTester componentDb = new ComponentDbTester(db);
+ private DbClient dbClient = db.getDbClient();
+ private DbSession dbSession = db.getSession();
+ private QualityProfileDao qualityProfileDao = dbClient.qualityProfileDao();
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private ActiveRuleIndex activeRuleIndex = mock(ActiveRuleIndex.class);
new QProfileLookup(dbClient),
new QProfileFactory(dbClient, UuidFactoryFast.getInstance(), System2.INSTANCE),
dbClient,
- new ComponentFinder(dbClient), activeRuleIndex, qProfileWsSupport),
- languages);
+ new ComponentFinder(dbClient), qProfileWsSupport),
+ languages,
+ activeRuleIndex,
+ dbClient);
ws = new WsActionTester(underTest);
}
"ORG1-D");
}
+
+ @Test
+ public void name_and_default_query_is_valid() throws Exception {
+ minimalValidSetup();
+
+ SearchWsRequest request = new SearchWsRequest()
+ .setProfileName("bla")
+ .setDefaults(true);
+
+ assertThat(findProfiles(request).getProfilesList()).isNotNull();
+ }
+
+ @Test
+ public void name_and_component_query_is_valid() throws Exception {
+ minimalValidSetup();
+ ComponentDto project = db.components().insertProject();
+
+ SearchWsRequest request = new SearchWsRequest()
+ .setProfileName("bla")
+ .setProjectKey(project.key());
+
+ assertThat(findProfiles(request).getProfilesList()).isNotNull();
+ }
+
+ @Test
+ public void name_requires_either_component_or_defaults() throws Exception {
+ SearchWsRequest request = new SearchWsRequest()
+ .setProfileName("bla");
+
+ thrown.expect(BadRequestException.class);
+ thrown.expectMessage("The name parameter requires either projectKey or defaults to be set.");
+
+ findProfiles(request);
+ }
+
+ @Test
+ public void default_and_component_cannot_be_set_at_same_time() throws Exception {
+ SearchWsRequest request = new SearchWsRequest()
+ .setDefaults(true)
+ .setProjectKey("blubb");
+
+ thrown.expect(BadRequestException.class);
+ thrown.expectMessage("The default parameter cannot be provided at the same time than the component key.");
+
+ findProfiles(request);
+ }
+
+ @Test
+ public void language_and_component_cannot_be_set_at_same_time() throws Exception {
+ SearchWsRequest request = new SearchWsRequest()
+ .setLanguage("xoo")
+ .setProjectKey("bla");
+
+ thrown.expect(BadRequestException.class);
+ thrown.expectMessage("The language parameter cannot be provided at the same time than the component key or profile name.");
+
+ findProfiles(request);
+ }
+
+ @Test
+ public void language_and_name_cannot_be_set_at_same_time() throws Exception {
+ SearchWsRequest request = new SearchWsRequest()
+ .setLanguage("xoo")
+ .setProfileName("bla");
+
+ thrown.expect(BadRequestException.class);
+ thrown.expectMessage("The language parameter cannot be provided at the same time than the component key or profile name.");
+
+ findProfiles(request);
+ }
+
+ private void minimalValidSetup() {
+ for (Language language : Arrays.asList(xoo1, xoo2)) {
+ db.qualityProfiles().insertQualityProfile(
+ QualityProfileTesting.newQualityProfileDto()
+ .setOrganizationUuid(getDefaultOrganization().getUuid())
+ .setLanguage(language.getKey())
+ .setDefault(true));
+ }
+ }
+
private QualityProfileDto createProfile(String keySuffix, Language language, OrganizationDto org, String name, boolean isDefault) {
return QualityProfileDto.createFor(org.getKey() + "-" + keySuffix)
.setOrganizationUuid(org.getUuid())
.setDefault(isDefault);
}
+ private SearchWsResponse findProfiles(SearchWsRequest request) {
+ return underTest.doHandle(request);
+ }
+
private SearchWsResponse call(TestRequest request) {
try {
return SearchWsResponse.parseFrom(request
}
private OrganizationDto getDefaultOrganization() {
- return qProfileWsSupport.getOrganizationByKey(db.getSession(), defaultOrganizationProvider.get().getKey());
+ return db.getDefaultOrganization();
}
}
*/
package org.sonar.server.qualityprofile.ws;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Consumer;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
+import org.sonar.core.util.UuidFactoryFast;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualityprofile.QualityProfileDto;
+import org.sonar.db.qualityprofile.QualityProfileTesting;
+import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.language.LanguageTesting;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
+import org.sonar.server.qualityprofile.QProfile;
+import org.sonar.server.qualityprofile.QProfileFactory;
+import org.sonar.server.qualityprofile.QProfileLookup;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+
public class SearchDataLoaderTest {
@Rule
- public ExpectedException thrown = ExpectedException.none();
+ public DbTester dbTester = DbTester.create(System2.INSTANCE);
- @Test
- public void name_and_default_query_is_valid() throws Exception {
- SearchWsRequest request = new SearchWsRequest()
- .setProfileName("bla")
- .setDefaults(true);
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
- SearchDataLoader.validateRequest(request);
+ private Languages languages;
+ private QProfileLookup profileLookup;
+ private QProfileFactory profileFactory;
+ private ComponentFinder componentFinder;
+ private ActiveRuleIndex activeRuleIndex;
+ private QProfileWsSupport qProfileWsSupport;
+ private OrganizationDto organization;
+
+ @Before
+ public void before() {
+ organization = dbTester.organizations().insert();
+ languages = new Languages();
+ DbClient dbClient = dbTester.getDbClient();
+ profileLookup = new QProfileLookup(dbClient);
+ TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
+ qProfileWsSupport = new QProfileWsSupport(dbClient, null, defaultOrganizationProvider);
+ profileFactory = new QProfileFactory(dbClient, UuidFactoryFast.getInstance(), new AlwaysIncreasingSystem2());
+ componentFinder = mock(ComponentFinder.class);
}
@Test
- public void name_and_component_query_is_valid() throws Exception {
- SearchWsRequest request = new SearchWsRequest()
- .setProfileName("bla")
- .setProjectKey("blubb");
-
- SearchDataLoader.validateRequest(request);
+ public void find_no_profiles_if_database_is_empty() throws Exception {
+ assertThat(findProfiles(
+ new SearchWsRequest()
+ )).isEmpty();
}
@Test
- public void name_requires_either_component_or_defaults() throws Exception {
- SearchWsRequest request = new SearchWsRequest()
- .setProfileName("bla");
-
- thrown.expect(BadRequestException.class);
- thrown.expectMessage("The name parameter requires either projectKey or defaults to be set.");
-
- SearchDataLoader.validateRequest(request);
+ public void findAll_in_default_organization() throws Exception {
+ insertQualityProfile(dbTester.getDefaultOrganization());
+ assertThat(findProfiles(
+ new SearchWsRequest()
+ )).hasSize(1);
}
@Test
- public void default_and_component_cannot_be_set_at_same_time() throws Exception {
- SearchWsRequest request = new SearchWsRequest()
- .setDefaults(true)
- .setProjectKey("blubb");
-
- thrown.expect(BadRequestException.class);
- thrown.expectMessage("The default parameter cannot be provided at the same time than the component key.");
+ public void findAll_in_specific_organization() throws Exception {
+ insertQualityProfile(organization);
+ assertThat(findProfiles(
+ new SearchWsRequest()
+ .setOrganizationKey(organization.getKey())
+ )).hasSize(1);
+ }
- SearchDataLoader.validateRequest(request);
+ @Test
+ public void findDefaults_in_specific_organization() throws Exception {
+ insertQualityProfile(organization, dto -> dto.setDefault(true));
+ assertThat(findProfiles(
+ new SearchWsRequest()
+ .setOrganizationKey(organization.getKey())
+ .setDefaults(true)
+ )).hasSize(1);
}
@Test
- public void language_and_component_cannot_be_set_at_same_time() throws Exception {
- SearchWsRequest request = new SearchWsRequest()
- .setLanguage("xoo")
- .setProjectKey("bla");
+ public void findForProject_in_specific_organization() throws Exception {
+ insertQualityProfile(organization, dto -> dto.setDefault(true));
+ ComponentDto project1 = insertProject();
+ assertThat(findProfiles(
+ new SearchWsRequest()
+ .setOrganizationKey(organization.getKey())
+ .setProjectKey(project1.getKey())
+ )).hasSize(1);
+ }
- thrown.expect(BadRequestException.class);
- thrown.expectMessage("The language parameter cannot be provided at the same time than the component key or profile name.");
+ @Test
+ public void findAllForLanguage_in_specific_organization() throws Exception {
+ QualityProfileDto qualityProfile = insertQualityProfile(organization, dto -> dto.setDefault(true));
+ assertThat(findProfiles(
+ new SearchWsRequest()
+ .setOrganizationKey(organization.getKey())
+ .setLanguage(qualityProfile.getLanguage())
+ )).hasSize(1);
+ assertThat(findProfiles(
+ new SearchWsRequest()
+ .setOrganizationKey(organization.getKey())
+ .setLanguage("other language")
+ )).hasSize(0);
+ }
- SearchDataLoader.validateRequest(request);
+ private List<QProfile> findProfiles(SearchWsRequest request) {
+ return new SearchDataLoader(languages, profileLookup, profileFactory, dbTester.getDbClient(), componentFinder, qProfileWsSupport)
+ .findProfiles(dbTester.getSession(), request);
}
- @Test
- public void language_and_name_cannot_be_set_at_same_time() throws Exception {
- SearchWsRequest request = new SearchWsRequest()
- .setLanguage("xoo")
- .setProfileName("bla");
+ private QualityProfileDto insertQualityProfile(OrganizationDto organization, Consumer<QualityProfileDto>... specials) {
+ Language language = insertLanguage();
+ QualityProfileDto qualityProfile = QualityProfileTesting.newQualityProfileDto()
+ .setOrganizationUuid(organization.getUuid())
+ .setLanguage(language.getKey());
+ Arrays.stream(specials)
+ .forEachOrdered(c -> c.accept(qualityProfile));
+ dbTester.qualityProfiles().insertQualityProfile(qualityProfile);
+ return qualityProfile;
+ }
- thrown.expect(BadRequestException.class);
- thrown.expectMessage("The language parameter cannot be provided at the same time than the component key or profile name.");
+ private Language insertLanguage() {
+ Language language = LanguageTesting.newLanguage(randomAlphanumeric(20));
+ languages.add(language);
+ return language;
+ }
- SearchDataLoader.validateRequest(request);
+ private ComponentDto insertProject() {
+ ComponentDto project = dbTester.components().insertProject(organization);
+ doReturn(project).when(componentFinder).getByKey(any(DbSession.class), eq(project.getKey()));
+ return project;
}
}
+++ /dev/null
-/*
- * 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.Arrays;
-import java.util.List;
-import java.util.function.Consumer;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.api.utils.System2;
-import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
-import org.sonar.core.util.UuidFactoryFast;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.db.qualityprofile.QualityProfileTesting;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.language.LanguageTesting;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
-import org.sonar.server.qualityprofile.QProfile;
-import org.sonar.server.qualityprofile.QProfileFactory;
-import org.sonar.server.qualityprofile.QProfileLookup;
-import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
-import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
-
-import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-
-
-public class SearchDataLoaderTest2 {
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
-
- private Languages languages;
- private QProfileLookup profileLookup;
- private QProfileFactory profileFactory;
- private ComponentFinder componentFinder;
- private ActiveRuleIndex activeRuleIndex;
- private QProfileWsSupport qProfileWsSupport;
- private OrganizationDto organization;
-
- @Before
- public void before() {
- organization = dbTester.organizations().insert();
- languages = new Languages();
- DbClient dbClient = dbTester.getDbClient();
- profileLookup = new QProfileLookup(dbClient);
- TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
- qProfileWsSupport = new QProfileWsSupport(dbClient, null, defaultOrganizationProvider);
- profileFactory = new QProfileFactory(dbClient, UuidFactoryFast.getInstance(), new AlwaysIncreasingSystem2());
- componentFinder = mock(ComponentFinder.class);
- }
-
- @Test
- public void find_no_profiles_if_database_is_empty() throws Exception {
- assertThat(run(
- new SearchWsRequest()
- )).isEmpty();
- }
-
- @Test
- public void findAll_in_default_organization() throws Exception {
- insertQualityProfile(dbTester.getDefaultOrganization());
- assertThat(run(
- new SearchWsRequest()
- )).hasSize(1);
- }
-
- @Test
- public void findAll_in_specific_organization() throws Exception {
- insertQualityProfile(organization);
- assertThat(run(
- new SearchWsRequest()
- .setOrganizationKey(organization.getKey())
- )).hasSize(1);
- }
-
- @Test
- public void findDefaults_in_specific_organization() throws Exception {
- insertQualityProfile(organization, dto -> dto.setDefault(true));
- assertThat(run(
- new SearchWsRequest()
- .setOrganizationKey(organization.getKey())
- .setDefaults(true)
- )).hasSize(1);
- }
-
- @Test
- public void findForProject_in_specific_organization() throws Exception {
- insertQualityProfile(organization, dto -> dto.setDefault(true));
- ComponentDto project1 = insertProject();
- assertThat(run(
- new SearchWsRequest()
- .setOrganizationKey(organization.getKey())
- .setProjectKey(project1.getKey())
- )).hasSize(1);
- }
-
- @Test
- public void findAllForLanguage_in_specific_organization() throws Exception {
- QualityProfileDto qualityProfile = insertQualityProfile(organization, dto -> dto.setDefault(true));
- assertThat(run(
- new SearchWsRequest()
- .setOrganizationKey(organization.getKey())
- .setLanguage(qualityProfile.getLanguage())
- )).hasSize(1);
- assertThat(run(
- new SearchWsRequest()
- .setOrganizationKey(organization.getKey())
- .setLanguage("other language")
- )).hasSize(0);
- }
-
- private List<QProfile> run(SearchWsRequest request) {
- return createLoader().findProfiles(dbTester.getSession(), request);
- }
-
- private SearchDataLoader createLoader() {
- return new SearchDataLoader(languages, profileLookup, profileFactory, dbTester.getDbClient(), componentFinder, activeRuleIndex, qProfileWsSupport);
- }
-
- private QualityProfileDto insertQualityProfile(OrganizationDto organization, Consumer<QualityProfileDto>... specials) {
- Language language = insertLanguage();
- QualityProfileDto qualityProfile = QualityProfileTesting.newQualityProfileDto()
- .setOrganizationUuid(organization.getUuid())
- .setLanguage(language.getKey());
- Arrays.stream(specials)
- .forEachOrdered(c -> c.accept(qualityProfile));
- dbTester.qualityProfiles().insertQualityProfile(qualityProfile);
- return qualityProfile;
- }
-
- private Language insertLanguage() {
- Language language = LanguageTesting.newLanguage(randomAlphanumeric(20));
- languages.add(language);
- return language;
- }
-
- private ComponentDto insertProject() {
- ComponentDto project = dbTester.components().insertProject(organization);
- doReturn(project).when(componentFinder).getByKey(any(DbSession.class), eq(project.getKey()));
- return project;
- }
-}