generated from reportportal/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EPMRPP-97593 update dependencies. add unit test
- Loading branch information
Showing
8 changed files
with
414 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
version=1.0.0 | ||
description=EPAM Report Portal. Slack plugin. | ||
pluginId = slack | ||
lombokVersion=1.18.36 | ||
junitVersion=5.11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,4 +204,4 @@ | |
] | ||
} | ||
] | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...om/epam/reportportal/extension/slack/event/launch/SlackLaunchFinishEventListenerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.epam.reportportal.extension.slack.event.launch; | ||
|
||
import static com.epam.reportportal.extension.slack.utils.SampleAttachment.SAMPLE_ATTACHMENT; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.epam.reportportal.extension.event.LaunchFinishedPluginEvent; | ||
import com.epam.reportportal.extension.slack.event.launch.resolver.AttachmentResolver; | ||
import com.epam.reportportal.extension.slack.event.launch.resolver.SenderCaseMatcher; | ||
import com.epam.reportportal.extension.slack.utils.MockData; | ||
import com.epam.ta.reportportal.dao.LaunchRepository; | ||
import com.epam.ta.reportportal.dao.ProjectRepository; | ||
import com.epam.ta.reportportal.entity.launch.Launch; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Answers; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SlackLaunchFinishEventListenerTest { | ||
|
||
private static final String LAUNCH_LINK = "http://localhost:8080/ui/#admin123/launches/all/55"; | ||
|
||
@Mock | ||
RestTemplate restTemplate;// = new RestTemplate(); | ||
@Mock | ||
ProjectRepository projectRepository; | ||
@Mock | ||
LaunchRepository launchRepository; | ||
@Mock(answer = Answers.CALLS_REAL_METHODS) | ||
SenderCaseMatcher senderCaseMatcher; | ||
@Mock | ||
AttachmentResolver attachmentResolver; | ||
|
||
@Test | ||
void sendNotificationPositive() throws URISyntaxException { | ||
var slackLaunchFinishEventListener = new SlackLaunchFinishEventListener(projectRepository, | ||
launchRepository, senderCaseMatcher, attachmentResolver, restTemplate); | ||
|
||
when(projectRepository.findById(anyLong())) | ||
.thenReturn(Optional.of(MockData.getProjectSample())); | ||
when(launchRepository.findById(anyLong())) | ||
.thenReturn(Optional.of(MockData.getLaunch())); | ||
when(attachmentResolver.resolve(any(Launch.class), anyString())) | ||
.thenReturn(Optional.of(SAMPLE_ATTACHMENT)); | ||
when(restTemplate.postForLocation(anyString(), anyString())) | ||
.thenReturn(new URI("http://localhost:8080")); | ||
|
||
slackLaunchFinishEventListener.onApplicationEvent( | ||
new LaunchFinishedPluginEvent(1L, 10L, LAUNCH_LINK)); | ||
|
||
verify(restTemplate, times(1)).postForLocation(anyString(), anyString()); | ||
|
||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/test/java/com/epam/reportportal/extension/slack/utils/MockData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.epam.reportportal.extension.slack.utils; | ||
|
||
import static com.epam.reportportal.extension.slack.event.launch.SlackLaunchFinishEventListener.PLUGIN_NOTIFICATION_TYPE; | ||
import static com.epam.reportportal.extension.slack.event.launch.SlackLaunchFinishEventListener.SLACK_NOTIFICATION_ATTRIBUTE; | ||
import static com.epam.reportportal.extension.slack.event.launch.SlackLaunchFinishEventListener.WEBHOOK_DETAILS; | ||
import static com.epam.ta.reportportal.entity.enums.ProjectAttributeEnum.NOTIFICATIONS_ENABLED; | ||
|
||
import com.epam.ta.reportportal.entity.attribute.Attribute; | ||
import com.epam.ta.reportportal.entity.enums.SendCase; | ||
import com.epam.ta.reportportal.entity.launch.Launch; | ||
import com.epam.ta.reportportal.entity.project.Project; | ||
import com.epam.ta.reportportal.entity.project.ProjectAttribute; | ||
import com.epam.ta.reportportal.entity.project.email.SenderCase; | ||
import com.epam.ta.reportportal.entity.project.email.SenderCaseOptions; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
public class MockData { | ||
|
||
private static final String LAUNCH_NAME_1 = "Launch name 1"; | ||
|
||
public static Project getProjectSample() { | ||
var project = new Project(); | ||
|
||
project.setId(1L); | ||
project.setSenderCases(Collections.singleton(getSenderCase())); | ||
project.setProjectAttributes(getProjectAttributes()); | ||
return project; | ||
} | ||
|
||
private static Set<ProjectAttribute> getProjectAttributes() { | ||
var attribute1 = new Attribute(); | ||
attribute1.setId(13L); | ||
attribute1.setName(NOTIFICATIONS_ENABLED.getAttribute()); | ||
|
||
var attribute2 = new Attribute(); | ||
attribute2.setId(21L); | ||
attribute2.setName(SLACK_NOTIFICATION_ATTRIBUTE); | ||
|
||
ProjectAttribute projectAttribute1 = new ProjectAttribute() | ||
.withAttribute(attribute1) | ||
.withProject(new Project()) | ||
.withValue("true"); | ||
|
||
ProjectAttribute projectAttribute2 = new ProjectAttribute() | ||
.withAttribute(attribute2) | ||
.withProject(new Project()) | ||
.withValue("true"); | ||
|
||
return Set.of(projectAttribute1, projectAttribute2); | ||
|
||
} | ||
|
||
|
||
public static SenderCase getSenderCase() { | ||
var senderCase = new SenderCase(); | ||
senderCase.setEnabled(true); | ||
senderCase.setType(PLUGIN_NOTIFICATION_TYPE); | ||
senderCase.setSendCase(SendCase.ALWAYS); | ||
senderCase.setLaunchNames(Set.of(LAUNCH_NAME_1)); | ||
senderCase.setRuleDetails(getSenderCaseOptions()); | ||
return senderCase; | ||
} | ||
|
||
public static Launch getLaunch() { | ||
var launch = new Launch(); | ||
launch.setId(20L); | ||
launch.setName(LAUNCH_NAME_1); | ||
return launch; | ||
} | ||
|
||
public static SenderCaseOptions getSenderCaseOptions() { | ||
SenderCaseOptions senderCaseOptions = new SenderCaseOptions(); | ||
senderCaseOptions.setOptions( | ||
Map.of(WEBHOOK_DETAILS, | ||
"https://hooks.slack.com/services/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")); | ||
return senderCaseOptions; | ||
} | ||
|
||
public static String readFileToString(String path) throws IOException { | ||
|
||
try (InputStream resourceAsStream = MockData.class.getClassLoader().getResourceAsStream(path)) { | ||
if (resourceAsStream != null) { | ||
return IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8); | ||
} else { | ||
StringBuilder sb = new StringBuilder(); | ||
try (Stream<String> lines = Files.lines(Paths.get(path))) { | ||
lines.forEach(sb::append); | ||
} | ||
return sb.toString(); | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.