public void DeserializeAttributes_VariousUpdatedMembers_RegistersTargetedFields()
        {
            // Arrange
            SetupFieldsManager(out List <AttrAttribute> attributesToUpdate, out List <RelationshipAttribute> relationshipsToUpdate);
            Document content = CreateTestResourceDocument();
            var      body    = JsonConvert.SerializeObject(content);

            // Act
            _deserializer.Deserialize(body);

            // Assert
            Assert.Equal(5, attributesToUpdate.Count);
            Assert.Empty(relationshipsToUpdate);
        }
示例#2
0
        public void When_resource_has_constructor_with_string_parameter_it_must_fail()
        {
            // Arrange
            var options = new JsonApiOptions();

            IResourceGraph graph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add <ResourceWithStringConstructor>().Build();

            var serviceContainer = new ServiceContainer();

            serviceContainer.AddService(typeof(IResourceDefinitionAccessor), new NeverResourceDefinitionAccessor());

            var serializer = new RequestDeserializer(graph, new ResourceFactory(serviceContainer), new TargetedFields(), _mockHttpContextAccessor.Object,
                                                     _requestMock.Object, options);

            var body = new
            {
                data = new
                {
                    id   = "1",
                    type = "resourceWithStringConstructors"
                }
            };

            string content = JsonConvert.SerializeObject(body);

            // Act
            Action action = () => serializer.Deserialize(content);

            // Assert
            var exception = Assert.Throws <InvalidOperationException>(action);

            Assert.Equal("Failed to create an instance of 'UnitTests.Models.ResourceWithStringConstructor' using injected constructor parameters.",
                         exception.Message);
        }
示例#3
0
        public void When_resource_has_default_constructor_it_must_succeed()
        {
            // Arrange
            var options = new JsonApiOptions();

            IResourceGraph graph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add <ResourceWithoutConstructor>().Build();

            var serviceContainer = new ServiceContainer();

            serviceContainer.AddService(typeof(IResourceDefinitionAccessor), new NeverResourceDefinitionAccessor());

            var serializer = new RequestDeserializer(graph, new ResourceFactory(serviceContainer), new TargetedFields(), _mockHttpContextAccessor.Object,
                                                     _requestMock.Object, options);

            var body = new
            {
                data = new
                {
                    id   = "1",
                    type = "resourceWithoutConstructors"
                }
            };

            string content = JsonConvert.SerializeObject(body);

            // Act
            object result = serializer.Deserialize(content);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(typeof(ResourceWithoutConstructor), result.GetType());
        }
        public void When_resource_has_constructor_with_injectable_parameter_it_must_succeed()
        {
            // Arrange
            var graph = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance)
                        .AddResource <ResourceWithDbContextConstructor>()
                        .Build();

            var appDbContext = new AppDbContext(new DbContextOptionsBuilder <AppDbContext>().Options, new FrozenSystemClock());

            var serviceContainer = new ServiceContainer();

            serviceContainer.AddService(typeof(AppDbContext), appDbContext);

            var serializer = new RequestDeserializer(graph, new DefaultResourceFactory(serviceContainer), new TargetedFields());

            var body = new
            {
                data = new
                {
                    id   = "1",
                    type = "resourceWithDbContextConstructors"
                }
            };

            string content = JsonConvert.SerializeObject(body);

            // Act
            object result = serializer.Deserialize(content);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(typeof(ResourceWithDbContextConstructor), result.GetType());
            Assert.Equal(appDbContext, ((ResourceWithDbContextConstructor)result).AppDbContext);
        }
        public void When_resource_has_default_constructor_that_throws_it_must_fail()
        {
            // Arrange
            var graph = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance)
                        .AddResource <ResourceWithThrowingConstructor>()
                        .Build();

            var serializer = new RequestDeserializer(graph, new DefaultResourceFactory(new ServiceContainer()), new TargetedFields());

            var body = new
            {
                data = new
                {
                    id   = "1",
                    type = "resourceWithThrowingConstructors"
                }
            };

            string content = JsonConvert.SerializeObject(body);

            // Act
            Action action = () => serializer.Deserialize(content);

            // Assert
            var exception = Assert.Throws <InvalidOperationException>(action);

            Assert.Equal(
                "Failed to create an instance of 'UnitTests.Models.ResourceWithThrowingConstructor' using its default constructor.",
                exception.Message);
        }
        public void When_resource_has_default_constructor_it_must_succeed()
        {
            // Arrange
            var graph = new ResourceGraphBuilder(new JsonApiOptions(), NullLoggerFactory.Instance)
                        .AddResource <ResourceWithoutConstructor>()
                        .Build();

            var serializer = new RequestDeserializer(graph, new DefaultResourceFactory(new ServiceContainer()), new TargetedFields());

            var body = new
            {
                data = new
                {
                    id   = "1",
                    type = "resourceWithoutConstructors"
                }
            };

            string content = JsonConvert.SerializeObject(body);

            // Act
            object result = serializer.Deserialize(content);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(typeof(ResourceWithoutConstructor), result.GetType());
        }
        public void When_model_has_no_parameterless_contructor_it_must_fail()
        {
            // Arrange
            var graph = new ResourceGraphBuilder(new JsonApiOptions()).AddResource <ResourceWithParameters>().Build();

            var serializer = new RequestDeserializer(graph, new TargetedFields());

            var body = new
            {
                data = new
                {
                    id   = "1",
                    type = "resourceWithParameters"
                }
            };
            string content = Newtonsoft.Json.JsonConvert.SerializeObject(body);

            // Act
            Action action = () => serializer.Deserialize(content);

            // Assert
            var exception = Assert.Throws <InvalidOperationException>(action);

            Assert.Equal("Failed to create an instance of 'UnitTests.Models.ConstructionTests+ResourceWithParameters' using its default constructor.", exception.Message);
        }
