public static final String API_ENDPOINT = "batch";
private final BatchIndex batchIndex;
- private final GlobalReferentialsAction globalReferentialsAction;
- private final ProjectReferentialsAction projectReferentialsAction;
+ private final GlobalRepositoryAction globalRepositoryAction;
+ private final ProjectRepositoryAction projectRepositoryAction;
- public BatchWs(BatchIndex batchIndex, GlobalReferentialsAction globalReferentialsAction, ProjectReferentialsAction projectReferentialsAction) {
+ public BatchWs(BatchIndex batchIndex, GlobalRepositoryAction globalRepositoryAction, ProjectRepositoryAction projectRepositoryAction) {
this.batchIndex = batchIndex;
- this.globalReferentialsAction = globalReferentialsAction;
- this.projectReferentialsAction = projectReferentialsAction;
+ this.globalRepositoryAction = globalRepositoryAction;
+ this.projectRepositoryAction = projectRepositoryAction;
}
@Override
defineIndexAction(controller);
defineFileAction(controller);
- globalReferentialsAction.define(controller);
- projectReferentialsAction.define(controller);
+ globalRepositoryAction.define(controller);
+ projectRepositoryAction.define(controller);
controller.done();
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch;
-
-import org.apache.commons.io.IOUtils;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.RequestHandler;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.batch.protocol.input.GlobalReferentials;
-import org.sonar.core.measure.db.MetricDto;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.properties.PropertiesDao;
-import org.sonar.core.properties.PropertyDto;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.plugins.MimeTypes;
-import org.sonar.server.user.UserSession;
-
-public class GlobalReferentialsAction implements RequestHandler {
-
- private final DbClient dbClient;
- private final PropertiesDao propertiesDao;
-
- public GlobalReferentialsAction(DbClient dbClient, PropertiesDao propertiesDao) {
- this.dbClient = dbClient;
- this.propertiesDao = propertiesDao;
- }
-
- void define(WebService.NewController controller) {
- controller.createAction("global")
- .setDescription("Return metrics and global properties")
- .setSince("4.5")
- .setInternal(true)
- .setHandler(this);
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- UserSession userSession = UserSession.get();
- boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
- boolean hasDryRunPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION);
-
- DbSession session = dbClient.openSession(false);
- try {
- GlobalReferentials ref = new GlobalReferentials();
- addMetrics(ref, session);
- addSettings(ref, hasScanPerm, hasDryRunPerm, session);
-
- response.stream().setMediaType(MimeTypes.JSON);
- IOUtils.write(ref.toJson(), response.stream().output());
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- private void addMetrics(GlobalReferentials ref, DbSession session) {
- for (MetricDto metric : dbClient.metricDao().findEnabled(session)) {
- Boolean optimizedBestValue = metric.isOptimizedBestValue();
- ref.addMetric(
- new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(),
- metric.getValueType(),
- metric.getDescription(),
- metric.getDirection(),
- metric.getName(),
- metric.isQualitative(),
- metric.isUserManaged(),
- metric.getWorstValue(),
- metric.getBestValue(),
- optimizedBestValue != null ? optimizedBestValue : false));
- }
- }
-
- private void addSettings(GlobalReferentials ref, boolean hasScanPerm, boolean hasDryRunPerm, DbSession session) {
- for (PropertyDto propertyDto : propertiesDao.selectGlobalProperties(session)) {
- String key = propertyDto.getKey();
- String value = propertyDto.getValue();
-
- if (isPropertyAllowed(key, hasScanPerm, hasDryRunPerm)) {
- ref.addGlobalSetting(key, value);
- }
- }
- }
-
- private static boolean isPropertyAllowed(String key, boolean hasScanPerm, boolean hasDryRunPerm) {
- return !key.contains(".secured") || hasScanPerm || (key.contains(".license") && hasDryRunPerm);
- }
-
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch;
+
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.batch.protocol.input.GlobalReferentials;
+import org.sonar.core.measure.db.MetricDto;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.plugins.MimeTypes;
+import org.sonar.server.user.UserSession;
+
+public class GlobalRepositoryAction implements RequestHandler {
+
+ private final DbClient dbClient;
+ private final PropertiesDao propertiesDao;
+
+ public GlobalRepositoryAction(DbClient dbClient, PropertiesDao propertiesDao) {
+ this.dbClient = dbClient;
+ this.propertiesDao = propertiesDao;
+ }
+
+ void define(WebService.NewController controller) {
+ controller.createAction("global")
+ .setDescription("Return metrics and global properties")
+ .setSince("4.5")
+ .setInternal(true)
+ .setHandler(this);
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ UserSession userSession = UserSession.get();
+ boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
+ boolean hasDryRunPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION);
+
+ DbSession session = dbClient.openSession(false);
+ try {
+ GlobalReferentials ref = new GlobalReferentials();
+ addMetrics(ref, session);
+ addSettings(ref, hasScanPerm, hasDryRunPerm, session);
+
+ response.stream().setMediaType(MimeTypes.JSON);
+ IOUtils.write(ref.toJson(), response.stream().output());
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ private void addMetrics(GlobalReferentials ref, DbSession session) {
+ for (MetricDto metric : dbClient.metricDao().findEnabled(session)) {
+ Boolean optimizedBestValue = metric.isOptimizedBestValue();
+ ref.addMetric(
+ new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(),
+ metric.getValueType(),
+ metric.getDescription(),
+ metric.getDirection(),
+ metric.getName(),
+ metric.isQualitative(),
+ metric.isUserManaged(),
+ metric.getWorstValue(),
+ metric.getBestValue(),
+ optimizedBestValue != null ? optimizedBestValue : false));
+ }
+ }
+
+ private void addSettings(GlobalReferentials ref, boolean hasScanPerm, boolean hasDryRunPerm, DbSession session) {
+ for (PropertyDto propertyDto : propertiesDao.selectGlobalProperties(session)) {
+ String key = propertyDto.getKey();
+ String value = propertyDto.getValue();
+
+ if (isPropertyAllowed(key, hasScanPerm, hasDryRunPerm)) {
+ ref.addGlobalSetting(key, value);
+ }
+ }
+ }
+
+ private static boolean isPropertyAllowed(String key, boolean hasScanPerm, boolean hasDryRunPerm) {
+ return !key.contains(".secured") || hasScanPerm || (key.contains(".license") && hasDryRunPerm);
+ }
+
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch;
-
-import org.apache.commons.io.IOUtils;
-import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.RequestHandler;
-import org.sonar.api.server.ws.Response;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.batch.protocol.input.ProjectReferentials;
-import org.sonar.server.plugins.MimeTypes;
-
-public class ProjectReferentialsAction implements RequestHandler {
-
- private static final String PARAM_KEY = "key";
- private static final String PARAM_PROFILE = "profile";
- private static final String PARAM_PREVIEW = "preview";
-
- private final ProjectReferentialsLoader projectReferentialsLoader;
-
- public ProjectReferentialsAction(ProjectReferentialsLoader projectReferentialsLoader) {
- this.projectReferentialsLoader = projectReferentialsLoader;
- }
-
- void define(WebService.NewController controller) {
- WebService.NewAction action = controller.createAction("project")
- .setDescription("Return project referentials")
- .setSince("4.5")
- .setInternal(true)
- .setHandler(this);
-
- action
- .createParam(PARAM_KEY)
- .setRequired(true)
- .setDescription("Project or module key")
- .setExampleValue("org.codehaus.sonar:sonar");
-
- action
- .createParam(PARAM_PROFILE)
- .setDescription("Profile name")
- .setExampleValue("SonarQube Way");
-
- action
- .createParam(PARAM_PREVIEW)
- .setDescription("Preview mode or not")
- .setDefaultValue(false)
- .setBooleanPossibleValues();
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- ProjectReferentials ref = projectReferentialsLoader.load(ProjectReferentialsQuery.create()
- .setModuleKey(request.mandatoryParam(PARAM_KEY))
- .setProfileName(request.param(PARAM_PROFILE))
- .setPreview(request.mandatoryParamAsBoolean(PARAM_PREVIEW)));
- response.stream().setMediaType(MimeTypes.JSON);
- IOUtils.write(ref.toJson(), response.stream().output());
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch;
-
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Multimap;
-import org.sonar.api.ServerComponent;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.batch.protocol.input.ProjectReferentials;
-import org.sonar.core.UtcDateUtils;
-import org.sonar.core.component.ComponentDto;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.properties.PropertyDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.qualityprofile.ActiveRule;
-import org.sonar.server.qualityprofile.QProfileFactory;
-import org.sonar.server.qualityprofile.QProfileLoader;
-import org.sonar.server.rule.Rule;
-import org.sonar.server.rule.RuleService;
-import org.sonar.server.user.UserSession;
-
-import javax.annotation.Nullable;
-
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static com.google.common.collect.Maps.newHashMap;
-
-public class ProjectReferentialsLoader implements ServerComponent {
-
- private final DbClient dbClient;
- private final QProfileFactory qProfileFactory;
- private final QProfileLoader qProfileLoader;
- private final RuleService ruleService;
- private final Languages languages;
-
- public ProjectReferentialsLoader(DbClient dbClient, QProfileFactory qProfileFactory, QProfileLoader qProfileLoader, RuleService ruleService,
- Languages languages) {
- this.dbClient = dbClient;
- this.qProfileFactory = qProfileFactory;
- this.qProfileLoader = qProfileLoader;
- this.ruleService = ruleService;
- this.languages = languages;
- }
-
- public ProjectReferentials load(ProjectReferentialsQuery query) {
- boolean hasScanPerm = UserSession.get().hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
- checkPermission(query.isPreview());
-
- DbSession session = dbClient.openSession(false);
- try {
- ProjectReferentials ref = new ProjectReferentials();
- String projectKey = query.getModuleKey();
- ComponentDto module = dbClient.componentDao().getNullableByKey(session, query.getModuleKey());
- // Current project/module can be null when analysing a new project
- if (module != null) {
- ComponentDto project = dbClient.componentDao().getNullableRootProjectByKey(query.getModuleKey(), session);
-
- // Can be null if the given project is a provisioned one
- if (project != null) {
- if (!project.key().equals(module.key())) {
- addSettings(ref, module.getKey(), getSettingsFromParents(module.key(), hasScanPerm, session));
- projectKey = project.key();
- }
-
- List<PropertyDto> moduleSettings = dbClient.propertiesDao().selectProjectProperties(query.getModuleKey(), session);
- List<ComponentDto> moduleChildren = dbClient.componentDao().findChildrenModulesFromModule(session, query.getModuleKey());
- List<PropertyDto> moduleChildrenSettings = newArrayList();
- if (!moduleChildren.isEmpty()) {
- moduleChildrenSettings = dbClient.propertiesDao().findChildrenModuleProperties(query.getModuleKey(), session);
- }
- TreeModuleSettings treeModuleSettings = new TreeModuleSettings(moduleChildren, moduleChildrenSettings, module, moduleSettings);
-
- addSettingsToChildrenModules(ref, query.getModuleKey(), Maps.<String, String>newHashMap(), treeModuleSettings, hasScanPerm, session);
- } else {
- // Add settings of the provisioned project
- addSettings(ref, query.getModuleKey(), getPropertiesMap(dbClient.propertiesDao().selectProjectProperties(query.getModuleKey(), session), hasScanPerm));
- }
- }
-
- addProfiles(ref, projectKey, query.getProfileName(), session);
- addActiveRules(ref);
- return ref;
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- private Map<String, String> getSettingsFromParents(String moduleKey, boolean hasScanPerm, DbSession session) {
- List<ComponentDto> parents = newArrayList();
- aggregateParentModules(moduleKey, parents, session);
- Collections.reverse(parents);
-
- Map<String, String> parentProperties = newHashMap();
- for (ComponentDto parent : parents) {
- parentProperties.putAll(getPropertiesMap(dbClient.propertiesDao().selectProjectProperties(parent.key(), session), hasScanPerm));
- }
- return parentProperties;
- }
-
- private void aggregateParentModules(String component, List<ComponentDto> parents, DbSession session) {
- ComponentDto parent = dbClient.componentDao().getParentModuleByKey(component, session);
- if (parent != null) {
- parents.add(parent);
- aggregateParentModules(parent.key(), parents, session);
- }
- }
-
- private void addSettingsToChildrenModules(ProjectReferentials ref, String moduleKey, Map<String, String> parentProperties, TreeModuleSettings treeModuleSettings,
- boolean hasScanPerm, DbSession session) {
- Map<String, String> currentParentProperties = newHashMap();
- currentParentProperties.putAll(parentProperties);
- currentParentProperties.putAll(getPropertiesMap(treeModuleSettings.findModuleSettings(moduleKey), hasScanPerm));
- addSettings(ref, moduleKey, currentParentProperties);
-
- for (ComponentDto childModule : treeModuleSettings.findChildrenModule(moduleKey)) {
- addSettings(ref, childModule.getKey(), currentParentProperties);
- addSettingsToChildrenModules(ref, childModule.getKey(), currentParentProperties, treeModuleSettings, hasScanPerm, session);
- }
- }
-
- private void addSettings(ProjectReferentials ref, String module, Map<String, String> properties) {
- if (!properties.isEmpty()) {
- ref.addSettings(module, properties);
- }
- }
-
- private Map<String, String> getPropertiesMap(List<PropertyDto> propertyDtos, boolean hasScanPerm) {
- Map<String, String> properties = newHashMap();
- for (PropertyDto propertyDto : propertyDtos) {
- String key = propertyDto.getKey();
- String value = propertyDto.getValue();
- if (isPropertyAllowed(key, hasScanPerm)) {
- properties.put(key, value);
- }
- }
- return properties;
- }
-
- private static boolean isPropertyAllowed(String key, boolean hasScanPerm) {
- return !key.contains(".secured") || hasScanPerm;
- }
-
- private void addProfiles(ProjectReferentials ref, @Nullable String projectKey, @Nullable String profileName, DbSession session) {
- for (Language language : languages.all()) {
- String languageKey = language.getKey();
- QualityProfileDto qualityProfileDto = getProfile(languageKey, projectKey, profileName, session);
- ref.addQProfile(new org.sonar.batch.protocol.input.QProfile(
- qualityProfileDto.getKey(),
- qualityProfileDto.getName(),
- qualityProfileDto.getLanguage(),
- UtcDateUtils.parseDateTime(qualityProfileDto.getRulesUpdatedAt())));
- }
- }
-
- /**
- * First try to find a quality profile matching the given name (if provided) and current language
- * If no profile found, try to find the quality profile set on the project (if provided)
- * If still no profile found, try to find the default profile of the language
- * <p/>
- * Never return null because a default profile should always be set on ech language
- */
- private QualityProfileDto getProfile(String languageKey, @Nullable String projectKey, @Nullable String profileName, DbSession session) {
- QualityProfileDto qualityProfileDto = profileName != null ? qProfileFactory.getByNameAndLanguage(session, profileName, languageKey) : null;
- if (qualityProfileDto == null && projectKey != null) {
- qualityProfileDto = qProfileFactory.getByProjectAndLanguage(session, projectKey, languageKey);
- }
- qualityProfileDto = qualityProfileDto != null ? qualityProfileDto : qProfileFactory.getDefault(session, languageKey);
- if (qualityProfileDto != null) {
- return qualityProfileDto;
- } else {
- throw new IllegalStateException(String.format("No quality profile can been found on language '%s' for project '%s'", languageKey, projectKey));
- }
- }
-
- private void addActiveRules(ProjectReferentials ref) {
- for (org.sonar.batch.protocol.input.QProfile qProfile : ref.qProfiles()) {
- for (ActiveRule activeRule : qProfileLoader.findActiveRulesByProfile(qProfile.key())) {
- Rule rule = ruleService.getNonNullByKey(activeRule.key().ruleKey());
- org.sonar.batch.protocol.input.ActiveRule inputActiveRule = new org.sonar.batch.protocol.input.ActiveRule(
- activeRule.key().ruleKey().repository(),
- activeRule.key().ruleKey().rule(),
- rule.name(),
- activeRule.severity(),
- rule.internalKey(),
- qProfile.language());
- for (Map.Entry<String, String> entry : activeRule.params().entrySet()) {
- inputActiveRule.addParam(entry.getKey(), entry.getValue());
- }
- ref.addActiveRule(inputActiveRule);
- }
- }
- }
-
- private void checkPermission(boolean preview) {
- UserSession userSession = UserSession.get();
- boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
- boolean hasPreviewPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION);
- if (!hasPreviewPerm && !hasScanPerm) {
- throw new ForbiddenException("You're not authorized to execute any SonarQube analysis. Please contact your SonarQube administrator.");
- }
- if (!preview && !hasScanPerm) {
- throw new ForbiddenException("You're only authorized to execute a local (dry run) SonarQube analysis without pushing the results to the SonarQube server. " +
- "Please contact your SonarQube administrator.");
- }
- }
-
- private static class TreeModuleSettings {
-
- private Map<String, Long> moduleIdsByKey;
- private Map<String, String> moduleUuidsByKey;
- private Multimap<Long, PropertyDto> propertiesByModuleId;
- private Multimap<String, ComponentDto> moduleChildrenByModuleUuid;
-
- private TreeModuleSettings(List<ComponentDto> moduleChildren, List<PropertyDto> moduleChildrenSettings, ComponentDto module, List<PropertyDto> moduleSettings) {
- propertiesByModuleId = ArrayListMultimap.create();
- moduleIdsByKey = newHashMap();
- moduleUuidsByKey = newHashMap();
- moduleChildrenByModuleUuid = ArrayListMultimap.create();
-
- for (PropertyDto settings : moduleChildrenSettings) {
- propertiesByModuleId.put(settings.getResourceId(), settings);
- }
- propertiesByModuleId.putAll(module.getId(), moduleSettings);
-
- moduleIdsByKey.put(module.key(), module.getId());
- moduleUuidsByKey.put(module.key(), module.uuid());
- for (ComponentDto componentDto : moduleChildren) {
- moduleIdsByKey.put(componentDto.key(), componentDto.getId());
- moduleUuidsByKey.put(componentDto.key(), componentDto.uuid());
- String moduleUuid = componentDto.moduleUuid();
- if (moduleUuid != null) {
- moduleChildrenByModuleUuid.put(moduleUuid, componentDto);
- } else {
- moduleChildrenByModuleUuid.put(module.uuid(), componentDto);
- }
- }
- }
-
- private List<PropertyDto> findModuleSettings(String moduleKey) {
- Long moduleId = moduleIdsByKey.get(moduleKey);
- return newArrayList(propertiesByModuleId.get(moduleId));
- }
-
- private List<ComponentDto> findChildrenModule(String moduleKey) {
- String moduleUuid = moduleUuidsByKey.get(moduleKey);
- return newArrayList(moduleChildrenByModuleUuid.get(moduleUuid));
- }
- }
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch;
-
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-
-public class ProjectReferentialsQuery {
-
- private String projectOrModuleKey;
- private String profileName;
- private boolean preview;
-
- private ProjectReferentialsQuery() {
- // No direct call
- }
-
- public boolean isPreview() {
- return preview;
- }
-
- public ProjectReferentialsQuery setPreview(boolean preview) {
- this.preview = preview;
- return this;
- }
-
- @CheckForNull
- public String getProfileName() {
- return profileName;
- }
-
- public ProjectReferentialsQuery setProfileName(@Nullable String profileName) {
- this.profileName = profileName;
- return this;
- }
-
- public String getModuleKey() {
- return projectOrModuleKey;
- }
-
- public ProjectReferentialsQuery setModuleKey(String projectOrModuleKey) {
- this.projectOrModuleKey = projectOrModuleKey;
- return this;
- }
-
- public static ProjectReferentialsQuery create(){
- return new ProjectReferentialsQuery();
- }
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch;
+
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+import org.sonar.server.plugins.MimeTypes;
+
+public class ProjectRepositoryAction implements RequestHandler {
+
+ private static final String PARAM_KEY = "key";
+ private static final String PARAM_PROFILE = "profile";
+ private static final String PARAM_PREVIEW = "preview";
+
+ private final ProjectRepositoryLoader projectReferentialsLoader;
+
+ public ProjectRepositoryAction(ProjectRepositoryLoader projectReferentialsLoader) {
+ this.projectReferentialsLoader = projectReferentialsLoader;
+ }
+
+ void define(WebService.NewController controller) {
+ WebService.NewAction action = controller.createAction("project")
+ .setDescription("Return project repository")
+ .setSince("4.5")
+ .setInternal(true)
+ .setHandler(this);
+
+ action
+ .createParam(PARAM_KEY)
+ .setRequired(true)
+ .setDescription("Project or module key")
+ .setExampleValue("org.codehaus.sonar:sonar");
+
+ action
+ .createParam(PARAM_PROFILE)
+ .setDescription("Profile name")
+ .setExampleValue("SonarQube Way");
+
+ action
+ .createParam(PARAM_PREVIEW)
+ .setDescription("Preview mode or not")
+ .setDefaultValue(false)
+ .setBooleanPossibleValues();
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ ProjectReferentials ref = projectReferentialsLoader.load(ProjectRepositoryQuery.create()
+ .setModuleKey(request.mandatoryParam(PARAM_KEY))
+ .setProfileName(request.param(PARAM_PROFILE))
+ .setPreview(request.mandatoryParamAsBoolean(PARAM_PREVIEW)));
+ response.stream().setMediaType(MimeTypes.JSON);
+ IOUtils.write(ref.toJson(), response.stream().output());
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
+import org.sonar.api.ServerComponent;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+import org.sonar.core.UtcDateUtils;
+import org.sonar.core.component.ComponentDto;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.properties.PropertyDto;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.qualityprofile.ActiveRule;
+import org.sonar.server.qualityprofile.QProfileFactory;
+import org.sonar.server.qualityprofile.QProfileLoader;
+import org.sonar.server.rule.Rule;
+import org.sonar.server.rule.RuleService;
+import org.sonar.server.user.UserSession;
+
+import javax.annotation.Nullable;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.collect.Maps.newHashMap;
+
+public class ProjectRepositoryLoader implements ServerComponent {
+
+ private final DbClient dbClient;
+ private final QProfileFactory qProfileFactory;
+ private final QProfileLoader qProfileLoader;
+ private final RuleService ruleService;
+ private final Languages languages;
+
+ public ProjectRepositoryLoader(DbClient dbClient, QProfileFactory qProfileFactory, QProfileLoader qProfileLoader, RuleService ruleService,
+ Languages languages) {
+ this.dbClient = dbClient;
+ this.qProfileFactory = qProfileFactory;
+ this.qProfileLoader = qProfileLoader;
+ this.ruleService = ruleService;
+ this.languages = languages;
+ }
+
+ public ProjectReferentials load(ProjectRepositoryQuery query) {
+ boolean hasScanPerm = UserSession.get().hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
+ checkPermission(query.isPreview());
+
+ DbSession session = dbClient.openSession(false);
+ try {
+ ProjectReferentials ref = new ProjectReferentials();
+ String projectKey = query.getModuleKey();
+ ComponentDto module = dbClient.componentDao().getNullableByKey(session, query.getModuleKey());
+ // Current project/module can be null when analysing a new project
+ if (module != null) {
+ ComponentDto project = dbClient.componentDao().getNullableRootProjectByKey(query.getModuleKey(), session);
+
+ // Can be null if the given project is a provisioned one
+ if (project != null) {
+ if (!project.key().equals(module.key())) {
+ addSettings(ref, module.getKey(), getSettingsFromParents(module.key(), hasScanPerm, session));
+ projectKey = project.key();
+ }
+
+ List<PropertyDto> moduleSettings = dbClient.propertiesDao().selectProjectProperties(query.getModuleKey(), session);
+ List<ComponentDto> moduleChildren = dbClient.componentDao().findChildrenModulesFromModule(session, query.getModuleKey());
+ List<PropertyDto> moduleChildrenSettings = newArrayList();
+ if (!moduleChildren.isEmpty()) {
+ moduleChildrenSettings = dbClient.propertiesDao().findChildrenModuleProperties(query.getModuleKey(), session);
+ }
+ TreeModuleSettings treeModuleSettings = new TreeModuleSettings(moduleChildren, moduleChildrenSettings, module, moduleSettings);
+
+ addSettingsToChildrenModules(ref, query.getModuleKey(), Maps.<String, String>newHashMap(), treeModuleSettings, hasScanPerm, session);
+ } else {
+ // Add settings of the provisioned project
+ addSettings(ref, query.getModuleKey(), getPropertiesMap(dbClient.propertiesDao().selectProjectProperties(query.getModuleKey(), session), hasScanPerm));
+ }
+ }
+
+ addProfiles(ref, projectKey, query.getProfileName(), session);
+ addActiveRules(ref);
+ return ref;
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ private Map<String, String> getSettingsFromParents(String moduleKey, boolean hasScanPerm, DbSession session) {
+ List<ComponentDto> parents = newArrayList();
+ aggregateParentModules(moduleKey, parents, session);
+ Collections.reverse(parents);
+
+ Map<String, String> parentProperties = newHashMap();
+ for (ComponentDto parent : parents) {
+ parentProperties.putAll(getPropertiesMap(dbClient.propertiesDao().selectProjectProperties(parent.key(), session), hasScanPerm));
+ }
+ return parentProperties;
+ }
+
+ private void aggregateParentModules(String component, List<ComponentDto> parents, DbSession session) {
+ ComponentDto parent = dbClient.componentDao().getParentModuleByKey(component, session);
+ if (parent != null) {
+ parents.add(parent);
+ aggregateParentModules(parent.key(), parents, session);
+ }
+ }
+
+ private void addSettingsToChildrenModules(ProjectReferentials ref, String moduleKey, Map<String, String> parentProperties, TreeModuleSettings treeModuleSettings,
+ boolean hasScanPerm, DbSession session) {
+ Map<String, String> currentParentProperties = newHashMap();
+ currentParentProperties.putAll(parentProperties);
+ currentParentProperties.putAll(getPropertiesMap(treeModuleSettings.findModuleSettings(moduleKey), hasScanPerm));
+ addSettings(ref, moduleKey, currentParentProperties);
+
+ for (ComponentDto childModule : treeModuleSettings.findChildrenModule(moduleKey)) {
+ addSettings(ref, childModule.getKey(), currentParentProperties);
+ addSettingsToChildrenModules(ref, childModule.getKey(), currentParentProperties, treeModuleSettings, hasScanPerm, session);
+ }
+ }
+
+ private void addSettings(ProjectReferentials ref, String module, Map<String, String> properties) {
+ if (!properties.isEmpty()) {
+ ref.addSettings(module, properties);
+ }
+ }
+
+ private Map<String, String> getPropertiesMap(List<PropertyDto> propertyDtos, boolean hasScanPerm) {
+ Map<String, String> properties = newHashMap();
+ for (PropertyDto propertyDto : propertyDtos) {
+ String key = propertyDto.getKey();
+ String value = propertyDto.getValue();
+ if (isPropertyAllowed(key, hasScanPerm)) {
+ properties.put(key, value);
+ }
+ }
+ return properties;
+ }
+
+ private static boolean isPropertyAllowed(String key, boolean hasScanPerm) {
+ return !key.contains(".secured") || hasScanPerm;
+ }
+
+ private void addProfiles(ProjectReferentials ref, @Nullable String projectKey, @Nullable String profileName, DbSession session) {
+ for (Language language : languages.all()) {
+ String languageKey = language.getKey();
+ QualityProfileDto qualityProfileDto = getProfile(languageKey, projectKey, profileName, session);
+ ref.addQProfile(new org.sonar.batch.protocol.input.QProfile(
+ qualityProfileDto.getKey(),
+ qualityProfileDto.getName(),
+ qualityProfileDto.getLanguage(),
+ UtcDateUtils.parseDateTime(qualityProfileDto.getRulesUpdatedAt())));
+ }
+ }
+
+ /**
+ * First try to find a quality profile matching the given name (if provided) and current language
+ * If no profile found, try to find the quality profile set on the project (if provided)
+ * If still no profile found, try to find the default profile of the language
+ * <p/>
+ * Never return null because a default profile should always be set on ech language
+ */
+ private QualityProfileDto getProfile(String languageKey, @Nullable String projectKey, @Nullable String profileName, DbSession session) {
+ QualityProfileDto qualityProfileDto = profileName != null ? qProfileFactory.getByNameAndLanguage(session, profileName, languageKey) : null;
+ if (qualityProfileDto == null && projectKey != null) {
+ qualityProfileDto = qProfileFactory.getByProjectAndLanguage(session, projectKey, languageKey);
+ }
+ qualityProfileDto = qualityProfileDto != null ? qualityProfileDto : qProfileFactory.getDefault(session, languageKey);
+ if (qualityProfileDto != null) {
+ return qualityProfileDto;
+ } else {
+ throw new IllegalStateException(String.format("No quality profile can been found on language '%s' for project '%s'", languageKey, projectKey));
+ }
+ }
+
+ private void addActiveRules(ProjectReferentials ref) {
+ for (org.sonar.batch.protocol.input.QProfile qProfile : ref.qProfiles()) {
+ for (ActiveRule activeRule : qProfileLoader.findActiveRulesByProfile(qProfile.key())) {
+ Rule rule = ruleService.getNonNullByKey(activeRule.key().ruleKey());
+ org.sonar.batch.protocol.input.ActiveRule inputActiveRule = new org.sonar.batch.protocol.input.ActiveRule(
+ activeRule.key().ruleKey().repository(),
+ activeRule.key().ruleKey().rule(),
+ rule.name(),
+ activeRule.severity(),
+ rule.internalKey(),
+ qProfile.language());
+ for (Map.Entry<String, String> entry : activeRule.params().entrySet()) {
+ inputActiveRule.addParam(entry.getKey(), entry.getValue());
+ }
+ ref.addActiveRule(inputActiveRule);
+ }
+ }
+ }
+
+ private void checkPermission(boolean preview) {
+ UserSession userSession = UserSession.get();
+ boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION);
+ boolean hasPreviewPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION);
+ if (!hasPreviewPerm && !hasScanPerm) {
+ throw new ForbiddenException("You're not authorized to execute any SonarQube analysis. Please contact your SonarQube administrator.");
+ }
+ if (!preview && !hasScanPerm) {
+ throw new ForbiddenException("You're only authorized to execute a local (dry run) SonarQube analysis without pushing the results to the SonarQube server. " +
+ "Please contact your SonarQube administrator.");
+ }
+ }
+
+ private static class TreeModuleSettings {
+
+ private Map<String, Long> moduleIdsByKey;
+ private Map<String, String> moduleUuidsByKey;
+ private Multimap<Long, PropertyDto> propertiesByModuleId;
+ private Multimap<String, ComponentDto> moduleChildrenByModuleUuid;
+
+ private TreeModuleSettings(List<ComponentDto> moduleChildren, List<PropertyDto> moduleChildrenSettings, ComponentDto module, List<PropertyDto> moduleSettings) {
+ propertiesByModuleId = ArrayListMultimap.create();
+ moduleIdsByKey = newHashMap();
+ moduleUuidsByKey = newHashMap();
+ moduleChildrenByModuleUuid = ArrayListMultimap.create();
+
+ for (PropertyDto settings : moduleChildrenSettings) {
+ propertiesByModuleId.put(settings.getResourceId(), settings);
+ }
+ propertiesByModuleId.putAll(module.getId(), moduleSettings);
+
+ moduleIdsByKey.put(module.key(), module.getId());
+ moduleUuidsByKey.put(module.key(), module.uuid());
+ for (ComponentDto componentDto : moduleChildren) {
+ moduleIdsByKey.put(componentDto.key(), componentDto.getId());
+ moduleUuidsByKey.put(componentDto.key(), componentDto.uuid());
+ String moduleUuid = componentDto.moduleUuid();
+ if (moduleUuid != null) {
+ moduleChildrenByModuleUuid.put(moduleUuid, componentDto);
+ } else {
+ moduleChildrenByModuleUuid.put(module.uuid(), componentDto);
+ }
+ }
+ }
+
+ private List<PropertyDto> findModuleSettings(String moduleKey) {
+ Long moduleId = moduleIdsByKey.get(moduleKey);
+ return newArrayList(propertiesByModuleId.get(moduleId));
+ }
+
+ private List<ComponentDto> findChildrenModule(String moduleKey) {
+ String moduleUuid = moduleUuidsByKey.get(moduleKey);
+ return newArrayList(moduleChildrenByModuleUuid.get(moduleUuid));
+ }
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class ProjectRepositoryQuery {
+
+ private String projectOrModuleKey;
+ private String profileName;
+ private boolean preview;
+
+ private ProjectRepositoryQuery() {
+ // No direct call
+ }
+
+ public boolean isPreview() {
+ return preview;
+ }
+
+ public ProjectRepositoryQuery setPreview(boolean preview) {
+ this.preview = preview;
+ return this;
+ }
+
+ @CheckForNull
+ public String getProfileName() {
+ return profileName;
+ }
+
+ public ProjectRepositoryQuery setProfileName(@Nullable String profileName) {
+ this.profileName = profileName;
+ return this;
+ }
+
+ public String getModuleKey() {
+ return projectOrModuleKey;
+ }
+
+ public ProjectRepositoryQuery setModuleKey(String projectOrModuleKey) {
+ this.projectOrModuleKey = projectOrModuleKey;
+ return this;
+ }
+
+ public static ProjectRepositoryQuery create() {
+ return new ProjectRepositoryQuery();
+ }
+}
// batch
pico.addSingleton(BatchIndex.class);
- pico.addSingleton(GlobalReferentialsAction.class);
- pico.addSingleton(ProjectReferentialsAction.class);
- pico.addSingleton(ProjectReferentialsLoader.class);
+ pico.addSingleton(GlobalRepositoryAction.class);
+ pico.addSingleton(ProjectRepositoryAction.class);
+ pico.addSingleton(ProjectRepositoryLoader.class);
pico.addSingleton(SubmitReportWsAction.class);
pico.addSingleton(BatchWs.class);
@Before
public void before() throws IOException {
tester = new WsTester(new BatchWs(batchIndex,
- new GlobalReferentialsAction(mock(DbClient.class), mock(PropertiesDao.class)),
- new ProjectReferentialsAction(mock(ProjectReferentialsLoader.class))));
+ new GlobalRepositoryAction(mock(DbClient.class), mock(PropertiesDao.class)),
+ new ProjectRepositoryAction(mock(ProjectRepositoryLoader.class))));
}
@Test
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.core.measure.db.MetricDto;
-import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.properties.PropertiesDao;
-import org.sonar.core.properties.PropertyDto;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.measure.persistence.MetricDao;
-import org.sonar.server.user.MockUserSession;
-import org.sonar.server.ws.WsTester;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public class GlobalReferentialsActionTest {
-
- @Mock
- DbSession session;
-
- @Mock
- MetricDao metricDao;
-
- @Mock
- PropertiesDao propertiesDao;
-
- WsTester tester;
-
- @Before
- public void setUp() throws Exception {
- DbClient dbClient = mock(DbClient.class);
- when(dbClient.openSession(false)).thenReturn(session);
- when(dbClient.metricDao()).thenReturn(metricDao);
-
- tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, propertiesDao), mock(ProjectReferentialsAction.class)));
- }
-
- @Test
- public void return_metrics() throws Exception {
- when(metricDao.findEnabled(session)).thenReturn(newArrayList(
- MetricDto.createFor("coverage").setDescription("Coverage by unit tests").setValueType("PERCENT").setQualitative(true)
- .setWorstValue(0d).setBestValue(100d).setOptimizedBestValue(false).setDirection(1).setEnabled(true)
- ));
-
- WsTester.TestRequest request = tester.newGetRequest("batch", "global");
- request.execute().assertJson(getClass(), "return_global_referentials.json");
- }
-
- @Test
- public void return_global_settings() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
-
- when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList(
- new PropertyDto().setKey("foo").setValue("bar"),
- new PropertyDto().setKey("foo.secured").setValue("1234"),
- new PropertyDto().setKey("foo.license.secured").setValue("5678")
- ));
-
- WsTester.TestRequest request = tester.newGetRequest("batch", "global");
- request.execute().assertJson(getClass(), "return_global_settings.json");
- }
-
- @Test
- public void return_only_license_settings_without_scan_but_with_preview_permission() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.DRY_RUN_EXECUTION);
-
- when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList(
- new PropertyDto().setKey("foo").setValue("bar"),
- new PropertyDto().setKey("foo.secured").setValue("1234"),
- new PropertyDto().setKey("foo.license.secured").setValue("5678")
- ));
-
- WsTester.TestRequest request = tester.newGetRequest("batch", "global");
- request.execute().assertJson(getClass(), "return_only_license_settings_without_scan_but_with_preview_permission.json");
- }
-
- @Test
- public void return_no_secured_settings_without_scan_and_preview_permission() throws Exception {
- MockUserSession.set().setLogin("john").setGlobalPermissions();
-
- when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList(
- new PropertyDto().setKey("foo").setValue("bar"),
- new PropertyDto().setKey("foo.secured").setValue("1234"),
- new PropertyDto().setKey("foo.license.secured").setValue("5678")
- ));
-
- WsTester.TestRequest request = tester.newGetRequest("batch", "global");
- request.execute().assertJson(getClass(), "return_no_secured_settings_without_scan_and_preview_permission.json");
- }
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.core.measure.db.MetricDto;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.measure.persistence.MetricDao;
+import org.sonar.server.user.MockUserSession;
+import org.sonar.server.ws.WsTester;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class GlobalRepositoryActionTest {
+
+ @Mock
+ DbSession session;
+
+ @Mock
+ MetricDao metricDao;
+
+ @Mock
+ PropertiesDao propertiesDao;
+
+ WsTester tester;
+
+ @Before
+ public void setUp() throws Exception {
+ DbClient dbClient = mock(DbClient.class);
+ when(dbClient.openSession(false)).thenReturn(session);
+ when(dbClient.metricDao()).thenReturn(metricDao);
+
+ tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalRepositoryAction(dbClient, propertiesDao), mock(ProjectRepositoryAction.class)));
+ }
+
+ @Test
+ public void return_metrics() throws Exception {
+ when(metricDao.findEnabled(session)).thenReturn(newArrayList(
+ MetricDto.createFor("coverage").setDescription("Coverage by unit tests").setValueType("PERCENT").setQualitative(true)
+ .setWorstValue(0d).setBestValue(100d).setOptimizedBestValue(false).setDirection(1).setEnabled(true)
+ ));
+
+ WsTester.TestRequest request = tester.newGetRequest("batch", "global");
+ request.execute().assertJson(getClass(), "return_global_referentials.json");
+ }
+
+ @Test
+ public void return_global_settings() throws Exception {
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION);
+
+ when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList(
+ new PropertyDto().setKey("foo").setValue("bar"),
+ new PropertyDto().setKey("foo.secured").setValue("1234"),
+ new PropertyDto().setKey("foo.license.secured").setValue("5678")
+ ));
+
+ WsTester.TestRequest request = tester.newGetRequest("batch", "global");
+ request.execute().assertJson(getClass(), "return_global_settings.json");
+ }
+
+ @Test
+ public void return_only_license_settings_without_scan_but_with_preview_permission() throws Exception {
+ MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.DRY_RUN_EXECUTION);
+
+ when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList(
+ new PropertyDto().setKey("foo").setValue("bar"),
+ new PropertyDto().setKey("foo.secured").setValue("1234"),
+ new PropertyDto().setKey("foo.license.secured").setValue("5678")
+ ));
+
+ WsTester.TestRequest request = tester.newGetRequest("batch", "global");
+ request.execute().assertJson(getClass(), "return_only_license_settings_without_scan_but_with_preview_permission.json");
+ }
+
+ @Test
+ public void return_no_secured_settings_without_scan_and_preview_permission() throws Exception {
+ MockUserSession.set().setLogin("john").setGlobalPermissions();
+
+ when(propertiesDao.selectGlobalProperties(session)).thenReturn(newArrayList(
+ new PropertyDto().setKey("foo").setValue("bar"),
+ new PropertyDto().setKey("foo.secured").setValue("1234"),
+ new PropertyDto().setKey("foo.license.secured").setValue("5678")
+ ));
+
+ WsTester.TestRequest request = tester.newGetRequest("batch", "global");
+ request.execute().assertJson(getClass(), "return_no_secured_settings_without_scan_and_preview_permission.json");
+ }
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.batch.protocol.input.ProjectReferentials;
-import org.sonar.server.ws.WsTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public class ProjectReferentialsActionTest {
-
- @Mock
- ProjectReferentialsLoader projectReferentialsLoader;
-
- WsTester tester;
-
- @Before
- public void setUp() throws Exception {
- tester = new WsTester(new BatchWs(mock(BatchIndex.class), mock(GlobalReferentialsAction.class),
- new ProjectReferentialsAction(projectReferentialsLoader)));
- }
-
- @Test
- public void project_referentials() throws Exception {
- String projectKey = "org.codehaus.sonar:sonar";
-
- ProjectReferentials projectReferentials = mock(ProjectReferentials.class);
- when(projectReferentials.toJson()).thenReturn("{\"settingsByModule\": {}}");
-
- ArgumentCaptor<ProjectReferentialsQuery> queryArgumentCaptor = ArgumentCaptor.forClass(ProjectReferentialsQuery.class);
- when(projectReferentialsLoader.load(queryArgumentCaptor.capture())).thenReturn(projectReferentials);
-
- WsTester.TestRequest request = tester.newGetRequest("batch", "project")
- .setParam("key", projectKey)
- .setParam("profile", "Default")
- .setParam("preview", "false");
- request.execute().assertJson("{\"settingsByModule\": {}}");
-
- assertThat(queryArgumentCaptor.getValue().getModuleKey()).isEqualTo(projectKey);
- assertThat(queryArgumentCaptor.getValue().getProfileName()).isEqualTo("Default");
- assertThat(queryArgumentCaptor.getValue().isPreview()).isFalse();
- }
-
-}
DbSession dbSession;
- ProjectReferentialsLoader loader;
+ ProjectRepositoryLoader loader;
@Before
public void before() {
tester.clearDbAndIndexes();
dbSession = tester.get(DbClient.class).openSession(false);
- loader = tester.get(ProjectReferentialsLoader.class);
+ loader = tester.get(ProjectRepositoryLoader.class);
}
@After
dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
Map<String, String> projectSettings = ref.settings(project.key());
assertThat(projectSettings).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john"
- ));
+ ));
}
@Test
dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()).setPreview(true));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()).setPreview(true));
Map<String, String> projectSettings = ref.settings(project.key());
assertThat(projectSettings).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR"
- ));
+ ));
}
@Test
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john"
- ));
+ ));
assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER",
"sonar.jira.login.secured", "john",
"sonar.coverage.exclusions", "**/*.java"
- ));
+ ));
}
@Test
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john"
- ));
+ ));
assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john"
- ));
+ ));
}
@Test
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john"
- ));
+ ));
assertThat(ref.settings(module.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER",
"sonar.jira.login.secured", "john",
"sonar.coverage.exclusions", "**/*.java"
- ));
+ ));
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER-DAO",
"sonar.jira.login.secured", "john",
"sonar.coverage.exclusions", "**/*.java"
- ));
+ ));
}
@Test
tester.get(DbClient.class).snapshotDao().insert(dbSession, SnapshotTesting.createForComponent(module2, projectSnapshot));
// Module 2 property
- tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR-APPLICATION").setResourceId(module2.getId()), dbSession);
+ tester.get(DbClient.class).propertiesDao()
+ .setProperty(new PropertyDto().setKey("sonar.jira.project.key").setValue("SONAR-APPLICATION").setResourceId(module2.getId()), dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john"
- ));
+ ));
assertThat(ref.settings(module1.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER",
"sonar.jira.login.secured", "john",
"sonar.coverage.exclusions", "**/*.java"
- ));
+ ));
assertThat(ref.settings(module2.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-APPLICATION",
"sonar.jira.login.secured", "john"
- ));
+ ));
}
@Test
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
assertThat(ref.settings(project.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john"
- ));
+ ));
}
@Test
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(subModule.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(subModule.key()));
assertThat(ref.settings(project.key())).isEmpty();
assertThat(ref.settings(module.key())).isEmpty();
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john",
"sonar.coverage.exclusions", "**/*.java"
- ));
+ ));
}
@Test
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(subModule.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(subModule.key()));
assertThat(ref.settings(project.key())).isEmpty();
assertThat(ref.settings(module.key())).isEmpty();
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john",
"sonar.coverage.exclusions", "**/*.java"
- ));
+ ));
}
@Test
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(subModule.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(subModule.key()));
assertThat(ref.settings(project.key())).isEmpty();
assertThat(ref.settings(module.key())).isEmpty();
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR",
"sonar.jira.login.secured", "john",
"sonar.coverage.exclusions", "**/*.java"
- ));
+ ));
}
@Test
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(subModule.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(subModule.key()));
assertThat(ref.settings(project.key())).isEmpty();
assertThat(ref.settings(module.key())).isEmpty();
assertThat(ref.settings(subModule.key())).isEqualTo(ImmutableMap.of(
"sonar.jira.project.key", "SONAR-SERVER",
"sonar.jira.login.secured", "john",
"sonar.coverage.exclusions", "**/*.java"
- ));
+ ));
}
@Test
tester.get(DbClient.class).componentDao().insert(dbSession, project);
tester.get(DbClient.class).snapshotDao().insert(dbSession, SnapshotTesting.createForProject(project));
- QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(DateUtils.formatDateTime(ruleUpdatedAt));
+ QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
+ DateUtils.formatDateTime(ruleUpdatedAt));
tester.get(DbClient.class).qualityProfileDao().insert(dbSession, profileDto);
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.profile.xoo").setValue("SonarQube way").setResourceId(project.getId()), dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
List<QProfile> profiles = newArrayList(ref.qProfiles());
assertThat(profiles).hasSize(1);
assertThat(profiles.get(0).key()).isEqualTo("abcd");
tester.get(DbClient.class).componentDao().insert(dbSession, project);
tester.get(DbClient.class).snapshotDao().insert(dbSession, SnapshotTesting.createForProject(project));
- QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(DateUtils.formatDateTime(ruleUpdatedAt));
+ QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
+ DateUtils.formatDateTime(ruleUpdatedAt));
tester.get(DbClient.class).qualityProfileDao().insert(dbSession, profileDto);
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.profile.xoo").setValue("SonarQube way"), dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
List<QProfile> profiles = newArrayList(ref.qProfiles());
assertThat(profiles).hasSize(1);
assertThat(profiles.get(0).key()).isEqualTo("abcd");
tester.get(DbClient.class).componentDao().insert(dbSession, project);
tester.get(DbClient.class).snapshotDao().insert(dbSession, SnapshotTesting.createForProject(project));
- QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(DateUtils.formatDateTime(ruleUpdatedAt));
+ QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
+ DateUtils.formatDateTime(ruleUpdatedAt));
tester.get(DbClient.class).qualityProfileDao().insert(dbSession, profileDto);
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.profile.xoo").setValue("SonarQube way"), dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()).setProfileName("SonarQube way"));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()).setProfileName("SonarQube way"));
List<QProfile> profiles = newArrayList(ref.qProfiles());
assertThat(profiles).hasSize(1);
assertThat(profiles.get(0).key()).isEqualTo("abcd");
MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
Date ruleUpdatedAt = DateUtils.parseDateTime("2014-01-14T13:00:00+0100");
- QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(DateUtils.formatDateTime(ruleUpdatedAt));
+ QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
+ DateUtils.formatDateTime(ruleUpdatedAt));
tester.get(DbClient.class).qualityProfileDao().insert(dbSession, profileDto);
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.profile.xoo").setValue("SonarQube way"), dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey("project"));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey("project"));
List<QProfile> profiles = newArrayList(ref.qProfiles());
assertThat(profiles).hasSize(1);
assertThat(profiles.get(0).key()).isEqualTo("abcd");
assertThat(profiles.get(0).rulesUpdatedAt()).isEqualTo(ruleUpdatedAt);
}
-
@Test
public void return_provisioned_project_profile() throws Exception {
MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
ComponentDto project = ComponentTesting.newProjectDto();
tester.get(DbClient.class).componentDao().insert(dbSession, project);
- QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(DateUtils.formatDateTime(ruleUpdatedAt));
+ QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
+ DateUtils.formatDateTime(ruleUpdatedAt));
tester.get(DbClient.class).qualityProfileDao().insert(dbSession, profileDto);
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.profile.xoo").setValue("SonarQube way").setResourceId(project.getId()), dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
List<QProfile> profiles = newArrayList(ref.qProfiles());
assertThat(profiles).hasSize(1);
assertThat(profiles.get(0).key()).isEqualTo("abcd");
dbSession.commit();
try {
- loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("No quality profile can been found on language 'xoo' for project 'org.codehaus.sonar:sonar'");
tester.get(DbClient.class).componentDao().insert(dbSession, project);
tester.get(DbClient.class).snapshotDao().insert(dbSession, SnapshotTesting.createForProject(project));
- QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(DateUtils.formatDateTime(ruleUpdatedAt));
+ QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
+ DateUtils.formatDateTime(ruleUpdatedAt));
tester.get(DbClient.class).qualityProfileDao().insert(dbSession, profileDto);
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.profile.xoo").setValue("SonarQube way"), dbSession);
dbSession.commit();
- ProjectReferentials ref = loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ ProjectReferentials ref = loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
List<ActiveRule> activeRules = newArrayList(ref.activeRules());
assertThat(activeRules).hasSize(1);
assertThat(activeRules.get(0).repositoryKey()).isEqualTo("squid");
dbSession.commit();
try {
- loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()));
+ loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(ForbiddenException.class).hasMessage("You're not authorized to execute any SonarQube analysis. Please contact your SonarQube administrator.");
dbSession.commit();
try {
- loader.load(ProjectReferentialsQuery.create().setModuleKey(project.key()).setPreview(false));
+ loader.load(ProjectRepositoryQuery.create().setModuleKey(project.key()).setPreview(false));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(ForbiddenException.class).hasMessage(
}
private void addDefaultProfile() {
- QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(DateUtils.formatDateTime(new Date()));
+ QualityProfileDto profileDto = QProfileTesting.newDto(QProfileName.createFor(ServerTester.Xoo.KEY, "SonarQube way"), "abcd").setRulesUpdatedAt(
+ DateUtils.formatDateTime(new Date()));
tester.get(DbClient.class).qualityProfileDao().insert(dbSession, profileDto);
tester.get(DbClient.class).propertiesDao().setProperty(new PropertyDto().setKey("sonar.profile.xoo").setValue("SonarQube way"), dbSession);
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+import org.sonar.server.ws.WsTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ProjectRepositoryActionTest {
+
+ @Mock
+ ProjectRepositoryLoader projectReferentialsLoader;
+
+ WsTester tester;
+
+ @Before
+ public void setUp() throws Exception {
+ tester = new WsTester(new BatchWs(mock(BatchIndex.class), mock(GlobalRepositoryAction.class),
+ new ProjectRepositoryAction(projectReferentialsLoader)));
+ }
+
+ @Test
+ public void project_referentials() throws Exception {
+ String projectKey = "org.codehaus.sonar:sonar";
+
+ ProjectReferentials projectReferentials = mock(ProjectReferentials.class);
+ when(projectReferentials.toJson()).thenReturn("{\"settingsByModule\": {}}");
+
+ ArgumentCaptor<ProjectRepositoryQuery> queryArgumentCaptor = ArgumentCaptor.forClass(ProjectRepositoryQuery.class);
+ when(projectReferentialsLoader.load(queryArgumentCaptor.capture())).thenReturn(projectReferentials);
+
+ WsTester.TestRequest request = tester.newGetRequest("batch", "project")
+ .setParam("key", projectKey)
+ .setParam("profile", "Default")
+ .setParam("preview", "false");
+ request.execute().assertJson("{\"settingsByModule\": {}}");
+
+ assertThat(queryArgumentCaptor.getValue().getModuleKey()).isEqualTo(projectKey);
+ assertThat(queryArgumentCaptor.getValue().getProfileName()).isEqualTo("Default");
+ assertThat(queryArgumentCaptor.getValue().isPreview()).isFalse();
+ }
+
+}
+++ /dev/null
-{
- "timestamp": 0,
- "metrics": [
- {
- "id": 0,
- "key": "coverage",
- "valueType": "PERCENT",
- "description": "Coverage by unit tests",
- "direction": 1,
- "name": "coverage",
- "qualitative": true,
- "userManaged": false,
- "worstValue": 0.0,
- "bestValue": 100.0,
- "optimizedBestValue": false
- }
- ],
- "globalSettings": {}
-}
+++ /dev/null
-{
- "timestamp": 0,
- "metrics": [],
- "globalSettings": {
- "foo" : "bar",
- "foo.secured" : "1234",
- "foo.license.secured" : "5678"
- }
-}
+++ /dev/null
-{
- "timestamp": 0,
- "metrics": [],
- "globalSettings": {
- "foo" : "bar"
- }
-}
+++ /dev/null
-{
- "timestamp": 0,
- "metrics": [],
- "globalSettings": {
- "foo" : "bar",
- "foo.license.secured" : "5678"
- }
-}
--- /dev/null
+{
+ "timestamp": 0,
+ "metrics": [
+ {
+ "id": 0,
+ "key": "coverage",
+ "valueType": "PERCENT",
+ "description": "Coverage by unit tests",
+ "direction": 1,
+ "name": "coverage",
+ "qualitative": true,
+ "userManaged": false,
+ "worstValue": 0.0,
+ "bestValue": 100.0,
+ "optimizedBestValue": false
+ }
+ ],
+ "globalSettings": {}
+}
--- /dev/null
+{
+ "timestamp": 0,
+ "metrics": [],
+ "globalSettings": {
+ "foo" : "bar",
+ "foo.secured" : "1234",
+ "foo.license.secured" : "5678"
+ }
+}
--- /dev/null
+{
+ "timestamp": 0,
+ "metrics": [],
+ "globalSettings": {
+ "foo" : "bar"
+ }
+}
--- /dev/null
+{
+ "timestamp": 0,
+ "metrics": [],
+ "globalSettings": {
+ "foo" : "bar",
+ "foo.license.secured" : "5678"
+ }
+}
+++ /dev/null
-{
- "timestamp": 0,
- "qprofilesByLanguage": {
- "java": {
- "key": "abcd",
- "name": "Default",
- "language": "java",
- "rulesUpdatedAt": "2014-01-14T13:00:00+0100"
- }
- },
- "activeRules": [
- {
- "repositoryKey": "squid",
- "ruleKey": "AvoidCycle",
- "name": "Avoid Cycle",
- "severity": "MINOR",
- "internalKey": "squid-1",
- "language": "java",
- "params": {
- "max" : "2"
- }
- }
- ],
- "settingsByModule": {
- "org.codehaus.sonar:sonar": {
- "sonar.jira.project.key": "SONAR",
- "sonar.jira.login.secured": "john"
- }
- },
- "fileDataByModuleAndPath": {}
-}
--- /dev/null
+{
+ "timestamp": 0,
+ "qprofilesByLanguage": {
+ "java": {
+ "key": "abcd",
+ "name": "Default",
+ "language": "java",
+ "rulesUpdatedAt": "2014-01-14T13:00:00+0100"
+ }
+ },
+ "activeRules": [
+ {
+ "repositoryKey": "squid",
+ "ruleKey": "AvoidCycle",
+ "name": "Avoid Cycle",
+ "severity": "MINOR",
+ "internalKey": "squid-1",
+ "language": "java",
+ "params": {
+ "max" : "2"
+ }
+ }
+ ],
+ "settingsByModule": {
+ "org.codehaus.sonar:sonar": {
+ "sonar.jira.project.key": "SONAR",
+ "sonar.jira.login.secured": "john"
+ }
+ },
+ "fileDataByModuleAndPath": {}
+}