Skip to content

Commit 2cfbc3d

Browse files
authored
Testable BatchResponse (#389)
* 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
1 parent b18cf69 commit 2cfbc3d

File tree

5 files changed

+68
-31
lines changed

5 files changed

+68
-31
lines changed

src/main/java/com/google/firebase/messaging/BatchResponse.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.firebase.messaging;
1818

19-
import com.google.common.collect.ImmutableList;
2019
import com.google.firebase.internal.NonNull;
2120
import java.util.List;
2221

@@ -25,32 +24,12 @@
2524
* See {@link FirebaseMessaging#sendAll(List)} and {@link
2625
* FirebaseMessaging#sendMulticast(MulticastMessage)}.
2726
*/
28-
public final class BatchResponse {
29-
30-
private final List<SendResponse> responses;
31-
private final int successCount;
32-
33-
BatchResponse(List<SendResponse> responses) {
34-
this.responses = ImmutableList.copyOf(responses);
35-
int successCount = 0;
36-
for (SendResponse response : this.responses) {
37-
if (response.isSuccessful()) {
38-
successCount++;
39-
}
40-
}
41-
this.successCount = successCount;
42-
}
27+
public interface BatchResponse {
4328

4429
@NonNull
45-
public List<SendResponse> getResponses() {
46-
return responses;
47-
}
30+
List<SendResponse> getResponses();
4831

49-
public int getSuccessCount() {
50-
return successCount;
51-
}
32+
int getSuccessCount();
5233

53-
public int getFailureCount() {
54-
return responses.size() - successCount;
55-
}
34+
int getFailureCount();
5635
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.messaging;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.firebase.internal.NonNull;
21+
22+
import java.util.List;
23+
24+
/**
25+
* Response from an operation that sends FCM messages to multiple recipients.
26+
* See {@link FirebaseMessaging#sendAll(List)} and {@link
27+
* FirebaseMessaging#sendMulticast(MulticastMessage)}.
28+
*/
29+
class BatchResponseImpl implements BatchResponse {
30+
31+
private final List<SendResponse> responses;
32+
private final int successCount;
33+
34+
BatchResponseImpl(List<SendResponse> responses) {
35+
this.responses = ImmutableList.copyOf(responses);
36+
int successCount = 0;
37+
for (SendResponse response : this.responses) {
38+
if (response.isSuccessful()) {
39+
successCount++;
40+
}
41+
}
42+
this.successCount = successCount;
43+
}
44+
45+
@NonNull
46+
public List<SendResponse> getResponses() {
47+
return responses;
48+
}
49+
50+
public int getSuccessCount() {
51+
return successCount;
52+
}
53+
54+
public int getFailureCount() {
55+
return responses.size() - successCount;
56+
}
57+
58+
}

src/main/java/com/google/firebase/messaging/FirebaseMessagingClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private BatchResponse sendBatchRequest(
167167
MessagingBatchCallback callback = new MessagingBatchCallback();
168168
BatchRequest batch = newBatchRequest(messages, dryRun, callback);
169169
batch.execute();
170-
return new BatchResponse(callback.getResponses());
170+
return new BatchResponseImpl(callback.getResponses());
171171
}
172172

173173
private BatchRequest newBatchRequest(

src/test/java/com/google/firebase/messaging/BatchResponseTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class BatchResponseTest {
3131
public void testEmptyResponses() {
3232
List<SendResponse> responses = new ArrayList<>();
3333

34-
BatchResponse batchResponse = new BatchResponse(responses);
34+
BatchResponse batchResponse = new BatchResponseImpl(responses);
3535

3636
assertEquals(0, batchResponse.getSuccessCount());
3737
assertEquals(0, batchResponse.getFailureCount());
@@ -47,7 +47,7 @@ public void testSomeResponse() {
4747
"error-message", null))
4848
);
4949

50-
BatchResponse batchResponse = new BatchResponse(responses);
50+
BatchResponse batchResponse = new BatchResponseImpl(responses);
5151

5252
assertEquals(2, batchResponse.getSuccessCount());
5353
assertEquals(1, batchResponse.getFailureCount());
@@ -61,7 +61,7 @@ public void testSomeResponse() {
6161
public void testResponsesImmutable() {
6262
List<SendResponse> responses = new ArrayList<>();
6363
responses.add(SendResponse.fromMessageId("message1"));
64-
BatchResponse batchResponse = new BatchResponse(responses);
64+
BatchResponse batchResponse = new BatchResponseImpl(responses);
6565
SendResponse sendResponse = SendResponse.fromMessageId("message2");
6666

6767
try {
@@ -74,6 +74,6 @@ public void testResponsesImmutable() {
7474

7575
@Test(expected = NullPointerException.class)
7676
public void testResponsesCannotBeNull() {
77-
new BatchResponse(null);
77+
new BatchResponseImpl(null);
7878
}
7979
}

src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ private BatchResponse getBatchResponse(String ...messageIds) {
668668
for (String messageId : messageIds) {
669669
listBuilder.add(SendResponse.fromMessageId(messageId));
670670
}
671-
return new BatchResponse(listBuilder.build());
671+
return new BatchResponseImpl(listBuilder.build());
672672
}
673673

674674
private static class MockFirebaseMessagingClient implements FirebaseMessagingClient {

0 commit comments

Comments
 (0)