-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SLI-1704 Add tests for notifications
- Loading branch information
1 parent
c2d8142
commit f4f4c07
Showing
3 changed files
with
275 additions
and
15 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
275 changes: 275 additions & 0 deletions
275
src/test/java/org/sonarlint/intellij/notifications/SonarLintProjectNotificationsTests.kt
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,275 @@ | ||
package org.sonarlint.intellij.notifications | ||
|
||
import com.google.common.collect.Lists | ||
import com.intellij.notification.Notification | ||
import com.intellij.notification.NotificationGroup | ||
import com.intellij.notification.NotificationType | ||
import com.intellij.notification.Notifications | ||
import com.intellij.notification.NotificationsManager | ||
import com.intellij.util.messages.MessageBusConnection | ||
import java.util.stream.Stream | ||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.sonarlint.intellij.AbstractSonarLintLightTests | ||
import org.sonarlint.intellij.SonarLintIcons | ||
import org.sonarlint.intellij.config.global.ServerConnection | ||
import org.sonarsource.sonarlint.core.rpc.protocol.client.smartnotification.ShowSmartNotificationParams | ||
|
||
class SonarLintProjectNotificationsTests : AbstractSonarLintLightTests() { | ||
|
||
private lateinit var notifications: MutableList<Notification> | ||
private lateinit var busConnection: MessageBusConnection | ||
private lateinit var sonarLintProjectNotifications: SonarLintProjectNotifications | ||
|
||
@BeforeEach | ||
fun before() { | ||
sonarLintProjectNotifications = SonarLintProjectNotifications(project) | ||
|
||
// register a listener to catch all notifications | ||
notifications = Lists.newCopyOnWriteArrayList() | ||
val project = project | ||
busConnection = project.messageBus.connect(project) | ||
busConnection.subscribe(Notifications.TOPIC, object : Notifications { | ||
override fun notify(notification: Notification) { | ||
notifications.add(notification) | ||
} | ||
}) | ||
} | ||
|
||
@AfterEach | ||
fun expireAfterTest() { | ||
val mgr = NotificationsManager.getNotificationsManager() | ||
val notifs = mgr.getNotificationsOfType(Notification::class.java, project) | ||
Stream.of(*notifs).forEach { notification -> mgr.expire(notification) } | ||
busConnection.disconnect() | ||
} | ||
|
||
@Test | ||
fun should_notify_project_binding_invalid_and_unbound() { | ||
sonarLintProjectNotifications.notifyProjectBindingInvalidAndUnbound() | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo("<b>SonarLint - Invalid binding</b>") | ||
Assertions.assertThat(notifications[0].content) | ||
.isEqualTo("Project binding is invalid and has been removed, the connection has probably been deleted previously.") | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.WARNING) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(1) | ||
} | ||
|
||
@Test | ||
fun should_notify_language_promotion() { | ||
val content = "content" | ||
|
||
sonarLintProjectNotifications.notifyLanguagePromotion(content) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo("<b>SonarLint suggestions</b>") | ||
Assertions.assertThat(notifications[0].content).isEqualTo(content) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.INFORMATION) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(4) | ||
} | ||
|
||
@Test | ||
fun should_notify_unable_to_open_finding() { | ||
val type = "issue" | ||
val message = "message" | ||
|
||
sonarLintProjectNotifications.notifyUnableToOpenFinding(type, message) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo("<b>SonarLint - Unable to open $type</b>") | ||
Assertions.assertThat(notifications[0].content).isEqualTo(message) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.INFORMATION) | ||
Assertions.assertThat(notifications[0].isExpired).isEqualTo(false) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions).isEmpty() | ||
} | ||
|
||
@Test | ||
fun should_expire_current_finding_notification() { | ||
val type = "issue" | ||
val message = "message" | ||
sonarLintProjectNotifications.notifyUnableToOpenFinding(type, message) | ||
|
||
sonarLintProjectNotifications.expireCurrentFindingNotificationIfNeeded() | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].isExpired).isEqualTo(true) | ||
} | ||
|
||
@Test | ||
fun should_handle_sonarqube_smart_notification() { | ||
val connection = ServerConnection.newBuilder().setName("connectionId").setHostUrl("url").build() | ||
globalSettings.serverConnections = listOf(connection) | ||
|
||
val param = ShowSmartNotificationParams( | ||
"text", | ||
"link", | ||
setOf("scopeId"), | ||
"category", | ||
"connectionId" | ||
) | ||
|
||
sonarLintProjectNotifications.handle(param) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo("<b>SonarQube Notification</b>") | ||
Assertions.assertThat(notifications[0].content).isEqualTo("text") | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.INFORMATION) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.ICON_SONARQUBE_16) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(2) | ||
} | ||
|
||
@Test | ||
fun should_handle_sonarcloud_smart_notification() { | ||
val connection = ServerConnection.newBuilder().setName("connectionId").setHostUrl("https://sonarcloud.io").build() | ||
globalSettings.serverConnections = listOf(connection) | ||
|
||
val param = ShowSmartNotificationParams( | ||
"text", | ||
"link", | ||
setOf("scopeId"), | ||
"category", | ||
"connectionId" | ||
) | ||
|
||
sonarLintProjectNotifications.handle(param) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo("<b>SonarCloud Notification</b>") | ||
Assertions.assertThat(notifications[0].content).isEqualTo("text") | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.INFORMATION) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.ICON_SONARCLOUD_16) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(2) | ||
} | ||
|
||
@Test | ||
fun should_display_successful_notification() { | ||
val content = "content" | ||
val group = NotificationGroup.allRegisteredGroups.first() | ||
|
||
sonarLintProjectNotifications.displaySuccessfulNotification(content, group) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isBlank() | ||
Assertions.assertThat(notifications[0].content).isEqualTo(content) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.INFORMATION) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions).isEmpty() | ||
} | ||
|
||
@Test | ||
fun should_display_error_notification() { | ||
val title = "title" | ||
val content = "content" | ||
val group = NotificationGroup.allRegisteredGroups.first() | ||
|
||
sonarLintProjectNotifications.displayErrorNotification(title, content, group) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo(title) | ||
Assertions.assertThat(notifications[0].content).isEqualTo(content) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.ERROR) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(1) | ||
} | ||
|
||
@Test | ||
fun should_display_warning_notification() { | ||
val content = "content" | ||
val group = NotificationGroup.allRegisteredGroups.first() | ||
|
||
sonarLintProjectNotifications.displayWarningNotification(content, group) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isBlank() | ||
Assertions.assertThat(notifications[0].content).isEqualTo(content) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.WARNING) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(1) | ||
} | ||
|
||
@Test | ||
fun should_send_notification_for_secret() { | ||
sonarLintProjectNotifications.sendNotification() | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo("SonarLint: secret(s) detected") | ||
Assertions.assertThat(notifications[0].content).isEqualTo( | ||
"SonarLint detected some secrets in one of the open files. " + | ||
"We strongly advise you to review those secrets and ensure they are not committed into repositories. " + | ||
"Please refer to the SonarLint tool window for more information." | ||
) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.WARNING) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions).isEmpty() | ||
} | ||
|
||
@Test | ||
fun should_display_one_time_balloon() { | ||
val message = "message" | ||
val doNotShowAgainId = "id" | ||
|
||
sonarLintProjectNotifications.showOneTimeBalloon(message, doNotShowAgainId, null) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isBlank() | ||
Assertions.assertThat(notifications[0].content).isEqualTo(message) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.WARNING) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(1) | ||
} | ||
|
||
@Test | ||
fun should_show_shared_configuration() { | ||
val title = "title" | ||
val message = "message" | ||
val doNotShowAgainId = "id" | ||
|
||
sonarLintProjectNotifications.showSharedConfigurationNotification(title, message, doNotShowAgainId) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo(title) | ||
Assertions.assertThat(notifications[0].content).isEqualTo(message) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.WARNING) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(2) | ||
} | ||
|
||
@Test | ||
fun should_show_auto_shared_configuration() { | ||
val title = "title" | ||
val message = "message" | ||
val doNotShowAgainId = "id" | ||
|
||
sonarLintProjectNotifications.showAutoSharedConfigurationNotification(title, message, doNotShowAgainId, null) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo(title) | ||
Assertions.assertThat(notifications[0].content).isEqualTo(message) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(NotificationType.INFORMATION) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions.size).isEqualTo(1) | ||
} | ||
|
||
@Test | ||
fun should_display_simple_notification() { | ||
val title = "title" | ||
val message = "message" | ||
val type = NotificationType.INFORMATION | ||
|
||
sonarLintProjectNotifications.simpleNotification(title, message, type) | ||
|
||
Assertions.assertThat(notifications).hasSize(1) | ||
Assertions.assertThat(notifications[0].title).isEqualTo(title) | ||
Assertions.assertThat(notifications[0].content).isEqualTo(message) | ||
Assertions.assertThat(notifications[0].type).isEqualTo(type) | ||
Assertions.assertThat(notifications[0].icon).isEqualTo(SonarLintIcons.SONARLINT) | ||
Assertions.assertThat(notifications[0].actions).isEmpty() | ||
} | ||
|
||
} |