Skip to content

Commit

Permalink
Merge pull request #16 from MDalfre/develop
Browse files Browse the repository at this point in the history
Mock import file:
  • Loading branch information
MDalfre committed Sep 23, 2022
2 parents 57d1766 + 4bbea16 commit 0c2599f
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 52 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ Simple generic Amazon SQS client and mock, designed to make asynchronous applica

SR-SQS is able to act as a generic consumer, producer and mock responses, to ensure your application workflow is running well.

## Json mock example
```json
{
"mockList": [
{
"sourceQueue": "some-test-queue",
"targetQueue": "another-test-queue",
"messageToWait": "*",
"mockResponse": "{\"status\":\"SUCCESS\"}"
},
{
"sourceQueue": "monday-test-queue",
"targetQueue": "friday-test-queue",
"messageToWait": "*",
"mockResponse": "{\"status\":\"SUCCESS\"}"
}
]
}
```

# ScreenShot

![alt text](https://github.com/raw/MDalfre/sr-sqs/main/.github/images/prtsc1.jpeg)
Expand Down
3 changes: 3 additions & 0 deletions aws/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
set -x

awslocal sqs create-queue --queue-name some-test-queue
awslocal sqs create-queue --queue-name another-test-queue
awslocal sqs create-queue --queue-name monday-test-queue
awslocal sqs create-queue --queue-name friday-test-queue

set +x
17 changes: 1 addition & 16 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
id("io.gitlab.arturbosch.detekt").version("1.18.0-RC3")
}

var lastAppVersion = "2.1.2"
var lastAppVersion = "2.1.3"

group = "ma.dalfre"
version = lastAppVersion
Expand Down Expand Up @@ -45,33 +45,18 @@ compose.desktop {
console = false
menuGroup = "Sr Sqs"
modules(
"java.instrument",
"java.management",
"java.naming",
"java.security.jgss",
"java.sql",
"jdk.unsupported"
)
iconFile.set(project.file("src/main/resources/icon.ico"))
}
linux {
modules(
"java.instrument",
"java.management",
"java.naming",
"java.security.jgss",
"java.sql",
"jdk.unsupported"
)
iconFile.set(project.file("src/main/resources/sr-sqs-icon.png"))
}
macOS {
modules(
"java.instrument",
"java.management",
"java.naming",
"java.security.jgss",
"java.sql",
"jdk.unsupported"
)
iconFile.set(project.file("src/main/resources/sr-sqs-icon.png"))
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/commons/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ object Constants {
const val DEFAULT_WAIT_INTERVAL = 100L
const val DEFAULT_WAIT_WILDCARD = "*"
const val DEFAULT_MOCK_INTERVAL = 200L
const val DEFAULT_MAX_CHARACTER = 150
}
9 changes: 7 additions & 2 deletions src/main/kotlin/commons/JsonTools.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package commons

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

fun Any.objectToJson(): String? {
val mapper = ObjectMapper()
val mapper = jacksonObjectMapper()
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this)
}

inline fun <reified T> String.jsonToObject(): T {
val mapper = jacksonObjectMapper()
return mapper.readValue(this, T::class.java)
}
61 changes: 36 additions & 25 deletions src/main/kotlin/components/MainView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
Expand All @@ -40,7 +40,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.DpSize
Expand All @@ -50,6 +49,7 @@ import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.rememberDialogState
import com.amazonaws.regions.Regions
import com.amazonaws.services.sqs.model.Message
import commons.Constants.DEFAULT_MAX_CHARACTER
import commons.DefaultColors.backgroundColor
import commons.DefaultColors.buttonColor
import commons.DefaultColors.secondaryColor
Expand All @@ -58,7 +58,6 @@ import commons.objectToJson
import connectionService
import model.ConnectionSettings
import model.CredentialType
import model.Log
import model.LogTypeEnum
import model.Queue
import model.SqsMock
Expand All @@ -67,7 +66,6 @@ import service.ConnectionService
import service.FileHandleService
import service.GenericSqsService
import service.MockSqsService
import java.io.FilenameFilter

