Exemple #1
0
        public async void SaveFigure_ShouldReturnOk_WhenFigureDataIsValid()
        {
            // Arrange
            var figureDto = new FigureDto()
            {
                Type       = FigureType.Circle,
                Parameters = new double[] { 10 }
            };
            var    createdFigure = new Figure();
            string tmp;

            _figureResolverMock.Setup(r => r.IsFigureTypeSupported(figureDto.Type)).Returns(true);
            _figureResolverMock.Setup(r => r.TryParseFigureParameters(figureDto.Type, figureDto.Parameters, out tmp)).Returns(true);
            _figuresRepositoryMock.Setup(r => r.Add(It.IsNotNull <Figure>())).Returns(createdFigure);

            // Act
            var response = await Controller.SaveFigure(figureDto, default);

            // Assert
            _figureResolverMock.Verify(r => r.IsFigureTypeSupported(figureDto.Type), Times.Once);
            _figureResolverMock.Verify(r => r.TryParseFigureParameters(figureDto.Type, figureDto.Parameters, out tmp), Times.Once);
            _figuresRepositoryMock.Verify(r => r.Add(It.IsNotNull <Figure>()), Times.Once);
            _figuresRepositoryMock.Verify(r => r.SaveAsync(default), Times.Once);
        public async Task <IActionResult> SaveFigure([FromBody] FigureDto figureDto, CancellationToken cancellationToken)
        {
            if (!_figureResolver.IsFigureTypeSupported(figureDto.Type))
            {
                return(BadRequest($"Figure type = {figureDto.Type} is not supported"));
            }

            if (!_figureResolver.TryParseFigureParameters(figureDto.Type, figureDto.Parameters, out var errorMessage))
            {
                return(BadRequest(errorMessage));
            }

            var figure = new Figure()
            {
                Parameters = figureDto.Parameters,
                Type       = figureDto.Type
            };

            var createdFigure = _figuresRepository.Add(figure);
            await _figuresRepository.SaveAsync(cancellationToken);

            return(Ok(createdFigure.Id));
        }