示例#1
0
        private void CheckCreatedFigureResponce(IActionResult actionResult, FigureRequest request)
        {
            var response = actionResult.Should().BeOfType <OkObjectResult>()
                           .Subject.Value.Should().BeOfType <CreatedFigureResponce>()
                           .Subject;

            response.Should().NotBeNull();
            response.Type.Should().Be(request.Type);
            response.Id.Should().Be(_currentId);
        }
        public async Task CreateFigureAsyncWithInvalidData_ShouldThrowException(FigureRequest request, Type expectedException)
        {
            //arrange
            var figureRepository = new FigureRepository(null);
            var figureService    = new FigureService(figureRepository);
            var controller       = new FigureController(figureService);

            //act & assert
            var exception = await Assert.ThrowsAsync(expectedException, async() => await controller.CreateFigureAsync(request));
        }
        public static FigureRecord ToFigureRecord(this FigureRequest request)
        {
            if (!Enum.TryParse <FigureType>(request.Type, out var type))
            {
                throw new InvalidFigureTypeException(request.Type);
            }

            string jsonParams = JsonConvert.SerializeObject(request.Params);

            return(new FigureRecord()
            {
                Type = type,
                Params = jsonParams
            });
        }
示例#4
0
        public async Task CreateCircle()
        {
            //arrange
            _currentId++;

            var request = new FigureRequest()
            {
                Type = FigureType.Circle.ToString(), Params = new Dictionary <string, double> {
                    { nameof(Circle.Radius), 2 }
                }
            };

            //act
            var actionResult = await _controller.CreateFigureAsync(request);

            //assert
            CheckCreatedFigureResponce(actionResult, request);
        }
示例#5
0
        public async Task CreateTriangle()
        {
            //arrange
            _currentId++;

            var request = new FigureRequest()
            {
                Type   = FigureType.Triangle.ToString(),
                Params = new Dictionary <string, double> {
                    { nameof(Triangle.SideA), 18 },
                    { nameof(Triangle.SideB), 30 },
                    { nameof(Triangle.SideC), 24 }
                }
            };

            //act
            var actionResult = await _controller.CreateFigureAsync(request);

            //assert
            CheckCreatedFigureResponce(actionResult, request);
        }
示例#6
0
        public async Task <CreatedFigureResponce> SaveFigureAsync(FigureRequest figureRequest)
        {
            if (figureRequest.Params == null || figureRequest.Params.Count == 0)
            {
                throw new InvalidFigureRequestException(nameof(figureRequest.Params));
            }

            var record = figureRequest.ToFigureRecord();

            var figure = DeserializeToFigure(record);

            if (!figure.IsValid())
            {
                throw new InvalidFigureException(figureRequest.Type, record.Params);
            }

            var id = (await _repository.SaveAsync(record)).Id;

            return(new CreatedFigureResponce()
            {
                Type = figureRequest.Type,
                Id = id
            });
        }
示例#7
0
        public async Task <IActionResult> CreateFigureAsync([FromBody] FigureRequest figure)
        {
            var figureResponse = await FigureService.SaveFigureAsync(figure);

            return(Ok(figureResponse));
        }