Skip to content

Commit 7057c89

Browse files
author
Bart Koelman
committed
Refactored tests for resource injection
1 parent 0c5d5e8 commit 7057c89

File tree

9 files changed

+528
-209
lines changed

9 files changed

+528
-209
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/InjectableResourceTests.cs

Lines changed: 0 additions & 209 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using Microsoft.AspNetCore.Authentication;
3+
4+
namespace JsonApiDotNetCoreExampleTests
5+
{
6+
internal sealed class FrozenSystemClock : ISystemClock
7+
{
8+
private static readonly DateTimeOffset _defaultTime =
9+
new DateTimeOffset(new DateTime(2000, 1, 1, 1, 1, 1), TimeSpan.FromHours(1));
10+
11+
public DateTimeOffset UtcNow { get; set; } = _defaultTime;
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations.Schema;
3+
using JsonApiDotNetCore.Resources;
4+
using JsonApiDotNetCore.Resources.Annotations;
5+
using Microsoft.AspNetCore.Authentication;
6+
7+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceConstructorInjection
8+
{
9+
public sealed class GiftCertificate : Identifiable
10+
{
11+
private readonly ISystemClock _systemClock;
12+
13+
[Attr]
14+
public DateTimeOffset IssueDate { get; set; }
15+
16+
[Attr(Capabilities = AttrCapabilities.AllowView)]
17+
[NotMapped]
18+
public bool HasExpired => IssueDate.AddYears(1) < _systemClock.UtcNow;
19+
20+
[HasOne]
21+
public PostOffice Issuer { get; set; }
22+
23+
public GiftCertificate(InjectionDbContext injectionDbContext)
24+
{
25+
_systemClock = injectionDbContext.SystemClock;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceConstructorInjection
7+
{
8+
public sealed class GiftCertificatesController : JsonApiController<GiftCertificate>
9+
{
10+
public GiftCertificatesController(IJsonApiOptions options, ILoggerFactory loggerFactory,
11+
IResourceService<GiftCertificate> resourceService)
12+
: base(options, loggerFactory, resourceService)
13+
{
14+
}
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceConstructorInjection
6+
{
7+
public sealed class InjectionDbContext : DbContext
8+
{
9+
public ISystemClock SystemClock { get; }
10+
11+
public DbSet<PostOffice> PostOffice { get; set; }
12+
public DbSet<GiftCertificate> GiftCertificates { get; set; }
13+
14+
public InjectionDbContext(DbContextOptions<InjectionDbContext> options, ISystemClock systemClock)
15+
: base(options)
16+
{
17+
SystemClock = systemClock ?? throw new ArgumentNullException(nameof(systemClock));
18+
}
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using Bogus;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceConstructorInjection
6+
{
7+
internal sealed class InjectionFakers : FakerContainer
8+
{
9+
private readonly IServiceProvider _serviceProvider;
10+
11+
private readonly Lazy<Faker<PostOffice>> _lazyPostOfficeFaker;
12+
private readonly Lazy<Faker<GiftCertificate>> _lazyGiftCertificateFaker;
13+
14+
public Faker<PostOffice> PostOffice => _lazyPostOfficeFaker.Value;
15+
public Faker<GiftCertificate> GiftCertificate => _lazyGiftCertificateFaker.Value;
16+
17+
public InjectionFakers(IServiceProvider serviceProvider)
18+
{
19+
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
20+
21+
_lazyPostOfficeFaker = new Lazy<Faker<PostOffice>>(() =>
22+
new Faker<PostOffice>()
23+
.UseSeed(GetFakerSeed())
24+
.CustomInstantiator(f => new PostOffice(ResolveDbContext()))
25+
.RuleFor(postOffice => postOffice.Address, f => f.Address.FullAddress()));
26+
27+
_lazyGiftCertificateFaker = new Lazy<Faker<GiftCertificate>>(() =>
28+
new Faker<GiftCertificate>()
29+
.UseSeed(GetFakerSeed())
30+
.CustomInstantiator(f => new GiftCertificate(ResolveDbContext()))
31+
.RuleFor(giftCertificate => giftCertificate.IssueDate, f => f.Date.PastOffset()));
32+
}
33+
34+
private InjectionDbContext ResolveDbContext()
35+
{
36+
using var scope = _serviceProvider.CreateScope();
37+
return scope.ServiceProvider.GetRequiredService<InjectionDbContext>();
38+
}
39+
}
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using JsonApiDotNetCore.Resources;
5+
using JsonApiDotNetCore.Resources.Annotations;
6+
using Microsoft.AspNetCore.Authentication;
7+
8+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceConstructorInjection
9+
{
10+
public sealed class PostOffice : Identifiable
11+
{
12+
private readonly ISystemClock _systemClock;
13+
14+
[Attr]
15+
public string Address { get; set; }
16+
17+
[Attr(Capabilities = AttrCapabilities.AllowView)]
18+
[NotMapped]
19+
public bool IsOpen => IsWithinOperatingHours();
20+
21+
[HasMany]
22+
public IList<GiftCertificate> GiftCertificates { get; set; }
23+
24+
public PostOffice(InjectionDbContext injectionDbContext)
25+
{
26+
_systemClock = injectionDbContext.SystemClock;
27+
}
28+
29+
private bool IsWithinOperatingHours()
30+
{
31+
var currentTime = _systemClock.UtcNow;
32+
return currentTime.DayOfWeek >= DayOfWeek.Monday && currentTime.DayOfWeek <= DayOfWeek.Friday && currentTime.Hour >= 9 && currentTime.Hour <= 17;
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceConstructorInjection
7+
{
8+
public sealed class PostOfficesController : JsonApiController<PostOffice>
9+
{
10+
public PostOfficesController(IJsonApiOptions options, ILoggerFactory loggerFactory,
11+
IResourceService<PostOffice> resourceService)
12+
: base(options, loggerFactory, resourceService)
13+
{
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)