public async Task <IActionResult> PutWorkTaskAsync([FromBody] WorkTaskResource newWorkTaskResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } if (newWorkTaskResource == null) { return(BadRequest($"Parameter {nameof(newWorkTaskResource)} is null")); } mLogger.LogDebug($"Requesting putting new task {newWorkTaskResource.Description} to group {newWorkTaskResource.GroupName}"); try { IResponse <IWorkTask> result = await mTasksGroupService.SaveTaskAsync(newWorkTaskResource.GroupName, newWorkTaskResource.Description) .ConfigureAwait(false); if (!result.IsSuccess) { return(StatusCode(StatusCodes.Status405MethodNotAllowed, result.Message)); } WorkTaskResource workTaskResource = mMapper.Map <IWorkTask, WorkTaskResource>(result.ResponseObject); return(Ok(workTaskResource)); } catch (Exception ex) { mLogger.LogError(ex, "Put operation failed with error"); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }
public async Task PutWorkTaskAsync_RequestSuccess_ExpectedResourceReturned() { IWorkTask expectedWorkTask = A.Fake <IWorkTask>(); A.CallTo(() => expectedWorkTask.Description).Returns("description"); A.CallTo(() => expectedWorkTask.GroupName).Returns("newGroupName"); ITasksGroupService tasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => tasksGroupService.SaveTaskAsync(expectedWorkTask.GroupName, expectedWorkTask.Description)) .Returns(new SuccessResponse <IWorkTask>(expectedWorkTask)); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(tasksGroupService); using HttpClient httpClient = testServer.CreateClient(); WorkTaskResource workTaskResource = new WorkTaskResource { GroupName = expectedWorkTask.GroupName, Description = expectedWorkTask.Description }; using StringContent jsonContent = new StringContent(JsonConvert.SerializeObject(workTaskResource), Encoding.UTF8, PostMediaType); using HttpResponseMessage response = await httpClient.PutAsync(MainRoute, jsonContent).ConfigureAwait(false); string stringResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false); WorkTaskResource returnedResource = JsonConvert.DeserializeObject <WorkTaskResource>(stringResponse); Assert.Equal(expectedWorkTask.GroupName, returnedResource.GroupName); Assert.Equal(expectedWorkTask.Description, returnedResource.Description); }
public async Task PutWorkTaskAsync_RequestNotSuccess_MethodNotAllowedReturned() { ITasksGroupService taskGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => taskGroupService.SaveTaskAsync(A <string> .Ignored, A <string> .Ignored)) .Returns(new FailResponse <IWorkTask>("")); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(taskGroupService); using HttpClient httpClient = testServer.CreateClient(); WorkTaskResource workTaskResource = new WorkTaskResource { GroupName = "newGroupName" }; using StringContent jsonContent = new StringContent(JsonConvert.SerializeObject(workTaskResource), Encoding.UTF8, PostMediaType); using HttpResponseMessage response = await httpClient.PutAsync(MainRoute, jsonContent).ConfigureAwait(false); A.CallTo(() => taskGroupService.SaveTaskAsync(A <string> .Ignored, A <string> .Ignored)).MustHaveHappenedOnceExactly(); Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode); }
public async Task PutWorkTaskAsync_ThrowsException_InternalServerErrorStatusCode() { ITasksGroupService fakeTasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => fakeTasksGroupService.SaveTaskAsync(A <string> .Ignored, A <string> .Ignored)) .Throws <Exception>(); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(fakeTasksGroupService); using HttpClient httpClient = testServer.CreateClient(); WorkTaskResource workTaskResource = new WorkTaskResource { GroupName = "newGroupName", Description = "description" }; using StringContent jsonContent = new StringContent(JsonConvert.SerializeObject(workTaskResource), Encoding.UTF8, PostMediaType); using HttpResponseMessage response = await httpClient.PutAsync(MainRoute, jsonContent).ConfigureAwait(false); Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); }
public async Task PutWorkTaskAsync_RequestSuccess_SuccessStatusCode() { WorkTaskResource workTaskResource = new WorkTaskResource { GroupName = "newGroupName", Description = "description" }; ITasksGroupService tasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => tasksGroupService.SaveTaskAsync(workTaskResource.GroupName, workTaskResource.Description)) .Returns(new SuccessResponse <IWorkTask>(A.Fake <IWorkTask>())); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(tasksGroupService); using HttpClient httpClient = testServer.CreateClient(); using StringContent jsonContent = new StringContent(JsonConvert.SerializeObject(workTaskResource), Encoding.UTF8, PostMediaType); using HttpResponseMessage response = await httpClient.PutAsync(MainRoute, jsonContent).ConfigureAwait(false); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }