public void WhenStartDateIsInThePastReturnsItIsValid()
        {
            var badRequest = CaseStatusHelper.CreateCaseStatusRequest(startDate: DateTime.Today.AddDays(-2));

            var response = _createCaseStatusRequestValidator.Validate(badRequest);

            response.IsValid.Should().BeTrue();
        }
        public void WhenRequestIsValidReturnsItIsValid()
        {
            var validRequest = CaseStatusHelper.CreateCaseStatusRequest();

            var response = _createCaseStatusRequestValidator.Validate(validRequest);

            response.IsValid.Should().BeTrue();
        }
        public void WhenCreatedByIsNotAnEmailAddressReturnsErrorWithMessage()
        {
            var badRequest = CaseStatusHelper.CreateCaseStatusRequest(createdBy: "foobar");

            var response = _createCaseStatusRequestValidator.Validate(badRequest);

            response.IsValid.Should().BeFalse();
            response.Errors.Should().Contain(e => e.ErrorMessage == "'createdBy' must be an email address.");
        }
        public void WhenNotesIsAbove1000CharactersReturnsErrorWithMessage()
        {
            var badRequest = CaseStatusHelper.CreateCaseStatusRequest(notes: _faker.Random.String(1001));

            var response = _createCaseStatusRequestValidator.Validate(badRequest);

            response.IsValid.Should().BeFalse();
            response.Errors.Should().Contain(e => e.ErrorMessage == "'notes' must be less than or equal to 1,000 characters.");
        }
        public void WhenStartDateIsInTheFutureReturnsErrorWithMessage()
        {
            var badRequest = CaseStatusHelper.CreateCaseStatusRequest(startDate: DateTime.Today.AddDays(1));

            var response = _createCaseStatusRequestValidator.Validate(badRequest);

            response.IsValid.Should().BeFalse();
            response.Errors.Should().Contain(e => e.ErrorMessage == "'start_date' must be today or in the past");
        }
        public void CallsCaseStatusUseCaseToCreateCaseStatus()
        {
            _mockCaseStatusesUseCase.Setup(x => x.ExecutePost(It.IsAny <CreateCaseStatusRequest>()));
            var request = CaseStatusHelper.CreateCaseStatusRequest();

            _caseStatusController.CreateCaseStatus(request);

            _mockCaseStatusesUseCase.Verify(x => x.ExecutePost(request));
        }
        public void WhenTypeIsNotCPorCINorLACReturnsErrorWithMessage()
        {
            var validRequest = CaseStatusHelper.CreateCaseStatusRequest(type: "random");

            var response = _createCaseStatusRequestValidator.Validate(validRequest);

            response.IsValid.Should().BeFalse();
            response.Errors.Should().Contain(e => e.ErrorMessage == "'type' must be CIN, CP or LAC.");
        }
        public void WhenTypeIsLACAndTheAnswerCountIsOneReturnsItIsValid()
        {
            var answers = CaseStatusHelper.CreateCaseStatusRequestAnswers(min: 2, max: 2);

            var request = CaseStatusHelper.CreateCaseStatusRequest(answers: answers);

            var response = _createCaseStatusRequestValidator.Validate(request);

            response.IsValid.Should().BeTrue();
        }
        public void WhenStartDateIsNotProvidedReturnsErrorMessage()
        {
            var badRequest = CaseStatusHelper.CreateCaseStatusRequest();

            badRequest.StartDate = DateTime.MinValue;

            var response = _createCaseStatusRequestValidator.Validate(badRequest);

            response.IsValid.Should().BeFalse();
            response.Errors.Should().Contain(e => e.ErrorMessage == "'startDate' must be provided.");
        }
        public void WhenTypeIsLACAndTheAnswerCountIsNotTwoReturnsErrorWithMessage(int answerCount)
        {
            var answers = CaseStatusHelper.CreateCaseStatusRequestAnswers(min: answerCount, max: answerCount);

            var request = CaseStatusHelper.CreateCaseStatusRequest(answers: answers, type: "LAC");

            var response = _createCaseStatusRequestValidator.Validate(request);

            response.IsValid.Should().BeFalse();
            response.Errors.Should().Contain(e => e.ErrorMessage == "LAC type must have two answers only");
        }
        public void WhenTypeIsCPorCINorLACReturnsItIsValid(string type)
        {
            var validRequest = CaseStatusHelper.CreateCaseStatusRequest(type: type);

            if (type == "LAC")
            {
                validRequest.Answers.AddRange(CaseStatusHelper.CreateCaseStatusRequestAnswers(min: 1, max: 1));
            }

            var response = _createCaseStatusRequestValidator.Validate(validRequest);

            response.IsValid.Should().BeTrue();
        }
        public void WhenInvalidStartDateExceptionIsThrownReturns400WithMessage()
        {
            var exceptionMessage = "error message";

            _mockCaseStatusesUseCase.Setup(x => x.ExecutePost(It.IsAny <CreateCaseStatusRequest>()))
            .Throws(new InvalidStartDateException(exceptionMessage));
            var request = CaseStatusHelper.CreateCaseStatusRequest();

            var response = _caseStatusController.CreateCaseStatus(request) as BadRequestObjectResult;

            response?.StatusCode.Should().Be(400);
            response?.Value.Should().Be(exceptionMessage);
        }
        public void CreatesACaseStatusWithoutAnswersShouldPassIfNoOverlappingCaseStatusExists()
        {
            var(_, person) = CaseStatusHelper.SavePersonWithPastCaseStatusToDatabase(DatabaseContext);

            var request = CaseStatusHelper.CreateCaseStatusRequest(personId: person.Id, answers: new List <CaseStatusRequestAnswers>(), startDate: DateTime.Today);

            _caseStatusGateway.CreateCaseStatus(request);

            var caseStatus = DatabaseContext.CaseStatuses.OrderByDescending(cs => cs.StartDate).FirstOrDefault();

            caseStatus.EndDate.Should().BeNull();
            caseStatus.StartDate.Should().Be(DateTime.Today);
        }
        public void CreatesACaseStatusWithoutAnswers()
        {
            var person = TestHelpers.CreatePerson();

            DatabaseContext.Persons.Add(person);
            DatabaseContext.SaveChanges();

            var request = CaseStatusHelper.CreateCaseStatusRequest(personId: person.Id, answers: new List <CaseStatusRequestAnswers>());

            _caseStatusGateway.CreateCaseStatus(request);

            var caseStatus = DatabaseContext.CaseStatuses.Include(x => x.Answers).FirstOrDefault();

            caseStatus?.Answers.Count.Should().Be(0);
        }
        public void WhenRequestIsValidReturnsSuccessfulResponse()
        {
            var caseStatus       = CaseStatusHelper.CreateCaseStatus();
            var caseStatusDomain = caseStatus.ToDomain();
            var request          = CaseStatusHelper.CreateCaseStatusRequest(caseStatus.PersonId);

            _mockCaseStatusesUseCase
            .Setup(x => x.ExecutePost(request))
            .Returns(caseStatusDomain);

            var response = _caseStatusController.CreateCaseStatus(request) as ObjectResult;

            _mockCaseStatusesUseCase.Verify(x => x.ExecutePost(request));
            response?.StatusCode.Should().Be(201);
            response?.Value.Should().Be("Successfully created case status.");
        }
        public void CreatesACaseStatusWithAnswers()
        {
            var person = TestHelpers.CreatePerson();

            DatabaseContext.Persons.Add(person);
            DatabaseContext.SaveChanges();

            var answersOne = new CaseStatusRequestAnswers()
            {
                Option = _faker.Random.String2(10),
                Value  = _faker.Random.String2(2)
            };

            var answersTwo = new CaseStatusRequestAnswers()
            {
                Option = _faker.Random.String2(10),
                Value  = _faker.Random.String2(2)
            };

            var requestAnswers = new List <CaseStatusRequestAnswers>()
            {
                answersOne, answersTwo
            };

            var request = CaseStatusHelper.CreateCaseStatusRequest(personId: person.Id, answers: requestAnswers);

            _caseStatusGateway.CreateCaseStatus(request);

            var caseStatus = DatabaseContext.CaseStatuses.Include(x => x.Answers).FirstOrDefault();

            caseStatus.Answers.Count.Should().Be(2);
            caseStatus.Answers.Any(a => a.Option == answersOne.Option).Should().Be(true);
            caseStatus.Answers.Any(a => a.Option == answersTwo.Option).Should().Be(true);
            caseStatus.Answers.Any(a => a.Value == answersOne.Value).Should().Be(true);
            caseStatus.Answers.Any(a => a.Value == answersTwo.Value).Should().Be(true);
            caseStatus.Answers[0].GroupId.Should().NotBe(null);
            caseStatus.Answers[1].GroupId.Should().NotBe(null);
            caseStatus.Answers[1].GroupId.Should().Equals(caseStatus.Answers[0].GroupId);
            caseStatus.Notes.Should().Be(request.Notes);
            caseStatus.PersonId.Should().Be(person.Id);
            caseStatus.Type.Should().Be(request.Type);
            caseStatus.StartDate.Should().Be(request.StartDate);
            caseStatus.EndDate.Should().BeNull();
            caseStatus.Answers.All(x => x.CreatedBy == request.CreatedBy).Should().BeTrue();
        }
