-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathDiscardOldBranchTraitTest.java
More file actions
82 lines (66 loc) · 3.08 KB
/
DiscardOldBranchTraitTest.java
File metadata and controls
82 lines (66 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package io.jenkins.plugins.gitlabbranchsource;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import io.jenkins.plugins.gitlabbranchsource.DiscardOldBranchTrait.ExcludeOldSCMHeadBranch;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.trait.SCMHeadFilter;
import org.apache.commons.lang.time.DateUtils;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Commit;
import org.junit.jupiter.api.Test;
class DiscardOldBranchTraitTest {
@Test
void should_include_branch_if_last_commit_within_range() throws Exception {
DiscardOldBranchTrait uut = new DiscardOldBranchTrait(10);
GitLabSCMSourceContext context = new GitLabSCMSourceContext(null, SCMHeadObserver.none());
uut.decorateContext(context);
Optional<SCMHeadFilter> optFilter = context.filters().stream()
.filter(it -> ExcludeOldSCMHeadBranch.class.equals(it.getClass()))
.findFirst();
assertTrue(optFilter.isPresent());
SCMHead head = mock(SCMHead.class);
when(head.getName()).thenReturn("expected");
Date today = new Date();
GitLabSCMSourceRequest request = mock(GitLabSCMSourceRequest.class);
when(request.getBranches())
.thenReturn(List.of(
buildBranch("other", DateUtils.addDays(today, -7)),
buildBranch("expected", DateUtils.addDays(today, -10))));
SCMHeadFilter filter = optFilter.get();
assertFalse(filter.isExcluded(request, head));
}
@Test
void should_exclude_branch_if_last_commit_not_within_range() throws Exception {
DiscardOldBranchTrait uut = new DiscardOldBranchTrait(10);
GitLabSCMSourceContext context = new GitLabSCMSourceContext(null, SCMHeadObserver.none());
uut.decorateContext(context);
Optional<SCMHeadFilter> optFilter = context.filters().stream()
.filter(it -> ExcludeOldSCMHeadBranch.class.equals(it.getClass()))
.findFirst();
assertTrue(optFilter.isPresent());
SCMHead head = mock(SCMHead.class);
when(head.getName()).thenReturn("expected");
Date today = new Date();
GitLabSCMSourceRequest request = mock(GitLabSCMSourceRequest.class);
when(request.getBranches())
.thenReturn(List.of(
buildBranch("other", DateUtils.addDays(today, -7)),
buildBranch("expected", DateUtils.addDays(today, -11))));
SCMHeadFilter filter = optFilter.get();
assertTrue(filter.isExcluded(request, head));
}
private Branch buildBranch(String name, Date commitDate) {
Branch branch = new Branch();
branch.setName(name);
Commit commit = new Commit();
commit.setCommittedDate(commitDate);
branch.setCommit(commit);
return branch;
}
}