public void TestCanHandleErronous11thFrameIssue_strike() { // arrange TraditionalBowlingScoringCalculator traditionalBowlingScoringCalculator = new TraditionalBowlingScoringCalculator(); TraditionalBowlingScoreCalculatorValidator traditionalBowlingScoreCalculatorValidator = new TraditionalBowlingScoreCalculatorValidator(traditionalBowlingScoringCalculator); TestGameDataDto testGameDataDto = new TestGameDataDto(); int[][] testGamePoints = new int[11][]; for (int i = 0; i <= 10; i++) { testGamePoints[i] = new int[] { 10, 0 } } ; testGamePoints[10] = new int[] { 10, 10 }; // simulates the 11th frame issue testGameDataDto.points = testGamePoints; // act traditionalBowlingScoreCalculatorValidator.HandleErronous11thFrameIssue(testGameDataDto); //assert Assert.IsTrue(testGameDataDto.points.Length == 10); Assert.AreEqual(testGameDataDto.points[9].Length, 3); Assert.IsTrue(testGameDataDto.points[9].All(point => point == 10)); }
/// <summary> /// Gets bowling test-data from a REST api. /// </summary> /// <remarks> /// Also retrieved from the rest api is an api-token, that we save for when we'll validate the calculated scores against the same service. /// </remarks> internal TestGameDataDto GetPlayedTestFramesFromRestApi(out string apiToken) { try { string url = ConfigurationManager.AppSettings["BowlingScoreCalculatorValidationServiceUrl"]; HttpClient httpClient = new HttpClient(); HttpResponseMessage response = httpClient.GetAsync(url).Result; if (!response.IsSuccessStatusCode) { throw new WebException("The REST api used for retrieving test-data didn't respond in an orderly fashion"); } TestGameDataDto parsedServiceData = Newtonsoft.Json.JsonConvert.DeserializeObject <TestGameDataDto>(response.Content.ReadAsStringAsync().Result); HandleErronous11thFrameIssue(parsedServiceData); apiToken = parsedServiceData.token; return(parsedServiceData); } catch (Newtonsoft.Json.JsonException) { throw new WebException("The test-data retrived from the REST api could not be converted into something meaningful."); } catch (Exception) { throw new WebException("The REST api used for retrieving test-data didn't respond in an orderly fashion"); } }
/// <summary> /// The REST api returns an extra 11th frame in case of a strike or spare in the 10th. That's not as per the traditonal scoring rule, /// so we'll need make up for that. /// </summary> internal void HandleErronous11thFrameIssue(TestGameDataDto parsedServiceData) { if (parsedServiceData.points.Length > 10 && parsedServiceData.points[9].Sum() == 10) { // combine 10th and 11th frame and remove the superfluous frame. if (parsedServiceData.points[9][0] == 10 /* strike */) { parsedServiceData.points[9] = new int[] { 10, parsedServiceData.points[10][0], parsedServiceData.points[10][1] }; } else /* spare */ { parsedServiceData.points[9] = new int[] { parsedServiceData.points[9][0], parsedServiceData.points[9][1], parsedServiceData.points[10][0] }; } Array.Resize(ref parsedServiceData.points, 10); } }
internal IList <BowlingFrame> ConvertTestDataToBowlingFrames(TestGameDataDto playedFramesPocos) { IList <BowlingFrame> playedFrames = new List <BowlingFrame>(); for (int i = 0; i < playedFramesPocos.points.Length; i++) { BowlingFrame playedFrame = BowlingFrame.CreateAndValidateFrameFromPoints(playedFramesPocos.points[i]); playedFrames.Add(playedFrame); } return(playedFrames); }
public void TestCanGetBowlingTestDataFromRestApi() { // arrange TraditionalBowlingScoringCalculator traditionalBowlingScoringCalculator = new TraditionalBowlingScoringCalculator(); TraditionalBowlingScoreCalculatorValidator traditionalBowlingScoreCalculatorValidator = new TraditionalBowlingScoreCalculatorValidator(traditionalBowlingScoringCalculator); // act string retrievedApiToken; TestGameDataDto testData = traditionalBowlingScoreCalculatorValidator.GetPlayedTestFramesFromRestApi(out retrievedApiToken); // assert Assert.IsTrue(testData.points.Length > 0); Assert.IsNotNull(retrievedApiToken); }
public BowlingScoreValidatorResultEnum ValidateBowlingScoreRules() { try { TestGameDataDto playedFramesTestData = GetPlayedTestFramesFromRestApi(out _apiValidationToken); IList <BowlingFrame> playedBowlingFrames = ConvertTestDataToBowlingFrames(playedFramesTestData); CalculatedBowlingScoresDto calculatedBowlingGameScoreDto = ConstructScoresToValidate(playedBowlingFrames, _scoreCalculator); BowlingScoreValidatorResultEnum validationResult = ValidateCalculatedGameScoreAgainstApi(calculatedBowlingGameScoreDto); return(validationResult); } catch (WebException) { return(BowlingScoreValidatorResultEnum.ScoreCalculatorValidationServiceInaccesible); } catch (Exception) { throw; } }