Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hejniak Michał committed Jan 20, 2023
1 parent b7c7e19 commit 31feeb8
Showing 1 changed file with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,40 @@ public void LogHttpDependencyWithDurationMeasurementWithDependencyId_ValidArgume
Assert.Contains(dependencyId, logMessage);
}

[Fact]
public void LogHttpDependencyWithDurationMeasurementWithDependencyIdWithIntStatusCode_ValidArguments_Succeeds()
{
// Arrange
var logger = new TestLogger();
var request = new HttpRequestMessage(HttpMethod.Get, BogusGenerator.Internet.UrlWithPath());
var statusCode = BogusGenerator.Random.Int(100, 599);
var measurement = DurationMeasurement.Start();
DateTimeOffset startTime = measurement.StartTime;
measurement.Dispose();
TimeSpan duration = measurement.Elapsed;
string dependencyId = BogusGenerator.Lorem.Word();

// Act
logger.LogHttpDependency(request, statusCode, measurement, dependencyId);

// Assert
string logMessage = logger.WrittenMessage;
Assert.Contains(TelemetryType.Dependency.ToString(), logMessage);
Assert.Contains(request.RequestUri?.Host, logMessage);
Assert.Contains(request.RequestUri?.PathAndQuery, logMessage);
Assert.Contains(request.Method.ToString(), logMessage);
Assert.Contains(statusCode.ToString(), logMessage);
Assert.Contains(startTime.ToString(FormatSpecifiers.InvariantTimestampFormat), logMessage);
Assert.Contains(duration.ToString(), logMessage);
bool isSuccessful = statusCode >= 200 && statusCode < 300;
Assert.Contains($"Successful: {isSuccessful}", logMessage);
Uri requestUri = request.RequestUri;
HttpMethod requestMethod = request.Method;
string dependencyName = $"{requestMethod} {requestUri?.AbsolutePath}";
Assert.Contains("Http " + dependencyName, logMessage);
Assert.Contains(dependencyId, logMessage);
}

[Fact]
public void LogHttpDependencyWithRequestWithDurationMeasurement_ValidArguments_Succeeds()
{
Expand Down Expand Up @@ -314,6 +348,38 @@ public void LogHttpDependencyWithRequestWithDurationMeasurementWithDependencyId_
Assert.False(dependency.IsSuccessful);
}

[Fact]
public void LogHttpDependencyWithRequestWithDurationMeasurementWithDependencyIdWithIntStatusCode_ValidArguments_Succeeds()
{
// Arrange
var logger = new TestLogger();
var requestUri = new Uri("https://arcus.test/unit");
HttpRequest request = CreateStubRequest(HttpMethod.Get, requestUri.Host, requestUri.AbsolutePath, requestUri.Scheme);

var statusCode = BogusGenerator.Random.Int(100, 599);
var measurement = DurationMeasurement.Start();
DateTimeOffset startTime = measurement.StartTime;
measurement.Dispose();
TimeSpan duration = measurement.Elapsed;
string dependencyId = BogusGenerator.Lorem.Word();

// Act
logger.LogHttpDependency(request, statusCode, measurement, dependencyId);

// Assert
DependencyLogEntry dependency = logger.GetMessageAsDependency();
Assert.Contains("Http", dependency.DependencyType);
Assert.Equal(dependencyId, dependency.DependencyId);
Assert.Contains(request.Method, logger.WrittenMessage);
Assert.Contains(request.Path, dependency.DependencyName);
Assert.Equal((int?)statusCode, dependency.ResultCode);
Assert.Equal(request.Host.Host, dependency.TargetName);
Assert.Equal(duration, dependency.Duration);
Assert.Equal(startTime.ToString(FormatSpecifiers.InvariantTimestampFormat), dependency.StartTime);
bool isSuccessful = statusCode >= 200 && statusCode < 300;
Assert.Equal(isSuccessful, dependency.IsSuccessful);
}

[Fact]
public void LogHttpDependencyWithRequestMessageWithDurationMeasurement_WithoutRequest_Fails()
{
Expand Down Expand Up @@ -341,6 +407,20 @@ public void LogHttpDependencyWithRequestMessageWithDurationMeasurementWithDepend
Assert.ThrowsAny<ArgumentException>(() => logger.LogHttpDependency(request: (HttpRequestMessage)null, statusCode, measurement, dependencyId));
}

