diff --git a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs index 1aa0df8..bf9071d 100644 --- a/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs +++ b/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiver.cs @@ -9,6 +9,7 @@ using System.Threading; using System.Threading.Tasks; using AutoFixture; using AutoFixture.Xunit2; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MongoDB.Bson; @@ -152,7 +153,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers { Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), Name = "N4m3", - Description = "someting", + Description = "something", CreatedAt = DateTime.Parse("2020-02-01") }; @@ -195,7 +196,146 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers // Assert Assert.Equal(HttpStatusCode.BadRequest, httpResponse.StatusCode); } - - // TODO test list + + [Fact] + public async Task Test_List_Ok() + { + // Arrange + await _mongoDb.DropAsync(); + var feedbackReceivers = new List + { + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3", + Description = "something", + CreatedAt = DateTime.UnixEpoch + }, + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3_Two", + Description = "something", + CreatedAt = DateTime.UnixEpoch + }, + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3_Three", + Description = "something", + CreatedAt = DateTime.UnixEpoch + } + }; + + await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers); + + + // Test + var httpResponse = await _client.GetAsync("/api/v1/FeedbackReceivers/", CancellationToken.None); + var items = JsonSerializer.Deserialize>( + await httpResponse.Content.ReadAsStringAsync()); + + // Assert + Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); + for (var i = 0; i < feedbackReceivers.Count; i++) + { + Assert.Equal(feedbackReceivers[i], items[i]); + } + } + + [Fact] + public async Task Test_List_Ok_Filter() + { + // Arrange + await _mongoDb.DropAsync(); + var feedbackReceivers = new List + { + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3", + Description = "something", + CreatedAt = DateTime.UnixEpoch + }, + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3_Two", + Description = "something", + CreatedAt = DateTime.UnixEpoch + }, + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3_Three", + Description = "something", + CreatedAt = DateTime.UnixEpoch + } + }; + + await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers); + + + // Test + var qb = new QueryBuilder {{"filter", new[] {feedbackReceivers[0].Id, feedbackReceivers[1].Id}}}; + var httpResponse = + await _client.GetAsync("/api/v1/FeedbackReceivers/" + qb, + CancellationToken.None); + var items = JsonSerializer.Deserialize>( + await httpResponse.Content.ReadAsStringAsync()); + + // Assert + Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); + Assert.Equal(2, items.Count); + for (var i = 0; i < 2; i++) + { + Assert.Equal(feedbackReceivers[i], items[i]); + } + } + + [Fact] + public async Task Test_List_Ok_LimitOffset() + { + // Arrange + await _mongoDb.DropAsync(); + var feedbackReceivers = new List + { + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3", + Description = "something", + CreatedAt = DateTime.UnixEpoch + }, + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3_Two", + Description = "something", + CreatedAt = DateTime.UnixEpoch + }, + new FeedbackReceiver + { + Id = new BsonObjectId(new ObjectId(new byte[] {3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14})).ToString(), + Name = "N4m3_Three", + Description = "something", + CreatedAt = DateTime.UnixEpoch + } + }; + + await _mongoDb.FeedbackReceiverCollection.InsertManyAsync(feedbackReceivers); + + + // Test + var qb = new QueryBuilder {{"offset", "1"}, {"limit", "1"}}; + var httpResponse = await _client.GetAsync("/api/v1/FeedbackReceivers/" + qb, CancellationToken.None); + var items = JsonSerializer.Deserialize>( + await httpResponse.Content.ReadAsStringAsync()); + + // Assert + Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); + Assert.Single(items); + Assert.Equal(feedbackReceivers[1], items[0]); + } } } \ No newline at end of file diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs index 4d3109e..6c61769 100644 --- a/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Controllers/TestFeedbackReceiverController.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using AutoFixture.Xunit2; using Microsoft.AspNetCore.Mvc; @@ -96,7 +97,21 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Controllers mockService.Verify(s => s.FindAsync(new[] {"bad_guid_but_unit_test_works_cause_validation_doesnt"}, null, null), Times.Once); } - - // TODO: test list + + [Theory, AutoData] + public async Task List_Ok(IEnumerable filter, int offset, int limit) + { + // Arrange + var mapper = TestUtils.GetMapper(); + var mockService = new Mock(); + var filterArr = filter as string[] ?? filter.ToArray(); + + // Test + var controller = new FeedbackReceiversController(mockService.Object, mapper, null); + var result = await controller.List(filterArr, offset, limit); + + Assert.IsType(result); + mockService.Verify(s => s.FindAsync(filterArr, offset, limit), Times.Once); + } } } \ No newline at end of file diff --git a/Retroactiune.WebAPI/Models/FeedbackReceiver.cs b/Retroactiune.WebAPI/Models/FeedbackReceiver.cs index 1ca1c0e..a7b3a73 100644 --- a/Retroactiune.WebAPI/Models/FeedbackReceiver.cs +++ b/Retroactiune.WebAPI/Models/FeedbackReceiver.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json.Serialization; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; @@ -9,14 +10,30 @@ namespace Retroactiune.Models /// public class FeedbackReceiver { - [BsonId] + [BsonId, JsonPropertyName("id")] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } - public string Name { get; set; } + [JsonPropertyName("name")] public string Name { get; set; } - public string Description { get; set; } + [JsonPropertyName("description")] public string Description { get; set; } - public DateTime CreatedAt { get; set; } + [JsonPropertyName("createdAt")] public DateTime CreatedAt { get; set; } + + public override bool Equals(object obj) + { + if (!(obj is FeedbackReceiver convertedObj)) + { + return false; + } + + return Id.Equals(convertedObj.Id) && Name.Equals(convertedObj.Name) && + Description.Equals(convertedObj.Description) && CreatedAt.Equals(convertedObj.CreatedAt); + } + + public override int GetHashCode() + { + return base.GetHashCode(); + } } } \ No newline at end of file