InterrogationRoomService tests

This commit is contained in:
Martijn Scheepers
2020-08-24 15:06:04 +02:00
parent 0bbeb60d8a
commit c49999657c
2 changed files with 283 additions and 8 deletions

View File

@@ -0,0 +1,264 @@
using Arrestanten_planbord.Data;
using Arrestanten_planbord.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Arrestanten_planbord.UnitTest
{
[TestClass]
public class InterrogationRoomServiceTest
{
private static Mock<ConfigFileHelper> _configFileHelperMoq;
private static string TestFolder = "InterrogationRoomServiceTest";
[ClassInitialize]
public static void MyClassInitialize(TestContext testContext)
{
Mock<ILogger<ConfigFileHelper>> configLoggerMoq = new Mock<ILogger<ConfigFileHelper>>();
IConfiguration testAppsettings = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
_configFileHelperMoq = new Mock<ConfigFileHelper>(configLoggerMoq.Object, testAppsettings);
string directory = testAppsettings.GetValue<string>("DatastoreLocation") + "\\" + TestFolder;
if (Directory.Exists(directory))
{
Directory.Delete(directory, true);
}
}
[ClassCleanup]
public static void MyClassClassCleanup()
{
}
[TestMethod]
public void StoreInterrogationRoomsData_InterrogationRoomsData_Null_Test()
{
Mock<ILogger<InterrogationRoomService>> transportLoggerMoq = new Mock<ILogger<InterrogationRoomService>>();
var interrogationRoomService = new InterrogationRoomService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
var called = false;
interrogationRoomService.InterrogationRoomChangeEvent += () => { called = true; };
var result = interrogationRoomService.StoreInterrogationRoomsData(null, TestFolder, "StoreInterrogationRoomsData_InterrogationRoomsData_Null_Test");
Assert.IsFalse(called);
Assert.IsFalse(result.Succeeded);
Assert.AreEqual("(InterrogationRoomService.StoreInterrogationRoomsData) interrogationRoomsData == null", result.Message);
transportLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(InterrogationRoomService.StoreInterrogationRoomsData) interrogationRoomsData == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void StoreInterrogationRoomsData_Location_Null_Test()
{
Mock<ILogger<InterrogationRoomService>> transportLoggerMoq = new Mock<ILogger<InterrogationRoomService>>();
var interrogationRoomService = new InterrogationRoomService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
var called = false;
interrogationRoomService.InterrogationRoomChangeEvent += () => { called = true; };
InterrogationRoomsData interrogationRoomsData = new InterrogationRoomsData();
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 1, RoomName = "Room 1", Name = "Billy", Cell = "Cell 1" });
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 2, RoomName = "Room 2", Name = "Billy", Cell = "Cell 2" });
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 3, RoomName = "Room 3", Name = "Billy", Cell = "Cell 3" });
var result = interrogationRoomService.StoreInterrogationRoomsData(interrogationRoomsData, null, "StoreInterrogationRoomsData_Location_Null_Test");
Assert.IsFalse(called);
Assert.IsFalse(result.Succeeded);
Assert.AreEqual("(InterrogationRoomService.StoreInterrogationRoomsData) location == null", result.Message);
transportLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(InterrogationRoomService.StoreInterrogationRoomsData) location == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void StoreInterrogationRoomsData_Filename_Null_Test()
{
Mock<ILogger<InterrogationRoomService>> transportLoggerMoq = new Mock<ILogger<InterrogationRoomService>>();
var interrogationRoomService = new InterrogationRoomService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
var called = false;
interrogationRoomService.InterrogationRoomChangeEvent += () => { called = true; };
InterrogationRoomsData interrogationRoomsData = new InterrogationRoomsData();
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 1, RoomName = "Room 1", Name = "Billy", Cell = "Cell 1" });
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 2, RoomName = "Room 2", Name = "Billy", Cell = "Cell 2" });
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 3, RoomName = "Room 3", Name = "Billy", Cell = "Cell 3" });
var result = interrogationRoomService.StoreInterrogationRoomsData(interrogationRoomsData, TestFolder, null);
Assert.IsFalse(called);
Assert.IsFalse(result.Succeeded);
Assert.AreEqual("(InterrogationRoomService.StoreInterrogationRoomsData) filename == null", result.Message);
transportLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(InterrogationRoomService.StoreInterrogationRoomsData) filename == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void GetInterrogationRoomsData_Location_Null_Test()
{
Mock<ILogger<InterrogationRoomService>> transportLoggerMoq = new Mock<ILogger<InterrogationRoomService>>();
var interrogationRoomService = new InterrogationRoomService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
var called = false;
interrogationRoomService.InterrogationRoomChangeEvent += () => { called = true; };
List<String> rooms = new List<string>();
rooms.Add("Room 1");
rooms.Add("Room 2");
rooms.Add("Room 3");
InterrogationRoomsData interrogationRoomsData = interrogationRoomService.GetInterrogationRoomsData(rooms.ToArray(), null, "GetInterrogationRoomsData_Location_Null_Test");
Assert.IsFalse(called);
Assert.IsNull(interrogationRoomsData);
transportLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(InterrogationRoomService.StoreInterrogationRoomsData) location == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void GetInterrogationRoomsData_Filename_Null_Test()
{
Mock<ILogger<InterrogationRoomService>> transportLoggerMoq = new Mock<ILogger<InterrogationRoomService>>();
var interrogationRoomService = new InterrogationRoomService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
var called = false;
interrogationRoomService.InterrogationRoomChangeEvent += () => { called = true; };
List<String> rooms = new List<string>();
rooms.Add("Room 1");
rooms.Add("Room 2");
rooms.Add("Room 3");
InterrogationRoomsData interrogationRoomsData = interrogationRoomService.GetInterrogationRoomsData(rooms.ToArray(), TestFolder, null);
Assert.IsFalse(called);
Assert.IsNull(interrogationRoomsData);
transportLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(InterrogationRoomService.StoreInterrogationRoomsData) filename == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void StoreInterrogationRoomsData_InterrogationRoomsData_Parrallel_Test()
{
Mock<ILogger<InterrogationRoomService>> transportLoggerMoq = new Mock<ILogger<InterrogationRoomService>>();
var interrogationRoomService = new InterrogationRoomService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
//write read parrallel
Parallel.For(0, 100, i =>
{
InterrogationRoomsData interrogationRoomsData = new InterrogationRoomsData();
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 1, RoomName = "Room 1", Name = "Billy", Cell = "Cell 1" });
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 2, RoomName = "Room 2", Name = "Billy", Cell = "Cell 2" });
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 3, RoomName = "Room 3", Name = "Billy", Cell = "Cell 3" });
var result = interrogationRoomService.StoreInterrogationRoomsData(interrogationRoomsData, TestFolder, $"StoreInterrogationRoomsData_InterrogationRoomsData_Parrallel_Test-{i}");
Assert.IsTrue(result.Succeeded);
});
//read parrallel all files
Parallel.For(0, 100, i =>
{
List<String> rooms = new List<string>();
rooms.Add("Room 1");
rooms.Add("Room 2");
rooms.Add("Room 3");
var data = interrogationRoomService.GetInterrogationRoomsData(rooms.ToArray(), TestFolder, $"StoreInterrogationRoomsData_InterrogationRoomsData_Parrallel_Test-{i}");
Assert.AreEqual(1, data.Rooms[0].Id);
Assert.AreEqual("Room 1", data.Rooms[0].RoomName);
Assert.AreEqual("Billy", data.Rooms[0].Name);
Assert.AreEqual("Cell 1", data.Rooms[0].Cell);
Assert.AreEqual(2, data.Rooms[1].Id);
Assert.AreEqual("Room 2", data.Rooms[1].RoomName);
Assert.AreEqual("Billy", data.Rooms[1].Name);
Assert.AreEqual("Cell 2", data.Rooms[1].Cell);
Assert.AreEqual(3, data.Rooms[2].Id);
Assert.AreEqual("Room 3", data.Rooms[2].RoomName);
Assert.AreEqual("Billy", data.Rooms[2].Name);
Assert.AreEqual("Cell 3", data.Rooms[2].Cell);
});
}
[TestMethod]
public void StoreInterrogationRoomsData_InterrogationRoomsData_Single_Parrallel_Test()
{
Mock<ILogger<InterrogationRoomService>> transportLoggerMoq = new Mock<ILogger<InterrogationRoomService>>();
var interrogationRoomService = new InterrogationRoomService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
//write read file
InterrogationRoomsData interrogationRoomsData = new InterrogationRoomsData();
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 1, RoomName = "Room 1", Name = "Billy", Cell = "Cell 1" });
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 2, RoomName = "Room 2", Name = "Billy", Cell = "Cell 2" });
interrogationRoomsData.Rooms.Add(new InterrogationRoomData { Id = 3, RoomName = "Room 3", Name = "Billy", Cell = "Cell 3" });
var result = interrogationRoomService.StoreInterrogationRoomsData(interrogationRoomsData, TestFolder, $"StoreInterrogationRoomsData_InterrogationRoomsData_Single_Parrallel_Test");
Assert.IsTrue(result.Succeeded);
//read parrallel one file
Parallel.For(0, 1000, i =>
{
List<String> rooms = new List<string>();
rooms.Add("Room 1");
rooms.Add("Room 2");
rooms.Add("Room 3");
var data = interrogationRoomService.GetInterrogationRoomsData(rooms.ToArray(), TestFolder, "StoreInterrogationRoomsData_InterrogationRoomsData_Single_Parrallel_Test");
Assert.AreEqual(1, data.Rooms[0].Id);
Assert.AreEqual("Room 1", data.Rooms[0].RoomName);
Assert.AreEqual("Billy", data.Rooms[0].Name);
Assert.AreEqual("Cell 1", data.Rooms[0].Cell);
Assert.AreEqual(2, data.Rooms[1].Id);
Assert.AreEqual("Room 2", data.Rooms[1].RoomName);
Assert.AreEqual("Billy", data.Rooms[1].Name);
Assert.AreEqual("Cell 2", data.Rooms[1].Cell);
Assert.AreEqual(3, data.Rooms[2].Id);
Assert.AreEqual("Room 3", data.Rooms[2].RoomName);
Assert.AreEqual("Billy", data.Rooms[2].Name);
Assert.AreEqual("Cell 3", data.Rooms[2].Cell);
});
}
}
}