[Fact]
public void LogHttpDependencyWithRequestMessageWithDurationMeasurementWithDependencyIdWithIntStatusCode_WithoutRequest_Fails()
{
// Arrange
var logger = new TestLogger();
var statusCode = BogusGenerator.Random.Int(100, 599);
var measurement = DurationMeasurement.Start();
measurement.Dispose();
string dependencyId = BogusGenerator.Lorem.Word();

// Act / Assert
Assert.ThrowsAny<ArgumentException>(() => logger.LogHttpDependency(request: (HttpRequestMessage)null, statusCode, measurement, dependencyId));
}

[Fact]
public void LogHttpDependencyWithRequestWithDurationMeasurement_WithoutRequest_Fails()
{
Expand Down Expand Up @@ -368,6 +448,20 @@ public void LogHttpDependencyWithRequestWithDurationMeasurementWithDependencyId_
Assert.ThrowsAny<ArgumentException>(() => logger.LogHttpDependency(request: (HttpRequest)null, statusCode, measurement, dependencyId));
}

[Fact]
public void LogHttpDependencyWithRequestWithDurationMeasurementWithDependencyIdWitIntStatusCode_WithoutRequest_Fails()
{
// Arrange
var logger = new TestLogger();
var statusCode = BogusGenerator.Random.Int(100, 599);
var measurement = DurationMeasurement.Start();
measurement.Dispose();
string dependencyId = BogusGenerator.Lorem.Word();

// Act / Assert
Assert.ThrowsAny<ArgumentException>(() => logger.LogHttpDependency(request: (HttpRequest)null, statusCode, measurement, dependencyId));
}

[Fact]
public void LogHttpDependencyWithRequestMessageWithDurationMeasurement_WithoutMeasurement_Fails()
{
Expand Down Expand Up @@ -397,6 +491,21 @@ public void LogHttpDependencyWithRequestMessageWithDurationMeasurementWithDepend
Assert.ThrowsAny<ArgumentException>(() => logger.LogHttpDependency(request, statusCode, measurement: null, dependencyId));
}

[Fact]
public void LogHttpDependencyWithRequestMessageWithDurationMeasurementWithDependencyIdWithIntStatusCode_WithoutMeasurement_Fails()
{
// Arrange
var logger = new TestLogger();
var request = new HttpRequestMessage(HttpMethod.Get, BogusGenerator.Internet.UrlWithPath());
var statusCode = BogusGenerator.Random.Int(100, 599);
var measurement = DurationMeasurement.Start();
measurement.Dispose();
string dependencyId = BogusGenerator.Lorem.Word();

// Act / Assert
Assert.ThrowsAny<ArgumentException>(() => logger.LogHttpDependency(request, statusCode, measurement: null, dependencyId));
}

[Fact]
public void LogHttpDependencyWithRequestWithDurationMeasurement_WithoutMeasurement_Fails()
{
Expand Down Expand Up @@ -426,6 +535,21 @@ public void LogHttpDependencyWithRequestWithDurationMeasurementWithDependencyId_
Assert.ThrowsAny<ArgumentException>(() => logger.LogHttpDependency(request, statusCode, measurement: null, dependencyId));
}

[Fact]
public void LogHttpDependencyWithRequestWithDurationMeasurementWithDependencyIdWithIntStatusCode_WithoutMeasurement_Fails()
{
// Arrange
var logger = new TestLogger();
HttpRequest request = CreateStubRequest(HttpMethod.Get, "host", "/path", "http");
var statusCode = BogusGenerator.Random.Int(100, 599);
var measurement = DurationMeasurement.Start();
measurement.Dispose();
string dependencyId = BogusGenerator.Lorem.Word();

// Act / Assert
Assert.ThrowsAny<ArgumentException>(() => logger.LogHttpDependency(request, statusCode, measurement: null, dependencyId));
}

[Fact]
public void LogHttpDependencyWithRequest_WithHttpStatusCodeOutsideAllowedRange_Fails()
{
Expand Down

0 comments on commit 31feeb8

Please sign in to comment.