コード例 #1
0
        public async Task CreatePeripheral_ResultOK()
        {
            var correctPeripheral = new PeripheralDto {
                Vendor = "Enterprise INC", CreationDate = DateTimeOffset.Now, Status = PeripheralStatus.Online, GatewayId = "4451-9834-7885-3446"
            };
            var result = await controller.CreatePeripheral(correctPeripheral);

            Assert.IsType <CreatedAtRouteResult>(result);
        }
コード例 #2
0
        public async Task CreatePeripheralMissingGatewayReference_ResultBadRequest()
        {
            var correctPeripheral = new PeripheralDto {
                Vendor = "Enterprise INC", CreationDate = DateTimeOffset.Now, Status = PeripheralStatus.Online
            };
            var result = await controller.CreatePeripheral(correctPeripheral);

            Assert.IsType <BadRequestObjectResult>(result);
        }
コード例 #3
0
        public async Task UpdatePeripheral__ReturnsOk()
        {
            const uint id = 3231232;
            var        inputPeripheral = new PeripheralDto {
                Vendor = "Modified Enterprise INC", CreationDate = DateTimeOffset.Now, Status = PeripheralStatus.Offline, GatewayId = "4451-9834-7885-3446"
            };
            var updateResult = await controller.UpdatePeripheral(id, inputPeripheral);

            Assert.IsType <OkObjectResult>(updateResult);
        }
コード例 #4
0
        public async Task CreatePeripheral_ReturnsBadRequest_MaxPeripheralAdmitted()
        {
            var peripheral = new PeripheralDto
            {
                Vendor       = "Enterprise INC",
                CreationDate = DateTimeOffset.Now,
                Status       = PeripheralStatus.Online,
                GatewayId    = "8763-3242-3343-8898"
            };

            var result = await controller.CreatePeripheral(peripheral);

            Assert.IsType <BadRequestObjectResult>(result);
        }
コード例 #5
0
        public async Task CreatePeripheral_ReturnsBadRequest_RequiredField()
        {
            var missingSerialPeripheral = new PeripheralDto
            {
                Vendor       = "Enterprise INC",
                CreationDate = DateTimeOffset.Now,
                GatewayId    = "4451-9834-7885-3446"
            };

            controller.ModelState.AddModelError("Status", "Required");
            var result = await controller.CreatePeripheral(missingSerialPeripheral);

            Assert.IsType <BadRequestObjectResult>(result);
        }
コード例 #6
0
        public async Task CreateValidPeripheral_AddItem()
        {
            var correctPeripheral = new PeripheralDto {
                Vendor = "Enterprise INC", CreationDate = DateTimeOffset.Now, Status = PeripheralStatus.Online, GatewayId = "4451-9834-7885-3446"
            };
            await controller.CreatePeripheral(correctPeripheral);

            var getResult = await controller.GetPeripherals();

            var okResult = getResult.Result as OkObjectResult;

            var items = okResult?.Value as IEnumerable <PeripheralDetailedDto>;

            Assert.Equal(13, items?.Count());
        }
コード例 #7
0
        public async Task UpdatePeripheral__ReturnsUpdatedItem()
        {
            const uint id = 3231232;
            var        inputPeripheral = new PeripheralDto {
                Vendor = "Modified Enterprise INC", CreationDate = DateTimeOffset.Now, Status = PeripheralStatus.Offline, GatewayId = "4451-9834-7885-3446"
            };
            await controller.UpdatePeripheral(id, inputPeripheral);

            var getAction = await controller.GetPeripheralById(id);

            var okGetResult = getAction.Result as OkObjectResult;

            var updatedPeripheralDb = okGetResult?.Value as PeripheralDetailedDto;

            Assert.Equal(updatedPeripheralDb?.Status, inputPeripheral.Status.ToDescriptionString());
            Assert.Equal(updatedPeripheralDb?.Vendor, inputPeripheral.Vendor);
        }
コード例 #8
0
        public async Task <ActionResult> UpdatePeripheral(uint id, [FromBody] PeripheralDto peripheralDto)
        {
            if (peripheralDto == null)
            {
                return(BadRequest("Peripheral object is null"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid model object"));
            }

            var peripheralEntity = await repoWrapper.Peripheral.GetPeripheralById(id);

            if (peripheralEntity == null)
            {
                return(NotFound());
            }

            var gateway = await repoWrapper.Gateway.GetGatewayById(peripheralEntity.GatewayId);

            if (gateway == null)
            {
                return(BadRequest("Invalid reference to gateway"));
            }

            mapper.Map(peripheralDto, peripheralEntity);

            repoWrapper.Peripheral.Update(peripheralEntity);
            var response = await repoWrapper.SaveAsync();

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }

            var peripheral = mapper.Map <Peripheral, PeripheralDetailedDto>(peripheralEntity);

            return(Ok(peripheral));
        }
コード例 #9
0
        public async Task <IActionResult> CreatePeripheral([FromBody] PeripheralDto peripheralDto)
        {
            if (peripheralDto == null)
            {
                return(BadRequest("Peripheral object is null"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid model object"));
            }

            var peripheralEntity = mapper.Map <PeripheralDto, Peripheral>(peripheralDto);

            var gateway = await repoWrapper.Gateway.GetGatewayById(peripheralEntity.GatewayId);

            if (gateway == null)
            {
                return(BadRequest("Invalid reference to gateway"));
            }
            if (gateway.AssociatedPeripherals.Count >= 10)
            {
                return(BadRequest("Gateway already has the maximum of admitted peripherals"));
            }

            repoWrapper.Peripheral.Create(peripheralEntity);
            var result = await repoWrapper.SaveAsync();

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            var peripheral = mapper.Map <Peripheral, PeripheralDetailedDto>(peripheralEntity);

            return(CreatedAtRoute("PeripheralById", new { id = peripheralEntity.UId }, peripheral));
        }