Skip to content

Commit

Permalink
fix(checkFeatureExists): when adding a new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhck committed Jul 11, 2020
1 parent 1d6dc14 commit 26f0a21
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 37 deletions.
14 changes: 13 additions & 1 deletion Feature.Manager.Api/FeatureRuns/FeatureRunService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Feature.Manager.Api.FeatureRuns.Exceptions;
using Feature.Manager.Api.FeatureRuns.ViewModels;
using Feature.Manager.Api.Features;
using Feature.Manager.Api.Features.Exceptions;
using Feature.Manager.Api.Features.ViewModels;

Expand All @@ -20,10 +21,12 @@ public interface IFeatureRunService
public class FeatureRunService : IFeatureRunService
{
private readonly IFeatureRunRepository _featureRunRepository;
private readonly IFeatureRepository _featureRepository;

public FeatureRunService(IFeatureRunRepository featureRunRepository)
public FeatureRunService(IFeatureRunRepository featureRunRepository, IFeatureRepository featureRepository)
{
_featureRunRepository = featureRunRepository;
_featureRepository = featureRepository;
}

public async Task<FeatureRun> CreateFeatureRun(CreateFeatureRunRequest request)
Expand All @@ -36,12 +39,21 @@ public async Task<FeatureRun> CreateFeatureRun(CreateFeatureRunRequest request)
throw new FeatureAlreadyRunningException();
}

if (await _featureRepository.FindByFeatId(request.FeatId) == null)
{
throw new FeatureNotFoundException();
}

return await _featureRunRepository.CreateFeatureRun(request);
}
catch (FeatureAlreadyRunningException)
{
throw;
}
catch (FeatureNotFoundException)
{
throw;
}
catch (Exception e)
{
throw new UnknownDbException(e.Message);
Expand Down
7 changes: 7 additions & 0 deletions Feature.Manager.Api/FeatureRuns/FeatureRunsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ public async Task<IActionResult> AddFeatureRun(CreateFeatureRunRequest request)
Title = "Feature is already running"
});
}
catch (FeatureNotFoundException)
{
return BadRequest(new ProblemDetails
{
Title = "feature id is invalid"
});
}
catch (UnknownDbException)
{
return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
using Feature.Manager.Api.FeatureRuns;
using Feature.Manager.Api.FeatureRuns.Exceptions;
using Feature.Manager.Api.FeatureRuns.ViewModels;
using Feature.Manager.Api.Features.ViewModels;
using Feature.Manager.Api.Features;
using Feature.Manager.Api.Features.Exceptions;
using Feature.Manager.Api.Uuid;
using Moq;
using NUnit.Framework;

namespace Feature.Manager.UnitTest.FeatureRuns
{
public class FeatureRunCreateService
public class FeatureRunCreateServiceTest
{
private Mock<IFeatureRunRepository> _mock;
private FeatureRunService _featureRunService;
private Mock<IFeatureRepository> _featureRepository;
private Mock<IFeatureRunRepository> _mock;
private UuidService _uuidService;

private FeatureRun MakeFeatureRun(StopResult? stopResult, string featId, DateTime? endAt)
Expand All @@ -25,17 +27,11 @@ private FeatureRun MakeFeatureRun(StopResult? stopResult, string featId, DateTim
Id = _uuidService.GenerateUuId(),
StartAt = DateTime.Now.Subtract(TimeSpan.FromDays(5)),
RunToken = _uuidService.GenerateUuId(),
FeatId = featId,
FeatId = featId
};
if (stopResult.HasValue)
{
run.StopResult = stopResult.Value;
}
if (stopResult.HasValue) run.StopResult = stopResult.Value;

if (endAt.HasValue)
{
run.EndAt = endAt;
}
if (endAt.HasValue) run.EndAt = endAt;

return run;
}
Expand All @@ -51,7 +47,7 @@ public void Setup()
Id = "rand",
FeatId = "APP-2",
RunToken = "run-token",
StartAt = DateTime.Now.Subtract(TimeSpan.FromHours(2)),
StartAt = DateTime.Now.Subtract(TimeSpan.FromHours(2))
});

