public void given_DocumentNotFound_exception_is_thrown_when_GetProcessData_controller_method_s_called_then_method_returns_404_status_code_with_correct_error_message() { //Arrange GetProcessDataRequest request = new GetProcessDataRequest { processRef = faker.Random.Guid().ToString() }; var errorResponse = $"Document with reference {request.processRef} was not found in the database"; _mockUsecase.Setup(x => x.ExecuteGet(request)).Throws <DocumentNotFound>(); _mockValidatorGet.Setup(x => x.Validate(request)).Returns(new FV.ValidationResult()); //Act var actualResponse = _processDataController.GetProcessData(request); var result = (ObjectResult)actualResponse; var resultContent = result.Value; //Assert Assert.NotNull(actualResponse); Assert.NotNull(result); Assert.NotNull(resultContent); Assert.AreEqual(errorResponse, resultContent); Assert.AreEqual(404, result.StatusCode); }
public void get_process_data_verify_gateway_calls_database_with_parameters(string processRef) { //arrange var request = new GetProcessDataRequest { processRef = processRef }; //act var result = processDataUseCase.ExecuteGet(request); //assert mockMatGateway.Verify(v => v.GetProcessData(It.Is <string>(i => i == processRef)), Times.Once); }
public void test_gateway_is_called() { //arrange string processRef = faker.Random.Guid().ToString(); var request = new GetProcessDataRequest { processRef = processRef }; //act processDataUseCase.ExecuteGet(request); mockMatGateway.Verify(x => x.GetProcessData(processRef)); }
public void check_gateway_returns_expected_response() { //arrange string processRef = faker.Random.Guid().ToString(); var request = new GetProcessDataRequest { processRef = processRef }; var response = new MatProcessData(); //act mockMatGateway.Setup(x => x.GetProcessData(processRef)).Returns(response); var result = processDataUseCase.ExecuteGet(request); //assert Assert.AreEqual(response, result.ProcessData); }
public void given_a_processRef_when_getprocessdata_method_is_called_it_then_calls_the_use_case_with_request_object_containing_that_processRef() { //arrange string processRef = faker.Random.Guid().ToString(); var request = new GetProcessDataRequest { processRef = processRef }; var response = new GetProcessDataResponse(request, new MatProcessData(), DateTime.Now); _mockUsecase.Setup(x => x.ExecuteGet(It.Is <GetProcessDataRequest>(i => i.processRef == processRef))).Returns(response); _mockValidatorGet.Setup(x => x.Validate(It.IsAny <GetProcessDataRequest>())).Returns(new FV.ValidationResult()); //act _processDataController.GetProcessData(request); //assert _mockUsecase.Verify(x => x.ExecuteGet((It.Is <GetProcessDataRequest>(i => i.processRef == processRef)))); }
public void given_an_invalid_processRef_when_GetProcessData_controller_method_is_called_then_it_returns_a_400_BadRequest_result() { //arrange int expectedStatusCode = 400; var request = new GetProcessDataRequest() { processRef = _faker.Random.Word() }; //processRef of invalid format //act var contollerResponse = _processDataController.GetProcessData(request); var controllerObjectResult = contollerResponse as ObjectResult; //assert Assert.NotNull(contollerResponse); Assert.IsInstanceOf <BadRequestObjectResult>(controllerObjectResult); Assert.AreEqual(expectedStatusCode, controllerObjectResult.StatusCode); }
public void given_a_valid_request_when_GetProcessData_controller_method_is_called_then_it_returns_a_response_that_resource_exists() { //arrange int expectedStatusCode = 200; GetProcessDataRequest requestObject = new GetProcessDataRequest(); _mockValidatorGet.Setup(v => v.Validate(It.IsAny <GetProcessDataRequest>())).Returns(new FV.ValidationResult()); //act IActionResult controllerResponse = _processDataController.GetProcessData(requestObject); var result = (ObjectResult)controllerResponse; var actualStatusCode = result.StatusCode; //Assert Assert.NotNull(controllerResponse); Assert.NotNull(result); Assert.AreEqual(expectedStatusCode, actualStatusCode); }
public void given_an_valid_and_nonexisting_processRef_when_GetProcessData_controller_method_is_called_then_it_returns_a_404_resource_NotFound_result() { //arrange int expectedStatusCode = 404; var request = new GetProcessDataRequest() { processRef = _faker.Random.Guid().ToString() }; //valid, but there's no instance of it in DB //act var contollerResponse = _processDataController.GetProcessData(request); var controllerObjectResult = contollerResponse as ObjectResult; //assert Assert.NotNull(contollerResponse); Assert.IsInstanceOf <NotFoundObjectResult>(controllerObjectResult); Assert.AreEqual(expectedStatusCode, controllerObjectResult.StatusCode); }
public void get_process_data_by_processref_returns_getprocessdataresponse_object() { //arrange string processRef = faker.Random.Guid().ToString(); var request = new GetProcessDataRequest { processRef = processRef }; var response = new MatProcessData(); //act mockMatGateway.Setup(x => x.GetProcessData(processRef)).Returns(response); var result = processDataUseCase.ExecuteGet(request); //assert Assert.IsInstanceOf <GetProcessDataResponse>(result); Assert.IsInstanceOf <GetProcessDataRequest>(result.Request); Assert.IsInstanceOf <MatProcessData>(result.ProcessData); Assert.AreEqual(processRef, result.Request.processRef); Assert.NotNull(result.GeneratedAt); Assert.IsInstanceOf <DateTime>(result.GeneratedAt); }
public void given_a_valid_and_existing_processRef_when_GetProcessData_controller_method_is_called_then_it_returns_a_200_Ok_result() { //arrange int expectedStatusCode = 200; var existingDocument = MatProcessDataHelper.CreateProcessDataObject(); _dbcontext.getCollection().InsertOne(BsonDocument.Parse(JsonConvert.SerializeObject(existingDocument))); // insert expected document, which we expect the gateway to retrieve var request = new GetProcessDataRequest() { processRef = existingDocument.Id }; //valid, and existing //act var contollerResponse = _processDataController.GetProcessData(request); var controllerObjectResult = contollerResponse as ObjectResult; //assert Assert.NotNull(contollerResponse); Assert.IsInstanceOf <OkObjectResult>(controllerObjectResult); Assert.AreEqual(expectedStatusCode, controllerObjectResult.StatusCode); }
public void given_an_unexpected_exception_thrown_when_GetProcessData_controller_method_is_called_then_controller_returns_500_status_code_response() { //Arrange GetProcessDataRequest request = new GetProcessDataRequest() { processRef = faker.Random.Guid().ToString() }; var randomExpectedError = ErrorThrowerHelper.GenerateError(); _mockUsecase.Setup(x => x.ExecuteGet(It.IsAny <GetProcessDataRequest>())).Throws(randomExpectedError); _mockValidatorGet.Setup(x => x.Validate(It.IsAny <GetProcessDataRequest>())).Returns(new FV.ValidationResult()); //Act var actualResult = _processDataController.GetProcessData(request); var result = (ObjectResult)actualResult; //Assert Assert.NotNull(actualResult); Assert.NotNull(result); Assert.AreEqual(500, result.StatusCode); }
public void given_GetProcessData_controller_method_call_when_request_validation_fails_then_the_controller_returns_correct_badRequest_response() { //arrange int errorCount = faker.Random.Int(1, 10); //simulate from 1 to 10 validation errors (triangulation). var validationErrorList = new List <ValidationFailure>(); //this list will be used as constructor argument for 'ValidationResult'. for (int i = errorCount; i > 0; i--) { validationErrorList.Add(new ValidationFailure(faker.Random.Word(), faker.Random.Word())); } //generate from 1 to 10 fake validation errors. Single line for-loop so that it wouldn't distract from what's key in this test. var fakeValidationResult = new FV.ValidationResult(validationErrorList); //Need to create ValidationResult so that I could setup Validator mock to return it upon '.Validate()' call. Also this is the only place where it's possible to manipulate the validation result - You can only make the validation result invalid by inserting a list of validation errors as a parameter through a constructor. The boolean '.IsValid' comes from expression 'IsValid => Errors.Count == 0;', so it can't be set manually. _mockValidatorGet.Setup(v => v.Validate(It.IsAny <GetProcessDataRequest>())).Returns(fakeValidationResult); string processRef = faker.Random.String(); GetProcessDataRequest request = new GetProcessDataRequest() { processRef = processRef }; //act var controllerResponse = _processDataController.GetProcessData(request); var result = (ObjectResult)controllerResponse; var resultContents = (IList <ValidationFailure>)result.Value; //assert Assert.NotNull(controllerResponse); Assert.IsInstanceOf <BadRequestObjectResult>(result); // should be bad request, since validation fails. Assert.NotNull(result); Assert.IsInstanceOf <IList <ValidationFailure> >(resultContents); Assert.NotNull(resultContents); Assert.AreEqual(errorCount, resultContents.Count); // there should be 1 validation failure coming from the invalid guid Assert.AreEqual(400, result.StatusCode); // expecting bad request result }
public void given_a_valid_and_existing_processRef_when_GetProcessData_controller_method_is_called_then_it_returns_matching_document_from_the_database() { //arrange var expectedDocument = MatProcessDataHelper.CreateProcessDataObject(); string expectedProcessRef = expectedDocument.Id; _dbcontext.getCollection().InsertOne(BsonDocument.Parse(JsonConvert.SerializeObject(expectedDocument))); // insert expected document, which we expect the gateway to retrieve GetProcessDataRequest request = new GetProcessDataRequest() { processRef = expectedProcessRef }; //act var response = _processDataController.GetProcessData(request); var okResult = (OkObjectResult)response; var getProcessDataResponse = okResult.Value as GetProcessDataResponse; var retrievedDocument = getProcessDataResponse.ProcessData; //assert Assert.IsInstanceOf <MatProcessData>(retrievedDocument); Assert.NotNull(getProcessDataResponse.ProcessData); Assert.NotNull(getProcessDataResponse.ProcessData.ProcessType); Assert.That( expectedProcessRef == retrievedDocument.Id && expectedDocument.ProcessType.name == retrievedDocument.ProcessType.name && expectedDocument.ProcessType.value == retrievedDocument.ProcessType.value && expectedDocument.DateCreated == retrievedDocument.DateCreated && expectedDocument.DateLastModified == retrievedDocument.DateLastModified && expectedDocument.DateCompleted == retrievedDocument.DateCompleted && expectedDocument.ProcessDataAvailable == retrievedDocument.ProcessDataAvailable && expectedDocument.ProcessDataSchemaVersion == retrievedDocument.ProcessDataSchemaVersion && expectedDocument.ProcessStage == retrievedDocument.ProcessStage && expectedDocument.LinkedProcessId == retrievedDocument.LinkedProcessId ); }
public IActionResult GetProcessData([FromRoute] GetProcessDataRequest request) { _logger.LogInformation($"Get ProcessData request for process ID {request.processRef}"); var isRequestValid = _getValidator.Validate(request); if (isRequestValid.IsValid) { try { var result = _processDataUsecase.ExecuteGet(request); return(Ok(result)); } catch (DocumentNotFound ex) { return(NotFound($"Document with reference {request.processRef} was not found in the database")); } catch (Exception ex) { return(StatusCode(500, "An error has occured while processing the request - " + ex.Message + " " + ex.InnerException)); } } return(BadRequest(isRequestValid.Errors)); }
public void given_a_processRef_when_getprocessdata_method_is_called_the_controller_returns_correct_json_response() { //arange string processRef = faker.Random.Guid().ToString(); var request = new GetProcessDataRequest() { processRef = processRef }; var response = new GetProcessDataResponse(request, new MatProcessData(), DateTime.Now); _mockUsecase.Setup(x => x.ExecuteGet(It.Is <GetProcessDataRequest>(i => i.processRef == processRef))).Returns(response); //act var actualResponse = _processDataController.GetProcessData(request); var okResult = (OkObjectResult)actualResponse; var resultContent = (GetProcessDataResponse)okResult.Value; //assert Assert.NotNull(actualResponse); Assert.NotNull(okResult); Assert.IsInstanceOf <GetProcessDataResponse>(resultContent); Assert.NotNull(resultContent); Assert.AreEqual(JsonConvert.SerializeObject(response), JsonConvert.SerializeObject(resultContent)); Assert.AreEqual(200, okResult.StatusCode); }
public GetProcessDataResponse(GetProcessDataRequest request, MatProcessData processData, DateTime generatedAt) { Request = request; GeneratedAt = generatedAt; ProcessData = processData; }
public GetProcessDataResponse ExecuteGet(GetProcessDataRequest request) { var gatewayResult = _processDataGateway.GetProcessData(request.processRef); return(new GetProcessDataResponse(request, gatewayResult, DateTime.Now)); }