Example #1
0
        public Post AddPost(Post post)
        {
            if (post == null)
                throw new ArgumentNullException("post");

            SetupPostDefaults(post);
            ProcessTagsInPost(post);

            post = postRepository.AddPost(post);
            return post;
        }
        public Post AddPost(Post post)
        {
            if (post == null)
                throw new ArgumentNullException("post");

            using (ITransaction transaction = Session.BeginTransaction())
            {
                Session.SaveOrUpdate(post);
                transaction.Commit();
            }

            return post;
        }
        public void AddPost_when_adding_an_tag_with_a_blank_tagname_should_throw_exception()
        {
            //Arrange
            SetupMocks();
            Post post = new Post
            {
                Id = 0
            };

            post.AddTag(new Tag());

            //Act
            service.AddPost(post);

            //Assert
            tagRepositoryMock.Verify(x => x.GetTagByName(It.IsAny<string>()), Times.Never());
            postRepositoryMock.Verify(x => x.AddPost(It.IsAny<Post>()), Times.Never());
        }
 private void SetupPostRepositoryMock(Mock<IPostRepository> postRepositoryMock)
 {
     postRepositoryMock.Setup(x => x.GetLatestPosts(It.IsAny<int>())).Returns((int count) => GeneratePosts(count));
     postRepositoryMock.Setup(x => x.AddPost(It.IsAny<Post>())).Returns((Post value) => { _AddedPost = value; return value; });
     postRepositoryMock.Setup(x => x.GetPostById(It.IsAny<int>())).Returns((int id) => { Post p = CreateNewPost(); p.Id = id; return p; });
 }
        public void SetupMocks()
        {
            var serviceMock = new MoqAutoMocker<PostServices>();
            postRepositoryMock = Mock.Get(serviceMock.Get<IPostRepository>());
            appConfigProviderMock = Mock.Get(serviceMock.Get<IAppConfigProvider>());
            tagRepositoryMock = Mock.Get(serviceMock.Get<ITagRepository>());

            SetupPostRepositoryMock(postRepositoryMock);
            SetupTagRepositoryMock(tagRepositoryMock);
            SetupAppConfigProviderMock(appConfigProviderMock);

            service = serviceMock.ClassUnderTest;

            _AddedPost = null;
        }
        public void AddPost_with_no_tags_should_throw_exception()
        {
            //Arrange
            SetupMocks();
            Post post = new Post();

            //Act
            service.AddPost(post);

            //Assert
            postRepositoryMock.Verify(x => x.AddPost(It.IsAny<Post>()), Times.Never());
        }
        public void AddPost_when_adding_a_tag_that_already_exists_should_use_the_same_instance_of_the_tag()
        {
            //Arrange
            SetupMocks();

            Tag newTag1 = new Tag { Id = 0, TagName = ".net" };
            Tag newTag2 = new Tag { Id = 0, TagName = "misc" };

            Post post = new Post();
            post.AddTag(newTag1);
            post.AddTag(newTag2);

            //Act
            service.AddPost(post);

            //Assert
            Assert.IsNotNull(_AddedPost,"The post was not saved");
            Assert.AreEqual(2, _AddedPost.Tags.Count);

            Tag tag1 = _AddedPost.Tags.Where(t => t.TagName == ".net").FirstOrDefault();
            Tag tag2 = _AddedPost.Tags.Where(t => t.TagName == "misc").FirstOrDefault();

            Assert.IsNotNull(tag1);
            Assert.IsNotNull(tag2);

            Assert.AreEqual(1, tag1.Id);
            Assert.AreEqual(2, tag2.Id);
        }
Example #8
0
        //TODO: Tests!
        public Post UpdatePost(Post postToSave)
        {
            if (postToSave == null)
                throw new ArgumentNullException("postToSave");

            if(postToSave.Id == 0)
                throw new DataException("A post to update should have a valid Id");

            ProcessTagsInPost(postToSave);

            postToSave.ModifiedDate = DateTime.Now;

            postToSave = postRepository.AddPost(postToSave);
            return postToSave;
        }
Example #9
0
        private void SetupPostDefaults(Post post)
        {
            post.Id = 0;
            post.ModifiedDate = DateTime.Now;
            post.CreateDate = DateTime.Now;
            post.UserId = 1; //TODO: remove this hardcoding once we add the user management

            if (post.ScheduledDate < DateTime.Now)
                post.ScheduledDate = DateTime.Now;
        }
Example #10
0
        private void ProcessTagsInPost(Post post)
        {
            if (post.Tags == null || post.Tags.Count == 0)
                throw new ConstraintViolationException("Every post should have at least one tag attached to it");

            IList<Tag> tags = new List<Tag>(post.Tags);

            foreach (var tag in tags)
            {
                if (string.IsNullOrEmpty(tag.TagName))
                    throw new ConstraintViolationException("Tags should not be empty");

                //tag.Id = 0;
                if (tag.Id == 0)
                {
                    if (tag.DateCreated < DateTime.Now)
                        tag.DateCreated = DateTime.Now;

                    tag.NormalizedTagName = StringUtility.GetNormalizedText(tag.TagName, '_');

                    post.Tags.Remove(tag);

                    Tag tagFromRepo = tagRepository.GetTagByName(tag.TagName);

                    post.AddTag(tagFromRepo ?? tag);
                }
            }
        }
Example #11
0
        private static Post StructToPost(Post p, XmlRpcStruct x, bool publish)
        {
            /*
             *    1. struct {
                   2.     string postid;
                   3.     DateTime dateCreated;
                   4.     string title;
                   5.     string description;
                   6.     string[] categories;
                   7.     bool publish;
                   8. }
             */
            if (p == null)
            {
                p = new Post();
                p.Id = x.ContainsKey("postid") ? Convert.ToInt32(x["postid"]) : 0;
            }

            p.Approved = publish;
            p.Body = x["description"].ToString();
            p.Title = x["title"].ToString();

            if (x.ContainsKey("categories"))
            {
                IEnumerable categories = x["categories"] as IEnumerable;

                if (categories != null)
                {
                    foreach(object category in categories)
                    {
                        if (category != null)
                        {
                            Tag tag = new Tag { TagName = category.ToString() };
                            p.AddTag(tag);
                        }
                    }
                }
            }

            return p;
        }
Example #12
0
 private static XmlRpcStruct PostToStruct(Post p)
 {
     /*
      *    1. struct {
            2.     string postid;
            3.     DateTime dateCreated;
            4.     string title;
            5.     string description;
            6.     string[] categories;
            7.     bool publish;
            8. }
      */
     XmlRpcStruct x = new XmlRpcStruct();
     x.Add("postid", p.Id);
     x.Add("dateCreated", p.ScheduledDate);
     x.Add("title", p.Title);
     x.Add("description", p.Body);
     x.Add("categories", ToStringArray(p.Tags));
     x.Add("link", "#");
     x.Add("publish", p.Approved);
     return x;
 }