356 lines
13 KiB
C#
356 lines
13 KiB
C#
using ClassLibrary1;
|
|
using Moq;
|
|
using BussinessLogick;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Test;
|
|
[TestFixture]
|
|
internal class AuthorBussinessTest
|
|
{
|
|
private AuthorBusinessLogic _authorBusinessLogic;
|
|
private Mock<IAuthorStorageContract> _authorStorageContract;
|
|
private Mock<ILogger> _loggerMock;
|
|
|
|
[OneTimeSetUp]
|
|
public void OneTimeSetUp()
|
|
{
|
|
_authorStorageContract = new Mock<IAuthorStorageContract>();
|
|
_loggerMock = new Mock<ILogger>();
|
|
_authorBusinessLogic = new AuthorBusinessLogic(_authorStorageContract.Object, _loggerMock.Object);
|
|
}
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_authorStorageContract.Reset();
|
|
}
|
|
|
|
// Тесты для GetAllAuthors
|
|
[Test]
|
|
public void GetAllAuthors_ReturnListOfRecords_Test()
|
|
{
|
|
// Arrange
|
|
var authorsList = new List<Author>
|
|
{
|
|
new Author(Guid.NewGuid().ToString(), "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()),
|
|
new Author(Guid.NewGuid().ToString(), "Author 2", "author2@example.com", DateTime.Now.AddYears(-25), Guid.NewGuid().ToString(), Guid.NewGuid().ToString())
|
|
};
|
|
_authorStorageContract.Setup(x => x.GetList()).Returns(authorsList);
|
|
|
|
// Act
|
|
var result = _authorBusinessLogic.GetAllAuthors();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result, Is.EquivalentTo(authorsList));
|
|
_authorStorageContract.Verify(x => x.GetList(), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllAuthors_ReturnEmptyList_Test()
|
|
{
|
|
// Arrange
|
|
_authorStorageContract.Setup(x => x.GetList()).Returns(new List<Author>());
|
|
|
|
// Act
|
|
var result = _authorBusinessLogic.GetAllAuthors();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result, Is.Empty);
|
|
_authorStorageContract.Verify(x => x.GetList(), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllAuthors_ReturnNull_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
_authorStorageContract.Setup(x => x.GetList()).Returns((List<Author>)null);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<NullListException>(() => _authorBusinessLogic.GetAllAuthors());
|
|
_authorStorageContract.Verify(x => x.GetList(), Times.Once());
|
|
}
|
|
|
|
|
|
|
|
// Тесты для GetAuthorByData
|
|
[Test]
|
|
public void GetAuthorByData_GetById_ReturnRecord_Test()
|
|
{
|
|
// Arrange
|
|
var id = Guid.NewGuid().ToString();
|
|
var author = new Author(id, "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementById(id)).Returns(author);
|
|
|
|
// Act
|
|
var result = _authorBusinessLogic.GetAuthorByData(id);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.Id, Is.EqualTo(id));
|
|
_authorStorageContract.Verify(x => x.GetElementById(id), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAuthorByData_GetByEmail_ReturnRecord_Test()
|
|
{
|
|
// Arrange
|
|
var email = "author1@example.com";
|
|
var author = new Author(Guid.NewGuid().ToString(), "Author 1", email, DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementByEmail(email)).Returns(author);
|
|
|
|
// Act
|
|
var result = _authorBusinessLogic.GetAuthorByData(email);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.Email, Is.EqualTo(email));
|
|
_authorStorageContract.Verify(x => x.GetElementByEmail(email), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAuthorByData_GetByFIO_ReturnRecord_Test()
|
|
{
|
|
// Arrange
|
|
var fio = "Author 1";
|
|
var author = new Author(Guid.NewGuid().ToString(), fio, "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns(author);
|
|
|
|
// Act
|
|
var result = _authorBusinessLogic.GetAuthorByData(fio);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.FIO, Is.EqualTo(fio));
|
|
_authorStorageContract.Verify(x => x.GetElementByFIO(fio), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAuthorByData_EmptyData_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentException>(() => _authorBusinessLogic.GetAuthorByData(null));
|
|
Assert.Throws<ArgumentException>(() => _authorBusinessLogic.GetAuthorByData(string.Empty));
|
|
_authorStorageContract.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Never());
|
|
_authorStorageContract.Verify(x => x.GetElementByEmail(It.IsAny<string>()), Times.Never());
|
|
_authorStorageContract.Verify(x => x.GetElementByFIO(It.IsAny<string>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAuthorByData_GetById_NotFound_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var id = Guid.NewGuid().ToString();
|
|
_authorStorageContract.Setup(x => x.GetElementById(id)).Returns((Author)null);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ElementNotFoundException>(() => _authorBusinessLogic.GetAuthorByData(id));
|
|
_authorStorageContract.Verify(x => x.GetElementById(id), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAuthorByData_GetByEmail_NotFound_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var email = "author1@example.com";
|
|
_authorStorageContract.Setup(x => x.GetElementByEmail(email)).Returns((Author)null);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ElementNotFoundException>(() => _authorBusinessLogic.GetAuthorByData(email));
|
|
_authorStorageContract.Verify(x => x.GetElementByEmail(email), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void GetAuthorByData_GetByFIO_NotFound_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var fio = "Author 1";
|
|
_authorStorageContract.Setup(x => x.GetElementByFIO(fio)).Returns((Author)null);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ElementNotFoundException>(() => _authorBusinessLogic.GetAuthorByData(fio));
|
|
_authorStorageContract.Verify(x => x.GetElementByFIO(fio), Times.Once());
|
|
}
|
|
|
|
// Тесты для InsertAuthor
|
|
[Test]
|
|
public void InsertAuthor_ValidAuthor_Success_Test()
|
|
{
|
|
// Arrange
|
|
var author = new Author(Guid.NewGuid().ToString(), "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementById(author.Id)).Returns((Author)null);
|
|
_authorStorageContract.Setup(x => x.GetElementByEmail(author.Email)).Returns((Author)null);
|
|
|
|
// Act
|
|
_authorBusinessLogic.InsertAuthor(author);
|
|
|
|
// Assert
|
|
_authorStorageContract.Verify(x => x.AddElement(author), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void InsertAuthor_DuplicateId_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var author = new Author(Guid.NewGuid().ToString(), "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementById(author.Id)).Returns(author);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ElementNotFoundException>(() => _authorBusinessLogic.InsertAuthor(author));
|
|
_authorStorageContract.Verify(x => x.AddElement(It.IsAny<Author>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void InsertAuthor_DuplicateEmail_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var author = new Author(Guid.NewGuid().ToString(), "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementById(author.Id)).Returns((Author)null);
|
|
_authorStorageContract.Setup(x => x.GetElementByEmail(author.Email)).Returns(author);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ElementNotFoundException>(() => _authorBusinessLogic.InsertAuthor(author));
|
|
_authorStorageContract.Verify(x => x.AddElement(It.IsAny<Author>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void InsertAuthor_NullAuthor_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentNullException>(() => _authorBusinessLogic.InsertAuthor(null));
|
|
_authorStorageContract.Verify(x => x.AddElement(It.IsAny<Author>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void InsertAuthor_InvalidAuthor_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var author = new Author("", "Author 1", "invalid_email", DateTime.Now.AddYears(-10), "", "");
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ValidationException>(() => _authorBusinessLogic.InsertAuthor(author));
|
|
_authorStorageContract.Verify(x => x.AddElement(It.IsAny<Author>()), Times.Never());
|
|
}
|
|
|
|
|
|
|
|
// Тесты для UpdateAuthor
|
|
[Test]
|
|
public void UpdateAuthor_ValidAuthor_Success_Test()
|
|
{
|
|
// Arrange
|
|
var author = new Author(Guid.NewGuid().ToString(), "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementById(author.Id)).Returns(author);
|
|
_authorStorageContract.Setup(x => x.GetElementByEmail(author.Email)).Returns((Author)null);
|
|
|
|
// Act
|
|
_authorBusinessLogic.UpdateAuthor(author);
|
|
|
|
// Assert
|
|
_authorStorageContract.Verify(x => x.UpdElement(author), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateAuthor_NonExistingAuthor_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var author = new Author(Guid.NewGuid().ToString(), "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementById(author.Id)).Returns((Author)null);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ElementNotFoundException>(() => _authorBusinessLogic.UpdateAuthor(author));
|
|
_authorStorageContract.Verify(x => x.UpdElement(It.IsAny<Author>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateAuthor_DuplicateEmail_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var author = new Author(Guid.NewGuid().ToString(), "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
var existingAuthor = new Author(Guid.NewGuid().ToString(), "Author 2", "author1@example.com", DateTime.Now.AddYears(-25), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementById(author.Id)).Returns(author);
|
|
_authorStorageContract.Setup(x => x.GetElementByEmail(author.Email)).Returns(existingAuthor);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ElementNotFoundException>(() => _authorBusinessLogic.UpdateAuthor(author));
|
|
_authorStorageContract.Verify(x => x.UpdElement(It.IsAny<Author>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateAuthor_NullAuthor_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentNullException>(() => _authorBusinessLogic.UpdateAuthor(null));
|
|
_authorStorageContract.Verify(x => x.UpdElement(It.IsAny<Author>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateAuthor_InvalidAuthor_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var author = new Author("", "Author 1", "invalid_email", DateTime.Now.AddYears(-10), "", "");
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ValidationException>(() => _authorBusinessLogic.UpdateAuthor(author));
|
|
_authorStorageContract.Verify(x => x.UpdElement(It.IsAny<Author>()), Times.Never());
|
|
}
|
|
|
|
|
|
|
|
// Тесты для DeleteAuthor
|
|
[Test]
|
|
public void DeleteAuthor_ValidId_Success_Test()
|
|
{
|
|
// Arrange
|
|
var id = Guid.NewGuid().ToString();
|
|
var author = new Author(id, "Author 1", "author1@example.com", DateTime.Now.AddYears(-30), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
|
_authorStorageContract.Setup(x => x.GetElementById(id)).Returns(author);
|
|
|
|
// Act
|
|
_authorBusinessLogic.DeleteAuthor(id);
|
|
|
|
// Assert
|
|
_authorStorageContract.Verify(x => x.DelElement(id), Times.Once());
|
|
}
|
|
|
|
[Test]
|
|
public void DeleteAuthor_NonExistingId_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var id = Guid.NewGuid().ToString();
|
|
_authorStorageContract.Setup(x => x.GetElementById(id)).Returns((Author)null);
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ElementNotFoundException>(() => _authorBusinessLogic.DeleteAuthor(id));
|
|
_authorStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void DeleteAuthor_EmptyId_ThrowException_Test()
|
|
{
|
|
// Act & Assert
|
|
Assert.Throws<ArgumentException>(() => _authorBusinessLogic.DeleteAuthor(null));
|
|
Assert.Throws<ArgumentException>(() => _authorBusinessLogic.DeleteAuthor(string.Empty));
|
|
_authorStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never());
|
|
}
|
|
|
|
[Test]
|
|
public void DeleteAuthor_InvalidId_ThrowException_Test()
|
|
{
|
|
// Arrange
|
|
var invalidId = "not-a-guid";
|
|
|
|
// Act & Assert
|
|
Assert.Throws<ValidationException>(() => _authorBusinessLogic.DeleteAuthor(invalidId));
|
|
_authorStorageContract.Verify(x => x.DelElement(It.IsAny<string>()), Times.Never());
|
|
}
|
|
|
|
|
|
}
|