Skip to content

Commit 8c4c923

Browse files
committed
More fix-ups
1 parent 9f9198a commit 8c4c923

File tree

10 files changed

+228
-214
lines changed

10 files changed

+228
-214
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
using System.Linq;
18+
using FirebaseAdmin.Messaging;
19+
using Xunit;
20+
21+
namespace FirebaseAdmin.Tests.Messaging
22+
{
23+
public class BatchResponseTest
24+
{
25+
[Fact]
26+
public void EmptyResponses()
27+
{
28+
var responses = new List<SendResponse>();
29+
30+
var batchResponse = new BatchResponse(responses);
31+
32+
Assert.Equal(0, batchResponse.SuccessCount);
33+
Assert.Equal(0, batchResponse.FailureCount);
34+
Assert.Equal(0, batchResponse.Responses.Count);
35+
}
36+
37+
[Fact]
38+
public void SomeResponse()
39+
{
40+
var responses = new SendResponse[]
41+
{
42+
SendResponse.FromMessageId("message1"),
43+
SendResponse.FromMessageId("message2"),
44+
SendResponse.FromException(
45+
new FirebaseException(
46+
"error-message",
47+
null)),
48+
};
49+
50+
var batchResponse = new BatchResponse(responses);
51+
52+
Assert.Equal(2, batchResponse.SuccessCount);
53+
Assert.Equal(1, batchResponse.FailureCount);
54+
Assert.Equal(3, batchResponse.Responses.Count);
55+
Assert.True(responses.SequenceEqual(batchResponse.Responses));
56+
}
57+
58+
[Fact]
59+
public void ResponsesCannotBeNull()
60+
{
61+
Assert.Throws<ArgumentNullException>(() => new BatchResponse(null));
62+
}
63+
}
64+
}

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MulticastMessageTest.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Linq;
1617
using FirebaseAdmin.Messaging;
1718
using Xunit;
1819

@@ -40,7 +41,18 @@ public void GetMessageListNoTokens()
4041
{
4142
var message = new MulticastMessage();
4243

43-
Assert.Throws<InvalidOperationException>(() => message.GetMessageList());
44+
Assert.Throws<ArgumentException>(() => message.GetMessageList());
45+
}
46+
47+
[Fact]
48+
public void GetMessageListTooManyTokens()
49+
{
50+
var message = new MulticastMessage
51+
{
52+
Tokens = Enumerable.Range(0, 101).Select(x => x.ToString()).ToList(),
53+
};
54+
55+
Assert.Throws<ArgumentException>(() => message.GetMessageList());
4456
}
4557
}
4658
}

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/SendItemResponseTest.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/SendResponseTest.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
// limitations under the License.
1414

1515
using System;
16-
using System.Collections.Generic;
17-
using System.Linq;
1816
using FirebaseAdmin.Messaging;
1917
using Xunit;
2018

