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.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.server.permission.index.PermissionIndexerTester;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
-import org.sonarqube.ws.Common;
+import org.sonarqube.ws.Common.BranchType;
import org.sonarqube.ws.MediaTypes;
import org.sonarqube.ws.ProjectBranches;
import org.sonarqube.ws.ProjectBranches.Branch;
import static org.sonar.api.rules.RuleType.VULNERABILITY;
import static org.sonar.api.utils.DateUtils.dateToLong;
import static org.sonar.api.utils.DateUtils.parseDateTime;
+import static org.sonar.api.web.UserRole.USER;
import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
import static org.sonar.db.component.BranchType.LONG;
import static org.sonar.db.component.BranchType.SHORT;
@Test
public void test_example() {
ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("sonarqube"));
+
ComponentDto longLivingBranch = db.components()
.insertProjectBranch(project, b -> b.setKey("feature/bar").setBranchType(LONG));
- ComponentDto shortLivingBranch = db.components()
- .insertProjectBranch(project, b -> b.setKey("feature/foo").setBranchType(SHORT).setMergeBranchUuid(longLivingBranch.uuid()));
- userSession.logIn().addProjectPermission(UserRole.USER, project);
-
db.getDbClient().snapshotDao().insert(db.getSession(),
newAnalysis(longLivingBranch).setLast(true).setCreatedAt(parseDateTime("2017-04-01T01:15:42+0100").getTime()));
+ db.measures().insertLiveMeasure(longLivingBranch, qualityGateStatus, m -> m.setData("OK"));
+
+ ComponentDto shortLivingBranch = db.components()
+ .insertProjectBranch(project, b -> b.setKey("feature/foo").setBranchType(SHORT).setMergeBranchUuid(longLivingBranch.uuid()));
db.getDbClient().snapshotDao().insert(db.getSession(),
newAnalysis(shortLivingBranch).setLast(true).setCreatedAt(parseDateTime("2017-04-03T13:37:00+0100").getTime()));
- db.commit();
+ db.measures().insertLiveMeasure(shortLivingBranch, qualityGateStatus, m -> m.setData("OK"));
+
+ RuleDefinitionDto rule = db.rules().insert();
+ db.issues().insert(rule, shortLivingBranch, shortLivingBranch, i -> i.setType(BUG).setResolution(null));
+
+ issueIndexer.indexOnStartup(emptySet());
+
+ userSession.logIn().addProjectPermission(USER, project);
String json = ws.newRequest()
.setParam("project", project.getDbKey())
.getInput();
assertJson(json).isSimilarTo(ws.getDef().responseExampleAsString());
+ assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(json);
}
@Test
public void test_with_SCAN_EXCUTION_permission() {
ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("sonarqube"));
- ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setKey("feature/bar").setBranchType(LONG));
- ComponentDto shortLivingBranch = db.components().insertProjectBranch(project,
- b -> b.setKey("feature/foo").setBranchType(SHORT).setMergeBranchUuid(longLivingBranch.uuid()));
- userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
+ ComponentDto longLivingBranch = db.components()
+ .insertProjectBranch(project, b -> b.setKey("feature/bar").setBranchType(LONG));
db.getDbClient().snapshotDao().insert(db.getSession(),
newAnalysis(longLivingBranch).setLast(true).setCreatedAt(parseDateTime("2017-04-01T01:15:42+0100").getTime()));
+ db.measures().insertLiveMeasure(longLivingBranch, qualityGateStatus, m -> m.setData("OK"));
+
+ ComponentDto shortLivingBranch = db.components()
+ .insertProjectBranch(project, b -> b.setKey("feature/foo").setBranchType(SHORT).setMergeBranchUuid(longLivingBranch.uuid()));
db.getDbClient().snapshotDao().insert(db.getSession(),
newAnalysis(shortLivingBranch).setLast(true).setCreatedAt(parseDateTime("2017-04-03T13:37:00+0100").getTime()));
- db.commit();
+ db.measures().insertLiveMeasure(shortLivingBranch, qualityGateStatus, m -> m.setData("OK"));
+
+ RuleDefinitionDto rule = db.rules().insert();
+ db.issues().insert(rule, shortLivingBranch, shortLivingBranch, i -> i.setType(BUG).setResolution(null));
+ issueIndexer.indexOnStartup(emptySet());
+
+ userSession.logIn().addProjectPermission(SCAN_EXECUTION, project);
String json = ws.newRequest()
.setParam("project", project.getDbKey())
@Test
public void main_branch() {
ComponentDto project = db.components().insertMainBranch();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ListWsResponse response = ws.newRequest()
.setParam("project", project.getDbKey())
assertThat(response.getBranchesList())
.extracting(Branch::getName, Branch::getIsMain, Branch::getType)
- .containsExactlyInAnyOrder(tuple("master", true, Common.BranchType.LONG));
+ .containsExactlyInAnyOrder(tuple("master", true, BranchType.LONG));
}
@Test
public void main_branch_with_specified_name() {
OrganizationDto organization = db.organizations().insert();
ComponentDto project = db.components().insertMainBranch(organization, "head");
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ListWsResponse response = ws.newRequest()
.setParam("project", project.getDbKey())
assertThat(response.getBranchesList())
.extracting(Branch::getName, Branch::getIsMain, Branch::getType)
- .containsExactlyInAnyOrder(tuple("head", true, Common.BranchType.LONG));
+ .containsExactlyInAnyOrder(tuple("head", true, BranchType.LONG));
}
@Test
public void test_project_with_zero_branches() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
String json = ws.newRequest()
.setParam("project", project.getDbKey())
ComponentDto project = db.components().insertMainBranch();
db.components().insertProjectBranch(project, b -> b.setKey("feature/bar"));
db.components().insertProjectBranch(project, b -> b.setKey("feature/foo"));
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ListWsResponse response = ws.newRequest()
.setParam("project", project.getDbKey())
assertThat(response.getBranchesList())
.extracting(Branch::getName, Branch::getType)
.containsExactlyInAnyOrder(
- tuple("master", Common.BranchType.LONG),
- tuple("feature/foo", Common.BranchType.LONG),
- tuple("feature/bar", Common.BranchType.LONG));
+ tuple("master", BranchType.LONG),
+ tuple("feature/foo", BranchType.LONG),
+ tuple("feature/bar", BranchType.LONG));
}
@Test
public void short_living_branches() {
ComponentDto project = db.components().insertMainBranch();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ComponentDto longLivingBranch = db.components().insertProjectBranch(project,
b -> b.setKey("long").setBranchType(LONG));
ComponentDto shortLivingBranch = db.components().insertProjectBranch(project,
assertThat(response.getBranchesList())
.extracting(Branch::getName, Branch::getType, Branch::getMergeBranch)
.containsExactlyInAnyOrder(
- tuple("master", Common.BranchType.LONG, ""),
- tuple(longLivingBranch.getBranch(), Common.BranchType.LONG, ""),
- tuple(shortLivingBranch.getBranch(), Common.BranchType.SHORT, longLivingBranch.getBranch()),
- tuple(shortLivingBranchOnMaster.getBranch(), Common.BranchType.SHORT, "master"));
+ tuple("master", BranchType.LONG, ""),
+ tuple(longLivingBranch.getBranch(), BranchType.LONG, ""),
+ tuple(shortLivingBranch.getBranch(), BranchType.SHORT, longLivingBranch.getBranch()),
+ tuple(shortLivingBranchOnMaster.getBranch(), BranchType.SHORT, "master"));
}
@Test
public void mergeBranch_is_using_default_main_name_when_main_branch_has_no_name() {
ComponentDto project = db.components().insertMainBranch();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ComponentDto shortLivingBranch = db.components().insertProjectBranch(project,
b -> b.setKey("short").setBranchType(SHORT).setMergeBranchUuid(project.uuid()));
assertThat(response.getBranch())
.extracting(Branch::getName, Branch::getType, Branch::getMergeBranch)
- .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), Common.BranchType.SHORT, "master");
+ .containsExactlyInAnyOrder(shortLivingBranch.getBranch(), BranchType.SHORT, "master");
}
@Test
public void short_living_branch_on_removed_branch() {
ComponentDto project = db.components().insertMainBranch();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ComponentDto shortLivingBranch = db.components().insertProjectBranch(project,
b -> b.setKey("short").setBranchType(SHORT).setMergeBranchUuid("unknown"));
assertThat(response.getBranchesList())
.extracting(Branch::getName, Branch::getType, Branch::hasMergeBranch, Branch::getIsOrphan)
.containsExactlyInAnyOrder(
- tuple("master", Common.BranchType.LONG, false, false),
- tuple(shortLivingBranch.getBranch(), Common.BranchType.SHORT, false, true));
+ tuple("master", BranchType.LONG, false, false),
+ tuple(shortLivingBranch.getBranch(), BranchType.SHORT, false, true));
}
@Test
public void status_on_long_living_branch() {
ComponentDto project = db.components().insertMainBranch();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG));
db.measures().insertLiveMeasure(branch, qualityGateStatus, m -> m.setData("OK"));
@Test
public void status_on_short_living_branches() {
ComponentDto project = db.components().insertMainBranch();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG));
ComponentDto shortLivingBranch = db.components().insertProjectBranch(project,
b -> b.setKey("short").setBranchType(SHORT).setMergeBranchUuid(longLivingBranch.uuid()));
@Test
public void status_on_short_living_branch_with_no_issue() {
ComponentDto project = db.components().insertMainBranch();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG));
db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT).setMergeBranchUuid(longLivingBranch.uuid()));
issueIndexer.indexOnStartup(emptySet());
.setParam("project", project.getKey())
.executeProtobuf(ListWsResponse.class);
- assertThat(response.getBranchesList().stream().filter(b -> b.getType().equals(Common.BranchType.SHORT)).map(ProjectBranches.Branch::getStatus))
+ assertThat(response.getBranchesList().stream().filter(b -> b.getType().equals(BranchType.SHORT)).map(ProjectBranches.Branch::getStatus))
.extracting(Status::getBugs, Status::getVulnerabilities, Status::getCodeSmells)
.containsExactlyInAnyOrder(tuple(0L, 0L, 0L));
}
Long lastAnalysisShortLivingBranch = dateToLong(parseDateTime("2017-04-03T00:00:00+0100"));
ComponentDto project = db.components().insertMainBranch();
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
ComponentDto shortLivingBranch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT).setMergeBranchUuid(project.uuid()));
ComponentDto longLivingBranch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG));
ComponentDto shortLivingBranch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT).setMergeBranchUuid(longLivingBranch2.uuid()));
.extracting(ProjectBranches.Branch::getType, ProjectBranches.Branch::hasAnalysisDate,
b -> "".equals(b.getAnalysisDate()) ? null : dateToLong(parseDateTime(b.getAnalysisDate())))
.containsExactlyInAnyOrder(
- tuple(Common.BranchType.LONG, false, null),
- tuple(Common.BranchType.SHORT, false, null),
- tuple(Common.BranchType.LONG, true, lastAnalysisLongLivingBranch),
- tuple(Common.BranchType.SHORT, true, lastAnalysisShortLivingBranch));
+ tuple(BranchType.LONG, false, null),
+ tuple(BranchType.SHORT, false, null),
+ tuple(BranchType.LONG, true, lastAnalysisLongLivingBranch),
+ tuple(BranchType.SHORT, true, lastAnalysisShortLivingBranch));
}
@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);
+ userSession.logIn().addProjectPermission(USER, project);
ComponentDto branch = db.components().insertProjectBranch(project);
expectedException.expect(NotFoundException.class);
public void fail_if_not_a_reference_on_project() {
ComponentDto project = db.components().insertPrivateProject();
ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
- userSession.logIn().addProjectPermission(UserRole.USER, project);
+ userSession.logIn().addProjectPermission(USER, project);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Invalid project key");