Update error handling and add extra tests

This commit is contained in:
Korna 2022-11-07 20:01:03 +03:00
parent fb7fd2f218
commit cc05a93f09
5 changed files with 99 additions and 27 deletions

View File

@ -8,7 +8,7 @@ internal enum class HmsResponseCode(
) : ValueableSerializableEnum<Int> {
UNKNOWN(-1, "Unknown"),
INVALID_CLIENT_SECRET(1101, "Invalid client_secret"),
INVALID_CLIENT_SECRET(1101, "Invalid client_secret: app or server has mismatching credentials"),
SUCCESS(80000000, "Success"),
SOME_TOKENS_ARE_INVALID(80100000, "Some tokens are right, the others are illegal"),
PARAMETERS_ARE_INVALID(80100001, "Parameters check error"),

View File

@ -61,7 +61,7 @@ class HmsHpkClientServiceImpl(
}
HmsResponseCode.INVALID_TOKEN,
HmsResponseCode.PERMISSION_DENIED -> {
HmsResponseCode.INVALID_CLIENT_SECRET -> {
throw InvalidPushTokenException()
}

View File

@ -22,31 +22,11 @@ class HmsHpkWebClientTest {
lateinit var hmsHpkWebClient: HmsHpkWebClient
@Test
fun messagesSend_pushTokenNotSpecified() {
fun messagesSend_permissionDeniedOnIncorrectAccessToken() {
val result = hmsHpkWebClient.messagesSend(
HmsHpkMessagesSendRequest(
hmsHpkMessagesSendBody = HmsHpkMessagesSendBody(
validateOnly = true,
message = Message.builder()
.addToken("pushTokenWithLongLength")
.setNotification(
Notification.builder()
.setTitle("title")
.setBody("body")
.setImage("https://avatars.githubusercontent.com/u/1435794?s=200&v=4")
.build()
)
.setAndroidConfig(
AndroidConfig.builder()
.setUrgency(AndroidUrgency.HIGH)
.setAndroidNotificationConfig(
AndroidNotificationConfig.builder()
.setDefaultSound(true)
.build(AndroidClickAction.builder().build(AndroidClickActionType.OPEN_APP))
)
.build()
)
.build()
hmsHpkMessagesSendBody = buildHmsHpkMessagesSendBody(
token = "pushTokenWithLongLength"
),
accessToken = "testAccessToken"
)
@ -58,4 +38,30 @@ class HmsHpkWebClientTest {
)
}
private fun buildHmsHpkMessagesSendBody(token: String): HmsHpkMessagesSendBody {
return HmsHpkMessagesSendBody(
validateOnly = true,
message = Message.builder()
.addToken(token)
.setNotification(
Notification.builder()
.setTitle("title")
.setBody("body")
.setImage("https://avatars.githubusercontent.com/u/1435794?s=200&v=4")
.build()
)
.setAndroidConfig(
AndroidConfig.builder()
.setUrgency(AndroidUrgency.HIGH)
.setAndroidNotificationConfig(
AndroidNotificationConfig.builder()
.setDefaultSound(true)
.build(AndroidClickAction.builder().build(AndroidClickActionType.OPEN_APP))
)
.build()
)
.build()
)
}
}

View File

@ -23,4 +23,5 @@ class HmsOauthWebClientTest {
HmsResponseCode.INVALID_CLIENT_SECRET.value
)
}
}

View File

@ -10,6 +10,7 @@ import org.springframework.boot.test.mock.mockito.MockBean
import ru.touchin.push.message.provider.dto.PushMessageNotification
import ru.touchin.push.message.provider.dto.request.PushTokenMessage
import ru.touchin.push.message.provider.exceptions.InvalidPushTokenException
import ru.touchin.push.message.provider.exceptions.PushMessageProviderException
import ru.touchin.push.message.provider.hpk.clients.hms_hpk.HmsHpkWebClient
import ru.touchin.push.message.provider.hpk.clients.hms.enums.HmsResponseCode
import ru.touchin.push.message.provider.hpk.clients.hms_hpk.responses.HmsHpkResponse
@ -27,8 +28,10 @@ class HmsHpkClientServiceTest {
lateinit var hmsHpkClientService: HmsHpkClientService
@Test
fun getAccessToken_throwsInvalidPushTokenExceptionForKnownErrors() {
Mockito.`when`(hmsOauthClientService.getAccessToken()).then { "accessToken" }
fun send_throwsInvalidPushTokenExceptionForKnownErrors() {
Mockito.`when`(
hmsOauthClientService.getAccessToken()
).then { "accessToken" }
Mockito.`when`(
hmsHpkWebClient.messagesSend(any())
@ -55,4 +58,66 @@ class HmsHpkClientServiceTest {
) { hmsHpkClientService.send(pushTokenMessage) }
}
@Test
fun send_throwsPushMessageProviderExceptionOnOtherExceptions() {
Mockito.`when`(
hmsOauthClientService.getAccessToken()
).then { "accessToken" }
Mockito.`when`(
hmsHpkWebClient.messagesSend(any())
).then {
HmsHpkResponse(
code = HmsResponseCode.OAUTH_TOKEN_EXPIRED.value.toString(),
msg = "0",
requestId = "requestId"
)
}
val pushTokenMessage = PushTokenMessage(
token = "token",
pushMessageNotification = PushMessageNotification(
title = "title",
description = "description",
imageUrl = null
),
data = emptyMap()
)
Assertions.assertThrows(
PushMessageProviderException::class.java
) { hmsHpkClientService.send(pushTokenMessage) }
}
@Test
fun send_passesSuccess() {
Mockito.`when`(
hmsOauthClientService.getAccessToken()
).then { "accessToken" }
Mockito.`when`(
hmsHpkWebClient.messagesSend(any())
).then {
HmsHpkResponse(
code = HmsResponseCode.SUCCESS.value.toString(),
msg = "0",
requestId = "requestId"
)
}
val pushTokenMessage = PushTokenMessage(
token = "token",
pushMessageNotification = PushMessageNotification(
title = "title",
description = "description",
imageUrl = null
),
data = emptyMap()
)
Assertions.assertDoesNotThrow {
hmsHpkClientService.send(pushTokenMessage)
}
}
}