Skip to content

Commit 518b874

Browse files
committed
feat(metrics): add unit tests for Metrics constructor and validation methods
1 parent b9ba2ce commit 518b874

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/MetricsTests.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Collections.Generic;
13
using AWS.Lambda.Powertools.Common;
24
using NSubstitute;
35
using Xunit;
@@ -30,4 +32,122 @@ public void Metrics_Set_Execution_Environment_Context()
3032

3133
env.Received(1).GetEnvironmentVariable("AWS_EXECUTION_ENV");
3234
}
35+
36+
[Fact]
37+
public void When_Constructor_With_Namespace_And_Service_Should_Set_Both()
38+
{
39+
// Arrange
40+
var metricsMock = Substitute.For<IMetrics>();
41+
var powertoolsConfigMock = Substitute.For<IPowertoolsConfigurations>();
42+
43+
// Act
44+
var metrics = new Metrics(powertoolsConfigMock, "TestNamespace", "TestService");
45+
46+
// Assert
47+
Assert.Equal("TestNamespace", metrics.GetNamespace());
48+
Assert.Equal("TestService", metrics.Options.Service);
49+
}
50+
51+
[Fact]
52+
public void When_Constructor_With_Null_Namespace_And_Service_Should_Not_Set()
53+
{
54+
// Arrange
55+
var metricsMock = Substitute.For<IMetrics>();
56+
var powertoolsConfigMock = Substitute.For<IPowertoolsConfigurations>();
57+
powertoolsConfigMock.MetricsNamespace.Returns((string)null);
58+
powertoolsConfigMock.Service.Returns("service_undefined");
59+
60+
// Act
61+
var metrics = new Metrics(powertoolsConfigMock, null, null);
62+
63+
// Assert
64+
Assert.Null(metrics.GetNamespace());
65+
Assert.Null(metrics.Options.Service);
66+
}
67+
68+
[Fact]
69+
public void When_AddMetric_With_EmptyKey_Should_ThrowArgumentNullException()
70+
{
71+
// Arrange
72+
var metricsMock = Substitute.For<IMetrics>();
73+
var powertoolsConfigMock = Substitute.For<IPowertoolsConfigurations>();
74+
IMetrics metrics = new Metrics(powertoolsConfigMock);
75+
76+
// Act & Assert
77+
var exception = Assert.Throws<ArgumentNullException>(() => metrics.AddMetric("", 1.0));
78+
Assert.Equal("key", exception.ParamName);
79+
Assert.Contains("'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.", exception.Message);
80+
}
81+
82+
[Theory]
83+
[InlineData(null)]
84+
[InlineData("")]
85+
[InlineData(" ")]
86+
public void When_AddMetric_With_InvalidKey_Should_ThrowArgumentNullException(string key)
87+
{
88+
// Arrange
89+
// var metricsMock = Substitute.For<IMetrics>();
90+
var powertoolsConfigMock = Substitute.For<IPowertoolsConfigurations>();
91+
IMetrics metrics = new Metrics(powertoolsConfigMock);
92+
93+
// Act & Assert
94+
var exception = Assert.Throws<ArgumentNullException>(() => metrics.AddMetric(key, 1.0));
95+
Assert.Equal("key", exception.ParamName);
96+
Assert.Contains("'AddMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.", exception.Message);
97+
}
98+
99+
[Fact]
100+
public void When_SetDefaultDimensions_With_InvalidKeyOrValue_Should_ThrowArgumentNullException()
101+
{
102+
// Arrange
103+
var powertoolsConfigMock = Substitute.For<IPowertoolsConfigurations>();
104+
IMetrics metrics = new Metrics(powertoolsConfigMock);
105+
106+
var invalidDimensions = new Dictionary<string, string>
107+
{
108+
{ "", "value" }, // empty key
109+
{ "key", "" }, // empty value
110+
{ " ", "value" }, // whitespace key
111+
{ "key1", " " }, // whitespace value
112+
{ "key2", null } // null value
113+
};
114+
115+
// Act & Assert
116+
foreach (var dimension in invalidDimensions)
117+
{
118+
var dimensions = new Dictionary<string, string> { { dimension.Key, dimension.Value } };
119+
var exception = Assert.Throws<ArgumentNullException>(() => metrics.SetDefaultDimensions(dimensions));
120+
Assert.Equal("Key", exception.ParamName);
121+
Assert.Contains("'SetDefaultDimensions' method requires a valid key pair. 'Null' or empty values are not allowed.", exception.Message);
122+
}
123+
}
124+
125+
[Fact]
126+
public void When_PushSingleMetric_With_EmptyName_Should_ThrowArgumentNullException()
127+
{
128+
// Arrange
129+
var powertoolsConfigMock = Substitute.For<IPowertoolsConfigurations>();
130+
IMetrics metrics = new Metrics(powertoolsConfigMock);
131+
132+
// Act & Assert
133+
var exception = Assert.Throws<ArgumentNullException>(() => metrics.PushSingleMetric("", 1.0, MetricUnit.Count));
134+
Assert.Equal("name", exception.ParamName);
135+
Assert.Contains("'PushSingleMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.", exception.Message);
136+
}
137+
138+
[Theory]
139+
[InlineData(null)]
140+
[InlineData("")]
141+
[InlineData(" ")]
142+
public void When_PushSingleMetric_With_InvalidName_Should_ThrowArgumentNullException(string name)
143+
{
144+
// Arrange
145+
var powertoolsConfigMock = Substitute.For<IPowertoolsConfigurations>();
146+
IMetrics metrics = new Metrics(powertoolsConfigMock);
147+
148+
// Act & Assert
149+
var exception = Assert.Throws<ArgumentNullException>(() => metrics.PushSingleMetric(name, 1.0, MetricUnit.Count));
150+
Assert.Equal("name", exception.ParamName);
151+
Assert.Contains("'PushSingleMetric' method requires a valid metrics key. 'Null' or empty values are not allowed.", exception.Message);
152+
}
33153
}

0 commit comments

Comments
 (0)