Тесты красной зоны

This commit is contained in:
2025-02-26 23:19:10 +04:00
parent 46f94f62b9
commit fda7d01074
16 changed files with 3483 additions and 14 deletions

View File

@@ -15,26 +15,20 @@ internal class RequestBusinessLogicContract(IRequestStorageContract requestStora
private readonly ILogger _logger = logger;
private readonly IRequestStorageContract _requestStorageContract =
requestStorageContract;
public List<RequestDataModel> GetAllRequestsByPeriod(DateTime fromDate, DateTime
toDate)
public List<RequestDataModel> GetAllRequestsByPeriod(DateTime fromDate, DateTime toDate)
{
return [];
}
public List<RequestDataModel> GetAllRequestsByWorkerByPeriod(string workerId,
DateTime fromDate, DateTime toDate)
public List<RequestDataModel> GetAllRequestsByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate)
{
return [];
}
public List<RequestDataModel> GetAllRequestsByBuyerByPeriod(string? buyerId,
DateTime fromDate, DateTime toDate)
{
return [];
}
public List<RequestDataModel> GetAllRequestsByProductByPeriod(string productId,
DateTime fromDate, DateTime toDate)
public List<RequestDataModel> GetAllRequestsBySoftwareByPeriod(string softwareId, DateTime fromDate, DateTime toDate)
{
return [];
}
public RequestDataModel GetRequestByData(string data)
{
return new("", "", "", 0, false, []);
@@ -43,7 +37,8 @@ internal class RequestBusinessLogicContract(IRequestStorageContract requestStora
{
}
public void CancelRequest(string id)
{
}
{
}
}

View File

@@ -10,6 +10,11 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="SmallSoftwareTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmallSoftwareContracts\SmallSoftwareContracts.csproj" />
</ItemGroup>

View File

@@ -9,9 +9,13 @@ namespace SmallSoftwareContracts.BusinessLogicsContracts;
public interface IRequestBusinessLogicContract
{
List<RequestDataModel> GetAllRequestsByPeriod(DateTime fromDate, DateTime toDate);
List<RequestDataModel> GetAllRequestsByWorkerByPeriod(string workerId, DateTime fromDate, DateTime toDate);
List<RequestDataModel> GetAllRequestsByProductByPeriod(string productId, DateTime fromDate, DateTime toDate);
List<RequestDataModel> GetAllRequestsBySoftwareByPeriod(string softwareId, DateTime fromDate, DateTime toDate);
RequestDataModel GetRequestByData(string data);
void InsertRequest(RequestDataModel requestDataModel);
void CancelRequest(string id);

View File

@@ -0,0 +1,13 @@

namespace SmallSoftwareContracts.Exceptions;
public class ElementExistsException : Exception
{
public string ParamName { get; private set; }
public string ParamValue { get; private set; }
public ElementExistsException(string paramName, string paramValue) :
base($"There is already an element with value{paramValue} of parameter { paramName}")
{
ParamName = paramName;
ParamValue = paramValue;
}
}

View File

@@ -0,0 +1,10 @@
namespace SmallSoftwareContracts.Exceptions;
public class ElementNotFoundException : Exception
{
public string Value { get; private set; }
public ElementNotFoundException(string value) : base($"Element not found at value = { value}")
{
Value = value;
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace SmallSoftwareContracts.Exceptions;
public class IncorrectDatesException : Exception
{
public IncorrectDatesException(DateTime start, DateTime end) :
base($"The end date must be later than the start date..StartDate: { start: dd.MM.YYYY}.EndDate: {end:dd.MM.YYYY}") { }
}

View File

@@ -0,0 +1,7 @@

namespace SmallSoftwareContracts.Exceptions;
public class NullListException : Exception
{
public NullListException() : base("The returned list is null") { }
}

View File

@@ -0,0 +1,8 @@

namespace SmallSoftwareContracts.Exceptions;
public class StorageException : Exception
{
public StorageException(Exception ex) : base($"Error while working in storage: { ex.Message}", ex) { }
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SmallSoftwareContracts.Extensions;
public static class DateTimeExtensions
{
public static bool IsDateNotOlder(this DateTime date, DateTime olderDate)
{
return date >= olderDate;
}
}

View File

@@ -0,0 +1,446 @@
using Microsoft.Extensions.Logging;
using Moq;
using SmallSoftwareBusinessLogic.Implementations;
using SmallSoftwareContracts.BusinessLogicsContracts;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.StoragesContracts;
namespace SmallSoftwareTests.BusinessLogicsContractsTests;
[TestFixture]
internal class ManufacturerBusinessLogicContractTests
{
private IManufacturerBusinessLogicContract _manufacturerBusinessLogicContract;
private Mock<IManufacturerStorageContract> _manufacturerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_manufacturerStorageContract = new
Mock<IManufacturerStorageContract>();
_manufacturerBusinessLogicContract = new ManufacturerBusinessLogicContract(_manufacturerStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_manufacturerStorageContract.Reset();
}
[Test]
public void GetAllManufacturers_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<ManufacturerDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1", null, null),
new(Guid.NewGuid().ToString(), "name 2", null, null),
new(Guid.NewGuid().ToString(), "name 3", null, null),
};
_manufacturerStorageContract.Setup(x =>
x.GetList()).Returns(listOriginal);
//Act
var list = _manufacturerBusinessLogicContract.GetAllManufacturers();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
}
[Test]
public void GetAllManufacturers_ReturnEmptyList_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x => x.GetList()).Returns([]);
//Act
var list = _manufacturerBusinessLogicContract.GetAllManufacturers();
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_manufacturerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllManufacturers_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.GetAllManufacturers(),
Throws.TypeOf<NullListException>());
_manufacturerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllManufacturers_StorageThrowError_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x => x.GetList()).Throws(new
StorageException(new InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.GetAllManufacturers(),
Throws.TypeOf<StorageException>());
_manufacturerStorageContract.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetManufacturerByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new ManufacturerDataModel(id, "name", null, null);
_manufacturerStorageContract.Setup(x =>
x.GetElementById(id)).Returns(record);
//Act
var element =
_manufacturerBusinessLogicContract.GetManufacturerByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_manufacturerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetManufacturerByData_GetByName_ReturnRecord_Test()
{
//Arrange
var manufacturerName = "name";
var record = new ManufacturerDataModel(Guid.NewGuid().ToString(),
manufacturerName, null, null);
_manufacturerStorageContract.Setup(x =>
x.GetElementByName(manufacturerName)).Returns(record);
//Act
var element =
_manufacturerBusinessLogicContract.GetManufacturerByData(manufacturerName);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.ManufacturerName, Is.EqualTo(manufacturerName));
_manufacturerStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetManufacturerByData_GetByOldName_ReturnRecord_Test()
{
//Arrange
var manufacturerOldName = "name before";
var record = new ManufacturerDataModel(Guid.NewGuid().ToString(),
"name", manufacturerOldName, null);
_manufacturerStorageContract.Setup(x =>
x.GetElementByOldName(manufacturerOldName)).Returns(record);
//Act
var element =
_manufacturerBusinessLogicContract.GetManufacturerByData(manufacturerOldName);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.PrevManufacturerName,
Is.EqualTo(manufacturerOldName));
_manufacturerStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
_manufacturerStorageContract.Verify(x =>
x.GetElementByOldName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetManufacturerByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.GetManufacturerByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_manufacturerBusinessLogicContract.GetManufacturerByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_manufacturerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
_manufacturerStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Never);
_manufacturerStorageContract.Verify(x =>
x.GetElementByOldName(It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetManufacturerByData__GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.GetManufacturerByData(Guid.NewGuid().ToString(
)), Throws.TypeOf<ElementNotFoundException>());
_manufacturerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_manufacturerStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Never);
_manufacturerStorageContract.Verify(x =>
x.GetElementByOldName(It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetManufacturerByData_GetByNameOrOldName_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.GetManufacturerByData("name"),
Throws.TypeOf<ElementNotFoundException>());
_manufacturerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
_manufacturerStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
_manufacturerStorageContract.Verify(x =>
x.GetElementByOldName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetManufacturerByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x =>
x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
_manufacturerStorageContract.Setup(x =>
x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.GetManufacturerByData(Guid.NewGuid().ToString(
)), Throws.TypeOf<StorageException>());
Assert.That(() =>
_manufacturerBusinessLogicContract.GetManufacturerByData("name"),
Throws.TypeOf<StorageException>());
_manufacturerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_manufacturerStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
_manufacturerStorageContract.Verify(x =>
x.GetElementByOldName(It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetManufacturerByData_GetByOldName_StorageThrowError_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x =>
x.GetElementByOldName(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.GetManufacturerByData("name"),
Throws.TypeOf<StorageException>());
_manufacturerStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
_manufacturerStorageContract.Verify(x =>
x.GetElementByOldName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertManufacturer_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new ManufacturerDataModel(Guid.NewGuid().ToString(),
"name", null, null);
_manufacturerStorageContract.Setup(x =>
x.AddElement(It.IsAny<ManufacturerDataModel>()))
.Callback((ManufacturerDataModel x) =>
{
flag = x.Id == record.Id && x.ManufacturerName ==
record.ManufacturerName;
});
//Act
_manufacturerBusinessLogicContract.InsertManufacturer(record);
//Assert
_manufacturerStorageContract.Verify(x =>
x.AddElement(It.IsAny<ManufacturerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertManufacturer_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x =>
x.AddElement(It.IsAny<ManufacturerDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.InsertManufacturer(new(Guid.NewGuid().ToString
(), "name", null, null)), Throws.TypeOf<ElementExistsException>());
_manufacturerStorageContract.Verify(x =>
x.AddElement(It.IsAny<ManufacturerDataModel>()), Times.Once);
}
[Test]
public void InsertManufacturer_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.InsertManufacturer(null),
Throws.TypeOf<ArgumentNullException>());
_manufacturerStorageContract.Verify(x =>
x.AddElement(It.IsAny<ManufacturerDataModel>()), Times.Never);
}
[Test]
public void InsertManufacturer_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.InsertManufacturer(new
ManufacturerDataModel("id", "name", null, null)),
Throws.TypeOf<ValidationException>());
_manufacturerStorageContract.Verify(x =>
x.AddElement(It.IsAny<ManufacturerDataModel>()), Times.Never);
}
[Test]
public void InsertManufacturer_StorageThrowError_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x =>
x.AddElement(It.IsAny<ManufacturerDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.InsertManufacturer(new(Guid.NewGuid().ToString
(), "name", null, null)), Throws.TypeOf<StorageException>());
_manufacturerStorageContract.Verify(x =>
x.AddElement(It.IsAny<ManufacturerDataModel>()), Times.Once);
}
[Test]
public void UpdateManufacturer_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new ManufacturerDataModel(Guid.NewGuid().ToString(),
"name", null, null);
_manufacturerStorageContract.Setup(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>()))
.Callback((ManufacturerDataModel x) =>
{
flag = x.Id == record.Id && x.ManufacturerName ==
record.ManufacturerName;
});
//Act
_manufacturerBusinessLogicContract.UpdateManufacturer(record);
//Assert
_manufacturerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void
UpdateManufacturer_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>())).Throws(new
ElementNotFoundException(""));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.UpdateManufacturer(new(Guid.NewGuid().ToString
(), "name", null, null)), Throws.TypeOf<ElementNotFoundException>());
_manufacturerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>()), Times.Once);
}
[Test]
public void UpdateManufacturer_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.UpdateManufacturer(new(Guid.NewGuid().ToString
(), "name", null, null)), Throws.TypeOf<ElementExistsException>());
_manufacturerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>()), Times.Once);
}
[Test]
public void UpdateManufacturer_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.UpdateManufacturer(null),
Throws.TypeOf<ArgumentNullException>());
_manufacturerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>()), Times.Never);
}
[Test]
public void UpdateManufacturer_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.UpdateManufacturer(new
ManufacturerDataModel("id", "name", null, null)),
Throws.TypeOf<ValidationException>());
_manufacturerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>()), Times.Never);
}
[Test]
public void UpdateManufacturer_StorageThrowError_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.UpdateManufacturer(new(Guid.NewGuid().ToString
(), "name", null, null)), Throws.TypeOf<StorageException>());
_manufacturerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<ManufacturerDataModel>()), Times.Once);
}
[Test]
public void DeleteManufacturer_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_manufacturerStorageContract.Setup(x => x.DelElement(It.Is((string x)
=> x == id))).Callback(() => { flag = true; });
//Act
_manufacturerBusinessLogicContract.DeleteManufacturer(id);
//Assert
_manufacturerStorageContract.Verify(x =>
x.DelElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteManufacturer_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_manufacturerStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.DeleteManufacturer(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_manufacturerStorageContract.Verify(x =>
x.DelElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteManufacturer_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.DeleteManufacturer(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_manufacturerBusinessLogicContract.DeleteManufacturer(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_manufacturerStorageContract.Verify(x =>
x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteManufacturer_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.DeleteManufacturer("id"),
Throws.TypeOf<ValidationException>());
_manufacturerStorageContract.Verify(x =>
x.DelElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteManufacturer_StorageThrowError_ThrowException_Test()
{
//Arrange
_manufacturerStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_manufacturerBusinessLogicContract.DeleteManufacturer(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_manufacturerStorageContract.Verify(x =>
x.DelElement(It.IsAny<string>()), Times.Once);
}
}

View File

@@ -0,0 +1,567 @@
using Microsoft.Extensions.Logging;
using Moq;
using SmallSoftwareBusinessLogic.Implementations;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Enums;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SmallSoftwareTests.BusinessLogicsContractsTests;
[TestFixture]
internal class PostBusinessLogicContractTests
{
private PostBusinessLogicContract _postBusinessLogicContract;
private Mock<IPostStorageContract> _postStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_postStorageContract = new Mock<IPostStorageContract>();
_postBusinessLogicContract = new
PostBusinessLogicContract(_postStorageContract.Object, new
Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_postStorageContract.Reset();
}
[Test]
public void GetAllPosts_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<PostDataModel>()
{
new(Guid.NewGuid().ToString(),"name 1", PostType.SoftInstaller,
10, true, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 2", PostType.SoftInstaller,
10, false, DateTime.UtcNow),
new(Guid.NewGuid().ToString(), "name 3", PostType.SoftInstaller,
10, true, DateTime.UtcNow),
};
_postStorageContract.Setup(x =>
x.GetList(It.IsAny<bool>())).Returns(listOriginal);
//Act
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
var listAll = _postBusinessLogicContract.GetAllPosts(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(listAll, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(listAll, Is.EquivalentTo(listOriginal));
});
_postStorageContract.Verify(x => x.GetList(true), Times.Once);
_postStorageContract.Verify(x => x.GetList(false), Times.Once);
}
[Test]
public void GetAllPosts_ReturnEmptyList_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.GetList(It.IsAny<bool>())).Returns([]);
//Act
var listOnlyActive = _postBusinessLogicContract.GetAllPosts(true);
var listAll = _postBusinessLogicContract.GetAllPosts(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(listAll, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(listAll, Has.Count.EqualTo(0));
});
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()),
Times.Exactly(2));
}
[Test]
public void GetAllPosts_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()),
Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()),
Times.Once);
}
[Test]
public void GetAllPosts_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.GetList(It.IsAny<bool>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.GetAllPosts(It.IsAny<bool>()),
Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.GetList(It.IsAny<bool>()),
Times.Once);
}
[Test]
public void GetAllDataOfPost_ReturnListOfRecords_Test()
{
//Arrange
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<PostDataModel>()
{
new(postId, "name 1", PostType.SoftInstaller, 10, true,
DateTime.UtcNow),
new(postId, "name 2", PostType.SoftInstaller, 10, false,
DateTime.UtcNow)
};
_postStorageContract.Setup(x =>
x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _postBusinessLogicContract.GetAllDataOfPost(postId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(2));
_postStorageContract.Verify(x => x.GetPostWithHistory(postId),
Times.Once);
}
[Test]
public void GetAllDataOfPost_ReturnEmptyList_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.GetPostWithHistory(It.IsAny<string>())).Returns([]);
//Act
var list =
_postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString());
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_postStorageContract.Verify(x =>
x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_PostIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_postBusinessLogicContract.GetAllDataOfPost(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x =>
x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllDataOfPost_PostIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetAllDataOfPost("id"),
Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x =>
x.GetPostWithHistory(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllDataOfPost_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()),
Throws.TypeOf<NullListException>());
_postStorageContract.Verify(x =>
x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllDataOfPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.GetPostWithHistory(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.GetAllDataOfPost(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x =>
x.GetPostWithHistory(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new PostDataModel(id, "name", PostType.SoftInstaller, 10,
true, DateTime.UtcNow);
_postStorageContract.Setup(x =>
x.GetElementById(id)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_postStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_GetByName_ReturnRecord_Test()
{
//Arrange
var postName = "name";
var record = new PostDataModel(Guid.NewGuid().ToString(), postName,
PostType.SoftInstaller, 10, true, DateTime.UtcNow);
_postStorageContract.Setup(x =>
x.GetElementByName(postName)).Returns(record);
//Act
var element = _postBusinessLogicContract.GetPostByData(postName);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.PostName, Is.EqualTo(postName));
_postStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_postBusinessLogicContract.GetPostByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
_postStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetPostByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_postStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetPostByData_GetByName_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"),
Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
_postStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetPostByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
_postStorageContract.Setup(x =>
x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.GetPostByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
Assert.That(() => _postBusinessLogicContract.GetPostByData("name"),
Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_postStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertPost_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.Supervisor, 10, true, DateTime.UtcNow.AddDays(-1));
_postStorageContract.Setup(x =>
x.AddElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName ==
record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
x.ChangeDate == record.ChangeDate;
});
//Act
_postBusinessLogicContract.InsertPost(record);
//Assert
_postStorageContract.Verify(x =>
x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertPost_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.AddElement(It.IsAny<PostDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name",
PostType.Supervisor, 10, true, DateTime.UtcNow)),
Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x =>
x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void InsertPost_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(null),
Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x =>
x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void InsertPost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new
PostDataModel("id", "name", PostType.Supervisor, 10, true, DateTime.UtcNow)),
Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x =>
x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void InsertPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name",
PostType.Supervisor, 10, true, DateTime.UtcNow)),
Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x =>
x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.Supervisor, 10, true, DateTime.UtcNow.AddDays(-1));
_postStorageContract.Setup(x =>
x.UpdElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) =>
{
flag = x.Id == record.Id && x.PostName ==
record.PostName && x.PostType == record.PostType && x.Salary == record.Salary &&
x.ChangeDate == record.ChangeDate;
});
//Act
_postBusinessLogicContract.UpdatePost(record);
//Assert
_postStorageContract.Verify(x =>
x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdatePost_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.UpdElement(It.IsAny<PostDataModel>())).Throws(new
ElementNotFoundException(""));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name",
PostType.Supervisor, 10, true, DateTime.UtcNow)),
Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x =>
x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.UpdElement(It.IsAny<PostDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme",
PostType.Supervisor, 10, true, DateTime.UtcNow)),
Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x =>
x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void UpdatePost_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(null),
Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x =>
x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void UpdatePost_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new
PostDataModel("id", "name", PostType.Supervisor, 10, true, DateTime.UtcNow)),
Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x =>
x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
}
[Test]
public void UpdatePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name",
PostType.Supervisor, 10, true, DateTime.UtcNow)),
Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x =>
x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
}
[Test]
public void DeletePost_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_postStorageContract.Setup(x => x.DelElement(It.Is((string x) => x ==
id))).Callback(() => { flag = true; });
//Act
_postBusinessLogicContract.DeletePost(id);
//Assert
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
Assert.That(flag);
}
[Test]
public void DeletePost_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void DeletePost_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_postBusinessLogicContract.DeletePost(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeletePost_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.DeletePost("id"),
Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeletePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.DeletePost(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void RestorePost_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_postStorageContract.Setup(x => x.ResElement(It.Is((string x) => x ==
id))).Callback(() => { flag = true; });
//Act
_postBusinessLogicContract.RestorePost(id);
//Assert
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()),
Times.Once);
Assert.That(flag);
}
[Test]
public void RestorePost_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_postStorageContract.Setup(x =>
x.ResElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void RestorePost_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_postBusinessLogicContract.RestorePost(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void RestorePost_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _postBusinessLogicContract.RestorePost("id"),
Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void RestorePost_StorageThrowError_ThrowException_Test()
{
//Arrange
_postStorageContract.Setup(x =>
x.ResElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_postBusinessLogicContract.RestorePost(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.ResElement(It.IsAny<string>()),
Times.Once);
}
}

View File

@@ -0,0 +1,536 @@
using Microsoft.Extensions.Logging;
using Moq;
using SmallSoftwareBusinessLogic.Implementations;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.StoragesContracts;
namespace SmallSoftwareTests.BusinessLogicsContractsTests;
[TestFixture]
internal class RequestBusinessLogicContractTests
{
private RequestBusinessLogicContract _requestBusinessLogicContract;
private Mock<IRequestStorageContract> _requestStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_requestStorageContract = new Mock<IRequestStorageContract>();
_requestBusinessLogicContract = new
RequestBusinessLogicContract(_requestStorageContract.Object, new
Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_requestStorageContract.Reset();
}
[Test]
public void GetAllRequestsByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<RequestDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, [new InstallationRequestDataModel(Guid.NewGuid().ToString(),Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []), new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []),
};
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list = _requestBusinessLogicContract.GetAllRequestsByPeriod(date,
date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_requestStorageContract.Verify(x => x.GetList(date, date.AddDays(1), null, null), Times.Once);
}
[Test]
public void GetAllRequestsByPeriod_ReturnEmptyList_Test()
{
//Arrange
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>())).Returns([]);
//Act
var list =
_requestBusinessLogicContract.GetAllRequestsByPeriod(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllRequestsByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByPeriod(date, date),
Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByPeriod(date, date.AddSeconds(-1)),
Throws.TypeOf<IncorrectDatesException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllRequestsByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByPeriod(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllRequestsByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByPeriod(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllRequestsByWorkerByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var workerId = Guid.NewGuid().ToString();
var listOriginal = new List<RequestDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, [new InstallationRequestDataModel(Guid.NewGuid().ToString(),Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []), new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []),
};
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>())).Returns(listOriginal);
//Act
var list =
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod(workerId, date,
date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_requestStorageContract.Verify(x => x.GetList(date, date.AddDays(1),
workerId, null), Times.Once);
}
[Test]
public void GetAllRequestsByWorkerByPeriod_ReturnEmptyList_Test()
{
//Arrange
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>())).Returns([]);
//Act
var list =
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod(Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllRequestsByWorkerByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod(Guid.NewGuid().ToString(),
date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod(Guid.NewGuid().ToString(),
date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllRequestsByWorkerByPeriod_WorkerIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod(null, DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod(string.Empty,
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ArgumentNullException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllRequestsByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod("workerId",
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ValidationException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllRequestsByWorkerByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod(Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<NullListException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllRequestsByWorkerByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsByWorkerByPeriod(Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllRequestsBySoftwareByPeriod_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var softwareId = Guid.NewGuid().ToString();
var listOriginal = new List<RequestDataModel>()
{
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, [new InstallationRequestDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []),
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, []),
};
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>())).Returns(listOriginal);
//Act
var list =
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod(softwareId, date,
date.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_requestStorageContract.Verify(x => x.GetList(date, date.AddDays(1),
null, null), Times.Once);
}
[Test]
public void GetAllRequestsBySoftwareByPeriod_ReturnEmptyList_Test()
{
//Arrange
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>())).Returns([]);
//Act
var list =
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod(Guid.NewGuid().ToString()
, DateTime.UtcNow, DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllRequestsBySoftwareByPeriod_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod(Guid.NewGuid().ToString()
, date, date), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod(Guid.NewGuid().ToString()
, date, date.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllRequestsBySoftwareByPeriod_SoftwareIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod(Guid.NewGuid().ToString(), DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod(string.Empty, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ArgumentNullException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllRequestsBySoftwareByPeriod_SoftwareIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod("softwareId",
DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<ValidationException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllRequestsBySoftwareByPeriod_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod(Guid.NewGuid().ToString()
, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<NullListException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllRequestsBySoftwareByPeriod_StorageThrowError_ThrowException_Test()
{
//Arrange
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetAllRequestsBySoftwareByPeriod(Guid.NewGuid().ToString()
, DateTime.UtcNow, DateTime.UtcNow.AddDays(1)),
Throws.TypeOf<StorageException>());
_requestStorageContract.Verify(x => x.GetList(It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<string>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetRequestByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new RequestDataModel(id, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, [new InstallationRequestDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_requestStorageContract.Setup(x =>
x.GetElementById(id)).Returns(record);
//Act
var element = _requestBusinessLogicContract.GetRequestByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_requestStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetRequestByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _requestBusinessLogicContract.GetRequestByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_requestBusinessLogicContract.GetRequestByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_requestStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetRequestByData_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _requestBusinessLogicContract.GetRequestByData("requestId"),
Throws.TypeOf<ValidationException>());
_requestStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetRequestByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetRequestByData(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_requestStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetRequestByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_requestStorageContract.Setup(x =>
x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.GetRequestByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_requestStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertRequest_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new RequestDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false,
[new InstallationRequestDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]);
_requestStorageContract.Setup(x => x.AddElement(It.IsAny<RequestDataModel>()))
.Callback((RequestDataModel x) =>
{
flag = x.Id == record.Id && x.WorkerId == record.WorkerId && x.IsCancel ==
record.IsCancel && x.Softwares.Count == record.Softwares.Count &&
x.Softwares.First().SoftwareId ==
record.Softwares.First().SoftwareId &&
x.Softwares.First().RequestId ==
record.Softwares.First().RequestId &&
x.Softwares.First().Count ==
record.Softwares.First().Count;
});
//Act
_requestBusinessLogicContract.InsertRequest(record);
//Assert
_requestStorageContract.Verify(x =>
x.AddElement(It.IsAny<RequestDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertRequest_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_requestStorageContract.Setup(x =>
x.AddElement(It.IsAny<RequestDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.InsertRequest(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10,false,
[new InstallationRequestDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
_requestStorageContract.Verify(x => x.AddElement(It.IsAny<RequestDataModel>()), Times.Once);
}
[Test]
public void InsertRequest_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _requestBusinessLogicContract.InsertRequest(null),
Throws.TypeOf<ArgumentNullException>());
_requestStorageContract.Verify(x =>
x.AddElement(It.IsAny<RequestDataModel>()), Times.Never);
}
[Test]
public void InsertRequest_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _requestBusinessLogicContract.InsertRequest(new RequestDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, [])), Throws.TypeOf<ValidationException>());
_requestStorageContract.Verify(x =>
x.AddElement(It.IsAny<RequestDataModel>()), Times.Never);
}
[Test]
public void InsertRequest_StorageThrowError_ThrowException_Test()
{
//Arrange
_requestStorageContract.Setup(x =>
x.AddElement(It.IsAny<RequestDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.InsertRequest(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, false, [new InstallationRequestDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
_requestStorageContract.Verify(x =>
x.AddElement(It.IsAny<RequestDataModel>()), Times.Once);
}
[Test]
public void CancelRequest_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_requestStorageContract.Setup(x => x.DelElement(It.Is((string x) => x ==
id))).Callback(() => { flag = true; });
//Act
_requestBusinessLogicContract.CancelRequest(id);
//Assert
_requestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
Assert.That(flag);
}
[Test]
public void CancelRequest_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_requestStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.CancelRequest(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_requestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void CancelRequest_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _requestBusinessLogicContract.CancelRequest(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_requestBusinessLogicContract.CancelRequest(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_requestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void CancelRequest_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _requestBusinessLogicContract.CancelRequest("id"),
Throws.TypeOf<ValidationException>());
_requestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void CancelRequest_StorageThrowError_ThrowException_Test()
{
//Arrange
_requestStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_requestBusinessLogicContract.CancelRequest(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_requestStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
}

View File

@@ -0,0 +1,460 @@
using Microsoft.Extensions.Logging;
using Moq;
using SmallSoftwareBusinessLogic.Implementations;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Enums;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SmallSoftwareTests.BusinessLogicsContractsTests;
[TestFixture]
internal class SalaryBusinessLogicContractTests
{
private SalaryBusinessLogicContract _salaryBusinessLogicContract;
private Mock<ISalaryStorageContract> _salaryStorageContract;
private Mock<IRequestStorageContract> _requestStorageContract;
private Mock<IPostStorageContract> _postStorageContract;
private Mock<IWorkerStorageContract> _workerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_salaryStorageContract = new Mock<ISalaryStorageContract>();
_requestStorageContract = new Mock<IRequestStorageContract>();
_postStorageContract = new Mock<IPostStorageContract>();
_workerStorageContract = new Mock<IWorkerStorageContract>();
_salaryBusinessLogicContract = new
SalaryBusinessLogicContract(_salaryStorageContract.Object,
_requestStorageContract.Object, _postStorageContract.Object,
_workerStorageContract.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_salaryStorageContract.Reset();
_requestStorageContract.Reset();
_postStorageContract.Reset();
_workerStorageContract.Reset();
}
[Test]
public void GetAllSalaries_ReturnListOfRecords_Test()
{
//Arrange
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(1);
var listOriginal = new List<SalaryDataModel>()
{
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1),
14),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1),
30),
};
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list =
_salaryBusinessLogicContract.GetAllSalariesByPeriod(startDate, endDate);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate,
null), Times.Once);
}
[Test]
public void GetAllSalaries_ReturnEmptyList_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
//Act
var list =
_salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalaries_IncorrectDates_ThrowException_Test()
{
//Arrange
var dateTime = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime, dateTime),
Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriod(dateTime,
dateTime.AddSeconds(-1)), Throws.TypeOf<IncorrectDatesException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalaries_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<NullListException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalaries_StorageThrowError_ThrowException_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriod(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1)), Throws.TypeOf<StorageException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_ReturnListOfRecords_Test()
{
//Arrange
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(1);
var workerId = Guid.NewGuid().ToString();
var listOriginal = new List<SalaryDataModel>()
{
new(Guid.NewGuid().ToString(), DateTime.UtcNow, 10),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(1),
14),
new(Guid.NewGuid().ToString(), DateTime.UtcNow.AddDays(-1),
30),
};
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>())).Returns(listOriginal);
//Act
var list =
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(startDate, endDate,
workerId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_salaryStorageContract.Verify(x => x.GetList(startDate, endDate,
workerId), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_ReturnEmptyList_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>())).Returns([]);
//Act
var list =
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString());
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_IncorrectDates_ThrowException_Test()
{
//Arrange
var dateTime = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime, dateTime,
Guid.NewGuid().ToString()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(dateTime,
dateTime.AddSeconds(-1), Guid.NewGuid().ToString()),
Throws.TypeOf<IncorrectDatesException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllSalariesByWorker_WorkerIdIsNUllOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), null), Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), string.Empty),
Throws.TypeOf<ArgumentNullException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByWorker_WorkerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), "workerId"), Throws.TypeOf<ValidationException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSalariesByWorker_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()),
Throws.TypeOf<NullListException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSalariesByWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_salaryStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.GetAllSalariesByPeriodByWorker(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_salaryStorageContract.Verify(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>()), Times.Once);
}
[Test]
public void CalculateSalaryByMounth_CalculateSalary_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
var requestSum = 200.0;
var postSalary = 2000.0;
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new RequestDataModel(Guid.NewGuid().ToString(), workerId, Guid.NewGuid().ToString(), requestSum, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.SoftInstaller, postSalary, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test",
Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
var expectedSum = postSalary + requestSum * 0.1;
_salaryStorageContract.Setup(x =>
x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
sum = x.Salary;
});
//Act
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
[Test]
public void CalculateSalaryByMounth_WithSeveralWorkers_Test()
{
//Arrange
var worker1Id = Guid.NewGuid().ToString();
var worker2Id = Guid.NewGuid().ToString();
var worker3Id = Guid.NewGuid().ToString();
var list = new List<WorkerDataModel>() {
new(worker1Id, "Test", Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow, false),
new(worker2Id, "Test", Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow, false),
new(worker3Id, "Test", Guid.NewGuid().ToString(),
DateTime.UtcNow, DateTime.UtcNow, false)
};
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new RequestDataModel(Guid.NewGuid().ToString(),
worker1Id, Guid.NewGuid().ToString(), 1, false, []),
new RequestDataModel(Guid.NewGuid().ToString(), worker1Id, Guid.NewGuid().ToString(),
1, false, []),
new RequestDataModel(Guid.NewGuid().ToString(), worker2Id, Guid.NewGuid().ToString(),
1, false, []),
new RequestDataModel(Guid.NewGuid().ToString(), worker3Id, Guid.NewGuid().ToString(),
1, false, []),
new RequestDataModel(Guid.NewGuid().ToString(), worker3Id, Guid.NewGuid().ToString(),
1, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.SoftInstaller, 2000, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns(list);
//Act
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow);
//Assert
_salaryStorageContract.Verify(x =>
x.AddElement(It.IsAny<SalaryDataModel>()), Times.Exactly(list.Count));
}
[Test]
public void CalculateSalaryByMounth_WithoitRequestsByWorker_Test()
{
//Arrange
var postSalary = 2000.0;
var workerId = Guid.NewGuid().ToString();
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.SoftInstaller, postSalary, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test",
Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0;
var expectedSum = postSalary;
_salaryStorageContract.Setup(x =>
x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) =>
{
sum = x.Salary;
});
//Act
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow);
//Assert
Assert.That(sum, Is.EqualTo(expectedSum));
}
[Test]
public void
CalculateSalaryByMounth_RequestStorageReturnNull_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.SoftInstaller, 2000, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test",
Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow),
Throws.TypeOf<NullListException>());
}
[Test]
public void
CalculateSalaryByMounth_PostStorageReturnNull_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new RequestDataModel(Guid.NewGuid().ToString(), workerId, Guid.NewGuid().ToString(), 200, false, [])]);
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test",
Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow),
Throws.TypeOf<NullListException>());
}
[Test]
public void
CalculateSalaryByMounth_WorkerStorageReturnNull_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new RequestDataModel(Guid.NewGuid().ToString(),
workerId, Guid.NewGuid().ToString(), 200, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.SoftInstaller, 2000, true, DateTime.UtcNow));
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow),
Throws.TypeOf<NullListException>());
}
[Test]
public void
CalculateSalaryByMounth_RequestStorageThrowException_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Throws(new StorageException(new
InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.SoftInstaller, 2000, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test",
Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow),
Throws.TypeOf<StorageException>());
}
[Test]
public void
CalculateSalaryByMounth_PostStorageThrowException_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new RequestDataModel(Guid.NewGuid().ToString(),
workerId, Guid.NewGuid().ToString(), 200, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new
InvalidOperationException()));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new WorkerDataModel(workerId, "Test",
Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow),
Throws.TypeOf<StorageException>());
}
[Test]
public void
CalculateSalaryByMounth_WorkerStorageThrowException_ThrowException_Test()
{
//Arrange
var workerId = Guid.NewGuid().ToString();
_requestStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(),
It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new RequestDataModel(Guid.NewGuid().ToString(), workerId, Guid.NewGuid().ToString(), 200, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name",
PostType.SoftInstaller, 2000, true, DateTime.UtcNow));
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_salaryBusinessLogicContract.CalculateSalaryByMonth(DateTime.UtcNow),
Throws.TypeOf<StorageException>());
}
}

