Skip to content

Commit d53d6c5

Browse files
markush81damianszczepanik
authored andcommitted
added two integration tests to have some coverage
1 parent 5bcc080 commit d53d6c5

File tree

4 files changed

+225
-2
lines changed

4 files changed

+225
-2
lines changed

src/test/java/jenkins/plugins/office365connector/helpers/WebhookBuilder.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@
1212
*/
1313
public class WebhookBuilder {
1414

15+
public static List<Webhook> sampleWebhookWithAllStatusesAdaptiveCard() {
16+
Webhook webhook = new Webhook(ClassicDisplayURLProviderBuilder.LOCALHOST_URL_TEMPLATE);
17+
18+
enableAllStatuses(webhook);
19+
webhook.setAdaptiveCards(true);
20+
21+
return List.of(webhook);
22+
}
23+
1524
public static List<Webhook> sampleWebhookWithAllStatuses() {
1625
Webhook webhook = new Webhook(ClassicDisplayURLProviderBuilder.LOCALHOST_URL_TEMPLATE);
1726

1827
enableAllStatuses(webhook);
19-
return Arrays.asList(webhook);
28+
return List.of(webhook);
2029
}
2130

2231
public static List<Webhook> sampleMultiplyWebhookWithAllStatuses() {
@@ -41,7 +50,7 @@ private static Webhook createWebhook(String template, String value) {
4150

4251
enableAllStatuses(webhook);
4352

44-
webhook.setMacros(Arrays.asList(new Macro(template, value)));
53+
webhook.setMacros(List.of(new Macro(template, value)));
4554
return webhook;
4655
}
4756

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package jenkins.plugins.office365connector.workflow;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.mockito.Mockito.*;
6+
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
import hudson.model.AbstractBuild;
11+
import hudson.model.Job;
12+
import hudson.model.Result;
13+
import hudson.scm.ChangeLogSet;
14+
import jenkins.model.Jenkins;
15+
import jenkins.plugins.office365connector.FileUtils;
16+
import jenkins.plugins.office365connector.Office365ConnectorWebhookNotifier;
17+
import jenkins.plugins.office365connector.Webhook;
18+
import jenkins.plugins.office365connector.helpers.AffectedFileBuilder;
19+
import jenkins.plugins.office365connector.helpers.ClassicDisplayURLProviderBuilder;
20+
import jenkins.plugins.office365connector.helpers.WebhookBuilder;
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.mockito.MockedStatic;
25+
26+
/**
27+
* @author Markus Helbig (markush81@github)
28+
*/
29+
public class AdaptiveCardIT extends AbstractTest {
30+
31+
private static final String JOB_NAME = "myFirst_Job_";
32+
private static final String CAUSE_DESCRIPTION = "Started by John";
33+
private static final int BUILD_NUMBER = 167;
34+
private static final String DEVELOPER = "Mike";
35+
36+
private MockedStatic<Jenkins> staticJenkins;
37+
38+
@Before
39+
public void setUp() {
40+
staticJenkins = mockStatic(Jenkins.class);
41+
Jenkins jenkins = mock(Jenkins.class);
42+
mockListener();
43+
44+
run = mockRun();
45+
mockCause(CAUSE_DESCRIPTION);
46+
47+
mockDisplayURLProvider(JOB_NAME, BUILD_NUMBER);
48+
mockEnvironment();
49+
mockHttpWorker();
50+
mockGetChangeSets();
51+
52+
staticJenkins.when(Jenkins::get).thenReturn(jenkins);
53+
54+
Webhook.DescriptorImpl mockDescriptor = mock(Webhook.DescriptorImpl.class);
55+
when(mockDescriptor.getName()).thenReturn("testName");
56+
57+
when(jenkins.getDescriptorOrDie(Webhook.class)).thenReturn(mockDescriptor);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
staticJenkins.close();
63+
}
64+
65+
private AbstractBuild mockRun() {
66+
AbstractBuild run = mock(AbstractBuild.class);
67+
68+
when(run.getNumber()).thenReturn(BUILD_NUMBER);
69+
70+
Job job = mockJob(JOB_NAME);
71+
when(run.getParent()).thenReturn(job);
72+
73+
mockProperty(job, WebhookBuilder.sampleWebhookWithAllStatusesAdaptiveCard());
74+
75+
return run;
76+
}
77+
78+
private void mockGetChangeSets() {
79+
List<ChangeLogSet> files = new AffectedFileBuilder().singleChangeLog(run, DEVELOPER);
80+
when(run.getChangeSets()).thenReturn(files);
81+
}
82+
83+
84+
@Test
85+
public void testAdaptiveCardStarted() {
86+
87+
// given
88+
when(run.getResult()).thenReturn(Result.SUCCESS);
89+
Office365ConnectorWebhookNotifier notifier = new Office365ConnectorWebhookNotifier(run, mockListener());
90+
91+
// when
92+
notifier.sendBuildCompletedNotification();
93+
94+
// then
95+
assertHasSameContent(workerData.get(0), FileUtils.getContentFile("adaptivecard_success.json"));
96+
assertEquals(1, workerConstruction.constructed().size());
97+
}
98+
99+
@Test
100+
public void testAdaptiveCardStep() {
101+
102+
// given
103+
StepParameters stepParameters = new StepParameters(
104+
"helloMessage", ClassicDisplayURLProviderBuilder.LOCALHOST_URL_TEMPLATE,
105+
"funnyStatus", Collections.emptyList(), "#FF00FF", false);
106+
107+
when(run.getResult()).thenReturn(Result.FAILURE);
108+
Office365ConnectorWebhookNotifier notifier = new Office365ConnectorWebhookNotifier(run, mockListener());
109+
110+
// when
111+
notifier.sendBuildStepNotification(stepParameters);
112+
113+
// then
114+
assertHasSameContent(workerData.get(0), FileUtils.getContentFile("adaptivecard_step.json"));
115+
assertEquals(1, workerConstruction.constructed().size());
116+
}
117+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"summary": "myFirst_Job_: Build #167",
3+
"themeColor": "#FF00FF",
4+
"sections": [
5+
{
6+
"markdown": true,
7+
"facts": [
8+
{
9+
"name": "Status",
10+
"value": "funnyStatus"
11+
}
12+
],
13+
"activityTitle": "Notification from myFirst\\_Job\\_",
14+
"activitySubtitle": "helloMessage"
15+
}
16+
],
17+
"potentialAction": [
18+
{
19+
"@context": "http://schema.org",
20+
"@type": "ViewAction",
21+
"name": "View Build",
22+
"target": [
23+
"http://localhost/job/myFirst_Job_/167/display/redirect"
24+
]
25+
}
26+
]
27+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"type": "message",
3+
"attachments": [
4+
{
5+
"contentType": "application/vnd.microsoft.card.adaptive",
6+
"content": {
7+
"type": "AdaptiveCard",
8+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
9+
"version": "1.4",
10+
"msTeams": {
11+
"width": "Full"
12+
},
13+
"body": [
14+
{
15+
"text": "myFirst_Job_: Build #167 Success",
16+
"weight": "bolder",
17+
"size": "large",
18+
"color": "good",
19+
"type": "TextBlock",
20+
"wrap": true
21+
},
22+
{
23+
"type": "ColumnSet",
24+
"columns": [
25+
{
26+
"type": "Column",
27+
"items": [
28+
{
29+
"text": "Notification from myFirst\\_Job\\_: Build Success",
30+
"weight": "bolder",
31+
"size": "default",
32+
"color": "default",
33+
"type": "TextBlock",
34+
"wrap": true
35+
},
36+
{
37+
"text": "Latest status of build #167",
38+
"weight": "default",
39+
"size": "default",
40+
"color": "default",
41+
"type": "TextBlock",
42+
"wrap": true
43+
}
44+
]
45+
}
46+
],
47+
"witdth": "stretch"
48+
},
49+
{
50+
"facts": [
51+
{
52+
"title": "Status",
53+
"value": "Build Success"
54+
},
55+
{
56+
"title": "Remarks",
57+
"value": "Started by John."
58+
},
59+
{
60+
"title": "Developers",
61+
"value": "Mike"
62+
}
63+
],
64+
"type": "FactSet"
65+
}
66+
]
67+
}
68+
}
69+
]
70+
}

0 commit comments

Comments
 (0)