コード例 #1
0
        public void given_postInitialProcessDocumentRequest_object_when_createProcessDataObject_factory_method_is_called_it_returns_correctly_populated_processData_domain_object()
        {
            //arrange
            PostInitialProcessDocumentRequest requestObject = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            //act
            var domainObject = ProcessDataFactory.CreateProcessDataObject(requestObject);

            //assert
            Assert.NotNull(domainObject);
            Assert.IsInstanceOf <MatProcessData>(domainObject);
            Assert.AreEqual(requestObject.processRef, domainObject.Id);
            Assert.AreEqual(requestObject.processType.value, domainObject.ProcessType.value);
            Assert.AreEqual(requestObject.processType.name, domainObject.ProcessType.name);

            Assert.NotNull(domainObject.DateCreated);
            Assert.NotNull(domainObject.DateLastModified);
            Assert.AreEqual(domainObject.DateCreated, domainObject.DateLastModified); //DateLastModified should be equal to the date of creation because the test is for an object that is created now.
            Assert.AreEqual(DateTime.MinValue, domainObject.DateCompleted);

            Assert.False(domainObject.ProcessDataAvailable);
            Assert.AreEqual(requestObject.processDataSchemaVersion, domainObject.ProcessDataSchemaVersion);
            Assert.AreEqual("Not completed", domainObject.ProcessStage); //it's not confirmed yet whether it's going to be int or string
            Assert.Null(domainObject.LinkedProcessId);

            // Assert.AreEqual(new { }, domainObject.PreProcessData); //Causes error --> Expected: <{ }> (<>f__AnonymousType0); But was:  <{ }> (<> f__AnonymousType0)
            Assert.AreEqual(0, domainObject.PreProcessData.GetType().GetProperties().Count()); // using reflections, because the above won't work
            Assert.AreEqual(0, domainObject.ProcessData.GetType().GetProperties().Count());
            Assert.AreEqual(0, domainObject.PostProcessData.GetType().GetProperties().Count());
        }
