示例#1
0
        public void TestUpdateLeagueSuccess()
        {
            var league = new League
            {
                Type = "Soccer",
                Name = "West Chester coed over 30",
                Address = new Address
                {
                    street = "400 Boot Rd",
                    city = "Downingtown",
                    state = "PA",
                    zip = "19053"
                },
                Image = "http://editor.swagger.io/photos"
            };

            var databaseFactory = new TestDatabaseFactory();
            var leagueController = new LeaguesController(databaseFactory);
            SetupControllerForTests(leagueController);

            // Add new league
            var response = leagueController.PostLeague(league);
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created), "The expected status code is 201 created");

            // Get the newly created league
            var leagues = leagueController.Get(league.Type, league.Name);
            Assert.That(leagues.Count, Is.EqualTo(1), "The number of leagues shoud be one");
            var createdLeague = leagues[0];

            // Modify league name
            createdLeague.Name = createdLeague.Name + "_Updated";

            // Update league
            var ret = leagueController.PutLeague(createdLeague.Id, createdLeague);

            // Get updated league
            leagues = leagueController.Get(league.Type, league.Name);
            Assert.That(leagues.Count, Is.EqualTo(1), "The number of leagues should be one");
            var modifiedLeague = leagues[0];

            Assert.That(modifiedLeague.Id, Is.EqualTo(createdLeague.Id), "The league Id should remain the same");
            Assert.That(modifiedLeague.Name, Is.EqualTo(createdLeague.Name), "The league name should remain the same");
        }
示例#2
0
        public void TestCreateLeague(League league, HttpStatusCode statusCode)
        {
            var databaseFactory = new TestDatabaseFactory();
            var leagueController = new LeaguesController(databaseFactory);
            SetupControllerForTests(leagueController);

            HttpResponseMessage response;

            response = leagueController.PostLeague(league);

            Assert.That(response.StatusCode, Is.EqualTo(statusCode), "The expected status code is " + statusCode);
        }
示例#3
0
        public void TestCreateLeagueDuplicateData()
        {
            var league = new League
            {
                Type = "Soccer",
                Name = "West Chester coed over 30",
                Address = new Address
                {
                    street = "400 Boot Rd",
                    city = "Downingtown",
                    state = "PA",
                    zip = "19053"
                },
                Image = "http://editor.swagger.io/photos"
            };

            var databaseFactory = new TestDatabaseFactory();
            var leagueController = new LeaguesController(databaseFactory);
            SetupControllerForTests(leagueController);

            HttpResponseMessage response;
            response = leagueController.PostLeague(league);
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created), "The expected status code is 201 created");

            //Try to add duplicate entry
            response = leagueController.PostLeague(league);
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Conflict), "The expected status code is 409 conflict");
            Assert.That(response.Headers.Contains("X-Status-Reason"), Is.True, "The expected response should have \"X-Status-Reason\" header");

            IEnumerable<string> values;
            response.Headers.TryGetValues("X-Status-Reason", out values);
            var enumerator = values.GetEnumerator();
            Assert.That(enumerator.MoveNext(), Is.True, "X-Status-Reason header in the response should have at least one value");
            Assert.That(enumerator.Current, Is.EqualTo(string.Format("The league with the name {0} already exists in the database", league.Name)), "unexpected response in header");
        }
示例#4
0
        /// <summary>
        /// Create League implementation
        /// </summary>
        /// <param name="league"></param>
        /// <returns></returns>
        public HttpResponseMessage PostLeague(League league)
        {
            HttpResponseMessage response;
            try
            {
                league = databasePlaceholder.Add(league);
            }
            catch(ArgumentNullException)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, league);
            }
            catch(DuplicateEntryException ex)
            {
                response = Request.CreateResponse<League>(HttpStatusCode.Conflict, league);
                response.Headers.Add("X-Status-Reason", ex.Message);
                return response;
            }

            string apiName = WebApiConfig.DEFAULT_ROUTE_NAME;
            response =
                Request.CreateResponse(HttpStatusCode.Created, league);
            string uri = Url.Link(apiName, new { id = league.Id, controller = "leagues" });
            response.Headers.Location = new Uri(uri);
            return response;
        }