-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathHealthCheckUtilitiesUnitTests.cs
More file actions
195 lines (166 loc) · 7.22 KB
/
HealthCheckUtilitiesUnitTests.cs
File metadata and controls
195 lines (166 loc) · 7.22 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
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Service.HealthCheck;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace Azure.DataApiBuilder.Service.Tests.UnitTests
{
/// <summary>
/// Unit tests for health check utility methods.
/// </summary>
[TestClass]
public class HealthCheckUtilitiesUnitTests
{
/// <summary>
/// Tests that PostgreSQL connection strings are properly normalized.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_PostgreSQL_Success()
{
// Arrange
string connectionString = "Host=localhost;Port=5432;Database=testdb;Username=testuser;Password=testpass";
DatabaseType dbType = DatabaseType.PostgreSQL;
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType);
// Assert
Assert.IsNotNull(result);
Assert.IsTrue(result.Contains("Host=localhost"));
Assert.IsTrue(result.Contains("Database=testdb"));
}
/// <summary>
/// Tests that MSSQL connection strings are properly normalized.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_MSSQL_Success()
{
// Arrange
string connectionString = "Server=localhost;Database=testdb;User Id=testuser;Password=testpass";
DatabaseType dbType = DatabaseType.MSSQL;
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType);
// Assert
Assert.IsNotNull(result);
Assert.IsTrue(result.Contains("Data Source=localhost"));
Assert.IsTrue(result.Contains("Initial Catalog=testdb"));
}
/// <summary>
/// Tests that DWSQL connection strings are properly normalized.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_DWSQL_Success()
{
// Arrange
string connectionString = "Server=localhost;Database=testdb;User Id=testuser;Password=testpass";
DatabaseType dbType = DatabaseType.DWSQL;
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType);
// Assert
Assert.IsNotNull(result);
Assert.IsTrue(result.Contains("Data Source=localhost"));
Assert.IsTrue(result.Contains("Initial Catalog=testdb"));
}
/// <summary>
/// Tests that MySQL connection strings are properly normalized.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_MySQL_Success()
{
// Arrange
string connectionString = "Server=localhost;Port=3306;Database=testdb;Uid=testuser;Pwd=testpass";
DatabaseType dbType = DatabaseType.MySQL;
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType);
// Assert
Assert.IsNotNull(result);
Assert.IsTrue(result.Contains("Server=localhost"));
Assert.IsTrue(result.Contains("Database=testdb"));
}
/// <summary>
/// Tests that unsupported database types return the original connection string.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_UnsupportedType_ReturnsOriginal()
{
// Arrange
string connectionString = "AccountEndpoint=https://test.documents.azure.com:443/;AccountKey=test";
DatabaseType dbType = DatabaseType.CosmosDB_NoSQL;
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType);
// Assert
Assert.AreEqual(connectionString, result);
}
/// <summary>
/// Tests that malformed connection strings are handled gracefully.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_MalformedString_ReturnsOriginalAndLogs()
{
// Arrange
string malformedConnectionString = "InvalidConnectionString;NoEquals";
DatabaseType dbType = DatabaseType.PostgreSQL;
Mock<ILogger> mockLogger = new Mock<ILogger>();
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(malformedConnectionString, dbType, mockLogger.Object);
// Assert
Assert.AreEqual(malformedConnectionString, result);
mockLogger.Verify(
x => x.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => true),
It.IsAny<Exception>(),
It.Is<Func<It.IsAnyType, Exception?, string>>((v, t) => true)),
Times.Once);
}
/// <summary>
/// Tests that null logger is handled gracefully.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_MalformedString_NullLogger_ReturnsOriginal()
{
// Arrange
string malformedConnectionString = "InvalidConnectionString;NoEquals";
DatabaseType dbType = DatabaseType.MSSQL;
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(malformedConnectionString, dbType, null);
// Assert
Assert.AreEqual(malformedConnectionString, result);
}
/// <summary>
/// Tests that PostgreSQL connection strings with lowercase keywords are normalized correctly.
/// This is the specific bug that was reported - lowercase 'host' was not supported.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_PostgreSQL_LowercaseKeywords_Success()
{
// Arrange
string connectionString = "host=localhost;port=5432;database=mydb;username=myuser;password=mypass";
DatabaseType dbType = DatabaseType.PostgreSQL;
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType);
// Assert
Assert.IsNotNull(result);
// NpgsqlConnectionStringBuilder should normalize lowercase keywords to proper format
Assert.IsTrue(result.Contains("Host=localhost") || result.Contains("host=localhost"));
Assert.IsTrue(result.Contains("Database=mydb") || result.Contains("database=mydb"));
}
/// <summary>
/// Tests that empty connection strings are handled gracefully.
/// </summary>
[TestMethod]
public void NormalizeConnectionString_EmptyString_ReturnsEmpty()
{
// Arrange
string connectionString = string.Empty;
DatabaseType dbType = DatabaseType.PostgreSQL;
// Act
string result = HealthCheck.Utilities.NormalizeConnectionString(connectionString, dbType);
// Assert
Assert.AreEqual(string.Empty, result);
}
}
}