Example #1
0
        public PoemDTO GetPoemById(Guid poemId)
        {
            var spec = new GetPoemByIdSpec(poemId);
            var poem = _poemRepository.GetBySpec(spec);

            return(PoemDTO.CreateDTOFromEntity(poem));
        }
Example #2
0
        public async void OnPost_AllFieldsModified_ExpectChangesSavedToRepository()
        {
            //Arrange
            var poem = new PoemBuilder().Default().Build();

            poem.PublishedRevision = null;
            var client = CreateClientAndTestData(poem);
            var form   = await GetInputFormAsync(client, poem.Id);

            var modifiedPoemDTO = PoemDTO.CreateDTOFromEntity(poem);

            modifiedPoemDTO.Name  = "Updated " + poem.Name;
            modifiedPoemDTO.Notes = poem.Notes + " and more notes";
            modifiedPoemDTO.ConceivedDateLocal  = poem.ConceivedDateUTC.ToLocalTime().AddDays(-1);
            modifiedPoemDTO.PublishedRevisionId = poem.PoemRevisions.FirstOrDefault().Id;

            var newRevision    = new RevisionBuilder(poem).Random().Build();
            var newRevisionDTO = RevisionDTO.CreateDTOFromEntity(newRevision);
            var inputs         = CreateFormInputs(modifiedPoemDTO, newRevisionDTO);

            //Act
            var response = await PostRequestHelper.PostWithUpdateInputsAsync(client, form, inputs);

            //Assert
            var revisionList = new RevisionList(poem.PoemRevisions.Append(newRevision));

            AssertEditPoemPage(response, modifiedPoemDTO, revisionList);
        }
Example #3
0
        public async void OnPost_WithoutNewRevision_VerifyServiceMethodIsCalledAndConfirmationMessageIsSet()
        {
            //Arrange
            var poem            = new PoemBuilder().Default().Build();
            var poemDTO         = PoemDTO.CreateDTOFromEntity(poem);
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.IsNameUsedByOtherPoem(poemDTO.Id, poemDTO.Name))
            .Returns(false);

            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.UpdatePoem(It.IsAny <PoemDTO>()));
            mockWriteService.Setup(w => w.SaveNewRevision(poemDTO.Id, It.IsAny <RevisionDTO>()));
            var testModel = new EditPoemModel(mockReadService.Object, mockWriteService.Object);

            //Act
            testModel.PoemToEdit  = poemDTO;
            testModel.NewRevision = new RevisionDTO();
            await testModel.OnPostAsync();

            //Assert
            mockWriteService.Verify(w => w.UpdatePoem(It.IsAny <PoemDTO>()), Times.Once);
            mockWriteService.Verify(w => w.SaveNewRevision(poemDTO.Id, It.IsAny <RevisionDTO>()), Times.Never);
            Assert.Equal(EditPoemModel.POEM_SAVED_MESSAGE, testModel.ConfirmationMessage);
        }
Example #4
0
        public async void OnPost_WithRevision_VerifyServiceMethodIsCalledAndConfirmationMessageIsSet()
        {
            //Arrange
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.IsNameUsed(It.IsAny <string>())).Returns(false);
            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()));

            //Act
            var testModel = new NewPoemModel(mockReadService.Object, mockWriteService.Object);

            testModel.NewPoem     = PoemDTO.CreateDTOFromEntity(new PoemBuilder().Default().Build());
            testModel.NewRevision = new RevisionDTO()
            {
                Title = "title",
                Body  = "body"
            };
            var testResult = await testModel.OnPostAsync();

            //Assert
            mockWriteService.Verify(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()));
            Assert.Equal(NewPoemModel.POEM_CREATED_MESSAGE, testModel.ConfirmationMessage);
        }
