IntegrationContractTests.cs
3,277 bytes
| 1 | using System.Text.Json; |
|---|---|
| 2 | using SplitApp.Shared.Contracts.Users; |
| 3 | using SplitApp.Shared.Messaging; |
| 4 | using SplitApp.Shared.Messaging.Integration.Users; |
| 5 | using Xunit; |
| 6 | |
| 7 | namespace SplitApp.Shared.Messaging.Tests; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Wire-format tests for the integration messages that cross the RabbitMQ boundary. |
| 11 | /// These guard against accidental rename / type drift that would silently break the |
| 12 | /// Users service ↔ monolith RPC contract. |
| 13 | /// </summary> |
| 14 | public class IntegrationContractTests |
| 15 | { |
| 16 | [Fact] |
| 17 | public void GetUserByIdRequest_RoundTripsThroughJson() |
| 18 | { |
| 19 | var id = Guid.NewGuid(); |
| 20 | var req = new GetUserByIdRequest(id); |
| 21 | |
| 22 | var json = JsonSerializer.SerializeToUtf8Bytes(req); |
| 23 | var back = JsonSerializer.Deserialize<GetUserByIdRequest>(json); |
| 24 | |
| 25 | Assert.NotNull(back); |
| 26 | Assert.Equal(id, back!.UserId); |
| 27 | } |
| 28 | |
| 29 | [Fact] |
| 30 | public void GetUsersByIdsRequest_RoundTripsThroughJson() |
| 31 | { |
| 32 | var ids = new[] { Guid.NewGuid(), Guid.NewGuid() }; |
| 33 | var req = new GetUsersByIdsRequest(ids); |
| 34 | |
| 35 | var json = JsonSerializer.SerializeToUtf8Bytes(req); |
| 36 | var back = JsonSerializer.Deserialize<GetUsersByIdsRequest>(json); |
| 37 | |
| 38 | Assert.NotNull(back); |
| 39 | Assert.Equal(ids, back!.UserIds); |
| 40 | } |
| 41 | |
| 42 | [Fact] |
| 43 | public void UserDeletedEvent_RoundTripsThroughJson() |
| 44 | { |
| 45 | var id = Guid.NewGuid(); |
| 46 | var evt = new UserDeletedEvent(id); |
| 47 | |
| 48 | var json = JsonSerializer.SerializeToUtf8Bytes(evt); |
| 49 | var back = JsonSerializer.Deserialize<UserDeletedEvent>(json); |
| 50 | |
| 51 | Assert.NotNull(back); |
| 52 | Assert.Equal(id, back!.UserId); |
| 53 | } |
| 54 | |
| 55 | [Fact] |
| 56 | public void UserDto_RoundTripsThroughJson() |
| 57 | { |
| 58 | var dto = new UserDto(Guid.NewGuid(), "Alice Johnson", "alice@example.com"); |
| 59 | var json = JsonSerializer.SerializeToUtf8Bytes(dto); |
| 60 | var back = JsonSerializer.Deserialize<UserDto>(json); |
| 61 | |
| 62 | Assert.NotNull(back); |
| 63 | Assert.Equal(dto.Id, back!.Id); |
| 64 | Assert.Equal(dto.DisplayName, back.DisplayName); |
| 65 | Assert.Equal(dto.Email, back.Email); |
| 66 | } |
| 67 | |
| 68 | [Fact] |
| 69 | public void GetUserByIdRequest_ImplementsIntegrationRequestOfUserDto() |
| 70 | { |
| 71 | Assert.True(typeof(IIntegrationRequest<UserDto?>).IsAssignableFrom(typeof(GetUserByIdRequest))); |
| 72 | } |
| 73 | |
| 74 | [Fact] |
| 75 | public void GetUsersByIdsRequest_ImplementsIntegrationRequestOfUserDtoArray() |
| 76 | { |
| 77 | Assert.True(typeof(IIntegrationRequest<UserDto[]>).IsAssignableFrom(typeof(GetUsersByIdsRequest))); |
| 78 | } |
| 79 | |
| 80 | [Fact] |
| 81 | public void UserDeletedEvent_ImplementsIntegrationEvent() |
| 82 | { |
| 83 | Assert.True(typeof(IIntegrationEvent).IsAssignableFrom(typeof(UserDeletedEvent))); |
| 84 | } |
| 85 | |
| 86 | [Fact] |
| 87 | public void MessagingOptions_DefaultsAreReasonable() |
| 88 | { |
| 89 | var opts = new MessagingOptions(); |
| 90 | Assert.Equal("localhost", opts.HostName); |
| 91 | Assert.Equal(5672, opts.Port); |
| 92 | Assert.Equal("guest", opts.UserName); |
| 93 | Assert.Equal("/", opts.VirtualHost); |
| 94 | Assert.True(opts.RpcReplyTimeoutSeconds > 0); |
| 95 | } |
| 96 | |
| 97 | [Fact] |
| 98 | public void RabbitMqTopology_QueueNamesAreStableAndScoped() |
| 99 | { |
| 100 | var name = RabbitMqTopology.HandlerQueueName("users-service", "GetUserByIdRequest"); |
| 101 | Assert.Equal("q.users-service.GetUserByIdRequest", name); |
| 102 | } |
| 103 | } |
| 104 | |