@@ -23,42 +21,44 @@ namespace FirebaseAdmin.Tests.Messaging
2321
public class SendResponseTest
2422
{
2523
[Fact]
26-
public void EmptyResponses()
24+
public void SuccessfulResponse()
2725
{
28-
var responses = new List<SendItemResponse>();
26+
var response = SendResponse.FromMessageId("message-id");
2927

30-
var batchResponse = new SendResponse(responses);
31-
32-
Assert.Equal(0, batchResponse.SuccessCount);
33-
Assert.Equal(0, batchResponse.FailureCount);
34-
Assert.Equal(0, batchResponse.Responses.Count);
28+
Assert.Equal("message-id", response.MessageId);
29+
Assert.True(response.IsSuccess);
30+
Assert.Null(response.Exception);
3531
}
3632

3733
[Fact]
38-
public void SomeResponse()
34+
public void FailureResponse()
3935
{
40-
var responses = new SendItemResponse[]
41-
{
42-
SendItemResponse.FromMessageId("message1"),
43-
SendItemResponse.FromMessageId("message2"),
44-
SendItemResponse.FromException(
45-
new FirebaseException(
46-
"error-message",
47-
null)),
48-
};
36+
var exception = new FirebaseException(
37+
"error-message",
38+
null);
39+
var response = SendResponse.FromException(exception);
40+
41+
Assert.Null(response.MessageId);
42+
Assert.False(response.IsSuccess);
43+
Assert.Same(exception, response.Exception);
44+
}
4945

50-
var batchResponse = new SendResponse(responses);
46+
[Fact]
47+
public void MessageIdCannotBeNull()
48+
{
49+
Assert.Throws<ArgumentException>(() => SendResponse.FromMessageId(null));
50+
}
5151

52-
Assert.Equal(2, batchResponse.SuccessCount);
53-
Assert.Equal(1, batchResponse.FailureCount);
54-
Assert.Equal(3, batchResponse.Responses.Count);
55-
Assert.True(responses.SequenceEqual(batchResponse.Responses));
52+
[Fact]
53+
public void MessageIdCannotBeEmpty()
54+
{
55+
Assert.Throws<ArgumentException>(() => SendResponse.FromMessageId(string.Empty));
5656
}
5757

5858
[Fact]
59-
public void ResponsesCannotBeNull()
59+
public void ExceptionCannotBeNull()
6060
{
61-
Assert.Throws<ArgumentNullException>(() => new SendResponse(null));
61+
Assert.Throws<ArgumentNullException>(() => SendResponse.FromException(null));
6262
}
6363
}
6464
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
using System.Linq;
18+
using Google.Apis.Util;
19+
20+
namespace FirebaseAdmin.Messaging
21+
{
22+
/// <summary>
23+
/// Response from an operation that sends FCM messages to multiple recipients.
24+
/// See <see cref="FirebaseMessaging.SendMulticastAsync(MulticastMessage)"/>.
25+
/// </summary>
26+
public sealed class BatchResponse
27+
{
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="BatchResponse"/> class.
30+
/// </summary>
31+
/// <param name="responses">The responses.</param>
32+
internal BatchResponse(IEnumerable<SendResponse> responses)
33+
{
34+
responses.ThrowIfNull(nameof(responses));
35+
36+
this.Responses = new List<SendResponse>(responses);
37+
this.SuccessCount = responses.Where(response => response.IsSuccess).Count();
38+
}
39+
40+
/// <summary>
41+
/// Gets information about all responses for the batch.
42+
/// </summary>
43+
public IReadOnlyList<SendResponse> Responses { get; }
44+
45+
/// <summary>
46+
/// Gets a count of how many of the responses in <see cref="Responses"/> were
47+
/// successful.
48+
/// </summary>
49+
public int SuccessCount { get; }
50+
51+
/// <summary>
52+
/// Gets a count of how many of the responses in <see cref="Responses"/> were
53+
/// unsuccessful.
54+
/// </summary>
55+
public int FailureCount => this.Responses.Count - this.SuccessCount;
56+
}
57+
}

