diff --git a/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/InstanceCommand.cs b/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/InstanceCommand.cs index 4e865e8..5f3d6e9 100644 --- a/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/InstanceCommand.cs +++ b/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/InstanceCommand.cs @@ -31,6 +31,17 @@ public abstract class InstanceCommand : TenantedCommand if (e.Context.Request.PathParts.Length < 5) return true; + string tenantName = e.Context.Request.PathParts[2]; + TenantHandle th = oms.GetTenantByName(tenantName); + if (th != TenantHandle.Empty) + { + oms.SelectTenant(th); + } + else + { + Console.Error.WriteLine("failed to select tenant '{0}'", tenantName); + } + string iid = e.Context.Request.PathParts[4]; if (oms.TryParseInstanceRef(iid, out InstanceHandle ih)) { diff --git a/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/TenantsListCommand.cs b/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/TenantsListCommand.cs index bfbc8c4..bd7304d 100644 --- a/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/TenantsListCommand.cs +++ b/mocha-dotnet/src/lib/Mocha.Core.UI.Server/Commands/TenantsListCommand.cs @@ -48,19 +48,29 @@ public class TenantsListCommand : OmsServerCommand } else if (e.Context.Request.PathParts.Length > 3) { - if (e.Context.Request.PathParts[3] == "instances") + if (e.Context.Request.PathParts[3] == "create") + { + string tenantName = e.Context.Request.PathParts[2]; + TenantHandle th = oms.CreateTenant(tenantName); + + JsonObject obj = new JsonObject(); + obj.Add("result", JsonValue.Create("success")); + obj.Add("tenantHandle", th.ToString()); + sw.Write(obj.ToJsonString()); + } + else if (e.Context.Request.PathParts[3] == "instances") { if (e.Context.Request.PathParts.Length > 4) { if (e.Context.Request.PathParts[4] == "create") { - + } else if (e.Context.Request.PathParts.Length >= 5) { // oms.TryParseInstanceRef(e.Context.Request.PathParts[4], out InstanceHandle h); - + if (e.Context.Request.PathParts.Length >= 6) { string command = e.Context.Request.PathParts[5]; @@ -69,7 +79,7 @@ public class TenantsListCommand : OmsServerCommand /* InstanceHandle instParent = oms.GetParentClass(ih); List relatedTasks = new List(); - + $instInstance = $this->getInstanceByGlobalIdentifier(KnownClassGuids::Instance); $relatedTasks0 = $this->getRelatedInstances($instInstance, KnownRelationshipGuids::Class__has_related__Task); foreach ($relatedTasks0 as $task) @@ -103,7 +113,7 @@ public class TenantsListCommand : OmsServerCommand } else { - + } } } diff --git a/mocha-dotnet/tests/Mocha.Core.Tests/TenantedTests.cs b/mocha-dotnet/tests/Mocha.Core.Tests/TenantedTests.cs index ea038d8..5eaf1ab 100644 --- a/mocha-dotnet/tests/Mocha.Core.Tests/TenantedTests.cs +++ b/mocha-dotnet/tests/Mocha.Core.Tests/TenantedTests.cs @@ -24,12 +24,12 @@ namespace Mocha.Core.Tests; public class TenantedTests : OmsTestsBase { - protected override void AfterSetup() - { - base.AfterSetup(); + protected override void AfterSetup() + { + base.AfterSetup(); Oms.CreateTenant("omstest"); - } + } [Test] public void Create_Class_On_First_Tenant_Doesnt_Show_Up_On_Second_Tenant() @@ -53,7 +53,7 @@ public class TenantedTests : OmsTestsBase Oms.SelectTenant(Oms.GetTenantByName("super")); Class cls = Oms.GetInstance(TEST_CLASS_GUID); - + Oms.SelectTenant(Oms.GetTenantByName("omstest")); Class cls2 = Oms.GetInstance(TEST_CLASS_GUID); @@ -77,4 +77,18 @@ public class TenantedTests : OmsTestsBase InstanceHandle att2 = Oms.GetInstance(KnownAttributeGuids.Text.ContentType); Assert.That(val, Is.Not.EqualTo("Test Value")); } + + [Test] + public void Get_Instance_After_Create_Tenant_Doesnt_Trash_Super_Tenant() + { + Oms.SelectTenant(Oms.GetTenantByName("super")); + + Class cls = Oms.GetInstance(TEST_CLASS_GUID); + Assert.That(cls, Is.Not.Null); + + Oms.CreateTenant("dummy"); + + Class cls2 = Oms.GetInstance(TEST_CLASS_GUID); + Assert.That(cls2, Is.Not.Null); + } } \ No newline at end of file diff --git a/mocha-dotnet/tests/Mocha.Oms.Server.Tests/RemoteTests.cs b/mocha-dotnet/tests/Mocha.Oms.Server.Tests/RemoteTests.cs index b73f924..b21996d 100644 --- a/mocha-dotnet/tests/Mocha.Oms.Server.Tests/RemoteTests.cs +++ b/mocha-dotnet/tests/Mocha.Oms.Server.Tests/RemoteTests.cs @@ -63,6 +63,55 @@ public class RemoteTests Assert.That(json["tenants"].AsArray()[0].ToString(), Is.EqualTo("super")); } + /// + /// Checks to make sure that the class Class [1$1] on the super tenant exists. + /// + /// + [Test] + public async Task Get_Class_Instance() + { + HttpClient client = new HttpClient(); + HttpResponseMessage resp = client.Send(new HttpRequestMessage(HttpMethod.Get, BuildUrl("/tenants/super/instances/1$1"))); + + Assert.That(resp.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK)); + Assert.That(resp.Content.Headers.ContentType.ToString(), Is.EqualTo("application/json")); + + string content = await resp.Content.ReadAsStringAsync(); + + JsonObject json = JsonNode.Parse(content) as JsonObject; + Assert.That(json["globalIdentifier"].ToString(), Is.EqualTo(KnownInstanceGuids.Classes.Class.ToString())); + } + + /// + /// Checks to make sure that the class Class [1$1] on the super tenant exists after a new tenant is created. + /// + /// + [Test] + public async Task Get_Class_Instance_After_Create_Tenant() + { + // This test was failing not because the class did not exist, but because after the call to create a tenant, + // the call to request instance details WAS NOT SELECTING THE PROPER TENANT. In other words, + // /tenants/***/instances... would NEVER select the specified tenant. In this case, it was using the tenant + // already selected from the call to create, e.g. 'contoso1', rather than the tenant we requested in the API + // call, 'super'. + // + // This API call was ALWAYS broken! + HttpClient client = new HttpClient(); + + HttpResponseMessage resp1 = client.Send(new HttpRequestMessage(HttpMethod.Get, BuildUrl("/tenants/contoso1/create"))); + string cvm = await resp1.Content.ReadAsStringAsync(); + + HttpResponseMessage resp = client.Send(new HttpRequestMessage(HttpMethod.Get, BuildUrl("/tenants/super/instances/1$1"))); + + Assert.That(resp.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK)); + Assert.That(resp.Content.Headers.ContentType.ToString(), Is.EqualTo("application/json")); + + string content = await resp.Content.ReadAsStringAsync(); + + JsonObject json = JsonNode.Parse(content) as JsonObject; + Assert.That(json["globalIdentifier"].ToString(), Is.EqualTo(KnownInstanceGuids.Classes.Class.ToString())); + } + [Test] public async Task Instance_Json_Test() {