Skip to content

Commit

Permalink
Testable BatchResponse (#389)
Browse files Browse the repository at this point in the history
* Testable BatchResponse
- Converts batch response to an interface
- Moves batch response implementation to BatchResponseImpl
- Update FirebaseMessagingClientImpl to use BatchResponseImpl
- Updated tests

* Make BatchResponseImpl package private
  • Loading branch information
ToxicBakery authored Apr 10, 2020
1 parent b18cf69 commit 2cfbc3d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 31 deletions.
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)}.
*/
class BatchResponseImpl implements BatchResponse {

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

0 comments on commit 2cfbc3d

Please sign in to comment.