From 371f5d81018e4d295a0c3230238c1473801642f3 Mon Sep 17 00:00:00 2001 From: Markus Helbig <3304672+markush81@users.noreply.github.com> Date: Thu, 15 Aug 2024 07:56:21 +0200 Subject: [PATCH] added two integration tests to have some coverage --- .../helpers/WebhookBuilder.java | 13 +- .../workflow/AdaptiveCardIT.java | 117 ++++++++++++++++++ .../resources/requests/adaptivecard_step.json | 27 ++++ .../requests/adaptivecard_success.json | 70 +++++++++++ 4 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 src/test/java/jenkins/plugins/office365connector/workflow/AdaptiveCardIT.java create mode 100644 src/test/resources/requests/adaptivecard_step.json create mode 100644 src/test/resources/requests/adaptivecard_success.json diff --git a/src/test/java/jenkins/plugins/office365connector/helpers/WebhookBuilder.java b/src/test/java/jenkins/plugins/office365connector/helpers/WebhookBuilder.java index 07662e10..1702d225 100644 --- a/src/test/java/jenkins/plugins/office365connector/helpers/WebhookBuilder.java +++ b/src/test/java/jenkins/plugins/office365connector/helpers/WebhookBuilder.java @@ -12,11 +12,20 @@ */ public class WebhookBuilder { + public static List sampleWebhookWithAllStatusesAdaptiveCard() { + Webhook webhook = new Webhook(ClassicDisplayURLProviderBuilder.LOCALHOST_URL_TEMPLATE); + + enableAllStatuses(webhook); + webhook.setAdaptiveCards(true); + + return List.of(webhook); + } + public static List sampleWebhookWithAllStatuses() { Webhook webhook = new Webhook(ClassicDisplayURLProviderBuilder.LOCALHOST_URL_TEMPLATE); enableAllStatuses(webhook); - return Arrays.asList(webhook); + return List.of(webhook); } public static List sampleMultiplyWebhookWithAllStatuses() { @@ -41,7 +50,7 @@ private static Webhook createWebhook(String template, String value) { enableAllStatuses(webhook); - webhook.setMacros(Arrays.asList(new Macro(template, value))); + webhook.setMacros(List.of(new Macro(template, value))); return webhook; } diff --git a/src/test/java/jenkins/plugins/office365connector/workflow/AdaptiveCardIT.java b/src/test/java/jenkins/plugins/office365connector/workflow/AdaptiveCardIT.java new file mode 100644 index 00000000..39d49678 --- /dev/null +++ b/src/test/java/jenkins/plugins/office365connector/workflow/AdaptiveCardIT.java @@ -0,0 +1,117 @@ +package jenkins.plugins.office365connector.workflow; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +import java.util.Collections; +import java.util.List; + +import hudson.model.AbstractBuild; +import hudson.model.Job; +import hudson.model.Result; +import hudson.scm.ChangeLogSet; +import jenkins.model.Jenkins; +import jenkins.plugins.office365connector.FileUtils; +import jenkins.plugins.office365connector.Office365ConnectorWebhookNotifier; +import jenkins.plugins.office365connector.Webhook; +import jenkins.plugins.office365connector.helpers.AffectedFileBuilder; +import jenkins.plugins.office365connector.helpers.ClassicDisplayURLProviderBuilder; +import jenkins.plugins.office365connector.helpers.WebhookBuilder; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.MockedStatic; + +/** + * @author Markus Helbig (markush81@github) + */ +public class AdaptiveCardIT extends AbstractTest { + + private static final String JOB_NAME = "myFirst_Job_"; + private static final String CAUSE_DESCRIPTION = "Started by John"; + private static final int BUILD_NUMBER = 167; + private static final String DEVELOPER = "Mike"; + + private MockedStatic staticJenkins; + + @Before + public void setUp() { + staticJenkins = mockStatic(Jenkins.class); + Jenkins jenkins = mock(Jenkins.class); + mockListener(); + + run = mockRun(); + mockCause(CAUSE_DESCRIPTION); + + mockDisplayURLProvider(JOB_NAME, BUILD_NUMBER); + mockEnvironment(); + mockHttpWorker(); + mockGetChangeSets(); + + staticJenkins.when(Jenkins::get).thenReturn(jenkins); + + Webhook.DescriptorImpl mockDescriptor = mock(Webhook.DescriptorImpl.class); + when(mockDescriptor.getName()).thenReturn("testName"); + + when(jenkins.getDescriptorOrDie(Webhook.class)).thenReturn(mockDescriptor); + } + + @After + public void tearDown() { + staticJenkins.close(); + } + + private AbstractBuild mockRun() { + AbstractBuild run = mock(AbstractBuild.class); + + when(run.getNumber()).thenReturn(BUILD_NUMBER); + + Job job = mockJob(JOB_NAME); + when(run.getParent()).thenReturn(job); + + mockProperty(job, WebhookBuilder.sampleWebhookWithAllStatusesAdaptiveCard()); + + return run; + } + + private void mockGetChangeSets() { + List files = new AffectedFileBuilder().singleChangeLog(run, DEVELOPER); + when(run.getChangeSets()).thenReturn(files); + } + + + @Test + public void testAdaptiveCardStarted() { + + // given + when(run.getResult()).thenReturn(Result.SUCCESS); + Office365ConnectorWebhookNotifier notifier = new Office365ConnectorWebhookNotifier(run, mockListener()); + + // when + notifier.sendBuildCompletedNotification(); + + // then + assertHasSameContent(workerData.get(0), FileUtils.getContentFile("adaptivecard_success.json")); + assertEquals(1, workerConstruction.constructed().size()); + } + + @Test + public void testAdaptiveCardStep() { + + // given + StepParameters stepParameters = new StepParameters( + "helloMessage", ClassicDisplayURLProviderBuilder.LOCALHOST_URL_TEMPLATE, + "funnyStatus", Collections.emptyList(), "#FF00FF", false); + + when(run.getResult()).thenReturn(Result.FAILURE); + Office365ConnectorWebhookNotifier notifier = new Office365ConnectorWebhookNotifier(run, mockListener()); + + // when + notifier.sendBuildStepNotification(stepParameters); + + // then + assertHasSameContent(workerData.get(0), FileUtils.getContentFile("adaptivecard_step.json")); + assertEquals(1, workerConstruction.constructed().size()); + } +} diff --git a/src/test/resources/requests/adaptivecard_step.json b/src/test/resources/requests/adaptivecard_step.json new file mode 100644 index 00000000..69e4a369 --- /dev/null +++ b/src/test/resources/requests/adaptivecard_step.json @@ -0,0 +1,27 @@ +{ + "summary": "myFirst_Job_: Build #167", + "themeColor": "#FF00FF", + "sections": [ + { + "markdown": true, + "facts": [ + { + "name": "Status", + "value": "funnyStatus" + } + ], + "activityTitle": "Notification from myFirst\\_Job\\_", + "activitySubtitle": "helloMessage" + } + ], + "potentialAction": [ + { + "@context": "http://schema.org", + "@type": "ViewAction", + "name": "View Build", + "target": [ + "http://localhost/job/myFirst_Job_/167/display/redirect" + ] + } + ] +} diff --git a/src/test/resources/requests/adaptivecard_success.json b/src/test/resources/requests/adaptivecard_success.json new file mode 100644 index 00000000..672772a2 --- /dev/null +++ b/src/test/resources/requests/adaptivecard_success.json @@ -0,0 +1,70 @@ +{ + "type": "message", + "attachments": [ + { + "contentType": "application/vnd.microsoft.card.adaptive", + "content": { + "type": "AdaptiveCard", + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "version": "1.4", + "msTeams": { + "width": "Full" + }, + "body": [ + { + "text": "myFirst_Job_: Build #167 Success", + "weight": "bolder", + "size": "large", + "color": "good", + "type": "TextBlock", + "wrap": true + }, + { + "type": "ColumnSet", + "columns": [ + { + "type": "Column", + "items": [ + { + "text": "Notification from myFirst\\_Job\\_: Build Success", + "weight": "bolder", + "size": "default", + "color": "default", + "type": "TextBlock", + "wrap": true + }, + { + "text": "Latest status of build #167", + "weight": "default", + "size": "default", + "color": "default", + "type": "TextBlock", + "wrap": true + } + ] + } + ], + "witdth": "stretch" + }, + { + "facts": [ + { + "title": "Status", + "value": "Build Success" + }, + { + "title": "Remarks", + "value": "Started by John." + }, + { + "title": "Developers", + "value": "Mike" + } + ], + "type": "FactSet" + } + ] + } + } + ] +}