diff --git a/Kaizen.Test/Controllers/ActivitiesControllerTest.cs b/Kaizen.Test/Controllers/ActivitiesControllerTest.cs index eff72109..f2617314 100644 --- a/Kaizen.Test/Controllers/ActivitiesControllerTest.cs +++ b/Kaizen.Test/Controllers/ActivitiesControllerTest.cs @@ -32,10 +32,13 @@ public void SetUp() _activitiesController = new ActivitiesController(_activitiesRepository.Object, _clientsRepository.Object, _unitWork.Object, ServiceProvider.GetService()); + + SetUpActivitiesRepository(); + SetUpClientsRepository(); + SetUpUnitWork(); } - [Test] - public async Task GetActivities() + private void SetUpActivitiesRepository() { _activitiesRepository.Setup(r => r.GetAll()).Returns(new TestAsyncEnumerable(new List { @@ -61,37 +64,14 @@ public async Task GetActivities() } }).AsQueryable()); - OkObjectResult result = (await _activitiesController.GetActivities()).Result as OkObjectResult; - - Assert.NotNull(result); - Assert.NotNull(result.Value); - - Assert.IsInstanceOf>(result.Value); - - IEnumerable value = result.Value as IEnumerable; - Assert.NotNull(value); - Assert.AreEqual(2, value.Count()); - } - - [Test] - public async Task GetActivity() - { _activitiesRepository.Setup(r => r.FindByIdAsync(1)).ReturnsAsync(new Activity { Code = 1, - ClientId = "1007870922" + ClientId = "1007870922", + State = ActivityState.Pending }); + _activitiesRepository.Setup(r => r.FindByIdAsync(3)).ReturnsAsync((Activity)null); - ActionResult result = await _activitiesController.GetActivity(1); - - Assert.NotNull(result); - Assert.NotNull(result.Value); - Assert.AreEqual(1, result.Value.Code); - } - - [Test] - public async Task EmployeeActivities() - { _activitiesRepository.Setup(r => r.GetPendingEmployeeActivities("32752423", It.IsAny())) .ReturnsAsync(new[] { @@ -102,15 +82,6 @@ public async Task EmployeeActivities() } }); - OkObjectResult result = (await _activitiesController.EmployeeActivities("32752423", DateTime.Now)).Result as OkObjectResult; - - Assert.NotNull(result); - Assert.NotNull(result.Value); - } - - [Test] - public async Task ClientActivities() - { _activitiesRepository.Setup(r => r.GetPendingClientActivities("1007870922")).ReturnsAsync(new List { new() @@ -120,15 +91,6 @@ public async Task ClientActivities() } }); - OkObjectResult result = (await _activitiesController.ClientActivities("1007870922")).Result as OkObjectResult; - - Assert.NotNull(result); - Assert.NotNull(result.Value); - } - - [Test] - public async Task AppliedClientActivities() - { _activitiesRepository.Setup(r => r.GetAppliedClientActivities("1007870922")).ReturnsAsync(new List { new() @@ -138,51 +100,122 @@ public async Task AppliedClientActivities() } }); - OkObjectResult result = (await _activitiesController.AppliedClientActivities("1007870922")).Result as OkObjectResult; - - Assert.NotNull(result); - Assert.NotNull(result.Value); + _activitiesRepository.Setup(r => r.Insert(It.IsAny())).Verifiable(); + _activitiesRepository.Setup(r => r.Update(It.IsAny())).Verifiable(); } - [Test] - public async Task PutActivity() + private void SetUpClientsRepository() { - _activitiesRepository.Setup(r => r.FindByIdAsync(1)).ReturnsAsync(new Activity + _clientsRepository.Setup(r => r.GetClientWithUser("1007870922")).ReturnsAsync(new Client { - Code = 1, - ClientId = "1007870922", - State = ActivityState.Pending + Id = "1007870922", + FirstName = "Manolo", + User = new ApplicationUser + { + Id = "111-222", + UserName = "client" + } }); - _activitiesRepository.Setup(r => r.Update(It.IsAny())).Verifiable(); + _clientsRepository.Setup(r => r.GetClientWithUser("1007870930")).ReturnsAsync((Client)null); + } + + private void SetUpUnitWork() + { _unitWork.Setup(r => r.SaveAsync()).Returns(Task.CompletedTask); + } + + [Test] + public async Task Get_All_Activities() + { + OkObjectResult result = (await _activitiesController.GetActivities()).Result as OkObjectResult; + + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); + + Assert.IsInstanceOf>(result.Value); + + IEnumerable value = result.Value as IEnumerable; + Assert.IsNotNull(value); + Assert.AreEqual(2, value.Count()); + } + [Test] + public async Task Get_Existing_Activity() + { + ActionResult result = await _activitiesController.GetActivity(1); + + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); + Assert.AreEqual(1, result.Value.Code); + } + + [Test] + public async Task Get_Non_Existent_Activity() + { + ActionResult result = await _activitiesController.GetActivity(3); + + Assert.IsNotNull(result); + Assert.IsNull(result.Value); + Assert.IsInstanceOf(result.Result); + } + + [Test] + public async Task Get_Employee_Activities() + { + OkObjectResult result = (await _activitiesController.EmployeeActivities("32752423", DateTime.Now)).Result as OkObjectResult; + + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); + } + + [Test] + public async Task Get_Client_Activities() + { + OkObjectResult result = (await _activitiesController.ClientActivities("1007870922")).Result as OkObjectResult; + + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); + } + + [Test] + public async Task Get_Applied_Client_Activities() + { + OkObjectResult result = (await _activitiesController.AppliedClientActivities("1007870922")).Result as OkObjectResult; + + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); + } + + [Test] + public async Task Update_Existing_Activity() + { ActionResult result = await _activitiesController.PutActivity(1, new ActivityEditModel { State = ActivityState.Accepted, }); - Assert.NotNull(result); - Assert.NotNull(result.Value); + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); Assert.AreEqual(ActivityState.Accepted, result.Value.State); } [Test] - public async Task PostActivity() + public async Task Update_Non_Existent_Activity() { - _clientsRepository.Setup(r => r.GetClientWithUser("1007870922")).ReturnsAsync(new Client + var result = await _activitiesController.PutActivity(3, new ActivityEditModel { - Id = "1007870922", - FirstName = "Manolo", - User = new ApplicationUser - { - Id = "111-222", - UserName = "client" - } + State = ActivityState.Accepted, }); - _activitiesRepository.Setup(r => r.Insert(It.IsAny())).Verifiable(); - _unitWork.Setup(r => r.SaveAsync()).Returns(Task.CompletedTask); + Assert.IsNotNull(result); + Assert.IsNull(result.Value); + Assert.IsInstanceOf(result.Result); + } + + [Test] + public async Task Save_Activity_With_Existing_Client() + { ActionResult result = await _activitiesController.PostActivity(new ActivityInputModel { ClientId = "1007870922", @@ -198,25 +231,43 @@ public async Task PostActivity() } }); - Assert.NotNull(result); - Assert.NotNull(result.Value); + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); } [Test] - public async Task DeleteActivity() + public async Task Save_Activity_With_Non_Existing_Client() { - _activitiesRepository.Setup(r => r.FindByIdAsync(1)).ReturnsAsync(new Activity + var result = await _activitiesController.PostActivity(new ActivityInputModel { - Code = 1, - ClientId = "1007870922" + ClientId = "1007870930", + Date = DateTime.Now, + ServiceCodes = new List + { + "123", + "321" + }, + EmployeeCodes = new List + { + "1007870945" + } }); + + Assert.IsNotNull(result); + Assert.IsNull(result.Value); + Assert.IsInstanceOf(result.Result); + } + + [Test] + public async Task DeleteActivity() + { _activitiesRepository.Setup(r => r.Delete(It.IsAny())).Verifiable(); _unitWork.Setup(r => r.SaveAsync()).Returns(Task.CompletedTask); - var result = await _activitiesController.DeleteActivity(1); + ActionResult result = await _activitiesController.DeleteActivity(1); - Assert.NotNull(result); - Assert.NotNull(result.Value); + Assert.IsNotNull(result); + Assert.IsNotNull(result.Value); } } }