|
| 1 | +// // Copyright (c) Microsoft Corporation. |
| 2 | +// // Licensed under the MIT License. |
| 3 | + |
| 4 | +using EventLogExpert.Eventing.Models; |
1 | 5 | using EventLogExpert.UI.Models; |
| 6 | +using EventLogExpert.UI.Tests.TestUtils; |
2 | 7 | using System.Linq.Dynamic.Core.Exceptions; |
3 | 8 |
|
4 | 9 | namespace EventLogExpert.UI.Tests.Models; |
5 | 10 |
|
6 | 11 | public sealed class FilterComparisonTests |
7 | 12 | { |
8 | 13 | [Fact] |
9 | | - public void Expression_WhenValidValue_ShouldContainFunc() |
| 14 | + public void Expression_WhenCompoundCondition_ShouldMatchCorrectEvent() |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + FilterComparison model = new() { Value = "Id == 100 && Level == \"Error\"" }; |
| 18 | + DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(100, level: "Error"); |
| 19 | + DisplayEventModel nonMatchingIdEvent = EventUtils.CreateTestEvent(200, level: "Error"); |
| 20 | + DisplayEventModel nonMatchingLevelEvent = EventUtils.CreateTestEvent(100, level: "Information"); |
| 21 | + |
| 22 | + // Act |
| 23 | + bool matchResult = model.Expression(matchingEvent); |
| 24 | + bool nonMatchIdResult = model.Expression(nonMatchingIdEvent); |
| 25 | + bool nonMatchLevelResult = model.Expression(nonMatchingLevelEvent); |
| 26 | + |
| 27 | + // Assert |
| 28 | + Assert.True(matchResult); |
| 29 | + Assert.False(nonMatchIdResult); |
| 30 | + Assert.False(nonMatchLevelResult); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void Expression_WhenComputerNameEquals_ShouldMatchCorrectEvent() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + FilterComparison model = new() { Value = "ComputerName == \"SERVER01\"" }; |
| 38 | + DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(computerName: "SERVER01"); |
| 39 | + DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(computerName: "SERVER02"); |
| 40 | + |
| 41 | + // Act |
| 42 | + bool matchResult = model.Expression(matchingEvent); |
| 43 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 44 | + |
| 45 | + // Assert |
| 46 | + Assert.True(matchResult); |
| 47 | + Assert.False(nonMatchResult); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void Expression_WhenDescriptionContains_ShouldMatchCorrectEvent() |
| 52 | + { |
| 53 | + // Arrange |
| 54 | + FilterComparison model = new() { Value = "Description.Contains(\"error occurred\")" }; |
| 55 | + |
| 56 | + DisplayEventModel matchingEvent = |
| 57 | + EventUtils.CreateTestEvent(description: "An error occurred in the application"); |
| 58 | + |
| 59 | + DisplayEventModel nonMatchingEvent = |
| 60 | + EventUtils.CreateTestEvent(description: "Operation completed successfully"); |
| 61 | + |
| 62 | + // Act |
| 63 | + bool matchResult = model.Expression(matchingEvent); |
| 64 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 65 | + |
| 66 | + // Assert |
| 67 | + Assert.True(matchResult); |
| 68 | + Assert.False(nonMatchResult); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void Expression_WhenIdEquals_ShouldMatchCorrectEvent() |
10 | 73 | { |
| 74 | + // Arrange |
11 | 75 | FilterComparison model = new() { Value = "Id == 100" }; |
| 76 | + DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(100); |
| 77 | + DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(200); |
| 78 | + |
| 79 | + // Act |
| 80 | + bool matchResult = model.Expression(matchingEvent); |
| 81 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 82 | + |
| 83 | + // Assert |
| 84 | + Assert.True(matchResult); |
| 85 | + Assert.False(nonMatchResult); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void Expression_WhenIdGreaterThan_ShouldMatchCorrectEvent() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + FilterComparison model = new() { Value = "Id > 100" }; |
| 93 | + DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(150); |
| 94 | + DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(50); |
| 95 | + |
| 96 | + // Act |
| 97 | + bool matchResult = model.Expression(matchingEvent); |
| 98 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 99 | + |
| 100 | + // Assert |
| 101 | + Assert.True(matchResult); |
| 102 | + Assert.False(nonMatchResult); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public void Expression_WhenIdNotEquals_ShouldMatchCorrectEvent() |
| 107 | + { |
| 108 | + // Arrange |
| 109 | + FilterComparison model = new() { Value = "Id != 100" }; |
| 110 | + DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(200); |
| 111 | + DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(100); |
| 112 | + |
| 113 | + // Act |
| 114 | + bool matchResult = model.Expression(matchingEvent); |
| 115 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 116 | + |
| 117 | + // Assert |
| 118 | + Assert.True(matchResult); |
| 119 | + Assert.False(nonMatchResult); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void Expression_WhenLevelEquals_ShouldMatchCorrectEvent() |
| 124 | + { |
| 125 | + // Arrange |
| 126 | + FilterComparison model = new() { Value = "Level == \"Error\"" }; |
| 127 | + DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(level: "Error"); |
| 128 | + DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(level: "Information"); |
| 129 | + |
| 130 | + // Act |
| 131 | + bool matchResult = model.Expression(matchingEvent); |
| 132 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 133 | + |
| 134 | + // Assert |
| 135 | + Assert.True(matchResult); |
| 136 | + Assert.False(nonMatchResult); |
| 137 | + } |
12 | 138 |
|
| 139 | + [Fact] |
| 140 | + public void Expression_WhenOrCondition_ShouldMatchCorrectEvent() |
| 141 | + { |
| 142 | + // Arrange |
| 143 | + FilterComparison model = new() { Value = "Id == 100 || Id == 200" }; |
| 144 | + DisplayEventModel matchingEvent1 = EventUtils.CreateTestEvent(100); |
| 145 | + DisplayEventModel matchingEvent2 = EventUtils.CreateTestEvent(200); |
| 146 | + DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(300); |
| 147 | + |
| 148 | + // Act |
| 149 | + bool match1Result = model.Expression(matchingEvent1); |
| 150 | + bool match2Result = model.Expression(matchingEvent2); |
| 151 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 152 | + |
| 153 | + // Assert |
| 154 | + Assert.True(match1Result); |
| 155 | + Assert.True(match2Result); |
| 156 | + Assert.False(nonMatchResult); |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public void Expression_WhenSourceContains_ShouldMatchCorrectEvent() |
| 161 | + { |
| 162 | + // Arrange |
| 163 | + FilterComparison model = new() { Value = "Source.Contains(\"Test\")" }; |
| 164 | + DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(source: "TestSource"); |
| 165 | + DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(source: "OtherSource"); |
| 166 | + |
| 167 | + // Act |
| 168 | + bool matchResult = model.Expression(matchingEvent); |
| 169 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 170 | + |
| 171 | + // Assert |
| 172 | + Assert.True(matchResult); |
| 173 | + Assert.False(nonMatchResult); |
| 174 | + } |
| 175 | + |
| 176 | + [Fact] |
| 177 | + public void Expression_WhenTaskCategoryContains_ShouldMatchCorrectEvent() |
| 178 | + { |
| 179 | + // Arrange |
| 180 | + FilterComparison model = new() { Value = "TaskCategory.Contains(\"Security\")" }; |
| 181 | + DisplayEventModel matchingEvent = EventUtils.CreateTestEvent(taskCategory: "Security Audit"); |
| 182 | + DisplayEventModel nonMatchingEvent = EventUtils.CreateTestEvent(taskCategory: "System"); |
| 183 | + |
| 184 | + // Act |
| 185 | + bool matchResult = model.Expression(matchingEvent); |
| 186 | + bool nonMatchResult = model.Expression(nonMatchingEvent); |
| 187 | + |
| 188 | + // Assert |
| 189 | + Assert.True(matchResult); |
| 190 | + Assert.False(nonMatchResult); |
| 191 | + } |
| 192 | + |
| 193 | + [Fact] |
| 194 | + public void Expression_WhenValidValue_ShouldContainFunc() |
| 195 | + { |
| 196 | + // Arrange |
| 197 | + FilterComparison model = new(); |
| 198 | + |
| 199 | + // Act |
| 200 | + model.Value = "Id == 100"; |
| 201 | + |
| 202 | + // Assert |
13 | 203 | Assert.NotNull(model.Expression); |
14 | 204 | } |
15 | 205 |
|
| 206 | + [Fact] |
| 207 | + public void Expression_WhenValueUpdated_ShouldUpdateExpression() |
| 208 | + { |
| 209 | + // Arrange |
| 210 | + FilterComparison model = new() { Value = "Id == 100" }; |
| 211 | + DisplayEventModel event100 = EventUtils.CreateTestEvent(100); |
| 212 | + DisplayEventModel event200 = EventUtils.CreateTestEvent(200); |
| 213 | + |
| 214 | + // Act |
| 215 | + bool initialMatch100 = model.Expression(event100); |
| 216 | + bool initialMatch200 = model.Expression(event200); |
| 217 | + model.Value = "Id == 200"; |
| 218 | + bool updatedMatch100 = model.Expression(event100); |
| 219 | + bool updatedMatch200 = model.Expression(event200); |
| 220 | + |
| 221 | + // Assert |
| 222 | + Assert.True(initialMatch100); |
| 223 | + Assert.False(initialMatch200); |
| 224 | + Assert.False(updatedMatch100); |
| 225 | + Assert.True(updatedMatch200); |
| 226 | + } |
| 227 | + |
| 228 | + [Fact] |
| 229 | + public void Value_WhenEmptyString_ShouldThrow() |
| 230 | + { |
| 231 | + // Arrange |
| 232 | + FilterComparison model = new(); |
| 233 | + |
| 234 | + // Act & Assert |
| 235 | + Assert.Throws<ArgumentException>(() => model.Value = ""); |
| 236 | + } |
| 237 | + |
| 238 | + [Fact] |
| 239 | + public void Value_WhenInvalidPropertyName_ShouldThrow() |
| 240 | + { |
| 241 | + // Arrange |
| 242 | + FilterComparison model = new(); |
| 243 | + |
| 244 | + // Act & Assert |
| 245 | + Assert.Throws<ParseException>(() => model.Value = "InvalidProperty == 100"); |
| 246 | + } |
| 247 | + |
16 | 248 | [Fact] |
17 | 249 | public void Value_WhenNotValid_ShouldThrow() |
18 | 250 | { |
| 251 | + // Arrange |
19 | 252 | FilterComparison model = new(); |
20 | 253 |
|
| 254 | + // Act & Assert |
21 | 255 | Assert.Throws<ParseException>(() => model.Value = "Id == invalid"); |
22 | 256 | } |
23 | 257 | } |
0 commit comments