Beispiel #1
0
        public async Task CreateMachineAsync_WhenSuccessfulCreation_ReturnsCreatedMachine()
        {
            _machineRepositoryMock.SetupCreateItemQuery();

            _machineRepositoryMock.Setup(m => m.CreateItemAsync(It.IsAny <Machine>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync((Machine m, CancellationToken ct) => m);

            var newMachine = Machine.Example();

            var result = await _controller.CreateMachineAsync(newMachine, Guid.NewGuid(), CancellationToken.None);

            Assert.Same(newMachine, result);
        }
        private async Task <object> CreateMachineAsync(dynamic input, CancellationToken cancellationToken)
        {
            Machine newMachine;

            try
            {
                newMachine = this.Bind <Machine>();
            }
            catch (Exception ex)
            {
                Logger.Info("Binding failed while attempting to create a Machine resource", ex);
                return(Response.BadRequestBindingException());
            }

            await RequiresAccess()
            .WithTenantIdExpansion(c => newMachine.TenantId)
            .ExecuteAsync(cancellationToken);

            try
            {
                var result = await _machinesController.CreateMachineAsync(newMachine, TenantId, cancellationToken);

                return(Negotiate
                       .WithModel(result)
                       .WithStatusCode(HttpStatusCode.Created));
            }
            catch (ValidationFailedException ex)
            {
                Logger.Debug("Validation failed for CreateMachine", ex);
                return(Response.BadRequestValidationFailed(ex.Errors));
            }
            catch (Exception ex)
            {
                Logger.Error("Failed to create machine resource due to an error", ex);
                return(Response.InternalServerError(ResponseReasons.InternalServerErrorCreateMachine));
            }
        }