public void CanCreateNewGameInDb() { ////Set up HTTP configuration for test. HttpConfiguration httpConfig = GameControllerIntegrationTest.SetUpIntegrationConfiguration(); ////Create HTTP request. HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost:1166/api/games")); httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); ////Create game request model to send to Web API method. GameBaseRequestModel requestModel = new GameBaseRequestModel() { HomeTeamId = 2, VisitingTeamId = 1, FavoriteTeamId = 1, StartDateTime = new DateTime(2014, 12, 13), WeekNumber = 13, Line = 3.5f, OverUnder = 38.5f }; ////Add game request model to body of HTTP request message. httpRequestMessage.Content = new ObjectContent<GameBaseRequestModel>(requestModel, new JsonMediaTypeFormatter()); ////Send HTTP request message, and block current thread until result is returned. HttpResponseMessage responseMessage = GameControllerIntegrationTest.SendHttpRequestMessage(httpRequestMessage, httpConfig).Result; GameDto gameDto = responseMessage.Content.ReadAsAsync<GameDto>().Result; Assert.IsNotNull(gameDto); ////Verify game was created in the database. bool doesGameExistInPersistenceStore = DatabaseUtility.DoesGameExist(gameDto.Id, "FootballPoolDb"); Assert.IsTrue(doesGameExistInPersistenceStore); }
public HttpResponseMessage Put(int id, GameBaseRequestModel gameRequest, HttpRequestMessage requestMessage) { if (gameRequest == null) { throw new ArgumentNullException("gameRequest", "gameRequest cannot be null."); } if (requestMessage == null) { throw new ArgumentNullException("requestMessage", "requestMessage cannot be null."); } ////Get GameSnapshot instance from GameRequest instance. GameSnapshot game = this.gameTypeMapper.GetGameSnapshot(gameRequest); ////Update game in the persistence store. ServiceOperationResult<Game> operationResult = this.gameService.UpdateGame(id, game); if (operationResult.OperationResult == OperationResult.Failed) { ////TODO: Determine if all of this information should be returned to the caller. Once approach is finalized, update the POST method accordingly. Message has to be logged. return requestMessage.CreateErrorResponse(HttpStatusCode.NotFound, string.Format(CultureInfo.CurrentCulture, "Game could not be saved due to the following issues:\n{0}", operationResult.GetBrokenRulesSummary())); } ////Get GameDto instance from updated Game instance. GameDto updatedGameDto = this.gameTypeMapper.GetEntityDto(operationResult.Entity, GamesController.GetGameUri(requestMessage, operationResult.Entity.Id)); ////Return HTTP response with status code indicating Game resource was created along with resource itself. HttpResponseMessage response = requestMessage.CreateResponse(HttpStatusCode.OK, updatedGameDto); return response; }
public void CannotCreateNewGameInDbFavoredTeamNotInGame() { ////Set up HTTP configuration for test. HttpConfiguration httpConfig = GameControllerIntegrationTest.SetUpIntegrationConfiguration(); ////Create HTTP request. HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost:1166/api/games")); httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); ////Create game request model to send to Web API method. GameBaseRequestModel requestModel = new GameBaseRequestModel() { HomeTeamId = 2, VisitingTeamId = 1, ////favorite team not part of game FavoriteTeamId = 3, StartDateTime = new DateTime(2014, 12, 13), WeekNumber = 13, Line = 3.5f, OverUnder = 38.5f }; ////Add game request model to body of HTTP request message. httpRequestMessage.Content = new ObjectContent<GameBaseRequestModel>(requestModel, new JsonMediaTypeFormatter()); ////Send HTTP request message, and block current thread until result is returned. HttpResponseMessage responseMessage = GameControllerIntegrationTest.SendHttpRequestMessage(httpRequestMessage, httpConfig).Result; Assert.IsNotNull(responseMessage); Assert.AreEqual(HttpStatusCode.NotFound, responseMessage.StatusCode); ////Get http error from the web service. HttpError httpError = responseMessage.Content.ReadAsAsync<HttpError>().Result; this.TestContext.WriteLine(httpError.Message); }
public HttpResponseMessage Post(GameBaseRequestModel gameRequest, HttpRequestMessage requestMessage) { if (gameRequest == null) { throw new ArgumentNullException("gameDto", "gameDto cannot be null."); } if (requestMessage == null) { throw new ArgumentNullException("requestMessage", "requestMessage cannot be null."); } ////Get GameSnapshot instance from GameRequest instance. GameSnapshot game = this.gameTypeMapper.GetGameSnapshot(gameRequest); ////Save game in persistence store which generates new game instance in persistence store with new id. ServiceOperationResult<Game> operationResult = this.gameService.SaveGame(game); if (operationResult.OperationResult == OperationResult.Failed) { ////TODO: Send back message indicating operation failed and why....perhaps don't include all detail in request. Detail must be logged however. return requestMessage.CreateErrorResponse(HttpStatusCode.NotFound, string.Format(CultureInfo.CurrentCulture, "Game could not be created due to the following issues:\n{0}", operationResult.GetBrokenRulesSummary())); } ////Get GameDto instance from Game instance. GameDto savedGameDto = this.gameTypeMapper.GetEntityDto(operationResult.Entity, GamesController.GetGameUri(requestMessage, operationResult.Entity.Id)); ////Return HTTP response with status code indicating Game resource was created along with resource itself. HttpResponseMessage response = requestMessage.CreateResponse(HttpStatusCode.Created, savedGameDto); response.Headers.Add("Location", savedGameDto.HypermediaLinks.Single(link => link.Rel == "self").Href); return response; }