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 TasksGroupsController(ITasksGroupService taskService, IMapper mapper, ILogger <TasksGroupsController> logger) { mTasksGroupService = taskService ?? throw new ArgumentNullException(nameof(taskService)); mMapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); mLogger = logger ?? throw new ArgumentNullException(nameof(logger)); }
public async Task RemoveGroupAsync_ThrowsException_InternalServerErrorStatusCode() { ITasksGroupService fakeTasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => fakeTasksGroupService.RemoveAsync(A <string> .Ignored)) .Throws <Exception>(); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(fakeTasksGroupService); using HttpClient httpClient = testServer.CreateClient(); using HttpResponseMessage response = await httpClient.DeleteAsync($"{MainRoute}/some-id").ConfigureAwait(false); Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); }
public async Task RemoveGroupAsync_RemoveSuccess_SuccessStatusCode() { ITasksGroupService fakeTasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => fakeTasksGroupService.RemoveAsync(A <string> .Ignored)) .Returns(new SuccessResponse <ITasksGroup>(A.Fake <ITasksGroup>(), "")); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(fakeTasksGroupService); using HttpClient httpClient = testServer.CreateClient(); using HttpResponseMessage response = await httpClient.DeleteAsync($"{MainRoute}/some-id").ConfigureAwait(false); response.EnsureSuccessStatusCode(); }
public async Task ListTasksOfSpecificGroupAsync_groupNotFound_EmptyListReturned() { ITasksGroupService tasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => tasksGroupService.ListAsync()).Returns(new List <ITasksGroup>()); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(tasksGroupService); using HttpClient httpClient = testServer.CreateClient(); using HttpResponseMessage response = await httpClient.GetAsync($"{MainRoute}/some-id").ConfigureAwait(false); string stringResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false); IEnumerable <WorkTaskResource> workTaskResources = JsonConvert.DeserializeObject <IEnumerable <WorkTaskResource> >(stringResponse); Assert.Empty(workTaskResources); }
public async Task ListTasksOfSpecificGroupAsync_SuccessStatusCode() { const string groupId = "some-id"; ITasksGroup tasksGroup = A.Fake <ITasksGroup>(); A.CallTo(() => tasksGroup.ID).Returns(groupId); ITasksGroupService tasksGroupService = A.Fake <ITasksGroupService>(); A.CallTo(() => tasksGroupService.ListAsync()).Returns(new List <ITasksGroup>()); using TestServer testServer = ApiTestHelper.BuildTestServerWithFakes(tasksGroupService); using HttpClient httpClient = testServer.CreateClient(); using HttpResponseMessage response = await httpClient.GetAsync($"{MainRoute}/{groupId}").ConfigureAwait(false); response.EnsureSuccessStatusCode(); }
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(); }
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); }
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 static TestServer BuildTestServerWithFakes( ITasksGroupService tasksGroupService = null, IWorkTaskService workTaskService = null, INoteService noteService = null, ICloudService cloudService = null) { if (tasksGroupService == null) { tasksGroupService = A.Fake <ITasksGroupService>(); } if (workTaskService == null) { workTaskService = A.Fake <IWorkTaskService>(); } if (noteService == null) { noteService = A.Fake <INoteService>(); } if (cloudService == null) { cloudService = A.Fake <ICloudService>(); } TestServer testServer = new TestServer(WebHost.CreateDefaultBuilder() .ConfigureTestServices(sc => { sc.AddSingleton(tasksGroupService); sc.AddSingleton(workTaskService); sc.AddSingleton(noteService); sc.AddSingleton(cloudService); }) .UseStartup <Startup>() .UseEnvironment("Development")); return(testServer); }