using ButcherShopBusinessLogic.Implementations; using Microsoft.Extensions.Logging; using Moq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TheButcherShopContracts.DataModels; using TheButcherShopContracts.Exceptions; using TheButcherShopContracts.StoragesContracts; namespace ButcherShopTests.BusinessLogicsContractsTests; internal class WorkerBusinessLogicContractTests { private WorkerBusinessLogicContract _workerBusinessLogicContract; private Mock _workerStorageContract; [OneTimeSetUp] public void OneTimeSetUp() { _workerStorageContract = new Mock(); _workerBusinessLogicContract = new WorkerBusinessLogicContract(_workerStorageContract.Object, new Mock().Object); } [SetUp] public void SetUp() { _workerStorageContract.Reset(); } [Test] public void GetAllWorkers_ReturnListOfRecords_Test() { //Arrange var listOriginal = new List() { new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), new(Guid.NewGuid().ToString(), "fio 2", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), new(Guid.NewGuid().ToString(), "fio 3", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), }; _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), null, null, null, null, null), Times.Exactly(2)); } [Test] public void GetAllWorkers_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllWorkers_StorageThrowError_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkers(It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), null, null, null, null, null), Times.Once); } [Test] public void GetAllWorkersByPost_ReturnListOfRecords_Test() { //Arrange var postId = Guid.NewGuid().ToString(); var listOriginal = new List() { new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), new(Guid.NewGuid().ToString(), "fio 2", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), new(Guid.NewGuid().ToString(), "fio 3", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), }; _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(2)); } [Test] public void GetAllWorkersByPost_PostIdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(null, It.IsAny()), Throws.TypeOf()); Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(string.Empty, It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void GetAllWorkersByPost_PostIdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost("postId", It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void GetAllWorkersByPost_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllWorkersByPost_StorageThrowError_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByPost(Guid.NewGuid().ToString(), It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllWorkersByBirthDate_ReturnListOfRecords_Test() { //Arrange var date = DateTime.UtcNow; var listOriginal = new List() { new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), new(Guid.NewGuid().ToString(), "fio 2", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), new(Guid.NewGuid().ToString(), "fio 3", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), }; _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), null, null), Times.Once); _workerStorageContract.Verify(x => x.GetList(false, null, It.IsAny(), It.IsAny(), 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()), Throws.TypeOf()); Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(date, date.AddSeconds(-1), It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void GetAllWorkersByBirthDate_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllWorkersByBirthDate_StorageThrowError_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByBirthDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllWorkersByEmploymentDate_ReturnListOfRecords_Test() { //Arrange var date = DateTime.UtcNow; var listOriginal = new List() { new(Guid.NewGuid().ToString(), "fio 1", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), new(Guid.NewGuid().ToString(), "fio 2", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, true), new(Guid.NewGuid().ToString(), "fio 3", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false), }; _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).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(), It.IsAny()), Times.Once); _workerStorageContract.Verify(x => x.GetList(false, null, null, null, It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllWorkersByEmploymentDate_IncorrectDates_ThrowException_Test() { //Arrange var date = DateTime.UtcNow; //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date, It.IsAny()), Throws.TypeOf()); Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(date, date.AddSeconds(-1), It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Test] public void GetAllWorkersByEmploymentDate_ReturnNull_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetAllWorkersByEmploymentDate_StorageThrowError_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetAllWorkersByEmploymentDate(DateTime.UtcNow, DateTime.UtcNow.AddDays(1), It.IsAny()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetList(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Test] public void GetWorkerByData_GetById_ReturnRecord_Test() { //Arrange var id = Guid.NewGuid().ToString(); var record = new WorkerDataModel(id, "fio", "+7-111-111-11-11", 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()), Times.Once); } [Test] public void GetWorkerByData_GetByFio_ReturnRecord_Test() { //Arrange var fio = "fio"; var record = new WorkerDataModel(Guid.NewGuid().ToString(), fio, "+7-111-111-11-11", 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()), Times.Once); } [Test] public void GetWorkerByData_GetByPhoneNumber_ReturnRecord_Test() { //Arrange var phoneNumber = "+7-111-111-11-11"; var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", phoneNumber, Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); _workerStorageContract.Setup(x => x.GetElementByPhoneNumber(phoneNumber)).Returns(record); //Act var element = _workerBusinessLogicContract.GetWorkerByData(phoneNumber); //Assert Assert.That(element, Is.Not.Null); Assert.That(element.PhoneNumber, Is.EqualTo(phoneNumber)); _workerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); } [Test] public void GetWorkerByData_EmptyData_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(null), Throws.TypeOf()); Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(string.Empty), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); _workerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); } [Test] public void GetWorkerByData_GetById_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); } [Test] public void GetWorkerByData_GetByFio_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); _workerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Never); } [Test] public void GetWorkerByData_GetByPhoneNumber_NotFoundRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("+7-111-111-11-12"), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Never); _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Never); _workerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); } [Test] public void GetWorkerByData_StorageThrowError_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.GetElementById(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); _workerStorageContract.Setup(x => x.GetElementByFIO(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); _workerStorageContract.Setup(x => x.GetElementByPhoneNumber(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _workerBusinessLogicContract.GetWorkerByData(Guid.NewGuid().ToString()), Throws.TypeOf()); Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("fio"), Throws.TypeOf()); Assert.That(() => _workerBusinessLogicContract.GetWorkerByData("+7-111-111-11-12"), Throws.TypeOf()); _workerStorageContract.Verify(x => x.GetElementById(It.IsAny()), Times.Once); _workerStorageContract.Verify(x => x.GetElementByFIO(It.IsAny()), Times.Once); _workerStorageContract.Verify(x => x.GetElementByPhoneNumber(It.IsAny()), Times.Once); } [Test] public void InsertWorker_CorrectRecord_Test() { //Arrange var flag = false; var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); _workerStorageContract.Setup(x => x.AddElement(It.IsAny())) .Callback((WorkerDataModel x) => { flag = x.Id == record.Id && x.FIO == record.FIO && x.PhoneNumber == record.PhoneNumber && 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()), Times.Once); Assert.That(flag); } [Test] public void InsertWorker_RecordWithExistsData_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new ElementExistsException("Data", "Data")); //Act&Assert Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void InsertWorker_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.InsertWorker(null), Throws.TypeOf()); _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertWorker_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.InsertWorker(new WorkerDataModel("id", "fio", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Never); } [Test] public void InsertWorker_StorageThrowError_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.AddElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _workerBusinessLogicContract.InsertWorker(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.AddElement(It.IsAny()), Times.Once); } [Test] public void UpdateWorker_CorrectRecord_Test() { //Arrange var flag = false; var record = new WorkerDataModel(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false); _workerStorageContract.Setup(x => x.UpdElement(It.IsAny())) .Callback((WorkerDataModel x) => { flag = x.Id == record.Id && x.FIO == record.FIO && x.PhoneNumber == record.PhoneNumber && 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()), Times.Once); Assert.That(flag); } [Test] public void UpdateWorker_RecordWithIncorrectData_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new ElementNotFoundException("")); //Act&Assert Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Once); } [Test] public void UpdateWorker_NullRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.UpdateWorker(null), Throws.TypeOf()); _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateWorker_InvalidRecord_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new WorkerDataModel("id", "fio", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), Times.Never); } [Test] public void UpdateWorker_StorageThrowError_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.UpdElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _workerBusinessLogicContract.UpdateWorker(new(Guid.NewGuid().ToString(), "fio", "+7-111-111-11-11", Guid.NewGuid().ToString(), DateTime.Now.AddYears(-16).AddDays(-1), DateTime.Now, false)), Throws.TypeOf()); _workerStorageContract.Verify(x => x.UpdElement(It.IsAny()), 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()), 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()); _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } [Test] public void DeleteWorker_IdIsNullOrEmpty_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.DeleteWorker(null), Throws.TypeOf()); Assert.That(() => _workerBusinessLogicContract.DeleteWorker(string.Empty), Throws.TypeOf()); _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteWorker_IdIsNotGuid_ThrowException_Test() { //Act&Assert Assert.That(() => _workerBusinessLogicContract.DeleteWorker("id"), Throws.TypeOf()); _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Never); } [Test] public void DeleteWorker_StorageThrowError_ThrowException_Test() { //Arrange _workerStorageContract.Setup(x => x.DelElement(It.IsAny())).Throws(new StorageException(new InvalidOperationException())); //Act&Assert Assert.That(() => _workerBusinessLogicContract.DeleteWorker(Guid.NewGuid().ToString()), Throws.TypeOf()); _workerStorageContract.Verify(x => x.DelElement(It.IsAny()), Times.Once); } }