Skip to content

Commit d40d71d

Browse files
author
paulwinter
committed
feat: add ChangeAuthorizationProfileMetadataValue command and test (#165)
1 parent 425ae19 commit d40d71d

9 files changed

Lines changed: 180 additions & 3 deletions

File tree

xesar-connect/src/main/kotlin/com/open200/xesar/connect/Topics.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,10 @@ class Topics(vararg val topics: String) {
480480

481481
/** MQTT topic string for the "ChangePersonMetadataValueMapi" command. */
482482
val CHANGE_PERSON_METADATA_VALUE = "xs3/1/cmd/ChangePersonMetadataValueMapi"
483+
484+
/** MQTT topic string for the "ChangeAuthorizationProfileMetadataValueMapi" command. */
485+
val CHANGE_AUTHORIZATION_PROFILE_METADATA_VALUE =
486+
"xs3/1/cmd/ChangeAuthorizationProfileMetadataValueMapi"
483487
}
484488
}
485489

xesar-connect/src/main/kotlin/com/open200/xesar/connect/extension/XesarConnectAuthorizationProfileExt.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,38 @@ suspend fun XesarConnect.changeAuthorizationProfileAsync(
162162
)
163163
}
164164

165+
/**
166+
* Changes the value of custom data field of an authorization profile asynchronously.
167+
*
168+
* @param id The ID of the authorization profile
169+
* @param metadataId The metadataID of the data field.
170+
* @param value The new value of the field.
171+
* @param requestConfig The request configuration (optional).
172+
*/
173+
suspend fun XesarConnect.changeAuthorizationProfileMetadataValueAsync(
174+
id: UUID,
175+
metadataId: UUID,
176+
value: String,
177+
requestConfig: XesarConnect.RequestConfig = buildRequestConfig(),
178+
): SingleEventResult<AuthorizationProfileInfoChanged> {
179+
return sendCommandAsync<
180+
ChangeAuthorizationProfileMetadataValueMapi,
181+
AuthorizationProfileInfoChanged,
182+
>(
183+
Topics.Command.CHANGE_AUTHORIZATION_PROFILE_METADATA_VALUE,
184+
Topics.Event.AUTHORIZATION_PROFILE_INFO_CHANGED,
185+
true,
186+
ChangeAuthorizationProfileMetadataValueMapi(
187+
config.uuidGenerator.generateId(),
188+
id,
189+
metadataId,
190+
value,
191+
token,
192+
),
193+
requestConfig,
194+
)
195+
}
196+
165197
/**
166198
* Retrieves a cold stream of [AuthorizationProfile] objects, fetching them incrementally in
167199
* smaller,more manageable chunks rather than retrieving the entire dataset at once. Use
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.open200.xesar.connect.messages.command
2+
3+
import com.open200.xesar.connect.utils.UUIDSerializer
4+
import java.util.*
5+
import kotlinx.serialization.Serializable
6+
7+
/**
8+
* Represents a command POJO to change a custom data field value for an authorization profile.
9+
*
10+
* @param commandId The id of the command.
11+
* @param id The id of the authorization profile.
12+
* @param metadataId The id of the custom data field.
13+
* @param value The new value of the custom data field.
14+
* @param token The token of the command.
15+
*/
16+
@Serializable
17+
data class ChangeAuthorizationProfileMetadataValueMapi(
18+
override val commandId: @Serializable(with = UUIDSerializer::class) UUID,
19+
val id: @Serializable(with = UUIDSerializer::class) UUID? = null,
20+
val metadataId: @Serializable(with = UUIDSerializer::class) UUID,
21+
val value: String,
22+
val token: String,
23+
) : Command

xesar-connect/src/main/kotlin/com/open200/xesar/connect/messages/event/AuthorizationProfileInfoChanged.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.open200.xesar.connect.messages.event
22

3+
import com.open200.xesar.connect.messages.EntityMetadata
34
import com.open200.xesar.connect.utils.UUIDSerializer
45
import java.util.*
56
import kotlinx.serialization.Serializable
@@ -11,10 +12,13 @@ import kotlinx.serialization.Serializable
1112
* @param name The name of the authorization profile.
1213
* @param description The description of the authorization profile.
1314
* @param id The id of the authorization profile.
15+
* @param entityMetadata Contains the information for all defined custom data fields for the
16+
* authorization profile.
1417
*/
1518
@Serializable
1619
data class AuthorizationProfileInfoChanged(
1720
val name: String? = null,
1821
val description: String? = null,
1922
val id: @Serializable(with = UUIDSerializer::class) UUID? = null,
23+
val entityMetadata: List<EntityMetadata>? = null,
2024
) : Event

