import org.sonar.server.es.ProjectIndexer;
import org.sonar.server.es.ProjectIndexers;
+import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Collections.singletonList;
@ServerSide
}
public void delete(DbSession dbSession, ComponentDto project) {
- if (hasNotProjectScope(project) || isNotDeletable(project)) {
- throw new IllegalArgumentException("Only projects can be deleted");
- }
+ checkArgument(!hasNotProjectScope(project) && !isNotDeletable(project) && project.getMainBranchProjectUuid() == null, "Only projects can be deleted");
dbClient.purgeDao().deleteProject(dbSession, project.uuid());
projectIndexers.commitAndIndex(dbSession, singletonList(project), ProjectIndexer.Cause.PROJECT_DELETION);
}
}
private static ComponentDto checkComponent(Optional<ComponentDto> componentDto, String message, Object... messageArguments) {
- if (componentDto.isPresent() && componentDto.get().isEnabled()) {
+ if (componentDto.isPresent() && componentDto.get().isEnabled() && componentDto.get().getMainBranchProjectUuid() == null) {
return componentDto.get();
}
throw new NotFoundException(format(message, messageArguments));
}
- private static ComponentDto checkComponent(java.util.Optional<ComponentDto> componentDto, String message, Object... messageArguments) {
- if (componentDto.isPresent() && componentDto.get().isEnabled()) {
- return componentDto.get();
- }
- throw new NotFoundException(format(message, messageArguments));
- }
-
-
public ComponentDto getRootComponentByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey) {
ComponentDto project;
if (projectUuid != null) {
* Components of the main branch won't be found
*/
public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) {
- return checkComponent(dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch), "Component '%s' on branch '%s' not found", key, branch);
+ java.util.Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch);
+ if (componentDto.isPresent() && componentDto.get().isEnabled()) {
+ return componentDto.get();
+ }
+ throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch));
}
public enum ParamNames {
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.web.UserRole;
+import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
- List<ComponentDto> projects = getAuthorizedComponents(searchComponents(dbSession, request));
+ List<ComponentDto> projects = getAuthorizedComponents(searchComponents(dbSession, request))
+ .stream()
+ .filter(p -> p.getMainBranchProjectUuid() == null)
+ .collect(MoreCollectors.toList());
JsonWriter json = response.newJsonWriter();
json.beginArray();
for (ComponentDto project : projects) {
}
}
- private Optional<ComponentDto> getProjectByKeyOrId(DbSession dbSession, String component) {
- try {
- Long componentId = Long.parseLong(component);
- return ofNullable(dbClient.componentDao().selectById(dbSession, componentId).orNull());
- } catch (NumberFormatException e) {
- return ofNullable(dbClient.componentDao().selectByKey(dbSession, component).orNull());
- }
- }
-
private List<ComponentDto> searchComponents(DbSession dbSession, Request request) {
String projectKey = request.param(PARAM_PROJECT);
List<ComponentDto> projects = new ArrayList<>();
return projects;
}
+ private Optional<ComponentDto> getProjectByKeyOrId(DbSession dbSession, String component) {
+ try {
+ Long componentId = Long.parseLong(component);
+ return ofNullable(dbClient.componentDao().selectById(dbSession, componentId).orNull());
+ } catch (NumberFormatException e) {
+ return ofNullable(dbClient.componentDao().selectByKey(dbSession, component).orNull());
+ }
+ }
+
private List<ComponentDto> getAuthorizedComponents(List<ComponentDto> components) {
return userSession.keepAuthorizedComponents(UserRole.USER, components);
}
import org.sonar.db.component.ComponentDto;
import org.sonar.db.measure.MeasureDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;
import org.sonar.server.ws.WsUtils;
import org.sonarqube.ws.WsBranches;
import static org.sonar.db.component.BranchType.LONG;
import static org.sonar.db.component.BranchType.SHORT;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
-import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
import static org.sonarqube.ws.Common.BranchType;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.ACTION_LIST;
import static org.sonarqube.ws.client.projectbranches.ProjectBranchesParameters.PARAM_PROJECT;
private final DbClient dbClient;
private final UserSession userSession;
+ private final ComponentFinder componentFinder;
- public ListAction(DbClient dbClient, UserSession userSession) {
+ public ListAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) {
this.dbClient = dbClient;
this.userSession = userSession;
+ this.componentFinder = componentFinder;
}
@Override
String projectKey = request.mandatoryParam(PARAM_PROJECT);
try (DbSession dbSession = dbClient.openSession(false)) {
- ComponentDto project = checkFoundWithOptional(
- dbClient.componentDao().selectByKey(dbSession, projectKey),
- "Project key '%s' not found", projectKey);
-
+ ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
userSession.checkComponentPermission(UserRole.USER, project);
checkArgument(project.isEnabled() && PROJECT.equals(project.qualifier()), "Invalid project key");
underTest.delete(dbSession, project);
}
+ @Test
+ public void fail_to_delete_project_when_branch() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(IllegalArgumentException.class);
+
+ underTest.delete(dbSession, branch);
+ }
+
private DbData insertData(int id) {
String suffix = String.valueOf(id);
ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid-" + suffix)
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
-
public class ComponentFinderTest {
@Rule
underTest.getByUuidOrKey(dbSession, null, "project-key", ID_AND_KEY);
}
+ @Test
+ public void fail_to_getByUuidOrKey_when_using_branch_uuid() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ underTest.getByUuidOrKey(dbSession, branch.uuid(), null, ID_AND_KEY);
+ }
+
+ @Test
+ public void fail_to_getByUuidOrKey_when_using_branch_key() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ underTest.getByUuidOrKey(dbSession, null, branch.getDbKey(), ID_AND_KEY);
+ }
+
@Test
public void fail_when_component_uuid_is_removed() {
ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization()));
underTest.getByUuid(dbSession, "file-uuid");
}
+ @Test
+ public void fail_to_getByUuid_on_branch() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ underTest.getByUuid(dbSession, branch.uuid());
+ }
+
@Test
public void fail_when_component_key_is_removed() {
ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization()));
underTest.getByKey(dbSession, "file-key");
}
+ @Test
+ public void fail_getByKey_on_branch() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ underTest.getByKey(dbSession, branch.getDbKey());
+ }
+
@Test
public void get_component_by_uuid() {
db.components().insertComponent(newPrivateProjectDto(db.organizations().insert(), "project-uuid"));
assertThat(call(request).getComponentsList()).extracting(Component::getKey).containsOnly(file1.getDbKey(), file2.getDbKey());
}
+ @Test
+ public void does_not_return_branches() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ userSession.logIn().setRoot();
+
+ SearchWsResponse response = call(new SearchWsRequest().setQualifiers(asList(PROJECT, MODULE, FILE)));
+
+ assertThat(response.getComponentsList()).extracting(Component::getKey)
+ .containsOnly(project.getDbKey());
+ }
+
@Test
public void fail_if_unknown_qualifier_provided() {
expectedException.expect(IllegalArgumentException.class);
tuple(publicProject.getDbKey(), publicProject.isPrivate() ? "private" : "public"));
}
+ @Test
+ public void does_not_return_branches() {
+ ComponentDto project = db.components().insertMainBranch();
+ authorizationIndexerTester.allowOnlyAnyone(project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ projectMeasuresIndexer.indexOnStartup(null);
+
+ SearchProjectsWsResponse result = call(request);
+
+ assertThat(result.getComponentsList()).extracting(Component::getKey)
+ .containsExactlyInAnyOrder(project.getDbKey());
+ }
+
@Test
public void fail_when_filter_metrics_are_unknown() {
userSession.logIn();
@Test
public void branch() {
- ComponentDto project = db.components().insertPrivateProject();
+ ComponentDto project = db.components().insertMainBranch();
userSession.addProjectPermission(UserRole.USER, project);
String branchKey = "my_branch";
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchKey));
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() {
+ ComponentDto project = db.components().insertMainBranch();
+ userSession.addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam(PARAM_COMPONENT, branch.getDbKey())
+ .executeProtobuf(ShowWsResponse.class);
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() {
+ ComponentDto project = db.components().insertMainBranch();
+ userSession.addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam(PARAM_COMPONENT_ID, branch.uuid())
+ .executeProtobuf(ShowWsResponse.class);
+ }
+
private ShowWsResponse newRequest(@Nullable String uuid, @Nullable String key) {
TestRequest request = ws.newRequest();
if (uuid != null) {
tuple(SuggestionCategory.UNIT_TEST_FILE.getName(), true));
}
+ @Test
+ public void does_not_return_branches() {
+ ComponentDto project = db.components().insertMainBranch();
+ authorizationIndexerTester.allowOnlyAnyone(project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ componentIndexer.indexOnStartup(null);
+ authorizationIndexerTester.allowOnlyAnyone(project);
+
+ SuggestionsWsResponse response = ws.newRequest()
+ .setMethod("POST")
+ .setParam(PARAM_QUERY, project.name())
+ .executeProtobuf(SuggestionsWsResponse.class);
+
+ assertThat(response.getResultsList())
+ .filteredOn(c -> "TRK".equals(c.getQ()))
+ .extracting(Category::getItemsList)
+ .hasSize(1);
+ }
+
@Test
public void should_not_propose_to_show_more_results_if_0_projects_are_found() {
check_proposal_to_show_more_results(0, 0, 0L, null, true);
tuple(file.getKey(), branchKey));
}
+ @Test
+ public void fail_when_using_branch_db_key() {
+ ComponentDto project = db.components().insertMainBranch();
+ userSession.addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam(PARAM_COMPONENT, branch.getDbKey())
+ .executeProtobuf(WsComponents.ShowWsResponse.class);
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() {
+ ComponentDto project = db.components().insertMainBranch();
+ userSession.addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam(PARAM_COMPONENT_ID, branch.uuid())
+ .executeProtobuf(WsComponents.ShowWsResponse.class);
+ }
+
@Test
public void fail_when_not_enough_privileges() {
ComponentDto project = componentDb.insertComponent(newPrivateProjectDto(db.organizations().insert(), "project-uuid"));
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.SnapshotTesting.newAnalysis;
newBaseRequest().execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam("key", branch.getDbKey())
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam("uuid", branch.uuid())
+ .execute();
+ }
+
private TestRequest newBaseRequest() {
return ws.newRequest();
}
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.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.util.Protobuf.setNullable;
call(PROJECT_KEY);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ call(branch.getDbKey());
+ }
+
@Test
public void definition() {
WebService.Action definition = ws.getDef();
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.server.ws.WebService;
+import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.util.Protobuf.setNullable;
call(PROJECT_KEY);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ call(branch.getDbKey());
+ }
+
@Test
public void definition() {
WebService.Action definition = ws.getDef();
import org.sonar.db.issue.IssueDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.rule.RuleDefinitionDto;
-import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.server.es.EsTester;
import org.sonar.server.issue.ActionFinder;
import org.sonar.server.issue.IssueFieldsSetter;
import org.sonar.server.view.index.ViewIndexer;
import org.sonar.server.ws.WsActionTester;
import org.sonar.server.ws.WsResponseCommonFormat;
-import org.sonarqube.ws.Issues.Component;
import org.sonarqube.ws.Issues;
+import org.sonarqube.ws.Issues.Component;
import org.sonarqube.ws.Issues.Issue;
import org.sonarqube.ws.Issues.SearchWsResponse;
import org.sonarqube.ws.client.issue.IssuesWsParameters;
-import static com.google.common.collect.Lists.newArrayList;
-import static java.util.Collections.emptySet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.utils.DateUtils.addDays;
.doesNotContain(branchIssue.getKey());
}
+ @Test
+ public void does_not_return_branch_issues_when_using_db_key() {
+ RuleDefinitionDto rule = db.rules().insert();
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.addProjectPermission(UserRole.USER, project);
+ ComponentDto projectFile = db.components().insertComponent(newFileDto(project));
+ IssueDto projectIssue = db.issues().insertIssue(newIssue(rule, project, projectFile));
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ ComponentDto branchFile = db.components().insertComponent(newFileDto(branch));
+ IssueDto branchIssue = db.issues().insertIssue(newIssue(rule, branch, branchFile));
+ allowAnyoneOnProjects(project);
+ indexIssues();
+
+ SearchWsResponse result = ws.newRequest()
+ .setParam(PARAM_COMPONENT_KEYS, branch.getDbKey())
+ .executeProtobuf(SearchWsResponse.class);
+
+ assertThat(result.getIssuesList()).isEmpty();
+ }
+
private void allowAnyoneOnProjects(ComponentDto... projects) {
userSession.registerComponents(projects);
Arrays.stream(projects).forEach(p -> permissionIndexer.allowOnlyAnyone(p));
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.measure.MeasureDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
-import org.sonarqube.ws.WsMeasures;
-import org.sonarqube.ws.WsMeasures.Component;
-import org.sonarqube.ws.Common;
import org.sonarqube.ws.Common;
import org.sonarqube.ws.WsMeasures;
import org.sonarqube.ws.WsMeasures.Component;
import org.sonarqube.ws.WsMeasures.ComponentWsResponse;
import static java.lang.Double.parseDouble;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.utils.DateUtils.parseDateTime;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ insertNclocMetric();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam(PARAM_COMPONENT, branch.getDbKey())
+ .setParam(PARAM_METRIC_KEYS, "ncloc")
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ insertNclocMetric();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam(DEPRECATED_PARAM_COMPONENT_ID, branch.uuid())
+ .setParam(PARAM_METRIC_KEYS, "ncloc")
+ .execute();
+ }
+
@Test
public void json_example() {
ComponentDto project = db.components().insertPrivateProject();
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.measures.Metric;
-import org.sonar.api.resources.Qualifiers;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import org.sonar.db.measure.MeasureDto;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.metric.MetricTesting;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import static org.sonar.server.measure.ws.ComponentTreeAction.WITH_MEASURES_ONLY_METRIC_SORT_FILTER;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.ADDITIONAL_PERIODS;
+import static org.sonarqube.ws.client.measure.MeasuresWsParameters.DEPRECATED_PARAM_BASE_COMPONENT_ID;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.DEPRECATED_PARAM_BASE_COMPONENT_KEY;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_BRANCH;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ insertNclocMetric();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam(PARAM_COMPONENT, branch.getDbKey())
+ .setParam(PARAM_METRIC_KEYS, "ncloc")
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ insertNclocMetric();
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam(DEPRECATED_PARAM_BASE_COMPONENT_ID, branch.uuid())
+ .setParam(PARAM_METRIC_KEYS, "ncloc")
+ .execute();
+ }
+
private static MetricDto newMetricDto() {
return MetricTesting.newMetricDto()
.setWorstValue(null)
assertThat(result.getMeasuresCount()).isEqualTo(1);
}
+ @Test
+ public void does_not_return_branch_when_using_db_key() {
+ MetricDto coverage = insertCoverageMetric();
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ SnapshotDto analysis = db.components().insertSnapshot(branch);
+ db.measures().insertMeasure(branch, analysis, coverage, m -> m.setValue(10d));
+ setBrowsePermissionOnUser(project);
+
+ SearchWsResponse result = call(asList(branch.getDbKey()), singletonList(coverage.getKey()));
+
+ assertThat(result.getMeasuresList()).isEmpty();
+ }
+
@Test
public void fail_if_no_metric() {
ComponentDto project = db.components().insertPrivateProject();
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.measure.MeasureDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.Double.parseDouble;
+import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
.containsExactlyInAnyOrder(measure.getValue());
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam(PARAM_COMPONENT, branch.getDbKey())
+ .setParam(PARAM_METRICS, "ncloc")
+ .execute();
+ }
+
@Test
public void fail_if_unknown_metric() {
wsRequest.setMetrics(newArrayList(complexityMetric.getKey(), nclocMetric.getKey(), "METRIC_42", "42_METRIC"));
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.client.notification.AddRequest;
+import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.util.Protobuf.setNullable;
call(request);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ call(request.setProject(branch.getDbKey()));
+ }
+
private TestResponse call(AddRequest.Builder wsRequestBuilder) {
AddRequest wsRequest = wsRequestBuilder.build();
TestRequest request = ws.newRequest();
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.client.notification.RemoveRequest;
+import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.util.Protobuf.setNullable;
call(request);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ call(request.setProject(branch.getDbKey()));
+ }
+
private TestResponse call(RemoveRequest.Builder wsRequestBuilder) {
RemoveRequest wsRequest = wsRequestBuilder.build();
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.ServerException;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.sonar.api.web.UserRole.CODEVIEWER;
assertThat(db.users().selectAnyonePermissions(organization, project)).isEmpty();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto group = db.users().insertGroup(organization);
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .setParam(PARAM_GROUP_NAME, group.getName())
+ .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto group = db.users().insertGroup(organization);
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project id '%s' not found", branch.uuid()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .setParam(PARAM_GROUP_NAME, group.getName())
+ .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
+ .execute();
+ }
+
private void executeRequest(GroupDto groupDto, String permission) throws Exception {
newRequest()
.setParam(PARAM_GROUP_NAME, groupDto.getName())
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.ServerException;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
assertThat(db.users().selectAnyonePermissions(organization, project)).isEmpty();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ addUserAsMemberOfOrganization(organization);
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .setParam(PARAM_USER_LOGIN, user.getLogin())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ addUserAsMemberOfOrganization(organization);
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project id '%s' not found", branch.uuid()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .setParam(PARAM_USER_LOGIN, user.getLogin())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+ }
+
private void addUserAsMemberOfOrganization(OrganizationDto organization) {
db.organizations().addMember(organization, user);
}
import org.sonar.db.user.GroupDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
.execute();
}
+ @Test
+ public void fail_when_using_branch_uuid() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ GroupDto group = db.users().insertGroup(db.getDefaultOrganization());
+ db.users().insertProjectPermissionOnGroup(group, ISSUE_ADMIN, project);
+ loginAsAdmin(db.getDefaultOrganization());
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project id '%s' not found", branch.uuid()));
+
+ newRequest()
+ .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_db_key() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ GroupDto group = db.users().insertGroup(db.getDefaultOrganization());
+ db.users().insertProjectPermissionOnGroup(group, ISSUE_ADMIN, project);
+ loginAsAdmin(db.getDefaultOrganization());
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ newRequest()
+ .setParam(PARAM_PERMISSION, ISSUE_ADMIN)
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .execute();
+ }
+
}
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.api.web.UserRole.CODEVIEWER;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto group = db.users().insertGroup(organization);
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .setParam(PARAM_GROUP_NAME, group.getName())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto group = db.users().insertGroup(organization);
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project id '%s' not found", branch.uuid()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .setParam(PARAM_GROUP_NAME, group.getName())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+ }
+
private void unsafeInsertProjectPermissionOnAnyone(String perm, ComponentDto project) {
GroupPermissionDto dto = new GroupPermissionDto()
.setOrganizationUuid(project.getOrganizationUuid())
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.ServerException;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .setParam(PARAM_USER_LOGIN, user.getLogin())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project id '%s' not found", branch.uuid()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .setParam(PARAM_USER_LOGIN, user.getLogin())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+ }
+
}
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.i18n.I18nRule;
import org.sonarqube.ws.WsPermissions;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
public class SearchProjectPermissionsActionTest extends BasePermissionWsTest<SearchProjectPermissionsAction> {
.isSimilarTo(getClass().getResource("SearchProjectPermissionsActionTest/display_all_project_permissions.json"));
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ newRequest()
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .execute();
+ }
+
private ComponentDto insertView() {
return db.components().insertComponent(newView(db.getDefaultOrganization())
.setUuid("752d8bfd-420c-4a83-a4e5-8ab19b13c8fc")
import org.sonar.api.web.UserRole;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
+import static java.lang.String.format;
import static org.apache.commons.lang.StringUtils.countMatches;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.db.permission.OrganizationPermission.SCAN;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.test.JsonAssert.assertJson;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
public class UsersActionTest extends BasePermissionWsTest<UsersAction> {
newRequest().setParam(TEXT_QUERY, "ab").execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ UserDto user = db.users().insertUser(newUserDto());
+ ComponentDto project = db.components().insertMainBranch(organization);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ db.users().insertProjectPermissionOnUser(user, ISSUE_ADMIN, project);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .setParam(PARAM_USER_LOGIN, user.getLogin())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ UserDto user = db.users().insertUser(newUserDto());
+ ComponentDto project = db.components().insertMainBranch(organization);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ db.users().insertProjectPermissionOnUser(user, ISSUE_ADMIN, project);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project id '%s' not found", branch.uuid()));
+
+ newRequest()
+ .setParam(PARAM_ORGANIZATION, organization.getKey())
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .setParam(PARAM_USER_LOGIN, user.getLogin())
+ .setParam(PARAM_PERMISSION, SYSTEM_ADMIN)
+ .execute();
+ }
+
private void insertUsersHavingGlobalPermissions() {
UserDto user1 = db.users().insertUser(newUserDto("login-1", "name-1", "email-1"));
db.organizations().addMember(db.getDefaultOrganization(), user1);
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
callDryRunByUuid(project.uuid(), FROM, TO);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component key '%s' not found", branch.getDbKey()));
+
+ callByKey(branch.getDbKey(), FROM, TO);
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .setParam(PARAM_FROM, "my_")
+ .setParam(PARAM_TO, "my_new_")
+ .execute();
+ }
+
@Test
public void api_definition() {
WebService.Action definition = ws.getDef();
import org.sonar.server.component.ComponentCleanerService;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
call(newRequest().setParam(PARAM_PROJECT_ID, project.uuid()));
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ userSessionRule.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component key '%s' not found", branch.getDbKey()));
+
+ call(newRequest().setParam(PARAM_PROJECT, branch.getDbKey()));
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ userSessionRule.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component id '%s' not found", branch.uuid()));
+
+ call(newRequest().setParam(PARAM_PROJECT_ID, branch.uuid()));
+ }
+
private WsTester.TestRequest newRequest() {
return ws.newPostRequest(CONTROLLER, ACTION);
}
.contains("ghost-key-1");
}
+ @Test
+ public void does_not_return_branches() {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto ghostProject = db.components().insertMainBranch(organization);
+ db.components().insertSnapshot(ghostProject, dto -> dto.setStatus("U"));
+ ComponentDto ghostBranchProject = db.components().insertProjectBranch(ghostProject);
+ userSessionRule.logIn().addPermission(ADMINISTER, organization);
+
+ TestResponse result = underTest.newRequest()
+ .setParam("organization", organization.getKey())
+ .execute();
+
+ assertJson(result.getInput()).isSimilarTo("{" +
+ " \"projects\": [" +
+ " {" +
+ " \"uuid\": \"" + ghostProject.uuid() + "\"," +
+ " \"key\": \"" + ghostProject.getDbKey() + "\"," +
+ " \"name\": \"" + ghostProject.name() + "\"," +
+ " \"visibility\": \"private\"" +
+ " }" +
+ " ]" +
+ "}");
+ }
+
@Test
public void ghost_projects_base_on_json_example() throws Exception {
OrganizationDto organization = db.organizations().insert();
"]");
}
+ @Test
+ public void does_not_return_branches_when_searching_all_components() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ userSession.setRoot();
+
+ String result = call(null, null, null);
+
+ assertJson(result).isSimilarTo("[" +
+ " {" +
+ " \"id\":" + project.getId() + "," +
+ " }" +
+ "]");
+ }
+
+ @Test
+ public void does_not_return_branches_when_searching_by_key() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ userSession.setRoot();
+
+ String result = call(branch.getDbKey(), null, null);
+
+ assertJson(result).isSimilarTo("[]");
+ }
+
@Test
public void test_example() {
insertProjectsAuthorizedForUser(
.doesNotContain(recentProject.getKey());
}
+ @Test
+ public void does_not_return_branches_when_searching_by_key() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ userSession.addPermission(ADMINISTER, db.getDefaultOrganization());
+
+ SearchWsResponse response = call(SearchWsRequest.builder().build());
+
+ assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly(project.getDbKey());
+ }
+
@Test
public void result_is_paginated() throws IOException {
userSession.addPermission(ADMINISTER, db.getDefaultOrganization());
}
@Test
- public void do_not_return_views() {
+ public void does_not_return_views() {
OrganizationDto organizationDto = db.organizations().insert();
ComponentDto jdk7 = insertJdk7(organizationDto);
ComponentDto view = insertView(organizationDto);
assertThat(result.getProjects(0).getId()).isEqualTo(jdk7.uuid());
}
+ @Test
+ public void does_not_return_branches() {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, project);
+
+ SearchMyProjectsWsResponse result = call_ws();
+
+ assertThat(result.getProjectsList())
+ .extracting(Project::getKey)
+ .containsExactlyInAnyOrder(project.getDbKey());
+ }
+
@Test
public void admin_via_groups() {
OrganizationDto org = db.organizations().insert();
callByUuid("UNKNOWN_UUID", ANOTHER_KEY);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component key '%s' not found", branch.getDbKey()));
+
+ callByKey(branch.getDbKey(), ANOTHER_KEY);
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component id '%s' not found", branch.uuid()));
+
+ callByUuid(branch.uuid(), ANOTHER_KEY);
+ }
+
@Test
public void api_definition() {
WebService.Action definition = ws.getDef();
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = dbTester.components().insertMainBranch();
+ userSessionRule.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = dbTester.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component key '%s' not found", branch.getDbKey()));
+
+ request.setParam(PARAM_PROJECT, branch.getDbKey())
+ .setParam(PARAM_VISIBILITY, PUBLIC)
+ .execute();
+ }
+
private void unsafeGiveAllPermissionsToRootComponent(ComponentDto component, UserDto user, GroupDto group, OrganizationDto organization) {
Arrays.stream(OrganizationPermission.values())
.forEach(organizationPermission -> {
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.WsBranches.Branch;
import org.sonarqube.ws.WsBranches.ListWsResponse;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.WsBranches.Branch.Status;
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
+ private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(PROJECT);
+
private MetricDto qualityGateStatus;
private MetricDto bugs;
private MetricDto vulnerabilities;
private MetricDto codeSmells;
- public WsActionTester tester = new WsActionTester(new ListAction(db.getDbClient(), userSession));
+ public WsActionTester tester = new WsActionTester(new ListAction(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), resourceTypes)));
@Before
public void setUp() throws Exception {
@Test
public void fail_if_project_does_not_exist() {
expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Project key 'foo' not found");
+ expectedException.expectMessage("Component key 'foo' not found");
tester.newRequest()
.setParam("project", "foo")
tuple(true, 1, true, 2, true, 3));
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ tester.newRequest()
+ .setParam("project", branch.getDbKey())
+ .execute();
+ }
+
@Test
public void test_example() {
ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("sonarqube"));
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(String.format("Component '%s' on branch '%s' not found", branch.getDbKey(), branch.getBranch()));
+
+ ws.newRequest()
+ .setParam("component", branch.getDbKey())
+ .setParam("branch", branch.getBranch())
+ .execute();
+ }
+
@Test
public void test_example() {
ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("sonarqube"));
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.WsProjectLinks;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
failIfNotAProject(view, subview);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .setParam(PARAM_NAME, "Custom")
+ .setParam(PARAM_URL, "http://example.org")
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_db_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .setParam(PARAM_NAME, "Custom")
+ .setParam(PARAM_URL, "http://example.org")
+ .execute();
+ }
+
private void failIfNotAProject(ComponentDto root, ComponentDto component) {
userSession.logIn().addProjectPermission(UserRole.ADMIN, root);
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentLinkDto;
import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonarqube.ws.WsProjectLinks.Link;
import org.sonarqube.ws.WsProjectLinks.SearchWsResponse;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_db_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Project id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam(PARAM_PROJECT_ID, branch.uuid())
+ .execute();
+ }
+
private ComponentDto insertProject(String projectKey, String projectUuid) {
return componentDb.insertComponent(newPrivateProjectDto(db.organizations().insert(), projectUuid).setDbKey(projectKey));
}
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.server.component.TestComponentFinder;
import org.sonar.server.es.TestProjectIndexers;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.core.util.Protobuf.setNullable;
call(file.getDbKey(), "secret");
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ call(branch.getDbKey(), "secret");
+ }
+
@Test
public void definition() {
WebService.Action definition = ws.getDef();
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.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
callByKey(gateId, project.getDbKey());
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ String gateId = String.valueOf(gate.getId());
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ callByKey(gateId, branch.getDbKey());
+ }
+
+ @Test
+ public void fail_when_using_branch_id() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ String gateId = String.valueOf(gate.getId());
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ callByUuid(gateId, branch.uuid());
+ }
+
private QualityGateDto insertQualityGate() {
QualityGateDto gate = new QualityGateDto().setName("Custom");
dbClient.qualityGateDao().insert(dbSession, gate);
import org.sonarqube.ws.WsQualityGates;
import org.sonarqube.ws.WsQualityGates.GetByProjectWsResponse;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_PROJECT_ID;
call("uuid", "key");
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ call(null, branch.getDbKey());
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ call(branch.uuid(), null);
+ }
+
private GetByProjectWsResponse callByUuid(String projectUuid) {
return call(projectUuid, null);
}
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse;
import org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse.Status;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.component.SnapshotTesting.newAnalysis;
import static org.sonar.db.measure.MeasureTesting.newMeasureDto;
ws.newRequest().execute().getInput();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ SnapshotDto snapshot = db.components().insertSnapshot(branch);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam(PARAM_PROJECT_KEY, branch.getDbKey())
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ SnapshotDto snapshot = db.components().insertSnapshot(branch);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam("projectId", branch.uuid())
+ .execute();
+ }
+
private ProjectStatusWsResponse call(String taskId) {
return ws.newRequest()
.setParam("analysisId", taskId)
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.qualitygate.QualityGateDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
callByKey(gateId, project.getDbKey());
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ String gateId = String.valueOf(gate.getId());
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ callByKey(gateId, branch.getDbKey());
+ }
+
+ @Test
+ public void fail_when_using_branch_id() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ String gateId = String.valueOf(gate.getId());
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ callByUuid(gateId, branch.uuid());
+ }
+
private QualityGateDto insertQualityGate() {
QualityGateDto gate = new QualityGateDto().setName("Custom");
dbClient.qualityGateDao().insert(dbSession, gate);
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
-import org.sonar.server.component.TestComponentFinder;
import org.sonar.db.qualityprofile.QProfileDto;
+import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ QProfileDto profile = db.qualityProfiles().insert(organization);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ tester.newRequest()
+ .setParam("project", branch.getDbKey())
+ .setParam("profileKey", profile.getKee())
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ QProfileDto profile = db.qualityProfiles().insert(organization);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ tester.newRequest()
+ .setParam("projectUuid", branch.uuid())
+ .setParam("profileKey", profile.getKee())
+ .execute();
+ }
+
private void assertProjectIsAssociatedToProfile(ComponentDto project, QProfileDto profile) {
QProfileDto loaded = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(db.getSession(), project, profile.getLanguage());
assertThat(loaded.getKee()).isEqualTo(profile.getKee());
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ResourceTypesRule;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ QProfileDto profile = db.qualityProfiles().insert(organization);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam("project", branch.getDbKey())
+ .setParam("profileKey", profile.getKee())
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ QProfileDto profile = db.qualityProfiles().insert(organization);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ ws.newRequest()
+ .setParam("projectUuid", branch.uuid())
+ .setParam("profileKey", profile.getKee())
+ .execute();
+ }
+
private void assertProjectIsAssociatedToProfile(ComponentDto project, QProfileDto profile) {
QProfileDto loaded = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(db.getSession(), project, profile.getLanguage());
assertThat(loaded.getKee()).isEqualTo(profile.getKee());
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.property.PropertyDbTester;
import org.sonar.db.property.PropertyQuery;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.i18n.I18nRule;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.MediaTypes;
+import static java.lang.String.format;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.resources.Qualifiers.PROJECT;
failForPropertyWithoutDefinitionOnUnsupportedComponent(view, projectCopy);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam("keys", "foo")
+ .setParam("component", branch.getDbKey())
+ .execute();
+ }
+
private void succeedForPropertyWithoutDefinitionAndValidComponent(ComponentDto root, ComponentDto module) {
logInAsProjectAdmin(root);
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.property.PropertyDbTester;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.property.PropertyQuery;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.i18n.I18nRule;
import org.sonar.server.platform.SettingsChangeNotifier;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import static com.google.common.collect.Lists.newArrayList;
+import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
GSON.toJson(ImmutableMap.of("firstField", "firstValue"))), project.getDbKey());
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ callForProjectSettingByKey("my.key", "My Value", branch.getDbKey());
+ }
+
@Test
public void definition() {
WebService.Action definition = ws.getDef();
import org.sonar.api.config.PropertyFieldDefinition;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.property.PropertyDbTester;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonarqube.ws.Settings;
import org.sonarqube.ws.Settings.ValuesWsResponse;
+import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
JsonAssert.assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto project = db.components().insertMainBranch(organization);
+ userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
+ ComponentDto branch = db.components().insertProjectBranch(project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ ws.newRequest()
+ .setParam("keys", "foo")
+ .setParam("component", branch.getDbKey())
+ .execute();
+ }
+
@Test
public void test_ws_definition() {
WebService.Action action = ws.getDef();
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
+import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
final static String COMPONENT_KEY = "Action.java";
final static String PROJECT_UUID = "ABCD";
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
@Rule
}
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ loginAndRegisterComponent(project.uuid());
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ tester.newGetRequest("api/sources", "hash")
+ .setParam("key", branch.getDbKey())
+ .execute();
+ }
+
@Test(expected = ForbiddenException.class)
public void fail_on_missing_permission() throws Exception {
db.prepareDbUnit(getClass(), "shared.xml");
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
+import static java.lang.String.format;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.db.component.ComponentTesting.newFileDto;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ userSession.addProjectPermission(USER, project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ tester.newRequest()
+ .setParam("resource", branch.getDbKey())
+ .execute();
+ }
+
private static DbFileSources.Data newData(String... lines) throws IOException {
DbFileSources.Data.Builder dataBuilder = DbFileSources.Data.newBuilder();
for (int i = 1; i <= lines.length; i++) {
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
+import static java.lang.String.format;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ userSession.addProjectPermission(UserRole.USER, project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ wsTester.newGetRequest("api/sources", "lines")
+ .setParam("key", branch.getDbKey())
+ .execute();
+ }
+
+ @Test
+ public void fail_when_using_branch_uuid() throws Exception {
+ ComponentDto project = db.components().insertMainBranch();
+ ComponentDto branch = db.components().insertProjectBranch(project);
+ userSession.addProjectPermission(UserRole.USER, project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
+
+ wsTester.newGetRequest("api/sources", "lines")
+ .setParam("uuid", branch.uuid())
+ .execute();
+ }
+
private void insertFileWithData(DbFileSources.Data fileData) throws IOException {
insertFile();
db.getDbClient().fileSourceDao().insert(db.getSession(), new FileSourceDto()
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import org.sonar.db.source.FileSourceDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.source.HtmlSourceDecorator;
import org.sonar.server.source.SourceService;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsTester;
+import static java.lang.String.format;
+
public class ScmActionTest {
private static final String FILE_KEY = "FILE_KEY";
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
-
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
@Rule
public UserSessionRule userSessionRule = UserSessionRule.standalone();
request.execute();
}
+ @Test
+ public void fail_when_using_branch_db_key() throws Exception {
+ ComponentDto project = dbTester.components().insertMainBranch();
+ ComponentDto branch = dbTester.components().insertProjectBranch(project);
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
+
+ expectedException.expect(NotFoundException.class);
+ expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
+
+ tester.newGetRequest("api/sources", "scm")
+ .setParam("key", branch.getDbKey())
+ .execute();
+ }
+
private DbFileSources.Line newSourceLine(String author, String revision, Date date, int line) {
return DbFileSources.Line.newBuilder()
.setScmAuthor(author)