Skip to content

Commit

Permalink
Fix #807: Fix error handling for FCM errors (#808)
Browse files Browse the repository at this point in the history
* Fix #807: Fix error handling for FCM errors
* - add test
  • Loading branch information
jandusil authored Apr 17, 2024
1 parent 855d55f commit 80b6b52
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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, [email protected]
*/
@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<Throwable> 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);
}

}

0 comments on commit 80b6b52

Please sign in to comment.