public async Task <IActionResult> PostGroupAsync(string id, [FromBody] TasksGroupResource saveTasksGroupResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } if (saveTasksGroupResource == null) { return(BadRequest("New group name resource is null")); } mLogger.LogDebug($"Requesting updating group id {id} with name {saveTasksGroupResource.GroupName}"); try { IResponse <ITasksGroup> result = await mTasksGroupService.UpdateGroupAsync(id, saveTasksGroupResource.GroupName).ConfigureAwait(false); mLogger.LogDebug($"Update result {(result.IsSuccess ? "succeeded" : "failed")}"); if (!result.IsSuccess) { return(StatusCode(StatusCodes.Status405MethodNotAllowed, result.Message)); } TasksGroupResource tasksGroupResource = mMapper.Map <ITasksGroup, TasksGroupResource>(result.ResponseObject); return(Ok(tasksGroupResource)); } catch (Exception ex) { mLogger.LogError(ex, "Update operation failed with error"); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }
public async Task PostGroupAsync_ThrowsException_InternalServerErrorStatusCode() { ITasksGroupService fakeTasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => fakeTasksGroupService.UpdateGroupAsync(A <string> .Ignored, A <string> .Ignored)) .Throws <Exception>(); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(fakeTasksGroupService); using HttpClient httpClient = testServer.CreateClient(); TasksGroupResource groupResource = new TasksGroupResource { GroupName = "newGroupName" }; using StringContent jsonContent = new StringContent(JsonConvert.SerializeObject(groupResource), Encoding.UTF8, PostMediaType); using HttpResponseMessage response = await httpClient.PostAsync($"{MainRoute}/some-id", jsonContent).ConfigureAwait(false); Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); }
public async Task PostGroupAsync_SuccessStatusCode() { ITasksGroupService fakeTasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => fakeTasksGroupService.UpdateGroupAsync(A <string> .Ignored, A <string> .Ignored)) .Returns(new SuccessResponse <ITasksGroup>(A.Fake <ITasksGroup>())); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(fakeTasksGroupService); using HttpClient httpClient = testServer.CreateClient(); TasksGroupResource groupResource = new TasksGroupResource { GroupName = "newGroupName" }; using StringContent jsonContent = new StringContent(JsonConvert.SerializeObject(groupResource), Encoding.UTF8, PostMediaType); using HttpResponseMessage response = await httpClient.PostAsync($"{MainRoute}/some-id", jsonContent).ConfigureAwait(false); response.EnsureSuccessStatusCode(); }