Skip to content

Commit

Permalink
added two integration tests to have some coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
markush81 authored and damianszczepanik committed Oct 9, 2024
1 parent 8b09210 commit 371f5d8
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@
*/
public class WebhookBuilder {

public static List<Webhook> sampleWebhookWithAllStatusesAdaptiveCard() {
Webhook webhook = new Webhook(ClassicDisplayURLProviderBuilder.LOCALHOST_URL_TEMPLATE);

enableAllStatuses(webhook);
webhook.setAdaptiveCards(true);

return List.of(webhook);
}

public static List<Webhook> sampleWebhookWithAllStatuses() {
Webhook webhook = new Webhook(ClassicDisplayURLProviderBuilder.LOCALHOST_URL_TEMPLATE);

enableAllStatuses(webhook);
return Arrays.asList(webhook);
return List.of(webhook);
}

public static List<Webhook> sampleMultiplyWebhookWithAllStatuses() {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Jenkins> 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<ChangeLogSet> 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());
}
}
27 changes: 27 additions & 0 deletions src/test/resources/requests/adaptivecard_step.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}
70 changes: 70 additions & 0 deletions src/test/resources/requests/adaptivecard_success.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
]
}

0 comments on commit 371f5d8

Please sign in to comment.