public void Update_ShouldUpdateDoc() { // Arrange int id = _rnd.Next(2, _docs.Count()); var newName = "NewUpdate"; var newTypeId = _docs[0].Type.Id; short newNumOfPages = 9; var newForm = 9.0f; var newDocRequest = new DocUpdateRequest { Name = newName, TypeId = newTypeId, NumOfPages = newNumOfPages, Form = newForm, }; // Act _service.Update(id, newDocRequest); // Assert _repository.Verify(mock => mock.Update(It.IsAny <Doc>()), Times.Once); Assert.Equal(newTypeId, _docs.SingleOrDefault(v => v.Id == id).Type.Id); Assert.Equal(newName, _docs.SingleOrDefault(v => v.Id == id).Name); Assert.Equal(newNumOfPages, _docs.SingleOrDefault(v => v.Id == id).NumOfPages); Assert.Equal(newForm, _docs.SingleOrDefault(v => v.Id == id).Form); }
public ActionResult Update(int id, [FromBody] DocUpdateRequest docRequest) { if (!docRequest.Validate()) { return(BadRequest()); } try { _service.Update(id, docRequest); } catch (ArgumentNullException) { return(NotFound()); } return(NoContent()); }
public void Update_ShouldFailWithNull_WhenWrongValues() { // Arrange int id = _rnd.Next(1, _docs.Count()); var newDocRequest = new DocUpdateRequest { Name = "NewUpdate", }; // Act & Assert Assert.Throws <ArgumentNullException>(() => _service.Update(id, null)); Assert.Throws <ArgumentNullException>(() => _service.Update( 999, newDocRequest)); _repository.Verify(mock => mock.Update(It.IsAny <Doc>()), Times.Never); }
public async Task Update_ShouldReturnUnauthorized_WhenNoAccessToken() { // Arrange int id = 1; var docRequest = new DocUpdateRequest { Name = "NewUpdate", }; string json = JsonSerializer.Serialize(docRequest); var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); var endpoint = $"/api/docs/{id}"; // Act var response = await _authHttpClient.PatchAsync(endpoint, httpContent); // Assert Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }
public async Task Update_ShouldReturnNoContent() { // Arrange int id = 1; var docRequest = new DocUpdateRequest { Name = "NewUpdate", ReleaseNum = 9, NumOfPages = 9, Note = "NewUpdate", }; string json = JsonSerializer.Serialize(docRequest); var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); var endpoint = $"/api/docs/{id}"; // Act var response = await _httpClient.PatchAsync(endpoint, httpContent); // Assert Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); }
public void Update( int id, DocUpdateRequest doc) { if (doc == null) { throw new ArgumentNullException(nameof(doc)); } var foundDoc = _repository.GetById(id); if (foundDoc == null) { throw new ArgumentNullException(nameof(foundDoc)); } if (doc.Name != null) { foundDoc.Name = doc.Name; } if (doc.Form != null) { foundDoc.Form = doc.Form.GetValueOrDefault(); } if (doc.ReleaseNum != null) { foundDoc.ReleaseNum = doc.ReleaseNum.GetValueOrDefault(); } if (doc.NumOfPages != null) { foundDoc.NumOfPages = doc.NumOfPages.GetValueOrDefault(); } if (doc.Note != null) { foundDoc.Note = doc.Note; } if (doc.TypeId != null) { var docs = _repository.GetAllByMarkIdAndDocType( foundDoc.Mark.Id, doc.TypeId.GetValueOrDefault()); int maxNum = 0; foreach (var s in docs) { if (s.Num > maxNum) { maxNum = s.Num; } } foundDoc.Num = (Int16)(maxNum + 1); var docType = _docTypeRepo.GetById(doc.TypeId.GetValueOrDefault()); if (docType == null) { throw new ArgumentNullException(nameof(docType)); } foundDoc.Type = docType; } if (doc.CreatorId != null) { var creator = _employeeRepo.GetById( doc.CreatorId.GetValueOrDefault()); if (creator == null) { throw new ArgumentNullException(nameof(creator)); } foundDoc.Creator = creator; } if (doc.InspectorId != null) { var inspectorId = doc.InspectorId.GetValueOrDefault(); if (inspectorId == -1) { foundDoc.InspectorId = null; } else { var inspector = _employeeRepo.GetById(inspectorId); if (inspector == null) { throw new ArgumentNullException(nameof(inspector)); } foundDoc.Inspector = inspector; } } if (doc.NormContrId != null) { var normContrId = doc.NormContrId.GetValueOrDefault(); if (normContrId == -1) { foundDoc.NormContrId = null; } else { var normContr = _employeeRepo.GetById(normContrId); if (normContr == null) { throw new ArgumentNullException(nameof(normContr)); } foundDoc.NormContr = normContr; } } _repository.Update(foundDoc); var foundMark = _markRepo.GetById(foundDoc.Mark.Id); foundMark.EditedDate = DateTime.Now; _markRepo.Update(foundMark); }