コード例 #2
0
        public void given_a_valid_postInitialProcessDocumentRequest_when_postInitialProcessDocument_controller_method_is_called_then_the_controller_returns_correct_json_response()
        {
            //arange
            PostInitialProcessDocumentRequest requestObject = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            _mockValidatorPost.Setup(v => v.Validate(It.IsAny <PostInitialProcessDocumentRequest>())).Returns(new FV.ValidationResult()); //set up mock validation to return Validation with no errors.

            var expectedResponse = new PostInitialProcessDocumentResponse(requestObject, requestObject.processRef, DateTime.Now);

            _mockUsecase.Setup(g => g.ExecutePost(It.IsAny <PostInitialProcessDocumentRequest>())).Returns(expectedResponse);

            ////act
            var actualResponse = _processDataController.PostInitialProcessDocument(requestObject);
            var result         = (ObjectResult)actualResponse;
            var resultContent  = (PostInitialProcessDocumentResponse)result.Value;

            ////assert
            Assert.NotNull(actualResponse);
            Assert.NotNull(result);
            Assert.IsInstanceOf <PostInitialProcessDocumentResponse>(resultContent);
            Assert.NotNull(resultContent);
            Assert.AreEqual(expectedResponse.ProcessRef, resultContent.ProcessRef);
            Assert.AreEqual(expectedResponse.GeneratedAt, resultContent.GeneratedAt);
            Assert.AreEqual(expectedResponse.Request.processRef, resultContent.Request.processRef);
            Assert.AreEqual(JsonConvert.SerializeObject(expectedResponse), JsonConvert.SerializeObject(resultContent));
        }
        public void postInitialProcessDocument_controller_method_end_to_end_test_with_a_invalid_request() //testing, when validation fails
        {
            //arrange
            PostInitialProcessDocumentRequest requestObject = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();
            string nonsenseGuid = _faker.Random.Word().ToString(); // invalid Guid (not even a guid) - this should trip the validation.

            requestObject.processRef = nonsenseGuid;

            var postValidator  = new PostInitialProcessDocumentRequestValidator();
            var isRequestValid = postValidator.Validate(requestObject);
            var errors         = isRequestValid.Errors;

            var expectedResponse = new BadRequestObjectResult(errors);

            //act
            var controllerResponse = _processDataController.PostInitialProcessDocument(requestObject);
            var actualResult       = (ObjectResult)controllerResponse;
            var actualContents     = (IList <ValidationFailure>)actualResult.Value;

            //assert
            Assert.NotNull(controllerResponse);
            Assert.IsInstanceOf <BadRequestObjectResult>(actualResult); // should be bad request, since validation fails.
            Assert.NotNull(actualResult);
            Assert.IsInstanceOf <IList <ValidationFailure> >(actualContents);
            Assert.NotNull(actualContents);

            Assert.AreEqual(1, actualContents.Count);      // there should be 1 validation failure coming from the invalid guid
            Assert.AreEqual(400, actualResult.StatusCode); // expecting bad request result

            //the following simply proves that validation errors get wraped up properly.
            Assert.AreEqual(((IList <ValidationFailure>)expectedResponse.Value)[0].ErrorMessage, actualContents[0].ErrorMessage);
        }
        public void postInitialProcessDocument_controller_method_end_to_end_test_with_a_conflict_exception_in_the_gateway() //testing exception handling. in this case testing how our custom exception bubbles up from gateway. chose conflict exception to test 2 things at once - exception handling + custom exception wrapping.
        {
            //arrange
            string conflictGuid = _faker.Random.Guid().ToString(); //This is the conflicting Guid, that will be repeated accross 2 request objects.

            PostInitialProcessDocumentRequest requestObject1 = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            requestObject1.processRef = conflictGuid;                          // set conflict Guid.
            _processDataController.PostInitialProcessDocument(requestObject1); //The process document with conflict Guid identifier has been inserted into the database.

            PostInitialProcessDocumentRequest requestObject2 = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            requestObject2.processRef = conflictGuid; // set conflict Guid.

            //act
            var controllerResponse = _processDataController.PostInitialProcessDocument(requestObject2); //The attempt to insert another document into the database with the already existing identifier (conflict guid). Conflict happens.
            var actualResult       = (ObjectResult)controllerResponse;
            var errorMessage       = (string)actualResult.Value;

            //assert
            Assert.NotNull(controllerResponse);
            Assert.IsInstanceOf <ConflictObjectResult>(actualResult);
            Assert.NotNull(actualResult);
            Assert.IsInstanceOf <string>(errorMessage);
            Assert.NotNull(errorMessage);
            Assert.IsNotEmpty(errorMessage);

            Assert.AreEqual(409, actualResult.StatusCode);
            Assert.AreNotEqual("An error inserting an object with duplicate key has occured - ", errorMessage); //If they're equal, then it means that empty exception has been appended to this sentense.
            //There is no good way to check the error message, since it would require to cause the exception manually through writing error handling implementation in the test.
        }
        public void postInitialProcessDocument_controller_method_end_to_end_test_with_a_valid_request() //testing regular response
        {
            //arrange
            PostInitialProcessDocumentRequest requestObject = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();
            var expectedResponse = new PostInitialProcessDocumentResponse(requestObject, requestObject.processRef, DateTime.Now);

            //act
            var controllerResponse = _processDataController.PostInitialProcessDocument(requestObject);
            var result             = (ObjectResult)controllerResponse;
            var resultContent      = (PostInitialProcessDocumentResponse)result.Value;

            //assert
            //check if the doc got inserted
            var documentFromDB = BsonSerializer.Deserialize <MatProcessData>(_dbcontext.getCollection().FindAsync(Builders <BsonDocument> .Filter.Eq("_id", requestObject.processRef)).Result.FirstOrDefault());

            Assert.NotNull(documentFromDB);
            Assert.AreEqual(requestObject.processRef, documentFromDB.Id);
            Assert.NotNull(documentFromDB.ProcessType);
            Assert.AreEqual(requestObject.processType.value, documentFromDB.ProcessType.value);
            Assert.AreEqual(requestObject.processType.name, documentFromDB.ProcessType.name);
            Assert.AreEqual(requestObject.processDataSchemaVersion, documentFromDB.ProcessDataSchemaVersion);

            //check the controller response
            Assert.NotNull(controllerResponse);
            Assert.IsInstanceOf <ObjectResult>(result);
            Assert.NotNull(result);
            Assert.IsInstanceOf <PostInitialProcessDocumentResponse>(result.Value);
            Assert.NotNull(result.Value);
            Assert.AreEqual(expectedResponse.ProcessRef, resultContent.ProcessRef);
            Assert.AreEqual(expectedResponse.Request.processRef, resultContent.Request.processRef);

            //check status code
            Assert.AreEqual(201, result.StatusCode);
        }
