|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using JsonApiDotNetCore.Builders; |
| 4 | +using JsonApiDotNetCore.Internal; |
| 5 | +using JsonApiDotNetCore.Models; |
| 6 | +using JsonApiDotNetCore.Models.Operations; |
| 7 | +using JsonApiDotNetCore.Serialization; |
| 8 | +using JsonApiDotNetCore.Services; |
| 9 | +using JsonApiDotNetCore.Services.Operations.Processors; |
| 10 | +using Moq; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace UnitTests.Services |
| 14 | +{ |
| 15 | + public class CreateOpProcessorTests |
| 16 | + { |
| 17 | + private readonly Mock<ICreateService<TestResource>> _createServiceMock; |
| 18 | + private readonly Mock<IJsonApiDeSerializer> _deserializerMock; |
| 19 | + private readonly Mock<IDocumentBuilder> _documentBuilderMock; |
| 20 | + |
| 21 | + public CreateOpProcessorTests() |
| 22 | + { |
| 23 | + _createServiceMock = new Mock<ICreateService<TestResource>>(); |
| 24 | + _deserializerMock = new Mock<IJsonApiDeSerializer>(); |
| 25 | + _documentBuilderMock = new Mock<IDocumentBuilder>(); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task ProcessAsync_Deserializes_And_Creates() |
| 30 | + { |
| 31 | + // arrange |
| 32 | + var testResource = new TestResource { |
| 33 | + Name = "some-name" |
| 34 | + }; |
| 35 | + |
| 36 | + var data = new DocumentData { |
| 37 | + Type = "test-resources", |
| 38 | + Attributes = new Dictionary<string, object> { |
| 39 | + { "name", testResource.Name } |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + var operation = new Operation { |
| 44 | + Data = data, |
| 45 | + }; |
| 46 | + |
| 47 | + var contextGraph = new ContextGraphBuilder() |
| 48 | + .AddResource<TestResource>("test-resources") |
| 49 | + .Build(); |
| 50 | + |
| 51 | + _deserializerMock.Setup(m => m.DocumentToObject(It.IsAny<DocumentData>())) |
| 52 | + .Returns(testResource); |
| 53 | + |
| 54 | + var opProcessor = new CreateOpProcessor<TestResource>( |
| 55 | + _createServiceMock.Object, |
| 56 | + _deserializerMock.Object, |
| 57 | + _documentBuilderMock.Object, |
| 58 | + contextGraph |
| 59 | + ); |
| 60 | + |
| 61 | + _documentBuilderMock.Setup(m => m.GetData(It.IsAny<ContextEntity>(), It.IsAny<TestResource>())) |
| 62 | + .Returns(data); |
| 63 | + |
| 64 | + // act |
| 65 | + var result = await opProcessor.ProcessAsync(operation); |
| 66 | + |
| 67 | + // assert |
| 68 | + Assert.Equal(OperationCode.add, result.Op); |
| 69 | + Assert.NotNull(result.Data); |
| 70 | + Assert.Equal(testResource.Name, result.DataObject.Attributes["name"]); |
| 71 | + _createServiceMock.Verify(m => m.CreateAsync(It.IsAny<TestResource>())); |
| 72 | + } |
| 73 | + |
| 74 | + public class TestResource : Identifiable |
| 75 | + { |
| 76 | + [Attr("name")] |
| 77 | + public string Name { get; set; } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments