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

Commit

Permalink
Handle duplicate key error in LoggerExtractor (#741)
Browse files Browse the repository at this point in the history
* Handle duplicate key error in LoggerExtractor

Co-authored-by: Farhad Alizada <[email protected]>
  • Loading branch information
f-alizada and Farhad Alizada authored Jun 8, 2022
1 parent 86f0844 commit 8f01a5e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ async Task LoadAllReferencedLoggers(

if (!diagnosticLoggerBindings.IsNullOrEmpty())
{
this.Cache.ApiDiagnosticLoggerBindings.Add(
NamingHelper.GenerateValidParameterName(curApiName, ParameterPrefix.Api),
diagnosticLoggerBindings);
var diagnosticLoggerKey = NamingHelper.GenerateValidParameterName(curApiName, ParameterPrefix.Api);
if (!this.Cache.ApiDiagnosticLoggerBindings.ContainsKey(diagnosticLoggerKey))
{
this.Cache.ApiDiagnosticLoggerBindings.Add(diagnosticLoggerKey, diagnosticLoggerBindings);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using FluentAssertions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Commands.Executors;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Logger.Cache;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Policy;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
Expand Down Expand Up @@ -70,5 +72,64 @@ public async Task GenerateLoggersTemplates_ProperlyLaysTheInformation()
loggerTemplate.TypedResources.Loggers.First().Name.Should().Contain(MockLoggerClient.LoggerName);
loggerTemplate.TypedResources.Loggers.First().Properties.LoggerType.Should().Be(MockDiagnosticClient.DefaultDiagnosticName);
}

[Fact]
public async Task GenerateLoggersTemplates_ShouldPopulateCacheWithoutError_GivenItCalledMultipleTimes()
{
// arrange
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(
splitAPIs: "true",
paramApiLoggerId: "true",
apiName: string.Empty);
var extractorParameters = new ExtractorParameters(extractorConfig);

var mockedDiagnosticClient = MockDiagnosticClient.GetMockedApiClientWithDefaultValues();
var mockedLoggerClient = MockLoggerClient.GetMockedClientWithDiagnosticDependentValues();
var loggerExtractor = new LoggerExtractor(
this.GetTestLogger<LoggerExtractor>(),
new TemplateBuilder(),
mockedLoggerClient,
mockedDiagnosticClient);

var apisToTest = new List<string>() {
"echo-api",
"echo-api-2"
};

var diagnosticLoggerBinding = new DiagnosticLoggerBinding
{
DiagnosticName = NamingHelper.GenerateValidParameterName(MockDiagnosticClient.DefaultDiagnosticName, ParameterPrefix.Diagnostic),
LoggerId = MockDiagnosticClient.LoggerIdValue
};

var expectedApiDiagnosticLoggerBingingsValues = new Dictionary<string, ISet<DiagnosticLoggerBinding>>()
{
{
NamingHelper.GenerateValidParameterName("echo-api", ParameterPrefix.Api),
new HashSet<DiagnosticLoggerBinding>() { diagnosticLoggerBinding }
},
{
NamingHelper.GenerateValidParameterName("echo-api-2", ParameterPrefix.Api),
new HashSet<DiagnosticLoggerBinding>() { diagnosticLoggerBinding }
}
};

// act
await loggerExtractor.GenerateLoggerTemplateAsync(apisToTest, null, extractorParameters);
foreach(var api in apisToTest)
{
await loggerExtractor.GenerateLoggerTemplateAsync(new List<string>() { api }, null, extractorParameters);
}

// assert
loggerExtractor.Cache.ServiceLevelDiagnosticLoggerBindings.Count.Should().Be(1);
loggerExtractor.Cache.ApiDiagnosticLoggerBindings.Count.Should().Be(2);

foreach (var (apiName, diagnosticLoggerBindings) in expectedApiDiagnosticLoggerBingingsValues)
{
loggerExtractor.Cache.ApiDiagnosticLoggerBindings[apiName].Count.Should().Be(1);
loggerExtractor.Cache.ApiDiagnosticLoggerBindings[apiName].First().Equals(diagnosticLoggerBindings.First()).Should().BeTrue();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class MockDiagnosticClient
{
public const string DiagnosticName = "diagnostic";
public const string DefaultDiagnosticName = "default-diagnostic";
public const string LoggerIdValue = "logger-id";

public static IDiagnosticClient GetMockedClientWithApiDependentValues()
{
Expand All @@ -29,7 +30,7 @@ public static IDiagnosticClient GetMockedClientWithApiDependentValues()
Name = DiagnosticName,
Properties = new DiagnosticTemplateProperties()
{
LoggerId = "logger-id"
LoggerId = LoggerIdValue
}
}
});
Expand All @@ -43,7 +44,7 @@ public static IDiagnosticClient GetMockedClientWithApiDependentValues()
Name = $"{apiName}-{DiagnosticName}",
Properties = new DiagnosticTemplateProperties()
{
LoggerId = "logger-id"
LoggerId = LoggerIdValue
}
}
});
Expand All @@ -64,7 +65,7 @@ public static IDiagnosticClient GetMockedApiClientWithDefaultValues()
Name = DefaultDiagnosticName,
Properties = new DiagnosticTemplateProperties()
{
LoggerId = "logger-id"
LoggerId = LoggerIdValue
}
}
});
Expand All @@ -78,7 +79,7 @@ public static IDiagnosticClient GetMockedApiClientWithDefaultValues()
Name = DefaultDiagnosticName,
Properties = new DiagnosticTemplateProperties()
{
LoggerId = "logger-id"
LoggerId = LoggerIdValue
}
}
});
Expand Down

0 comments on commit 8f01a5e

Please sign in to comment.