using System.Collections.Generic; using System.IO; using GameServiceWarden.Core.Module; using GameServiceWarden.ModuleFramework; using Xunit; using Xunit.Abstractions; [assembly: CollectionBehavior(DisableTestParallelization = true)] namespace GameServiceWarden.Core.Tests.Modules { [CollectionDefinition("Service")] public class ServiceManagerTest { public ServiceManagerTest(ITestOutputHelper output) { Logger.AddLogListener(new XUnitLogger(output)); } [Fact] public void CreateService_NewManager_NewServiceCreated() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "CreateService_NewManager_NewServiceCreated"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule(); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name); //Then Assert.True(FAKE_SERVICE_NAME.Equals(stubMonitor.GetLastDelta().service)); } [Fact] public void CreateService_OneService_ServiceDeleted() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "CreateService_OneService_ServiceDeleted"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule(); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name); serviceManager.DeleteService(FAKE_SERVICE_NAME); //Then Assert.True(stubMonitor.GetLastDelta().subtract); Assert.True(FAKE_SERVICE_NAME.Equals(stubMonitor.GetLastDelta().service)); } [Fact] public void GetServiceNames_MultipleServices_AllCorrectNames() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_PREFIX = "GetServiceNames_MultipleServices_AllCorrectNames_"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule(); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When for (int i = 0; i < 100; i++) { serviceManager.CreateService(FAKE_SERVICE_PREFIX + i, ASSEMBLY_NAME, stubServiceModule.Name); } //Then for (int i = 0; i < 100; i++) { Assert.Contains(FAKE_SERVICE_PREFIX + i, serviceManager.GetServiceNames()); } } [Fact] public void GetServiceOptions_ThreeOptionService_CorrectOptions() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "GetServiceOptions_ThreeOptionService_CorrectOptions"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule( new FakeServiceConfigurable("A"), new FakeServiceConfigurable("B"), new FakeServiceConfigurable("C") ); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name); //Then Assert.Contains("A", serviceManager.GetOptions()[FAKE_SERVICE_NAME].Keys); Assert.Contains("B", serviceManager.GetOptions()[FAKE_SERVICE_NAME].Keys); Assert.Contains("C", serviceManager.GetOptions()[FAKE_SERVICE_NAME].Keys); } [Fact] public void SetandGetServiceOptionValue_OneOption_OptionChanged() { const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "SetandGetServiceOptionValue_OneOption_OptionChanged"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule( new FakeServiceConfigurable("A") ); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name); serviceManager.SetServiceOptionValue(FAKE_SERVICE_NAME, "A", "Test"); //Then Assert.True("Test".Equals(serviceManager.GetOptions()[FAKE_SERVICE_NAME]["A"])); } [Fact] public void GetServiceState_NotRunning_ReturnsNotRunningState() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "GetServiceState_NotRunning_ReturnsNotRunningState"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule(); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name); //Then Assert.DoesNotContain(FAKE_SERVICE_NAME, serviceManager.GetRunningServiceNames()); } [Fact] public void StartService_NotStarted_SuccessfulStart() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "StartService_NotStarted_SuccessfulStart"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule(); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name); serviceManager.StartService(FAKE_SERVICE_NAME); //Then Assert.Contains(FAKE_SERVICE_NAME, serviceManager.GetRunningServiceNames()); serviceManager.StopService(FAKE_SERVICE_NAME); } [Fact] public void StopService_ServiceStartedThenStopped_StateUpdated() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "StopService_ServiceStartedThenStopped_StateUpdated"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule(); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name); serviceManager.StartService(FAKE_SERVICE_NAME); serviceManager.StopService(FAKE_SERVICE_NAME); //Then Assert.DoesNotContain(FAKE_SERVICE_NAME, serviceManager.GetRunningServiceNames()); } [Fact] public void ExecuteCommand_ServiceStarted_CommandLogged() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "ExecuteCommand_ServiceStarted_CommandLogged"; const string COMMAND = "TEST"; FakePersistence> stubPersistentModuleDictionary = new FakePersistence>(); FakePersistence> stubPersistentServiceDictionary = new FakePersistence>(); FakeServiceManagerMonitor stubMonitor = new FakeServiceManagerMonitor(); ServiceManager serviceManager = new ServiceManager(stubMonitor, stubPersistentServiceDictionary, stubPersistentModuleDictionary); Dictionary stubAssemblyModulesDictionary = new Dictionary(); IServiceModule stubServiceModule = new FakeServiceModule(); stubAssemblyModulesDictionary.Add(stubServiceModule.Name, stubServiceModule); stubPersistentModuleDictionary.AddToPersistence(ASSEMBLY_NAME, stubAssemblyModulesDictionary); //When serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubServiceModule.Name); serviceManager.StartService(FAKE_SERVICE_NAME); serviceManager.ExecuteCommand(FAKE_SERVICE_NAME, COMMAND); //Then using (MemoryStream mem = new MemoryStream(serviceManager.GetLogBuffers()[FAKE_SERVICE_NAME])) { using (StreamReader reader = new StreamReader(mem)) { Assert.Equal(COMMAND, reader.ReadLine()); } } serviceManager.StopService(FAKE_SERVICE_NAME); } } }