public async Task TestInteractor_Get() { // ARRANGE // Authorization Service Mocking var authServiceMock = new Mock <IAuthService>(); authServiceMock.Setup(d => d.IsAuthenticated()) .Returns(true); authServiceMock.Setup(d => d.IsAuthorized(AuthorizationRole.Admin)) .Returns(true); // Repository Mocking (Motorcycle motorcycle, _) = Motorcycle.NewMotorcycle("Honda", "Shadow", 2006, "01234567890123456"); motorcycle.Id = 1; var repositoryMock = new Mock <IMotorcycleRepository>(); // Repository Get Mocking var tcs = new TaskCompletionSource <(Motorcycle, OperationStatus, IError)>(); tcs.SetResult((motorcycle, OperationStatus.Ok, null)); repositoryMock.Setup(d => d.FetchByIdAsync(It.IsAny <long>())) .Returns(tcs.Task); var(request, _) = GetMotorcycleRequest.NewGetMotorcycleRequest(motorcycle.Id); (GetMotorcycleInteractor interactor, _) = GetMotorcycleInteractor.NewGetMotorcycleInteractor(repositoryMock.Object, authServiceMock.Object); // ACT (GetMotorcycleResponse response, _) = await interactor.HandleAsync(request); // ASSERT Assert.True(response.Motorcycle.Id == motorcycle.Id); }
public async Task TestInteractor_NotExist() { // ARRANGE // Authorization Service Mocking var authServiceMock = new Mock <IAuthService>(); authServiceMock.Setup(d => d.IsAuthenticated()) .Returns(true); authServiceMock.Setup(d => d.IsAuthorized(AuthorizationRole.Admin)) .Returns(true); // Repository Mocking var repositoryMock = new Mock <IMotorcycleRepository>(); // Repository Get Mocking var tcs = new TaskCompletionSource <(Motorcycle, OperationStatus, IError)>(); tcs.SetResult((null, OperationStatus.NotFound, new Error("Not Found"))); repositoryMock.Setup(d => d.FetchByIdAsync(It.IsAny <long>())) .Returns(tcs.Task); var(request, _) = GetMotorcycleRequest.NewGetMotorcycleRequest(123); (GetMotorcycleInteractor interactor, _) = GetMotorcycleInteractor.NewGetMotorcycleInteractor(repositoryMock.Object, authServiceMock.Object); // ACT (_, IError error) = await interactor.HandleAsync(request); // ASSERT Assert.NotNull(error); }
public async Task TestInteractor_NotAuthenticated() { // ARRANGE var authServiceMock = new Mock <IAuthService>(); authServiceMock.Setup(d => d.IsAuthenticated()).Returns(false); var repositoryMock = new Mock <IMotorcycleRepository>(); var(request, _) = GetMotorcycleRequest.NewGetMotorcycleRequest(123); (GetMotorcycleInteractor interactor, _) = GetMotorcycleInteractor.NewGetMotorcycleInteractor(repositoryMock.Object, authServiceMock.Object); // ACT (_, IError error) = await interactor.HandleAsync(request); // ASSERT Assert.NotNull(error); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Handle processes the request message and generates the response message. It is performing /// the use case. The request message is a dto containing the required data for completing the /// use case. /// </summary> /// /// <param name="request"> The request. </param> /// /// <returns> /// On success, the method returns the (response message, nil), otherwise (nil, error). /// </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// public async Task <(GetMotorcycleResponse response, IError error)> HandleAsync(GetMotorcycleRequest request) { // Verify that the user has been properly authenticated. if (!_authService.IsAuthenticated()) { return(GetMotorcycleResponse.NewGetMotorcycleResponse(null, OperationStatus.NotAuthenticated, new Error("Get operation failed due to not being authenticated."))); } // Verify that the user has the necessary authorizations. if (!_authService.IsAuthorized(AuthorizationRole.Admin)) { return(GetMotorcycleResponse.NewGetMotorcycleResponse(null, OperationStatus.NotAuthorized, new Error("Get operation failed due to not being authorized, so please contact your system administrator."))); } // Get the motorcycle with Id from the repository. (Motorcycle motorcycle, OperationStatus status, IError error) = await _motorcycleRepository.FetchByIdAsync(request.Id); if (error != null) { return(GetMotorcycleResponse.NewGetMotorcycleResponse(null, status, error)); } // Save the changes. (status, error) = await _motorcycleRepository.SaveAsync(); if (error != null) { return(GetMotorcycleResponse.NewGetMotorcycleResponse(null, status, error)); } // Return the successful response message. return(GetMotorcycleResponse.NewGetMotorcycleResponse(motorcycle, OperationStatus.Ok, null)); }