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.
Merge pull request #9 from reportportal/EPMRPP-97593-update-dependencies
EPMRPP-97593 update dependencies. add unit test
- Loading branch information
Showing
8 changed files
with
482 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,23 +27,23 @@ | |
import com.epam.ta.reportportal.entity.project.Project; | ||
import com.epam.ta.reportportal.entity.project.ProjectUtils; | ||
import com.epam.ta.reportportal.entity.project.email.SenderCase; | ||
import com.slack.api.Slack; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.apache.commons.lang3.BooleanUtils; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Andrei Piankouski</a> | ||
*/ | ||
public class SlackLaunchFinishEventListener implements | ||
ApplicationListener<LaunchFinishedPluginEvent> { | ||
|
||
private final static String SLACK_NOTIFICATION_ATTRIBUTE = "notifications.slack.enabled"; | ||
public final static String SLACK_NOTIFICATION_ATTRIBUTE = "notifications.slack.enabled"; | ||
|
||
private final static String WEBHOOK_DETAILS = "webhookURL"; | ||
public final static String WEBHOOK_DETAILS = "webhookURL"; | ||
|
||
private final static String PLUGIN_NOTIFICATION_TYPE = "slack"; | ||
public final static String PLUGIN_NOTIFICATION_TYPE = "slack"; | ||
|
||
private final ProjectRepository projectRepository; | ||
|
||
|
@@ -52,15 +52,18 @@ public class SlackLaunchFinishEventListener implements | |
private final SenderCaseMatcher senderCaseMatcher; | ||
|
||
private final AttachmentResolver attachmentResolver; | ||
private final RestTemplate restTemplate; | ||
|
||
|
||
public SlackLaunchFinishEventListener( | ||
ProjectRepository projectRepository, LaunchRepository launchRepository, | ||
SenderCaseMatcher senderCaseMatcher, AttachmentResolver attachmentResolver) { | ||
SenderCaseMatcher senderCaseMatcher, AttachmentResolver attachmentResolver, | ||
RestTemplate restTemplate) { | ||
this.projectRepository = projectRepository; | ||
this.launchRepository = launchRepository; | ||
this.senderCaseMatcher = senderCaseMatcher; | ||
this.attachmentResolver = attachmentResolver; | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
@Override | ||
|
@@ -103,7 +106,7 @@ private void sendNotification(SenderCase senderCase, Launch launch, String launc | |
Optional<String> webhookUrl = getWebhookUrl(senderCase); | ||
Optional<String> attachment = resolveAttachment(launch, launchLink); | ||
if (webhookUrl.isPresent() && attachment.isPresent()) { | ||
sendSlackNotification(webhookUrl.get(), attachment.get()); | ||
restTemplate.postForLocation(webhookUrl.get(), attachment.get()); | ||
} | ||
} | ||
|
||
|
@@ -116,13 +119,6 @@ private Optional<String> resolveAttachment(Launch launch, String launchLink) { | |
return attachmentResolver.resolve(launch, launchLink); | ||
} | ||
|
||
private void sendSlackNotification(String webhookUrl, String attachment) { | ||
try (Slack slack = Slack.getInstance()) { | ||
slack.send(webhookUrl, attachment); | ||
} catch (Exception e) { | ||
throw new ReportPortalException("Failed to send Slack notification", e); | ||
} | ||
} | ||
|
||
private boolean isNotificationsEnabled(Project project) { | ||
Map<String, String> projectConfig = ProjectUtils.getConfigParameters( | ||
|
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 @@ | |
] | ||
} | ||
] | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...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,85 @@ | ||
/* | ||
* Copyright 2024 EPAM Systems | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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.Disabled; | ||
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; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Siarhei Hrabko</a> | ||
*/ | ||
@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 | ||
@Disabled("until RestTemplate initialization in SlackPluginExtension switched to @Autowired") | ||
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()); | ||
|
||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
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,119 @@ | ||
/* | ||
* Copyright 2024 EPAM Systems | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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.