Ejemplo n.º 1
0
        public void GetNonExistingMatch()
        {
            // arrange
            using (var context = new GameStatsDbDataContext())
            {
                context.ExecuteCommand("DELETE FROM Servers");
                context.ExecuteCommand("DELETE FROM Matches");
            }

            ApiResponse putServerResponse = new ServerController().PutInfo(staticServerEndpoint, staticServerInfoJson);

            Assert.AreEqual(HttpStatusCode.OK, putServerResponse.Status);

            // act
            // get stats from server having no matches played on it
            ApiResponse getServerStats = new ServerController().GetStats(staticServerEndpoint);

            Assert.AreEqual(HttpStatusCode.OK, getServerStats.Status);

            ServerController.ServerStatsJson expectedStats = ServerController.ServerStatsJson.Trivial();
            ServerController.ServerStatsJson actualStats   =
                JsonConvert.DeserializeObject <ServerController.ServerStatsJson>(getServerStats.Body);
            // assert
            Assert.AreEqual(expectedStats, actualStats);

            // act
            // get match from non-existing server
            ApiResponse getMatchInfo = new MatchController().GetMatch("non-existing-endpoint", staticTimestamp);

            // assert
            Assert.AreEqual(HttpStatusCode.NotFound, getMatchInfo.Status);

            // act
            // get non-existing match from existing server
            ApiResponse getMatchInfoFromExistingServer =
                new MatchController().GetMatch(staticServerEndpoint, staticTimestamp);

            // assert
            Assert.AreEqual(HttpStatusCode.NotFound, getMatchInfoFromExistingServer.Status);


            // act
            // get recent matches
            ApiResponse getRecentMatches      = new MatchController().GetRecentMatches(10);
            var         expectedRecentMatches = new List <MatchController.RecentMatchJson>();
            var         actualRecentMatches   =
                JsonConvert.DeserializeObject <List <MatchController.RecentMatchJson> >(getRecentMatches.Body);

            Assert.AreEqual(HttpStatusCode.OK, getRecentMatches.Status);
            CollectionAssert.AreEqual(expectedRecentMatches, actualRecentMatches);
        }
Ejemplo n.º 2
0
        public void GetServerStats()
        {
            // arrange
            using (var context = new GameStatsDbDataContext())
            {
                context.ExecuteCommand("DELETE FROM Servers");
                context.ExecuteCommand("DELETE FROM Matches");
            }
            ServerController.ServerInfoJson server = new ServerController.ServerInfoJson
            {
                Name      = "test_server",
                GameModes = new List <string> {
                    "DM", "TDM"
                }
            };
            string serverJson = JsonConvert.SerializeObject(server);

            MatchController.MatchJson match1 = new MatchController.MatchJson
            {
                FragLimit   = 20,
                GameMode    = "DM",
                Map         = "test_map",
                TimeElapsed = 10,
                TimeLimit   = 20,
                Scoreboard  = new List <MatchController.ScoreboardRecordJson>
                {
                    new MatchController.ScoreboardRecordJson
                    {
                        Deaths = 5,
                        Frags  = 5,
                        Kills  = 5,
                        Name   = "test_player_1"
                    },
                    new MatchController.ScoreboardRecordJson
                    {
                        Deaths = 5,
                        Frags  = 5,
                        Kills  = 5,
                        Name   = "test_player_2"
                    },
                    new MatchController.ScoreboardRecordJson
                    {
                        Deaths = 5,
                        Frags  = 5,
                        Kills  = 5,
                        Name   = "test_player_3"
                    },
                    new MatchController.ScoreboardRecordJson
                    {
                        Deaths = 5,
                        Frags  = 5,
                        Kills  = 5,
                        Name   = "test_player_4"
                    }
                }
            };
            string match1json = JsonConvert.SerializeObject(match1);

            MatchController.MatchJson match2 = new MatchController.MatchJson
            {
                FragLimit   = 20,
                GameMode    = "DM",
                Map         = "test_map",
                TimeElapsed = 10,
                TimeLimit   = 20,
                Scoreboard  = new List <MatchController.ScoreboardRecordJson>
                {
                    new MatchController.ScoreboardRecordJson
                    {
                        Deaths = 5,
                        Frags  = 5,
                        Kills  = 5,
                        Name   = "test_player_1"
                    },
                    new MatchController.ScoreboardRecordJson
                    {
                        Deaths = 5,
                        Frags  = 5,
                        Kills  = 5,
                        Name   = "test_player_2"
                    }
                }
            };
            string   match2json = JsonConvert.SerializeObject(match2);
            DateTime timestamp1 = Convert.ToDateTime("2017-01-22T15:11:12Z");
            DateTime timestamp2 = timestamp1.AddHours(1);

            // expected answer
            var expectedStats = new ServerController.ServerStatsJson
            {
                AverageMatchesPerDay = 2, // match1 and match2 played in 1 day
                AveragePopulation    = (match1.Scoreboard.Count + match2.Scoreboard.Count) / 2.0,
                MaximumMatchesPerDay = 2,
                MaximumPopulation    = Math.Max(match1.Scoreboard.Count, match2.Scoreboard.Count),
                Top5GameModes        = new List <string> {
                    "DM"
                },
                Top5Maps = new List <string> {
                    "test_map"
                },
                TotalMatchesPlayed = 2
            };

            // act
            ApiResponse putServerResponse = new ServerController().PutInfo("test-server.com-1337", serverJson);
            ApiResponse putMatch1Response = new MatchController().PutMatch("test-server.com-1337", match1json, timestamp1);
            ApiResponse putMatch2Response = new MatchController().PutMatch("test-server.com-1337", match2json, timestamp2);
            ApiResponse getStatsResponse  = new ServerController().GetStats("test-server.com-1337");

            var actualStats = JsonConvert.DeserializeObject <ServerController.ServerStatsJson>(getStatsResponse.Body);

            // assert
            Assert.AreEqual(HttpStatusCode.OK, putServerResponse.Status);
            Assert.AreEqual(HttpStatusCode.OK, putMatch1Response.Status);
            Assert.AreEqual(HttpStatusCode.OK, putMatch2Response.Status);
            Assert.AreEqual(HttpStatusCode.OK, getStatsResponse.Status);
            Assert.AreEqual(expectedStats, actualStats);
        }