Refactored naming and directory layout of project.

This commit is contained in:
2020-12-28 00:43:02 -06:00
parent 334fd37dc6
commit 0cf2335aa7
28 changed files with 324 additions and 183 deletions

View File

@@ -0,0 +1,26 @@
using GameServiceWarden.ModuleAPI;
namespace GameServiceWarden.Core.Tests.Modules.Games
{
public class FakeGameConfigurable : IGameConfigurable
{
private string value;
public string OptionName { get; private set; }
public FakeGameConfigurable(string optionName)
{
this.OptionName = optionName;
}
public string GetValue()
{
return value;
}
public bool SetValue(string value)
{
this.value = value;
return true;
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using GameServiceWarden.ModuleAPI;
namespace GameServiceWarden.Core.Tests.Modules.Games
{
public class FakeGameService : IGameService
{
public IReadOnlyCollection<IGameConfigurable> Configurables { get; set; }
public event EventHandler<ServiceState> StateChangeEvent;
public ServiceState CurrentState { get; private set; } = ServiceState.Stopped;
private TextWriter consoleStream;
public FakeGameService(params IGameConfigurable[] configurables)
{
HashSet<IGameConfigurable> modifiable = new HashSet<IGameConfigurable>();
foreach (IGameConfigurable configurable in configurables)
{
modifiable.Add(configurable);
}
this.Configurables = modifiable;
}
public void ElegantShutdown()
{
CurrentState = ServiceState.Stopped;
StateChangeEvent?.Invoke(this, CurrentState);
}
public void ExecuteCommand(string command)
{
consoleStream.WriteLine(command);
consoleStream.Flush();
}
public void InitializeService(TextWriter stream)
{
CurrentState = ServiceState.Running;
StateChangeEvent?.Invoke(this, CurrentState);
this.consoleStream = stream;
}
}
}

View File

@@ -0,0 +1,25 @@
using System.Collections.Generic;
using GameServiceWarden.ModuleAPI;
namespace GameServiceWarden.Core.Tests.Modules.Games
{
public class FakeGameServiceModule : IGameServiceModule
{
public string Name => "FakeModule";
public string Description => "A fake module for testing.";
private IGameConfigurable[] configurables;
public FakeGameServiceModule(params IGameConfigurable[] configurables)
{
this.configurables = configurables;
}
public IEnumerable<string> Authors { get; private set; } = new string[] { "FakeAuthor", "FakeAuthor2" };
public IGameService CreateGameService()
{
return new FakeGameService(configurables);
}
}
}

View File

@@ -0,0 +1,155 @@
using System.Collections.Generic;
using System.IO;
using GameServiceWarden.Core.Games;
using GameServiceWarden.ModuleAPI;
using Xunit;
namespace GameServiceWarden.Core.Tests.Modules.Games
{
// Testing convention from: https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices
// Fakes are generic test objects,
// mocks are the objects being asserted upon,
// stubs are objects used as part of the test.
public class GameServiceInfoTest
{
//MethodTested_ScenarioTested_ExpectedBehavior
[Fact]
public void Start_FromStopped_StateIsRunning()
{
//Arrange, Act, Assert
IGameService stubGameService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubGameService, "FakeModule", "FakeAssembly");
serviceInfo.Start();
Assert.Equal(ServiceState.Running, serviceInfo.GetServiceState());
serviceInfo.Dispose();
}
[Fact]
public void Stop_FromStart_Stopped()
{
IGameService stubService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", "FakeAssembly");
serviceInfo.Start();
serviceInfo.Stop();
Assert.Equal(ServiceState.Stopped, serviceInfo.GetServiceState());
serviceInfo.Dispose();
}
[Fact]
public void GetConfigurableOptions_ServiceStopped_ReturnsConfigurables()
{
//Given
FakeGameService stubService = new FakeGameService();
FakeGameConfigurable stubConfigurable = new FakeGameConfigurable("Option");
HashSet<IGameConfigurable> configurables = new HashSet<IGameConfigurable>();
configurables.Add(stubConfigurable);
stubService.Configurables = configurables;
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", "FakeAssembly");
//Then
Assert.Contains<string>(stubConfigurable.OptionName, serviceInfo.GetConfigurableOptions());
serviceInfo.Dispose();
}
[Fact]
public void SetAndGetConfigurationValue_ServiceStopped_AppropriateValueReturned()
{
//Given
FakeGameService stubService = new FakeGameService();
FakeGameConfigurable stubConfigurable = new FakeGameConfigurable("Option");
HashSet<IGameConfigurable> configurables = new HashSet<IGameConfigurable>();
configurables.Add(stubConfigurable);
stubService.Configurables = configurables;
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", "FakeAssembly");
//When
serviceInfo.SetConfigurableValue(stubConfigurable.OptionName, "success");
//Then
Assert.Equal<string>("success", serviceInfo.GetConfigurableValue(stubConfigurable.OptionName));
}
[Fact]
public void GetServiceState_ServiceNotStarted_ReturnsStoppedState()
{
//Given
IGameService stubService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", "FakeAssembly");
//Then
Assert.Equal(ServiceState.Stopped, serviceInfo.GetServiceState());
serviceInfo.Dispose();
}
[Fact]
public void GetServiceState_ServiceStarted_ReturnsRunningState()
{
//Given
IGameService stubService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", "FakeAssembly");
//When
serviceInfo.Start();
//Then
Assert.Equal(ServiceState.Running, serviceInfo.GetServiceState());
serviceInfo.Dispose();
}
[Fact]
public void GetModuleName_ServiceNotStarted_ReturnsSetName()
{
//Given
const string MODULE_NAME = "FakeModule";
IGameService stubService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, MODULE_NAME, "FakeAssembly");
//Then
Assert.Equal(MODULE_NAME, serviceInfo.GetModuleName());
serviceInfo.Dispose();
}
[Fact]
public void GetAssemblyName_ServiceNotStarted_ReturnsSetAssemblyName()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
IGameService stubService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", ASSEMBLY_NAME);
//Then
Assert.Equal(ASSEMBLY_NAME, serviceInfo.GetAssemblyName());
serviceInfo.Dispose();
}
[Fact]
public void SetAndGetServiceName_ServiceNotStartedSingleThread_ServiceNameUpdated()
{
//Given
const string SERVICE_NAME = "Service";
IGameService stubService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", "FakeAssemblyName");
//When
serviceInfo.ServiceName = SERVICE_NAME;
//Then
Assert.Equal(SERVICE_NAME, serviceInfo.ServiceName);
serviceInfo.Dispose();
}
[Fact]
public void ServiceConsoleStream_ServiceNotStarted_NullReturned()
{
//Given
IGameService stubService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", "FakeAssembly");
//Then
Assert.Null(serviceInfo.ServiceConsoleStream);
serviceInfo.Dispose();
}
[Fact]
public void ServiceConsoleStream_ServiceStarted_StreamReturned()
{
//Given
IGameService stubService = new FakeGameService();
GameServiceInfo serviceInfo = new GameServiceInfo(stubService, "FakeModule", "FakeAssembly");
//When
serviceInfo.Start();
//Then
Assert.IsAssignableFrom<Stream>(serviceInfo.ServiceConsoleStream);
serviceInfo.Dispose();
}
}
}

