Skip to content

Commit 9719659

Browse files
committed
Add BatchResponse class
1 parent 9f2cca7 commit 9719659

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FirebaseAdmin.Messaging;
5+
using Xunit;
6+
7+
namespace FirebaseAdmin.Tests.Messaging
8+
{
9+
public class BatchResponseTest
10+
{
11+
[Fact]
12+
public void EmptyResponses()
13+
{
14+
var responses = new List<SendResponse>();
15+
16+
var batchResponse = new BatchResponse(responses);
17+
18+
Assert.Equal(0, batchResponse.SuccessCount);
19+
Assert.Equal(0, batchResponse.FailureCount);
20+
Assert.Equal(0, batchResponse.Responses.Count);
21+
}
22+
23+
[Fact]
24+
public void SomeResponse()
25+
{
26+
var responses = new SendResponse[]
27+
{
28+
SendResponse.FromMessageId("message1"),
29+
SendResponse.FromMessageId("message2"),
30+
SendResponse.FromException(
31+
new FirebaseMessagingException(
32+
"error-code",
33+
"error-message",
34+
null)),
35+
};
36+
37+
var batchResponse = new BatchResponse(responses);
38+
39+
Assert.Equal(2, batchResponse.SuccessCount);
40+
Assert.Equal(1, batchResponse.FailureCount);
41+
Assert.Equal(3, batchResponse.Responses.Count);
42+
Assert.True(responses.SequenceEqual(batchResponse.Responses));
43+
}
44+
45+
[Fact]
46+
public void ResponsesCannotBeNull()
47+
{
48+
Assert.Throws<ArgumentNullException>(() => new BatchResponse(null));
49+
}
50+
}
51+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2018, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using System.Collections.Generic;
17+
18+
namespace FirebaseAdmin.Messaging
19+
{
20+
/// <summary>
21+
/// Response from an operation that sends FCM messages to multiple recipients.
22+
/// See <see cref="FirebaseMessaging.SendMulticastAsync(MulticastMessage)"/>.
23+
/// </summary>
24+
public sealed class BatchResponse
25+
{
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="BatchResponse"/> class.
28+
/// </summary>
29+
/// <param name="responses">The responses.</param>
30+
public BatchResponse(IEnumerable<SendResponse> responses)
31+
{
32+
if (responses == null)
33+
{
34+
throw new ArgumentNullException(nameof(responses));
35+
}
36+
37+
this.Responses = responses.Copy();
38+
39+
var successCount = 0;
40+
41+
foreach (var response in responses)
42+
{
43+
if (response.IsSuccessful)
44+
{
45+
++successCount;
46+
}
47+
}
48+
49+
this.SuccessCount = successCount;
50+
}
51+
52+
/// <summary>
53+
/// Gets information about all responses for the batch.
54+
/// </summary>
55+
public IReadOnlyList<SendResponse> Responses { get; }
56+
57+
/// <summary>
58+
/// Gets a count of how many of the responses in <see cref="Responses"/> were
59+
/// successful.
60+
/// </summary>
61+
public int SuccessCount { get; }
62+
63+
/// <summary>
64+
/// Gets a count of how many of the responses in <see cref="Responses"/> were
65+
/// unsuccessful.
66+
/// </summary>
67+
public int FailureCount => this.Responses.Count - this.SuccessCount;
68+
}
69+
}

0 commit comments

Comments
 (0)