public async Task When_Recommendations_Are_Found_Return_Values() { //arrange _length = 1; _age = 1; _type = CrossCountrySkiType.Classic; var expectedObj = new CrossCountrySkiRecommendation(_length, string.Empty); _crossCountrySkiService.Setup(s => s.GetRecommendationAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CrossCountrySkiType>())) .Returns(Task.FromResult(expectedObj)); //act var result = await _crossCountrySkiController.GetRecommendationAsync(_length, _age, _type); //assert Assert.IsInstanceOfType(result, typeof(OkObjectResult)); _crossCountrySkiService.Verify(s => s.GetRecommendationAsync( It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CrossCountrySkiType>()), Times.Once); var okObj = (OkObjectResult)result; Assert.IsInstanceOfType(okObj.Value, typeof(CrossCountrySkiRecommendation)); var recom = (CrossCountrySkiRecommendation)okObj.Value; Assert.AreEqual(recom.SkiLength, _length); }
public async Task <CrossCountrySkiCategory> GetCategoryAsync(int age, CrossCountrySkiType type) { var categories = await _crossCountrySkiCategoryRepository.GetCategoriesAsync(); if (!categories.Any()) { return(null); } var ageFilteredCategories = categories.Where(c => c.ToAge.HasValue ? c.FromAge <= age && c.ToAge.Value >= age : c.FromAge <= age).ToList(); return(ageFilteredCategories.FirstOrDefault(c => string.IsNullOrEmpty(c.Style) ? true : Enum.TryParse(c.Style, out CrossCountrySkiType currType) ? currType == type : false)); }
public async Task When_Categories_Are_Found_Return_Matched_Category(int age, CrossCountrySkiType type, string name) { //arrange var categories = GetCategories(); _crossCountrySkiCategoryRepository.Setup(s => s.GetCategoriesAsync()) .Returns(Task.FromResult(categories)); //act var result = await _crossCountrySkiCategoryService.GetCategoryAsync(age, type); //assert Assert.IsNotNull(result); Assert.AreEqual(result.Name, name); _crossCountrySkiCategoryRepository.Verify(s => s.GetCategoriesAsync(), Times.Once); }
public async Task When_No_Recommendations_Are_Found_Return_Not_Found() { //arrange _length = 1; _age = 1; _type = CrossCountrySkiType.Classic; _crossCountrySkiService.Setup(s => s.GetRecommendationAsync(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CrossCountrySkiType>())) .Returns(Task.FromResult((CrossCountrySkiRecommendation)null)); //act var result = await _crossCountrySkiController.GetRecommendationAsync(_length, _age, _type); //assert Assert.IsInstanceOfType(result, typeof(NotFoundObjectResult)); _crossCountrySkiService.Verify(s => s.GetRecommendationAsync( It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CrossCountrySkiType>()), Times.Once); }
public SkiResponseDto CalculateLength(int height, int age, CrossCountrySkiType type) { int minCompetitionLength = 0; int minLength = 0; int maxLength = 0; if (height >= _maxAvalebleClassicLength - 20 && type == CrossCountrySkiType.Classic) { minLength = _maxAvalebleClassicLength; maxLength = _maxAvalebleClassicLength; } else if (age <= 4) { minLength = height; maxLength = height; } else if (age >= 5 && age <= 8) { minLength = height + 10; maxLength = height + 20; } else if (age > 8 && type == CrossCountrySkiType.Classic) { minLength = height + 20; maxLength = height + 20; } else if (age > 8 && type == CrossCountrySkiType.Skate) { minLength = height + 10; maxLength = height + 15; } if (type == CrossCountrySkiType.Skate) { minCompetitionLength = height - 10; } return(new SkiResponseDto { MinLength = minLength, MaxLength = maxLength, MinCompetitionLength = minCompetitionLength }); }
public IValidationResult ValidateRequest(int height, int age, CrossCountrySkiType type) { var validationResult = new ValidationResult(); if (height <= 0) { validationResult.AddError("Height cannot be 0 or below"); } if (age <= 0) { validationResult.AddError("Age cannot be 0 or below"); } if (!CrossCountrySkiType.IsDefined(typeof(CrossCountrySkiType), type)) { validationResult.AddError("Type is not set"); } return(validationResult); }
public async Task <CrossCountrySkiRecommendation> GetRecommendationAsync(int length, int age, CrossCountrySkiType type) { var category = await _crossCountrySkiCategoryService.GetCategoryAsync(age, type); if (category == null) { return(null); } var recommendedLength = length + category.AdditionalLength; var skiLength = category.MaxLength.HasValue ? (recommendedLength > category.MaxLength.Value ? category.MaxLength.Value : recommendedLength) : recommendedLength; return(new CrossCountrySkiRecommendation(skiLength, category.Info)); }
public async Task <IActionResult> GetRecommendationAsync([FromRoute] int length, [FromRoute] int age, [FromRoute] CrossCountrySkiType type) { if (!IsGreaterThanZero(length)) { return(BadRequest(NegativeNumberMsg(nameof(length)))); } else if (!IsGreaterThanZero(age)) { return(BadRequest(NegativeNumberMsg(nameof(length)))); } var result = await _crossCountrySkiService.GetRecommendationAsync(length, age, type); if (result == null) { return(NotFound("No recommendations found for given values")); } return(Ok(result)); }
public IHttpActionResult GetSkiLength([FromUri] int height, [FromUri] int age, [FromUri] CrossCountrySkiType type) { var validatonResult = _validator.ValidateRequest(height, age, type); if (validatonResult.Errors.Any()) { return(Content(HttpStatusCode.BadRequest, validatonResult)); } var dto = _calculator.CalculateLength(height, age, type); return(Ok(dto)); }