const val ALERT_WIDTH = 600
const val ALERT_HEIGHT = 450
Expand Down Expand Up @@ -125,7 +123,6 @@ fun mainView(
var message by remember { mutableStateOf("") }
var sourceMessage by remember { mutableStateOf("*") }
var targetMessage by remember { mutableStateOf("") }
var systemLog by mutableStateOf(listOf<Log>())

/** End of States **/

Expand Down Expand Up @@ -286,7 +283,7 @@ fun mainView(
.height(30.dp),
state = listStateLog,
) {
items(systemLog) { logMessage ->
items(communicationService.systemLog.asReversed()) { logMessage ->
val color = when (logMessage.type) {
LogTypeEnum.INFO -> Color.Gray
LogTypeEnum.WARN -> Color.Yellow
Expand Down Expand Up @@ -326,7 +323,7 @@ fun mainView(
defaultTextField(
modifier = Modifier.clickable {
if (connectionService != null) {
queues = GenericSqsService(connectionService!!, communicationService).getQueues()
queues = GenericSqsService(connectionService!!, communicationService).getQueues(log = false)
}
expandedToSend = !expandedToSend
},
Expand Down Expand Up @@ -384,15 +381,7 @@ fun mainView(
enabled = connecting && (selectedQueueToSend != " "),
colors = defaultButtonColor,
onClick = {
communicationService.logInfo("Importing file ...")
val filePicker = java.awt.FileDialog(ComposeWindow())
filePicker.filenameFilter = FilenameFilter { _, name -> name.endsWith(".srsqs") }
filePicker.isVisible = true
val file = "${filePicker.directory}${filePicker.file}"
if (filePicker.file != null) {
message = FileHandleService().importFile(file)
communicationService.logSuccess("Imported: ${filePicker.file}")
}
message = FileHandleService().importMessage()
}
) {
Text("Import")
Expand All @@ -407,7 +396,7 @@ fun mainView(
defaultTextField(
modifier = Modifier.clickable {
if (connectionService != null) {
queues = GenericSqsService(connectionService!!, communicationService).getQueues()
queues = GenericSqsService(connectionService!!, communicationService).getQueues(log = false)
}
expandedToReceive = !expandedToReceive
},
Expand Down Expand Up @@ -583,8 +572,13 @@ fun mainView(
Text("Awaiting on: ${it.sourceQueue.substringAfterLast("/")}")
Text("Respond on: ${it.targetQueue.substringAfterLast("/")}")
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
Text("Awaiting for: ${it.messageToWait}", style = MaterialTheme.typography.body2)
Text("Response: ${it.mockResponse}", style = MaterialTheme.typography.body2)
Text(
"Awaiting for: ${it.messageToWait.take(DEFAULT_MAX_CHARACTER)}",
style = MaterialTheme.typography.body2
)
Text(
"Response: ${it.mockResponse.take(DEFAULT_MAX_CHARACTER)}",
style = MaterialTheme.typography.body2)
Row(
modifier = Modifier.align(Alignment.End)
) {
Expand All @@ -610,7 +604,8 @@ fun mainView(
defaultTextField(
modifier = Modifier.clickable {
if (connectionService != null) {
queues = GenericSqsService(connectionService!!, communicationService).getQueues()
queues = GenericSqsService(connectionService!!, communicationService)
.getQueues(log = false)
}
expandedMockSource = !expandedMockSource
}.width(450.dp),
Expand Down Expand Up @@ -644,7 +639,8 @@ fun mainView(
defaultTextField(
modifier = Modifier.clickable {
if (connectionService != null) {
queues = GenericSqsService(connectionService!!, communicationService).getQueues()
queues = GenericSqsService(connectionService!!, communicationService)
.getQueues(log = false)
}
expandedMockTarget = !expandedMockTarget
}.width(450.dp),
Expand Down Expand Up @@ -720,6 +716,10 @@ fun mainView(
mockResponse = targetMessage.trim()
)
)
sourceMessage = "*"
targetMessage = ""
selectedMockQueueTarget = " "
selectedMockQueueSource = " "
}
) {
Text("Create Mock")
Expand All @@ -746,12 +746,23 @@ fun mainView(
) {
Text("Stop Mock")
}
Button(
modifier = buttonModifier,
enabled = connecting,
colors = defaultButtonColor,
onClick = {
val currentQueues = GenericSqsService(
connectionService = requireNotNull(connectionService) { "SQS service not connected" },
communicationService = communicationService
).getQueues(log = false)
val mockFile = FileHandleService().importSqsMock(currentQueues)
mockList = mockList.plus(mockFile)
}
) {
Text("Import")
}
}
}

}

if (systemLog.size != communicationService.systemLog.size) {
systemLog = communicationService.systemLog.map { it }.reversed()
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/components/TopBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fun topBar(communicationService: CommunicationService) {
)
}
Text(
text = "v2.1.2",
text = "v2.1.3",
modifier = Modifier.padding(top = 7.dp),
style = TextStyle(fontSize = 13.sp),
color = DefaultColors.tintColor
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/model/SqsMockList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package model

data class SqsMockList(
val mockList: List<SqsMock>
)
13 changes: 8 additions & 5 deletions src/main/kotlin/service/CommunicationService.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package service

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import model.Log
import model.LogTypeEnum
import model.ProcessStatusEnum

class CommunicationService {
val systemLog = mutableListOf<Log>()
var systemLog by mutableStateOf(listOf<Log>())
var messageCounter = 1
var reprocessingDql = ProcessStatusEnum.NOT_STARTED
var mockService = ProcessStatusEnum.NOT_STARTED

fun logInfo(message: String) {
systemLog.add(
systemLog = systemLog.plus(
Log(LogTypeEnum.INFO, message)
)
}

fun logWarn(message: String) {
systemLog.add(
systemLog = systemLog.plus(
Log(LogTypeEnum.WARN, message)
)
}

fun logError(message: String?) {
systemLog.add(
systemLog = systemLog.plus(
Log(LogTypeEnum.ERROR, message ?: "Internal Error")
)
}

fun logSuccess(message: String) {
systemLog.add(
systemLog = systemLog.plus(
Log(LogTypeEnum.SUCCESS, message)
)
}
Expand Down
49 changes: 49 additions & 0 deletions src/main/kotlin/service/FileHandleService.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package service

import androidx.compose.ui.awt.ComposeWindow
import com.amazonaws.regions.Regions
import com.amazonaws.services.sqs.model.Message
import commons.jsonToObject
import model.ConnectionSettings
import model.CredentialType
import model.Queue
import model.SqsMock
import model.SqsMockList
import java.io.File
import java.io.FileInputStream
import java.io.FilenameFilter
import java.time.LocalDateTime
import java.util.Properties

Expand Down Expand Up @@ -111,4 +116,48 @@ class FileHandleService {
fun importFile(file: String): String {
return FileInputStream(file).reader(Charsets.UTF_8).readText()
}

fun importSqsMock(currentQueues: List<Queue>): List<SqsMock> {
val filePicker = java.awt.FileDialog(ComposeWindow())
filePicker.filenameFilter = FilenameFilter { _, name -> name.endsWith(".json") }
filePicker.isVisible = true
val file = "${filePicker.directory}${filePicker.file}"
if (filePicker.file != null) {
val mockList = mutableListOf<SqsMock>()
importMock(file).mockList.forEach { sqsMock ->
val sourceUrl = currentQueues.find { it.name == sqsMock.sourceQueue }?.url ?: "QueueNotFound"
val targetUrl = currentQueues.find { it.name == sqsMock.targetQueue }?.url ?: "QueueNotFound"
mockList.add(
SqsMock(
sourceQueue = sourceUrl,
targetQueue = targetUrl,
messageToWait = sqsMock.messageToWait,
mockResponse = sqsMock.mockResponse
)
)
}
return mockList

} else {
return emptyList()
}
}

fun importMock(mockFile: String): SqsMockList {
val json = File(mockFile).readText(Charsets.UTF_8)
return json.jsonToObject()

}

fun importMessage(): String {
val filePicker = java.awt.FileDialog(ComposeWindow())
filePicker.filenameFilter = FilenameFilter { _, name -> name.endsWith(".srsqs") }
filePicker.isVisible = true
val file = "${filePicker.directory}${filePicker.file}"
return if (filePicker.file != null) {
FileHandleService().importFile(file)
} else {
""
}
}
}
Loading

0 comments on commit 0c2599f

Please sign in to comment.