コード例 #6
0
        public void given_the_double_insert_of_matProcessData_domain_object_when_postInitialProcessDocument_gateway_method_is_called_then_conflict_exception_is_thrown()
        {
            //arrange
            MatProcessData domainObject = ProcessDataFactory.CreateProcessDataObject(MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject());

            //act
            string response_Id = processDataGateway.PostInitialProcessDocument(domainObject);                     //first insert.

            Assert.Throws <ConflictException>(() => processDataGateway.PostInitialProcessDocument(domainObject)); //the second document insertion happends, while asserting.
        }
コード例 #7
0
        public void given_the_matProcessData_domain_object_when_postInitialProcessDocument_gateway_method_is_called_then_it_returns_id_of_the_inserted_document()
        {
            //arrange
            MatProcessData domainObject = ProcessDataFactory.CreateProcessDataObject(MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject());

            //act
            string response_Id = processDataGateway.PostInitialProcessDocument(domainObject);

            //assert
            Assert.AreEqual(domainObject.Id, response_Id);
        }
コード例 #8
0
        public void given_a_postInitialProcessDocumentRequest_when_postInitialProcessDocument_controller_method_is_called_it_then_calls_the_use_case_while_passing_the_request_object_to_it()
        {
            //arrange
            PostInitialProcessDocumentRequest requestObject = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            _mockValidatorPost.Setup(v => v.Validate(It.IsAny <PostInitialProcessDocumentRequest>())).Returns(new FV.ValidationResult()); //set up mock validation to return Validation with no errors.
            _mockUsecase.Setup(g => g.ExecutePost(It.IsAny <PostInitialProcessDocumentRequest>()));

            //act
            _processDataController.PostInitialProcessDocument(requestObject);

            //assert
            _mockUsecase.Verify(x => x.ExecutePost(
                                    It.Is <PostInitialProcessDocumentRequest>(req => req.processRef == requestObject.processRef)
                                    ), Times.Once);
        }
コード例 #9
0
        public void given_the_matProcessData_domain_object_when_postInitialProcessDocument_gateway_method_is_called_then_the_parsed_object_gets_added_into_the_database()
        {
            //arrange
            MatProcessData domainObject = ProcessDataFactory.CreateProcessDataObject(MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject());

            //act
            processDataGateway.PostInitialProcessDocument(domainObject);

            //assert
            var documentFromDB = BsonSerializer.Deserialize <MatProcessData>(collection.FindAsync(Builders <BsonDocument> .Filter.Eq("_id", domainObject.Id)).Result.FirstOrDefault());

            Assert.AreEqual(domainObject.Id, documentFromDB.Id);
            Assert.AreEqual(domainObject.ProcessType.value, documentFromDB.ProcessType.value);
            Assert.AreEqual(domainObject.ProcessType.name, documentFromDB.ProcessType.name);
            Assert.AreEqual(domainObject.ProcessDataSchemaVersion, documentFromDB.ProcessDataSchemaVersion);
        }
コード例 #10
0
        public void given_a_valid_request_when_postinitialprocessdocument_controller_method_is_called_then_it_returns_a_response_that_resource_was_created() //temporary test until actual implementation will be worked on.
        {
            //arrange
            int expectedStatusCode = 201;
            PostInitialProcessDocumentRequest requestObject = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            _mockValidatorPost.Setup(v => v.Validate(It.IsAny <PostInitialProcessDocumentRequest>())).Returns(new FV.ValidationResult()); //set up mock validation to return Validation with no errors.

            //act
            IActionResult controllerResponse = _processDataController.PostInitialProcessDocument(requestObject);
            var           result             = (ObjectResult)controllerResponse;
            var           actualStatusCode   = result.StatusCode;

            //assert
            Assert.NotNull(controllerResponse);
            Assert.NotNull(result);
            Assert.AreEqual(expectedStatusCode, actualStatusCode);
        }