// APP-1 will have 1 item with no end date, (NO STOP RESULT)
Expand All @@ -61,45 +57,65 @@ public void Setup()
// APP-5 will have 1 item with end date BUT with stop result of all B (to show that even if you set it to B, you can still create new runs)
mock.Setup(x => x.GetRunsForFeatureByFeatId("APP-1")).ReturnsAsync(new List<FeatureRun>
{
MakeFeatureRun(null, "APP-1", null),
MakeFeatureRun(null, "APP-1", null)
});
mock.Setup(x => x.GetRunsForFeatureByFeatId("APP-2")).ReturnsAsync(new List<FeatureRun>
{
MakeFeatureRun(StopResult.ChangeSettings, "APP-2", DateTime.Now.Subtract(TimeSpan.FromDays(2))),
MakeFeatureRun(null, "APP-2", null),
MakeFeatureRun(null, "APP-2", null)
});
mock.Setup(x => x.GetRunsForFeatureByFeatId("APP-3")).ReturnsAsync(new List<FeatureRun>
{
MakeFeatureRun(StopResult.ChangeSettings, "APP-3", DateTime.Now.Subtract(TimeSpan.FromDays(2))),
MakeFeatureRun(StopResult.ChangeSettings, "APP-3", DateTime.Now.Subtract(TimeSpan.FromDays(1))),
MakeFeatureRun(StopResult.ChangeSettings, "APP-3", DateTime.Now.Subtract(TimeSpan.FromHours(12))),
MakeFeatureRun(StopResult.ChangeSettings, "APP-3", DateTime.Now.Subtract(TimeSpan.FromHours(12)))
});
mock.Setup(x => x.GetRunsForFeatureByFeatId("APP-4")).ReturnsAsync(new List<FeatureRun>());
mock.Setup(x => x.GetRunsForFeatureByFeatId("APP-5")).ReturnsAsync(new List<FeatureRun>
{
MakeFeatureRun(StopResult.AllB, "APP-5", DateTime.Now.Subtract(TimeSpan.FromHours(12))),
MakeFeatureRun(StopResult.AllB, "APP-5", DateTime.Now.Subtract(TimeSpan.FromHours(12)))
});
mock.Setup(x => x.GetRunsForFeatureByFeatId("APP-19")).ReturnsAsync(new List<FeatureRun>());
_mock = mock;
_featureRunService = new FeatureRunService(_mock.Object);
var featureRepository = new Mock<IFeatureRepository>();
featureRepository.Setup(x => x.FindByFeatId(It.IsAny<string>())).ReturnsAsync((string featId) =>
{
if (featId == "APP-19")
{
return null;
}
return new Api.Features.Feature
{
Description = "asdfasdfasdf",
Hypothesis = "asdfasdfsd",
Id = "asdfasdf",
FeatId = featId,
FeatureToken = "asldf"
};
});
_featureRepository = featureRepository;
_featureRunService = new FeatureRunService(_mock.Object, _featureRepository.Object);
}

[Test]
public async Task TestCannotCreateNewRunIfARunIsAlreadyRunning()
{
Assert.ThrowsAsync<FeatureAlreadyRunningException>(() => _featureRunService.CreateFeatureRun(new CreateFeatureRunRequest
{
Allocation = 100,
EndAt = DateTime.Now.Add(TimeSpan.FromDays(20)),
StartAt = DateTime.Now,
FeatId = "APP-1"
}));
Assert.ThrowsAsync<FeatureAlreadyRunningException>(() => _featureRunService.CreateFeatureRun(new CreateFeatureRunRequest
{
Allocation = 100,
EndAt = DateTime.Now.Add(TimeSpan.FromDays(20)),
StartAt = DateTime.Now,
FeatId = "APP-2"
}));
Assert.ThrowsAsync<FeatureAlreadyRunningException>(() => _featureRunService.CreateFeatureRun(
new CreateFeatureRunRequest
{
Allocation = 100,
EndAt = DateTime.Now.Add(TimeSpan.FromDays(20)),
StartAt = DateTime.Now,
FeatId = "APP-1"
}));
Assert.ThrowsAsync<FeatureAlreadyRunningException>(() => _featureRunService.CreateFeatureRun(
new CreateFeatureRunRequest
{
Allocation = 100,
EndAt = DateTime.Now.Add(TimeSpan.FromDays(20)),
StartAt = DateTime.Now,
FeatId = "APP-2"
}));
}

[Test]
Expand All @@ -114,5 +130,18 @@ public async Task TestCreatesNewRunWhenNoFeaturesAreRunning()
});
Assert.NotNull(result);
}

[Test]
public async Task TestCreateFailsIfFeatureDoesNotExist()
{
Assert.ThrowsAsync<FeatureNotFoundException>(() => _featureRunService.CreateFeatureRun(
new CreateFeatureRunRequest
{
Allocation = 100,
EndAt = DateTime.Now.Add(TimeSpan.FromDays(20)),
StartAt = DateTime.Now,
FeatId = "APP-19"
}));
}
}
}
}
6 changes: 3 additions & 3 deletions Feature.Manager.UnitTest/FeatureRuns/FeatureRunServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void Setup()
});
mock.Setup(x => x.GetRunsForFeatureByFeatId("APP-2")).ThrowsAsync(new InvalidCastException());
_mock = mock;
_featureRunService = new FeatureRunService(_mock.Object);
_featureRunService = new FeatureRunService(_mock.Object, null);
}

[Test]
Expand Down Expand Up @@ -186,7 +186,7 @@ public async Task TestGetRunningFeaturesReturnsFromRepository()
},
};
mock.Setup(x => x.GetRunningFeatures()).ReturnsAsync(mockData);
var systemUnderTest = new FeatureRunService(mock.Object);
var systemUnderTest = new FeatureRunService(mock.Object, null);
var result = await systemUnderTest.GetRunningFeatures();
Assert.AreSame(mockData, result);
}
Expand All @@ -196,7 +196,7 @@ public async Task TestGetRunningFeaturesHandlesException()
{
var mock = new Mock<IFeatureRunRepository>();
mock.Setup(x => x.GetRunningFeatures()).ThrowsAsync(new InvalidCastException());
var systemUnderTest = new FeatureRunService(mock.Object);
var systemUnderTest = new FeatureRunService(mock.Object, null);
Assert.ThrowsAsync<UnknownDbException>(() => systemUnderTest.GetRunningFeatures());
}
}
Expand Down

0 comments on commit 26f0a21

Please # to comment.