|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using NotificationSystem.Models; |
| 3 | +using NotificationSystem.Template; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Moq; |
| 8 | + |
| 9 | +namespace NotificationSystem.Tests.Unit |
| 10 | +{ |
| 11 | + public class EmailTemplateTests |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Tests that GetSubscriberEmailBody generates correct map URIs for various locations |
| 15 | + /// by verifying the generated HTML contains the expected image URLs. |
| 16 | + /// Uses mocked OrcasiteHelper to simulate production behavior. |
| 17 | + /// </summary> |
| 18 | + [Theory] |
| 19 | + [InlineData("Sunset Bay", "sunset-bay.jpg")] |
| 20 | + [InlineData("Mast Center", "mast-center.jpg")] |
| 21 | + [InlineData("North San Juan Channel", "north-sjc.jpg")] |
| 22 | + [InlineData("Point Robinson", "point-robinson.jpg")] |
| 23 | + [InlineData("Bush Point", "bush-point.jpg")] |
| 24 | + [InlineData("Haro Strait", "haro-strait.jpg")] |
| 25 | + [InlineData("Port Townsend", "port-townsend.jpg")] |
| 26 | + [InlineData("Orcasound Lab", "orcasound-lab.jpg")] |
| 27 | + public void GetSubscriberEmailBody_GeneratesCorrectMapUri_ForLocationName(string locationName, string expectedFileName) |
| 28 | + { |
| 29 | + // Arrange |
| 30 | + var messages = new List<JObject> |
| 31 | + { |
| 32 | + JObject.FromObject(new |
| 33 | + { |
| 34 | + timestamp = DateTime.UtcNow, |
| 35 | + location = new |
| 36 | + { |
| 37 | + name = locationName, |
| 38 | + latitude = 48.123, |
| 39 | + longitude = -122.456, |
| 40 | + id = "test_location" |
| 41 | + }, |
| 42 | + moderator = "Test Moderator", |
| 43 | + comments = "Test comments" |
| 44 | + }) |
| 45 | + }; |
| 46 | + |
| 47 | + // Mock OrcasiteHelper to simulate production behavior |
| 48 | + var mockOrcasiteHelper = new Mock<OrcasiteHelper>( |
| 49 | + new Mock<ILogger<OrcasiteHelper>>().Object, |
| 50 | + new System.Net.Http.HttpClient() |
| 51 | + ); |
| 52 | + |
| 53 | + // Setup slug mappings based on actual Orcasite feeds |
| 54 | + mockOrcasiteHelper.Setup(x => x.GetSlugByLocationName(It.IsAny<string>())) |
| 55 | + .Returns<string>(name => |
| 56 | + { |
| 57 | + // Return actual slugs from Orcasite for locations where they differ from simple transformation |
| 58 | + if (name == "North San Juan Channel") return "north-sjc"; |
| 59 | + // For other locations, return null to fall back to simple transformation |
| 60 | + return null; |
| 61 | + }); |
| 62 | + |
| 63 | + string expectedMapUrl = $"https://orcanotificationstorage.blob.core.windows.net/images/{expectedFileName}"; |
| 64 | + |
| 65 | + // Act - with OrcasiteHelper as in production |
| 66 | + string emailBody = EmailTemplate.GetSubscriberEmailBody(messages, mockOrcasiteHelper.Object); |
| 67 | + |
| 68 | + // Assert |
| 69 | + Assert.Contains(expectedMapUrl, emailBody); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Tests that location names with multiple words are correctly converted with hyphens in URIs. |
| 74 | + /// </summary> |
| 75 | + [Fact] |
| 76 | + public void GetSubscriberEmailBody_HandlesMultipleSpacesCorrectly() |
| 77 | + { |
| 78 | + // Arrange |
| 79 | + var messages = new List<JObject> |
| 80 | + { |
| 81 | + JObject.FromObject(new |
| 82 | + { |
| 83 | + timestamp = DateTime.UtcNow, |
| 84 | + location = new |
| 85 | + { |
| 86 | + name = "North San Juan Channel", |
| 87 | + latitude = 48.591294, |
| 88 | + longitude = -123.058779, |
| 89 | + id = "rpi_north_sjc" |
| 90 | + }, |
| 91 | + moderator = "Test Moderator", |
| 92 | + comments = "Test comments" |
| 93 | + }) |
| 94 | + }; |
| 95 | + |
| 96 | + // Mock OrcasiteHelper to return the correct slug |
| 97 | + var mockOrcasiteHelper = new Mock<OrcasiteHelper>( |
| 98 | + new Mock<ILogger<OrcasiteHelper>>().Object, |
| 99 | + new System.Net.Http.HttpClient() |
| 100 | + ); |
| 101 | + mockOrcasiteHelper.Setup(x => x.GetSlugByLocationName("North San Juan Channel")) |
| 102 | + .Returns("north-sjc"); |
| 103 | + |
| 104 | + // Act - with OrcasiteHelper as in production |
| 105 | + string emailBody = EmailTemplate.GetSubscriberEmailBody(messages, mockOrcasiteHelper.Object); |
| 106 | + |
| 107 | + // Assert - the URI should use "north-sjc" from OrcasiteHelper |
| 108 | + Assert.Contains("north-sjc.jpg", emailBody); |
| 109 | + Assert.DoesNotContain("north-san-juan-channel.jpg", emailBody); |
| 110 | + // The location name should still display with spaces |
| 111 | + Assert.Contains("North San Juan Channel", emailBody); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Tests that the fallback behavior works when OrcasiteHelper is not available. |
| 116 | + /// </summary> |
| 117 | + [Fact] |
| 118 | + public void GetSubscriberEmailBody_FallsBackToSimpleTransformation_WhenOrcasiteHelperNotProvided() |
| 119 | + { |
| 120 | + // Arrange |
| 121 | + var messages = new List<JObject> |
| 122 | + { |
| 123 | + JObject.FromObject(new |
| 124 | + { |
| 125 | + timestamp = DateTime.UtcNow, |
| 126 | + location = new |
| 127 | + { |
| 128 | + name = "Sunset Bay", |
| 129 | + latitude = 47.86497296593844, |
| 130 | + longitude = -122.33393605795372, |
| 131 | + id = "rpi_sunset_bay" |
| 132 | + }, |
| 133 | + moderator = "Test Moderator", |
| 134 | + comments = "Test comments" |
| 135 | + }) |
| 136 | + }; |
| 137 | + |
| 138 | + // Act - without OrcasiteHelper, it falls back to simple transformation |
| 139 | + string emailBody = EmailTemplate.GetSubscriberEmailBody(messages, null); |
| 140 | + |
| 141 | + // Assert - should use simple transformation |
| 142 | + Assert.Contains("sunset-bay.jpg", emailBody); |
| 143 | + Assert.Contains("Sunset Bay", emailBody); |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Tests that the email body contains all required sections and location information. |
| 148 | + /// </summary> |
| 149 | + [Fact] |
| 150 | + public void GetSubscriberEmailBody_ContainsAllRequiredSections() |
| 151 | + { |
| 152 | + // Arrange |
| 153 | + var testTimestamp = new DateTime(2025, 1, 15, 10, 30, 0, DateTimeKind.Utc); |
| 154 | + var messages = new List<JObject> |
| 155 | + { |
| 156 | + JObject.FromObject(new |
| 157 | + { |
| 158 | + timestamp = testTimestamp, |
| 159 | + location = new |
| 160 | + { |
| 161 | + name = "Sunset Bay", |
| 162 | + latitude = 47.86497296593844, |
| 163 | + longitude = -122.33393605795372, |
| 164 | + id = "rpi_sunset_bay" |
| 165 | + }, |
| 166 | + moderator = "Jane Doe", |
| 167 | + comments = "Clear SRKW calls detected" |
| 168 | + }) |
| 169 | + }; |
| 170 | + |
| 171 | + // Act - without OrcasiteHelper, it falls back to simple transformation |
| 172 | + string emailBody = EmailTemplate.GetSubscriberEmailBody(messages, null); |
| 173 | + |
| 174 | + // Assert |
| 175 | + Assert.Contains("Southern Resident Killer Whale Detected", emailBody); |
| 176 | + Assert.Contains("Sunset Bay", emailBody); |
| 177 | + Assert.Contains("47.86497296593844", emailBody); |
| 178 | + Assert.Contains("-122.33393605795372", emailBody); |
| 179 | + Assert.Contains("Jane Doe", emailBody); |
| 180 | + Assert.Contains("Clear SRKW calls detected", emailBody); |
| 181 | + Assert.Contains("https://orcanotificationstorage.blob.core.windows.net/images/sunset-bay.jpg", emailBody); |
| 182 | + } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// Tests that GetSubscriberEmailBody uses OrcasiteHelper to lookup the correct slug when provided. |
| 186 | + /// </summary> |
| 187 | + [Fact] |
| 188 | + public void GetSubscriberEmailBody_UsesOrcasiteHelperSlug_WhenProvided() |
| 189 | + { |
| 190 | + // Arrange |
| 191 | + var messages = new List<JObject> |
| 192 | + { |
| 193 | + JObject.FromObject(new |
| 194 | + { |
| 195 | + timestamp = DateTime.UtcNow, |
| 196 | + location = new |
| 197 | + { |
| 198 | + name = "North San Juan Channel", |
| 199 | + latitude = 48.591294, |
| 200 | + longitude = -123.058779, |
| 201 | + id = "rpi_north_sjc" |
| 202 | + }, |
| 203 | + moderator = "Test Moderator", |
| 204 | + comments = "Test comments" |
| 205 | + }) |
| 206 | + }; |
| 207 | + |
| 208 | + // Mock OrcasiteHelper that returns the actual slug |
| 209 | + var mockOrcasiteHelper = new Mock<OrcasiteHelper>( |
| 210 | + new Mock<ILogger<OrcasiteHelper>>().Object, |
| 211 | + new System.Net.Http.HttpClient() |
| 212 | + ); |
| 213 | + mockOrcasiteHelper.Setup(x => x.GetSlugByLocationName(It.IsAny<string>())) |
| 214 | + .Returns<string>(locationName => |
| 215 | + { |
| 216 | + if (locationName == "North San Juan Channel") return "north-sjc"; |
| 217 | + return null; |
| 218 | + }); |
| 219 | + |
| 220 | + // Act |
| 221 | + string emailBody = EmailTemplate.GetSubscriberEmailBody(messages, mockOrcasiteHelper.Object); |
| 222 | + |
| 223 | + // Assert - should use "north-sjc" from OrcasiteHelper, not "north-san-juan-channel" |
| 224 | + Assert.Contains("north-sjc.jpg", emailBody); |
| 225 | + Assert.DoesNotContain("north-san-juan-channel.jpg", emailBody); |
| 226 | + Assert.Contains("North San Juan Channel", emailBody); |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments