diff --git a/powerauth-push-server/src/main/java/io/getlime/push/service/PushSendingWorker.java b/powerauth-push-server/src/main/java/io/getlime/push/service/PushSendingWorker.java index 0d6c2d5f7..8613a6d17 100644 --- a/powerauth-push-server/src/main/java/io/getlime/push/service/PushSendingWorker.java +++ b/powerauth-push-server/src/main/java/io/getlime/push/service/PushSendingWorker.java @@ -188,7 +188,7 @@ void sendMessageToAndroid(final FcmClient fcmClient, final PushMessageBody pushM final MessagingErrorCode errorCode = fcmConverter.convertExceptionToErrorCode(restClientException); logger.warn("FCM server returned error response: {}.", (restClientException).getResponse()); switch (errorCode) { - case UNREGISTERED -> { + case UNREGISTERED, INVALID_ARGUMENT -> { logger.info("Push message rejected by FCM gateway, device registration for token: {} is invalid and will be removed. Error: {}", pushToken, errorCode); callback.didFinishSendingMessage(PushSendingCallback.Result.FAILED_DELETE); return; @@ -200,7 +200,7 @@ void sendMessageToAndroid(final FcmClient fcmClient, final PushMessageBody pushM callback.didFinishSendingMessage(PushSendingCallback.Result.PENDING); return; } - case SENDER_ID_MISMATCH, THIRD_PARTY_AUTH_ERROR, INVALID_ARGUMENT -> { + case SENDER_ID_MISMATCH, THIRD_PARTY_AUTH_ERROR -> { logger.warn("Push message rejected by FCM gateway. Error: {}", errorCode); callback.didFinishSendingMessage(PushSendingCallback.Result.FAILED); return; diff --git a/powerauth-push-server/src/test/java/io/getlime/push/service/PushSendingWorkerTest.java b/powerauth-push-server/src/test/java/io/getlime/push/service/PushSendingWorkerTest.java new file mode 100644 index 000000000..ddd4f13b1 --- /dev/null +++ b/powerauth-push-server/src/test/java/io/getlime/push/service/PushSendingWorkerTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2024 Wultra s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.getlime.push.service; + +import com.google.firebase.messaging.MessagingErrorCode; +import com.wultra.core.rest.client.base.RestClientException; +import io.getlime.push.configuration.PushServiceConfiguration; +import io.getlime.push.errorhandling.exceptions.FcmMissingTokenException; +import io.getlime.push.model.entity.PushMessageAttributes; +import io.getlime.push.model.entity.PushMessageBody; +import io.getlime.push.model.enumeration.Priority; +import io.getlime.push.service.fcm.FcmClient; +import io.getlime.push.service.fcm.FcmModelConverter; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import java.util.function.Consumer; + +import static org.mockito.Mockito.*; + +/** + * Tests of {@link PushSendingWorker} + * + * @author Jan Dusil, jan.dusil@wultra.com + */ +@SpringBootTest +@ActiveProfiles("test") +class PushSendingWorkerTest { + + @Mock + private FcmClient fcmClient; + + @Mock + private PushSendingCallback callback; + + @Mock + private PushServiceConfiguration pushServiceConfiguration; + + @Mock + private FcmModelConverter fcmModelConverter; + + @InjectMocks + private PushSendingWorker tested; + + @Test + void testSendMessageToAndroidError() throws FcmMissingTokenException { + final RestClientException simulatedException = new RestClientException("Simulated INVALID_ARGUMENT error"); + when(fcmModelConverter.convertExceptionToErrorCode(simulatedException)).thenReturn(MessagingErrorCode.INVALID_ARGUMENT); + doAnswer(invocation -> { + final Consumer onError = invocation.getArgument(3); + onError.accept(simulatedException); + return null; + }).when(fcmClient).exchange(any(), anyBoolean(), any(), any()); + + tested.sendMessageToAndroid(fcmClient, new PushMessageBody(), new PushMessageAttributes(), Priority.HIGH, "dummyToken", callback); + verify(callback).didFinishSendingMessage(PushSendingCallback.Result.FAILED_DELETE); + } + +} +