示例#8
0
        private void ParseCommand(ref ReadOnlySequence <byte> buffer)
        {
            if (_clientManager.SupportAcknowledgement)
            {
                ReadAcknowledgement(ref buffer);
            }

            short cmdType = 0;

            if (_clientManager.ClientVersion >= 5000)
            {
                var cmdTypeBuffer = buffer.Slice(0, 2);

                if (cmdTypeBuffer.First.Span.Length >= 2)
                {
                    cmdType = HelperFxn.ConvertToShort(cmdTypeBuffer.First.Span);
                }
                else
                {
                    cmdType = HelperFxn.ConvertToShort(cmdTypeBuffer.ToArray());
                }

                buffer = buffer.Slice(2, buffer.End);
            }

            var commandLengthBuffer = buffer.Slice(0, _headerLength);
            var cmdLength           = RequestDeserializer.ToInt32(commandLengthBuffer.ToArray(), 0, _headerLength);

            buffer = buffer.Slice(_headerLength, buffer.End);

            var    commandBuffer = buffer.Slice(0, cmdLength);
            object command       = null;

            using (var stream = new MemoryStream(commandBuffer.ToArray()))
                command = RequestDeserializer.Deserialize(cmdType, stream);

            buffer = buffer.Slice(cmdLength, buffer.End);

            if (CommandHelper.IsBasicCRUDOperation((Common.Protobuf.Command.Type)cmdType))
            {
                _cmdManager.ProcessCommand(_clientManager, command, cmdType, _acknowledgementId, null, buffer.Length > 0);
            }
            else
            {
                //Due to response pipelining, old responses of basic CRUD operations are queued for sending
                //therefore before executing this long running command, we should send those responses
                _clientManager.SendPendingResponses(true);
                ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessCommandAsync), new LongRunningCommand()
                {
                    Command = command, CommandType = cmdType, AcknowledgementId = _acknowledgementId
                });
            }

            _expectedLength    = null;
            _acknowledgementId = -1;
        }