Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #807: Fix error handling for FCM errors #808

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}