Skip to content

Commit

Permalink
Merge pull request #146 from axonivy/more-tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
alexsuter authored Dec 23, 2024
2 parents b07bc41 + f6cf007 commit d4b62da
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/main/java/io/ivyteam/devops/github/GitHubSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,13 @@ private String license(GHRepository repo, String file) throws IOException {

private PullRequest toPullRequest(GHPullRequest pr) {
try {
var title = pr.getTitle();
var user = pr.getUser().getLogin();
var id = pr.getNumber();
var p = new PullRequest(pr.getRepository().getFullName(), id, title, user, pr.getHead().getRef());
return p;
return PullRequest.create()
.repository(pr.getRepository().getFullName())
.id(pr.getNumber())
.title(pr.getTitle())
.user(pr.getUser().getLogin())
.branchName(pr.getHead().getRef())
.build();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,13 @@ record PullRequestBean(
Organization organization) {

PullRequest toPullRequest() {
return new PullRequest(this.repository.full_name, this.pull_request.number, this.pull_request.title,
this.pull_request.user.login, this.pull_request.head.ref);
return PullRequest.create()
.repository(this.repository.full_name)
.id(this.pull_request.number)
.title(this.pull_request.title)
.user(this.pull_request.user.login)
.branchName(this.pull_request.head.ref)
.build();
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/io/ivyteam/devops/pullrequest/PullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,53 @@ public record PullRequest(
String user,
String branchName) {

public static Builder create() {
return new Builder();
}

public String repoLink() {
return "/repository/" + repository;
}

public String ghLink() {
return "https://github.com/" + repository + "/pull/" + id;
}

public static class Builder {

private String repository;
private long id;
private String title;
private String user;
private String branchName;

public Builder repository(String repository) {
this.repository = repository;
return this;
}

public Builder id(long id) {
this.id = id;
return this;
}

public Builder title(String title) {
this.title = title;
return this;
}

public Builder user(String user) {
this.user = user;
return this;
}

public Builder branchName(String branchName) {
this.branchName = branchName;
return this;
}

public PullRequest build() {
return new PullRequest(repository, id, title, user, branchName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class PullRequestRepository {
@Autowired
private UserRepository users;

public PullRequestRepository(Database db, UserRepository users) {
this.db = db;
this.users = users;
}

public List<PullRequest> all() {
try (var connection = db.connection()) {
try (var stmt = connection.prepareStatement("SELECT * FROM pull_request ORDER BY title")) {
Expand Down Expand Up @@ -102,12 +107,13 @@ private List<PullRequest> query(PreparedStatement stmt) throws SQLException {
}

private PullRequest toPr(ResultSet result) throws SQLException {
var repository = result.getString("repository");
var id = result.getLong("id");
var title = result.getString("title");
var user = result.getString("user");
var branchName = result.getString("branchName");
return new PullRequest(repository, id, title, user, branchName);
return PullRequest.create()
.repository(result.getString("repository"))
.id(result.getLong("id"))
.title(result.getString("title"))
.user(result.getString("user"))
.branchName(result.getString("branchName"))
.build();
}

public Map<String, Long> countByRepo() {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/io/ivyteam/devops/repo/Repo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,95 @@ public record Repo(
public String link() {
return "/repository/" + name;
}

public static Builder create() {
return new Builder();
}

public static class Builder {

private String name;
private boolean archived;
private boolean privateRepo;
private boolean deleteBranchOnMerge;
private boolean projects;
private boolean issues;
private boolean wiki;
private boolean hooks;
private boolean fork;
private boolean isVulnAlertOn;
private String license;
private String securityMd;
private String codeOfConduct;

public Builder name(String name) {
this.name = name;
return this;
}

public Builder archived(boolean archived) {
this.archived = archived;
return this;
}

public Builder privateRepo(boolean privateRepo) {
this.privateRepo = privateRepo;
return this;
}

public Builder deleteBranchOnMerge(boolean deleteBranchOnMerge) {
this.deleteBranchOnMerge = deleteBranchOnMerge;
return this;
}

public Builder projects(boolean projects) {
this.projects = projects;
return this;
}

public Builder issues(boolean issues) {
this.issues = issues;
return this;
}

public Builder wiki(boolean wiki) {
this.wiki = wiki;
return this;
}

public Builder hooks(boolean hooks) {
this.hooks = hooks;
return this;
}

public Builder fork(boolean fork) {
this.fork = fork;
return this;
}

public Builder isVulnAlertOn(boolean isVulnAlertOn) {
this.isVulnAlertOn = isVulnAlertOn;
return this;
}

public Builder license(String license) {
this.license = license;
return this;
}

public Builder securityMd(String securityMd) {
this.securityMd = securityMd;
return this;
}

public Builder codeOfConduct(String codeOfConduct) {
this.codeOfConduct = codeOfConduct;
return this;
}

public Repo build() {
return new Repo(name, archived, privateRepo, deleteBranchOnMerge, projects, issues, wiki, hooks, fork,
isVulnAlertOn, license, securityMd, codeOfConduct);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/ivyteam/devops/repo/RepoRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class RepoRepository {
@Autowired
private Database db;

public RepoRepository(Database db) {
this.db = db;
}

public List<Repo> all() {
try (var connection = db.connection()) {
try (var stmt = connection.prepareStatement("SELECT * FROM repository ORDER BY name")) {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/ivyteam/devops/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,28 @@ public record User(String name, String avatarUrl) {
public String link() {
return "/users/" + name;
}

public static Builder create() {
return new Builder();
}

public static class Builder {

private String name;
private String avatarUrl;

public Builder name(String name) {
this.name = name;
return this;
}

public Builder avatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
return this;
}

public User build() {
return new User(name, avatarUrl);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.ivyteam.devops.pullrequest;

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

import java.nio.file.Path;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import io.ivyteam.devops.db.Database;
import io.ivyteam.devops.repo.Repo;
import io.ivyteam.devops.repo.RepoRepository;
import io.ivyteam.devops.user.UserRepository;

class TestPullRequestRepository {

@TempDir
Path tempDir;

RepoRepository repos;

PullRequestRepository prs;

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

@BeforeEach
void beforeEach() {
var db = new Database(tempDir.resolve("test.db"));
var users = new UserRepository(db);
repos = new RepoRepository(db);
repos.create(Repo.create().name("axonivy/test").build());
prs = new PullRequestRepository(db, users);
}

@Test
void all_empty() {
assertThat(prs.all()).isEmpty();
}

@Test
void findByUser_empty() {
assertThat(prs.findByUser("suter")).isEmpty();
}

@Test
void findByUser() {
prs.create(PR);
assertThat(prs.findByUser("alex")).containsExactly(PR);
}

@Test
void findByRepository_empty() {
assertThat(prs.findByRepository("gotham")).isEmpty();
}

@Test
void findByRepository() {
prs.create(PR);
assertThat(prs.findByRepository("axonivy/test")).containsExactly(PR);
}

@Test
void delete() {
prs.create(PR);
prs.delete(PR);
assertThat(prs.all()).isEmpty();
}

@Test
void countByRepo() {
prs.create(PR);
assertThat(prs.countByRepo()).containsExactly(Map.entry("axonivy/test", 1L));
}

@Test
void create() {
prs.create(PR);
assertThat(prs.all()).containsExactly(PR);
}
}
34 changes: 32 additions & 2 deletions src/test/java/io/ivyteam/devops/user/TestUserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@

class TestUserRepository {

private static final User ALEX = new User("alex", "https://example.com/alex.png");
private static final User LOUIS = new User("louis", "https://example.com/louis.png");
private static final User ALEX = User.create()
.name("alex")
.avatarUrl("https://example.com/alex.png")
.build();

private static final User LOUIS = User.create()
.name("louis")
.avatarUrl("https://example.com/louis.png")
.build();

@TempDir
Path tempDir;
Expand Down Expand Up @@ -54,4 +61,27 @@ void all_ordered() {
void all_empty() {
assertThat(users.all()).isEmpty();
}

@Test
void update() {
users.create(ALEX);
var update = UserUpdate.create("alex")
.avatarUrl("https://example.com/suter.png")
.build();
users.update(update);
var suter = User.create()
.name("alex")
.avatarUrl("https://example.com/suter.png")
.build();
assertThat(users.all()).containsExactly(suter);
}

@Test
void update_nonExisting() {
var update = UserUpdate.create("lukas")
.avatarUrl("https://example.com/alex.png")
.build();
users.update(update);
assertThat(users.all()).isEmpty();
}
}

0 comments on commit d4b62da

Please sign in to comment.