FirebaseAdmin/FirebaseAdmin/Messaging/FirebaseMessaging.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public async Task<string> SendAsync(
174174
/// method, this is a significantly more efficient way to send multiple messages.
175175
/// </summary>
176176
/// <param name="messages">Up to 100 messages to send in the batch. Cannot be null.</param>
177-
/// <returns>A <see cref="SendResponse"/> containing details of the batch operation's
177+
/// <returns>A <see cref="BatchResponse"/> containing details of the batch operation's
178178
/// outcome.</returns>
179-
public async Task<SendResponse> SendAllAsync(IEnumerable<Message> messages)
179+
public async Task<BatchResponse> SendAllAsync(IEnumerable<Message> messages)
180180
{
181181
return await this.SendAllAsync(messages, false);
182182
}
@@ -189,9 +189,9 @@ public async Task<SendResponse> SendAllAsync(IEnumerable<Message> messages)
189189
/// <param name="messages">Up to 100 messages to send in the batch. Cannot be null.</param>
190190
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
191191
/// operation.</param>
192-
/// <returns>A <see cref="SendResponse"/> containing details of the batch operation's
192+
/// <returns>A <see cref="BatchResponse"/> containing details of the batch operation's
193193
/// outcome.</returns>
194-
public async Task<SendResponse> SendAllAsync(IEnumerable<Message> messages, CancellationToken cancellationToken)
194+
public async Task<BatchResponse> SendAllAsync(IEnumerable<Message> messages, CancellationToken cancellationToken)
195195
{
196196
return await this.SendAllAsync(messages, false, cancellationToken);
197197
}
@@ -205,9 +205,9 @@ public async Task<SendResponse> SendAllAsync(IEnumerable<Message> messages, Canc
205205
/// <param name="dryRun">A boolean indicating whether to perform a dry run (validation
206206
/// only) of the send. If set to true, the message will be sent to the FCM backend service,
207207
/// but it will not be delivered to any actual recipients.</param>
208-
/// <returns>A <see cref="SendResponse"/> containing details of the batch operation's
208+
/// <returns>A <see cref="BatchResponse"/> containing details of the batch operation's
209209
/// outcome.</returns>
210-
public async Task<SendResponse> SendAllAsync(IEnumerable<Message> messages, bool dryRun)
210+
public async Task<BatchResponse> SendAllAsync(IEnumerable<Message> messages, bool dryRun)
211211
{
212212
return await this.SendAllAsync(messages, dryRun, default);
213213
}
@@ -223,9 +223,9 @@ public async Task<SendResponse> SendAllAsync(IEnumerable<Message> messages, bool
223223
/// but it will not be delivered to any actual recipients.</param>
224224
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
225225
/// operation.</param>
226-
/// <returns>A <see cref="SendResponse"/> containing details of the batch operation's
226+
/// <returns>A <see cref="BatchResponse"/> containing details of the batch operation's
227227
/// outcome.</returns>
228-
public async Task<SendResponse> SendAllAsync(IEnumerable<Message> messages, bool dryRun, CancellationToken cancellationToken)
228+
public async Task<BatchResponse> SendAllAsync(IEnumerable<Message> messages, bool dryRun, CancellationToken cancellationToken)
229229
{
230230
return await this.messagingClient.SendAllAsync(messages, dryRun, cancellationToken);
231231
}
@@ -234,8 +234,9 @@ public async Task<SendResponse> SendAllAsync(IEnumerable<Message> messages, bool
234234
/// Sends the given multicast message to all the FCM registration tokens specified in it.
235235
/// </summary>
236236
/// <param name="message">The message to be sent. Must not be null.</param>
237-
/// <returns>A task that completes with message ID strings.</returns>
238-
public async Task<SendResponse> SendMulticastAsync(MulticastMessage message)
237+
/// <returns>A <see cref="BatchResponse"/> containing details of the batch operation's
238+
/// outcome.</returns>
239+
public async Task<BatchResponse> SendMulticastAsync(MulticastMessage message)
239240
{
240241
return await this.SendMulticastAsync(message, false);
241242
}
@@ -246,8 +247,9 @@ public async Task<SendResponse> SendMulticastAsync(MulticastMessage message)
246247
/// <param name="message">The message to be sent. Must not be null.</param>
247248
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
248249
/// operation.</param>
249-
/// <returns>A task that completes with message ID strings.</returns>
250-
public async Task<SendResponse> SendMulticastAsync(MulticastMessage message, CancellationToken cancellationToken)
250+
/// <returns>A <see cref="BatchResponse"/> containing details of the batch operation's
251+
/// outcome.</returns>
252+
public async Task<BatchResponse> SendMulticastAsync(MulticastMessage message, CancellationToken cancellationToken)
251253
{
252254
return await this.SendMulticastAsync(message, false, cancellationToken);
253255
}
@@ -263,8 +265,9 @@ public async Task<SendResponse> SendMulticastAsync(MulticastMessage message, Can
263265
/// <param name="dryRun">A boolean indicating whether to perform a dry run (validation
264266
/// only) of the send. If set to true, the message will be sent to the FCM backend service,
265267
/// but it will not be delivered to any actual recipients.</param>
266-
/// <returns>A task that completes with message ID strings.</returns>
267-
public async Task<SendResponse> SendMulticastAsync(MulticastMessage message, bool dryRun)
268+
/// <returns>A <see cref="BatchResponse"/> containing details of the batch operation's
269+
/// outcome.</returns>
270+
public async Task<BatchResponse> SendMulticastAsync(MulticastMessage message, bool dryRun)
268271
{
269272
return await this.SendMulticastAsync(message, dryRun, default);
270273
}
@@ -282,8 +285,9 @@ public async Task<SendResponse> SendMulticastAsync(MulticastMessage message, boo
282285
/// but it will not be delivered to any actual recipients.</param>
283286
/// <param name="cancellationToken">A cancellation token to monitor the asynchronous
284287
/// operation.</param>
285-
/// <returns>A task that completes with message ID strings.</returns>
286-
public async Task<SendResponse> SendMulticastAsync(
288+
/// <returns>A <see cref="BatchResponse"/> containing details of the batch operation's
289+
/// outcome.</returns>
290+
public async Task<BatchResponse> SendMulticastAsync(
287291
MulticastMessage message, bool dryRun, CancellationToken cancellationToken)
288292
{
289293
return await this.SendAllAsync(

0 commit comments

Comments
 (0)