|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using JsonApiDotNetCore.Builders; |
| 4 | +using JsonApiDotNetCore.Configuration; |
| 5 | +using JsonApiDotNetCore.Internal; |
| 6 | +using JsonApiDotNetCore.Models; |
| 7 | +using JsonApiDotNetCore.Services; |
| 8 | +using Moq; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace UnitTests |
| 12 | +{ |
| 13 | + public class DocumentBuilder_Tests |
| 14 | + { |
| 15 | + private readonly Mock<IJsonApiContext> _jsonApiContextMock; |
| 16 | + private readonly PageManager _pageManager; |
| 17 | + private readonly JsonApiOptions _options; |
| 18 | + |
| 19 | + public DocumentBuilder_Tests() |
| 20 | + { |
| 21 | + _jsonApiContextMock = new Mock<IJsonApiContext>(); |
| 22 | + |
| 23 | + _options = new JsonApiOptions(); |
| 24 | + |
| 25 | + _options.BuildContextGraph(builder => |
| 26 | + { |
| 27 | + builder.AddResource<Model>("models"); |
| 28 | + }); |
| 29 | + |
| 30 | + _jsonApiContextMock |
| 31 | + .Setup(m => m.Options) |
| 32 | + .Returns(_options); |
| 33 | + |
| 34 | + _jsonApiContextMock |
| 35 | + .Setup(m => m.ContextGraph) |
| 36 | + .Returns(_options.ContextGraph); |
| 37 | + |
| 38 | + _jsonApiContextMock |
| 39 | + .Setup(m => m.MetaBuilder) |
| 40 | + .Returns(new MetaBuilder()); |
| 41 | + |
| 42 | + _pageManager = new PageManager(); |
| 43 | + _jsonApiContextMock |
| 44 | + .Setup(m => m.PageManager) |
| 45 | + .Returns(_pageManager); |
| 46 | + |
| 47 | + _jsonApiContextMock |
| 48 | + .Setup(m => m.BasePath) |
| 49 | + .Returns("localhost"); |
| 50 | + |
| 51 | + _jsonApiContextMock |
| 52 | + .Setup(m => m.RequestEntity) |
| 53 | + .Returns(_options.ContextGraph.GetContextEntity(typeof(Model))); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void Includes_Paging_Links_By_Default() |
| 58 | + { |
| 59 | + // arrange |
| 60 | + _pageManager.PageSize = 1; |
| 61 | + _pageManager.TotalRecords = 1; |
| 62 | + _pageManager.CurrentPage = 1; |
| 63 | + |
| 64 | + var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object); |
| 65 | + var entity = new Model(); |
| 66 | + |
| 67 | + // act |
| 68 | + var document = documentBuilder.Build(entity); |
| 69 | + |
| 70 | + // assert |
| 71 | + Assert.NotNull(document.Links); |
| 72 | + Assert.NotNull(document.Links.Last); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void Page_Links_Can_Be_Disabled_Globally() |
| 77 | + { |
| 78 | + // arrange |
| 79 | + _pageManager.PageSize = 1; |
| 80 | + _pageManager.TotalRecords = 1; |
| 81 | + _pageManager.CurrentPage = 1; |
| 82 | + |
| 83 | + _options.BuildContextGraph(builder => |
| 84 | + { |
| 85 | + builder.DocumentLinks = Link.None; |
| 86 | + builder.AddResource<Model>("models"); |
| 87 | + }); |
| 88 | + |
| 89 | + _jsonApiContextMock |
| 90 | + .Setup(m => m.ContextGraph) |
| 91 | + .Returns(_options.ContextGraph); |
| 92 | + |
| 93 | + var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object); |
| 94 | + var entity = new Model(); |
| 95 | + |
| 96 | + // act |
| 97 | + var document = documentBuilder.Build(entity); |
| 98 | + |
| 99 | + // assert |
| 100 | + Assert.Null(document.Links); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void Related_Links_Can_Be_Disabled() |
| 105 | + { |
| 106 | + // arrange |
| 107 | + _pageManager.PageSize = 1; |
| 108 | + _pageManager.TotalRecords = 1; |
| 109 | + _pageManager.CurrentPage = 1; |
| 110 | + |
| 111 | + _jsonApiContextMock |
| 112 | + .Setup(m => m.ContextGraph) |
| 113 | + .Returns(_options.ContextGraph); |
| 114 | + |
| 115 | + var documentBuilder = new DocumentBuilder(_jsonApiContextMock.Object); |
| 116 | + var entity = new Model(); |
| 117 | + |
| 118 | + // act |
| 119 | + var document = documentBuilder.Build(entity); |
| 120 | + |
| 121 | + // assert |
| 122 | + Assert.Null(document.Data.Relationships["related-model"].Links); |
| 123 | + } |
| 124 | + |
| 125 | + private class Model : Identifiable |
| 126 | + { |
| 127 | + [HasOne("related-model", Link.None)] |
| 128 | + public RelatedModel RelatedModel { get; set; } |
| 129 | + public int RelatedModelId { get; set; } |
| 130 | + } |
| 131 | + |
| 132 | + private class RelatedModel : Identifiable |
| 133 | + { |
| 134 | + [HasMany("models")] |
| 135 | + public List<Model> Models { get; set; } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments