|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using FluentAssertions; |
| 9 | +using NoteBookmark.Domain; |
| 10 | +using NoteBookmark.SharedUI; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace NoteBookmark.BlazorApp.Tests.Tests; |
| 14 | + |
| 15 | +public class PostNoteClientTests |
| 16 | +{ |
| 17 | + private class TestHttpMessageHandler : HttpMessageHandler |
| 18 | + { |
| 19 | + public Func<HttpRequestMessage, HttpResponseMessage> Handler { get; set; } = null!; |
| 20 | + |
| 21 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 22 | + { |
| 23 | + return Task.FromResult(Handler(request)); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public async Task CreateReadingNotes_WithNullTags_ShouldNotThrowAndShouldFallbackToMiscellaneous() |
| 29 | + { |
| 30 | + // Arrange |
| 31 | + var handler = new TestHttpMessageHandler(); |
| 32 | + using var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost/") }; |
| 33 | + var postNoteClient = new PostNoteClient(client); |
| 34 | + |
| 35 | + var notesList = new List<ReadingNote> |
| 36 | + { |
| 37 | + new ReadingNote { Title = "Note 1", Tags = null, Category = null } |
| 38 | + }; |
| 39 | + |
| 40 | + handler.Handler = (req) => |
| 41 | + { |
| 42 | + if (req.RequestUri!.PathAndQuery.Contains("GetNextReadingNotesCounter")) |
| 43 | + { |
| 44 | + return new HttpResponseMessage(HttpStatusCode.OK) |
| 45 | + { |
| 46 | + Content = new StringContent("708") |
| 47 | + }; |
| 48 | + } |
| 49 | + if (req.RequestUri!.PathAndQuery.Contains("GetNotesForSummary/708")) |
| 50 | + { |
| 51 | + return new HttpResponseMessage(HttpStatusCode.OK) |
| 52 | + { |
| 53 | + Content = new StringContent(JsonSerializer.Serialize(notesList), System.Text.Encoding.UTF8, "application/json") |
| 54 | + }; |
| 55 | + } |
| 56 | + return new HttpResponseMessage(HttpStatusCode.NotFound); |
| 57 | + }; |
| 58 | + |
| 59 | + // Act |
| 60 | + var result = await postNoteClient.CreateReadingNotes(); |
| 61 | + |
| 62 | + // Assert |
| 63 | + result.Should().NotBeNull(); |
| 64 | + result.Notes.Should().ContainKey("Miscellaneous"); |
| 65 | + result.Notes["Miscellaneous"].Should().HaveCount(1); |
| 66 | + result.Notes["Miscellaneous"][0].Title.Should().Be("Note 1"); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task CreateReadingNotes_WithEmptyTags_ShouldNotThrowAndShouldFallbackToMiscellaneous() |
| 71 | + { |
| 72 | + // Arrange |
| 73 | + var handler = new TestHttpMessageHandler(); |
| 74 | + using var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost/") }; |
| 75 | + var postNoteClient = new PostNoteClient(client); |
| 76 | + |
| 77 | + var notesList = new List<ReadingNote> |
| 78 | + { |
| 79 | + new ReadingNote { Title = "Note 2", Tags = "", Category = null } |
| 80 | + }; |
| 81 | + |
| 82 | + handler.Handler = (req) => |
| 83 | + { |
| 84 | + if (req.RequestUri!.PathAndQuery.Contains("GetNextReadingNotesCounter")) |
| 85 | + { |
| 86 | + return new HttpResponseMessage(HttpStatusCode.OK) |
| 87 | + { |
| 88 | + Content = new StringContent("708") |
| 89 | + }; |
| 90 | + } |
| 91 | + if (req.RequestUri!.PathAndQuery.Contains("GetNotesForSummary/708")) |
| 92 | + { |
| 93 | + return new HttpResponseMessage(HttpStatusCode.OK) |
| 94 | + { |
| 95 | + Content = new StringContent(JsonSerializer.Serialize(notesList), System.Text.Encoding.UTF8, "application/json") |
| 96 | + }; |
| 97 | + } |
| 98 | + return new HttpResponseMessage(HttpStatusCode.NotFound); |
| 99 | + }; |
| 100 | + |
| 101 | + // Act |
| 102 | + var result = await postNoteClient.CreateReadingNotes(); |
| 103 | + |
| 104 | + // Assert |
| 105 | + result.Should().NotBeNull(); |
| 106 | + result.Notes.Should().ContainKey("Miscellaneous"); |
| 107 | + result.Notes["Miscellaneous"].Should().HaveCount(1); |
| 108 | + result.Notes["Miscellaneous"][0].Title.Should().Be("Note 2"); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public async Task CreateReadingNotes_WithValidTags_ShouldGroupByCategory() |
| 113 | + { |
| 114 | + // Arrange |
| 115 | + var handler = new TestHttpMessageHandler(); |
| 116 | + using var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost/") }; |
| 117 | + var postNoteClient = new PostNoteClient(client); |
| 118 | + |
| 119 | + var notesList = new List<ReadingNote> |
| 120 | + { |
| 121 | + new ReadingNote { Title = "Note 3", Tags = "cloud,dev", Category = null } |
| 122 | + }; |
| 123 | + |
| 124 | + handler.Handler = (req) => |
| 125 | + { |
| 126 | + if (req.RequestUri!.PathAndQuery.Contains("GetNextReadingNotesCounter")) |
| 127 | + { |
| 128 | + return new HttpResponseMessage(HttpStatusCode.OK) |
| 129 | + { |
| 130 | + Content = new StringContent("708") |
| 131 | + }; |
| 132 | + } |
| 133 | + if (req.RequestUri!.PathAndQuery.Contains("GetNotesForSummary/708")) |
| 134 | + { |
| 135 | + return new HttpResponseMessage(HttpStatusCode.OK) |
| 136 | + { |
| 137 | + Content = new StringContent(JsonSerializer.Serialize(notesList), System.Text.Encoding.UTF8, "application/json") |
| 138 | + }; |
| 139 | + } |
| 140 | + return new HttpResponseMessage(HttpStatusCode.NotFound); |
| 141 | + }; |
| 142 | + |
| 143 | + // Act |
| 144 | + var result = await postNoteClient.CreateReadingNotes(); |
| 145 | + |
| 146 | + // Assert |
| 147 | + result.Should().NotBeNull(); |
| 148 | + result.Notes.Should().ContainKey("Cloud"); |
| 149 | + result.Notes["Cloud"].Should().HaveCount(1); |
| 150 | + result.Notes["Cloud"][0].Title.Should().Be("Note 3"); |
| 151 | + } |
| 152 | +} |
0 commit comments