コード例 #11
0
        public void given_a_request_when_postInitialProcessDocument_controller_method_is_called_then_it_calls_the_validator_with_that_request()
        {
            //arrange
            PostInitialProcessDocumentRequest request = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            _mockValidatorPost.Setup(v => v.Validate(It.IsAny <PostInitialProcessDocumentRequest>())).Returns(new FV.ValidationResult()); //set up mock validation to return Validation with no errors.
            _mockUsecase.Setup(g => g.ExecutePost(It.IsAny <PostInitialProcessDocumentRequest>()));

            //act
            _processDataController.PostInitialProcessDocument(request);

            //assert
            _mockValidatorPost.Verify(v => v.Validate(It.Is <PostInitialProcessDocumentRequest>(i =>
                                                                                                i.processRef == request.processRef &&
                                                                                                i.processType.name == request.processType.name &&
                                                                                                i.processType.value == request.processType.value &&
                                                                                                i.processDataSchemaVersion == request.processDataSchemaVersion
                                                                                                )), Times.Once);
        }
コード例 #12
0
        public void given_the_requestObject_when_executePost_usecase_method_is_called_then_the_gateway_is_called_with_factory_output() // this should be 2 tests really, one to see if factory gets called, and the other to see if gateway is called, but due to using static factory method this becomes impossible to separate.
        {
            //arrange
            PostInitialProcessDocumentRequest requestObject = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();
            MatProcessData domainObject = ProcessDataFactory.CreateProcessDataObject(requestObject);

            mockMatGateway.Setup(g => g.PostInitialProcessDocument(It.IsAny <MatProcessData>())).Returns((MatProcessData dobj) => dobj.Id);
            //act
            processDataUseCase.ExecutePost(requestObject);

            //assert
            mockMatGateway.Verify(g => g.PostInitialProcessDocument(It.Is <MatProcessData>(d =>
                                                                                           d.ProcessType.name == requestObject.processType.name && //checking whether properties that have to be transfered from request object to domain object are present
                                                                                           d.Id == requestObject.processRef &&
                                                                                           d.ProcessType.value == requestObject.processType.value &&
                                                                                           d.ProcessDataSchemaVersion == requestObject.processDataSchemaVersion &&
                                                                                           d.DateCompleted == DateTime.MinValue && //checking whether some of the properties that factory has to generate itself are present.
                                                                                           d.ProcessStage == "Not completed"
                                                                                           )), Times.Once);
            // This checks whether the usecase calls the gateway with the output of the factory method.
        }
コード例 #13
0
        public void given_the_requestObject_when_executePost_usecase_method_is_called_then_it_wraps_gateway_output_into_postInitialDocumentResponse_object()
        {
            //arrange
            PostInitialProcessDocumentRequest requestObject = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();
            MatProcessData domainObject = ProcessDataFactory.CreateProcessDataObject(requestObject);

            mockMatGateway.Setup(g => g.PostInitialProcessDocument(It.IsAny <MatProcessData>())).Returns((MatProcessData dobj) => dobj.Id);

            //act
            var usecaseResponse = processDataUseCase.ExecutePost(requestObject);

            //assert
            Assert.IsInstanceOf <PostInitialProcessDocumentResponse>(usecaseResponse);

            Assert.IsInstanceOf <PostInitialProcessDocumentRequest>(usecaseResponse.Request);
            Assert.IsInstanceOf <string>(usecaseResponse.ProcessRef);
            Assert.IsInstanceOf <DateTime>(usecaseResponse.GeneratedAt);

            Assert.AreEqual(requestObject.processRef, usecaseResponse.Request.processRef);
            Assert.AreEqual(requestObject.processRef, usecaseResponse.ProcessRef);
            Assert.NotNull(usecaseResponse.GeneratedAt);
            Assert.AreNotEqual(DateTime.MinValue, usecaseResponse.GeneratedAt);
        }
