Skip to content

Commit

Permalink
Add UT for send notification API
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Dai <daichen@amazon.com>
  • Loading branch information
dai-chen committed Jul 19, 2021
1 parent c2790ee commit dde3fe6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
8 changes: 8 additions & 0 deletions notifications/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,19 @@ dependencies {
'org.assertj:assertj-core:3.19.0',
'org.mockito:mockito-junit-jupiter:3.10.0',
'com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0',
'io.mockk:mockk:1.11.0',
'io.mockk:mockk-common:1.11.0',
'io.mockk:mockk-dsl:1.11.0',
'io.mockk:mockk-dsl-jvm:1.11.0',
'io.mockk:mockk-agent-api:1.11.0',
'io.mockk:mockk-agent-common:1.11.0',
'io.mockk:mockk-agent-jvm:1.11.0',
"org.junit.jupiter:junit-jupiter-api:${junit_version}"
)
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junit_version}")
testCompile "org.opensearch.test:framework:${opensearch_version}"
testCompile "org.jetbrains.kotlin:kotlin-test:${kotlin_version}"
testCompile "org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}" // required by mockk
testCompile "org.mockito:mockito-core:3.10.0"
testCompile 'com.google.code.gson:gson:2.8.7'
testImplementation 'org.springframework.integration:spring-integration-mail:5.5.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.notifications.action

import com.nhaarman.mockitokotlin2.verify
import io.mockk.every
import io.mockk.mockkObject
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Answers
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mock
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.junit.jupiter.MockitoExtension
import org.opensearch.action.ActionListener
import org.opensearch.action.support.ActionFilters
import org.opensearch.client.Client
import org.opensearch.common.xcontent.NamedXContentRegistry
import org.opensearch.commons.notifications.action.SendNotificationRequest
import org.opensearch.commons.notifications.action.SendNotificationResponse
import org.opensearch.commons.notifications.model.ChannelMessage
import org.opensearch.commons.notifications.model.EventSource
import org.opensearch.commons.notifications.model.Feature
import org.opensearch.commons.notifications.model.SeverityType
import org.opensearch.notifications.send.SendMessageActionHelper
import org.opensearch.tasks.Task
import org.opensearch.transport.TransportService

@ExtendWith(MockitoExtension::class)
internal class SendNotificationActionTests {

@Mock
private lateinit var transportService: TransportService

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private lateinit var client: Client

@Mock
private lateinit var xContentRegistry: NamedXContentRegistry

@Mock
private lateinit var listener: ActionListener<SendNotificationResponse>

private val actionFilters = ActionFilters(setOf())

private lateinit var sendNotificationAction: SendNotificationAction

@BeforeEach
fun setUp() {
sendNotificationAction = SendNotificationAction(
transportService, client, actionFilters, xContentRegistry)
}

@Test
fun doExecute() {
val notificationId = "notification-1"
val task = mock(Task::class.java)
val request = createSendNotificationRequest()
val response = SendNotificationResponse(notificationId)

// Mock singleton's method by mockk framework
mockkObject(SendMessageActionHelper)
every { SendMessageActionHelper.executeRequest(request) } returns response

sendNotificationAction.execute(task, request, listener)
verify(listener, times(1)).onResponse(eq(response))
}

private fun createSendNotificationRequest(): SendNotificationRequest {
val notificationInfo = EventSource(
"title",
"reference_id",
Feature.REPORTS,
SeverityType.HIGH,
listOf("tag1", "tag2")
)
val channelMessage = ChannelMessage(
"text_description",
"<b>htmlDescription</b>",
null
)
return SendNotificationRequest(
notificationInfo,
channelMessage,
listOf("channelId1", "channelId2"),
"sample-thread-context"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline

0 comments on commit dde3fe6

Please sign in to comment.