xesar-connect/src/main/kotlin/com/open200/xesar/connect/messages/query/AuthorizationProfile.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.open200.xesar.connect.messages.query
22

3+
import com.open200.xesar.connect.messages.EntityMetadata
34
import com.open200.xesar.connect.utils.UUIDSerializer
45
import java.util.*
56
import kotlinx.serialization.Serializable
@@ -17,6 +18,8 @@ import kotlinx.serialization.Serializable
1718
* @param anyAuthorizations Indicates if the authorization profile allows any authorizations.
1819
* @param standardTimeProfile The used standard time profile id or null for an all-day time profile.
1920
* (optional).
21+
* @param entityMetadata Contains the information for all defined custom data fields for the
22+
* authorization profile.
2023
*/
2124
@Serializable
2225
data class AuthorizationProfile(
@@ -28,6 +31,7 @@ data class AuthorizationProfile(
2831
val manualOfficeMode: Boolean,
2932
val anyAuthorizations: Boolean,
3033
@Serializable(with = UUIDSerializer::class) val standardTimeProfile: UUID? = null,
34+
val entityMetadata: List<EntityMetadata>? = null,
3135
) : QueryListResource, QueryElementResource {
3236

3337
/**

xesar-connect/src/test/kotlin/com/open200/xesar/connect/encodingDecoding/query/AuthorizationProfileElementTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class AuthorizationProfileElementTest :
3232
"\"timeProfileId\":\"532534ef-d5aa-4cca-acfb-e558c623b00a\"}]," +
3333
"\"manualOfficeMode\":true," +
3434
"\"anyAuthorizations\":true," +
35-
"\"standardTimeProfile\":\"a58e45f8-7bff-4b3a-bd0e-a831b3fa8053\"}}"
35+
"\"standardTimeProfile\":\"a58e45f8-7bff-4b3a-bd0e-a831b3fa8053\"," +
36+
"\"entityMetadata\":[]}}"
3637

3738
test("encoding QueryListElement for an authorization profile") {
3839
val authorizationProfileEncoded = encodeQueryElement(authorizationProfileTest)

xesar-connect/src/test/kotlin/com/open200/xesar/connect/encodingDecoding/query/AuthorizationProfileListTest.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ class AuthorizationProfileListTest :
4545
"\"timeProfileId\":\"532534ef-d5aa-4cca-acfb-e558c623b00a\"}]," +
4646
"\"manualOfficeMode\":true," +
4747
"\"anyAuthorizations\":true," +
48-
"\"standardTimeProfile\":\"a58e45f8-7bff-4b3a-bd0e-a831b3fa8053\"}," +
48+
"\"standardTimeProfile\":\"a58e45f8-7bff-4b3a-bd0e-a831b3fa8053\"," +
49+
"\"entityMetadata\":[]}," +
4950
"{\"id\":\"555e7d1a-54f1-432a-ade7-80d20a63ee2d\"," +
5051
"\"name\":\"authorization profile 2 String\"," +
5152
"\"description\":\"description profile 2 String\"," +
5253
"\"installationPoints\":[]," +
5354
"\"zones\":[]," +
5455
"\"manualOfficeMode\":true," +
5556
"\"anyAuthorizations\":true," +
56-
"\"standardTimeProfile\":\"a58e45f8-7bff-4b3a-bd0e-a831b3fa8053\"}]," +
57+
"\"standardTimeProfile\":\"a58e45f8-7bff-4b3a-bd0e-a831b3fa8053\"," +
58+
"\"entityMetadata\":[]}]," +
5759
"\"totalCount\":2," +
5860
"\"filterCount\":2}}"
5961

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.open200.xesar.connect.it.command
2+
3+
import com.open200.xesar.connect.Topics
4+
import com.open200.xesar.connect.XesarConnect
5+
import com.open200.xesar.connect.XesarMqttClient
6+
import com.open200.xesar.connect.extension.changeAuthorizationProfileMetadataValueAsync
7+
import com.open200.xesar.connect.it.MosquittoContainer
8+
import com.open200.xesar.connect.messages.EntityMetadata
9+
import com.open200.xesar.connect.messages.event.ApiEvent
10+
import com.open200.xesar.connect.messages.event.AuthorizationProfileInfoChanged
11+
import com.open200.xesar.connect.messages.event.encodeEvent
12+
import io.kotest.common.runBlocking
13+
import io.kotest.core.spec.style.FunSpec
14+
import io.kotest.extensions.testcontainers.perProject
15+
import io.kotest.matchers.equals.shouldBeEqual
16+
import io.mockk.coEvery
17+
import java.util.*
18+
import kotlinx.coroutines.CompletableDeferred
19+
import kotlinx.coroutines.launch
20+
21+
class ChangeAuthorizationProfileMetadataValueTest :
22+
FunSpec({
23+
val container = MosquittoContainer.container()
24+
val config = MosquittoContainer.config(container)
25+
listener(container.perProject())
26+
27+
test("change authorization profile metadata value") {
28+
coEvery { config.uuidGenerator.generateId() }
29+
.returns(UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"))
30+
31+
val authorizationProfileId = UUID.fromString("11111111-2222-3333-4444-555555555555")
32+
val metadataId = UUID.fromString("aaaaaaaa-0000-0000-0000-000000000001")
33+
34+
runBlocking {
35+
val simulatedBackendReady = CompletableDeferred<Unit>()
36+
val commandReceived = CompletableDeferred<String>()
37+
38+
launch {
39+
XesarMqttClient.connectAsync(config).await().use { client ->
40+
client.subscribeAsync(arrayOf(Topics.ALL_TOPICS)).await()
41+
42+
client.onMessage = { topic, payload ->
43+
when (topic) {
44+
Topics.Command.CHANGE_AUTHORIZATION_PROFILE_METADATA_VALUE -> {
45+
commandReceived.complete(payload.decodeToString())
46+
}
47+
}
48+
}
49+
50+
simulatedBackendReady.complete(Unit)
51+
52+
val commandContent = commandReceived.await()
53+
54+
commandContent.shouldBeEqual(
55+
"{\"commandId\":\"00000000-1281-40ae-89d7-5c541d77a757\",\"id\":\"11111111-2222-3333-4444-555555555555\",\"metadataId\":\"aaaaaaaa-0000-0000-0000-000000000001\",\"value\":\"Test Value\",\"token\":\"JDJhJDEwJDFSNEljZ2FaRUNXUXBTQ25XN05KbE9qRzFHQ1VjMzkvWTBVcFpZb1M4Vmt0dnJYZ0tJVFBx\"}"
56+
)
57+
58+
val apiEvent =
59+
ApiEvent(
60+
UUID.fromString("00000000-1281-40ae-89d7-5c541d77a757"),
61+
AuthorizationProfileInfoChanged(
62+
id = authorizationProfileId,
63+
entityMetadata =
64+
listOf(
65+
EntityMetadata(
66+
id = metadataId,
67+
name = "test",
68+
value = "Test Value",
69+
)
70+
),
71+
),
72+
)
73+
74+
client
75+
.publishAsync(
76+
Topics.Event.AUTHORIZATION_PROFILE_INFO_CHANGED,
77+
encodeEvent(apiEvent),
78+
)
79+
.await()
80+
}
81+
}
82+
83+
launch {
84+
simulatedBackendReady.await()
85+
86+
val api = XesarConnect.connectAndLoginAsync(config).await()
87+
api.subscribeAsync(Topics(Topics.Event.AUTHORIZATION_PROFILE_INFO_CHANGED))
88+
.await()
89+
90+
val result =
91+
api.changeAuthorizationProfileMetadataValueAsync(
92+
id = authorizationProfileId,
93+
metadataId = metadataId,
94+
value = "Test Value",
95+
)
96+
.await()
97+
98+
result.id?.shouldBeEqual(authorizationProfileId)
99+
result.entityMetadata!!
100+
.single { it.id == metadataId }
101+
.value
102+
?.shouldBeEqual("Test Value")
103+
}
104+
}
105+
}
106+
})

xesar-connect/src/test/kotlin/com/open200/xesar/connect/util/fixture/AuthorizationProfileFixture.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ object AuthorizationProfileFixture {
2727
manualOfficeMode = true,
2828
anyAuthorizations = true,
2929
standardTimeProfile = UUID.fromString("a58e45f8-7bff-4b3a-bd0e-a831b3fa8053"),
30+
entityMetadata = emptyList(),
3031
)
3132
}

0 commit comments

Comments
 (0)