View File

@@ -23,6 +23,17 @@ namespace Arrestanten_planbord.Data
public InterrogationRoomsData GetInterrogationRoomsData(string[] rooms, string location, string filename)
{
if (location == null)
{
_logger.LogError("(InterrogationRoomService.StoreInterrogationRoomsData) location == null");
return null;
}
if (filename == null)
{
_logger.LogError("(InterrogationRoomService.StoreInterrogationRoomsData) filename == null");
return null;
}
string filePath = _configFileHelper.GetFilePath(filename, location);
if (File.Exists(filePath) == false)
@@ -37,28 +48,28 @@ namespace Arrestanten_planbord.Data
return _configFileHelper.ReadConfigFile<InterrogationRoomsData>(filePath) as InterrogationRoomsData;
}
public void StoreInterrogationRoomsData(InterrogationRoomsData interrogationRoomsData, string location, string filename)
public ResultModel StoreInterrogationRoomsData(InterrogationRoomsData interrogationRoomsData, string location, string filename)
{
if (interrogationRoomsData == null)
{
_logger.LogError("(InterrogationRoomService.StoreInterrogationRoomsData) interrogationRoomsData == null");
return;
return new ResultModel { Succeeded = false, Message = "(InterrogationRoomService.StoreInterrogationRoomsData) interrogationRoomsData == null" };
}
if (location == null)
{
_logger.LogError("location == null");
return;
_logger.LogError("(InterrogationRoomService.StoreInterrogationRoomsData) location == null");
return new ResultModel { Succeeded = false, Message = "(InterrogationRoomService.StoreInterrogationRoomsData) location == null" };
}
if (filename == null)
{
_logger.LogError("filename == null");
return;
_logger.LogError("(InterrogationRoomService.StoreInterrogationRoomsData) filename == null");
return new ResultModel { Succeeded = false, Message = "(InterrogationRoomService.StoreInterrogationRoomsData) filename == null" };
}
string filePath = _configFileHelper.GetFilePath(filename, location);
_configFileHelper.WriteConfigFile(interrogationRoomsData, filePath);
var result = _configFileHelper.WriteConfigFile(interrogationRoomsData, filePath);
InterrogationRoomChangeEvent?.Invoke();
return result;
}
}
}