public void Test_For_AddTask() { // Arrange var userGuid = Guid.NewGuid(); var testTaskVM = new Models.ViewModels.Task() { TaskName = "TestTask1", Priority = 20, ProjectName = "TestProject100", OwnerFullName = "First Last", StartDate = DateTime.Today }; var testTaskDM = new Models.DataModels.Task() { TaskName = "TestTask1", Priority = 20, ProjectId = 100, TaskOwnerId = userGuid, StartDate = DateTime.Today, Project = new Models.DataModels.Project() { ProjectId = 100, ProjectName = "TestProject100", Priority = 1, ManagerId = userGuid }, TaskOwner = new Models.DataModels.User() { Id = userGuid, UserId = "TestUser1", FirstName = "First", LastName = "Last" } }; mockTaskRepo.Setup(repo => repo.Create(It.IsAny <Models.DataModels.Task>())).Returns(testTaskDM); // Act var actualTask = tasksLogicTest.CreateTask(testTaskVM); // Assert Assert.NotNull(actualTask); Assert.Equal(testTaskVM.ProjectName, actualTask.ProjectName); Assert.Equal(testTaskVM.OwnerFullName, actualTask.OwnerFullName); }
public void Test_Post_NewTask_Valid() { // Arrange var newTestTask = new Models.ViewModels.Task() { TaskId = 20, TaskName = "Test New Task", TaskOwnerId = Guid.NewGuid(), Priority = 20, StartDate = DateTime.Today, ProjectId = 100 }; var expectedUrlPart = "api/Task"; var supposedToBeBaseUri = "http://localhost/projectmanapi"; var expectedCreatedUri = string.Join("/", supposedToBeBaseUri, expectedUrlPart, newTestTask.TaskId); var mockRequest = mockController.Request ?? new System.Net.Http.HttpRequestMessage(); mockRequest.RequestUri = new Uri(string.Join("/", supposedToBeBaseUri, expectedUrlPart)); mockRequest.Method = System.Net.Http.HttpMethod.Post; mockController.Request = mockRequest; var expectedTestResult = new CreatedNegotiatedContentResult <Models.ViewModels.Task>(new Uri(expectedCreatedUri), newTestTask, mockController); mockTaskLogic.Setup(api => api.CreateTask(newTestTask)).Returns(newTestTask); // Act var actualResult = mockController.Post(newTestTask); var actualProjData = ((CreatedNegotiatedContentResult <Models.ViewModels.Task>)actualResult).Content; var actualCreatedUri = ((CreatedNegotiatedContentResult <Models.ViewModels.Task>)actualResult).Location.ToString(); // Assert Assert.IsNotNull(actualResult); Assert.IsInstanceOf(typeof(CreatedNegotiatedContentResult <Models.ViewModels.Task>), actualResult); Assert.AreEqual(newTestTask, actualProjData); Assert.IsNotEmpty(actualCreatedUri); Assert.AreEqual(expectedCreatedUri, actualCreatedUri); Assert.IsTrue(actualCreatedUri.Contains(expectedUrlPart)); }
public void Test_Post_New_Task_InValid() { // Arrange var newTestTask = new Models.ViewModels.Task() { TaskId = 25, TaskName = "Test New Task", Priority = 20 }; var validationCxt = new ValidationContext(newTestTask); var expectedErrMsg = "Invalid request information. Please verify the information entered."; var fnMissingModelState = new ModelState(); var mockModelErr = new ModelError(expectedErrMsg); fnMissingModelState.Errors.Add(mockModelErr); mockController.ModelState.Add("ProjectId", fnMissingModelState); mockTaskLogic.Setup(api => api.CreateTask(newTestTask)); // Act var actualResult = mockController.Post(newTestTask); var actualModelState = ((InvalidModelStateResult)actualResult).ModelState; // Assert Assert.IsNotNull(actualResult); Assert.Contains("ProjectId", actualModelState.Keys.ToList()); Assert.AreEqual(fnMissingModelState, actualModelState["ProjectId"]); Assert.IsNotEmpty(actualModelState["ProjectId"].Errors[0].ErrorMessage); Assert.AreEqual(expectedErrMsg, actualModelState["ProjectId"].Errors[0].ErrorMessage); }
public void Test_Get_User_Tasks() { // Arrange string testUserId = "User1Test"; var testUserGuid = Guid.NewGuid(); var mockedTaskList = new Models.ViewModels.Task[] { new Models.ViewModels.Task() { TaskId = 1, TaskName = "TestTask-1", Priority = 5, TaskOwnerId = testUserGuid, OwnerFullName = "User1Test", ProjectId = 100 }, new Models.ViewModels.Task() { TaskId = 2, TaskName = "TestTask-2", Priority = 15, TaskOwnerId = testUserGuid, OwnerFullName = "User1Test", ProjectId = 100 }, new Models.ViewModels.Task() { TaskId = 16, TaskName = "TestTask-16", Priority = 25, TaskOwnerId = testUserGuid, OwnerFullName = "User1Test", ProjectId = 200 }, new Models.ViewModels.Task() { TaskId = 20, TaskName = "TestTask-20", Priority = 10, TaskOwnerId = testUserGuid, OwnerFullName = "User1Test", ProjectId = 250 }, }; mockTaskLogic.Setup(api => api.GetAllTasksForUser(testUserId)).Returns(mockedTaskList); // Act var actualResult = mockController.GetAllTasksForUser(testUserId); var actualData = (OkObjectResult)actualResult; // Assert Assert.Equal(StatusCodes.Status200OK, actualData.StatusCode); Assert.Equal(mockedTaskList.Count(), (actualData.Value as IEnumerable <Models.ViewModels.Task>).Count()); }
public bool UpdateTask(int taskId, Models.ViewModels.Task taskModel) { if (taskRepository.GetById(taskId) != null && taskModel.TaskId == taskId) { return(taskRepository.Update(taskModel.AsDataModel())); } else { return(false); } }
public async Task Test_Create_Tasks() { //var managerIdToTest = await apiTestClient.GetAsync("/api/Users/Test-User1").Result.Content.ReadAsAsync<Models.ViewModels.User>(); var TaskIdToCreate = new Models.ViewModels.Task { TaskId = 11, TaskName = "NewTestTask", Priority = 20, TaskOwnerId = Guid.NewGuid(), StartDate = DateTime.Today, ProjectId = 1 }; var response = await apiTestClient.PostAsJsonAsync("/api/Tasks", TaskIdToCreate); response.EnsureSuccessStatusCode(); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.NotEqual(0, (response.Content.ReadAsAsync <Models.ViewModels.Task>().Result).TaskId); }
public void Test_For_Search_Projects(string searchText, bool exactMatch) { // Arrange var tasksList = new Models.ViewModels.Task[] { new Models.ViewModels.Task() { ProjectId = 1, ProjectName = "TestProject-1", TaskOwnerId = Guid.NewGuid(), Priority = 10, TaskId = 10, TaskName = "TestTask1" }, new Models.ViewModels.Task() { ProjectId = 2, ProjectName = "TestProject-2", TaskOwnerId = Guid.NewGuid(), Priority = 5, TaskId = 11, TaskName = "TestTask2" }, new Models.ViewModels.Task() { ProjectId = 3, ProjectName = "TestProject-3", TaskOwnerId = Guid.NewGuid(), Priority = 15, TaskId = 9, TaskName = "TestTask3" }, new Models.ViewModels.Task() { ProjectId = 4, ProjectName = "TestProject-4", TaskOwnerId = Guid.NewGuid(), Priority = 20, TaskId = 7, TaskName = "TestTask4" }, new Models.ViewModels.Task() { ProjectId = 5, ProjectName = "TestProject-5", TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "TestTask5", ParentTaskName = "TestParentTask1", ParentTaskId = 200 }, new Models.ViewModels.Task() { ProjectId = 5, ProjectName = "TestProject-5", TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "TestTask6", ParentTaskName = "TestParentTask1", ParentTaskId = 200 }, new Models.ViewModels.Task() { ProjectId = 5, ProjectName = "TestProject-5", TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "TestTask7", ParentTaskName = "TestParentTask2", ParentTaskId = 202 }, }.AsEnumerable(); IEnumerable <Models.ViewModels.Task> expectedResult; if (exactMatch) { expectedResult = tasksList.Where(p => p.ProjectName.ToLower().Equals(searchText.ToLower())); } else { expectedResult = tasksList; } mockTaskRepo.Setup(repo => repo.Search(It.IsAny <System.Linq.Expressions.Expression <Func <Models.DataModels.Task, bool> > >())).Returns(expectedResult.AsDataModel()); // Act var actualResult = tasksLogicTest.Search(searchText, exactMatch); // Assert Assert.NotNull(actualResult); Assert.Equal(expectedResult.Count(), actualResult.Count()); }
public static Models.DataModels.Task AsDataModel(this Models.ViewModels.Task taskData, Models.DataModels.Task dataModel = null) { var config = new MapperConfiguration(cfg => { cfg.CreateMap <Models.ViewModels.Task, Models.DataModels.Task>().ReverseMap(); }); if (dataModel == null) { return(config.CreateMapper().Map <Models.ViewModels.Task, Models.DataModels.Task>(taskData)); } else { return(config.CreateMapper().Map(taskData, dataModel)); } }
public void Test_For_DeleteTask(int testTaskIdForDelete, bool expectedResult) { // Arrange var userGuid = Guid.NewGuid(); var testTaskVM = new Models.ViewModels.Task() { TaskId = testTaskIdForDelete, TaskName = "TestTask1", Priority = 20, ProjectName = "TestProject100", OwnerFullName = "First Last", StartDate = DateTime.Today }; Models.DataModels.Task testTaskForDelete; if (expectedResult) { testTaskForDelete = new Models.DataModels.Task() { TaskId = testTaskIdForDelete, TaskName = "TestTask1", Priority = 20, ProjectId = 100, TaskOwnerId = userGuid, StartDate = DateTime.Today, Project = new Models.DataModels.Project() { ProjectId = 100, ProjectName = "TestProject100", Priority = 1, ManagerId = userGuid }, TaskOwner = new Models.DataModels.User() { Id = userGuid, UserId = "TestUser1", FirstName = "First", LastName = "Last" } }; } else { testTaskForDelete = null; } mockTaskRepo.Setup(repo => repo.GetById(testTaskIdForDelete)).Returns(testTaskForDelete); mockTaskRepo.Setup(repo => repo.Delete(It.IsAny <Models.DataModels.Task>())).Returns(expectedResult); // Act var actualResult = tasksLogicTest.DeleteTask(testTaskIdForDelete); // Assert Assert.Equal(expectedResult, actualResult); }
public void Test_For_GetUserProjectTasks() { // Arrange Guid testUserGuid = Guid.NewGuid(); int testProjectId = 10; var tasksList = new Models.ViewModels.Task[] { new Models.ViewModels.Task() { ProjectId = 10, ProjectName = "TestProject-10", TaskOwnerId = testUserGuid, Priority = 10, TaskId = 10, TaskName = "TestTask1" }, new Models.ViewModels.Task() { ProjectId = 2, ProjectName = "TestProject-2", TaskOwnerId = Guid.NewGuid(), Priority = 5, TaskId = 11, TaskName = "TestTask2" }, new Models.ViewModels.Task() { ProjectId = 10, ProjectName = "TestProject-10", TaskOwnerId = testUserGuid, Priority = 15, TaskId = 9, TaskName = "TestTask3" }, new Models.ViewModels.Task() { ProjectId = 4, ProjectName = "TestProject-4", TaskOwnerId = Guid.NewGuid(), Priority = 20, TaskId = 7, TaskName = "TestTask4" }, new Models.ViewModels.Task() { ProjectId = 5, ProjectName = "TestProject-5", TaskOwnerId = testUserGuid, Priority = 30, TaskId = 5, TaskName = "TestTask5", ParentTaskName = "TestParentTask1", ParentTaskId = 200 }, new Models.ViewModels.Task() { ProjectId = 10, ProjectName = "TestProject-10", TaskOwnerId = testUserGuid, Priority = 30, TaskId = 5, TaskName = "TestTask6", ParentTaskName = "TestParentTask1", ParentTaskId = 200 }, new Models.ViewModels.Task() { ProjectId = 5, ProjectName = "TestProject-5", TaskOwnerId = testUserGuid, Priority = 30, TaskId = 5, TaskName = "TestTask7", ParentTaskName = "TestParentTask2", ParentTaskId = 202 }, }.AsEnumerable(); IEnumerable <Models.ViewModels.Task> expectedResult; expectedResult = tasksList.Where(p => p.TaskOwnerId == testUserGuid && p.ProjectId == testProjectId); mockTaskRepo.Setup(repo => repo.Search(It.IsAny <System.Linq.Expressions.Expression <Func <Models.DataModels.Task, bool> > >())).Returns(expectedResult.AsDataModel()); // Act var actualResult = tasksLogicTest.GetUserProjectTasks("TestUser1", testProjectId); // Assert Assert.Equal(expectedResult.Count(), actualResult.Count()); }
//[TestCase(Description = "Create a new Task", TestName = "Test Create Task throws Exception")] public void Test_Post_New_Task_For_Exception() { // Arrange var newTestTask = new Models.ViewModels.Task() { TaskId = 5, TaskName = "Test New Task", TaskOwnerId = Guid.NewGuid(), Priority = 20, StartDate = DateTime.Today }; var expectedErrMsg = "Test Exception raised missing Project Id"; mockTaskLogic.Setup(api => api.CreateTask(newTestTask)).Throws(new Exception(expectedErrMsg)); // Act var actualResult = mockController.Post(newTestTask); var actualData = (ObjectResult)actualResult; // Assert Assert.Equal(StatusCodes.Status500InternalServerError, actualData.StatusCode); Assert.Equal(expectedErrMsg, actualData.Value); }
//[TestCase(Description = "Create a new Task", TestName = "Test for Create Task returns Valid result")] public void Test_Post_NewTask_Valid() { // Arrange var newTestTask = new Models.ViewModels.Task() { TaskId = 20, TaskName = "Test New Task", TaskOwnerId = Guid.NewGuid(), Priority = 20, StartDate = DateTime.Today, ProjectId = 100 }; var expectedTestResult = new CreatedResult(string.Concat("/", newTestTask.TaskId), newTestTask); mockTaskLogic.Setup(api => api.CreateTask(newTestTask)).Returns(newTestTask); // Act var actualResult = mockController.Post(newTestTask); var actualTaskResult = (CreatedResult)actualResult; // Assert Assert.NotNull(actualTaskResult); Assert.Equal(StatusCodes.Status201Created, actualTaskResult.StatusCode); Assert.Equal(newTestTask, actualTaskResult.Value); }
public void Test_Post_New_Task_For_Exception() { // Arrange var newTestTask = new Models.ViewModels.Task() { TaskId = 5, TaskName = "Test New Task", TaskOwnerId = Guid.NewGuid(), Priority = 20, StartDate = DateTime.Today }; var expectedErrMsg = "Test Exception raised missing Project Id"; mockTaskLogic.Setup(api => api.CreateTask(newTestTask)).Throws(new Exception(expectedErrMsg)); // Act var actualResult = mockController.Post(newTestTask); var actualData = ((ExceptionResult)actualResult).Exception; // Assert Assert.IsNotNull(actualResult); Assert.IsInstanceOf(typeof(Exception), actualData); Assert.AreEqual(actualData.Message, expectedErrMsg); }
public void Test_Get_AllTasks_For_Project() { // Arrange int projectId = 2; var expectedTestResult = new OkObjectResult(mockProjectsList); var mockTasks = new Models.ViewModels.Task[] { new Models.ViewModels.Task() { ProjectId = projectId, TaskId = 1, TaskName = "TestTask-1", TaskOwnerId = Guid.NewGuid(), Priority = 5 }, new Models.ViewModels.Task() { ProjectId = projectId, TaskId = 2, TaskName = "TestTask-2", TaskOwnerId = Guid.NewGuid(), Priority = 10 }, new Models.ViewModels.Task() { ProjectId = projectId, TaskId = 3, TaskName = "TestTask-3", TaskOwnerId = Guid.NewGuid(), Priority = 15 }, new Models.ViewModels.Task() { ProjectId = projectId, TaskId = 4, TaskName = "TestTask-4", TaskOwnerId = Guid.NewGuid(), Priority = 20 }, new Models.ViewModels.Task() { ProjectId = projectId, TaskId = 5, TaskName = "TestTask-5", TaskOwnerId = Guid.NewGuid(), Priority = 25 }, }; mockTaskLogic.Setup(api => api.GetAllTasksForProject(projectId)).Returns(mockTasks); // Act var actualResult = mockController.GetAllTasksForProject(projectId); var actualResultData = (OkObjectResult)actualResult; var actualUserProjList = actualResultData.Value as Models.ViewModels.Task[]; // Assert Assert.IsType <OkObjectResult>(actualResultData); Assert.Equal(StatusCodes.Status200OK, actualResultData.StatusCode); Assert.Equal(mockTasks, actualUserProjList); }
public void Test_For_Get_Task_By_Id() { // Arrange var testTaskId = 5; var testTaskVM = new Models.ViewModels.Task() { TaskId = testTaskId, TaskName = "TestTask1", Priority = 20, ProjectName = "TestProject100", OwnerFullName = "First Last", StartDate = DateTime.Today }; mockTaskRepo.Setup(repo => repo.GetById(testTaskId)).Returns(testTaskVM.AsDataModel()); // Act var actualTask = tasksLogicTest.GetTask(testTaskId); // Assert Assert.NotNull(actualTask); Assert.Equal(testTaskVM.TaskName, actualTask.TaskName); }
//[TestCase(Description = "Create a new Task", TestName = "Test Create Invalid Task returns BadRequest result")] public void Test_Post_New_Task_InValid() { // Arrange var newTestTask = new Models.ViewModels.Task() { TaskId = 25, TaskName = "Test New Task", Priority = 20 }; var validationCxt = new ValidationContext(newTestTask); var expectedErrMsg = "Invalid request information. Please verify the information entered."; mockController.ModelState.AddModelError("ProjectId", expectedErrMsg); mockTaskLogic.Setup(api => api.CreateTask(newTestTask)); // Act var actualStatus = mockController.Post(newTestTask); var actualResult = (BadRequestObjectResult)actualStatus; var actualModelState = (SerializableError)actualResult.Value; // Assert Assert.NotNull(actualResult); Assert.Contains("ProjectId", actualModelState.Keys); Assert.Equal(StatusCodes.Status400BadRequest, actualResult.StatusCode); Assert.Equal(expectedErrMsg, (actualModelState["ProjectId"] as string[])[0]); }
public Models.ViewModels.Task CreateTask(Models.ViewModels.Task task) { return(taskRepository.Create(task.AsDataModel()).AsViewModel()); }
public void Test_For_Converting_AsDataModel_List() { // Arrange var tasksList = new Models.ViewModels.Task[] { new Models.ViewModels.Task() { ProjectId = 1, ProjectName = "TestProject-1", TaskOwnerId = Guid.NewGuid(), Priority = 10, TaskId = 10, TaskName = "TestTask1" }, new Models.ViewModels.Task() { ProjectId = 2, ProjectName = "TestProject-2", TaskOwnerId = Guid.NewGuid(), Priority = 5, TaskId = 11, TaskName = "TestTask2" }, new Models.ViewModels.Task() { ProjectId = 3, ProjectName = "TestProject-3", TaskOwnerId = Guid.NewGuid(), Priority = 15, TaskId = 9, TaskName = "TestTask3" }, new Models.ViewModels.Task() { ProjectId = 4, ProjectName = "TestProject-4", TaskOwnerId = Guid.NewGuid(), Priority = 20, TaskId = 7, TaskName = "TestTask4" }, new Models.ViewModels.Task() { ProjectId = 5, ProjectName = "TestProject-5", TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "TestTask5", ParentTaskName = "TestParentTask1", ParentTaskId = 200 }, new Models.ViewModels.Task() { ProjectId = 6, ProjectName = "TestProject-5", TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "TestTask6", ParentTaskName = "TestParentTask1", ParentTaskId = 200 }, new Models.ViewModels.Task() { ProjectId = 7, ProjectName = "TestProject-5", TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "TestTask7", ParentTaskName = "TestParentTask2", ParentTaskId = 202 }, }.AsEnumerable(); var testDataModelList = new Models.DataModels.Task[] { new Models.DataModels.Task() { ProjectId = 1, Project = new Models.DataModels.Project() { ProjectName = "test-1", ProjectId = 1 }, TaskOwnerId = Guid.NewGuid(), Priority = 10, TaskId = 10, TaskName = "As-Is-Task1" }, new Models.DataModels.Task() { ProjectId = 2, Project = new Models.DataModels.Project() { ProjectName = "test-2", ProjectId = 2 }, TaskOwnerId = Guid.NewGuid(), Priority = 5, TaskId = 11, TaskName = "As-Is-Task2" }, new Models.DataModels.Task() { ProjectId = 3, Project = new Models.DataModels.Project() { ProjectName = "test-3", ProjectId = 3 }, TaskOwnerId = Guid.NewGuid(), Priority = 15, TaskId = 9, TaskName = "As-Is-Task3" }, new Models.DataModels.Task() { ProjectId = 4, Project = new Models.DataModels.Project() { ProjectName = "test-4", ProjectId = 4 }, TaskOwnerId = Guid.NewGuid(), Priority = 20, TaskId = 7, TaskName = "As-Is-Task4" }, new Models.DataModels.Task() { ProjectId = 5, Project = new Models.DataModels.Project() { ProjectName = "test-5", ProjectId = 5 }, TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "As-Is-Task5", }, new Models.DataModels.Task() { ProjectId = 6, Project = new Models.DataModels.Project() { ProjectName = "test-6", ProjectId = 6 }, TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "As-Is-Task6", }, new Models.DataModels.Task() { ProjectId = 7, Project = new Models.DataModels.Project() { ProjectName = "test-7", ProjectId = 7 }, TaskOwnerId = Guid.NewGuid(), Priority = 30, TaskId = 5, TaskName = "As-Is-Task7", }, }.AsEnumerable(); // Act var actualDataModelResult = tasksList.AsDataModel(testDataModelList); // Assert Assert.Equal(testDataModelList.Select(t => t.ProjectId), actualDataModelResult.Select(t => t.ProjectId)); Assert.Equal(tasksList.Select(t => t.TaskName), actualDataModelResult.Select(t => t.TaskName)); }