Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

93 righe
3.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Security.Claims;
  6. using System.Threading.Tasks;
  7. using Cap.Consistency.Infrastructure;
  8. using Microsoft.AspNetCore.Builder;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.Logging;
  12. using Xunit;
  13. namespace Cap.Consistency.Test
  14. {
  15. public abstract class MessageManagerTestBase<TMessage> : MessageManagerTestBase<TMessage, string>
  16. where TMessage : ConsistencyMessage
  17. {
  18. }
  19. public abstract class MessageManagerTestBase<TMessage, TKey>
  20. where TMessage : ConsistencyMessage
  21. where TKey : IEquatable<TKey>
  22. {
  23. private const string NullValue = "(null)";
  24. protected virtual bool ShouldSkipDbTests() {
  25. return false;
  26. }
  27. protected virtual void SetupMessageServices(IServiceCollection services, object context = null) {
  28. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  29. services.AddConsistency<TMessage>();
  30. AddMessageStore(services, context);
  31. services.AddSingleton<ILogger<ConsistencyMessageManager<TMessage>>>(new TestLogger<ConsistencyMessageManager<TMessage>>());
  32. }
  33. protected virtual ConsistencyMessageManager<TMessage> CreateManager(object context = null, IServiceCollection services = null, Action<IServiceCollection> configureServices = null) {
  34. if (services == null) {
  35. services = new ServiceCollection();
  36. }
  37. if (context == null) {
  38. context = CreateTestContext();
  39. }
  40. SetupMessageServices(services, context);
  41. configureServices?.Invoke(services);
  42. return services.BuildServiceProvider().GetService<ConsistencyMessageManager<TMessage>>();
  43. }
  44. protected abstract object CreateTestContext();
  45. protected abstract TMessage CreateTestMessage(string payload = "");
  46. protected abstract void AddMessageStore(IServiceCollection services, object context = null);
  47. [Fact]
  48. public async Task CanDeleteMessage() {
  49. if (ShouldSkipDbTests()) {
  50. return;
  51. }
  52. var manager = CreateManager();
  53. var message = CreateTestMessage();
  54. var operateResult = await manager.CreateAsync(message);
  55. Assert.NotNull(operateResult);
  56. Assert.True(operateResult.Succeeded);
  57. var messageId = await manager.GetMessageIdAsync(message);
  58. operateResult = await manager.DeleteAsync(message);
  59. Assert.Null(await manager.FindByIdAsync(messageId));
  60. }
  61. [Fact]
  62. public async Task CanFindById() {
  63. if (ShouldSkipDbTests()) {
  64. return;
  65. }
  66. var manager = CreateManager();
  67. var message = CreateTestMessage();
  68. var operateResult = await manager.CreateAsync(message);
  69. Assert.NotNull(operateResult);
  70. Assert.True(operateResult.Succeeded);
  71. var messageId = await manager.GetMessageIdAsync(message);
  72. Assert.NotNull(await manager.FindByIdAsync(messageId));
  73. }
  74. }
  75. }