public void Update_ShouldUpdateConstructionElement()
        {
            // Arrange
            int   id            = _rnd.Next(1, _updateConstructionElements.Count());
            int   profileId     = _rnd.Next(1, _profiles.Count());
            short steelId       = (Int16)(_rnd.Next(1, _steel.Count()));
            var   newFloatValue = 9.0f;

            var newConstructionElementRequest = new ConstructionElementUpdateRequest
            {
                ProfileId = profileId,
                SteelId   = steelId,
                Length    = newFloatValue,
            };

            // Act
            _updateService.Update(id, newConstructionElementRequest);

            // Assert
            _updateRepository.Verify(mock => mock.Update(It.IsAny <ConstructionElement>()), Times.Once);
            Assert.Equal(_profiles.SingleOrDefault(v => v.Id == profileId), _updateConstructionElements.SingleOrDefault(
                             v => v.Id == id).Profile);
            Assert.Equal(_steel.SingleOrDefault(v => v.Id == steelId), _updateConstructionElements.SingleOrDefault(
                             v => v.Id == id).Steel);
            Assert.Equal(newFloatValue, _updateConstructionElements.SingleOrDefault(
                             v => v.Id == id).Length);
        }
Exemple #2
0
        public void Update(int id, ConstructionElementUpdateRequest constructionElementRequest)
        {
            if (constructionElementRequest == null)
            {
                throw new ArgumentNullException(nameof(constructionElementRequest));
            }
            var foundConstructionElement = _repository.GetById(id);

            if (foundConstructionElement == null)
            {
                throw new ArgumentNullException(nameof(foundConstructionElement));
            }

            // var uniqueConstraintViolationCheck = _repository.GetByMarkIdAndLinkedDocId(
            //     foundConstructionElement.Mark.Id, ConstructionElementRequest.LinkedDocId);
            // if (uniqueConstraintViolationCheck != null)
            //     throw new ConflictException(nameof(uniqueConstraintViolationCheck));

            if (constructionElementRequest.ProfileId != null)
            {
                var profile = _profileRepo.GetById(
                    constructionElementRequest.ProfileId.GetValueOrDefault());
                if (profile == null)
                {
                    throw new ArgumentNullException(nameof(profile));
                }
                foundConstructionElement.Profile = profile;
            }
            if (constructionElementRequest.SteelId != null)
            {
                var steel = _steelRepo.GetById(
                    constructionElementRequest.SteelId.GetValueOrDefault());
                if (steel == null)
                {
                    throw new ArgumentNullException(nameof(steel));
                }
                foundConstructionElement.Steel = steel;
            }
            if (constructionElementRequest.Length != null)
            {
                foundConstructionElement.Length = constructionElementRequest.Length.GetValueOrDefault();
            }

            _repository.Update(foundConstructionElement);

            var foundMark = _markRepo.GetById(foundConstructionElement.Construction.Specification.Mark.Id);

            foundMark.EditedDate = DateTime.Now;
            _markRepo.Update(foundMark);
        }
        public async Task Update_ShouldReturnUnauthorized_WhenNoAccessToken()
        {
            // Arrange
            int id = 1;
            var constructionElementRequest = new ConstructionElementUpdateRequest
            {
                Length = 9,
            };
            string json        = JsonSerializer.Serialize(constructionElementRequest);
            var    httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var    endpoint    = $"/api/construction-elements/{id}";

            // Act
            var response = await _authHttpClient.PatchAsync(endpoint, httpContent);

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
        }
        public async Task Update_ShouldReturnBadRequest_WhenWrongValues()
        {
            // Arrange
            int id = 1;
            var wrongConstructionElementRequest = new ConstructionElementUpdateRequest
            {
                Length = -9.0f,
            };
            var json        = JsonSerializer.Serialize(wrongConstructionElementRequest);
            var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var endpoint    = $"/api/construction-elements/{id}";

            // Act
            var response = await _httpClient.PatchAsync(endpoint, httpContent);

            // Assert
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public void Update_ShouldFailWithNull_WhenWrongValues()
        {
            // Arrange
            int id = _rnd.Next(1, _updateConstructionElements.Count());

            var newConstructionElementRequest = new ConstructionElementUpdateRequest
            {
                Length = 9,
            };

            // Act & Assert
            Assert.Throws <ArgumentNullException>(() => _updateService.Update(id, null));
            Assert.Throws <ArgumentNullException>(() => _updateService.Update(
                                                      999, newConstructionElementRequest));

            _updateRepository.Verify(mock => mock.Update(
                                         It.IsAny <ConstructionElement>()), Times.Never);
        }
 public ActionResult Update(
     int id, [FromBody] ConstructionElementUpdateRequest constructionElementRequest)
 {
     if (!constructionElementRequest.Validate())
     {
         return(BadRequest());
     }
     try
     {
         _service.Update(id, constructionElementRequest);
     }
     catch (ArgumentNullException)
     {
         return(NotFound());
     }
     catch (ConflictException)
     {
         return(Conflict());
     }
     return(NoContent());
 }
        public async Task Update_ShouldReturnNoContent()
        {
            // Arrange
            int   id        = 1;
            int   profileId = 2;
            short steelId   = 2;
            var   constructionElementRequest = new ConstructionElementUpdateRequest
            {
                ProfileId = profileId,
                SteelId   = steelId,
                Length    = 9,
            };
            string json        = JsonSerializer.Serialize(constructionElementRequest);
            var    httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var    endpoint    = $"/api/construction-elements/{id}";

            // Act
            var response = await _httpClient.PatchAsync(endpoint, httpContent);

            // Assert
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
        }