|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using Microsoft.ApplicationInsights.Channel; |
| 7 | +using Microsoft.ApplicationInsights.DataContracts; |
| 8 | +using Moq; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace NuGet.Services.Logging.Tests |
| 12 | +{ |
| 13 | + public class DeploymentLabelEnricherTests |
| 14 | + { |
| 15 | + const string DeploymentLabel = "TestDeploymentLabel"; |
| 16 | + const string DeploymentLabelPropertyName = "DeploymentLabel"; |
| 17 | + |
| 18 | + private DeploymentLabelEnricher Target { get; } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void ConstructorThrowsWhenDeploymentLabelNull() |
| 22 | + { |
| 23 | + var ex = Assert.Throws<ArgumentNullException>(() => new DeploymentLabelEnricher(deploymentLabel: null)); |
| 24 | + Assert.Equal("deploymentLabel", ex.ParamName); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void WorksWithAnyITelemetry() |
| 29 | + { |
| 30 | + var telemetryMock = new Mock<ITelemetry>(); |
| 31 | + var ex = Record.Exception(() => Target.Initialize(telemetryMock.Object)); |
| 32 | + Assert.Null(ex); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void DoesNotOverwriteExistingLabel() |
| 37 | + { |
| 38 | + var telemetryMock = new Mock<ITelemetry>(); |
| 39 | + var propertiesMock = new Mock<IDictionary<string, string>>(); |
| 40 | + telemetryMock |
| 41 | + .As<ISupportProperties>() |
| 42 | + .SetupGet(sp => sp.Properties) |
| 43 | + .Returns(propertiesMock.Object); |
| 44 | + propertiesMock |
| 45 | + .Setup(p => p.ContainsKey(DeploymentLabelPropertyName)) |
| 46 | + .Returns(true); |
| 47 | + Target.Initialize(telemetryMock.Object); |
| 48 | + |
| 49 | + propertiesMock |
| 50 | + .Verify(p => p.ContainsKey(DeploymentLabelPropertyName), Times.Once); |
| 51 | + propertiesMock |
| 52 | + .VerifyNoOtherCalls(); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void StoresDeploymentLabel() |
| 57 | + { |
| 58 | + var telemetryMock = new Mock<ITelemetry>(); |
| 59 | + var propertiesMock = new Mock<IDictionary<string, string>>(); |
| 60 | + telemetryMock |
| 61 | + .As<ISupportProperties>() |
| 62 | + .SetupGet(sp => sp.Properties) |
| 63 | + .Returns(propertiesMock.Object); |
| 64 | + propertiesMock |
| 65 | + .Setup(p => p.ContainsKey(DeploymentLabelPropertyName)) |
| 66 | + .Returns(false); |
| 67 | + Target.Initialize(telemetryMock.Object); |
| 68 | + |
| 69 | + propertiesMock |
| 70 | + .Verify(p => p.ContainsKey(DeploymentLabelPropertyName), Times.Once); |
| 71 | + propertiesMock |
| 72 | + .Verify(p => p.Add(DeploymentLabelPropertyName, DeploymentLabel), Times.Once); |
| 73 | + } |
| 74 | + |
| 75 | + public DeploymentLabelEnricherTests() |
| 76 | + { |
| 77 | + Target = new DeploymentLabelEnricher(DeploymentLabel); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments