Skip to content

Commit

Permalink
test(BranchService): add test that pinned branches and prs are shown …
Browse files Browse the repository at this point in the history
…first
  • Loading branch information
thielpa committed Feb 23, 2025
1 parent 1c2c28a commit 8c77c82
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public List<PullRequestBaseInfoDto> getAllPullRequests() {
} else if (!pr1.isPinned() && pr2.isPinned()) {
return 1;
} else {
return pr2.updatedAt().compareTo(pr1.updatedAt());
return 0;
}
})
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package de.tum.cit.aet.helios.branch;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

import de.tum.cit.aet.helios.auth.AuthService;
import de.tum.cit.aet.helios.gitrepo.GitRepository;
import de.tum.cit.aet.helios.releasecandidate.ReleaseCandidateRepository;
import de.tum.cit.aet.helios.userpreference.UserPreference;
import de.tum.cit.aet.helios.userpreference.UserPreferenceRepository;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class BranchServiceTest {

@InjectMocks private BranchService branchService;
@Mock private BranchRepository branchRepository;
@Mock private ReleaseCandidateRepository releaseCandidateRepository;
@Mock private UserPreferenceRepository userPreferenceRepository;
@Mock private AuthService authService;

@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
}

@Test
public void testPinnedBranchesAreShownFirst() {
final GitRepository repo = new GitRepository();
repo.setRepositoryId(1L);

final Branch b1 = new Branch();
b1.setName("branch1");
b1.setRepository(repo);

final Branch b2 = new Branch();
b2.setName("branch2");
b2.setRepository(repo);

final List<Branch> branches = List.of(b1, b2);

final UserPreference userPreference = new UserPreference();
userPreference.setFavouriteBranches(Set.of(b2));

when(branchRepository.findAll()).thenReturn(branches);
when(authService.getUserFromGithubId()).thenReturn(null);
when(userPreferenceRepository.findByUser(null)).thenReturn(Optional.of(userPreference));

BranchInfoDto b1Dto =
BranchInfoDto.fromBranchAndUserPreference(b1, Optional.of(userPreference));
BranchInfoDto b2Dto =
BranchInfoDto.fromBranchAndUserPreference(b2, Optional.of(userPreference));

assertEquals(2, branchService.getAllBranches().size());
Assertions.assertIterableEquals(List.of(b2Dto, b1Dto), branchService.getAllBranches());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.tum.cit.aet.helios.pullrequest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

import de.tum.cit.aet.helios.auth.AuthService;
import de.tum.cit.aet.helios.gitrepo.GitRepository;
import de.tum.cit.aet.helios.userpreference.UserPreference;
import de.tum.cit.aet.helios.userpreference.UserPreferenceRepository;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class PullRequestServiceTest {

@InjectMocks private PullRequestService pullRequestService;
@Mock private PullRequestRepository pullRequestsRepository;
@Mock private UserPreferenceRepository userPreferenceRepository;
@Mock private AuthService authService;

@BeforeEach
public void init() {
MockitoAnnotations.openMocks(this);
}

@Test
public void testPinnedBranchesAreShownFirst() {
final GitRepository repo = new GitRepository();
repo.setRepositoryId(1L);

final PullRequest pr1 = new PullRequest();
pr1.setId(1L);
pr1.setNumber(1);
pr1.setRepository(repo);

final PullRequest pr2 = new PullRequest();
pr2.setId(2L);
pr2.setNumber(2);
pr2.setRepository(repo);

final List<PullRequest> prs = List.of(pr1, pr2);

final UserPreference userPreference = new UserPreference();
userPreference.setFavouritePullRequests(Set.of(pr2));

when(pullRequestsRepository.findAllByOrderByUpdatedAtDesc()).thenReturn(prs);
when(authService.getUserFromGithubId()).thenReturn(null);
when(userPreferenceRepository.findByUser(null)).thenReturn(Optional.of(userPreference));

PullRequestBaseInfoDto pr1Dto =
PullRequestBaseInfoDto.fromPullRequestAndUserPreference(pr1, Optional.of(userPreference));

PullRequestBaseInfoDto pr2Dto =
PullRequestBaseInfoDto.fromPullRequestAndUserPreference(pr2, Optional.of(userPreference));

assertEquals(2, pullRequestService.getAllPullRequests().size());
Assertions.assertIterableEquals(
List.of(pr2Dto, pr1Dto), pullRequestService.getAllPullRequests());
}
}

0 comments on commit 8c77c82

Please sign in to comment.