|
| 1 | +using System; |
| 2 | +using Moq; |
| 3 | +using Serilog.Configuration; |
| 4 | +using Serilog.Formatting; |
| 5 | +using Serilog.Sinks.MSSqlServer.Configuration.Factories; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Serilog.Sinks.MSSqlServer.Tests.Configuration.Extensions.System.Configuration |
| 9 | +{ |
| 10 | + public class LoggerConfigurationMSSqlServerExtensionsTests |
| 11 | + { |
| 12 | + private readonly LoggerConfiguration _loggerConfiguration; |
| 13 | + private readonly Mock<IApplySystemConfiguration> _applySystemConfigurationMock; |
| 14 | + |
| 15 | + public LoggerConfigurationMSSqlServerExtensionsTests() |
| 16 | + { |
| 17 | + _loggerConfiguration = new LoggerConfiguration(); |
| 18 | + _applySystemConfigurationMock = new Mock<IApplySystemConfiguration>(); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void MSSqlServerCallsApplySystemGetSectionWithSectionName() |
| 23 | + { |
| 24 | + // Arrange |
| 25 | + const string inputSectionName = "TestConfigSectionName"; |
| 26 | + var sinkFactoryMock = new Mock<IMSSqlServerSinkFactory>(); |
| 27 | + |
| 28 | + // Act |
| 29 | + _loggerConfiguration.WriteTo.MSSqlServerInternal( |
| 30 | + configSectionName: inputSectionName, |
| 31 | + connectionString: "TestConnectionString", |
| 32 | + tableName: "TestTableName", |
| 33 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 34 | + sinkFactory: sinkFactoryMock.Object); |
| 35 | + |
| 36 | + // Assert |
| 37 | + _applySystemConfigurationMock.Verify(c => c.GetSinkConfigurationSection(inputSectionName), |
| 38 | + Times.Once); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void MSSqlServerCallsApplySystemConfigurationGetConnectionString() |
| 43 | + { |
| 44 | + // Arrange |
| 45 | + const string inputConnectionString = "TestConnectionString"; |
| 46 | + _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny<string>())) |
| 47 | + .Returns(new MSSqlServerConfigurationSection()); |
| 48 | + var sinkFactoryMock = new Mock<IMSSqlServerSinkFactory>(); |
| 49 | + |
| 50 | + // Act |
| 51 | + _loggerConfiguration.WriteTo.MSSqlServerInternal( |
| 52 | + configSectionName: "TestConfigSectionName", |
| 53 | + connectionString: inputConnectionString, |
| 54 | + tableName: "TestTableName", |
| 55 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 56 | + sinkFactory: sinkFactoryMock.Object); |
| 57 | + |
| 58 | + // Assert |
| 59 | + _applySystemConfigurationMock.Verify(c => c.GetConnectionString(inputConnectionString), |
| 60 | + Times.Once); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void MSSqlServerCallsSinkFactoryWithConnectionStringFromSystemConfig() |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + const string configConnectionString = "TestConnectionStringFromConfig"; |
| 68 | + _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny<string>())) |
| 69 | + .Returns(new MSSqlServerConfigurationSection()); |
| 70 | + _applySystemConfigurationMock.Setup(c => c.GetConnectionString(It.IsAny<string>())) |
| 71 | + .Returns(configConnectionString); |
| 72 | + var sinkFactoryMock = new Mock<IMSSqlServerSinkFactory>(); |
| 73 | + |
| 74 | + // Act |
| 75 | + _loggerConfiguration.WriteTo.MSSqlServerInternal( |
| 76 | + configSectionName: "TestConfigSectionName", |
| 77 | + connectionString: "TestConnectionString", |
| 78 | + tableName: "TestTableName", |
| 79 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 80 | + sinkFactory: sinkFactoryMock.Object); |
| 81 | + |
| 82 | + // Assert |
| 83 | + sinkFactoryMock.Verify(f => f.Create(configConnectionString, It.IsAny<string>(), It.IsAny<int>(), It.IsAny<TimeSpan>(), |
| 84 | + It.IsAny<IFormatProvider>(), It.IsAny<bool>(), It.IsAny<ColumnOptions>(), It.IsAny<string>(), It.IsAny<ITextFormatter>()), Times.Once); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void MSSqlServerCallsApplySystemConfigurationGetColumnOptions() |
| 89 | + { |
| 90 | + // Arrange |
| 91 | + var columnOptions = new ColumnOptions(); |
| 92 | + var systemConfigSection = new MSSqlServerConfigurationSection(); |
| 93 | + _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny<string>())) |
| 94 | + .Returns(systemConfigSection); |
| 95 | + var sinkFactoryMock = new Mock<IMSSqlServerSinkFactory>(); |
| 96 | + |
| 97 | + // Act |
| 98 | + _loggerConfiguration.WriteTo.MSSqlServerInternal( |
| 99 | + configSectionName: "TestConfigSectionName", |
| 100 | + connectionString: "TestConnectionString", |
| 101 | + tableName: "TestTableName", |
| 102 | + columnOptions: columnOptions, |
| 103 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 104 | + sinkFactory: sinkFactoryMock.Object); |
| 105 | + |
| 106 | + // Assert |
| 107 | + _applySystemConfigurationMock.Verify(c => c.ConfigureColumnOptions(systemConfigSection, columnOptions), |
| 108 | + Times.Once); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void MSSqlServerCallsSinkFactoryWithColumnOptionsFromSystemConfig() |
| 113 | + { |
| 114 | + // Arrange |
| 115 | + var configColumnOptions = new ColumnOptions(); |
| 116 | + _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny<string>())) |
| 117 | + .Returns(new MSSqlServerConfigurationSection()); |
| 118 | + _applySystemConfigurationMock.Setup(c => c.ConfigureColumnOptions(It.IsAny<MSSqlServerConfigurationSection>(), It.IsAny<ColumnOptions>())) |
| 119 | + .Returns(configColumnOptions); |
| 120 | + var sinkFactoryMock = new Mock<IMSSqlServerSinkFactory>(); |
| 121 | + |
| 122 | + // Act |
| 123 | + _loggerConfiguration.WriteTo.MSSqlServerInternal( |
| 124 | + configSectionName: "TestConfigSectionName", |
| 125 | + connectionString: "TestConnectionString", |
| 126 | + tableName: "TestTableName", |
| 127 | + columnOptions: new ColumnOptions(), |
| 128 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 129 | + sinkFactory: sinkFactoryMock.Object); |
| 130 | + |
| 131 | + // Assert |
| 132 | + sinkFactoryMock.Verify(f => f.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>(), It.IsAny<TimeSpan>(), |
| 133 | + It.IsAny<IFormatProvider>(), It.IsAny<bool>(), configColumnOptions, It.IsAny<string>(), It.IsAny<ITextFormatter>()), Times.Once); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void MSSqlServerDoesNotCallApplySystemConfigurationGetColumnOptionsWhenNotUsingSystemConfig() |
| 138 | + { |
| 139 | + // Arrange |
| 140 | + var sinkFactoryMock = new Mock<IMSSqlServerSinkFactory>(); |
| 141 | + |
| 142 | + // Act |
| 143 | + _loggerConfiguration.WriteTo.MSSqlServerInternal( |
| 144 | + configSectionName: "TestConfigSectionName", |
| 145 | + connectionString: "TestConnectionString", |
| 146 | + tableName: "TestTableName", |
| 147 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 148 | + sinkFactory: sinkFactoryMock.Object); |
| 149 | + |
| 150 | + // Assert |
| 151 | + _applySystemConfigurationMock.Verify(c => c.ConfigureColumnOptions(It.IsAny<MSSqlServerConfigurationSection>(), It.IsAny<ColumnOptions>()), |
| 152 | + Times.Never); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void MSSqlServerCallsSinkFactoryWithSuppliedParameters() |
| 157 | + { |
| 158 | + // Arrange |
| 159 | + const string tableName = "TestTableName"; |
| 160 | + const int batchPostingLimit = 12345; |
| 161 | + const bool autoCreateSqlTable = true; |
| 162 | + const string schemaName = "TestSchemaName"; |
| 163 | + var columnOptions = new ColumnOptions(); |
| 164 | + var batchPeriod = new TimeSpan(0, 0, 44); |
| 165 | + var formatProviderMock = new Mock<IFormatProvider>(); |
| 166 | + var logEventFormatterMock = new Mock<ITextFormatter>(); |
| 167 | + |
| 168 | + var sinkFactoryMock = new Mock<IMSSqlServerSinkFactory>(); |
| 169 | + |
| 170 | + // Act |
| 171 | + _loggerConfiguration.WriteTo.MSSqlServerInternal( |
| 172 | + configSectionName: "TestConfigSectionName", |
| 173 | + connectionString: "TestConnectionString", |
| 174 | + tableName: tableName, |
| 175 | + columnOptions: columnOptions, |
| 176 | + batchPostingLimit: batchPostingLimit, |
| 177 | + period: batchPeriod, |
| 178 | + formatProvider: formatProviderMock.Object, |
| 179 | + autoCreateSqlTable: autoCreateSqlTable, |
| 180 | + schemaName: schemaName, |
| 181 | + logEventFormatter: logEventFormatterMock.Object, |
| 182 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 183 | + sinkFactory: sinkFactoryMock.Object); |
| 184 | + |
| 185 | + // Assert |
| 186 | + sinkFactoryMock.Verify(f => f.Create(It.IsAny<string>(), tableName, batchPostingLimit, batchPeriod, |
| 187 | + formatProviderMock.Object, autoCreateSqlTable, columnOptions, schemaName, logEventFormatterMock.Object), |
| 188 | + Times.Once); |
| 189 | + } |
| 190 | + |
| 191 | + [Fact] |
| 192 | + public void MSSqlServerAuditCallsApplySystemGetSectionWithSectionName() |
| 193 | + { |
| 194 | + // Arrange |
| 195 | + const string inputSectionName = "TestConfigSectionName"; |
| 196 | + var auditSinkFactoryMock = new Mock<IMSSqlServerAuditSinkFactory>(); |
| 197 | + |
| 198 | + // Act |
| 199 | + _loggerConfiguration.AuditTo.MSSqlServerInternal( |
| 200 | + configSectionName: inputSectionName, |
| 201 | + connectionString: "TestConnectionString", |
| 202 | + tableName: "TestTableName", |
| 203 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 204 | + auditSinkFactory: auditSinkFactoryMock.Object); |
| 205 | + |
| 206 | + // Assert |
| 207 | + _applySystemConfigurationMock.Verify(c => c.GetSinkConfigurationSection(inputSectionName), |
| 208 | + Times.Once); |
| 209 | + } |
| 210 | + |
| 211 | + [Fact] |
| 212 | + public void MSSqlServerAuditCallsApplySystemConfigurationGetConnectionString() |
| 213 | + { |
| 214 | + // Arrange |
| 215 | + const string inputConnectionString = "TestConnectionString"; |
| 216 | + _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny<string>())) |
| 217 | + .Returns(new MSSqlServerConfigurationSection()); |
| 218 | + var auditSinkFactoryMock = new Mock<IMSSqlServerAuditSinkFactory>(); |
| 219 | + |
| 220 | + // Act |
| 221 | + _loggerConfiguration.AuditTo.MSSqlServerInternal( |
| 222 | + configSectionName: "TestConfigSectionName", |
| 223 | + connectionString: inputConnectionString, |
| 224 | + tableName: "TestTableName", |
| 225 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 226 | + auditSinkFactory: auditSinkFactoryMock.Object); |
| 227 | + |
| 228 | + // Assert |
| 229 | + _applySystemConfigurationMock.Verify(c => c.GetConnectionString(inputConnectionString), |
| 230 | + Times.Once); |
| 231 | + } |
| 232 | + |
| 233 | + [Fact] |
| 234 | + public void MSSqlServerAuditCallsSinkFactoryWithConnectionStringFromSystemConfig() |
| 235 | + { |
| 236 | + // Arrange |
| 237 | + const string configConnectionString = "TestConnectionStringFromConfig"; |
| 238 | + _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny<string>())) |
| 239 | + .Returns(new MSSqlServerConfigurationSection()); |
| 240 | + _applySystemConfigurationMock.Setup(c => c.GetConnectionString(It.IsAny<string>())) |
| 241 | + .Returns(configConnectionString); |
| 242 | + var auditSinkFactoryMock = new Mock<IMSSqlServerAuditSinkFactory>(); |
| 243 | + |
| 244 | + // Act |
| 245 | + _loggerConfiguration.AuditTo.MSSqlServerInternal( |
| 246 | + configSectionName: "TestConfigSectionName", |
| 247 | + connectionString: "TestConnectionString", |
| 248 | + tableName: "TestTableName", |
| 249 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 250 | + auditSinkFactory: auditSinkFactoryMock.Object); |
| 251 | + |
| 252 | + // Assert |
| 253 | + auditSinkFactoryMock.Verify(f => f.Create(configConnectionString, It.IsAny<string>(), It.IsAny<IFormatProvider>(), |
| 254 | + It.IsAny<bool>(), It.IsAny<ColumnOptions>(), It.IsAny<string>(), It.IsAny<ITextFormatter>()), Times.Once); |
| 255 | + } |
| 256 | + |
| 257 | + [Fact] |
| 258 | + public void MSSqlServerAuditCallsApplySystemConfigurationGetColumnOptions() |
| 259 | + { |
| 260 | + // Arrange |
| 261 | + var columnOptions = new ColumnOptions(); |
| 262 | + var systemConfigSection = new MSSqlServerConfigurationSection(); |
| 263 | + _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny<string>())) |
| 264 | + .Returns(systemConfigSection); |
| 265 | + var auditSinkFactoryMock = new Mock<IMSSqlServerAuditSinkFactory>(); |
| 266 | + |
| 267 | + // Act |
| 268 | + _loggerConfiguration.AuditTo.MSSqlServerInternal( |
| 269 | + configSectionName: "TestConfigSectionName", |
| 270 | + connectionString: "TestConnectionString", |
| 271 | + tableName: "TestTableName", |
| 272 | + columnOptions: columnOptions, |
| 273 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 274 | + auditSinkFactory: auditSinkFactoryMock.Object); |
| 275 | + |
| 276 | + // Assert |
| 277 | + _applySystemConfigurationMock.Verify(c => c.ConfigureColumnOptions(systemConfigSection, columnOptions), |
| 278 | + Times.Once); |
| 279 | + } |
| 280 | + |
| 281 | + [Fact] |
| 282 | + public void MSSqlServerAuditCallsSinkFactoryWithColumnOptionsFromSystemConfig() |
| 283 | + { |
| 284 | + // Arrange |
| 285 | + var configColumnOptions = new ColumnOptions(); |
| 286 | + _applySystemConfigurationMock.Setup(c => c.GetSinkConfigurationSection(It.IsAny<string>())) |
| 287 | + .Returns(new MSSqlServerConfigurationSection()); |
| 288 | + _applySystemConfigurationMock.Setup(c => c.ConfigureColumnOptions(It.IsAny<MSSqlServerConfigurationSection>(), It.IsAny<ColumnOptions>())) |
| 289 | + .Returns(configColumnOptions); |
| 290 | + var auditSinkFactoryMock = new Mock<IMSSqlServerAuditSinkFactory>(); |
| 291 | + |
| 292 | + // Act |
| 293 | + _loggerConfiguration.AuditTo.MSSqlServerInternal( |
| 294 | + configSectionName: "TestConfigSectionName", |
| 295 | + connectionString: "TestConnectionString", |
| 296 | + tableName: "TestTableName", |
| 297 | + columnOptions: new ColumnOptions(), |
| 298 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 299 | + auditSinkFactory: auditSinkFactoryMock.Object); |
| 300 | + |
| 301 | + // Assert |
| 302 | + auditSinkFactoryMock.Verify(f => f.Create(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IFormatProvider>(), |
| 303 | + It.IsAny<bool>(), configColumnOptions, It.IsAny<string>(), It.IsAny<ITextFormatter>()), Times.Once); |
| 304 | + } |
| 305 | + |
| 306 | + [Fact] |
| 307 | + public void MSSqlServerAuditDoesNotCallApplySystemConfigurationGetColumnOptionsWhenNotUsingSystemConfig() |
| 308 | + { |
| 309 | + // Arrange |
| 310 | + var auditSinkFactoryMock = new Mock<IMSSqlServerAuditSinkFactory>(); |
| 311 | + |
| 312 | + // Act |
| 313 | + _loggerConfiguration.AuditTo.MSSqlServerInternal( |
| 314 | + configSectionName: "TestConfigSectionName", |
| 315 | + connectionString: "TestConnectionString", |
| 316 | + tableName: "TestTableName", |
| 317 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 318 | + auditSinkFactory: auditSinkFactoryMock.Object); |
| 319 | + |
| 320 | + // Assert |
| 321 | + _applySystemConfigurationMock.Verify(c => c.ConfigureColumnOptions(It.IsAny<MSSqlServerConfigurationSection>(), It.IsAny<ColumnOptions>()), |
| 322 | + Times.Never); |
| 323 | + } |
| 324 | + |
| 325 | + [Fact] |
| 326 | + public void MSSqlServerAuditCallsSinkFactoryWithSuppliedParameters() |
| 327 | + { |
| 328 | + // Arrange |
| 329 | + const string tableName = "TestTableName"; |
| 330 | + const bool autoCreateSqlTable = true; |
| 331 | + const string schemaName = "TestSchemaName"; |
| 332 | + var columnOptions = new ColumnOptions(); |
| 333 | + var batchPeriod = new TimeSpan(0, 0, 44); |
| 334 | + var formatProviderMock = new Mock<IFormatProvider>(); |
| 335 | + var logEventFormatterMock = new Mock<ITextFormatter>(); |
| 336 | + |
| 337 | + var auditSinkFactoryMock = new Mock<IMSSqlServerAuditSinkFactory>(); |
| 338 | + |
| 339 | + // Act |
| 340 | + _loggerConfiguration.AuditTo.MSSqlServerInternal( |
| 341 | + configSectionName: "TestConfigSectionName", |
| 342 | + connectionString: "TestConnectionString", |
| 343 | + tableName: tableName, |
| 344 | + columnOptions: columnOptions, |
| 345 | + formatProvider: formatProviderMock.Object, |
| 346 | + autoCreateSqlTable: autoCreateSqlTable, |
| 347 | + schemaName: schemaName, |
| 348 | + logEventFormatter: logEventFormatterMock.Object, |
| 349 | + applySystemConfiguration: _applySystemConfigurationMock.Object, |
| 350 | + auditSinkFactory: auditSinkFactoryMock.Object); |
| 351 | + |
| 352 | + // Assert |
| 353 | + auditSinkFactoryMock.Verify(f => f.Create(It.IsAny<string>(), tableName, |
| 354 | + formatProviderMock.Object, autoCreateSqlTable, columnOptions, schemaName, logEventFormatterMock.Object), |
| 355 | + Times.Once); |
| 356 | + } |
| 357 | + } |
| 358 | +} |
0 commit comments