Ejemplo n.º 1
0
        public async Task AddLapResult_NotRegisteredError()
        {
            var lapResult = new RequestObject.LapResult
            {
                ipAddress = "10.1.1.1",
                lapTime   = "0:0:1:12"
            };
            var jsonString = JsonSerializer.Serialize(lapResult);
            var lapContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(prefix + "/AddLapResultAsString", lapContent);

            response.EnsureSuccessStatusCode();
            var responseObject = JsonSerializer.Deserialize <ResponseObject>(
                await response.Content.ReadAsStringAsync());

            Assert.Equal("The given IP address '10.1.1.1' is not registered.", responseObject.responseMessage);
        }
Ejemplo n.º 2
0
        public async Task GetLastRaceResultById_IdNotFound()
        {
            string ip       = "1.1.1.1";
            var    response = await _httpClient.GetAsync(prefix + "/Register/" + ip);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/SetRaceStartCountdownDuration/" + 0);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/StartRace");

            response.EnsureSuccessStatusCode();

            // this test is dependent on the default number of laps being 10 (haven't implement a way to change it yet)
            for (int lap = 1; lap <= 10; lap++)
            {
                var lapResult = new RequestObject.LapResult
                {
                    ipAddress = ip,
                    lapTime   = "0:0:1:0"
                };

                var jsonString = JsonSerializer.Serialize(lapResult);
                var lapContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
                await _httpClient.PostAsync(prefix + "/AddLapResultAsString", lapContent);
            }

            response = await _httpClient.GetAsync(prefix + "/GetLastRaceResultById/" + 4356);

            response.EnsureSuccessStatusCode();
            var raceResult = JsonSerializer.Deserialize <ResponseObject.RaceResultByID>(
                await response.Content.ReadAsStringAsync());

            Assert.Equal(4356, raceResult.id);
            Assert.Equal("ID [4356] was not found.", raceResult.responseMessage);
        }
Ejemplo n.º 3
0
        public async Task AddLapResult_Success()
        {
            string ipAddress = "10.1.1.1";

            var registerResponse = await _httpClient.GetAsync(prefix + "/Register/" + ipAddress);

            registerResponse.EnsureSuccessStatusCode();

            var lapResult = new RequestObject.LapResult
            {
                ipAddress = ipAddress,
                lapTime   = "0:0:1:12"
            };
            var jsonString = JsonSerializer.Serialize(lapResult);
            var lapContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(prefix + "/AddLapResultAsString", lapContent);

            var responseObject = JsonSerializer.Deserialize <ResponseObject>(
                await response.Content.ReadAsStringAsync());

            Assert.Equal("success", responseObject.responseMessage);

            var lapResultMillis = new RequestObject.LapResultMilliseconds
            {
                ipAddress = ipAddress,
                lapTime   = 123456
            };
            var jsonString2 = JsonSerializer.Serialize(lapResultMillis);
            var lapContent2 = new StringContent(jsonString2, Encoding.UTF8, "application/json");

            var response2 = await _httpClient.PostAsync(prefix + "/AddLapResultInMilliseconds", lapContent2);

            var responseObject2 = JsonSerializer.Deserialize <ResponseObject>(
                await response2.Content.ReadAsStringAsync());

            Assert.Equal("success", responseObject2.responseMessage);
        }
Ejemplo n.º 4
0
        public JsonResult AddLapResultAsString([FromBody] RequestObject.LapResult lapResult)
        {
            _logger.LogInformation("POST /AddLapResultAsString \n\tBODY: ipAddress:" + lapResult.ipAddress + " | lapTime: " + lapResult.lapTime);

            try
            {
                IPAddress ipAddress = IPAddress.Parse(lapResult.ipAddress);
                TimeSpan  lapTime   = TimeSpan.Parse(lapResult.lapTime, CultureInfo.InvariantCulture);
                _raceManager.AddLapResult(ipAddress, lapTime);
                return(Json(new ResponseObject {
                    responseMessage = "success"
                }));
            }
            catch (Exception error)
            {
                _logger.LogError("RaceTimer/AddLapResult Exception: " + error.Message);

                if (error is KeyNotFoundException)
                {
                    return(Json(new ResponseObject
                    {
                        responseMessage = "The given IP address '" + lapResult.ipAddress + "' is not registered."
                    }));
                }

                ResponseObject response = new ResponseObject
                {
                    responseMessage = error.Message,
                };

                return(new JsonResult(response)
                {
                    StatusCode = (int)HttpStatusCode.BadRequest
                });
            }
        }
Ejemplo n.º 5
0
        public async Task GetCurrentRaceResults_MultipleRaces()
        {
            string ip1      = "1.1.1.1";
            string ip2      = "2.2.2.2";
            var    response = await _httpClient.GetAsync(prefix + "/SetMaxParticipants/" + 2);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/Register/" + ip1);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/Register/" + ip2);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/SetRaceStartCountdownDuration/" + 0);

            response.EnsureSuccessStatusCode();

            response = await _httpClient.GetAsync(prefix + "/StartRace");

            response.EnsureSuccessStatusCode();

            // this test is dependent on the default number of laps being 10 (haven't implement a way to change it yet)
            for (int lap = 1; lap <= 10; lap++)
            {
                var lapResult = new RequestObject.LapResult
                {
                    lapTime = "0:0:1:" + lap
                };

                foreach (string ipAddress in new List <string> {
                    ip2, ip1
                })
                {
                    lapResult.ipAddress = ipAddress;
                    var jsonString = JsonSerializer.Serialize(lapResult);
                    var lapContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    await _httpClient.PostAsync(prefix + "/AddLapResultAsString", lapContent);
                }
            }

            response = await _httpClient.GetAsync(prefix + "/GetCurrentRaceResults");

            response.EnsureSuccessStatusCode();
            var stringContent = await response.Content.ReadAsStringAsync();

            var raceResults = JsonSerializer.Deserialize <ResponseObject.Race>(stringContent);

            // duration is the only element that isn't deserialized correctly
            var    splitString        = stringContent.Split("totalMilliseconds");
            string millisecondsString = splitString[1]
                                        .Split("totalMinutes")[0]
                                        .Split(":")[1]
                                        .Split(",")[0];
            double durationMilliseconds = double.Parse(millisecondsString);

            Assert.Equal("Finished", raceResults.raceState);
            Assert.Equal(10, raceResults.numberOfLaps);
            Assert.True(new DateTime() < raceResults.startTime, "start time should be greater than 0");
            Assert.True(raceResults.startTime < raceResults.finishTime, "finish time should be greater than start time");
            Assert.True(durationMilliseconds > 0, "duration should be greater than 0");
            Assert.Equal(2, raceResults.finishOrder[0]);
            Assert.Equal(1, raceResults.finishOrder[1]);

            for (int i = 1; i < 3; i++)
            {
                Assert.Equal(i, raceResults.lapResults[i - 1].timerID);

                for (int lap = 1; lap <= 10; lap++)
                {
                    Assert.Contains(lap + ":", raceResults.lapResults[i - 1].laps[lap - 1]);
                    if (lap < 10)
                    {
                        Assert.Contains("00:01:0" + lap, raceResults.lapResults[i - 1].laps[lap - 1]);
                    }
                    else
                    {
                        Assert.Contains("00:01:" + lap, raceResults.lapResults[i - 1].laps[lap - 1]);
                    }
                }
            }

            Assert.Equal("success", raceResults.responseMessage);
        }