|
1 | | -using Microsoft.AspNetCore.Http; |
| 1 | +using MediatR; |
| 2 | +using Microsoft.AspNetCore.Http; |
| 3 | +using EventFlow.Application.Features.Events.Commands.CreateEvent; |
| 4 | +using EventFlow.Application.Features.Events.Commands.DeleteEvent; |
| 5 | +using EventFlow.Application.Features.Events.Commands.UpdateEvent; |
| 6 | +using EventFlow.Application.Features.Events.Queries.GetAllEvents; |
| 7 | +using EventFlow.Application.Features.Events.Queries.GetEventById; |
| 8 | +using EventFlow.Core.Primitives; |
2 | 9 |
|
3 | 10 | namespace EventFlow_API.Tests.Controllers; |
4 | 11 |
|
5 | 12 | public class EventControllerTests |
6 | 13 | { |
7 | | - private readonly Mock<IEventService> _serviceMock; |
8 | 14 | private readonly EventController _controller; |
| 15 | + private readonly Mock<IMediator> _mockMediator; |
9 | 16 |
|
10 | 17 | public EventControllerTests() |
11 | 18 | { |
12 | | - _serviceMock = new Mock<IEventService>(); |
13 | | - _controller = new EventController(_serviceMock.Object); |
| 19 | + _mockMediator = new Mock<IMediator>(); |
| 20 | + _controller = new EventController(_mockMediator.Object); |
14 | 21 | _controller.ControllerContext = new ControllerContext |
15 | 22 | { |
16 | 23 | HttpContext = new DefaultHttpContext() |
17 | 24 | }; |
18 | 25 | } |
19 | 26 |
|
20 | 27 | [Fact] |
21 | | - public async Task PostAsync_ReturnsOk_WhenEventCreated() |
| 28 | + public async Task Create_ReturnsCreatedAtAction_WhenEventCreated() |
22 | 29 | { |
23 | | - var command = new EventCommand { Title = "Test", Description = "Test Desc", Date = DateTime.Now, Location = "Test Location", OrganizerId = 1 }; |
24 | | - var createdEvent = new Event { Id = 1, Title = "Test" }; |
| 30 | + var command = new CreateEventCommand("Test Event", "Description", DateTime.Now.AddDays(1), "Location", 1); |
| 31 | + var result = Result<int>.Success(1); |
25 | 32 |
|
26 | | - _serviceMock.Setup(s => s.CreateAsync(command)).ReturnsAsync(createdEvent); |
| 33 | + _mockMediator.Setup(m => m.Send(command, It.IsAny<CancellationToken>())).ReturnsAsync(result); |
27 | 34 |
|
28 | | - var result = await _controller.PostAsync(command); |
| 35 | + var actionResult = await _controller.Create(command, CancellationToken.None); |
29 | 36 |
|
30 | | - var okResult = Assert.IsType<OkObjectResult>(result); |
31 | | - var returnedEvent = Assert.IsType<Event>(okResult.Value); |
32 | | - Assert.Equal(1, returnedEvent.Id); |
| 37 | + var createdResult = Assert.IsType<CreatedAtActionResult>(actionResult); |
| 38 | + Assert.Equal(1, createdResult.RouteValues!["id"]); |
33 | 39 | } |
34 | 40 |
|
35 | 41 | [Fact] |
36 | | - public async Task PostAsync_ReturnsBadRequest_WhenCreationFails() |
| 42 | + public async Task Create_ReturnsError_WhenCreationFails() |
37 | 43 | { |
38 | | - var command = new EventCommand { Title = "Test", Description = "Test Desc", Date = DateTime.Now, Location = "Test Location", OrganizerId = 1 }; |
39 | | - _serviceMock.Setup(s => s.CreateAsync(command)).ReturnsAsync((Event)null!); |
| 44 | + var command = new CreateEventCommand("Test Event", "Description", DateTime.Now.AddDays(1), "Location", 1); |
| 45 | + var result = Result<int>.Failure(Error.Validation("Event.CreateFailed", "Failed to create event")); |
40 | 46 |
|
41 | | - var result = await _controller.PostAsync(command); |
| 47 | + _mockMediator.Setup(m => m.Send(command, It.IsAny<CancellationToken>())).ReturnsAsync(result); |
42 | 48 |
|
43 | | - Assert.IsType<BadRequestResult>(result); |
| 49 | + var actionResult = await _controller.Create(command, CancellationToken.None); |
| 50 | + |
| 51 | + Assert.IsType<BadRequestObjectResult>(actionResult); |
44 | 52 | } |
45 | 53 |
|
46 | 54 | [Fact] |
47 | | - public async Task UpdateAsync_ReturnsOk_WhenUpdated() |
| 55 | + public async Task Update_ReturnsOk_WhenUpdated() |
48 | 56 | { |
49 | | - var command = new EventCommand { Title = "Updated", Description = "Desc", Date = DateTime.Now, Location = "Loc", OrganizerId = 1 }; |
50 | | - var updatedEvent = new EventDTO { Id = 1, Title = "Updated" }; |
| 57 | + var command = new UpdateEventCommand(1, "Updated Event", "Description", DateTime.Now.AddDays(1), "Location"); |
| 58 | + var result = Result.Success(); |
51 | 59 |
|
52 | | - _serviceMock.Setup(s => s.UpdateAsync(1, command)).ReturnsAsync(updatedEvent); |
| 60 | + _mockMediator.Setup(m => m.Send(command, It.IsAny<CancellationToken>())).ReturnsAsync(result); |
53 | 61 |
|
54 | | - var result = await _controller.UpdateAsync(1, command); |
| 62 | + var actionResult = await _controller.Update(1, command, CancellationToken.None); |
55 | 63 |
|
56 | | - var okResult = Assert.IsType<OkObjectResult>(result); |
57 | | - var returnedEvent = Assert.IsType<EventDTO>(okResult.Value); |
58 | | - Assert.Equal(1, returnedEvent.Id); |
| 64 | + Assert.IsType<OkResult>(actionResult); |
59 | 65 | } |
60 | 66 |
|
61 | 67 | [Fact] |
62 | | - public async Task UpdateAsync_ReturnsNotFound_WhenUpdateFails() |
| 68 | + public async Task Update_ReturnsNotFound_WhenEventNotFound() |
63 | 69 | { |
64 | | - var command = new EventCommand { Title = "Updated" }; |
65 | | - _serviceMock.Setup(s => s.UpdateAsync(1, command)).ReturnsAsync((EventDTO)null!); |
| 70 | + var command = new UpdateEventCommand(1, "Updated Event", "Description", DateTime.Now.AddDays(1), "Location"); |
| 71 | + var result = Result.Failure(Error.NotFound("Event.NotFound", "Event not found")); |
| 72 | + |
| 73 | + _mockMediator.Setup(m => m.Send(command, It.IsAny<CancellationToken>())).ReturnsAsync(result); |
66 | 74 |
|
67 | | - var result = await _controller.UpdateAsync(1, command); |
| 75 | + var actionResult = await _controller.Update(1, command, CancellationToken.None); |
68 | 76 |
|
69 | | - Assert.IsType<NotFoundResult>(result); |
| 77 | + Assert.IsType<NotFoundObjectResult>(actionResult); |
70 | 78 | } |
71 | 79 |
|
72 | 80 | [Fact] |
73 | | - public async Task DeleteAsync_ReturnsOk_WhenDeleted() |
| 81 | + public async Task Delete_ReturnsOk_WhenDeleted() |
74 | 82 | { |
75 | | - _serviceMock.Setup(s => s.DeleteAsync(1)).ReturnsAsync(true); |
| 83 | + var command = new DeleteEventCommand(1); |
| 84 | + var result = Result.Success(); |
| 85 | + |
| 86 | + _mockMediator.Setup(m => m.Send(command, It.IsAny<CancellationToken>())).ReturnsAsync(result); |
76 | 87 |
|
77 | | - var result = await _controller.DeleteAsync(1); |
| 88 | + var actionResult = await _controller.Delete(1, CancellationToken.None); |
78 | 89 |
|
79 | | - var okResult = Assert.IsType<OkObjectResult>(result); |
80 | | - Assert.Equal(1, okResult.Value); |
| 90 | + Assert.IsType<OkResult>(actionResult); |
81 | 91 | } |
82 | 92 |
|
83 | 93 | [Fact] |
84 | | - public async Task DeleteAsync_ReturnsNotFound_WhenDeleteFails() |
| 94 | + public async Task Delete_ReturnsNotFound_WhenDeleteFails() |
85 | 95 | { |
86 | | - _serviceMock.Setup(s => s.DeleteAsync(1)).ReturnsAsync(false); |
| 96 | + var command = new DeleteEventCommand(1); |
| 97 | + var result = Result.Failure(Error.NotFound("Event.NotFound", "Event not found")); |
87 | 98 |
|
88 | | - var result = await _controller.DeleteAsync(1); |
| 99 | + _mockMediator.Setup(m => m.Send(command, It.IsAny<CancellationToken>())).ReturnsAsync(result); |
89 | 100 |
|
90 | | - Assert.IsType<NotFoundResult>(result); |
| 101 | + var actionResult = await _controller.Delete(1, CancellationToken.None); |
| 102 | + |
| 103 | + Assert.IsType<NotFoundObjectResult>(actionResult); |
91 | 104 | } |
92 | 105 |
|
93 | 106 | [Fact] |
94 | 107 | public async Task GetById_ReturnsOk_WhenEventExists() |
95 | 108 | { |
96 | | - var eventId = 1; |
97 | | - var eventDto = new EventDTO { Id = eventId, Title = "Test Event" }; |
98 | | - _serviceMock.Setup(s => s.GetByIdAsync(eventId)).ReturnsAsync(eventDto); |
| 109 | + var query = new GetEventByIdQuery(1); |
| 110 | + var evento = new EventDTO { Id = 1, Title = "Event 1" }; |
| 111 | + var result = Result<EventDTO>.Success(evento); |
| 112 | + |
| 113 | + _mockMediator.Setup(m => m.Send(query, It.IsAny<CancellationToken>())).ReturnsAsync(result); |
99 | 114 |
|
100 | | - var result = await _controller.GetEventByIdAsync(eventId); |
| 115 | + var actionResult = await _controller.GetById(1, CancellationToken.None); |
101 | 116 |
|
102 | | - var okResult = Assert.IsType<OkObjectResult>(result); |
| 117 | + var okResult = Assert.IsType<OkObjectResult>(actionResult); |
103 | 118 | var returnValue = Assert.IsType<EventDTO>(okResult.Value); |
104 | | - Assert.Equal(eventDto.Id, returnValue.Id); |
| 119 | + returnValue.Id.Should().Be(1); |
105 | 120 | } |
106 | 121 |
|
107 | 122 | [Fact] |
108 | | - public async Task GetAllEventsAsync_ReturnsNotFound_WhenNoEventsExist() |
| 123 | + public async Task GetAll_ReturnsOk_WithList() |
109 | 124 | { |
110 | 125 | var queryParameters = new QueryParameters(); |
111 | | - var emptyList = new List<EventDTO>(); |
112 | | - |
113 | | - var pagedResult = new PagedResult<EventDTO>(emptyList, 1, 10, 0); |
| 126 | + var query = new GetAllEventsQuery(queryParameters); |
| 127 | + var events = new List<EventDTO> { new() { Id = 1, Title = "Event 1" } }; |
| 128 | + var pagedResult = new PagedResult<EventDTO>(events, 1, 10, 1); |
114 | 129 |
|
115 | | - _serviceMock |
116 | | - .Setup(s => s.GetAllPagedEventsAsync(It.IsAny<QueryParameters>())) |
117 | | - .ReturnsAsync(pagedResult); |
| 130 | + _mockMediator.Setup(m => m.Send(query, It.IsAny<CancellationToken>())).ReturnsAsync(pagedResult); |
118 | 131 |
|
119 | | - var result = await _controller.GetAllEventsAsync(queryParameters); |
| 132 | + var actionResult = await _controller.GetAll(queryParameters, CancellationToken.None); |
120 | 133 |
|
121 | | - Assert.IsType<NotFoundResult>(result); |
| 134 | + var okResult = Assert.IsType<OkObjectResult>(actionResult); |
| 135 | + var returnValue = Assert.IsType<List<EventDTO>>(okResult.Value); |
| 136 | + returnValue.Should().HaveCount(1); |
122 | 137 | } |
123 | 138 | } |
0 commit comments