public async Task Update_ShouldReturnUnauthorized_WhenNoAccessToken() { // Arrange int id = 3; short typeId = 3; short weldingControlId = 3; var constructionRequest = new ConstructionUpdateRequest { Name = "NewUpdate", TypeId = typeId, Valuation = "NewUpdate", StandardAlbumCode = "NewUpdate", NumOfStandardConstructions = 2, HasEdgeBlunting = true, HasDynamicLoad = true, HasFlangedConnections = true, WeldingControlId = weldingControlId, PaintworkCoeff = 2.0f, }; string json = JsonSerializer.Serialize(constructionRequest); var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); var endpoint = $"/api/constructions/{id}"; // Act var response = await _authHttpClient.PatchAsync(endpoint, httpContent); // Assert Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }
public void Update_ShouldFailWithNull_WhenWrongValues() { // Arrange int id = _rnd.Next(1, _updateConstructions.Count()); var newConstructionRequest = new ConstructionUpdateRequest { Valuation = "NewUpdate", }; // Act & Assert Assert.Throws <ArgumentNullException>(() => _updateService.Update(id, null)); Assert.Throws <ArgumentNullException>(() => _updateService.Update( 999, newConstructionRequest)); _updateRepository.Verify(mock => mock.Update(It.IsAny <Construction>()), Times.Never); }
public void Update_ShouldFailWithConflict() { // Arrange var id = _updateConstructions[1].Id; var conflictName = _updateConstructions[0].Name; var conflictPaintworkCoeff = _updateConstructions[0].PaintworkCoeff; var newConstructionRequest = new ConstructionUpdateRequest { Name = conflictName, PaintworkCoeff = conflictPaintworkCoeff, Valuation = "NewUpdate", }; // Act & Assert Assert.Throws <ConflictException>(() => _updateService.Update(id, newConstructionRequest)); _updateRepository.Verify(mock => mock.Update(It.IsAny <Construction>()), Times.Never); }
public async Task Update_ShouldReturnConflict_WhenConflictValues() { // Arrange int id = 2; var conflictName = TestData.constructions[0].Name; var conflictPaintworkCoeff = TestData.constructions[0].PaintworkCoeff; var constructionRequest = new ConstructionUpdateRequest { Name = conflictName, PaintworkCoeff = conflictPaintworkCoeff, }; string json = JsonSerializer.Serialize(constructionRequest); var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); var endpoint = $"/api/constructions/{id}"; // Act var response = await _httpClient.PatchAsync(endpoint, httpContent); // Assert Assert.Equal(HttpStatusCode.Conflict, response.StatusCode); }
public ActionResult Update( int id, [FromBody] ConstructionUpdateRequest constructionRequest) { if (!constructionRequest.Validate()) { return(BadRequest()); } try { _service.Update(id, constructionRequest); } catch (ArgumentNullException) { return(NotFound()); } catch (ConflictException) { return(Conflict()); } return(NoContent()); }
public void Update_ShouldUpdateConstruction() { // Arrange int id = _rnd.Next(1, _updateConstructions.Count()); var newStringValue = "NewUpdate"; var newBoolValue = true; short newIntValue = 99; var newConstructionRequest = new ConstructionUpdateRequest { Name = newStringValue, Valuation = newStringValue, NumOfStandardConstructions = newIntValue, HasEdgeBlunting = newBoolValue, HasDynamicLoad = newBoolValue, HasFlangedConnections = newBoolValue, PaintworkCoeff = newIntValue, }; // Act _updateService.Update(id, newConstructionRequest); // Assert _updateRepository.Verify(mock => mock.Update(It.IsAny <Construction>()), Times.Once); Assert.Equal( newStringValue, _updateConstructions.SingleOrDefault(v => v.Id == id).Name); Assert.Equal( newStringValue, _updateConstructions.SingleOrDefault(v => v.Id == id).Valuation); Assert.Equal( newIntValue, _updateConstructions.SingleOrDefault(v => v.Id == id).NumOfStandardConstructions); Assert.Equal( newBoolValue, _updateConstructions.SingleOrDefault(v => v.Id == id).HasEdgeBlunting); Assert.Equal( newBoolValue, _updateConstructions.SingleOrDefault(v => v.Id == id).HasDynamicLoad); Assert.Equal( newBoolValue, _updateConstructions.SingleOrDefault(v => v.Id == id).HasFlangedConnections); Assert.Equal( newIntValue, _updateConstructions.SingleOrDefault(v => v.Id == id).PaintworkCoeff); }
public void Update( int id, ConstructionUpdateRequest construction) { if (construction == null) { throw new ArgumentNullException(nameof(Construction)); } var foundConstruction = _repository.GetById(id); if (foundConstruction == null) { throw new ArgumentNullException(nameof(foundConstruction)); } var isUniqueKeyChanged = false; if (construction.Name != null) { // var uniqueConstraintViolationCheck = _repository.GetByUniqueKey( // foundConstruction.Specification.Id, construction.Name, construction.PaintworkCoeff); // if (uniqueConstraintViolationCheck != null && uniqueConstraintViolationCheck.Id != id) // throw new ConflictException(uniqueConstraintViolationCheck.Id.ToString()); foundConstruction.Name = construction.Name; isUniqueKeyChanged = true; } if (construction.TypeId != null) { var typeId = construction.TypeId.GetValueOrDefault(); var type = _constructionTypeRepo.GetById(typeId); if (type == null) { throw new ArgumentNullException(nameof(type)); } foundConstruction.Type = type; } if (construction.SubtypeId != null) { var subtypeId = construction.SubtypeId.GetValueOrDefault(); if (subtypeId == -1) { foundConstruction.Subtype = null; } else { var subtype = _constructionSubtypeRepo.GetById(subtypeId); if (subtype == null) { throw new ArgumentNullException(nameof(subtype)); } foundConstruction.Subtype = subtype; } } if (construction.Valuation != null) { foundConstruction.Valuation = construction.Valuation; } if (construction.StandardAlbumCode != null) { foundConstruction.StandardAlbumCode = construction.StandardAlbumCode; } if (construction.NumOfStandardConstructions != null) { foundConstruction.NumOfStandardConstructions = construction.NumOfStandardConstructions.GetValueOrDefault(); } if (construction.HasEdgeBlunting != null) { foundConstruction.HasEdgeBlunting = construction.HasEdgeBlunting.GetValueOrDefault(); } if (construction.HasDynamicLoad != null) { foundConstruction.HasDynamicLoad = construction.HasDynamicLoad.GetValueOrDefault(); } if (construction.HasFlangedConnections != null) { foundConstruction.HasFlangedConnections = construction.HasFlangedConnections.GetValueOrDefault(); } if (construction.WeldingControlId != null) { var weldingControlId = construction.WeldingControlId.GetValueOrDefault(); var weldingControl = _weldingControlRepo.GetById(weldingControlId); if (weldingControl == null) { throw new ArgumentNullException(nameof(weldingControl)); } foundConstruction.WeldingControl = weldingControl; } if (construction.PaintworkCoeff != null) { foundConstruction.PaintworkCoeff = construction.PaintworkCoeff.GetValueOrDefault(); isUniqueKeyChanged = true; } if (isUniqueKeyChanged) { var uniqueConstraintViolationCheck = _repository.GetByUniqueKey( foundConstruction.Specification.Id, foundConstruction.Name, foundConstruction.PaintworkCoeff); if (uniqueConstraintViolationCheck != null && uniqueConstraintViolationCheck.Id != id) { throw new ConflictException(uniqueConstraintViolationCheck.Id.ToString()); } } _repository.Update(foundConstruction); var foundMark = _markRepo.GetById(foundConstruction.Specification.Mark.Id); foundMark.EditedDate = DateTime.Now; _markRepo.Update(foundMark); }