|
52 | 52 | using Microsoft.Extensions.Logging; |
53 | 53 | using Microsoft.IdentityModel.Tokens; |
54 | 54 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 55 | +using ModelContextProtocol.AspNetCore; |
55 | 56 | using Moq; |
56 | 57 | using Moq.Protected; |
57 | 58 | using Serilog; |
@@ -6893,6 +6894,145 @@ private static (string RootConfigPath, FileSystemRuntimeConfigLoader ValidateLoa |
6893 | 6894 | return (rootConfigPath, new FileSystemRuntimeConfigLoader(fileSystem), fileSystem, tempDir); |
6894 | 6895 | } |
6895 | 6896 |
|
| 6897 | + [DataTestMethod] |
| 6898 | + [DataRow(true, false)] |
| 6899 | + [DataRow(false, false)] |
| 6900 | + [DataRow(true, true)] |
| 6901 | + [DataRow(false, true)] |
| 6902 | + [TestCategory(TestCategory.MSSQL)] |
| 6903 | + public async Task LinkedConfigMutations_UseChildDataSource(bool rootHasDataSource, bool useMcp) |
| 6904 | + { |
| 6905 | + string connectionString = GetConnectionStringFromEnvironmentConfig(environment: TestCategory.MSSQL); |
| 6906 | + string tempDir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "dab-linked-writes-" + Guid.NewGuid().ToString("N")); |
| 6907 | + string name = "linked-write-" + Guid.NewGuid().ToString("N"); |
| 6908 | + using SqlConnection connection = new(connectionString); |
| 6909 | + await connection.OpenAsync(); |
| 6910 | + Directory.CreateDirectory(tempDir); |
| 6911 | + try |
| 6912 | + { |
| 6913 | + string rootPath = System.IO.Path.Combine(tempDir, "root.json"); |
| 6914 | + string childPath = System.IO.Path.Combine(tempDir, "child.json"); |
| 6915 | + JsonObject child = JsonNode.Parse(""" |
| 6916 | + { |
| 6917 | + "data-source": { "database-type": "mssql", "connection-string": "" }, |
| 6918 | + "entities": { |
| 6919 | + "LinkedPublisher": { |
| 6920 | + "source": "dbo.publishers", |
| 6921 | + "rest": true, |
| 6922 | + "graphql": false, |
| 6923 | + "permissions": [{ "role": "anonymous", "actions": ["*"] }] |
| 6924 | + } |
| 6925 | + } |
| 6926 | + } |
| 6927 | + """)!.AsObject(); |
| 6928 | + child["data-source"]!["connection-string"] = connectionString; |
| 6929 | + JsonObject root = JsonNode.Parse(""" |
| 6930 | + { |
| 6931 | + "runtime": { |
| 6932 | + "rest": { "enabled": true }, |
| 6933 | + "graphql": { "enabled": false }, |
| 6934 | + "mcp": { "enabled": true }, |
| 6935 | + "host": { "mode": "development" } |
| 6936 | + }, |
| 6937 | + "entities": {} |
| 6938 | + } |
| 6939 | + """)!.AsObject(); |
| 6940 | + root["data-source-files"] = new JsonArray(childPath); |
| 6941 | + if (rootHasDataSource) |
| 6942 | + { |
| 6943 | + root["data-source"] = child["data-source"]!.DeepClone(); |
| 6944 | + } |
| 6945 | + |
| 6946 | + File.WriteAllText(childPath, child.ToJsonString()); |
| 6947 | + File.WriteAllText(rootPath, root.ToJsonString()); |
| 6948 | + using TestServer server = new(Program.CreateWebHostBuilder(new[] { $"--ConfigFileName={rootPath}" }) |
| 6949 | + .ConfigureServices(services => services.PostConfigure<HttpServerTransportOptions>(options => options.Stateless = true))); |
| 6950 | + using HttpClient client = server.CreateClient(); |
| 6951 | + client.Timeout = TimeSpan.FromSeconds(30); |
| 6952 | + if (useMcp) |
| 6953 | + { |
| 6954 | + client.DefaultRequestHeaders.Accept.ParseAdd("application/json"); |
| 6955 | + client.DefaultRequestHeaders.Accept.ParseAdd("text/event-stream"); |
| 6956 | + client.DefaultRequestHeaders.Add("MCP-Protocol-Version", "2025-11-25"); |
| 6957 | + await SendMcpRequestAsync(client, 1, "initialize", new |
| 6958 | + { |
| 6959 | + protocolVersion = "2025-11-25", |
| 6960 | + capabilities = new { }, |
| 6961 | + clientInfo = new { name = "linked-config-test", version = "1" } |
| 6962 | + }); |
| 6963 | + await SendMcpRequestAsync(client, 2, "tools/call", new |
| 6964 | + { |
| 6965 | + name = "create_record", |
| 6966 | + arguments = new { entity = "LinkedPublisher", data = new { name } } |
| 6967 | + }); |
| 6968 | + using SqlCommand lookup = connection.CreateCommand(); |
| 6969 | + lookup.CommandText = "SELECT id FROM dbo.publishers WHERE name = @name"; |
| 6970 | + lookup.Parameters.AddWithValue("@name", name); |
| 6971 | + object insertedId = await lookup.ExecuteScalarAsync(); |
| 6972 | + Assert.IsNotNull(insertedId); |
| 6973 | + int publisherId = (int)insertedId; |
| 6974 | + await SendMcpRequestAsync(client, 3, "tools/call", new |
| 6975 | + { |
| 6976 | + name = "update_record", |
| 6977 | + arguments = new { entity = "LinkedPublisher", keys = new { id = publisherId }, fields = new { name = name + "-patch" } } |
| 6978 | + }); |
| 6979 | + lookup.Parameters["@name"].Value = name + "-patch"; |
| 6980 | + Assert.AreEqual(publisherId, await lookup.ExecuteScalarAsync()); |
| 6981 | + await SendMcpRequestAsync(client, 4, "tools/call", new |
| 6982 | + { |
| 6983 | + name = "delete_record", |
| 6984 | + arguments = new { entity = "LinkedPublisher", keys = new { id = publisherId } } |
| 6985 | + }); |
| 6986 | + Assert.IsNull(await lookup.ExecuteScalarAsync()); |
| 6987 | + return; |
| 6988 | + } |
| 6989 | + |
| 6990 | + using HttpResponseMessage created = await client.PostAsJsonAsync("/api/LinkedPublisher", new { name }); |
| 6991 | + Assert.AreEqual(HttpStatusCode.Created, created.StatusCode, await created.Content.ReadAsStringAsync()); |
| 6992 | + Assert.IsNotNull(created.Headers.Location); |
| 6993 | + using JsonDocument createdBody = JsonDocument.Parse(await created.Content.ReadAsStringAsync()); |
| 6994 | + int id = createdBody.RootElement.GetProperty("value")[0].GetProperty("id").GetInt32(); |
| 6995 | + StringAssert.EndsWith(created.Headers.Location.ToString(), $"/id/{id}"); |
| 6996 | + string itemPath = $"/api/LinkedPublisher/id/{id}"; |
| 6997 | + |
| 6998 | + using HttpResponseMessage replaced = await client.PutAsJsonAsync(itemPath, new { name = name + "-put" }); |
| 6999 | + Assert.AreEqual(HttpStatusCode.OK, replaced.StatusCode, await replaced.Content.ReadAsStringAsync()); |
| 7000 | + using JsonDocument replacedBody = JsonDocument.Parse(await replaced.Content.ReadAsStringAsync()); |
| 7001 | + Assert.AreEqual(name + "-put", replacedBody.RootElement.GetProperty("value")[0].GetProperty("name").GetString()); |
| 7002 | + using HttpResponseMessage patched = await client.PatchAsJsonAsync(itemPath, new { name = name + "-patch" }); |
| 7003 | + Assert.AreEqual(HttpStatusCode.OK, patched.StatusCode, await patched.Content.ReadAsStringAsync()); |
| 7004 | + using JsonDocument patchedBody = JsonDocument.Parse(await patched.Content.ReadAsStringAsync()); |
| 7005 | + Assert.AreEqual(name + "-patch", patchedBody.RootElement.GetProperty("value")[0].GetProperty("name").GetString()); |
| 7006 | + using HttpResponseMessage deleted = await client.DeleteAsync(itemPath); |
| 7007 | + Assert.AreEqual(HttpStatusCode.NoContent, deleted.StatusCode, await deleted.Content.ReadAsStringAsync()); |
| 7008 | + } |
| 7009 | + finally |
| 7010 | + { |
| 7011 | + using SqlCommand cleanup = connection.CreateCommand(); |
| 7012 | + cleanup.CommandText = "DELETE FROM dbo.publishers WHERE name IN (@name, @putName, @patchName)"; |
| 7013 | + cleanup.Parameters.AddWithValue("@name", name); |
| 7014 | + cleanup.Parameters.AddWithValue("@putName", name + "-put"); |
| 7015 | + cleanup.Parameters.AddWithValue("@patchName", name + "-patch"); |
| 7016 | + await cleanup.ExecuteNonQueryAsync(); |
| 7017 | + Directory.Delete(tempDir, recursive: true); |
| 7018 | + } |
| 7019 | + } |
| 7020 | + |
| 7021 | + private static async Task SendMcpRequestAsync(HttpClient client, int id, string method, object parameters) |
| 7022 | + { |
| 7023 | + using HttpResponseMessage response = await client.PostAsJsonAsync("/mcp", new { jsonrpc = "2.0", id, method, @params = parameters }); |
| 7024 | + string payload = await response.Content.ReadAsStringAsync(); |
| 7025 | + Assert.IsTrue(response.IsSuccessStatusCode, $"HTTP {response.StatusCode}: {payload}"); |
| 7026 | + if (response.Content.Headers.ContentType?.MediaType == "text/event-stream") |
| 7027 | + { |
| 7028 | + payload = payload.Split('\n').Last(line => line.StartsWith("data:", StringComparison.Ordinal))[5..].Trim(); |
| 7029 | + } |
| 7030 | + |
| 7031 | + using JsonDocument document = JsonDocument.Parse(payload); |
| 7032 | + Assert.IsTrue(document.RootElement.TryGetProperty("result", out JsonElement result), payload); |
| 7033 | + Assert.IsFalse(result.TryGetProperty("isError", out JsonElement error) && error.GetBoolean(), payload); |
| 7034 | + } |
| 7035 | + |
6896 | 7036 | /// <summary> |
6897 | 7037 | /// Helper: builds an autoentity map containing a single definition whose include pattern |
6898 | 7038 | /// controls which tables it resolves against the target database. |
|
0 commit comments