forked from jenkinsci/office-365-connector-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionableBuilderTest.java
More file actions
236 lines (176 loc) · 8.81 KB
/
ActionableBuilderTest.java
File metadata and controls
236 lines (176 loc) · 8.81 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package jenkins.plugins.office365connector;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.TaskListener;
import jenkins.plugins.office365connector.helpers.ReflectionHelper;
import jenkins.plugins.office365connector.helpers.SCMHeadBuilder;
import jenkins.plugins.office365connector.model.CardAction;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.metadata.ContributorMetadataAction;
import jenkins.scm.api.metadata.ObjectMetadataAction;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
class ActionableBuilderTest {
private static final String JOB_URL = "http://localhost/job/myFirstJob/167/display/redirect";
private AbstractBuild run;
private TaskListener taskListener;
private ActionableBuilder actionableBuilder;
private FactsBuilder factsBuilder;
private MockedStatic<DisplayURLProvider> displayUrlProviderStatic;
@BeforeEach
void setUp() {
run = mock(AbstractBuild.class);
taskListener = mock(TaskListener.class);
factsBuilder = new FactsBuilder(run, taskListener);
actionableBuilder = new ActionableBuilder(run, factsBuilder, false);
DisplayURLProvider displayURLProvider = mock(DisplayURLProvider.class);
when(displayURLProvider.getRunURL(run)).thenReturn(JOB_URL);
displayUrlProviderStatic = mockStatic(DisplayURLProvider.class);
displayUrlProviderStatic.when(DisplayURLProvider::get).thenReturn(displayURLProvider);
}
@AfterEach
void tearDown() {
displayUrlProviderStatic.close();
}
@Test
void buildActionable_OnEmptyAction_ReturnsEmptyList() {
// given
// from @Before
// when
List<CardAction> potentialActions = actionableBuilder.buildActionable();
// then
assertThat(potentialActions, hasSize(1));
CardAction potentialAction = potentialActions.get(0);
assertThat(potentialAction.getName(), equalTo("View Build"));
}
@Test
void pullRequestActionable_OnNoSCM_DoesNotAddFact() {
// given
// from @Before
// when
ReflectionHelper.invokeMethod(actionableBuilder, "pullRequestActionable");
// then
assertThat(factsBuilder.collect(), empty());
}
@Test
void pullRequestActionable_OnPureChangeRequestSCMHead_DoesNotAddFact() {
// given
// from @Before
SCMHead head = new SCMHeadBuilder("Pull Request");
AbstractProject job = mock(AbstractProject.class);
when(run.getParent()).thenReturn(job);
try (MockedStatic<SCMHead.HeadByItem> headByItem = mockStatic(SCMHead.HeadByItem.class)) {
headByItem.when(() -> SCMHead.HeadByItem.findHead(job)).thenReturn(head);
when(job.getAction(ObjectMetadataAction.class)).thenReturn(null);
// when
ReflectionHelper.invokeMethod(actionableBuilder, "pullRequestActionable");
}
// then
assertThat(factsBuilder.collect(), empty());
}
@Test
void pullRequestActionable_OnContributorMetadataAction_AddsFact() {
// given
// from @Before
SCMHead head = new SCMHeadBuilder("Pull Request");
AbstractProject job = mock(AbstractProject.class);
when(run.getParent()).thenReturn(job);
try (MockedStatic<SCMHead.HeadByItem> headByItem = mockStatic(SCMHead.HeadByItem.class)) {
headByItem.when(() -> SCMHead.HeadByItem.findHead(job)).thenReturn(head);
ObjectMetadataAction objectMetadataAction = mock(ObjectMetadataAction.class);
when(objectMetadataAction.getObjectUrl()).thenReturn("https://github.com/organization/repository/pull/1");
when(objectMetadataAction.getObjectDisplayName()).thenReturn("test pull request");
when(job.getAction(ObjectMetadataAction.class)).thenReturn(objectMetadataAction);
// when
ReflectionHelper.invokeMethod(actionableBuilder, "pullRequestActionable");
}
// then
assertThat(factsBuilder.collect(), hasSize(1));
List<CardAction> potentialActions = ReflectionHelper.getField(actionableBuilder,"potentialActions");
assertThat(potentialActions, hasSize(1));
}
@Test
void pullRequestActionable_OnObjectMetadataAction_DoesNotAddFact() {
// given
// from @Before
SCMHead head = new SCMHeadBuilder("Pull Request");
AbstractProject job = mock(AbstractProject.class);
when(run.getParent()).thenReturn(job);
try (MockedStatic<SCMHead.HeadByItem> headByItem = mockStatic(SCMHead.HeadByItem.class)) {
headByItem.when(() -> SCMHead.HeadByItem.findHead(job)).thenReturn(head);
when(job.getAction(ObjectMetadataAction.class)).thenReturn(null);
ContributorMetadataAction contributorMetadataAction = mock(ContributorMetadataAction.class);
when(contributorMetadataAction.getContributor()).thenReturn("damianszczepanik");
when(contributorMetadataAction.getContributorDisplayName()).thenReturn("Damian Szczepanik");
when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);
// when
ReflectionHelper.invokeMethod(actionableBuilder, "pullRequestActionable");
}
// then
assertThat(factsBuilder.collect(), hasSize(1));
List<CardAction> potentialActions = ReflectionHelper.getField(actionableBuilder,"potentialActions");
assertThat(potentialActions, empty());
}
@Test
void pullRequestActionable_OnEmptyContributor_AdddFact() {
// given
// from @Before
SCMHead head = new SCMHeadBuilder("Pull Request");
AbstractProject job = mock(AbstractProject.class);
when(run.getParent()).thenReturn(job);
try (MockedStatic<SCMHead.HeadByItem> headByItem = mockStatic(SCMHead.HeadByItem.class)) {
headByItem.when(() -> SCMHead.HeadByItem.findHead(job)).thenReturn(head);
when(job.getAction(ObjectMetadataAction.class)).thenReturn(null);
ContributorMetadataAction contributorMetadataAction = mock(ContributorMetadataAction.class);
when(contributorMetadataAction.getContributor()).thenReturn(null);
when(contributorMetadataAction.getContributorDisplayName()).thenReturn("Damian Szczepanik");
when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);
// when
ReflectionHelper.invokeMethod(actionableBuilder, "pullRequestActionable");
}
// then
String pronoun = StringUtils.defaultIfBlank(
head.getPronoun(),
Messages.Office365ConnectorWebhookNotifier_ChangeRequestPronoun());
FactAssertion.assertThat(factsBuilder)
.hasName(Messages.Office365ConnectorWebhookNotifier_AuthorHeader(pronoun))
.hasValue("Damian Szczepanik");
}
@Test
void pullRequestActionable_OnEmptyContributorDisplayName_AdddFact() {
// given
// from @Before
SCMHead head = new SCMHeadBuilder("Pull Request");
AbstractProject job = mock(AbstractProject.class);
when(run.getParent()).thenReturn(job);
try (MockedStatic<SCMHead.HeadByItem> headByItem = mockStatic(SCMHead.HeadByItem.class)) {
headByItem.when(() -> SCMHead.HeadByItem.findHead(job)).thenReturn(head);
when(job.getAction(ObjectMetadataAction.class)).thenReturn(null);
ContributorMetadataAction contributorMetadataAction = mock(ContributorMetadataAction.class);
when(contributorMetadataAction.getContributor()).thenReturn("damianszczepanik");
when(contributorMetadataAction.getContributorDisplayName()).thenReturn(null);
when(job.getAction(ContributorMetadataAction.class)).thenReturn(contributorMetadataAction);
// when
ReflectionHelper.invokeMethod(actionableBuilder, "pullRequestActionable");
}
// then
String pronoun = StringUtils.defaultIfBlank(
head.getPronoun(),
Messages.Office365ConnectorWebhookNotifier_ChangeRequestPronoun());
FactAssertion.assertThat(factsBuilder)
.hasName(Messages.Office365ConnectorWebhookNotifier_AuthorHeader(pronoun))
.hasValue("damianszczepanik");
}
}