Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/DeploymentGates.UnitTests/DeploymentGates.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 29 additions & 3 deletions src/DeploymentGates.UnitTests/TimeBasedArgsUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public void TimeBasedArgs_IsInWindow()
string json = @"
{
'startTimeSpan': '12:30',
'endTimeSpan': '17:00',
'timeZoneId': 'Eastern Standard Time'
'endTimeSpan': '19:00',
'timeZoneId': 'UTC'
}";
TimeBasedArgs args = TimeBasedArgs.FromJson(json);
Assert.True(args.IsInsideWindow(DateTime.Parse("7/22/2019 18:00:00Z")));
Assert.True(args.IsInsideWindow(DateTime.Parse("7/22/2019 17:59:00Z")));
Assert.False(args.IsInsideWindow(DateTime.Parse("7/22/2019 1:00:00Z")));
}

Expand Down Expand Up @@ -114,5 +114,31 @@ public void IsValidDate_0InvalidDates()
TimeBasedArgs args = TimeBasedArgs.FromJson(json);
Assert.True(args.IsValidDate(DateTime.Parse("7/22/2019 18:00:00Z")));
}

[Fact]
public void IsValidDate_ParcialInvalidDates()
{
string json = @"
{
'timeZoneId': 'Eastern Standard Time',
'validDaysOfWeek': [ 'Monday' ],
'invalidDates': [
'7/22/9999', '7/23/9999'
]
}";
TimeBasedArgs args = TimeBasedArgs.FromJson(json);


int currentYear = DateTime.UtcNow.Year;
int previousYear = DateTime.UtcNow.Year - 1;

Assert.True(args.IsValidDate(DateTime.Parse($"7/24/{previousYear} 18:00:00Z")));
Assert.True(args.IsValidDate(DateTime.Parse($"7/24/{currentYear} 18:00:00Z")));

Assert.True(args.IsValidDate(DateTime.Parse($"7/22/{previousYear} 18:00:00Z")));
Assert.False(args.IsValidDate(DateTime.Parse($"7/22/{currentYear} 18:00:00Z")));
Assert.True(args.IsValidDate(DateTime.Parse($"7/23/{previousYear} 18:00:00Z")));
Assert.False(args.IsValidDate(DateTime.Parse($"7/23/{currentYear} 18:00:00Z")));
}
}
}
3 changes: 1 addition & 2 deletions src/DeploymentGates/DateTimeGates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using DeploymentGates.Models;
using System.Linq;

Expand Down Expand Up @@ -58,7 +57,7 @@ public static async Task<IActionResult> Run(

// Determine if the datetime is valid.
bool isInsideTimeWindow = args.IsInsideWindow(localTime);
bool isValidDayOfWeek = (args.ValidDaysOfWeek.Count() == 0 || args.IsValidDayOfWeek(localTime));
bool isValidDayOfWeek = !args.ValidDaysOfWeek.Any() || args.IsValidDayOfWeek(localTime);
bool isValidDate = args.IsValidDate(localTime);

/// Log data
Expand Down
4 changes: 2 additions & 2 deletions src/DeploymentGates/DeploymentGates.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.8" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
28 changes: 22 additions & 6 deletions src/DeploymentGates/Models/TimeBasedArgs.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection.Metadata.Ecma335;

namespace DeploymentGates.Models
{
Expand All @@ -26,31 +26,47 @@ public TimeZoneInfo GetTimeZoneInfo()
public static TimeBasedArgs FromJson(string json)
{
// Parse JSON to object
return JsonConvert.DeserializeObject<TimeBasedArgs>(json);
return ResolveInvalidDates(JsonConvert.DeserializeObject<TimeBasedArgs>(json));
}

private static TimeBasedArgs ResolveInvalidDates(TimeBasedArgs args)
{
var newInvalidDates = new List<DateTime>();
if (args.InvalidDates == null || !args.InvalidDates.Any()) return args;

foreach (var invalidDate in args.InvalidDates)
{
newInvalidDates.Add(invalidDate.Year.Equals(9999)
? new DateTime(DateTime.UtcNow.Year, invalidDate.Month, invalidDate.Day)
: invalidDate);
}

args.InvalidDates = newInvalidDates;
return args;
}

public bool IsInsideWindow(DateTime dateTime)
{
if (this.StartTimeSpan == default(TimeSpan) && this.EndTimeSpan == default(TimeSpan))
if (StartTimeSpan == default && EndTimeSpan == default)
{
return true;
}

TimeSpan time = dateTime.TimeOfDay;
return ((time > this.StartTimeSpan) && (time < this.EndTimeSpan));
return time > StartTimeSpan && time < EndTimeSpan;
}

public bool IsValidDayOfWeek(DateTime dateTime)
{
return this.ValidDaysOfWeek.Contains(dateTime.DayOfWeek);
return ValidDaysOfWeek.Contains(dateTime.DayOfWeek);
}

/// <summary>
/// Ensure the date is not in the InvalidDates list.
/// </summary>
public bool IsValidDate(DateTime dateTime)
{
return !this.InvalidDates.Contains(dateTime.Date);
return !InvalidDates.Contains(dateTime.Date);
}

public override string ToString()
Expand Down