Example #5
0
        public async void OnPost_PublishNewRevision_NewRevisionNotSaved_VerifyServiceMethodIsNotCalledAndErrorMessageIsSetCorrectly()
        {
            //Arrange
            var poem        = new PoemBuilder().Default().Build();
            var poemDTO     = PoemDTO.CreateDTOFromEntity(poem);
            var revisionDTO = RevisionDTO.CreateDTOFromEntity(poem.PoemRevisions.FirstOrDefault());

            revisionDTO.Id = Guid.NewGuid();
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.IsNameUsedByOtherPoem(poemDTO.Id, poemDTO.Name))
            .Returns(false);
            mockReadService.Setup(r => r.GetLatestPoemRevision(poemDTO.Id))
            .Returns(new RevisionDTO());

            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.UpdatePoem(It.IsAny <PoemDTO>()));
            mockWriteService.Setup(w => w.SaveNewRevision(poemDTO.Id, It.IsAny <RevisionDTO>()));
            var testModel = new EditPoemModel(mockReadService.Object, mockWriteService.Object);

            //Act
            testModel.PoemToEdit = poemDTO;
            testModel.PoemToEdit.PublishedRevisionId = EditPoemModel.PUBLISH_NEW_REVISION;
            testModel.NewRevision = revisionDTO;

            //Assert
            await Assert.ThrowsAsync <ApplicationException>(() => testModel.OnPostAsync());
        }
Example #6
0
        public void OnGet_WithRevision_ExpectCorrectValuesInProperties()
        {
            //Arrange
            var poem            = new PoemBuilder().Default().Build();
            var poemDTO         = PoemDTO.CreateDTOFromEntity(poem);
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.GetPoemById(poemDTO.Id))
            .Returns(poemDTO);

            var revisions = new RevisionBuilder(poem).Default().BuildCollection(size: 2);

            revisions.First().CreatedDateUTC = DateTime.UtcNow.AddDays(-2);
            revisions.Last().CreatedDateUTC  = DateTime.UtcNow.AddDays(-1);
            var revisionList = new RevisionList(revisions);

            mockReadService.Setup(r => r.GetPoemRevisions(poemDTO.Id))
            .Returns(revisionList);

            var mockWriteService = new Mock <IWritePoemService>();
            var testModel        = new EditPoemModel(mockReadService.Object, mockWriteService.Object);

            //Act
            testModel.OnGet(poemDTO.Id);

            //Assert
            Assert.Equal(poemDTO, testModel.PoemToEdit);
            Assert.Equal(revisionList, testModel.PastRevisions);
            var latestRevision = revisionList.OrderByDescending(r => r.CreatedDateLocal).First();

            Assert.Equal(latestRevision, testModel.LatestRevision);
        }
Example #7
0
        public async void OnPost_NameConflict_VerifyServiceMethodIsNotCalledAndErrorMessageIsSetCorrectly()
        {
            var newName = "new name";
            //Arrange
            var poem            = new PoemBuilder().Default().Build();
            var poemDTO         = PoemDTO.CreateDTOFromEntity(poem);
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.GetPoemRevisions(poemDTO.Id))
            .Returns(new RevisionList());
            mockReadService.Setup(r => r.IsNameUsedByOtherPoem(poemDTO.Id, newName))
            .Returns(true);

            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.UpdatePoem(It.IsAny <PoemDTO>()));
            var testModel = new EditPoemModel(mockReadService.Object, mockWriteService.Object);

            //Act
            testModel.PoemToEdit      = poemDTO;
            testModel.PoemToEdit.Name = newName;
            await testModel.OnPostAsync();

            //Assert
            mockWriteService.Verify(w => w.UpdatePoem(It.IsAny <PoemDTO>()), Times.Never);

            var inputFieldName = nameof(testModel.PoemToEdit) + "." + nameof(testModel.PoemToEdit.Name);

            AssertKeyInErrorState(testModel, inputFieldName);
            AssertErrorMessage(testModel, EditPoemModel.NAME_CONFLICT_MESSAGE);
        }
