using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading.Tasks; using AutoFixture; using AutoFixture.Xunit2; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MongoDB.Driver; using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures; using Retroactiune.Models; using Retroactiune.Settings; using Xunit; namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers { public class TestFeedbackReceiver : IClassFixture { private readonly MongoDbFixture _mongoDb; private readonly HttpClient _client; public TestFeedbackReceiver(WebApiTestingFactory factory) { _client = factory.CreateClient(); var dbSettings = factory.Services.GetService>(); _mongoDb = new MongoDbFixture(dbSettings); } [Fact] public async Task Test_CreateFeedbackReceiver_NoContent() { var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", new StringContent("[]", Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode); } [Fact] public async Task Test_CreateFeedbackReceiver_NoName() { // Arrange var fixture = new Fixture(); var item = fixture.Create(); item.Name = null; var jsonContent = JsonSerializer.Serialize(new List {item}); // Test var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", new StringContent(jsonContent, Encoding.UTF8, "application/json")); // Assert Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode); } [Fact] public async Task Test_CreateFeedbackReceiver_NoDescription() { // Arrange var fixture = new Fixture(); var item = fixture.Create(); item.Description = null; var jsonContent = JsonSerializer.Serialize(new List {item}); // Test var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", new StringContent(jsonContent, Encoding.UTF8, "application/json")); // Assert Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode); } [Theory, AutoData] public async Task Test_CreateFeedbackReceiver_Ok(IEnumerable items) { // Arrange await _mongoDb.DropAsync(); var feedbackReceiversDto = items.ToList(); var jsonContent = JsonSerializer.Serialize(feedbackReceiversDto); // Test var httpResponse = await _client.PostAsync("/api/v1/FeedbackReceivers/", new StringContent(jsonContent, Encoding.UTF8, "application/json")); // Assert httpResponse.EnsureSuccessStatusCode(); var filter = new FilterDefinitionBuilder().Empty; var createdDocs = await _mongoDb.FeedbackReceiverCollection.CountDocumentsAsync(filter); Assert.Equal(feedbackReceiversDto.Count(), createdDocs); } } }