Skip to content

Commit 86a47ad

Browse files
authored
#1288 NotificationService: Missing IEquatable<T> Implementation (#1291)
* #1288 NotificationService: Missing IEquatable<T> Implementation in NotificationMessage * #1288 NotificationService: Missing IEquatable<T> Implementation in NotificationMessage - fix for GetHashCode
1 parent fecf36a commit 86a47ad

File tree

2 files changed

+299
-21
lines changed

2 files changed

+299
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
using System;
2+
using System.Linq;
3+
using Xunit;
4+
5+
namespace Radzen.Blazor.Tests
6+
{
7+
public class NotificationServiceTests
8+
{
9+
[Fact]
10+
public void NotificationService_IsMessageIsNull_ExceptionExpected()
11+
{
12+
NotificationService notificationService = new NotificationService();
13+
NotificationMessage notificationMessage = null;
14+
15+
var exception = Record.Exception(() => notificationService.Notify(notificationMessage));
16+
17+
Assert.IsType<ArgumentNullException>(exception);
18+
}
19+
20+
[Fact]
21+
public void NotificationService_CheckAreTwoMessages_Equals()
22+
{
23+
var messageOne = new NotificationMessage()
24+
{
25+
Severity = NotificationSeverity.Info,
26+
Summary = "Info Summary",
27+
Detail = "Info Detail",
28+
Duration = 4000
29+
};
30+
31+
var messageTwo = new NotificationMessage()
32+
{
33+
Severity = NotificationSeverity.Info,
34+
Summary = "Info Summary",
35+
Detail = "Info Detail",
36+
Duration = 4000
37+
};
38+
39+
Assert.True(messageOne.Equals(messageTwo));
40+
}
41+
42+
[Fact]
43+
public void NotificationService_CheckAreTwoMessages_NotEquals()
44+
{
45+
var messageOne = new NotificationMessage()
46+
{
47+
Severity = NotificationSeverity.Info,
48+
Summary = "Info Summary",
49+
Detail = "Info Detail",
50+
Duration = 4000
51+
};
52+
53+
var messageTwo = new NotificationMessage()
54+
{
55+
Severity = NotificationSeverity.Info,
56+
Summary = "Info Summary Two",
57+
Detail = "Info Detail Two",
58+
Duration = 6000
59+
};
60+
61+
Assert.False(messageOne.Equals(messageTwo));
62+
}
63+
64+
[Fact]
65+
public void NotificationService_CheckAreTwoMessages_EqualsByReference()
66+
{
67+
var messageOne = new NotificationMessage()
68+
{
69+
Severity = NotificationSeverity.Info,
70+
Summary = "Info Summary",
71+
Detail = "Info Detail",
72+
Duration = 4000
73+
};
74+
75+
var messageTwo = messageOne;
76+
77+
Assert.True(messageOne.Equals(messageTwo));
78+
}
79+
80+
[Fact]
81+
public void NotificationService_CheckAreTwoMessages_EqualsByOperator()
82+
{
83+
var messageOne = new NotificationMessage()
84+
{
85+
Severity = NotificationSeverity.Info,
86+
Summary = "Info Summary",
87+
Detail = "Info Detail",
88+
Duration = 4000
89+
};
90+
91+
var messageTwo = new NotificationMessage()
92+
{
93+
Severity = NotificationSeverity.Info,
94+
Summary = "Info Summary",
95+
Detail = "Info Detail",
96+
Duration = 4000
97+
};
98+
99+
Assert.True(messageOne == messageTwo);
100+
}
101+
102+
[Fact]
103+
public void NotificationService_CheckAreTwoMessages_NotEqualsByOperator()
104+
{
105+
var messageOne = new NotificationMessage()
106+
{
107+
Severity = NotificationSeverity.Info,
108+
Summary = "Info Summary",
109+
Detail = "Info Detail",
110+
Duration = 4000
111+
};
112+
113+
var messageTwo = new NotificationMessage()
114+
{
115+
Severity = NotificationSeverity.Info,
116+
Summary = "Info Summary Two",
117+
Detail = "Info Detail Two",
118+
Duration = 6000
119+
};
120+
121+
Assert.True(messageOne != messageTwo);
122+
}
123+
124+
[Fact]
125+
public void NotificationService_CheckAreTwoMessages_EqualsByHashCode()
126+
{
127+
var messageOne = new NotificationMessage()
128+
{
129+
Severity = NotificationSeverity.Info,
130+
Summary = "Info Summary",
131+
Detail = "Info Detail",
132+
Duration = 4000
133+
};
134+
135+
var messageTwo = new NotificationMessage()
136+
{
137+
Severity = NotificationSeverity.Info,
138+
Summary = "Info Summary",
139+
Detail = "Info Detail",
140+
Duration = 4000
141+
};
142+
143+
var messageOneHashCode = messageOne.GetHashCode();
144+
var messageTwoHashCode = messageTwo.GetHashCode();
145+
146+
Assert.Equal(messageOneHashCode, messageTwoHashCode);
147+
}
148+
149+
[Fact]
150+
public void NotificationService_CheckAreTwoMessages_NotEqualsByHashCode()
151+
{
152+
var messageOne = new NotificationMessage()
153+
{
154+
Severity = NotificationSeverity.Info,
155+
Summary = "Info Summary",
156+
Detail = "Info Detail",
157+
Duration = 4000
158+
};
159+
160+
var messageTwo = new NotificationMessage()
161+
{
162+
Severity = NotificationSeverity.Info,
163+
Summary = "Info Summary Two",
164+
Detail = "Info Detail Tow",
165+
Duration = 5000
166+
};
167+
168+
var messageOneHashCode = messageOne.GetHashCode();
169+
var messageTwoHashCode = messageTwo.GetHashCode();
170+
171+
Assert.NotEqual(messageOneHashCode, messageTwoHashCode);
172+
}
173+
174+
[Fact]
175+
public void NotificationService_MessagesCount_AfterAddingMessages()
176+
{
177+
NotificationService notificationService = new NotificationService();
178+
179+
//Messages are the same so only one should be added
180+
notificationService.Notify(NotificationSeverity.Info, "Info Summary", "Info Detail", 4000);
181+
notificationService.Notify(new NotificationMessage()
182+
{
183+
Severity = NotificationSeverity.Info,
184+
Summary = "Info Summary",
185+
Detail = "Info Detail",
186+
Duration = 4000
187+
});
188+
189+
int expectedMessagesNumber = 1;
190+
191+
Assert.Equal(expectedMessagesNumber, notificationService.Messages.Count);
192+
}
193+
194+
[Fact]
195+
public void NotificationService_MessagesCount_AfterAddingTwoDifferentMessages()
196+
{
197+
NotificationService notificationService = new NotificationService();
198+
199+
//Messages are the same so only one should be added
200+
notificationService.Notify(NotificationSeverity.Info, "Info Summary 2", "Info Detail 2", 6000);
201+
notificationService.Notify(new NotificationMessage()
202+
{
203+
Severity = NotificationSeverity.Info,
204+
Summary = "Info Summary",
205+
Detail = "Info Detail",
206+
Duration = 4000
207+
});
208+
209+
int expectedMessagesNumber = 2;
210+
211+
Assert.Equal(expectedMessagesNumber, notificationService.Messages.Count);
212+
}
213+
}
214+
}