Example #8
0
        public async void OnPost_PublishEmptyRevision_VerifyServiceMethodIsNotCalledAndErrorMessageIsSetCorrectly()
        {
            //Arrange
            var poem            = new PoemBuilder().Default().Build();
            var poemDTO         = PoemDTO.CreateDTOFromEntity(poem);
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.GetPoemRevisions(poemDTO.Id))
            .Returns(new RevisionList());
            mockReadService.Setup(r => r.IsNameUsedByOtherPoem(poemDTO.Id, poemDTO.Name))
            .Returns(false);

            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.UpdatePoem(It.IsAny <PoemDTO>()));
            var testModel = new EditPoemModel(mockReadService.Object, mockWriteService.Object);

            //Act
            testModel.PoemToEdit = poemDTO;
            testModel.PoemToEdit.PublishedRevisionId = EditPoemModel.PUBLISH_NEW_REVISION;
            testModel.NewRevision = new RevisionDTO();
            await testModel.OnPostAsync();

            //Assert
            mockWriteService.Verify(w => w.UpdatePoem(It.IsAny <PoemDTO>()), Times.Never);

            var inputFieldName = nameof(testModel.PoemToEdit) + "." + nameof(testModel.PoemToEdit.PublishedRevisionId);

            AssertKeyInErrorState(testModel, inputFieldName);
            AssertErrorMessage(testModel, EditPoemModel.NEW_REVISION_EMPTY_MESSAGE);
        }
Example #9
0
        public async void OnPost_InvalidModelState_VerifyServiceMethodIsNotCalled()
        {
            //Arrange
            var poem            = new PoemBuilder().Default().Build();
            var poemDTO         = PoemDTO.CreateDTOFromEntity(poem);
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.GetPoemRevisions(poemDTO.Id))
            .Returns(new RevisionList());
            mockReadService.Setup(r => r.IsNameUsedByOtherPoem(poemDTO.Id, poemDTO.Name));

            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.UpdatePoem(It.IsAny <PoemDTO>()));
            var testModel = new EditPoemModel(mockReadService.Object, mockWriteService.Object);

            //Act
            testModel.PoemToEdit = poemDTO;
            testModel.ModelState.AddModelError("test", "invalid state");
            await testModel.OnPostAsync();

            //Assert
            mockReadService.Verify(r => r.IsNameUsedByOtherPoem(poemDTO.Id, poemDTO.Name), Times.Never);
            mockWriteService.Verify(w => w.UpdatePoem(It.IsAny <PoemDTO>()), Times.Never);
        }
Example #10
0
        public async void OnPost_PublishNewRevision_VerifyServiceMethodIsCalledAndConfirmationMessageIsSet()
        {
            //Arrange
            var poem        = new PoemBuilder().Default().Build();
            var poemDTO     = PoemDTO.CreateDTOFromEntity(poem);
            var revisionDTO = RevisionDTO.CreateDTOFromEntity(poem.PoemRevisions.FirstOrDefault());

            revisionDTO.Id = Guid.NewGuid();
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.IsNameUsedByOtherPoem(poemDTO.Id, poemDTO.Name))
            .Returns(false);
            mockReadService.Setup(r => r.GetLatestPoemRevision(poemDTO.Id))
            .Returns(revisionDTO);

            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.UpdatePoem(It.IsAny <PoemDTO>()));
            mockWriteService.Setup(w => w.SaveNewRevision(poemDTO.Id, It.IsAny <RevisionDTO>()));
            var testModel = new EditPoemModel(mockReadService.Object, mockWriteService.Object);

            //Act
            testModel.PoemToEdit = poemDTO;
            testModel.PoemToEdit.PublishedRevisionId = EditPoemModel.PUBLISH_NEW_REVISION;
            testModel.NewRevision = revisionDTO;
            await testModel.OnPostAsync();

            //Assert
            mockReadService.Verify(r => r.GetLatestPoemRevision(poemDTO.Id), Times.Once);
            mockWriteService.Verify(w => w.UpdatePoem(testModel.PoemToEdit), Times.Once);
            mockWriteService.Verify(w => w.SaveNewRevision(poemDTO.Id, revisionDTO), Times.Once);
            Assert.Equal(EditPoemModel.POEM_SAVED_MESSAGE, testModel.ConfirmationMessage);
        }