コード例 #14
0
        public void given_request_with_unique_key_that_already_exists_when_postInitialProcessDocument_controller_method_is_called_then_upon_catching_custom_conflict_exception_the_correct_conflict_result_is_returned() //testing exception handling. in this case testing how our custom exception bubbles up from gateway. chose conflict exception to test 2 things at once - exception handling + custom exception wrapping.
        {
            //arrange
            //setup mocks:
            _mockValidatorPost.Setup(v => v.Validate(It.IsAny <PostInitialProcessDocumentRequest>())).Returns(new FV.ValidationResult()); //set up mock validation to return Validation with no errors.
            _mockUsecase.Setup(g => g.ExecutePost(It.IsAny <PostInitialProcessDocumentRequest>())).Returns(() => throw new ConflictException(faker.Random.Word(), new Exception(faker.Random.Word())));

            //setup double insert:
            string conflictGuid = faker.Random.Guid().ToString(); //This is the conflicting Guid, that will be repeated accross 2 request objects.

            PostInitialProcessDocumentRequest requestObject1 = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            requestObject1.processRef = conflictGuid;                          // set conflict Guid.
            _processDataController.PostInitialProcessDocument(requestObject1); //The process document with conflict Guid identifier has been inserted into the database.

            PostInitialProcessDocumentRequest requestObject2 = MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject();

            requestObject2.processRef = conflictGuid; // set conflict Guid.

            //act
            var controllerResponse = _processDataController.PostInitialProcessDocument(requestObject2); //The attempt to insert another document into the database with the already existing identifier (conflict guid). Conflict happens.
            var actualResult       = (ObjectResult)controllerResponse;
            var errorMessage       = (string)actualResult.Value;

            //assert
            Assert.NotNull(controllerResponse);
            Assert.IsInstanceOf <ConflictObjectResult>(actualResult);
            Assert.NotNull(actualResult);
            Assert.IsInstanceOf <string>(errorMessage);
            Assert.NotNull(errorMessage);
            Assert.IsNotEmpty(errorMessage);

            Assert.AreEqual(409, actualResult.StatusCode);
            Assert.AreNotEqual("An error inserting an object with duplicate key has occured - ", errorMessage); //If they're equal, then it means that empty exception has been appended to this sentense.
            //There is no good way to check the error message, since it would require to cause the exception manually through writing error handling implementation in the test.
        }
コード例 #15
0
        public void given_the_matProcessData_domain_object_when_postInitialProcessDocument_gateway_method_is_called_then_the_number_of_documents_in_the_database_increases_by_one() //test that checks whether the db doesn't get cleared or overwritten somehow upon insertion
        {
            //arrange
            var unclearedDocumentCount = collection.CountDocuments(Builders <BsonDocument> .Filter.Empty); //did some testing around this, seems like the database doesn't get cleared after every test. Depending on the ordering, it might not actually be empty at the start of this test. When this is unaccounted for, it makes this test fail.

            //pre-insert between 0 and 7 documents into database, so that it wouldn't be necessarily empty (triangulation)
            int preInsertedDocumentCount = _faker.Random.Int(0, 7);

            for (int i = preInsertedDocumentCount; i > 0; i--)
            {
                MatProcessData preInsertedDomainObject = ProcessDataFactory.CreateProcessDataObject(MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject());
                collection.InsertOne(BsonDocument.Parse(JsonConvert.SerializeObject(preInsertedDomainObject)));
            }

            //a new object that will be inserted upon gateway call
            MatProcessData toBeInsertedDomainObject = ProcessDataFactory.CreateProcessDataObject(MatProcessDataHelper.CreatePostInitialProcessDocumentRequestObject());

            //act
            processDataGateway.PostInitialProcessDocument(toBeInsertedDomainObject);

            //assert
            var startingDocumentCount = unclearedDocumentCount + preInsertedDocumentCount;

            Assert.AreEqual(startingDocumentCount + 1, collection.CountDocuments(Builders <BsonDocument> .Filter.Empty));
        }