Radzen.Blazor/NotificationService.cs

+85-21
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,14 @@ public class NotificationService
3131
/// <param name="message">The message.</param>
3232
public void Notify(NotificationMessage message)
3333
{
34-
var newMessage = new NotificationMessage()
34+
if (message is null)
3535
{
36-
Duration = message != null && message.Duration.HasValue ? message.Duration : 3000,
37-
Severity = message.Severity,
38-
Summary = message.Summary,
39-
Detail = message.Detail,
40-
Style = message.Style,
41-
Click = message.Click,
42-
Close = message.Close,
43-
CloseOnClick = message.CloseOnClick,
44-
Payload = message.Payload
45-
};
36+
throw new ArgumentNullException(nameof(message));
37+
}
4638

47-
if (!Messages.Contains(newMessage))
39+
if (!Messages.Contains(message))
4840
{
49-
Messages.Add(newMessage);
41+
Messages.Add(message);
5042
}
5143
}
5244

@@ -85,38 +77,38 @@ public void Notify(NotificationSeverity severity = NotificationSeverity.Info, st
8577
/// <summary>
8678
/// Class NotificationMessage.
8779
/// </summary>
88-
public class NotificationMessage
80+
public class NotificationMessage : IEquatable<NotificationMessage>
8981
{
9082
/// <summary>
9183
/// Gets or sets the duration.
9284
/// </summary>
9385
/// <value>The duration.</value>
94-
public double? Duration { get; set; }
86+
public double? Duration { get; set; } = 3000;
9587
/// <summary>
9688
/// Gets or sets the severity.
9789
/// </summary>
9890
/// <value>The severity.</value>
99-
public NotificationSeverity Severity { get; set; }
91+
public NotificationSeverity Severity { get; set; } = NotificationSeverity.Info;
10092
/// <summary>
10193
/// Gets or sets the summary.
10294
/// </summary>
10395
/// <value>The summary.</value>
104-
public string Summary { get; set; }
96+
public string Summary { get; set; } = string.Empty;
10597
/// <summary>
10698
/// Gets or sets the detail.
10799
/// </summary>
108100
/// <value>The detail.</value>
109-
public string Detail { get; set; }
101+
public string Detail { get; set; } = string.Empty;
110102
/// <summary>
111103
/// Gets or sets the style.
112104
/// </summary>
113105
/// <value>The style.</value>
114-
public string Style { get; set; }
106+
public string Style { get; set; } = string.Empty;
115107
/// <summary>
116108
/// Gets or sets the click event.
117109
/// </summary>
118110
/// <value>This event handler is called when the notification is clicked on.</value>
119-
public Action<NotificationMessage> Click { get; set; }
111+
public Action<NotificationMessage> Click { get; set; }
120112
/// <summary>
121113
/// Get or set the event for when the notification is closed
122114
/// </summary>
@@ -131,5 +123,77 @@ public class NotificationMessage
131123
/// </summary>
132124
/// <value>Used to store a custom payload that can be retreived later in the click event handler.</value>
133125
public object Payload { get; set; }
134-
}
126+
127+
128+
#region Implementation of IEquatable<NotificationMessage> and operators overloading
129+
130+
/// <summary>
131+
/// Check if NotificationMessage instance is equal to current instance.
132+
/// </summary>
133+
/// <param name="other"></param>
134+
/// <returns></returns>
135+
public bool Equals(NotificationMessage other)
136+
{
137+
if(other == null) return false;
138+
139+
if(object.ReferenceEquals(this, other)) return true;
140+
141+
return this.Severity == other.Severity
142+
&& this.Summary == other.Summary
143+
&& this.Detail == other.Detail
144+
&& this.Duration == other.Duration
145+
&& this.Style == other.Style
146+
&& this.Click == other.Click
147+
&& this.Close == other.Close
148+
&& this.CloseOnClick == other.CloseOnClick
149+
&& this.Payload == other.Payload;
150+
}
151+
152+
/// <summary>
153+
/// Check if object instance is equal to current instance.
154+
/// </summary>
155+
/// <param name="obj"></param>
156+
/// <returns></returns>
157+
public override bool Equals(object obj) => Equals(obj as NotificationMessage);
158+
159+
/// <summary>
160+
/// Return a hash code for the current object
161+
/// </summary>
162+
/// <returns></returns>
163+
public override int GetHashCode() => HashCode.Combine(Summary, Detail, Duration, Style, Click, Close, CloseOnClick, Payload);
164+
165+
/// <summary>
166+
/// Overloading == operator for NotificationMessage.
167+
/// </summary>
168+
/// <param name="message"></param>
169+
/// <param name="otherMessage"></param>
170+
/// <returns></returns>
171+
public static bool operator ==(NotificationMessage message, NotificationMessage otherMessage)
172+
{
173+
if (message is null)
174+
{
175+
if (otherMessage is null)
176+
{
177+
return true;
178+
}
179+
180+
return false;
181+
}
182+
183+
return message.Equals(otherMessage);
184+
}
185+
186+
/// <summary>
187+
/// Overloading != operator for NotificationMessage.
188+
/// </summary>
189+
/// <param name="message"></param>
190+
/// <param name="otherMessage"></param>
191+
/// <returns></returns>
192+
public static bool operator !=(NotificationMessage message, NotificationMessage otherMessage)
193+
{
194+
return !(message == otherMessage);
195+
}
196+
197+
#endregion
198+
}
135199
}

0 commit comments

Comments
 (0)