using System.IO; using GameServiceWarden.Core.Games; using GameServiceWarden.ModuleAPI; using Xunit; namespace GameServiceWarden.Core.Tests.Modules.Games { public class GameServiceManagerTest { [Fact] public void CreateService_NewManager_NewServiceCreated() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); //Then Assert.Contains(FAKE_SERVICE_NAME, serviceManager.GetServiceNames()); } [Fact] public void CreateService_OneService_ServiceDeleted() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); serviceManager.DeleteService(FAKE_SERVICE_NAME); //Then Assert.DoesNotContain(FAKE_SERVICE_NAME, serviceManager.GetServiceNames()); } [Fact] public void GetServiceNames_MultipleServices_AllCorrectNames() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string FAKE_SERVICE_PREFIX = "FakeService_"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); for (int i = 0; i < 100; i++) { serviceManager.CreateService(FAKE_SERVICE_PREFIX + i, ASSEMBLY_NAME, stubGameServiceModule.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 SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule( new FakeGameConfigurable("A"), new FakeGameConfigurable("B"), new FakeGameConfigurable("C") ); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); //Then Assert.Contains("A", serviceManager.GetServiceOptions(SERVICE_NAME)); Assert.Contains("B", serviceManager.GetServiceOptions(SERVICE_NAME)); Assert.Contains("C", serviceManager.GetServiceOptions(SERVICE_NAME)); } [Fact] public void SetandGetServiceOptionValue_OneOption_OptionChanged() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule( new FakeGameConfigurable("A") ); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); serviceManager.SetServiceOptionValue(SERVICE_NAME, "A", "Test"); //Then Assert.Equal("Test", serviceManager.GetServiceOptionValue(SERVICE_NAME, "A")); } [Fact] public void GetServiceState_NotRunning_ReturnsNotRunningState() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); //Then Assert.Equal(ServiceState.Stopped, serviceManager.GetServiceState(SERVICE_NAME)); } [Fact] public void GetServiceState_Running_ReturnsNotRunningState() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); serviceManager.StartService(SERVICE_NAME); //Then Assert.Equal(ServiceState.Running, serviceManager.GetServiceState(SERVICE_NAME)); } [Fact] public void StartService_NotStarted_SuccessfulStart() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); serviceManager.StartService(SERVICE_NAME); //Then Assert.Equal(ServiceState.Running, serviceManager.GetServiceState(SERVICE_NAME)); } [Fact] public void StopService_Stopped_StateUpdated() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); serviceManager.StartService(SERVICE_NAME); serviceManager.StopService(SERVICE_NAME); //Then Assert.Equal(ServiceState.Stopped, serviceManager.GetServiceState(SERVICE_NAME)); } [Fact] public void ExecuteCommand_ServiceStarted_CommandLogged() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); serviceManager.StartService(SERVICE_NAME); serviceManager.ExecuteCommand(SERVICE_NAME, "Test"); //Then Stream stream = serviceManager.GetServiceConsoleStream(SERVICE_NAME); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Assert.Equal("Test", reader.ReadLine()); } } [Fact] public void GetServiceConsoleStream_ServiceStopped_ExceptionThrown() { //Given const string ASSEMBLY_NAME = "FakeAssembly"; const string SERVICE_NAME = "FakeService"; FakePersistentDictionary stubPersistentModuleDictionary = new FakePersistentDictionary(); FakePersistentDictionary stubPersistentServiceDictionary = new FakePersistentDictionary(); GameServiceManager serviceManager = new GameServiceManager(stubPersistentServiceDictionary, stubPersistentModuleDictionary); IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(); //When stubPersistentModuleDictionary.Add(ASSEMBLY_NAME + Path.DirectorySeparatorChar + stubGameServiceModule.Name, stubGameServiceModule); serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name); //Then Assert.Null(serviceManager.GetServiceConsoleStream(SERVICE_NAME)); } } }