-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAnnouncementsLoaderTests.cs
More file actions
185 lines (162 loc) · 5.86 KB
/
AnnouncementsLoaderTests.cs
File metadata and controls
185 lines (162 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using ByteSync.Common.Controls.Json;
using ByteSync.Common.Business.Announcements;
using ByteSync.ServerCommon.Business.Settings;
using ByteSync.ServerCommon.Loaders;
using FakeItEasy;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Net;
using System.Text;
using ByteSync.ServerCommon.Tests.Helpers;
namespace ByteSync.ServerCommon.Tests.Loaders;
[TestFixture]
public class AnnouncementsLoaderTests
{
private ILogger<AnnouncementsLoader> _mockLogger;
private IOptions<AppSettings> _mockAppSettings;
private HttpClient _httpClient;
private AnnouncementsLoader _loader;
[SetUp]
public void SetUp()
{
_mockLogger = A.Fake<ILogger<AnnouncementsLoader>>();
_mockAppSettings = Options.Create(new AppSettings
{
AnnouncementsUrl = "https://test.example.com/announcements.json"
});
_httpClient = new HttpClient(new MockHttpMessageHandler(""));
_loader = new AnnouncementsLoader(_mockAppSettings, _mockLogger, _httpClient);
}
[TearDown]
public void TearDown()
{
_httpClient?.Dispose();
}
[Test]
public async Task Load_ShouldReturnAnnouncements_WhenValidDataIsRetrieved()
{
// Arrange
var announcements = new List<Announcement>
{
new Announcement
{
Id = "msg1",
StartDate = DateTime.UtcNow.AddDays(-1),
EndDate = DateTime.UtcNow.AddDays(1),
Message = new Dictionary<string, string>
{
{ "en", "Test message in English" },
{ "fr", "Message de test en français" }
}
},
new Announcement
{
Id = "msg2",
StartDate = DateTime.UtcNow,
EndDate = DateTime.UtcNow.AddDays(7),
Message = new Dictionary<string, string>
{
{ "en", "Another test message" },
{ "fr", "Un autre message de test" }
}
}
};
var jsonContent = JsonHelper.Serialize(announcements);
_httpClient = new HttpClient(new MockHttpMessageHandler(jsonContent));
_loader = new AnnouncementsLoader(_mockAppSettings, _mockLogger, _httpClient);
// Act
var result = await _loader.Load();
// Assert
result.Should().NotBeNull();
result.Should().HaveCount(2);
result[0].Id.Should().Be("msg1");
result[0].Message["en"].Should().Be("Test message in English");
result[0].Message["fr"].Should().Be("Message de test en français");
result[1].Id.Should().Be("msg2");
}
[Test]
public async Task Load_ShouldThrowException_WhenHttpRequestFails()
{
// Arrange
_httpClient = new HttpClient(new MockHttpMessageHandler("", HttpStatusCode.NotFound));
_loader = new AnnouncementsLoader(_mockAppSettings, _mockLogger, _httpClient);
// Act & Assert
await FluentActions.Awaiting(() => _loader.Load())
.Should().ThrowAsync<Exception>();
}
[Test]
public async Task Load_ShouldHandleEmptyList_AndReturnEmptyList()
{
// Arrange
var announcements = new List<Announcement>();
var jsonContent = JsonHelper.Serialize(announcements);
_httpClient = new HttpClient(new MockHttpMessageHandler(jsonContent));
_loader = new AnnouncementsLoader(_mockAppSettings, _mockLogger, _httpClient);
// Act
var result = await _loader.Load();
// Assert
result.Should().NotBeNull();
result.Should().BeEmpty();
}
[Test]
public async Task Load_ShouldThrowException_WhenDeserializationReturnsNull()
{
// Arrange
var malformedJson = "{ invalid json }";
_httpClient = new HttpClient(new MockHttpMessageHandler(malformedJson));
_loader = new AnnouncementsLoader(_mockAppSettings, _mockLogger, _httpClient);
// Act & Assert
await FluentActions.Awaiting(() => _loader.Load())
.Should().ThrowAsync<Exception>();
}
[Test]
public async Task Load_ShouldHandleAnnouncementsWithEmptyMessages()
{
// Arrange
var announcements = new List<Announcement>
{
new Announcement
{
Id = "msg1",
StartDate = DateTime.UtcNow.AddDays(-1),
EndDate = DateTime.UtcNow.AddDays(1),
Message = new Dictionary<string, string>() // Empty messages
}
};
var jsonContent = JsonHelper.Serialize(announcements);
_httpClient = new HttpClient(new MockHttpMessageHandler(jsonContent));
_loader = new AnnouncementsLoader(_mockAppSettings, _mockLogger, _httpClient);
// Act
var result = await _loader.Load();
// Assert
result.Should().NotBeNull();
result.Should().HaveCount(1);
result[0].Id.Should().Be("msg1");
result[0].Message.Should().BeEmpty();
}
[Test]
public async Task Load_ShouldHandleAnnouncementsWithNullMessages()
{
// Arrange
var announcements = new List<Announcement>
{
new Announcement
{
Id = "msg1",
StartDate = DateTime.UtcNow.AddDays(-1),
EndDate = DateTime.UtcNow.AddDays(1),
Message = null! // Null messages
}
};
var jsonContent = JsonHelper.Serialize(announcements);
_httpClient = new HttpClient(new MockHttpMessageHandler(jsonContent));
_loader = new AnnouncementsLoader(_mockAppSettings, _mockLogger, _httpClient);
// Act
var result = await _loader.Load();
// Assert
result.Should().NotBeNull();
result.Should().HaveCount(1);
result[0].Id.Should().Be("msg1");
}
}