Example #11
0
        public PoemDTO GetPoemById(Guid id)
        {
            var spec = new GetPoemAndPublishedRevisionByIdSpec(id);
            var poem = _poemRepository.GetBySpec(spec);

            return(PoemDTO.CreateDTOFromEntity(poem));
        }
Example #12
0
        public PoemDTO GetDefaultPoem()
        {
            var spec = new GetDefaultPoemSpec();
            var poem = _poemRepository.GetBySpec(spec);

            return(PoemDTO.CreateDTOFromEntity(poem));
        }
Example #13
0
        public IEnumerable <PoemDTO> ListByLastModifiedDate()
        {
            IEnumerable <Poem> poems = _poemRepository.List(
                ListPoemsByLastModifiedDateSpec.CreateSpecification());

            if (poems == null)
            {
                return(new List <PoemDTO>());
            }
            else
            {
                return(poems.Select(p => { return PoemDTO.CreateDTOFromEntity(p); }));
            }
        }
Example #14
0
        public IEnumerable <PoemDTO> ListByConceivedDate()
        {
            IEnumerable <Poem> poems = _poemRepository.List(
                new ListPoemsByConceivedDateSpec());

            if (poems == null)
            {
                return(new List <PoemDTO>());
            }
            else
            {
                return(poems.Select(p => { return PoemDTO.CreateDTOFromEntity(p); }));
            }
        }
Example #15
0
        public IEnumerable <PoemDTO> ListPoemsByDate()
        {
            var spec  = new ListPoemsByDateSpec();
            var poems = _poemRepository.List(spec);

            if (poems != null)
            {
                return(poems.Select(p => PoemDTO.CreateDTOFromEntity(p)));
            }
            else
            {
                return(new List <PoemDTO>());
            }
        }
Example #16
0
        void ListPoemsByTitle_RepositoryReturnsPoemList_ExpectPoemDTOList()
        {
            //Arrange
            var poemList       = new PoemBuilder().Default().BuildCollection(size: 3);
            var mockRepository = new Mock <IEntityRepository <Poem> >();

            mockRepository.Setup(r => r.List(It.IsAny <ListPoemsByTitleSpec>()))
            .Returns(poemList);

            //Act
            var service    = new ListPoemsService(mockRepository.Object);
            var testResult = service.ListPoemsByTitle();

            //Assert
            var expectResult = poemList.Select(p => PoemDTO.CreateDTOFromEntity(p));

            Assert.Equal(expectResult.Count(), testResult.Count());
        }
Example #17
0
        public void OnGet_VerifyPageIsReturnedAndPoemListIsSet()
        {
            //Arrange
            var poems       = new PoemBuilder().Default().BuildCollection(size: 3);
            var poemDTOList = poems.Select(p => PoemDTO.CreateDTOFromEntity(p));

            var mockService = new Mock <IListPoemsService>();

            mockService.Setup(s => s.ListPoemsByDate()).Returns(poemDTOList);

            //Act
            var listModel  = new ListPoemsByDateModel(mockService.Object);
            var testResult = listModel.OnGet();

            //Assert

            Assert.IsType <PageResult>(testResult);
            Assert.Equal(poemDTOList.Count(), listModel.Poems.Count());
        }
