示例#1
0
        public ActionResult <MachineReadDto> CreateMachine(MachineCreateDto createDto)
        {
            _service.CreateMachine(createDto);


            return(Created($"api/Machine", null));
        }
示例#2
0
        public void MethodShouldCallCreateMachineMethodFromRepoOnce()
        {
            //Given
            var machineRepo = new Mock <IMachineRepository>();

            //When
            machineRepo.Setup(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>()));

            var machineService = new MachineService(machineRepo.Object);

            machineService.CreateMachine();
            //Then
            machineRepo.Verify(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>()), Times.Once);
        }
示例#3
0
        public void MethodShoouldNotCallRepoMethodAndThrowInvalidDataExceptionIfMachineIsNull()
        {
            //Given
            var machineRepo = new Mock <IMachineRepository>();

            //When
            machineRepo.Setup(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>()));

            var machineService = new MachineService(machineRepo.Object);


            //Then
            Assert.Throws <InvalidDataException>(() => machineService.CreateMachine(null));
            machineRepo.Verify(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>()), Times.Never);
        }
示例#4
0
        public async Task <IHttpActionResult> CreateMachine(MachineCreate machine)
        {
            //Check if model is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState)); //404
            }

            //Instantiate service
            MachineService service = CreateMachineService();

            if (await service.CreateMachine(machine) == false)
            {
                return(InternalServerError());
            }

            return(Ok()); //200
        }
        public async Task <ActionResult <MachineDTO> > CreateMachine(MachineDTO machineDTO)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var machine = await _serviceMachine.CreateMachine(machineDTO);

                return(CreatedAtAction("CreateMachine", _mapper.Map <Machine, MachineDTO>(machine)));
            }
            catch (KeyNotFoundException exception)
            {
                return(NotFound(exception.Message));
            }
        }
示例#6
0
        public void MethodShouldNotCallMachineRepoAndThrowExceptionIfDataIsInvalid(string name, string type)
        {
            //Given
            var machineRepo = new Mock <IMachineRepository>();
            var machine     = new AlarmSystem.Core.Entity.DB.Machine
            {
                MachineId = "machine-id-test",
                Name      = name,
                Type      = type
            };

            //When
            machineRepo.Setup(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>()));

            var machineService = new MachineService(machineRepo.Object);


            //Then
            Assert.Throws <InvalidDataException>(() => machineService.CreateMachine(machine));
            machineRepo.Verify(mr => mr.CreateMachine(It.IsAny <AlarmSystem.Core.Entity.DB.Machine>()), Times.Never);
        }