Skip to content

Commit

Permalink
fix: complete comments and refactoring #19
Browse files Browse the repository at this point in the history
  • Loading branch information
berka3 committed Dec 7, 2023
1 parent 0128915 commit 6b10c5f
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import kotlinx.serialization.Serializable
*
* @property apiProperties The API properties containing hostname, port, userId, and token.
* @property mqttCertificates The MQTT certificates used for secure communication (optional).
* @property requestIdGenerator The generator for request IDs.
* @property uuidGenerator The generator for request IDs.
* @property mqttConnectOptions The MQTT connect options.
* @property logoutOnClose Whether the logout is set up on close of the connection (optional).
*/
data class Config(
val apiProperties: ApiProperties,
val mqttCertificates: MqttCertificates? = null,
val requestIdGenerator: IRequestIdGenerator = DefaultRequestIdGenerator(),
val uuidGenerator: IRequestIdGenerator = DefaultRequestIdGenerator(),
val mqttConnectOptions: MqttConnectOptions = MqttConnectOptions(),
val logoutOnClose: Boolean? = null,
) {
Expand Down Expand Up @@ -118,7 +118,7 @@ data class Config(
clientCertificate =
readX509CertificateFromZip(archiveOptions.clientCertificateName, zipFile),
clientKey = readKeyPairWithZip(archiveOptions.clientKeyName, zipFile)),
requestIdGenerator = requestIdGenerator,
uuidGenerator = requestIdGenerator,
MqttConnectOptions(
isCleanSession = mqttConnectOptions.isCleanSession,
connectionTimeout = mqttConnectOptions.connectionTimeout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import java.util.UUID
class Topics(vararg val topics: String) {

companion object {
/** MQTT topic string representing the wildcard topic for subscribing to all topics. */
/**
* MQTT topic string representing the wildcard topic for subscribing to all topics.
*
* Do not subscribe to ALL_TOPICS as a client during testing if you are internally using
* on(QueryIdFilter) or on(CommandIdFilter). This can lead to the client receiving its own
* sent messages due to the lack of configuration (mosquitto.conf) in the Mosquitto test
* containers.
*/
const val ALL_TOPICS = "xs3/1/#"
}
class Event {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
requestConfig: RequestConfig = buildRequestConfig(),
): Deferred<Token> {
val token = CompletableDeferred<Token>()
val commandId = config.requestIdGenerator.generateId()
val commandId = config.uuidGenerator.generateId()

try {
withTimeout(requestConfig.timeout) {
Expand Down Expand Up @@ -436,7 +436,7 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
requestConfig: RequestConfig = buildRequestConfig()
): Deferred<QueryList.Response<T>> {
val deferred = CompletableDeferred<QueryList.Response<T>>()
val requestId = config.requestIdGenerator.generateId()
val requestId = config.uuidGenerator.generateId()
val query = Query(resource, requestId, requestConfig.token, id = null, params = params)

try {
Expand Down Expand Up @@ -653,7 +653,7 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
requestConfig: RequestConfig = buildRequestConfig()
): Deferred<T> {
val deferred = CompletableDeferred<T>()
val requestId = config.requestIdGenerator.generateId()
val requestId = config.uuidGenerator.generateId()

try {
withTimeout(requestConfig.timeout) {
Expand Down Expand Up @@ -691,12 +691,12 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
}

private suspend inline fun <reified K : Command, reified T : Event> sendCommand(
requestResource: String,
topic: String,
cmd: K,
requestConfig: RequestConfig
): Deferred<T> {
val deferred = CompletableDeferred<T>()
val commandId = config.requestIdGenerator.generateId()
val commandId = config.uuidGenerator.generateId()

try {
withTimeout(requestConfig.timeout) {
Expand All @@ -710,7 +710,7 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
deferred.completeExceptionally(HttpErrorException("${apiEvent.event.error}"))
}

client.publishAsync(requestResource, encodeCommand(cmd)).await()
client.publishAsync(topic, encodeCommand(cmd)).await()
}
} catch (e: TimeoutCancellationException) {
logger.error("Timeout while waiting for Command response", e)
Expand All @@ -724,7 +724,9 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
/**
* Remotely disengage an online component.
*
* @param installationPointId The ID of the installation point TODO extended TODO requestConfig
* @param installationPointId The ID of the installation point
* @param extended false for SHORT / true for LONG disengage
* @param requestConfig The request configuration (optional).
*/
suspend fun executeRemoteDisengage(
installationPointId: UUID,
Expand All @@ -734,14 +736,20 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
return sendCommand<RemoteDisengage, RemoteDisengagePerformed>(
Topics.Command.REMOTE_DISENGAGE,
RemoteDisengage(
config.requestIdGenerator.generateId(),
config.uuidGenerator.generateId(),
installationPointId,
extended,
requestConfig.token),
requestConfig)
}

// TODO COMMENT
/**
* Remotely disengage an online component permanently.
*
* @param installationPointId The ID of the installation point
* @param enable Enable or disable the permanent disengage
* @param requestConfig The request configuration (optional).
*/
suspend fun executeRemoteDisengagePermanent(
installationPointId: UUID,
enable: Boolean? = null,
Expand All @@ -750,7 +758,7 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
return sendCommand<RemoteDisengagePermanent, RemoteDisengagePermanentPerformed>(
Topics.Command.REMOTE_DISENGAGE_PERMANENT,
RemoteDisengagePermanent(
config.requestIdGenerator.generateId(),
config.uuidGenerator.generateId(),
installationPointId,
enable,
requestConfig.token),
Expand All @@ -772,7 +780,7 @@ class XesarConnect(private val client: IXesarMqttClient, val config: Config) : A
return sendCommand<FindComponent, FindComponentPerformed>(
Topics.Command.FIND_COMPONENT,
FindComponent(
config.requestIdGenerator.generateId(),
config.uuidGenerator.generateId(),
installationPointId,
enable,
requestConfig.token),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.open200.xesar.connect.exception

// TODO Kommentar
/** Exception thrown when an HTTP error occurs. */
class HttpErrorException : XesarApiException {

constructor() : super()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import mu.KotlinLogging
data class ApiEvent<out T : Event>(
@Serializable(with = UUIDSerializer::class) val commandId: UUID? = null,
val event: T
) // :Message ? TODO: investigate if i need this message interface here
) : Message

val jsonFormat = Json {
encodeDefaults = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ package com.open200.xesar.connect.messages.event

import kotlinx.serialization.Serializable

/**
* Represents a remote disengage event.
*
* @param ok The result of the command.
*/
@Serializable data class RemoteDisengagePerformed(val ok: String) : Event
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ package com.open200.xesar.connect.messages.event

import kotlinx.serialization.Serializable

/**
* Represents a remote disengage permanent event.
*
* @param ok The result of the command.
*/
@Serializable data class RemoteDisengagePermanentPerformed(val ok: String) : Event
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ConnectAndLoginTest :
listener(container.perProject())

test("connect and login as well as logout with testcontainers work") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("a7d184b6-dd9d-4bb8-acb7-0b51ada3e3b7"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down Expand Up @@ -50,7 +50,6 @@ class ConnectAndLoginTest :
client
.publishAsync(
Topics.Event.loggedIn(config.apiProperties.userId),
// TODO könnte encodeEvent() benützen
"{\"commandId\":\"a7d184b6-dd9d-4bb8-acb7-0b51ada3e3b7\",\"event\":{\"token\":\"JDJhJDEwJDFSNEljZ2FaRUNXUXBTQ25XN05KbE9qRzFHQ1VjMzkvWTBVcFpZb1M4Vmt0dnJYZ0tJVFBx\"}}")
.await()

Expand Down Expand Up @@ -89,7 +88,7 @@ class ConnectAndLoginTest :

test(
"connect and login with testcontainers should throw Exception when wrong username, password") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("a7d184b6-dd9d-4bb8-acb7-0b51ada3e3b7"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DelayUntilCloseTest :
val messageSend = CompletableDeferred<Boolean>()
val firstConnectionClosed = CompletableDeferred<Boolean>()

coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("00000000-1281-42c0-9a15-c5844850c748"))

withTimeout(5000) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FindComponentTest :
listener(container.perProject())

test("findComponent with HttpErrorException") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down Expand Up @@ -61,9 +61,7 @@ class FindComponentTest :
client
.publishAsync(
Topics.Event.error(config.apiProperties.userId),
encodeEvent(errorEvent)
// "{\"commandId\":\"faf3d0c4-1281-40ae-89d7-5c541d77a757\",\"event\":{\"reason\":null,\"correlationId\":\"00000000-1281-40ae-89d7-5c541d77a757\",\"error\":403}}"
)
encodeEvent(errorEvent))
.await()
}
}
Expand All @@ -81,7 +79,7 @@ class FindComponentTest :
}
}
test("find Component with success") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RemoteDisengagePermanentTest :
listener(container.perProject())

test("remote disengage permanent with HttpErrorException") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down Expand Up @@ -82,7 +82,7 @@ class RemoteDisengagePermanentTest :
}
}
test("remote disengage permanent with success") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down Expand Up @@ -111,12 +111,11 @@ class RemoteDisengagePermanentTest :
ApiEvent(
UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"),
RemoteDisengagePerformed("ok"))
val remoteDisengagePerformed = encodeEvent(apiEvent)

client
.publishAsync(
Topics.Event.REMOTE_DISENGAGE_PERMANENT_PERFORMED,
remoteDisengagePerformed)
encodeEvent(apiEvent))
.await()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RemoteDisengageTest :
listener(container.perProject())

test("remote disengage with HttpErrorException") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down Expand Up @@ -79,7 +79,7 @@ class RemoteDisengageTest :
}
}
test("remote disengage with success") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class QueryAccessProtocolEventTest :
listener(container.perProject())

test("queryAccessProtocolEventList without params") {
coEvery { config.requestIdGenerator.generateId() }
coEvery { config.uuidGenerator.generateId() }
.returns(UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"))
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class QueryAuthorizationProfileTest :

test("queryAuthorizationProfileList without params") {
val requestId = UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757")
coEvery { config.requestIdGenerator.generateId() }.returns(requestId)
coEvery { config.uuidGenerator.generateId() }.returns(requestId)
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
val queryReceived = CompletableDeferred<String>()
Expand Down Expand Up @@ -89,7 +89,7 @@ class QueryAuthorizationProfileTest :

test("queryAuthorizationProfileById") {
val requestId = UUID.fromString("00000000-1281-42c0-9a15-c5844850c748")
coEvery { config.requestIdGenerator.generateId() }.returns(requestId)
coEvery { config.uuidGenerator.generateId() }.returns(requestId)

runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class QueryCalendarTest :
test("queryCalendarList without params") {
val requestId = UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757")

coEvery { config.requestIdGenerator.generateId() }.returns(requestId)
coEvery { config.uuidGenerator.generateId() }.returns(requestId)
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
val queryReceived = CompletableDeferred<String>()
Expand Down Expand Up @@ -93,7 +93,7 @@ class QueryCalendarTest :

test("queryCalendarById") {
val requestId = UUID.fromString("00000000-1281-42c0-9a15-c5844850c748")
coEvery { config.requestIdGenerator.generateId() }.returns(requestId)
coEvery { config.uuidGenerator.generateId() }.returns(requestId)

runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QueryCodingStationTest :
test("queryCodingStationList without params") {
val requestId = UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757")

coEvery { config.requestIdGenerator.generateId() }.returns(requestId)
coEvery { config.uuidGenerator.generateId() }.returns(requestId)
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
val queryReceived = CompletableDeferred<String>()
Expand Down Expand Up @@ -90,7 +90,7 @@ class QueryCodingStationTest :

test("queryCodingStationById") {
val requestId = UUID.fromString("00000000-1281-42c0-9a15-c5844850c748")
coEvery { config.requestIdGenerator.generateId() }.returns(requestId)
coEvery { config.uuidGenerator.generateId() }.returns(requestId)

runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class QueryEvvaComponentTest :

test("queryEvvaComponentList with params") {
val requestId = UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757")
coEvery { config.requestIdGenerator.generateId() }.returns(requestId)
coEvery { config.uuidGenerator.generateId() }.returns(requestId)
runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
val queryReceived = CompletableDeferred<String>()
Expand Down Expand Up @@ -89,7 +89,7 @@ class QueryEvvaComponentTest :
test("queryEvvaComponentById") {
val requestId = UUID.fromString("00000000-1281-42c0-9a15-c5844850c748")

coEvery { config.requestIdGenerator.generateId() }.returns(requestId)
coEvery { config.uuidGenerator.generateId() }.returns(requestId)

runBlocking {
val simulatedBackendReady = CompletableDeferred<Unit>()
Expand Down
Loading

0 comments on commit 6b10c5f

Please sign in to comment.