-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
232 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
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
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/net/folivo/matrix/bridge/sms/handler/SmsAbortCommand.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,15 @@ | ||
package net.folivo.matrix.bridge.sms.handler | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import kotlinx.coroutines.runBlocking | ||
import net.folivo.matrix.core.model.MatrixId.RoomId | ||
|
||
class SmsAbortCommand( | ||
private val roomId: RoomId, | ||
private val handler: SmsAbortCommandHandler | ||
) : CliktCommand(name = "abort") { | ||
|
||
override fun run() { | ||
echo(runBlocking { handler.handleCommand(roomId) }) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/net/folivo/matrix/bridge/sms/handler/SmsAbortCommandHandler.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,36 @@ | ||
package net.folivo.matrix.bridge.sms.handler | ||
|
||
import net.folivo.matrix.bridge.sms.SmsBridgeProperties | ||
import net.folivo.matrix.bridge.sms.SmsBridgeProperties.SmsBridgeTemplateProperties | ||
import net.folivo.matrix.bridge.sms.message.MatrixMessageService | ||
import net.folivo.matrix.core.model.MatrixId.RoomId | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Component | ||
|
||
|
||
@Component | ||
class SmsAbortCommandHandler( | ||
private val messageService: MatrixMessageService, | ||
smsBridgeProperties: SmsBridgeProperties, | ||
) { | ||
|
||
private val templates: SmsBridgeTemplateProperties = smsBridgeProperties.templates | ||
|
||
companion object { | ||
private val LOG = LoggerFactory.getLogger(this::class.java) | ||
} | ||
|
||
suspend fun handleCommand( | ||
roomId: RoomId | ||
): String { | ||
return try { | ||
messageService.deleteByRoomId(roomId) | ||
templates.botSmsAbortSuccess | ||
} catch (ex: Throwable) { | ||
LOG.debug("got exception") | ||
templates.botSmsAbortError | ||
.replace("{error}", ex.message ?: "unknown") | ||
} | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
src/main/kotlin/net/folivo/matrix/bridge/sms/message/MatrixMessageRepository.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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package net.folivo.matrix.bridge.sms.message | ||
|
||
import net.folivo.matrix.core.model.MatrixId.RoomId | ||
import org.springframework.data.repository.kotlin.CoroutineCrudRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface MatrixMessageRepository : CoroutineCrudRepository<MatrixMessage, Long> { | ||
suspend fun deleteByRoomId(roomId: RoomId) | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/test/kotlin/net/folivo/matrix/bridge/sms/handler/SmsAbortCommandHandlerTest.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,37 @@ | ||
package net.folivo.matrix.bridge.sms.handler | ||
|
||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.* | ||
import net.folivo.matrix.bridge.sms.SmsBridgeProperties | ||
import net.folivo.matrix.bridge.sms.message.MatrixMessageService | ||
import net.folivo.matrix.core.model.MatrixId.RoomId | ||
|
||
class SmsAbortCommandHandlerTest : DescribeSpec(testBody()) | ||
|
||
private fun testBody(): DescribeSpec.() -> Unit { | ||
return { | ||
val messageServiceMock: MatrixMessageService = mockk() | ||
val smsBridgePropertiesMock: SmsBridgeProperties = mockk { | ||
every { templates.botSmsAbortSuccess }.returns("success") | ||
every { templates.botSmsAbortError }.returns("error:{error}") | ||
} | ||
val cut = SmsAbortCommandHandler(messageServiceMock, smsBridgePropertiesMock) | ||
|
||
val roomId = RoomId("room", "server") | ||
|
||
describe(SmsAbortCommandHandler::handleCommand.name) { | ||
it("should delete messages") { | ||
coEvery { messageServiceMock.deleteByRoomId(roomId) } just Runs | ||
cut.handleCommand(roomId).shouldBe("success") | ||
coVerify { messageServiceMock.deleteByRoomId(roomId) } | ||
} | ||
it("should catch error") { | ||
coEvery { messageServiceMock.deleteByRoomId(roomId) }.throws(RuntimeException("meteor")) | ||
cut.handleCommand(roomId).shouldBe("error:meteor") | ||
} | ||
} | ||
|
||
afterTest { clearMocks(messageServiceMock) } | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/kotlin/net/folivo/matrix/bridge/sms/handler/SmsAbortCommandTest.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,33 @@ | ||
package net.folivo.matrix.bridge.sms.handler | ||
|
||
import com.github.ajalt.clikt.core.context | ||
import com.github.ajalt.clikt.output.CliktConsole | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.mockk.clearMocks | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.mockk | ||
import net.folivo.matrix.core.model.MatrixId.RoomId | ||
|
||
class SmsAbortCommandTest : DescribeSpec(testBody()) | ||
|
||
private fun testBody(): DescribeSpec.() -> Unit { | ||
return { | ||
val roomId = RoomId("room", "server") | ||
val handlerMock: SmsAbortCommandHandler = mockk() | ||
val consoleMock: CliktConsole = mockk(relaxed = true) | ||
val cut = SmsAbortCommand(roomId, handlerMock) | ||
cut.context { console = consoleMock } | ||
|
||
describe("run command") { | ||
it("should run command") { | ||
coEvery { handlerMock.handleCommand(roomId) }.returns("answer") | ||
cut.parse(listOf()) | ||
coVerify { handlerMock.handleCommand(roomId) } | ||
coVerify { consoleMock.print("answer", false) } | ||
} | ||
} | ||
|
||
afterTest { clearMocks(consoleMock) } | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
src/test/kotlin/net/folivo/matrix/bridge/sms/message/MatrixMessageRepositoryTest.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,52 @@ | ||
package net.folivo.matrix.bridge.sms.message | ||
|
||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.reactive.awaitFirst | ||
import net.folivo.matrix.bot.config.MatrixBotDatabaseAutoconfiguration | ||
import net.folivo.matrix.bot.room.MatrixRoom | ||
import net.folivo.matrix.bridge.sms.SmsBridgeDatabaseConfiguration | ||
import net.folivo.matrix.core.model.MatrixId.RoomId | ||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration | ||
import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest | ||
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate | ||
import org.springframework.data.r2dbc.core.delete | ||
|
||
@DataR2dbcTest | ||
@ImportAutoConfiguration(value = [MatrixBotDatabaseAutoconfiguration::class, SmsBridgeDatabaseConfiguration::class]) | ||
class MatrixMessageRepositoryTest( | ||
cut: MatrixMessageRepository, | ||
db: R2dbcEntityTemplate | ||
) : DescribeSpec(testBody(cut, db)) | ||
|
||
private fun testBody(cut: MatrixMessageRepository, db: R2dbcEntityTemplate): DescribeSpec.() -> Unit { | ||
return { | ||
val room1 = RoomId("room1", "server") | ||
val room2 = RoomId("room2", "server") | ||
|
||
val message1 = MatrixMessage(room1, "some body 1") | ||
val message2 = MatrixMessage(room1, "some body 2") | ||
val message3 = MatrixMessage(room2, "some body 3") | ||
|
||
beforeSpec { | ||
db.insert(MatrixRoom(room1)).awaitFirst() | ||
db.insert(MatrixRoom(room2)).awaitFirst() | ||
db.insert(message1).awaitFirst() | ||
db.insert(message2).awaitFirst() | ||
db.insert(message3).awaitFirst() | ||
} | ||
|
||
describe(MatrixMessageRepository::deleteByRoomId.name) { | ||
it("should delete all matching rooms") { | ||
cut.count().shouldBe(3) | ||
cut.deleteByRoomId(room1) | ||
cut.count().shouldBe(1) | ||
} | ||
} | ||
|
||
afterSpec { | ||
db.delete<MatrixMessage>().all().awaitFirst() | ||
db.delete<MatrixRoom>().all().awaitFirst() | ||
} | ||
} | ||
} |