Example #18
0
        public void OnGet_ExistingPoems_VerifyPoemListIsNotEmpty()
        {
            //Arrange
            var poems       = new PoemBuilder().Default().BuildCollection(size: 3);
            var poemDTOList = poems.Select(p => PoemDTO.CreateDTOFromEntity(p));

            var mockService = new Mock <IListPoemsService>();

            mockService.Setup(s => s.ListByConceivedDate()).Returns(poemDTOList);

            //Act
            var listModel = new ListPoemsByConceivedDateModel(mockService.Object);

            listModel.OnGet();

            //Assert

            Assert.Equal(poemDTOList.Count(), listModel.Poems.Count());
        }
Example #19
0
        public void GetPoemById_ValidId_ExpectMatchingDTO()
        {
            //Arrange
            var poem         = new PoemBuilder().Default().Build();
            var expectResult = PoemDTO.CreateDTOFromEntity(poem);

            var mockPoemRepository = new Mock <IEntityRepository <Poem> >();

            mockPoemRepository.Setup(pr => pr.GetBySpec(It.IsAny <GetPoemAndPublishedRevisionByIdSpec>()))
            .Returns(poem);
            var mockRevisionRepository = new Mock <IEntityRepository <PoemRevision> >();

            //Act
            var testResult = new ReadPoemService(
                mockPoemRepository.Object, mockRevisionRepository.Object)
                             .GetPoemById(poem.Id);

            //Assert
            Assert.True(IsEqual(expectResult, testResult));
        }
Example #20
0
        public void OnGet_WithoutRevision_ExpectCorrectValuesInProperties()
        {
            //Arrange
            var poem            = new PoemBuilder().Default().Build();
            var poemDTO         = PoemDTO.CreateDTOFromEntity(poem);
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.GetPoemById(poemDTO.Id))
            .Returns(poemDTO);

            mockReadService.Setup(r => r.GetPoemRevisions(poemDTO.Id))
            .Returns(new RevisionList());

            var mockWriteService = new Mock <IWritePoemService>();
            var testModel        = new EditPoemModel(mockReadService.Object, mockWriteService.Object);

            //Act
            testModel.OnGet(poemDTO.Id);

            //Assert
            Assert.Equal(poemDTO, testModel.PoemToEdit);
        }
Example #21
0
        public async void OnPost_HasPublishedRevision_ChangeToNotPublish_ExpectChangesSavedToRepository()
        {
            //Arrange
            var poem = new PoemBuilder().Random().Build();

            poem.PoemRevisions     = new RevisionBuilder(poem).Default().BuildCollection(size: 2);
            poem.PublishedRevision = poem.PoemRevisions.FirstOrDefault();
            var client = CreateClientAndTestData(poem);
            var form   = await GetInputFormAsync(client, poem.Id);

            var modifiedPoemDTO = PoemDTO.CreateDTOFromEntity(poem);

            modifiedPoemDTO.PublishedRevisionId = null;
            var inputs = CreateFormInputs(modifiedPoemDTO);

            //Act
            var response = await PostRequestHelper.PostWithUpdateInputsAsync(client, form, inputs);

            //Assert
            var revisionList = new RevisionList(poem.PoemRevisions);

            AssertEditPoemPage(response, modifiedPoemDTO, revisionList);
        }
Example #22
0
        public async void OnPost_NameConflict_VerifyMethodServiceIsNotCalledAndErrorMessageIsSetCorrectly()
        {
            //Arrange
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.IsNameUsed(It.IsAny <string>())).Returns(true);
            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()));

            //Act
            var testModel = new NewPoemModel(mockReadService.Object, mockWriteService.Object);

            testModel.NewPoem = PoemDTO.CreateDTOFromEntity(new PoemBuilder().Default().Build());
            var testResult = await testModel.OnPostAsync();

            //Assert
            mockWriteService.Verify(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()), Times.Never);

            var inputFieldName = nameof(testModel.NewPoem) + "." + nameof(testModel.NewPoem.Name);

            AssertKeyInErrorState(testModel, inputFieldName);
            AssertErrorMessage(testModel, NewPoemModel.NAME_CONFLICT_MESSAGE);
        }