public void Update_ShouldUpdateConstructionBolt()
        {
            // Arrange
            int id        = _rnd.Next(1, _updateConstructionBolts.Count());
            var newNumber = 6;

            var newConstructionBoltRequest = new ConstructionBoltUpdateRequest
            {
                Packet    = newNumber,
                Num       = newNumber,
                NutNum    = newNumber,
                WasherNum = newNumber,
            };

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

            // Assert
            _updateRepository.Verify(mock => mock.Update(It.IsAny <ConstructionBolt>()), Times.Once);
            Assert.Equal(newNumber, _updateConstructionBolts.SingleOrDefault(
                             v => v.Id == id).Packet);
            Assert.Equal(newNumber, _updateConstructionBolts.SingleOrDefault(
                             v => v.Id == id).Num);
            Assert.Equal(newNumber, _updateConstructionBolts.SingleOrDefault(
                             v => v.Id == id).NutNum);
            Assert.Equal(newNumber, _updateConstructionBolts.SingleOrDefault(
                             v => v.Id == id).WasherNum);
        }
Exemplo n.º 2
0
        public async Task Update_ShouldReturnNotFound_WhenWrongValues()
        {
            // Arrange
            int id = 1;
            var constructionBoltRequest = new ConstructionBoltUpdateRequest
            {
                Packet = 9,
            };
            var wrongConstructionBoltRequest = new ConstructionBoltUpdateRequest
            {
                DiameterId = 999,
            };
            string json1        = JsonSerializer.Serialize(wrongConstructionBoltRequest);
            string json2        = JsonSerializer.Serialize(constructionBoltRequest);
            var    httpContent1 = new StringContent(json1, Encoding.UTF8, "application/json");
            var    httpContent2 = new StringContent(json2, Encoding.UTF8, "application/json");
            var    endpoint1    = $"/api/construction-bolts/{id}";
            var    endpoint2    = $"/api/construction-bolts/{999}";

            // Act
            var response1 = await _httpClient.PatchAsync(endpoint1, httpContent1);

            var response2 = await _httpClient.PatchAsync(endpoint2, httpContent2);

            // Assert
            Assert.Equal(HttpStatusCode.NotFound, response1.StatusCode);
            Assert.Equal(HttpStatusCode.NotFound, response2.StatusCode);
        }
Exemplo n.º 3
0
        public void Update(int id, ConstructionBoltUpdateRequest constructionBoltRequest)
        {
            if (constructionBoltRequest == null)
            {
                throw new ArgumentNullException(nameof(constructionBoltRequest));
            }
            var foundConstructionBolt = _repository.GetById(id);

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

            if (constructionBoltRequest.DiameterId != null)
            {
                var boltDiameter = _boltDiameterRepo.GetById(
                    constructionBoltRequest.DiameterId.GetValueOrDefault());
                if (boltDiameter == null)
                {
                    throw new ArgumentNullException(nameof(boltDiameter));
                }

                var uniqueConstraintViolationCheck = _repository.GetByUniqueKey(
                    foundConstructionBolt.Construction.Id, constructionBoltRequest.DiameterId.GetValueOrDefault());
                if (uniqueConstraintViolationCheck != null)
                {
                    throw new ConflictException(nameof(uniqueConstraintViolationCheck));
                }

                foundConstructionBolt.Diameter = boltDiameter;
            }

            if (constructionBoltRequest.Packet != null)
            {
                foundConstructionBolt.Packet = constructionBoltRequest.Packet.GetValueOrDefault();
            }
            if (constructionBoltRequest.Num != null)
            {
                foundConstructionBolt.Num = constructionBoltRequest.Num.GetValueOrDefault();
            }
            if (constructionBoltRequest.NutNum != null)
            {
                foundConstructionBolt.NutNum = constructionBoltRequest.NutNum.GetValueOrDefault();
            }
            if (constructionBoltRequest.WasherNum != null)
            {
                foundConstructionBolt.WasherNum = constructionBoltRequest.WasherNum.GetValueOrDefault();
            }

            _repository.Update(foundConstructionBolt);

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

            foundMark.EditedDate = DateTime.Now;
            _markRepo.Update(foundMark);
        }
        public void Update_ShouldFailWithConflict()
        {
            // Arrange
            var conflictDiameterId = 2;
            var id = 1;

            var newConstructionBoltRequest = new ConstructionBoltUpdateRequest
            {
                DiameterId = conflictDiameterId,
            };

            // Act & Assert
            Assert.Throws <ConflictException>(() => _updateService.Update(id, newConstructionBoltRequest));

            _updateRepository.Verify(mock => mock.Update(
                                         It.IsAny <ConstructionBolt>()), Times.Never);
        }
Exemplo n.º 5
0
        public async Task Update_ShouldReturnUnauthorized_WhenNoAccessToken()
        {
            // Arrange
            int id = 1;
            var constructionBoltRequest = new ConstructionBoltUpdateRequest
            {
                Packet = 9,
            };
            string json        = JsonSerializer.Serialize(constructionBoltRequest);
            var    httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var    endpoint    = $"/api/construction-bolts/{id}";

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

            // Assert
            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
        }
Exemplo n.º 6
0
        public async Task Update_ShouldReturnConflict_WhenConflictValues()
        {
            // Arrange
            int id         = 3;
            int diameterId = 3;
            var constructionBoltRequest = new ConstructionBoltUpdateRequest
            {
                DiameterId = diameterId,
            };
            string json        = JsonSerializer.Serialize(constructionBoltRequest);
            var    httpContent = new StringContent(json, Encoding.UTF8, "application/json");
            var    endpoint    = $"/api/construction-bolts/{id}";

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

            // Assert
            Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
        }
Exemplo n.º 7
0
 public ActionResult Update(
     int id, [FromBody] ConstructionBoltUpdateRequest constructionBoltRequest)
 {
     if (!constructionBoltRequest.Validate())
     {
         return(BadRequest());
     }
     try
     {
         _service.Update(id, constructionBoltRequest);
     }
     catch (ArgumentNullException)
     {
         return(NotFound());
     }
     catch (ConflictException)
     {
         return(Conflict());
     }
     return(NoContent());
 }
        public void Update_ShouldFailWithNull_WhenWrongValues()
        {
            // Arrange
            int id        = _rnd.Next(1, _updateConstructionBolts.Count());
            int wrongId   = 999;
            var newNumber = 6;

            var newConstructionBoltRequest = new ConstructionBoltUpdateRequest
            {
                Packet    = newNumber,
                Num       = newNumber,
                NutNum    = newNumber,
                WasherNum = newNumber,
            };

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

            _updateRepository.Verify(mock => mock.Update(
                                         It.IsAny <ConstructionBolt>()), Times.Never);
        }