Package org.uned.agonzalo16.bitacora.dao

Source Code of org.uned.agonzalo16.bitacora.dao.CommentDaoTest

package org.uned.agonzalo16.bitacora.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.uned.agonzalo16.bitacora.BaseDataStoreTest;
import org.uned.agonzalo16.bitacora.domain.Article;
import org.uned.agonzalo16.bitacora.domain.Blog;
import org.uned.agonzalo16.bitacora.domain.Comment;
import org.uned.agonzalo16.bitacora.domain.User;
import org.uned.agonzalo16.bitacora.domain.UserType;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext-test.xml")
public class CommentDaoTest extends BaseDataStoreTest {

  @Autowired
  private CommentDao commentDao;

  @Autowired
  private ArticleDao articleDao;

  @Autowired
  private BlogDao blogDao;

  @Autowired
  private UserDao userDao;

  private Blog createBlog() {
    Blog blog = new Blog();
    blog.setName("blog 1");
    blog.setCreationDate(new Date());
    blog.setDescription("description 1");

    return blogDao.merge(blog);
  }

  private Article createArticle(Blog blog) {
    Article article = new Article();
    article.setTitle("title 1");
    article.setCreationDate(new Date());
    article.setText("text 1");
    article.setOpenComments(true);
    article.setBlog(blog);
    article.setUser(createUser());

    return articleDao.merge(article);
  }

  private User createUser() {
    User user = new User();
    user.setActive(true);
    user.setCreationDate(new Date());
    user.setEmail("email@test.com");
    user.setLocalization("Spain");
    user.setPassword("password");
    user.setType(UserType.VISITOR.getType());
    user.setUsername("username");
    user.setWebSite("www.site.com");

    return userDao.merge(user);
  }

  @Test
  public void testList() {
    Article article = createArticle(createBlog());
    for (int i = 0; i < 100; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article);
      comment.setUser(createUser());

      commentDao.merge(comment);
    }

    List<Comment> allComments = commentDao.findAll();

    assertEquals("100 comments in datastore", allComments.size(), 100);
  }

  @Test
  public void testSave() {
    Article article = createArticle(createBlog());
    Comment comment = new Comment();
    comment.setContent("comment 1");
    comment.setCreationDate(new Date());
    comment.setArticle(article);
    comment.setUser(createUser());

    Comment newComment = commentDao.merge(comment);

    assertEquals("Id must be the same", comment.getId(), newComment.getId());
  }

  @Test
  public void testDelete() {
    Article article = createArticle(createBlog());
    Comment comment = new Comment();
    comment.setContent("comment 1");
    comment.setCreationDate(new Date());
    comment.setArticle(article);
    comment.setUser(createUser());

    Comment newComment = commentDao.merge(comment);

    commentDao.delete(article, newComment.getId());

    assertNull("The entity has been deleted", commentDao.get(article, newComment.getId()));
  }

  @Test
  public void testFindByArticle() {
    Blog blog = createBlog();
    Article article1 = createArticle(blog);
    Article article2 = createArticle(blog);
    for (int i = 0; i < 10; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article1);
      comment.setUser(createUser());

      commentDao.merge(comment);
    }

    for (int i = 10; i < 30; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article2);
      comment.setUser(createUser());

      commentDao.merge(comment);
    }

    List<Comment> article1Comments = commentDao.findByArticle(article1);
    assertEquals("10 comments in article 1", article1Comments.size(), 10);

    List<Comment> article2Comments = commentDao.findByArticle(article2);
    assertEquals("20 comments in article 2", article2Comments.size(), 20);
  }

  @Test
  public void testCountByArticle() {
    Blog blog = createBlog();
    Article article1 = createArticle(blog);
    Article article2 = createArticle(blog);
    for (int i = 0; i < 10; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article1);
      comment.setUser(createUser());

      commentDao.merge(comment);
    }

    for (int i = 10; i < 30; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article2);
      comment.setUser(createUser());

      commentDao.merge(comment);
    }

    assertEquals("10 comments in article 1", commentDao.countByArticle(article1), 10);
    assertEquals("20 comments in article 2", commentDao.countByArticle(article2), 20);
  }

  @Test
  public void testDeleteAricleWithComments() {
    Blog blog = createBlog();
    Article article1 = createArticle(blog);
    Article article2 = createArticle(blog);
    for (int i = 0; i < 10; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article1);
      comment.setUser(createUser());

      commentDao.merge(comment);
    }

    for (int i = 10; i < 30; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article2);
      comment.setUser(createUser());

      commentDao.merge(comment);
    }

    assertEquals("30 comments", commentDao.findAll().size(), 30);

    articleDao.delete(article1.getId());
    assertEquals("20 comments", commentDao.findAll().size(), 20);

    articleDao.delete(article2.getId());
    assertEquals("0 comments", commentDao.findAll().size(), 0);
  }

  @Test
  public void testFidByUser() {
    Article article = createArticle(createBlog());
    User user1 = createUser();
    User user2 = createUser();
    for (int i = 0; i < 10; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article);
      comment.setUser(user1);

      commentDao.merge(comment);
    }

    for (int i = 10; i < 30; i++) {
      Comment comment = new Comment();
      comment.setContent("comment " + i);
      comment.setCreationDate(new Date());
      comment.setArticle(article);
      comment.setUser(user2);

      commentDao.merge(comment);
    }

    List<Comment> user1Comments = commentDao.findByUser(user1);
    assertEquals("10 comments in article 1", user1Comments.size(), 10);

    List<Comment> user2Comments = commentDao.findByUser(user2);
    assertEquals("20 comments in article 2", user2Comments.size(), 20);
  }

}
TOP

Related Classes of org.uned.agonzalo16.bitacora.dao.CommentDaoTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.