Skip to content

Commit

Permalink
refactor: api mocks [WPB-9999] (#3053)
Browse files Browse the repository at this point in the history
* refactor: api mocks

* removed unnecessary conversation creation in test

* fix: hide conversation name in logs

* added documentation to logger

* test fixes
  • Loading branch information
Garzas authored Oct 8, 2024
1 parent 75b92ab commit 8be3015
Show file tree
Hide file tree
Showing 34 changed files with 794 additions and 549 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ internal class ConfirmationDeliveryHandlerImpl(
}, {
kaliumLogger.logStructuredJson(
level = KaliumLogLevel.DEBUG,
leadingMessage = "Delivery confirmation sent for ${conversation.name} and message count: ${messages.size}",
leadingMessage = "Delivery confirmation sent for ${conversation.id.toLogString()} and message count: ${messages.size}",
jsonStringKeyValues = mapOf(
"conversationId" to conversation.id.toLogString(),
"messages" to messages.joinToString { it.obfuscateId() },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.util

import kotlinx.datetime.Clock
import kotlinx.datetime.Instant

/**
* The `TimeLogger` class is designed to measure the duration of a process for benchmarking purposes.
* It takes a `processName` as a parameter to identify the process being timed.
*
* Usage:
* - Call `start()` to begin tracking the time for a process.
* - Call `finish()` to log the time taken since `start()` was invoked.
*
* @property processName The name of the process being measured.
*
* Methods:
* - `start()`: Logs the process start and records the current time.
* - `finish()`: Logs the process end, calculates the elapsed time, and prints the duration in milliseconds.
*
* This class is useful for performance analysis and optimization by providing a simple way to track
* execution times for different parts of code.
*/
class TimeLogger(private val processName: String) {

private lateinit var startTime: Instant
fun start() {
println("$processName starting")
startTime = Clock.System.now()

}

fun finish() {
val endTime = Clock.System.now()
val duration = endTime - startTime
println("$processName finished after: ${duration.inWholeMilliseconds} milliseconds")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,20 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.mocks.mocks.asset

package com.wire.kalium.mocks.responses.asset

import com.wire.kalium.mocks.responses.ValidJsonProvider
import com.wire.kalium.network.api.authenticated.asset.AssetResponse
import com.wire.kalium.network.api.model.ErrorResponse

object AssetDownloadResponseJson {
private val invalidJsonProvider = { serializable: ErrorResponse ->
"""
|{
| "code": "${serializable.code}",
| "message": "${serializable.message}",
| "label": "${serializable.label}"
|}
""".trimMargin()
}
object AssetMocks {

val invalid = ErrorResponse(code = 401, message = "Invalid Asset Token", label = "invalid_asset_token")

val invalid = ValidJsonProvider(
ErrorResponse(code = 401, message = "Invalid Asset Token", label = "invalid_asset_token"),
invalidJsonProvider
val asset = AssetResponse(
key = "3-1-e7788668-1b22-488a-b63c-acede42f771f",
expires = "expiration_date",
token = "asset_token",
domain = "staging.wire.link"
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.mocks.mocks.client

import com.wire.kalium.network.api.authenticated.client.Capabilities
import com.wire.kalium.network.api.authenticated.client.ClientCapabilityDTO
import com.wire.kalium.network.api.authenticated.client.ClientDTO
import com.wire.kalium.network.api.authenticated.client.ClientTypeDTO
import com.wire.kalium.network.api.authenticated.client.DeviceTypeDTO

object ClientMocks {

const val selfClientId = "defkrr8e7grgsoufhg8"

val selfClient = ClientDTO(
clientId = selfClientId,
type = ClientTypeDTO.Permanent,
deviceType = DeviceTypeDTO.Phone,
registrationTime = "2021-05-12T10:52:02.671Z",
lastActive = "2021-05-12T10:52:02.671Z",
label = "label",
cookie = "sldkfmdeklmwldwlek23kl44mntiuepfojfndkjd",
capabilities = Capabilities(listOf(ClientCapabilityDTO.LegalHoldImplicitConsent)),
model = "model",
mlsPublicKeys = null
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,17 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.mocks.mocks.client

package com.wire.kalium.mocks.responses.connection
import com.wire.kalium.network.api.model.AccessTokenDTO

import com.wire.kalium.mocks.responses.ValidJsonProvider
object TokenMocks {

object ConnectionRequestsJson {

val validEmptyBody = ValidJsonProvider(String) {
"""
{
"size":500
}
""".trimIndent()
}

val validPagingState = ValidJsonProvider("PAGING_STATE_1234") {
"""
{
"paging_state": "$it",
"size":500
}
""".trimIndent()
}

val validConnectionStatusUpdate = ValidJsonProvider("accepted") {
"""
{
"status": "$it"
}
""".trimIndent()
}
val accessToken = AccessTokenDTO(
userId = "user_id", // TODO check structure of userId to connect with User Mocks
value = "Nlrhltkj-NgJUjEVevHz8Ilgy_pyWCT2b0kQb-GlnamyswanghN9DcC3an5RUuA7sh1_nC3hv2ZzMRlIhPM7Ag==.v=1.k=1.d=1637254939." +
"t=a.l=.u=75ebeb16-a860-4be4-84a7-157654b492cf.c=18401233206926541098",
expiresIn = 900,
tokenType = "Bearer"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.mocks.mocks.connection

import com.wire.kalium.network.api.authenticated.connection.ConnectionDTO
import com.wire.kalium.network.api.authenticated.connection.ConnectionResponse
import com.wire.kalium.network.api.authenticated.connection.ConnectionStateDTO
import com.wire.kalium.network.api.authenticated.connection.UpdateConnectionRequest
import com.wire.kalium.network.api.model.ConversationId
import com.wire.kalium.network.api.model.PaginationRequest
import com.wire.kalium.network.api.model.QualifiedID
import kotlinx.datetime.Instant

object ConnectionMocks {
val connectionList = listOf(
ConnectionDTO(
conversationId = "addb6fbf-2bc3-4b59-b428-6fa4c594fb05",
from = "36ef84a9-837a-4f75-af81-5a2e70e06836",
lastUpdate = Instant.parse("2022-04-04T16:11:28.388Z"),
qualifiedConversationId = ConversationId(
domain = "staging.zinfra.io",
value = "addb6fbf-2bc3-4b59-b428-6fa4c594fb05"
),
qualifiedToId = QualifiedID(
domain = "staging.zinfra.io",
value = "76ebeb16-a849-4be4-84a7-157654b492cf"
),
status = ConnectionStateDTO.ACCEPTED,
toId = "76ebeb16-a849-4be4-84a7-157654b492cf"
),
ConnectionDTO(
conversationId = "af6d3c9a-7934-4790-9ebf-f655e13acc76",
from = "36ef84a9-837a-4f75-af81-5a2e70e06836",
lastUpdate = Instant.parse("2022-03-23T16:53:32.515Z"),
qualifiedConversationId = ConversationId(
domain = "staging.zinfra.io",
value = "af6d3c9a-7934-4790-9ebf-f655e13acc76"
),
qualifiedToId = QualifiedID(
domain = "staging.zinfra.io",
value = "787db7f1-f5ba-481b-af3e-9c27705a6440"
),
status = ConnectionStateDTO.ACCEPTED,
toId = "787db7f1-f5ba-481b-af3e-9c27705a6440"
),
ConnectionDTO(
conversationId = "f15a944a-b62b-4d9a-aff4-014d78a02294",
from = "36ef84a9-837a-4f75-af81-5a2e70e06836",
lastUpdate = Instant.parse("2022-03-25T17:20:13.637Z"),
qualifiedConversationId = ConversationId(
domain = "staging.zinfra.io",
value = "f15a944a-b62b-4d9a-aff4-014d78a02294"
),
qualifiedToId = QualifiedID(
domain = "staging.zinfra.io",
value = "ba6b0fa1-32b1-4e25-8072-a71f07bfba5e"
),
status = ConnectionStateDTO.ACCEPTED,
toId = "ba6b0fa1-32b1-4e25-8072-a71f07bfba5e"
)
)

val connectionsResponse = ConnectionResponse(
connections = connectionList,
hasMore = false,
pagingState = "AQ=="
)

val connection = ConnectionDTO(
conversationId = "addb6fbf-2bc3-4b59-b428-6fa4c594fb05",
from = "36ef84a9-837a-4f75-af81-5a2e70e06836",
lastUpdate = Instant.parse("2022-04-04T16:11:28.388Z"),
qualifiedConversationId = ConversationId(
domain = "staging.zinfra.io",
value = "addb6fbf-2bc3-4b59-b428-6fa4c594fb05"
),
qualifiedToId = QualifiedID(
domain = "staging.zinfra.io",
value = "76ebeb16-a849-4be4-84a7-157654b492cf"
),
status = ConnectionStateDTO.ACCEPTED,
toId = "76ebeb16-a849-4be4-84a7-157654b492cf"
)

val emptyPaginationRequest = PaginationRequest(
size = 500,
pagingState = null
)

val paginationRequest = PaginationRequest(
size = 500,
pagingState = "PAGING_STATE_1234"
)

val acceptedConnectionRequest = UpdateConnectionRequest(ConnectionStateDTO.ACCEPTED)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.mocks.mocks.conversation

import com.wire.kalium.mocks.mocks.domain.DomainMocks
import com.wire.kalium.network.api.authenticated.conversation.ConvProtocol
import com.wire.kalium.network.api.authenticated.conversation.ConversationMemberDTO
import com.wire.kalium.network.api.authenticated.conversation.ConversationMembersResponse
import com.wire.kalium.network.api.authenticated.conversation.ConversationPagingResponse
import com.wire.kalium.network.api.authenticated.conversation.ConversationResponse
import com.wire.kalium.network.api.authenticated.conversation.ConversationsDetailsRequest
import com.wire.kalium.network.api.authenticated.conversation.ReceiptMode
import com.wire.kalium.network.api.model.ConversationAccessDTO
import com.wire.kalium.network.api.model.ConversationAccessRoleDTO
import com.wire.kalium.network.api.model.ConversationId
import com.wire.kalium.network.api.model.UserId

object ConversationMocks {

val conversationId = ConversationId("conversation_id", DomainMocks.domain)

val conversation = ConversationResponse(
"creator",
ConversationMembersResponse(
ConversationMemberDTO.Self(UserId("someValue", "someDomain"), "wire_member"),
emptyList()
),
"group name",
conversationId,
null,
0UL,
ConversationResponse.Type.GROUP,
0,
null,
ConvProtocol.PROTEUS,
lastEventTime = "2024-03-30T15:36:00.000Z",
access = setOf(ConversationAccessDTO.INVITE, ConversationAccessDTO.CODE),
accessRole = setOf(
ConversationAccessRoleDTO.GUEST,
ConversationAccessRoleDTO.TEAM_MEMBER,
ConversationAccessRoleDTO.NON_TEAM_MEMBER
),
mlsCipherSuiteTag = null,
receiptMode = ReceiptMode.DISABLED
)

val conversationListIdsResponse = ConversationPagingResponse(
conversationsIds = listOf(
conversationId,
ConversationId("f4680835-2cfe-4d4d-8491-cbb201bd5c2b", "anta.wire.link")
),
hasMore = false,
pagingState = "AQ=="
)

val conversationsDetailsRequest = ConversationsDetailsRequest(
conversationsIds = listOf(
conversationId,
ConversationId("f4680835-2cfe-4d4d-8491-cbb201bd5c2b", "anta.wire.link")
)
)
}
Loading

0 comments on commit 8be3015

Please sign in to comment.