示例#1
0
        public IActionResult UpdateReferenceRace(int id, ReferenceRaceDto payload)
        {
            var referenceRaceInDb = _unitOfWork.ReferenceRaces.Get(id);

            if (referenceRaceInDb == null)
            {
                return(NotFound());
            }

            _mapper.Map(payload, referenceRaceInDb);
            _unitOfWork.Complete();

            return(NoContent());
        }
示例#2
0
        public async Task Update_MalformedPayload_HttpBadRequest()
        {
            // Arrange
            var randomRaceId    = PreDefinedData.GetRandomRaceId();
            var path            = GetRelativePath(nameof(ReferenceRacesController), randomRaceId.ToString());
            var apiUpdatingRace = new ReferenceRaceDto
            {
                Id = randomRaceId
                     // Code is required, keep it missing
            };

            // Act
            var response = await Client.PutAsync(path, new StringContent(
                                                     JsonConvert.SerializeObject(apiUpdatingRace),
                                                     Encoding.UTF8,
                                                     JsonMediaType));

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
        }
示例#3
0
        public async Task Create_MalformedPayload_HttpBadRequest()
        {
            // Arrange
            var path = GetRelativePath(nameof(ReferenceRacesController));

            var newRaceDto = new ReferenceRaceDto
            {
                // Code is required, keep it missing
                CreatedDate = DateTime.UtcNow
            };

            // Act
            var response = await Client.PostAsync(path, new StringContent(
                                                      JsonConvert.SerializeObject(newRaceDto),
                                                      Encoding.UTF8,
                                                      JsonMediaType));

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
        }
示例#4
0
        public async Task Update_InvalidRace_HttpNotFound()
        {
            // Arrange
            var notExistsRaceCode = PreDefinedData.GetNotExistsRaceCode();
            var path            = GetRelativePath(nameof(ReferenceRacesController), int.MaxValue.ToString());
            var apiUpdatingRace = new ReferenceRaceDto
            {
                Id       = int.MaxValue,
                Code     = notExistsRaceCode,
                LongName = "Update Test"
            };

            // Act
            var response = await Client.PutAsync(path, new StringContent(
                                                     JsonConvert.SerializeObject(apiUpdatingRace),
                                                     Encoding.UTF8,
                                                     JsonMediaType));

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
        }
示例#5
0
        public async Task Create_ExistingRace_HttpConflict()
        {
            // Arrange
            var path         = GetRelativePath(nameof(ReferenceRacesController));
            var randomRaceId = PreDefinedData.GetRandomRaceId();
            var randomRace   = PreDefinedData.ReferenceRaces[randomRaceId - 1];

            var newRaceDto = new ReferenceRaceDto
            {
                Code        = randomRace.Code,
                LongName    = "Create Test",
                CreatedDate = DateTime.UtcNow
            };

            // Act
            var response = await Client.PostAsync(path, new StringContent(
                                                      JsonConvert.SerializeObject(newRaceDto),
                                                      Encoding.UTF8,
                                                      JsonMediaType));

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Conflict));
        }
示例#6
0
        public IActionResult CreateReferenceRace(ReferenceRaceDto payload)
        {
            var referenceRace = _mapper.Map <ReferenceRaceDto, ReferenceRace>(payload);

            var referenceRaceInDb = _unitOfWork.ReferenceRaces.Find(p => p.Code == payload.Code);

            if (referenceRaceInDb.Any())
            {
                return(new ConflictResult());
            }

            _unitOfWork.ReferenceRaces.Add(referenceRace);
            _unitOfWork.Complete();

            payload.Id = referenceRace.Id;

            return(CreatedAtAction(nameof(GetReferenceRace),
                                   new
            {
                id = payload.Id
            },
                                   payload));
        }
示例#7
0
        public async Task Create_ValidPayload_HttpCreatedAndReturnsNewRace()
        {
            // Arrange
            var path = GetRelativePath(nameof(ReferenceRacesController));
            var notExistsRaceCode = PreDefinedData.GetNotExistsRaceCode();
            var newRaceDto        = new ReferenceRaceDto
            {
                Code         = notExistsRaceCode,
                LongName     = "New Race",
                CreatedDate  = DateTime.UtcNow,
                CreatedBy    = CreatedModifiedBy,
                ModifiedDate = DateTime.UtcNow,
                ModifiedBy   = CreatedModifiedBy
            };

            // Act
            var response = await Client.PostAsync(path, new StringContent(
                                                      JsonConvert.SerializeObject(newRaceDto),
                                                      Encoding.UTF8,
                                                      JsonMediaType));

            var responseString = await response.Content.ReadAsStringAsync();

            // Assert
            Assert.DoesNotThrow(
                () => response.EnsureSuccessStatusCode(),
                string.Format(HttpExceptionFormattedMessage, responseString));
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Created));

            var apiReturnedObject =
                JsonConvert.DeserializeObject <ReferenceRaceDto>(responseString);

            Assert.That(apiReturnedObject.Id, Is.GreaterThan(0));

            newRaceDto.Id = apiReturnedObject.Id;
            AssertHelper.AreObjectsEqual(apiReturnedObject, newRaceDto);
        }