forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
288 lines
10 KiB
C#
288 lines
10 KiB
C#
using MagicCarpetContracts.BindingModels;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetDatabase.Models;
|
|
using MagicCarpetTests.Infrastructure;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetTests.WebApiControllersTests;
|
|
|
|
[TestFixture]
|
|
internal class AgencyControllerTests : BaseWebApiControllerTest
|
|
{
|
|
private string _componentId;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_componentId = MagicCarpetDbContext.InsertTourToDatabaseAndReturn().Id;
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
MagicCarpetDbContext.RemoveToursFromDatabase();
|
|
MagicCarpetDbContext.RemoveAgenciesFromDatabase();
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetAllAgencys_WhenHaveRecords_ShouldSuccess_Test()
|
|
{
|
|
// Arrange
|
|
var agency = MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
|
// Act
|
|
var response = await HttpClient.GetAsync("/api/agency");
|
|
// Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<AgencyViewModel>>(response);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(3));
|
|
});
|
|
AssertElement(data.First(x => x.Id == agency.Id), agency);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetList_WhenNoRecords_ShouldSuccess_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.GetAsync("/api/agency");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<AgencyViewModel>>(response);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(0));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var agency = MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/agency/{agency.Id}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
AssertElement(await GetModelFromResponseAsync<AgencyViewModel>(response), agency);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
|
{
|
|
//Arrange
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/agency/{Guid.NewGuid()}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn(tours: [(_componentId, 5)]);
|
|
var agency = CreateModel(components: [(_componentId, 5)]);
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/agency", MakeContent(agency));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
AssertElement(MagicCarpetDbContext.GetAgencyFromDatabaseById(agency.Id!), agency);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/agency", MakeContent(string.Empty));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/agency", MakeContent(new { Data = "test", Position = 10 }));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var furnitureIdIncorrect = new AgencyBindingModel { Id = "Id" };
|
|
var furnitureWithNameIncorrect = new AgencyBindingModel { Id = Guid.NewGuid().ToString(), TourType = TourType.None };
|
|
//Act
|
|
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/agency", MakeContent(furnitureIdIncorrect));
|
|
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/agency", MakeContent(furnitureWithNameIncorrect));
|
|
//Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
|
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
|
var agency = CreateModel(components: [(_componentId, 5)]);
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn(agency.Id);
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/agency", MakeContent(agency));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
MagicCarpetDbContext.ChangeTracker.Clear();
|
|
AssertElement(MagicCarpetDbContext.GetAgencyFromDatabaseById(agency.Id!), agency);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var furniture = CreateModel();
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/agency", MakeContent(furniture));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/agency", MakeContent(string.Empty));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/agency", MakeContent(new { Data = "test", Position = 10 }));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var component = Guid.NewGuid().ToString();
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn(component);
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/agency/{component}");
|
|
//Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
Assert.That(MagicCarpetDbContext.GetSaleFromDatabaseById(component), Is.Null);
|
|
});
|
|
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
MagicCarpetDbContext.InsertAgencyToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/agency/{Guid.NewGuid()}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/agency/id");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
private static AgencyBindingModel CreateModel(string? id = null, TourType tourType = TourType.Beach, int count = 21, List<(string, int)>? components = null)
|
|
{
|
|
var agency = new AgencyBindingModel() { Id = id ?? Guid.NewGuid().ToString(), TourType = tourType, Count = count, Tours = [] };
|
|
if (components is not null)
|
|
{
|
|
foreach (var elem in components)
|
|
{
|
|
agency.Tours.Add(new TourAgencyBindingModel { AgencyId = agency.Id, TourId = elem.Item1, Count = elem.Item2 });
|
|
}
|
|
}
|
|
return agency;
|
|
}
|
|
|
|
private static void AssertElement(AgencyViewModel? actual, Agency expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
|
});
|
|
if (expected.Tours is not null)
|
|
{
|
|
Assert.That(actual.Components, Is.Not.Null);
|
|
Assert.That(actual.Components, Has.Count.EqualTo(expected.Tours.Count));
|
|
for (int i = 0; i < actual.Components.Count; ++i)
|
|
{
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Components[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
|
Assert.That(actual.Components[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Assert.That(actual.Components, Is.Null);
|
|
}
|
|
}
|
|
|
|
private static void AssertElement(Agency? actual, AgencyBindingModel expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.Count, Is.EqualTo(expected.Count));
|
|
});
|
|
if (expected.Tours is not null)
|
|
{
|
|
Assert.That(actual.Tours, Is.Not.Null);
|
|
Assert.That(actual.Tours, Has.Count.EqualTo(expected.Tours.Count));
|
|
for (int i = 0; i < actual.Tours.Count; ++i)
|
|
{
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
|
|
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.Tours[i].Count));
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Assert.That(actual.Tours, Is.Null);
|
|
}
|
|
}
|
|
}
|