Skip to content

Commit 9f2cca7

Browse files
committed
Add SendResponse class
1 parent bdb2730 commit 9f2cca7

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-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 FirebaseAdmin.Messaging;
3+
using Xunit;
4+
5+
namespace FirebaseAdmin.Tests.Messaging
6+
{
7+
public class SendResponseTest
8+
{
9+
[Fact]
10+
public void SuccessfulResponse()
11+
{
12+
var response = SendResponse.FromMessageId("message-id");
13+
14+
Assert.Equal("message-id", response.MessageId);
15+
Assert.True(response.IsSuccessful);
16+
Assert.Null(response.Exception);
17+
}
18+
19+
[Fact]
20+
public void FailureResponse()
21+
{
22+
FirebaseMessagingException exception = new FirebaseMessagingException(
23+
"error-code",
24+
"error-message",
25+
null);
26+
SendResponse response = SendResponse.FromException(exception);
27+
28+
Assert.Null(response.MessageId);
29+
Assert.False(response.IsSuccessful);
30+
Assert.Same(exception, response.Exception);
31+
}
32+
33+
[Fact]
34+
public void MessageIdCannotBeNull()
35+
{
36+
Assert.Throws<ArgumentNullException>(() => SendResponse.FromMessageId(null));
37+
}
38+
39+
[Fact]
40+
public void MessageIdCannotBeEmpty()
41+
{
42+
Assert.Throws<ArgumentException>(() => SendResponse.FromMessageId(string.Empty));
43+
}
44+
45+
[Fact]
46+
public void ExceptionCannotBeNull()
47+
{
48+
Assert.Throws<ArgumentNullException>(() => SendResponse.FromException(null));
49+
}
50+
}
51+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
17+
namespace FirebaseAdmin.Messaging
18+
{
19+
/// <summary>
20+
/// The result of an individual send operation that was executed as part of a batch. See
21+
/// <see cref="BatchResponse"/> for more details.
22+
/// </summary>
23+
public sealed class SendResponse
24+
{
25+
private SendResponse(string messageId)
26+
{
27+
this.MessageId = messageId;
28+
}
29+
30+
private SendResponse(FirebaseMessagingException exception)
31+
{
32+
this.Exception = exception;
33+
}
34+
35+
/// <summary>
36+
/// Gets a message ID string if the send operation was successful. Otherwise returns null.
37+
/// </summary>
38+
public string MessageId { get; }
39+
40+
/// <summary>
41+
/// Gets an exception if the send operation failed. Otherwise returns null.
42+
/// </summary>
43+
public FirebaseMessagingException Exception { get; }
44+
45+
/// <summary>
46+
/// Gets a value indicating whether the send operation was successful or not. When this property
47+
/// is <see langword="true"/>, <see cref="MessageId"/> is guaranteed to return a
48+
/// non-<see langword="null"/> value. When this property is <see langword="false"/>,
49+
/// <see cref="Exception"/> is guaranteed to return a non-<see langword="null"/> value.
50+
/// </summary>
51+
public bool IsSuccessful => !string.IsNullOrEmpty(this.MessageId);
52+
53+
internal static SendResponse FromMessageId(string messageId)
54+
{
55+
if (messageId == null)
56+
{
57+
throw new ArgumentNullException(nameof(messageId));
58+
}
59+
60+
if (messageId == string.Empty)
61+
{
62+
throw new ArgumentException("Cannot be empty.", nameof(messageId));
63+
}
64+
65+
return new SendResponse(messageId);
66+
}
67+
68+
internal static SendResponse FromException(FirebaseMessagingException exception)
69+
{
70+
if (exception == null)
71+
{
72+
throw new ArgumentNullException(nameof(exception));
73+
}
74+
75+
return new SendResponse(exception);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)