This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
gameservicewarden/tests/GameServiceWarden.Host.Tests/Modules/ServiceInfoTest.cs
Harrison Deng 6467c178c3 Basic service entity completed and tested.
added UML to guide actual implementation for Host.

ModuleLoader, ServiceGateway, are written but untested.
2020-12-24 16:33:17 -06:00

141 lines
5.2 KiB
C#

using System.Collections.Generic;
using System.IO;
using GameServiceWarden.Host.Modules;
using GameServiceWarden.ModuleAPI;
using Xunit;
namespace GameServiceWarden.Host.Tests.Modules
{
// 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 ServiceInfoTest
{
//MethodTested_ScenarioTested_ExpectedBehavior
[Fact]
public void Start_FromStopped_StateIsRunning()
{
//Arrange, Act, Assert
IGameService stubGameService = new FakeService();
ServiceInfo serviceInfo = new ServiceInfo(stubGameService, "FakeModule", "FakeAssembly");
serviceInfo.Start();
Assert.Equal(ServiceState.Running, serviceInfo.GetServiceState());
serviceInfo.Dispose();
}
[Fact]
public void Stop_FromStart_Stopped()
{
IGameService stubService = new FakeService();
ServiceInfo serviceInfo = new ServiceInfo(stubService, "FakeModule", "FakeAssembly");
serviceInfo.Start();
serviceInfo.Stop();
Assert.Equal(ServiceState.Stopped, serviceInfo.GetServiceState());
serviceInfo.Dispose();
}
[Fact]
public void GetConfigurables_ServiceStopped_ReturnsConfigurables()
{
//Given
FakeService stubService = new FakeService();
FakeConfigurable stubConfigurable = new FakeConfigurable();
HashSet<IConfigurable> configurables = new HashSet<IConfigurable>();
configurables.Add(stubConfigurable);
stubService.Configurables = configurables;
ServiceInfo serviceInfo = new ServiceInfo(stubService, "FakeModule", "FakeAssembly");
//When
serviceInfo.Start();
//Then
Assert.Contains(stubConfigurable, serviceInfo.GetConfigurables().Values);
serviceInfo.Dispose();
}
[Fact]
public void GetServiceState_ServiceNotStarted_ReturnsStoppedState()
{
//Given
IGameService stubService = new FakeService();
ServiceInfo serviceInfo = new ServiceInfo(stubService, "FakeModule", "FakeAssembly");
//Then
Assert.Equal(ServiceState.Stopped, serviceInfo.GetServiceState());
serviceInfo.Dispose();
}
[Fact]
public void GetServiceState_ServiceStarted_ReturnsRunningState()
{
//Given
IGameService stubService = new FakeService();
ServiceInfo serviceInfo = new ServiceInfo(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 FakeService();
ServiceInfo serviceInfo = new ServiceInfo(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 FakeService();
ServiceInfo serviceInfo = new ServiceInfo(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 FakeService();
ServiceInfo serviceInfo = new ServiceInfo(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 FakeService();
ServiceInfo serviceInfo = new ServiceInfo(stubService, "FakeModule", "FakeAssembly");
//Then
Assert.Null(serviceInfo.ServiceConsoleStream);
serviceInfo.Dispose();
}
[Fact]
public void ServiceConsoleStream_ServiceStarted_StreamReturned()
{
//Given
IGameService stubService = new FakeService();
ServiceInfo serviceInfo = new ServiceInfo(stubService, "FakeModule", "FakeAssembly");
//When
serviceInfo.Start();
//Then
Assert.IsAssignableFrom<Stream>(serviceInfo.ServiceConsoleStream);
serviceInfo.Dispose();
}
}
}