Esempio n. 17
0
        public void SetUp()
        {
            _mockDatabaseGateway   = new Mock <IDatabaseGateway>();
            _mockCaseStatusGateway = new Mock <ICaseStatusGateway>();
            _caseStatusesUseCase   = new CaseStatusesUseCase(_mockCaseStatusGateway.Object, _mockDatabaseGateway.Object);

            _request = CaseStatusHelper.CreateCaseStatusRequest();

            _mockDatabaseGateway
            .Setup(x => x.GetPersonByMosaicId(It.IsAny <int>()))
            .Returns(TestHelpers.CreatePerson(_request.PersonId));

            var caseStatus = CaseStatusHelper.CreateCaseStatus().ToDomain();

            _mockCaseStatusGateway
            .Setup(x => x.CreateCaseStatus(It.IsAny <CreateCaseStatusRequest>()))
            .Returns(caseStatus);
        }
        public void WhenAnswersAreNotProvidedForTypesThatRequireThemReturnsAnErrorWithMessages(string type)
        {
            var answersWithoutValues = CaseStatusHelper.CreateCaseStatusRequestAnswers(min: 2, max: 2);

            foreach (var a in answersWithoutValues)
            {
                a.Option = "";
                a.Value  = "";
            }
            ;

            var request = CaseStatusHelper.CreateCaseStatusRequest(answers: answersWithoutValues, type: type);

            var response = _createCaseStatusRequestValidator.Validate(request);

            response.IsValid.Should().BeFalse();
            response.Errors.Should().Contain(e => e.ErrorMessage == "'option' must not be empty");
            response.Errors.Should().Contain(e => e.ErrorMessage == "'value' must not be empty");
        }
        public void CreatesACaseStatus()
        {
            var person = TestHelpers.CreatePerson();

            DatabaseContext.Persons.Add(person);
            DatabaseContext.SaveChanges();
            var request = CaseStatusHelper.CreateCaseStatusRequest(personId: person.Id);

            _caseStatusGateway.CreateCaseStatus(request);

            var caseStatus = DatabaseContext.CaseStatuses.FirstOrDefault();

            caseStatus.Notes.Should().Be(request.Notes);
            caseStatus.PersonId.Should().Be(person.Id);
            caseStatus.Type.Should().Be(request.Type);
            caseStatus.StartDate.Should().Be(request.StartDate);
            caseStatus.EndDate.Should().BeNull();
            caseStatus.CreatedBy.Should().Be(request.CreatedBy);
            caseStatus.CreatedAt.Should().NotBeNull();
        }
        public void UpdatesCreatedAtAndCreatedByAuditPropertiesForTheAnswers()
        {
            var person = TestHelpers.CreatePerson();

            DatabaseContext.Persons.Add(person);
            DatabaseContext.SaveChanges();

            var requestField = new List <CaseStatusRequestAnswers> {
                new CaseStatusRequestAnswers()
                {
                    Option = _faker.Random.String2(10), Value = _faker.Random.String2(2)
                }
            };
            var request = CaseStatusHelper.CreateCaseStatusRequest(person.Id, answers: requestField);

            _caseStatusGateway.CreateCaseStatus(request);

            var caseStatus = DatabaseContext.CaseStatuses.Include(x => x.Answers).FirstOrDefault();

            caseStatus.Answers.First().CreatedAt.Should().NotBeNull();
            caseStatus.Answers.First().CreatedBy.Should().Be(request.CreatedBy);
        }
        public void SetsStartDateToProvidedDateTime()
        {
            var fakeTime = new DateTime(2000, 1, 1, 15, 30, 0);

            var person = TestHelpers.CreatePerson();

            DatabaseContext.Persons.Add(person);
            DatabaseContext.SaveChanges();

            var requestField = new List <CaseStatusRequestAnswers>()
            {
                new CaseStatusRequestAnswers()
                {
                    Option = _faker.Random.String2(10), Value = _faker.Random.String2(2)
                }
            };
            var request = CaseStatusHelper.CreateCaseStatusRequest(personId: person.Id, answers: requestField, startDate: fakeTime);

            _caseStatusGateway.CreateCaseStatus(request);

            var caseStatus = DatabaseContext.CaseStatuses.FirstOrDefault();

            caseStatus?.StartDate.Should().Be(fakeTime);
        }
        public async Task EndLACCaseStatusThatHasScheduledAnswers()
        {
            //create new LAC case status, start date 12/01/2000
            var postUri = new Uri($"api/v1/residents/{_person.Id}/case-statuses", UriKind.Relative);

            var answers = CaseStatusHelper.CreateCaseStatusRequestAnswers(min: 2, max: 2);

            var request = CaseStatusHelper.CreateCaseStatusRequest(
                personId: _person.Id,
                type: "LAC",
                answers: answers,
                startDate: new DateTime(2000, 01, 12),
                createdBy: _worker.Email
                );

            request.Notes = null;

            var requestContent = new StringContent(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");

            var createCaseStatusResponse = await Client.PostAsync(postUri, requestContent).ConfigureAwait(true);

            createCaseStatusResponse.StatusCode.Should().Be(201);

            //Get request to check that the case status has been added
            var getUri = new Uri($"api/v1/residents/{_person.Id}/case-statuses", UriKind.Relative);
            var getCaseStatusesResponse = await Client.GetAsync(getUri).ConfigureAwait(true);

            getCaseStatusesResponse.StatusCode.Should().Be(200);

            var addedContent = await getCaseStatusesResponse.Content.ReadAsStringAsync().ConfigureAwait(true);

            var addedCaseStatusResponse = JsonConvert.DeserializeObject <List <CaseStatusResponse> >(addedContent).ToList();

            addedCaseStatusResponse.Count.Should().Be(1);
            addedCaseStatusResponse.Single().Answers.Count.Should().Be(2);
            addedCaseStatusResponse.Single().EndDate.Should().BeNull();
            addedCaseStatusResponse.Single().Notes.Should().BeNull();
            addedCaseStatusResponse.Single().StartDate.Should().Be(request.StartDate.ToString("O"));
            addedCaseStatusResponse.Single().Type.Should().Be(request.Type);

            //patch request to update the start date to 11/01/2000
            var caseStatusId = addedCaseStatusResponse.First().Id;

            var patchUri = new Uri($"api/v1/case-statuses/{caseStatusId}", UriKind.Relative);

            var patchRequest = TestHelpers.CreateUpdateCaseStatusRequest(startDate: new DateTime(2000, 01, 11), email: _worker.Email, caseStatusId: caseStatusId, min: 2, max: 2);

            patchRequest.Notes   = null;
            patchRequest.EndDate = null;

            var patchRequestContent = new StringContent(JsonSerializer.Serialize(patchRequest), Encoding.UTF8, "application/json");

            var patchStatusResponse = await Client.PatchAsync(patchUri, patchRequestContent).ConfigureAwait(true);

            patchStatusResponse.StatusCode.Should().Be(200);

            //Get request to check that the case status was update
            var getCaseStatusesResponseAfterUpdate = await Client.GetAsync(getUri).ConfigureAwait(true);

            getCaseStatusesResponseAfterUpdate.StatusCode.Should().Be(200);

            var updateContent = await getCaseStatusesResponseAfterUpdate.Content.ReadAsStringAsync().ConfigureAwait(true);

            var updatedCaseStatusResponse = JsonConvert.DeserializeObject <List <CaseStatusResponse> >(updateContent).ToList();

            updatedCaseStatusResponse.Count.Should().Be(1);
            updatedCaseStatusResponse.Single().Answers.Count.Should().Be(2);
            updatedCaseStatusResponse.Single().EndDate.Should().BeNull();
            updatedCaseStatusResponse.Single().Notes.Should().BeNull();
            updatedCaseStatusResponse.Single().StartDate.Should().Be(patchRequest.StartDate?.ToString("O"));
            updatedCaseStatusResponse.Single().Type.Should().Be(request.Type);

            //add new scheduled answer
            var postScheduledAnswersUri = new Uri($"api/v1/case-statuses/{caseStatusId}/answers", UriKind.Relative);

            var addScheduledAnswersRequest = CaseStatusHelper.CreateCaseStatusAnswerRequest(
                caseStatusId: caseStatusId,
                startDate: new DateTime(2040, 02, 01),
                createdBy: _worker.Email
                );

            request.Notes = null;

            var scheduledAnswersRequestContent = new StringContent(JsonSerializer.Serialize(addScheduledAnswersRequest), Encoding.UTF8, "application/json");

            var createScheduledAnswersResponse = await Client.PostAsync(postScheduledAnswersUri, scheduledAnswersRequestContent).ConfigureAwait(true);

            createScheduledAnswersResponse.StatusCode.Should().Be(201);

            //Get request to check that the scheduled answers were added
            var getCaseStatusesResponseAfterScheduledUpdate = await Client.GetAsync(getUri).ConfigureAwait(true);

            getCaseStatusesResponseAfterScheduledUpdate.StatusCode.Should().Be(200);

            var updatedContentWithScheduledStatus = await getCaseStatusesResponseAfterScheduledUpdate.Content.ReadAsStringAsync().ConfigureAwait(true);

            var updatedCaseStatusWithScheduledStatusResponse = JsonConvert.DeserializeObject <List <CaseStatusResponse> >(updatedContentWithScheduledStatus).ToList();

            updatedCaseStatusWithScheduledStatusResponse.Count.Should().Be(1);
            updatedCaseStatusWithScheduledStatusResponse.Single().Answers.Count.Should().Be(5);
            updatedCaseStatusWithScheduledStatusResponse.Single().Answers.Where(x => x.Option != CaseStatusAnswerOption.EpisodeReason).Last().StartDate.Should().Be(addScheduledAnswersRequest.StartDate);

            //patch case status to end it
            var endRequest = TestHelpers.CreateUpdateCaseStatusRequest(endDate: new DateTime(2000, 01, 11), email: _worker.Email, caseStatusId: caseStatusId, min: 1, max: 1);

            patchRequest.Notes     = null;
            patchRequest.StartDate = null;

            var serialisedEndRequest = JsonSerializer.Serialize(endRequest);
            var endRequestContent    = new StringContent(serialisedEndRequest, Encoding.UTF8, "application/json");

            var endStatusResponse = await Client.PatchAsync(patchUri, endRequestContent).ConfigureAwait(true);

            endStatusResponse.StatusCode.Should().Be(200);

            //get request to check that the case has been closed (end point only returns active ones at the moment)
            var getCaseStatusesResponseAfterEnd = await Client.GetAsync(getUri).ConfigureAwait(true);

            getCaseStatusesResponseAfterEnd.StatusCode.Should().Be(200);

            var contentAfterContent = await getCaseStatusesResponseAfterEnd.Content.ReadAsStringAsync().ConfigureAwait(true);

            var endCaseStatusResponse = JsonConvert.DeserializeObject <List <CaseStatusResponse> >(contentAfterContent).ToList();

            endCaseStatusResponse.Count.Should().Be(0);
        }