View File

@@ -0,0 +1,230 @@
using System.IO;
using GameServiceWarden.Core.Games;
using GameServiceWarden.ModuleAPI;
using Xunit;
namespace GameServiceWarden.Core.Tests.Modules.Games
{
public class GameServiceManagerTest
{
[Fact]
public void AddModule_NewManager_SuccessfulAddition()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
//Then
Assert.Contains<string>(ASSEMBLY_NAME, serviceManager.GetAssemblyNames());
Assert.Contains<string>(stubGameServiceModule.Name, serviceManager.GetModuleNames(ASSEMBLY_NAME));
}
[Fact]
public void RemoveModule_NewManager_SuccessfulRemoval()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.RemoveModule(ASSEMBLY_NAME, stubGameServiceModule.Name);
//Then
Assert.DoesNotContain<string>(ASSEMBLY_NAME, serviceManager.GetAssemblyNames());
}
[Fact]
public void CreateService_NewManager_NewServiceCreated()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
//When
serviceManager.CreateService("FakeService", ASSEMBLY_NAME, stubGameServiceModule.Name);
//Then
Assert.Contains<string>(FAKE_SERVICE_NAME, serviceManager.GetServiceNames());
}
[Fact]
public void CreateService_OneService_ServiceDeleted()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.CreateService(FAKE_SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
//When
serviceManager.DeleteService(FAKE_SERVICE_NAME);
//Then
Assert.DoesNotContain<string>(FAKE_SERVICE_NAME, serviceManager.GetServiceNames());
}
[Fact]
public void GetServiceNames_MultipleServices_AllCorrectNames()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string FAKE_SERVICE_PREFIX = "FakeService_";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_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<string>(FAKE_SERVICE_PREFIX + i, serviceManager.GetServiceNames());
}
}
[Fact]
public void GetServiceOptions_ThreeOptionService_CorrectOptions()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(
new FakeGameConfigurable("A"),
new FakeGameConfigurable("B"),
new FakeGameConfigurable("C")
);
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
//Then
Assert.Contains<string>("A", serviceManager.GetServiceOptions(SERVICE_NAME));
Assert.Contains<string>("B", serviceManager.GetServiceOptions(SERVICE_NAME));
Assert.Contains<string>("C", serviceManager.GetServiceOptions(SERVICE_NAME));
}
[Fact]
public void SetandGetServiceOptionValue_OneOption_OptionChanged()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule(
new FakeGameConfigurable("A")
);
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
serviceManager.SetServiceOptionValue(SERVICE_NAME, "A", "Test");
//Then
Assert.Equal<string>("Test", serviceManager.GetServiceOptionValue(SERVICE_NAME, "A"));
}
[Fact]
public void GetServiceState_NotRunning_ReturnsNotRunningState()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
//Then
Assert.Equal<ServiceState>(ServiceState.Stopped, serviceManager.GetServiceState(SERVICE_NAME));
}
[Fact]
public void GetServiceState_Running_ReturnsNotRunningState()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
serviceManager.StartService(SERVICE_NAME);
//Then
Assert.Equal<ServiceState>(ServiceState.Running, serviceManager.GetServiceState(SERVICE_NAME));
}
[Fact]
public void StartService_NotStarted_SuccessfulStart()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
serviceManager.StartService(SERVICE_NAME);
//Then
Assert.Equal<ServiceState>(ServiceState.Running, serviceManager.GetServiceState(SERVICE_NAME));
}
[Fact]
public void StopService_Stopped_StateUpdated()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
serviceManager.StartService(SERVICE_NAME);
serviceManager.StopService(SERVICE_NAME);
//Then
Assert.Equal<ServiceState>(ServiceState.Stopped, serviceManager.GetServiceState(SERVICE_NAME));
}
[Fact]
public void ExecuteCommand_ServiceStarted_CommandLogged()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_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<string>("Test", reader.ReadLine());
}
}
[Fact]
public void GetServiceConsoleStream_ServiceStopped_ExceptionThrown()
{
//Given
const string ASSEMBLY_NAME = "FakeAssembly";
const string SERVICE_NAME = "FakeService";
GameServiceManager serviceManager = new GameServiceManager();
IGameServiceModule stubGameServiceModule = new FakeGameServiceModule();
//When
serviceManager.AddModule(ASSEMBLY_NAME, stubGameServiceModule);
serviceManager.CreateService(SERVICE_NAME, ASSEMBLY_NAME, stubGameServiceModule.Name);
//Then
Assert.Null(serviceManager.GetServiceConsoleStream(SERVICE_NAME));
}
}
}