Skip to content

Commit

Permalink
Add tests for builders
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsuter committed Dec 24, 2024
1 parent 67f792d commit 3d64dc7
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/ivyteam/devops/branch/BranchRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public void create(Branch branch) {
}
}

public void delete(Branch branch) {
public void delete(String repo, String name) {
try (var connection = db.connection()) {
try (var s = connection.prepareStatement("DELETE FROM branch WHERE repository = ? AND name = ?")) {
s.setString(1, branch.repository());
s.setString(2, branch.name());
s.setString(1, repo);
s.setString(2, name);
s.execute();
}
} catch (SQLException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ public ResponseEntity<Branch> push(@RequestBody PushBean bean) {
}

@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE, headers = "X-GitHub-Event=delete")
public ResponseEntity<Branch> deleteBranch(@RequestBody BranchBean bean) {
public ResponseEntity<BranchBean> deleteBranch(@RequestBody BranchBean bean) {
validateBean(bean);
if ("branch".equals(bean.ref_type)) {
var branch = bean.toBranch();
branches.delete(branch);
return ResponseEntity.ok().body(branch);
branches.delete(bean.repo(), bean.name());
return ResponseEntity.ok().body(bean);
}
throw new RuntimeException("ref type not supported: " + bean.ref_type);
}
Expand Down Expand Up @@ -124,12 +123,12 @@ record BranchBean(
Organization organization,
String updated_at) {

Branch toBranch() {
var authoredDate = new Date();
if (this.updated_at != null) {
authoredDate = tsToDate(this.updated_at);
}
return new Branch(this.repository.full_name, this.ref, this.sender.login, false, authoredDate);
String repo() {
return repository.full_name;
}

String name() {
return ref;
}
}

Expand All @@ -151,11 +150,9 @@ PullRequest toPullRequest() {
}

record Commit(String id, String timestamp, String url, Author author) {

}

record Author(String username) {

}

record Organization(String login) {
Expand All @@ -165,11 +162,9 @@ record Repository(String full_name) {
}

record User(String login) {

}

record PrDetail(long number, String title, User user, Head head) {

}

record Head(String ref) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,23 @@ private static SecurityScanner parseAlerts(String json, String repoName, ScanTyp
}

public void synch(ScanType scantype) throws IOException {
var json = SecurityScannerApiHelper.getAlerts(repo.getUrl(), token, scantype);
var json = getAlerts(repo.getUrl(), token, scantype);
if (json == null) {
return;
}
var alerts = SecurityScannerApiHelper.parseAlerts(json, repo.getName(), scantype);
var alerts = parseAlerts(json, repo.getName(), scantype);
if (alerts == null) {
return;
}
SecurityScanner ss = new SecurityScanner(repo.getFullName(), alerts.scantype(), alerts.critical(), alerts.high(),
alerts.medium(), alerts.low());
securityScanners.create(ss);
var scanner = SecurityScanner.create()
.repo(repo.getFullName())
.scantype(alerts.scantype())
.critical(alerts.critical())
.high(alerts.high())
.medium(alerts.medium())
.low(alerts.medium())
.build();
securityScanners.create(scanner);
}

private static URI toUri(URL url, String path) {
Expand All @@ -144,5 +150,4 @@ private static URI toUri(URL url, String path) {
throw new RuntimeException("Failed to build URI for dependabot alerts: " + url, ex);
}
}

}
60 changes: 60 additions & 0 deletions src/test/java/io/ivyteam/devops/branch/TestBranch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.ivyteam.devops.branch;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Date;

import org.junit.jupiter.api.Test;

class TestBranch {

private static final Date NOW = new Date();

private static final Branch BRANCH = Branch.create()
.lastCommitAuthor("alex")
.protectedBranch(false)
.name("test-alex")
.repository("axonivy/test")
.authoredDate(NOW)
.build();

@Test
void name() {
assertThat(BRANCH.name()).isEqualTo("test-alex");
}

@Test
void repository() {
assertThat(BRANCH.repository()).isEqualTo("axonivy/test");
}

@Test
void lastCommitAuthor() {
assertThat(BRANCH.lastCommitAuthor()).isEqualTo("alex");
}

@Test
void authoredDate() {
assertThat(BRANCH.authoredDate()).isEqualTo(NOW);
}

@Test
void protectedBranch() {
assertThat(BRANCH.protectedBranch()).isFalse();
}

@Test
void repoLink() {
assertThat(BRANCH.repoLink()).isEqualTo("/repository/axonivy/test");
}

@Test
void ghRepoLink() {
assertThat(BRANCH.ghRepoLink()).isEqualTo("https://github.com/axonivy/test");
}

@Test
void ghLink() {
assertThat(BRANCH.ghLink()).isEqualTo("https://github.com/axonivy/test/tree/test-alex");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void create() {
void delete() {
repos.create(Repo.create().name("axonivy/test").build());
branches.create(BRANCH);
branches.delete(BRANCH);
branches.delete("axonivy/test", "test-alex");
assertThat(branches.all()).isEmpty();
}

Expand Down
41 changes: 41 additions & 0 deletions src/test/java/io/ivyteam/devops/pullrequest/TestPullRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.ivyteam.devops.pullrequest;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class TestPullRequest {

private static final PullRequest PR = PullRequest.create()
.title("PR-1")
.user("alex")
.repository("axonivy/test")
.id(1)
.branchName("master")
.build();

@Test
void title() {
assertThat(PR.title()).isEqualTo("PR-1");
}

@Test
void user() {
assertThat(PR.user()).isEqualTo("alex");
}

@Test
void repository() {
assertThat(PR.repository()).isEqualTo("axonivy/test");
}

@Test
void id() {
assertThat(PR.id()).isEqualTo(1);
}

@Test
void branchName() {
assertThat(PR.branchName()).isEqualTo("master");
}
}
71 changes: 71 additions & 0 deletions src/test/java/io/ivyteam/devops/repo/TestRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.ivyteam.devops.repo;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class TestRepo {

private static final Repo REPO = Repo.create()
.name("dummy/abc")
.codeOfConduct("code of conduct")
.deleteBranchOnMerge(true)
.fork(false)
.isVulnAlertOn(true)
.license("lic")
.issues(false)
.privateRepo(false)
.archived(false)
.securityMd("security md")
.build();

@Test
void name() {
assertThat(REPO.name()).isEqualTo("dummy/abc");
}

@Test
void codeOfConduct() {
assertThat(REPO.codeOfConduct()).isEqualTo("code of conduct");
}

@Test
void deleteBranchOnMerge() {
assertThat(REPO.deleteBranchOnMerge()).isTrue();
}

@Test
void fork() {
assertThat(REPO.fork()).isFalse();
}

@Test
void isVulnAlertOn() {
assertThat(REPO.isVulnAlertOn()).isTrue();
}

@Test
void license() {
assertThat(REPO.license()).isEqualTo("lic");
}

@Test
void issues() {
assertThat(REPO.issues()).isFalse();
}

@Test
void privateRepo() {
assertThat(REPO.privateRepo()).isFalse();
}

@Test
void archived() {
assertThat(REPO.archived()).isFalse();
}

@Test
void securityMd() {
assertThat(REPO.securityMd()).isEqualTo("security md");
}
}

0 comments on commit 3d64dc7

Please sign in to comment.