Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 8f01a5e

Browse files
f-alizadaFarhad Alizada
andauthored
Handle duplicate key error in LoggerExtractor (#741)
* Handle duplicate key error in LoggerExtractor Co-authored-by: Farhad Alizada <falizada@microsoft.com>
1 parent 86f0844 commit 8f01a5e

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

src/ArmTemplates/Extractor/EntityExtractors/LoggerExtractor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ async Task LoadAllReferencedLoggers(
148148

149149
if (!diagnosticLoggerBindings.IsNullOrEmpty())
150150
{
151-
this.Cache.ApiDiagnosticLoggerBindings.Add(
152-
NamingHelper.GenerateValidParameterName(curApiName, ParameterPrefix.Api),
153-
diagnosticLoggerBindings);
151+
var diagnosticLoggerKey = NamingHelper.GenerateValidParameterName(curApiName, ParameterPrefix.Api);
152+
if (!this.Cache.ApiDiagnosticLoggerBindings.ContainsKey(diagnosticLoggerKey))
153+
{
154+
this.Cache.ApiDiagnosticLoggerBindings.Add(diagnosticLoggerKey, diagnosticLoggerBindings);
155+
}
154156
}
155157
}
156158
}

tests/ArmTemplates.Tests/Extractor/Scenarios/LoggerExtractorTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
using FluentAssertions;
1111
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Commands.Executors;
1212
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
13+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
1314
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders;
15+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Logger.Cache;
1416
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Policy;
1517
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors;
1618
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
@@ -70,5 +72,64 @@ public async Task GenerateLoggersTemplates_ProperlyLaysTheInformation()
7072
loggerTemplate.TypedResources.Loggers.First().Name.Should().Contain(MockLoggerClient.LoggerName);
7173
loggerTemplate.TypedResources.Loggers.First().Properties.LoggerType.Should().Be(MockDiagnosticClient.DefaultDiagnosticName);
7274
}
75+
76+
[Fact]
77+
public async Task GenerateLoggersTemplates_ShouldPopulateCacheWithoutError_GivenItCalledMultipleTimes()
78+
{
79+
// arrange
80+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(
81+
splitAPIs: "true",
82+
paramApiLoggerId: "true",
83+
apiName: string.Empty);
84+
var extractorParameters = new ExtractorParameters(extractorConfig);
85+
86+
var mockedDiagnosticClient = MockDiagnosticClient.GetMockedApiClientWithDefaultValues();
87+
var mockedLoggerClient = MockLoggerClient.GetMockedClientWithDiagnosticDependentValues();
88+
var loggerExtractor = new LoggerExtractor(
89+
this.GetTestLogger<LoggerExtractor>(),
90+
new TemplateBuilder(),
91+
mockedLoggerClient,
92+
mockedDiagnosticClient);
93+
94+
var apisToTest = new List<string>() {
95+
"echo-api",
96+
"echo-api-2"
97+
};
98+
99+
var diagnosticLoggerBinding = new DiagnosticLoggerBinding
100+
{
101+
DiagnosticName = NamingHelper.GenerateValidParameterName(MockDiagnosticClient.DefaultDiagnosticName, ParameterPrefix.Diagnostic),
102+
LoggerId = MockDiagnosticClient.LoggerIdValue
103+
};
104+
105+
var expectedApiDiagnosticLoggerBingingsValues = new Dictionary<string, ISet<DiagnosticLoggerBinding>>()
106+
{
107+
{
108+
NamingHelper.GenerateValidParameterName("echo-api", ParameterPrefix.Api),
109+
new HashSet<DiagnosticLoggerBinding>() { diagnosticLoggerBinding }
110+
},
111+
{
112+
NamingHelper.GenerateValidParameterName("echo-api-2", ParameterPrefix.Api),
113+
new HashSet<DiagnosticLoggerBinding>() { diagnosticLoggerBinding }
114+
}
115+
};
116+
117+
// act
118+
await loggerExtractor.GenerateLoggerTemplateAsync(apisToTest, null, extractorParameters);
119+
foreach(var api in apisToTest)
120+
{
121+
await loggerExtractor.GenerateLoggerTemplateAsync(new List<string>() { api }, null, extractorParameters);
122+
}
123+
124+
// assert
125+
loggerExtractor.Cache.ServiceLevelDiagnosticLoggerBindings.Count.Should().Be(1);
126+
loggerExtractor.Cache.ApiDiagnosticLoggerBindings.Count.Should().Be(2);
127+
128+
foreach (var (apiName, diagnosticLoggerBindings) in expectedApiDiagnosticLoggerBingingsValues)
129+
{
130+
loggerExtractor.Cache.ApiDiagnosticLoggerBindings[apiName].Count.Should().Be(1);
131+
loggerExtractor.Cache.ApiDiagnosticLoggerBindings[apiName].First().Equals(diagnosticLoggerBindings.First()).Should().BeTrue();
132+
}
133+
}
73134
}
74135
}

tests/ArmTemplates.Tests/Moqs/ApiClients/MockDiagnosticClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class MockDiagnosticClient
1515
{
1616
public const string DiagnosticName = "diagnostic";
1717
public const string DefaultDiagnosticName = "default-diagnostic";
18+
public const string LoggerIdValue = "logger-id";
1819

1920
public static IDiagnosticClient GetMockedClientWithApiDependentValues()
2021
{
@@ -29,7 +30,7 @@ public static IDiagnosticClient GetMockedClientWithApiDependentValues()
2930
Name = DiagnosticName,
3031
Properties = new DiagnosticTemplateProperties()
3132
{
32-
LoggerId = "logger-id"
33+
LoggerId = LoggerIdValue
3334
}
3435
}
3536
});
@@ -43,7 +44,7 @@ public static IDiagnosticClient GetMockedClientWithApiDependentValues()
4344
Name = $"{apiName}-{DiagnosticName}",
4445
Properties = new DiagnosticTemplateProperties()
4546
{
46-
LoggerId = "logger-id"
47+
LoggerId = LoggerIdValue
4748
}
4849
}
4950
});
@@ -64,7 +65,7 @@ public static IDiagnosticClient GetMockedApiClientWithDefaultValues()
6465
Name = DefaultDiagnosticName,
6566
Properties = new DiagnosticTemplateProperties()
6667
{
67-
LoggerId = "logger-id"
68+
LoggerId = LoggerIdValue
6869
}
6970
}
7071
});
@@ -78,7 +79,7 @@ public static IDiagnosticClient GetMockedApiClientWithDefaultValues()
7879
Name = DefaultDiagnosticName,
7980
Properties = new DiagnosticTemplateProperties()
8081
{
81-
LoggerId = "logger-id"
82+
LoggerId = LoggerIdValue
8283
}
8384
}
8485
});

0 commit comments

Comments
 (0)