|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Text; |
| 5 | +using Confluent.Kafka; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using Microsoft.Extensions.Hosting; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Aspire.Kafka.Consumer.Tests; |
| 12 | + |
| 13 | +public class AspireKafkaConsumerExtensionsTests |
| 14 | +{ |
| 15 | + [ConditionalTheory] |
| 16 | + [InlineData(true)] |
| 17 | + [InlineData(false)] |
| 18 | + public void ReadsFromConnectionStringsCorrectly(bool useKeyed) |
| 19 | + { |
| 20 | + var builder = Host.CreateEmptyApplicationBuilder(null); |
| 21 | + builder.Configuration.AddInMemoryCollection([ |
| 22 | + new KeyValuePair<string, string?>("ConnectionStrings:messaging", AspireKafkaConsumerHelpers.TestingEndpoint) |
| 23 | + ]); |
| 24 | + |
| 25 | + if (useKeyed) |
| 26 | + { |
| 27 | + builder.AddKeyedKafkaConsumer<string, string>("messaging"); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + builder.AddKafkaConsumer<string, string>("messaging"); |
| 32 | + } |
| 33 | + |
| 34 | + var host = builder.Build(); |
| 35 | + var ConsumerConfig = useKeyed ? |
| 36 | + host.Services.GetRequiredKeyedService<ConsumerConfig>("messaging") : |
| 37 | + host.Services.GetRequiredService<ConsumerConfig>(); |
| 38 | + |
| 39 | + Assert.Equal(AspireKafkaConsumerHelpers.TestingEndpoint, ConsumerConfig.BootstrapServers); |
| 40 | + } |
| 41 | + |
| 42 | + [ConditionalTheory] |
| 43 | + [InlineData(true)] |
| 44 | + [InlineData(false)] |
| 45 | + public void ConnectionStringCanBeSetInCode(bool useKeyed) |
| 46 | + { |
| 47 | + var builder = Host.CreateEmptyApplicationBuilder(null); |
| 48 | + builder.Configuration.AddInMemoryCollection([ |
| 49 | + new KeyValuePair<string, string?>("ConnectionStrings:messaging", "unused") |
| 50 | + ]); |
| 51 | + |
| 52 | + static void SetConnectionString(ConsumerConfig config) => config.BootstrapServers = AspireKafkaConsumerHelpers.TestingEndpoint; |
| 53 | + if (useKeyed) |
| 54 | + { |
| 55 | + builder.AddKeyedKafkaConsumer<string, string>("messaging", configureConsumerConfig: SetConnectionString); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + builder.AddKafkaConsumer<string, string>("messaging", configureConsumerConfig: SetConnectionString); |
| 60 | + } |
| 61 | + |
| 62 | + var host = builder.Build(); |
| 63 | + var config = useKeyed ? |
| 64 | + host.Services.GetRequiredKeyedService<ConsumerConfig>("messaging") : |
| 65 | + host.Services.GetRequiredService<ConsumerConfig>(); |
| 66 | + |
| 67 | + Assert.Equal(AspireKafkaConsumerHelpers.TestingEndpoint, config.BootstrapServers); |
| 68 | + } |
| 69 | + |
| 70 | + [ConditionalTheory] |
| 71 | + [InlineData(true)] |
| 72 | + [InlineData(false)] |
| 73 | + public void ConnectionNameWinsOverConfigSection(bool useKeyed) |
| 74 | + { |
| 75 | + var builder = Host.CreateEmptyApplicationBuilder(null); |
| 76 | + |
| 77 | + var key = useKeyed ? "redis" : null; |
| 78 | + builder.Configuration.AddInMemoryCollection([ |
| 79 | + new KeyValuePair<string, string?>(ConformanceTests.CreateConfigKey("Aspire:Kafka:Consumer", key, "ConnectionString"), "unused"), |
| 80 | + new KeyValuePair<string, string?>("ConnectionStrings:messaging", AspireKafkaConsumerHelpers.TestingEndpoint) |
| 81 | + ]); |
| 82 | + |
| 83 | + if (useKeyed) |
| 84 | + { |
| 85 | + builder.AddKeyedKafkaConsumer<string, string>("messaging"); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + builder.AddKafkaConsumer<string, string>("messaging"); |
| 90 | + } |
| 91 | + |
| 92 | + var host = builder.Build(); |
| 93 | + var config = useKeyed ? |
| 94 | + host.Services.GetRequiredKeyedService<ConsumerConfig>("messaging") : |
| 95 | + host.Services.GetRequiredService<ConsumerConfig>(); |
| 96 | + |
| 97 | + Assert.Equal(AspireKafkaConsumerHelpers.TestingEndpoint, config.BootstrapServers); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void ConsumerConfigOptionsFromConfig() |
| 102 | + { |
| 103 | + static Stream CreateStreamFromString(string data) => new MemoryStream(Encoding.UTF8.GetBytes(data)); |
| 104 | + |
| 105 | + using var jsonStream = CreateStreamFromString(""" |
| 106 | + { |
| 107 | + "Aspire": { |
| 108 | + "Kafka": { |
| 109 | + "Consumer": { |
| 110 | + "AutoOffsetReset": "Earliest", |
| 111 | + "SaslUsername": "user", |
| 112 | + "SaslPassword": "password", |
| 113 | + "SaslMechanism": "Plain", |
| 114 | + "SecurityProtocol": "Plaintext" |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + """); |
| 120 | + |
| 121 | + var builder = Host.CreateEmptyApplicationBuilder(null); |
| 122 | + |
| 123 | + builder.Configuration.AddJsonStream(jsonStream); |
| 124 | + |
| 125 | + builder.AddKafkaConsumer<string, string>("messaging"); |
| 126 | + |
| 127 | + var host = builder.Build(); |
| 128 | + var config = (ConsumerConfig)host.Services.GetRequiredService<ConsumerConfig>(); |
| 129 | + |
| 130 | + Assert.Equal(AutoOffsetReset.Earliest, config.AutoOffsetReset); |
| 131 | + Assert.Equal("user", config.SaslUsername); |
| 132 | + Assert.Equal("password", config.SaslPassword); |
| 133 | + Assert.Equal(SaslMechanism.Plain, config.SaslMechanism); |
| 134 | + Assert.Equal(SecurityProtocol.Plaintext, config.SecurityProtocol); |
| 135 | + } |
| 136 | +} |
0 commit comments