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

Testable BatchResponse #389

Merged
merged 3 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 4 additions & 25 deletions src/main/java/com/google/firebase/messaging/BatchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.firebase.messaging;

import com.google.common.collect.ImmutableList;
import com.google.firebase.internal.NonNull;
import java.util.List;

Expand All @@ -25,32 +24,12 @@
* See {@link FirebaseMessaging#sendAll(List)} and {@link
* FirebaseMessaging#sendMulticast(MulticastMessage)}.
*/
public final class BatchResponse {

private final List<SendResponse> responses;
private final int successCount;

BatchResponse(List<SendResponse> responses) {
this.responses = ImmutableList.copyOf(responses);
int successCount = 0;
for (SendResponse response : this.responses) {
if (response.isSuccessful()) {
successCount++;
}
}
this.successCount = successCount;
}
public interface BatchResponse {

@NonNull
public List<SendResponse> getResponses() {
return responses;
}
List<SendResponse> getResponses();

public int getSuccessCount() {
return successCount;
}
int getSuccessCount();

public int getFailureCount() {
return responses.size() - successCount;
}
int getFailureCount();
}
58 changes: 58 additions & 0 deletions src/main/java/com/google/firebase/messaging/BatchResponseImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2020 Google Inc.
*
* 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 com.google.firebase.messaging;

import com.google.common.collect.ImmutableList;
import com.google.firebase.internal.NonNull;

import java.util.List;

/**
* Response from an operation that sends FCM messages to multiple recipients.
* See {@link FirebaseMessaging#sendAll(List)} and {@link
* FirebaseMessaging#sendMulticast(MulticastMessage)}.
*/
public class BatchResponseImpl implements BatchResponse {
hiranya911 marked this conversation as resolved.
Show resolved Hide resolved

private final List<SendResponse> responses;
private final int successCount;

BatchResponseImpl(List<SendResponse> responses) {
this.responses = ImmutableList.copyOf(responses);
int successCount = 0;
for (SendResponse response : this.responses) {
if (response.isSuccessful()) {
successCount++;
}
}
this.successCount = successCount;
}

@NonNull
public List<SendResponse> getResponses() {
return responses;
}

public int getSuccessCount() {
return successCount;
}

public int getFailureCount() {
return responses.size() - successCount;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private BatchResponse sendBatchRequest(
MessagingBatchCallback callback = new MessagingBatchCallback();
BatchRequest batch = newBatchRequest(messages, dryRun, callback);
batch.execute();
return new BatchResponse(callback.getResponses());
return new BatchResponseImpl(callback.getResponses());
}

private BatchRequest newBatchRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class BatchResponseTest {
public void testEmptyResponses() {
List<SendResponse> responses = new ArrayList<>();

BatchResponse batchResponse = new BatchResponse(responses);
BatchResponse batchResponse = new BatchResponseImpl(responses);

assertEquals(0, batchResponse.getSuccessCount());
assertEquals(0, batchResponse.getFailureCount());
Expand All @@ -47,7 +47,7 @@ public void testSomeResponse() {
"error-message", null))
);

BatchResponse batchResponse = new BatchResponse(responses);
BatchResponse batchResponse = new BatchResponseImpl(responses);

assertEquals(2, batchResponse.getSuccessCount());
assertEquals(1, batchResponse.getFailureCount());
Expand All @@ -61,7 +61,7 @@ public void testSomeResponse() {
public void testResponsesImmutable() {
List<SendResponse> responses = new ArrayList<>();
responses.add(SendResponse.fromMessageId("message1"));
BatchResponse batchResponse = new BatchResponse(responses);
BatchResponse batchResponse = new BatchResponseImpl(responses);
SendResponse sendResponse = SendResponse.fromMessageId("message2");

try {
Expand All @@ -74,6 +74,6 @@ public void testResponsesImmutable() {

@Test(expected = NullPointerException.class)
public void testResponsesCannotBeNull() {
new BatchResponse(null);
new BatchResponseImpl(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ private BatchResponse getBatchResponse(String ...messageIds) {
for (String messageId : messageIds) {
listBuilder.add(SendResponse.fromMessageId(messageId));
}
return new BatchResponse(listBuilder.build());
return new BatchResponseImpl(listBuilder.build());
}

private static class MockFirebaseMessagingClient implements FirebaseMessagingClient {
Expand Down