using System.Reflection; using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management; namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Messages; /// /// Tests for , including the Commons-004 /// regression: GetCommandName and Resolve must be symmetric — every /// type for which GetCommandName yields a name must round-trip back to the /// same type via Resolve. /// public class ManagementCommandRegistryTests { private static IEnumerable RegisteredCommandTypes() => typeof(ManagementEnvelope).Assembly.GetTypes() .Where(t => t.Namespace == typeof(ManagementEnvelope).Namespace && t.Name.EndsWith("Command", StringComparison.Ordinal) && !t.IsAbstract); [Fact] public void GetCommandName_Resolve_RoundTrips_ForEveryRegisteredCommand() { foreach (var type in RegisteredCommandTypes()) { var name = ManagementCommandRegistry.GetCommandName(type); var resolved = ManagementCommandRegistry.Resolve(name); Assert.Equal(type, resolved); } } [Fact] public void Resolve_KnownCommand_ReturnsType() { var type = ManagementCommandRegistry.Resolve("CreateSite"); Assert.Equal(typeof(CreateSiteCommand), type); } [Fact] public void Resolve_UnknownCommand_ReturnsNull() { Assert.Null(ManagementCommandRegistry.Resolve("NoSuchCommand")); } [Fact] public void Resolve_IsCaseInsensitive() { Assert.Equal(typeof(CreateSiteCommand), ManagementCommandRegistry.Resolve("createsite")); } /// /// Commons-004: GetCommandName previously stripped a Command suffix /// from any type, producing names the registry cannot resolve. It must /// only return a name for a command type the registry actually contains. /// [Fact] public void GetCommandName_UnregisteredCommandType_Throws() { // A *Command type that is not in the Messages.Management namespace. Assert.Throws( () => ManagementCommandRegistry.GetCommandName(typeof(UnregisteredFakeCommand))); } [Fact] public void GetCommandName_NonCommandType_Throws() { Assert.Throws( () => ManagementCommandRegistry.GetCommandName(typeof(string))); } [Fact] public void GetCommandName_RegisteredCommand_ReturnsStrippedName() { Assert.Equal("CreateSite", ManagementCommandRegistry.GetCommandName(typeof(CreateSiteCommand))); } [Fact] public void Resolve_NativeAlarmSourceCommands_AllRegistered() { Assert.Equal(typeof(AddTemplateNativeAlarmSourceCommand), ManagementCommandRegistry.Resolve("AddTemplateNativeAlarmSource")); Assert.Equal(typeof(UpdateTemplateNativeAlarmSourceCommand), ManagementCommandRegistry.Resolve("UpdateTemplateNativeAlarmSource")); Assert.Equal(typeof(DeleteTemplateNativeAlarmSourceCommand), ManagementCommandRegistry.Resolve("DeleteTemplateNativeAlarmSource")); Assert.Equal(typeof(ListTemplateNativeAlarmSourcesCommand), ManagementCommandRegistry.Resolve("ListTemplateNativeAlarmSources")); Assert.Equal(typeof(SetInstanceNativeAlarmSourceOverrideCommand), ManagementCommandRegistry.Resolve("SetInstanceNativeAlarmSourceOverride")); Assert.Equal(typeof(DeleteInstanceNativeAlarmSourceOverrideCommand), ManagementCommandRegistry.Resolve("DeleteInstanceNativeAlarmSourceOverride")); Assert.Equal(typeof(ListInstanceNativeAlarmSourceOverridesCommand), ManagementCommandRegistry.Resolve("ListInstanceNativeAlarmSourceOverrides")); } /// A *Command record outside the Management namespace, for the negative test. private record UnregisteredFakeCommand(int Id); }