@Test public void isMain_is_true_if_branch_uuid_equals_project_uuid() { underTest.setProjectUuid("U1"); underTest.setUuid("U1"); assertThat(underTest.isMain()).isTrue(); }
@Test public void isMain_is_false_if_branch_uuid_does_not_equal_project_uuid() { underTest.setProjectUuid("U1"); underTest.setUuid("U2"); assertThat(underTest.isMain()).isFalse(); }
@Test public void insert_branch_with_all_fields_and_max_length_values() { BranchDto dto = new BranchDto(); dto.setProjectUuid(repeat("a", 50)); dto.setUuid(repeat("b", 50)); dto.setBranchType(BranchType.SHORT); dto.setKey(repeat("c", 255)); dto.setMergeBranchUuid(repeat("d", 50)); underTest.insert(dbSession, dto); Map<String, Object> map = db.selectFirst(dbSession, SELECT_FROM + " where uuid='" + dto.getUuid() + "'"); assertThat((String) map.get("projectUuid")).contains("a").isEqualTo(dto.getProjectUuid()); assertThat((String) map.get("uuid")).contains("b").isEqualTo(dto.getUuid()); assertThat((String) map.get("kee")).contains("c").isEqualTo(dto.getKey()); assertThat((String) map.get("mergeBranchUuid")).contains("d").isEqualTo(dto.getMergeBranchUuid()); }
private BranchDto createMainBranch(DbSession session, String componentUuid) { BranchDto branch = new BranchDto() .setBranchType(BranchType.LONG) .setUuid(componentUuid) .setKey(BranchDto.DEFAULT_MAIN_BRANCH_NAME) .setMergeBranchUuid(null) .setProjectUuid(componentUuid); dbClient.branchDao().upsert(session, branch); return branch; }
@Test public void upsert_branch() { BranchDto dto = new BranchDto(); dto.setProjectUuid("U1"); dto.setUuid("U2"); dto.setBranchType(BranchType.LONG); dto.setKey("foo"); underTest.insert(dbSession, dto); // the fields that can be updated dto.setMergeBranchUuid("U3"); // the fields that can't be updated. New values are ignored. dto.setProjectUuid("ignored"); dto.setBranchType(BranchType.SHORT); underTest.upsert(dbSession, dto); BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "foo").get(); assertThat(loaded.getMergeBranchUuid()).isEqualTo("U3"); assertThat(loaded.getProjectUuid()).isEqualTo("U1"); assertThat(loaded.getBranchType()).isEqualTo(BranchType.LONG); }
public static BranchDto newBranchDto(@Nullable String projectUuid, BranchType branchType) { String key = projectUuid == null ? null : "branch_" + randomAlphanumeric(248); return new BranchDto() .setKey(key) .setUuid(Uuids.createFast()) // MainBranchProjectUuid will be null if it's a main branch .setProjectUuid(projectUuid) .setBranchType(branchType); }
@Test public void insert_pull_request_branch_with_only_non_null_fields() { String projectUuid = "U1"; String uuid = "U2"; BranchType branchType = BranchType.PULL_REQUEST; String kee = "123"; BranchDto dto = new BranchDto(); dto.setProjectUuid(projectUuid); dto.setUuid(uuid); dto.setBranchType(branchType); dto.setKey(kee); underTest.insert(dbSession, dto); BranchDto loaded = underTest.selectByUuid(dbSession, dto.getUuid()).get(); assertThat(loaded.getProjectUuid()).isEqualTo(projectUuid); assertThat(loaded.getUuid()).isEqualTo(uuid); assertThat(loaded.getBranchType()).isEqualTo(branchType); assertThat(loaded.getKey()).isEqualTo(kee); assertThat(loaded.getMergeBranchUuid()).isNull(); assertThat(loaded.getPullRequestData()).isNull(); }
dto.setProjectUuid(projectUuid); dto.setUuid(uuid); dto.setBranchType(branchType);
@Test public void update_main_branch_name() { BranchDto dto = new BranchDto(); dto.setProjectUuid("U1"); dto.setUuid("U1"); dto.setBranchType(BranchType.LONG); dto.setKey("feature"); underTest.insert(dbSession, dto); BranchDto dto2 = new BranchDto(); dto2.setProjectUuid("U2"); dto2.setUuid("U2"); dto2.setBranchType(BranchType.LONG); dto2.setKey("branch"); underTest.insert(dbSession, dto2); underTest.updateMainBranchName(dbSession, "U1", "master"); BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "master").get(); assertThat(loaded.getMergeBranchUuid()).isNull(); assertThat(loaded.getProjectUuid()).isEqualTo("U1"); assertThat(loaded.getBranchType()).isEqualTo(BranchType.LONG); }
@Test public void upsert_pull_request() { BranchDto dto = new BranchDto(); dto.setProjectUuid("U1"); dto.setUuid("U2"); dto.setBranchType(BranchType.PULL_REQUEST); dto.setProjectUuid("ignored"); dto.setBranchType(BranchType.SHORT); underTest.upsert(dbSession, dto);
@Test public void selectByPullRequestKey() { BranchDto mainBranch = new BranchDto(); mainBranch.setProjectUuid("U1"); mainBranch.setUuid("U1"); mainBranch.setBranchType(BranchType.LONG); mainBranch.setKey("master"); underTest.insert(dbSession, mainBranch); String pullRequestId = "123"; BranchDto pullRequest = new BranchDto(); pullRequest.setProjectUuid("U1"); pullRequest.setUuid("U2"); pullRequest.setBranchType(BranchType.PULL_REQUEST); pullRequest.setKey(pullRequestId); pullRequest.setMergeBranchUuid("U3"); underTest.insert(dbSession, pullRequest); // select the feature branch BranchDto loaded = underTest.selectByPullRequestKey(dbSession, "U1", pullRequestId).get(); assertThat(loaded.getUuid()).isEqualTo(pullRequest.getUuid()); assertThat(loaded.getKey()).isEqualTo(pullRequest.getKey()); assertThat(loaded.getProjectUuid()).isEqualTo(pullRequest.getProjectUuid()); assertThat(loaded.getBranchType()).isEqualTo(pullRequest.getBranchType()); assertThat(loaded.getMergeBranchUuid()).isEqualTo(pullRequest.getMergeBranchUuid()); // select a branch on another project with same branch name assertThat(underTest.selectByPullRequestKey(dbSession, "U3", pullRequestId)).isEmpty(); }
@Test public void selectByBranchKey() { BranchDto mainBranch = new BranchDto(); mainBranch.setProjectUuid("U1"); mainBranch.setUuid("U1"); mainBranch.setBranchType(BranchType.LONG); mainBranch.setKey("master"); underTest.insert(dbSession, mainBranch); BranchDto featureBranch = new BranchDto(); featureBranch.setProjectUuid("U1"); featureBranch.setUuid("U2"); featureBranch.setBranchType(BranchType.SHORT); featureBranch.setKey("feature/foo"); featureBranch.setMergeBranchUuid("U3"); underTest.insert(dbSession, featureBranch); // select the feature branch BranchDto loaded = underTest.selectByBranchKey(dbSession, "U1", "feature/foo").get(); assertThat(loaded.getUuid()).isEqualTo(featureBranch.getUuid()); assertThat(loaded.getKey()).isEqualTo(featureBranch.getKey()); assertThat(loaded.getProjectUuid()).isEqualTo(featureBranch.getProjectUuid()); assertThat(loaded.getBranchType()).isEqualTo(featureBranch.getBranchType()); assertThat(loaded.getMergeBranchUuid()).isEqualTo(featureBranch.getMergeBranchUuid()); // select a branch on another project with same branch name assertThat(underTest.selectByBranchKey(dbSession, "U3", "feature/foo")).isEmpty(); }
@Test public void update_pull_request_data() { BranchDto dto = new BranchDto(); dto.setProjectUuid("U1"); dto.setUuid("U2"); dto.setBranchType(BranchType.PULL_REQUEST);
protected BranchDto toBranchDto(ComponentDto componentDto, Branch branch) { BranchDto dto = new BranchDto(); dto.setUuid(componentDto.uuid()); // MainBranchProjectUuid will be null if it's a main branch dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid())); dto.setBranchType(branch.getType()); // merge branch is only present if it's a short living branch dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null)); if (branch.getType() == BranchType.PULL_REQUEST) { dto.setKey(analysisMetadataHolder.getPullRequestKey()); DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder() .setBranch(branch.getName()) .setTitle(branch.getName()) .build(); dto.setPullRequestData(pullRequestData); } else { dto.setKey(branch.getName()); } return dto; }
public static BranchDto newBranchDto(ComponentDto branchComponent, BranchType branchType) { boolean isMain = branchComponent.getMainBranchProjectUuid() == null; String projectUuid = isMain ? branchComponent.uuid() : branchComponent.getMainBranchProjectUuid(); String key = isMain ? "master" : "branch_" + randomAlphanumeric(248); return new BranchDto() .setKey(key) .setUuid(branchComponent.uuid()) .setProjectUuid(projectUuid) .setBranchType(branchType); }
private BranchDto createBranch(DbSession session, String componentUuid) { BranchDto branch = new BranchDto() .setBranchType(BranchType.LONG) .setUuid(componentUuid) .setKey(BranchDto.DEFAULT_MAIN_BRANCH_NAME) .setMergeBranchUuid(null) .setProjectUuid(componentUuid); dbClient.branchDao().upsert(session, branch); return branch; }
private BranchDto toBranchDto(ComponentDto componentDto, Branch branch) { BranchDto dto = new BranchDto(); dto.setUuid(componentDto.uuid()); // MainBranchProjectUuid will be null if it's a main branch dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid())); dto.setBranchType(branch.getType()); // merge branch is only present if it's a short living branch dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null)); if (branch.getType() == BranchType.PULL_REQUEST) { dto.setKey(analysisMetadataHolder.getPullRequestKey()); DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder() .setBranch(branch.getName()) .setTitle(branch.getName()) .build(); dto.setPullRequestData(pullRequestData); } else { dto.setKey(branch.getName()); } return dto; }