View File

@@ -0,0 +1,620 @@
using Microsoft.Extensions.Logging;
using Moq;
using SmallSoftwareBusinessLogic.Implementations;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Enums;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static NUnit.Framework.Internal.OSPlatform;
namespace SmallSoftwareTests.BusinessLogicsContractsTests;
[TestFixture]
internal class SoftwareBusinessLogicContractTests
{
private SoftwareBusinessLogicContract _softwareBusinessLogicContract;
private Mock<ISoftwareStorageContract> _softwareStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_softwareStorageContract = new Mock<ISoftwareStorageContract>();
_softwareBusinessLogicContract = new
SoftwareBusinessLogicContract(_softwareStorageContract.Object, new
Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_softwareStorageContract.Reset();
}
[Test]
public void GetAllSoftwares_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<SoftwareDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1", SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false),
new(Guid.NewGuid().ToString(), "name 2", SoftwareType.Windows, Guid.NewGuid().ToString(), 10, true),
new(Guid.NewGuid().ToString(), "name 3", SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false),
};
_softwareStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>())).Returns(listOriginal);
//Act
var listOnlyActive =
_softwareBusinessLogicContract.GetAllSoftwares(true);
var list = _softwareBusinessLogicContract.GetAllSoftwares(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_softwareStorageContract.Verify(x => x.GetList(true, null),
Times.Once);
_softwareStorageContract.Verify(x => x.GetList(false, null),
Times.Once);
}
[Test]
public void GetAllSoftwares_ReturnEmptyList_Test()
{
//Arrange
_softwareStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>())).Returns([]);
//Act
var listOnlyActive =
_softwareBusinessLogicContract.GetAllSoftwares(true);
var list = _softwareBusinessLogicContract.GetAllSoftwares(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_softwareStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
null), Times.Exactly(2));
}
[Test]
public void GetAllSoftwares_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetAllSoftwares(It.IsAny<bool>()),
Throws.TypeOf<NullListException>());
_softwareStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSoftwares_StorageThrowError_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetAllSoftwares(It.IsAny<bool>()),
Throws.TypeOf<StorageException>());
_softwareStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetAllSoftwaresByManufacturer_ReturnListOfRecords_Test()
{
//Arrange
var manufacturerId = Guid.NewGuid().ToString();
var listOriginal = new List<SoftwareDataModel>()
{
new(Guid.NewGuid().ToString(), "name 1",SoftwareType.Windows,
Guid.NewGuid().ToString(), 10, false),
new(Guid.NewGuid().ToString(), "name 2",
SoftwareType.Windows, Guid.NewGuid().ToString(), 10, true),
new(Guid.NewGuid().ToString(), "name 3",
SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false),
};
_softwareStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>())).Returns(listOriginal);
//Act
var listOnlyActive =
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(manufacturerId, true);
var list =
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(manufacturerId,
false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_softwareStorageContract.Verify(x => x.GetList(true, manufacturerId),
Times.Once);
_softwareStorageContract.Verify(x => x.GetList(false, manufacturerId),
Times.Once);
}
[Test]
public void GetAllSoftwaresByManufacturer_ReturnEmptyList_Test()
{
//Arrange
var manufacturerId = Guid.NewGuid().ToString();
_softwareStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>())).Returns([]);
//Act
var listOnlyActive =
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(manufacturerId, true);
var list =
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(manufacturerId,
false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_softwareStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
manufacturerId), Times.Exactly(2));
}
[Test]
public void
GetAllSoftwaresByManufacturer_ManufacturerIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(null,
It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(string.Empty,
It.IsAny<bool>()), Throws.TypeOf<ArgumentNullException>());
_softwareStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetAllSoftwaresByManufacturer_ManufacturerIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer("manufacturerId",
It.IsAny<bool>()), Throws.TypeOf<ValidationException>());
_softwareStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>()), Times.Never);
}
[Test]
public void GetAllSoftwaresByManufacturer_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_softwareStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetAllSoftwaresByManufacturer_StorageThrowError_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetAllSoftwaresByManufacturer(Guid.NewGuid().ToString(), It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_softwareStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSoftwareHistoryBySoftware_ReturnListOfRecords_Test()
{
//Arrange
var softwareId = Guid.NewGuid().ToString();
var listOriginal = new List<SoftwareHistoryDataModel>()
{
new(Guid.NewGuid().ToString(), 10),
new(Guid.NewGuid().ToString(), 15),
new(Guid.NewGuid().ToString(), 10),
};
_softwareStorageContract.Setup(x =>
x.GetHistoryBySoftwareId(It.IsAny<string>())).Returns(listOriginal);
//Act
var list =
_softwareBusinessLogicContract.GetSoftwareHistoryBySoftware(softwareId);
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(listOriginal));
_softwareStorageContract.Verify(x =>
x.GetHistoryBySoftwareId(softwareId), Times.Once);
}
[Test]
public void GetSoftwareHistoryBySoftware_ReturnEmptyList_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.GetHistoryBySoftwareId(It.IsAny<string>())).Returns([]);
//Act
var list =
_softwareBusinessLogicContract.GetSoftwareHistoryBySoftware(Guid.NewGuid().ToString(
));
//Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_softwareStorageContract.Verify(x =>
x.GetHistoryBySoftwareId(It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetSoftwareHistoryBySoftware_SoftwareIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareHistoryBySoftware(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareHistoryBySoftware(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_softwareStorageContract.Verify(x =>
x.GetHistoryBySoftwareId(It.IsAny<string>()), Times.Never);
}
[Test]
public void
GetSoftwareHistoryBySoftware_SoftwareIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareHistoryBySoftware("softwareId"),
Throws.TypeOf<ValidationException>());
_softwareStorageContract.Verify(x =>
x.GetHistoryBySoftwareId(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSoftwareHistoryBySoftware_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareHistoryBySoftware(Guid.NewGuid().ToString(
)), Throws.TypeOf<NullListException>());
_softwareStorageContract.Verify(x =>
x.GetHistoryBySoftwareId(It.IsAny<string>()), Times.Once);
}
[Test]
public void
GetSoftwareHistoryBySoftware_StorageThrowError_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.GetHistoryBySoftwareId(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareHistoryBySoftware(Guid.NewGuid().ToString(
)), Throws.TypeOf<StorageException>());
_softwareStorageContract.Verify(x =>
x.GetHistoryBySoftwareId(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSoftwareByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new SoftwareDataModel(id, "name", SoftwareType.Windows,
Guid.NewGuid().ToString(), 10, false);
_softwareStorageContract.Setup(x =>
x.GetElementById(id)).Returns(record);
//Act
var element = _softwareBusinessLogicContract.GetSoftwareByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_softwareStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSoftwareByData_GetByName_ReturnRecord_Test()
{
//Arrange
var name = "name";
var record = new SoftwareDataModel(Guid.NewGuid().ToString(), name,
SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false);
_softwareStorageContract.Setup(x =>
x.GetElementByName(name)).Returns(record);
//Act
var element = _softwareBusinessLogicContract.GetSoftwareByData(name);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.SoftwareName, Is.EqualTo(name));
_softwareStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSoftwareByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_softwareStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Never);
_softwareStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetSoftwareByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareByData(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_softwareStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSoftwareByData_GetByName_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareByData("name"),
Throws.TypeOf<ElementNotFoundException>());
_softwareStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetSoftwareByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
_softwareStorageContract.Setup(x =>
x.GetElementByName(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
Assert.That(() =>
_softwareBusinessLogicContract.GetSoftwareByData("name"),
Throws.TypeOf<StorageException>());
_softwareStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_softwareStorageContract.Verify(x =>
x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertSoftware_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new SoftwareDataModel(Guid.NewGuid().ToString(), "name",
SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false);
_softwareStorageContract.Setup(x =>
x.AddElement(It.IsAny<SoftwareDataModel>()))
.Callback((SoftwareDataModel x) =>
{
flag = x.Id == record.Id && x.SoftwareName ==
record.SoftwareName && x.SoftwareType == record.SoftwareType &&
x.ManufacturerId == record.ManufacturerId && x.Price ==
record.Price && x.IsDeleted == record.IsDeleted;
});
//Act
_softwareBusinessLogicContract.InsertSoftware(record);
//Assert
_softwareStorageContract.Verify(x =>
x.AddElement(It.IsAny<SoftwareDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertSoftware_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.AddElement(It.IsAny<SoftwareDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.InsertSoftware(new(Guid.NewGuid().ToString(),
"name", SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false)),
Throws.TypeOf<ElementExistsException>());
_softwareStorageContract.Verify(x =>
x.AddElement(It.IsAny<SoftwareDataModel>()), Times.Once);
}
[Test]
public void InsertSoftware_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _softwareBusinessLogicContract.InsertSoftware(null),
Throws.TypeOf<ArgumentNullException>());
_softwareStorageContract.Verify(x =>
x.AddElement(It.IsAny<SoftwareDataModel>()), Times.Never);
}
[Test]
public void InsertSoftware_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _softwareBusinessLogicContract.InsertSoftware(new
SoftwareDataModel("id", "name", SoftwareType.Windows, Guid.NewGuid().ToString(),
10, false)), Throws.TypeOf<ValidationException>());
_softwareStorageContract.Verify(x =>
x.AddElement(It.IsAny<SoftwareDataModel>()), Times.Never);
}
[Test]
public void InsertSoftware_StorageThrowError_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.AddElement(It.IsAny<SoftwareDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.InsertSoftware(new(Guid.NewGuid().ToString(),
"name", SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false)),
Throws.TypeOf<StorageException>());
_softwareStorageContract.Verify(x =>
x.AddElement(It.IsAny<SoftwareDataModel>()), Times.Once);
}
[Test]
public void UpdateSoftware_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new SoftwareDataModel(Guid.NewGuid().ToString(), "name",
SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false);
_softwareStorageContract.Setup(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>()))
.Callback((SoftwareDataModel x) =>
{
flag = x.Id == record.Id && x.SoftwareName ==
record.SoftwareName && x.SoftwareType == record.SoftwareType &&
x.ManufacturerId == record.ManufacturerId && x.Price ==
record.Price && x.IsDeleted == record.IsDeleted;
});
//Act
_softwareBusinessLogicContract.UpdateSoftware(record);
//Assert
_softwareStorageContract.Verify(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateSoftware_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>())).Throws(new
ElementNotFoundException(""));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.UpdateSoftware(new(Guid.NewGuid().ToString(),
"name", SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false)),
Throws.TypeOf<ElementNotFoundException>());
_softwareStorageContract.Verify(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>()), Times.Once);
}
[Test]
public void UpdateSoftware_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.UpdateSoftware(new(Guid.NewGuid().ToString(),
"anme", SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false)),
Throws.TypeOf<ElementExistsException>());
_softwareStorageContract.Verify(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>()), Times.Once);
}
[Test]
public void UpdateSoftware_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _softwareBusinessLogicContract.UpdateSoftware(null),
Throws.TypeOf<ArgumentNullException>());
_softwareStorageContract.Verify(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>()), Times.Never);
}
[Test]
public void UpdateSoftware_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _softwareBusinessLogicContract.UpdateSoftware(new
SoftwareDataModel("id", "name", SoftwareType.Windows, Guid.NewGuid().ToString(),
10, false)), Throws.TypeOf<ValidationException>());
_softwareStorageContract.Verify(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>()), Times.Never);
}
[Test]
public void UpdateSoftware_StorageThrowError_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.UpdateSoftware(new(Guid.NewGuid().ToString(),
"name", SoftwareType.Windows, Guid.NewGuid().ToString(), 10, false)),
Throws.TypeOf<StorageException>());
_softwareStorageContract.Verify(x =>
x.UpdElement(It.IsAny<SoftwareDataModel>()), Times.Once);
}
[Test]
public void DeleteSoftware_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_softwareStorageContract.Setup(x => x.DelElement(It.Is((string x) => x
== id))).Callback(() => { flag = true; });
//Act
_softwareBusinessLogicContract.DeleteSoftware(id);
//Assert
_softwareStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteSoftware_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_softwareStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.DeleteSoftware(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_softwareStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void DeleteSoftware_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _softwareBusinessLogicContract.DeleteSoftware(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_softwareBusinessLogicContract.DeleteSoftware(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_softwareStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteSoftware_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _softwareBusinessLogicContract.DeleteSoftware("id"),
Throws.TypeOf<ValidationException>());
_softwareStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteSoftware_StorageThrowError_ThrowException_Test()
{
//Arrange
_softwareStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_softwareBusinessLogicContract.DeleteSoftware(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_softwareStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
}

View File

@@ -0,0 +1,766 @@
using Microsoft.Extensions.Logging;
using Moq;
using SmallSoftwareBusinessLogic.Implementations;
using SmallSoftwareContracts.DataModels;
using SmallSoftwareContracts.Exceptions;
using SmallSoftwareContracts.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SmallSoftwareTests.BusinessLogicsContractsTests;
[TestFixture]
internal class WorkerBusinessLogicContractTests
{
private WorkerBusinessLogicContract _workerBusinessLogicContract;
private Mock<IWorkerStorageContract> _workerStorageContract;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_workerStorageContract = new Mock<IWorkerStorageContract>();
_workerBusinessLogicContract = new
WorkerBusinessLogicContract(_workerStorageContract.Object, new
Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_workerStorageContract.Reset();
}
[Test]
public void GetAllWorkers_ReturnListOfRecords_Test()
{
//Arrange
var listOriginal = new List<WorkerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false),
new(Guid.NewGuid().ToString(), "fio 2",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
true),
new(Guid.NewGuid().ToString(), "fio 3",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive =
_workerBusinessLogicContract.GetAllWorkers(true);
var list = _workerBusinessLogicContract.GetAllWorkers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, null, null, null,
null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, null, null,
null, null), Times.Once);
}
[Test]
public void GetAllWorkers_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive =
_workerBusinessLogicContract.GetAllWorkers(true);
var list = _workerBusinessLogicContract.GetAllWorkers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null,
null, null, null, null), Times.Exactly(2));
}
[Test]
public void GetAllWorkers_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()),
Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkers_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkers(It.IsAny<bool>()),
Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(), null,
null, null, null, null), Times.Once);
}
[Test]
public void GetAllWorkersByPost_ReturnListOfRecords_Test()
{
//Arrange
var postId = Guid.NewGuid().ToString();
var listOriginal = new List<WorkerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false),
new(Guid.NewGuid().ToString(), "fio 2",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
true),
new(Guid.NewGuid().ToString(), "fio 3",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive =
_workerBusinessLogicContract.GetAllWorkersByPost(postId, true);
var list = _workerBusinessLogicContract.GetAllWorkersByPost(postId,
false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, postId, null,
null, null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, postId, null,
null, null, null), Times.Once);
}
[Test]
public void GetAllWorkersByPost_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive =
_workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(),
true);
var list =
_workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(),
false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Exactly(2));
}
[Test]
public void GetAllWorkersByPost_PostIdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByPost(null, It.IsAny<bool>()),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByPost(string.Empty, It.IsAny<bool>()),
Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllWorkersByPost_PostIdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByPost("postId", It.IsAny<bool>()),
Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllWorkersByPost_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(),
It.IsAny<bool>()), Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByPost_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(),
It.IsAny<bool>()), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByBirthDate_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<WorkerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false),
new(Guid.NewGuid().ToString(), "fio 2",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
true),
new(Guid.NewGuid().ToString(), "fio 3",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive =
_workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1),
true);
var list =
_workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddDays(1),
false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, null, date,
date.AddDays(1), null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, date,
date.AddDays(1), null, null), Times.Once);
}
[Test]
public void GetAllWorkersByBirthDate_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive =
_workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), true);
var list = _workerBusinessLogicContract.GetAllWorkers(false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(true, null,
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null,
It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), null, null), Times.Once);
}
[Test]
public void GetAllWorkersByBirthDate_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date,
It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddSeconds(-1),
It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllWorkersByBirthDate_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), It.IsAny<bool>()),
Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void
GetAllWorkersByBirthDate_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), It.IsAny<bool>()),
Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetAllWorkersByEmploymentDate_ReturnListOfRecords_Test()
{
//Arrange
var date = DateTime.UtcNow;
var listOriginal = new List<WorkerDataModel>()
{
new(Guid.NewGuid().ToString(), "fio 1",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false),
new(Guid.NewGuid().ToString(), "fio 2",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
true),
new(Guid.NewGuid().ToString(), "fio 3",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false),
};
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns(listOriginal);
//Act
var listOnlyActive =
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1),
true);
var list =
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddDays(1),
false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
Assert.That(list, Is.EquivalentTo(listOriginal));
});
_workerStorageContract.Verify(x => x.GetList(true, null, null, null,
date, date.AddDays(1)), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, null, null,
date, date.AddDays(1)), Times.Once);
}
[Test]
public void GetAllWorkersByEmploymentDate_ReturnEmptyList_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Returns([]);
//Act
var listOnlyActive =
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), true);
var list =
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), false);
//Assert
Assert.Multiple(() =>
{
Assert.That(listOnlyActive, Is.Not.Null);
Assert.That(list, Is.Not.Null);
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
Assert.That(list, Has.Count.EqualTo(0));
});
_workerStorageContract.Verify(x => x.GetList(true, null, null, null,
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
_workerStorageContract.Verify(x => x.GetList(false, null, null, null,
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void
GetAllWorkersByEmploymentDate_IncorrectDates_ThrowException_Test()
{
//Arrange
var date = DateTime.UtcNow;
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date,
It.IsAny<bool>()), Throws.TypeOf<IncorrectDatesException>());
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date,
date.AddSeconds(-1), It.IsAny<bool>()),
Throws.TypeOf<IncorrectDatesException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Never);
}
[Test]
public void GetAllWorkersByEmploymentDate_ReturnNull_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), It.IsAny<bool>()),
Throws.TypeOf<NullListException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void
GetAllWorkersByEmploymentDate_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow,
DateTime.UtcNow.AddDays(1), It.IsAny<bool>()),
Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(),
It.IsAny<DateTime?>(), It.IsAny<DateTime?>()), Times.Once);
}
[Test]
public void GetWorkerByData_GetById_ReturnRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var record = new WorkerDataModel(id, "fio",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false);
_workerStorageContract.Setup(x =>
x.GetElementById(id)).Returns(record);
//Act
var element = _workerBusinessLogicContract.GetWorkerByData(id);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_workerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_GetByFio_ReturnRecord_Test()
{
//Arrange
var fio = "fio";
var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio,
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false);
_workerStorageContract.Setup(x =>
x.GetElementByFIO(fio)).Returns(record);
//Act
var element = _workerBusinessLogicContract.GetWorkerByData(fio);
//Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.FIO, Is.EqualTo(fio));
_workerStorageContract.Verify(x =>
x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_EmptyData_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_workerBusinessLogicContract.GetWorkerByData(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
_workerStorageContract.Verify(x =>
x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_workerStorageContract.Verify(x =>
x.GetElementByFIO(It.IsAny<string>()), Times.Never);
}
[Test]
public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetWorkerByData("fio"),
Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Never);
_workerStorageContract.Verify(x =>
x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetWorkerByData_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x =>
x.GetElementById(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
_workerStorageContract.Setup(x =>
x.GetElementByFIO(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
Assert.That(() =>
_workerBusinessLogicContract.GetWorkerByData("fio"),
Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x =>
x.GetElementById(It.IsAny<string>()), Times.Once);
_workerStorageContract.Verify(x =>
x.GetElementByFIO(It.IsAny<string>()), Times.Once);
}
[Test]
public void InsertWorker_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false);
_workerStorageContract.Setup(x =>
x.AddElement(It.IsAny<WorkerDataModel>()))
.Callback((WorkerDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO &&
x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
x.EmploymentDate == record.EmploymentDate &&
x.IsDeleted == record.IsDeleted;
});
//Act
_workerBusinessLogicContract.InsertWorker(record);
//Assert
_workerStorageContract.Verify(x =>
x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void InsertWorker_RecordWithExistsData_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x =>
x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new
ElementExistsException("Data", "Data"));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false)), Throws.TypeOf<ElementExistsException>());
_workerStorageContract.Verify(x =>
x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void InsertWorker_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertWorker(null),
Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x =>
x.AddElement(It.IsAny<WorkerDataModel>()), Times.Never);
}
[Test]
public void InsertWorker_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.InsertWorker(new
WorkerDataModel("id", "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-
16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x =>
x.AddElement(It.IsAny<WorkerDataModel>()), Times.Never);
}
[Test]
public void InsertWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x =>
x.AddElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false)), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x =>
x.AddElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void UpdateWorker_CorrectRecord_Test()
{
//Arrange
var flag = false;
var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false);
_workerStorageContract.Setup(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()))
.Callback((WorkerDataModel x) =>
{
flag = x.Id == record.Id && x.FIO == record.FIO &&
x.PostId == record.PostId && x.BirthDate == record.BirthDate &&
x.EmploymentDate == record.EmploymentDate &&
x.IsDeleted == record.IsDeleted;
});
//Act
_workerBusinessLogicContract.UpdateWorker(record);
//Assert
_workerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x =>
x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new
ElementNotFoundException(""));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false)), Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void UpdateWorker_NullRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(null),
Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Never);
}
[Test]
public void UpdateWorker_InvalidRecord_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new
WorkerDataModel("id", "fio", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-
16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Never);
}
[Test]
public void UpdateWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x =>
x.UpdElement(It.IsAny<WorkerDataModel>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio",
Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now,
false)), Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x =>
x.UpdElement(It.IsAny<WorkerDataModel>()), Times.Once);
}
[Test]
public void DeleteWorker_CorrectRecord_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
var flag = false;
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x
== id))).Callback(() => { flag = true; });
//Act
_workerBusinessLogicContract.DeleteWorker(id);
//Assert
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteWorker_RecordWithIncorrectId_ThrowException_Test()
{
//Arrange
var id = Guid.NewGuid().ToString();
_workerStorageContract.Setup(x => x.DelElement(It.Is((string x) => x
!= id))).Throws(new ElementNotFoundException(id));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()),
Throws.TypeOf<ElementNotFoundException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
[Test]
public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteWorker(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() =>
_workerBusinessLogicContract.DeleteWorker(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteWorker_IdIsNotGuid_ThrowException_Test()
{
//Act&Assert
Assert.That(() => _workerBusinessLogicContract.DeleteWorker("id"),
Throws.TypeOf<ValidationException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Never);
}
[Test]
public void DeleteWorker_StorageThrowError_ThrowException_Test()
{
//Arrange
_workerStorageContract.Setup(x =>
x.DelElement(It.IsAny<string>())).Throws(new StorageException(new
InvalidOperationException()));
//Act&Assert
Assert.That(() =>
_workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_workerStorageContract.Verify(x => x.DelElement(It.IsAny<string>()),
Times.Once);
}
}

View File

@@ -11,12 +11,14 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SmallSoftwareBusinessLogic\SmallSoftwareBusinessLogic.csproj" />
<ProjectReference Include="..\SmallSoftwareContracts\SmallSoftwareContracts.csproj" />
</ItemGroup>