472 lines
19 KiB
C#
472 lines
19 KiB
C#
using SmallSoftwareContracts.BindingModels;
|
|
using SmallSoftwareContracts.Enums;
|
|
using SmallSoftwareContracts.ViewModels;
|
|
using SmallSoftwareDatabase.Models;
|
|
using SmallSoftwareTests.Infrastructure;
|
|
using System.Net;
|
|
|
|
namespace SmallSoftwareTests.WebApiControllersApi;
|
|
|
|
|
|
[TestFixture]
|
|
internal class PostControllerTests : BaseWebApiControllerTest
|
|
{
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
SmallSoftwareDbContext.RemovePostsFromDatabase();
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetRecords_WhenHaveRecords_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var post = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postName: "name 2");
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postName: "name 3");
|
|
//Act
|
|
var response = await HttpClient.GetAsync("/api/posts");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(3));
|
|
});
|
|
AssertElement(data.First(x => x.Id == post.PostId), post);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetRecords_WhenNoRecords_ShouldSuccess_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.GetAsync("/api/posts");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<ManufacturerViewModel>>(response);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(0));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetHistory_WhenHaveRecords_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var postId = Guid.NewGuid().ToString();
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
|
|
var post = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posthistory/{postId}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(2));
|
|
});
|
|
AssertElement(data.First(x => x.Id == post.PostId), post);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetHistory_WhenNoRecords_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var postId = Guid.NewGuid().ToString();
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posthistory/{Guid.NewGuid()}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(data, Is.Not.Null);
|
|
Assert.That(data, Has.Count.EqualTo(0));
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetHistory_WhenWrongData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posthistory/id");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ById_WhenHaveRecord_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var post = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posts/{post.PostId}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
AssertElement(await GetModelFromResponseAsync<PostViewModel>(response), post);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ById_WhenNoRecord_ShouldNotFound_Test()
|
|
{
|
|
//Arrange
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posts/{Guid.NewGuid()}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ById_WhenRecordWasDeleted_ShouldNotFound_Test()
|
|
{
|
|
//Arrange
|
|
var post = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posts/{post.PostId}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ByName_WhenHaveRecord_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var post = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posts/{post.PostName}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
AssertElement(await GetModelFromResponseAsync<PostViewModel>(response), post);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ByName_WhenNoRecord_ShouldNotFound_Test()
|
|
{
|
|
//Arrange
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posts/New%20Name");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetElement_ByName_WhenRecordWasDeleted_ShouldNotFound_Test()
|
|
{
|
|
//Arrange
|
|
var post = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(isActual: false);
|
|
//Act
|
|
var response = await HttpClient.GetAsync($"/api/posts/{post.PostName}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn();
|
|
var postModel = CreateModel();
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
AssertElement(SmallSoftwareDbContext.GetPostFromDatabaseByPostId(postModel.Id!), postModel);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenHaveRecordWithSameId_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var postModel = CreateModel();
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postModel.Id);
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenHaveRecordWithSameName_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var postModel = CreateModel(postName: "unique name");
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postName: postModel.PostName!);
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Supervisor.ToString(), Salary = 10 };
|
|
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Supervisor.ToString(), Salary = 10 };
|
|
var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, Salary = 10 };
|
|
var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Supervisor.ToString(), Salary = -10 };
|
|
//Act
|
|
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithIdIncorrect));
|
|
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithNameIncorrect));
|
|
var responseWithPostTypeIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithPostTypeIncorrect));
|
|
var responseWithSalaryIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithSalaryIncorrect));
|
|
//Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
|
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
|
|
Assert.That(responseWithPostTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
|
Assert.That(responseWithSalaryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Salary is incorrect");
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task Post_WhenSendEmptyData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PostAsync($"/api/posts", 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/posts", MakeContent(new { Data = "test", Position = 10 }));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var postModel = CreateModel();
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postModel.Id);
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
SmallSoftwareDbContext.ChangeTracker.Clear();
|
|
AssertElement(SmallSoftwareDbContext.GetPostFromDatabaseByPostId(postModel.Id!), postModel);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenNoFoundRecord_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var postModel = CreateModel();
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenRecordWasDeleted_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var postModel = CreateModel();
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postModel.Id, isActual: false);
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenHaveRecordWithSameName_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var postModel = CreateModel(postName: "unique name");
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postModel.Id);
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(postName: postModel.PostName!);
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Supervisor.ToString(), Salary = 10 };
|
|
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Supervisor.ToString(), Salary = 10 };
|
|
var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, Salary = 10 };
|
|
var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Supervisor.ToString(), Salary = -10 };
|
|
//Act
|
|
var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithIdIncorrect));
|
|
var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithNameIncorrect));
|
|
var responseWithPostTypeIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithPostTypeIncorrect));
|
|
var responseWithSalaryIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithSalaryIncorrect));
|
|
//Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
|
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
|
|
Assert.That(responseWithPostTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Type is incorrect");
|
|
Assert.That(responseWithSalaryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Salary is incorrect");
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task Put_WhenSendEmptyData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PutAsync($"/api/posts", 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/posts", MakeContent(new { Data = "test", Position = 10 }));
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var postId = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn().PostId;
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/posts/{postId}");
|
|
//Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
Assert.That(SmallSoftwareDbContext.GetPostFromDatabaseByPostId(postId), Is.Null);
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_WhenNoFoundRecord_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/posts/{Guid.NewGuid()}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_WhenRecordWasDeleted_Test()
|
|
{
|
|
//Arrange
|
|
var postId = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(isActual: false).PostId;
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/posts/{postId}");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.DeleteAsync($"/api/posts/id");
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Patch_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var postId = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn(isActual: false).PostId;
|
|
//Act
|
|
var response = await HttpClient.PatchAsync($"/api/posts/{postId}", null);
|
|
//Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
Assert.That(SmallSoftwareDbContext.GetPostFromDatabaseByPostId(postId), Is.Not.Null);
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task Patch_WhenNoFoundRecord_ShouldBadRequest_Test()
|
|
{
|
|
//Arrange
|
|
SmallSoftwareDbContext.InsertPostToDatabaseAndReturn();
|
|
//Act
|
|
var response = await HttpClient.PatchAsync($"/api/posts/{Guid.NewGuid()}", null);
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Patch_WhenRecordNotWasDeleted_ShouldSuccess_Test()
|
|
{
|
|
//Arrange
|
|
var postId = SmallSoftwareDbContext.InsertPostToDatabaseAndReturn().PostId;
|
|
//Act
|
|
var response = await HttpClient.PatchAsync($"/api/posts/{postId}", null);
|
|
//Assert
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
Assert.That(SmallSoftwareDbContext.GetPostFromDatabaseByPostId(postId), Is.Not.Null);
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task Patch_WhenSendWrongFormatData_ShouldBadRequest_Test()
|
|
{
|
|
//Act
|
|
var response = await HttpClient.PatchAsync($"/api/posts/id", null);
|
|
//Assert
|
|
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
private static void AssertElement(PostViewModel? actual, Post expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.PostId));
|
|
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
|
Assert.That(actual.PostType, Is.EqualTo(expected.PostType.ToString()));
|
|
Assert.That(actual.Salary, Is.EqualTo(expected.Salary));
|
|
});
|
|
}
|
|
|
|
private static PostBindingModel CreateModel(string? postId = null, string postName = "name", PostType postType = PostType.Supervisor, double salary = 10)
|
|
=> new()
|
|
{
|
|
Id = postId ?? Guid.NewGuid().ToString(),
|
|
PostName = postName,
|
|
PostType = postType.ToString(),
|
|
Salary = salary
|
|
};
|
|
|
|
private static void AssertElement(Post? actual, PostBindingModel expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.PostId, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
|
Assert.That(actual.PostType.ToString(), Is.EqualTo(expected.PostType));
|
|
Assert.That(actual.Salary, Is.EqualTo(expected.Salary));
|
|
});
|
|
}
|
|
}
|