Beispiel #1
0
        private async Task <(int, string)> ValidateLabel(DTOs.Label label, bool isCreation)
        {
            // Label could be neither null nor whitespace
            if (string.IsNullOrWhiteSpace(label.Name))
            {
                return(StatusCodes.Status400BadRequest, $"Label could be neither null nor whitespace.");
            }

            // Check label existence
            var labels = await _labelRepository.GetLabelsAsync();

            var labelRetrieved = labels.FirstOrDefault(l => l.Name == label.Name);

            if (labelRetrieved == null)
            {
                return(200, null);
            }

            if (isCreation)
            {
                return(StatusCodes.Status400BadRequest, $"Label '{label.Name}' already exists, cannot create.");
            }
            else
            {
                if (labelRetrieved.Id != label.Id)
                {
                    return(StatusCodes.Status400BadRequest, $"Label '{label.Name}' already exists, cannot update.");
                }
                else
                {
                    return(200, null);
                }
            }
        }
Beispiel #2
0
        public async Task <ActionResult <DTOs.Label> > PostAsync([FromBody] DTOs.Label label)
        {
            try
            {
                if (label == null)
                {
                    return(BadRequest($"Label object from body is null."));
                }

                // Validate label
                var(statusCode, msg) = await ValidateLabel(label, isCreation : true);

                if (statusCode == StatusCodes.Status400BadRequest)
                {
                    _logger.LogError(msg);
                    return(StatusCode(StatusCodes.Status400BadRequest, msg));
                }

                // Create Label
                var createdLabel = await _labelRepository.AddLabelAsync(_mapper.Map <Models.Label>(label));

                var labelDTO = _mapper.Map <DTOs.Label>(createdLabel);
                return(CreatedAtRoute("GetLabel", new { id = labelDTO.Id }, labelDTO));
            }
            catch (Exception)
            {
                var msg = "Error occurred while creating new label into database.";
                _logger.LogError(msg);
                return(StatusCode(StatusCodes.Status500InternalServerError, msg));
            }
        }
        public void PostAsync_Failed_LabelExisted()
        {
            var mockLabelRepo = new Mock <ILabelRepository>();
            var mockLogger    = new Mock <ILogger <LabelsController> >();
            var mockMapper    = new Mock <IMapper>();

            var labels = new List <Models.Label>()
            {
                new Models.Label {
                    Name = "LabelA"
                }, new Models.Label {
                    Name = "LabelB"
                }
            };

            mockLabelRepo.Setup(x => x.GetLabelsAsync()).Returns(Task.FromResult(labels.AsEnumerable()));

            // Arrange
            var controller = new LabelsController(mockLogger.Object, mockMapper.Object, mockLabelRepo.Object);

            // Act
            var label = new DTOs.Label {
                Name = "LabelA"
            };
            var response = controller.PostAsync(label);

            // Assert
            var result = response.Result.Result as ObjectResult;

            result.StatusCode.ShouldBe(400);
            result.Value.ShouldBe($"Label '{label.Name}' already exists, cannot create.");
        }
