|
| 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 Microsoft.ApplicationInsights.Channel; |
| 5 | +using Microsoft.ApplicationInsights.DataContracts; |
| 6 | +using Moq; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace NuGetGallery |
| 10 | +{ |
| 11 | + public class DeploymentIdTelemetryEnricherFacts |
| 12 | + { |
| 13 | + private const string PropertyKey = "CloudDeploymentId"; |
| 14 | + |
| 15 | + public DeploymentIdTelemetryEnricherFacts() |
| 16 | + { |
| 17 | + Telemetry = new Mock<ITelemetry>(); |
| 18 | + Telemetry.Setup(x => x.Context).Returns(new TelemetryContext()); |
| 19 | + Target = new Mock<TestableDeploymentIdTelemetryEnricher>(); |
| 20 | + Target.Object.SetDeploymentId("TheDeploymentId"); |
| 21 | + } |
| 22 | + |
| 23 | + public Mock<ITelemetry> Telemetry { get; } |
| 24 | + public Mock<TestableDeploymentIdTelemetryEnricher> Target { get; } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void DoesNothingWithNullTelemetry() |
| 28 | + { |
| 29 | + Target.Object.Initialize(telemetry: null); |
| 30 | + |
| 31 | + Target.Verify(x => x.DeploymentId, Times.Never); |
| 32 | + Assert.DoesNotContain(PropertyKey, Telemetry.Object.Context.Properties.Keys); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void DoesNothingWhenDeploymentIdIsNull() |
| 37 | + { |
| 38 | + Target.Object.SetDeploymentId(null); |
| 39 | + |
| 40 | + Target.Object.Initialize(Telemetry.Object); |
| 41 | + |
| 42 | + Target.Verify(x => x.DeploymentId, Times.Never); |
| 43 | + Assert.DoesNotContain(PropertyKey, Telemetry.Object.Context.Properties.Keys); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void DoesNothingWhenDeploymentIdIsAlreadySet() |
| 48 | + { |
| 49 | + Telemetry.Object.Context.Properties[PropertyKey] = "something else"; |
| 50 | + |
| 51 | + Target.Object.Initialize(Telemetry.Object); |
| 52 | + |
| 53 | + Target.Verify(x => x.DeploymentId, Times.Never); |
| 54 | + Assert.Equal(Telemetry.Object.Context.Properties[PropertyKey], "something else"); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void SetsDeploymentId() |
| 59 | + { |
| 60 | + Target.Object.Initialize(Telemetry.Object); |
| 61 | + |
| 62 | + Assert.Equal(Telemetry.Object.Context.Properties[PropertyKey], "TheDeploymentId"); |
| 63 | + } |
| 64 | + |
| 65 | + public class TestableDeploymentIdTelemetryEnricher : DeploymentIdTelemetryEnricher |
| 66 | + { |
| 67 | + private string _deploymentId; |
| 68 | + |
| 69 | + public void SetDeploymentId(string deploymentId) |
| 70 | + { |
| 71 | + _deploymentId = deploymentId; |
| 72 | + } |
| 73 | + |
| 74 | + internal override string DeploymentId => _deploymentId; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments