Esempio n. 1
0
        public IActionResult UpdateReferenceHairColor(int id, ReferenceHairColorDto payload)
        {
            var referenceHairColorInDb = _unitOfWork.ReferenceHairColors.Get(id);

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

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

            return(NoContent());
        }
Esempio n. 2
0
        public async Task Update_MalformedPayload_HttpBadRequest()
        {
            // Arrange
            var randomHairColorId = PreDefinedData.GetRandomHairColorId();
            var path = GetRelativePath(nameof(ReferenceHairColorsController), randomHairColorId.ToString());
            var apiUpdatingHairColor = new ReferenceHairColorDto
            {
                Id = randomHairColorId
                     // Code is required, keep it missing
            };

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

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

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

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

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
        }
Esempio n. 4
0
        public async Task Update_InvalidHairColor_HttpNotFound()
        {
            // Arrange
            var notExistsHairColorCode = PreDefinedData.GetNotExistsHairColorCode();
            var path = GetRelativePath(nameof(ReferenceHairColorsController), int.MaxValue.ToString());
            var apiUpdatingHairColor = new ReferenceHairColorDto
            {
                Id       = int.MaxValue,
                Code     = notExistsHairColorCode,
                LongName = "Update"
            };

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

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
        }
Esempio n. 5
0
        public IActionResult CreateReferenceHairColor(ReferenceHairColorDto payload)
        {
            var referenceHairColor = _mapper.Map <ReferenceHairColorDto, ReferenceHairColor>(payload);

            var referenceHairColorInDb = _unitOfWork.ReferenceHairColors.Find(p => p.Code == payload.Code);

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

            _unitOfWork.ReferenceHairColors.Add(referenceHairColor);
            _unitOfWork.Complete();

            payload.Id = referenceHairColor.Id;

            return(CreatedAtAction(nameof(GetReferenceHairColor),
                                   new
            {
                id = payload.Id
            },
                                   payload));
        }
Esempio n. 6
0
        public async Task Create_ExistingHairColor_HttpConflict()
        {
            // Arrange
            var path = GetRelativePath(nameof(ReferenceHairColorsController));
            var randomHairColorId = PreDefinedData.GetRandomHairColorId();
            var randomHairColor   = PreDefinedData.ReferenceHairColors[randomHairColorId - 1];

            var newHairColorDto = new ReferenceHairColorDto
            {
                Code        = randomHairColor.Code,
                LongName    = "Create",
                CreatedDate = DateTime.UtcNow
            };

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

            // Assert
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Conflict));
        }
Esempio n. 7
0
        public async Task Create_ValidPayload_HttpCreatedAndReturnsNewHairColor()
        {
            // Arrange
            var path = GetRelativePath(nameof(ReferenceHairColorsController));
            var notExistsHairColorCode = PreDefinedData.GetNotExistsHairColorCode();
            var newHairColorDto        = new ReferenceHairColorDto
            {
                Code         = notExistsHairColorCode,
                LongName     = "New",
                CreatedDate  = DateTime.UtcNow,
                CreatedBy    = CreatedModifiedBy,
                ModifiedDate = DateTime.UtcNow,
                ModifiedBy   = CreatedModifiedBy
            };

            // Act
            var response = await Client.PostAsync(path, new StringContent(
                                                      JsonConvert.SerializeObject(newHairColorDto),
                                                      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 <ReferenceHairColorDto>(responseString);

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

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