Beispiel #4
0
        public async Task <ActionResult <DTOs.Label> > PutAsync(int id, [FromBody] DTOs.Label label)
        {
            try
            {
                if (id != label.Id)
                {
                    return(BadRequest($"Label id from url and body are not identical."));
                }

                if (await _labelRepository.GetLabelAsync(id) == null)
                {
                    return(NotFound($"Label with id '{id}' not found."));
                }

                // Validate label
                var(statusCode, msg) = await ValidateLabel(label, isCreation : false);

                if (statusCode == StatusCodes.Status400BadRequest)
                {
                    _logger.LogError(msg);
                    return(StatusCode(StatusCodes.Status400BadRequest, msg));
                }

                var labelUpdated = await _labelRepository.UpdateLabelAsync(_mapper.Map <Models.Label>(label));

                var labelDTO = _mapper.Map <DTOs.Label>(labelUpdated);
                return(Ok(labelDTO));
            }
            catch (Exception)
            {
                var msg = "Error occurred while updating data of database.";
                _logger.LogError(msg);
                return(StatusCode(StatusCodes.Status500InternalServerError, msg));
            }
        }
        public void PutAsync_Failed_LabelIsNullOrWhiteSpace(string labelName)
        {
            var mockLabelRepo = new Mock <ILabelRepository>();
            var mockLogger    = new Mock <ILogger <LabelsController> >();
            var mockMapper    = new Mock <IMapper>();

            var labelModel = new Models.Label {
                Id = 1, Name = "LabelA"
            };

            mockLabelRepo.Setup(x => x.GetLabelAsync(It.IsAny <int>())).Returns(Task.FromResult(labelModel));

            // Arrange
            var controller = new LabelsController(mockLogger.Object, mockMapper.Object, mockLabelRepo.Object);

            // Act
            var label = new DTOs.Label {
                Id = 1, Name = labelName
            };
            var response = controller.PutAsync(1, label);

            // Assert
            var result = response.Result.Result as ObjectResult;

            result.StatusCode.ShouldBe(400);
            result.Value.ShouldBe($"Label could be neither null nor whitespace.");
        }
        public void PutAsync_Succeed()
        {
            var mockLabelRepo = new Mock <ILabelRepository>();
            var mockLogger    = new Mock <ILogger <LabelsController> >();
            var mapper        = new Mapper(new MapperConfiguration(cfg => cfg.AddProfile(new Mappings.MappingProfile())));

            var labelModel = new Models.Label {
                Id = 1, Name = "LabelA"
            };

            mockLabelRepo.Setup(x => x.GetLabelAsync(It.IsAny <int>())).Returns(Task.FromResult(labelModel));
            var labels = new List <Models.Label>()
            {
                labelModel, new Models.Label {
                    Name = "LabelB"
                }
            };

            mockLabelRepo.Setup(x => x.GetLabelsAsync()).Returns(Task.FromResult(labels.AsEnumerable()));
            var newLabelModel = new Models.Label {
                Id = 1, Name = "NewLabel"
            };

            mockLabelRepo.Setup(x => x.UpdateLabelAsync(It.IsAny <Models.Label>())).Returns(Task.FromResult(newLabelModel));

            // Arrange
            var controller = new LabelsController(mockLogger.Object, mapper, mockLabelRepo.Object);

            // Act
            var labelDTO = new DTOs.Label {
                Id = 1, Name = "NewLabel"
            };
            var response = controller.PutAsync(1, labelDTO);

            // Assert
            var result = response.Result.Result as ObjectResult;

            result.StatusCode.ShouldBe(200);
            var returnedLabel = result.Value as DTOs.Label;

            returnedLabel.Name.ShouldBe(labelDTO.Name);
        }
        public void PostAsync_Failed_LabelIsNullOrWhiteSpace(string labelName)
        {
            var mockLabelRepo = new Mock <ILabelRepository>();
            var mockLogger    = new Mock <ILogger <LabelsController> >();
            var mockMapper    = new Mock <IMapper>();

            // Arrange
            var controller = new LabelsController(mockLogger.Object, mockMapper.Object, mockLabelRepo.Object);

            // Act
            var label = new DTOs.Label {
                Name = labelName
            };
            var response = controller.PostAsync(label);

            // Assert
            var result = response.Result.Result as ObjectResult;

            result.StatusCode.ShouldBe(400);
            result.Value.ShouldBe($"Label could be neither null nor whitespace.");
        }
        public void PutAsync_Failed_LabelIdNotIdentical()
        {
            var mockLabelRepo = new Mock <ILabelRepository>();
            var mockLogger    = new Mock <ILogger <LabelsController> >();
            var mockMapper    = new Mock <IMapper>();

            // Arrange
            var controller = new LabelsController(mockLogger.Object, mockMapper.Object, mockLabelRepo.Object);

            // Act
            var label = new DTOs.Label {
                Id = 1, Name = "LabelA"
            };
            var response = controller.PutAsync(2, label);

            // Assert
            var result = response.Result.Result as ObjectResult;

            result.StatusCode.ShouldBe(400);
            result.Value.ShouldBe($"Label id from url and body are not identical.");
        }
        public void PutAsync_Failed_LabelNotFound()
        {
            var mockLabelRepo = new Mock <ILabelRepository>();
            var mockLogger    = new Mock <ILogger <LabelsController> >();
            var mockMapper    = new Mock <IMapper>();

            mockLabelRepo.Setup(x => x.GetLabelAsync(It.IsAny <int>())).Returns(Task.FromResult <Models.Label>(null));

            // Arrange
            var controller = new LabelsController(mockLogger.Object, mockMapper.Object, mockLabelRepo.Object);

            // Act
            var label = new DTOs.Label {
                Id = 1, Name = "NewLabelA"
            };
            var response = controller.PutAsync(1, label);

            // Assert
            var result = response.Result.Result as ObjectResult;

            result.StatusCode.ShouldBe(404);
            result.Value.ShouldBe($"Label with id '{label.Id}' not found.");
        }