|
| 1 | +using System; |
| 2 | +using GettingStarted.Models; |
| 3 | +using GettingStarted.ResourceDefinitionExample; |
| 4 | +using JsonApiDotNetCore.Builders; |
| 5 | +using JsonApiDotNetCore.Data; |
| 6 | +using JsonApiDotNetCore.Graph; |
| 7 | +using JsonApiDotNetCore.Models; |
| 8 | +using JsonApiDotNetCore.Services; |
| 9 | +using Microsoft.Extensions.DependencyInjection; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Moq; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace DiscoveryTests |
| 15 | +{ |
| 16 | + public class ServiceDiscoveryFacadeTests |
| 17 | + { |
| 18 | + private readonly IServiceCollection _services = new ServiceCollection(); |
| 19 | + private readonly ContextGraphBuilder _graphBuilder = new ContextGraphBuilder(); |
| 20 | + private ServiceDiscoveryFacade _facade => new ServiceDiscoveryFacade(_services, _graphBuilder); |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void AddAssembly_Adds_All_Resources_To_Graph() |
| 24 | + { |
| 25 | + // arrange, act |
| 26 | + _facade.AddAssembly(typeof(Person).Assembly); |
| 27 | + |
| 28 | + // assert |
| 29 | + var graph = _graphBuilder.Build(); |
| 30 | + var personResource = graph.GetContextEntity(typeof(Person)); |
| 31 | + var articleResource = graph.GetContextEntity(typeof(Article)); |
| 32 | + var modelResource = graph.GetContextEntity(typeof(Model)); |
| 33 | + |
| 34 | + Assert.NotNull(personResource); |
| 35 | + Assert.NotNull(articleResource); |
| 36 | + Assert.NotNull(modelResource); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void AddCurrentAssembly_Adds_Resources_To_Graph() |
| 41 | + { |
| 42 | + // arrange, act |
| 43 | + _facade.AddCurrentAssembly(); |
| 44 | + |
| 45 | + // assert |
| 46 | + var graph = _graphBuilder.Build(); |
| 47 | + var testModelResource = graph.GetContextEntity(typeof(TestModel)); |
| 48 | + Assert.NotNull(testModelResource); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void AddCurrentAssembly_Adds_Services_To_Container() |
| 53 | + { |
| 54 | + // arrange, act |
| 55 | + _facade.AddCurrentAssembly(); |
| 56 | + |
| 57 | + // assert |
| 58 | + var services = _services.BuildServiceProvider(); |
| 59 | + Assert.IsType<TestModelService>(services.GetService<IResourceService<TestModel>>()); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void AddCurrentAssembly_Adds_Repositories_To_Container() |
| 64 | + { |
| 65 | + // arrange, act |
| 66 | + _facade.AddCurrentAssembly(); |
| 67 | + |
| 68 | + // assert |
| 69 | + var services = _services.BuildServiceProvider(); |
| 70 | + Assert.IsType<TestModelRepository>(services.GetService<IEntityRepository<TestModel>>()); |
| 71 | + } |
| 72 | + |
| 73 | + public class TestModel : Identifiable { } |
| 74 | + |
| 75 | + public class TestModelService : EntityResourceService<TestModel> |
| 76 | + { |
| 77 | + private static IEntityRepository<TestModel> _repo = new Mock<IEntityRepository<TestModel>>().Object; |
| 78 | + private static IJsonApiContext _jsonApiContext = new Mock<IJsonApiContext>().Object; |
| 79 | + public TestModelService() : base(_jsonApiContext, _repo) { } |
| 80 | + } |
| 81 | + |
| 82 | + public class TestModelRepository : DefaultEntityRepository<TestModel> |
| 83 | + { |
| 84 | + private static IDbContextResolver _dbContextResolver = new Mock<IDbContextResolver>().Object; |
| 85 | + private static IJsonApiContext _jsonApiContext = new Mock<IJsonApiContext>().Object; |
| 86 | + public TestModelRepository() : base(_jsonApiContext, _dbContextResolver) { } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments