Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
simpat-adam committed Jan 8, 2025
1 parent e9f699d commit 4c1e097
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System.Diagnostics;
using System.Globalization;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using EdFi.DataManagementService.Core.Middleware;
Expand All @@ -29,7 +31,7 @@ public class Given_Valid_Request_Body : InjectVersionMetadataToEdFiDocumentMiddl
{
private readonly PipelineContext _context = No.PipelineContext();
private readonly string _pattern = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$";
private readonly string _propertyName = "_lastModifiedDate";
private readonly string _lastModifiedDatePropertyName = "_lastModifiedDate";

[SetUp]
public async Task Setup()
Expand Down Expand Up @@ -70,10 +72,32 @@ public void It_should_not_have_response()
[Test]
public void It_should_have_parsed_body_with_formatted_lastmodifieddate()
{
var lastModifiedDate = _context?.ParsedBody[_propertyName]?.AsValue();
var lastModifiedDate = _context?.ParsedBody[_lastModifiedDatePropertyName]?.AsValue();
lastModifiedDate.Should().NotBeNull();
var IsValid = Regex.IsMatch(lastModifiedDate!.ToString(), _pattern);
IsValid.Should().BeTrue();
}

[Test]
public void It_should_have_parsed_body_with_etag()
{
var lastModifiedDate = _context?.ParsedBody[_lastModifiedDatePropertyName]?.AsValue();
lastModifiedDate.Should().NotBeNull();

var eTag = _context?.ParsedBody["_etag"]?.AsValue();
eTag.Should().NotBeNull();

Trace.Assert(lastModifiedDate != null);
Trace.Assert(eTag != null);

var datetime = DateTime.ParseExact(
lastModifiedDate.GetValue<string>(),
"yyyy-MM-ddTHH:mm:ssZ",
DateTimeFormatInfo.InvariantInfo
);
var reverseEtag = datetime.ToBinary().ToString(CultureInfo.InvariantCulture);

reverseEtag.Should().BeEquivalentTo(eTag.GetValue<string>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public async Task Execute(PipelineContext context, Func<Task> next)
DateTimeOffset utcNow = DateTimeOffset.UtcNow;
string formattedUtcDateTime = utcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
context.ParsedBody["_lastModifiedDate"] = formattedUtcDateTime;
context.ParsedBody["_etag"] = utcNow.DateTime.ToBinary().ToString(CultureInfo.InvariantCulture);
context.ParsedBody["_etag"] = DateTime
.ParseExact(formattedUtcDateTime, "yyyy-MM-ddTHH:mm:ssZ", DateTimeFormatInfo.InvariantInfo)
.ToBinary()
.ToString(CultureInfo.InvariantCulture);
await next();
}
}
Expand Down

0 comments on commit 4c1e097

Please sign in to comment.