Friday 16 August 2013

hw 3.2 jUnit coverage

//here the example of coverage hw 3.2 + 3.3 by unit tests

import com.mongodb.*;
import course.BlogPostDAO;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class BlogPostDAOTest {

    static MongoClient mongoClient;
    static DB blogDatabase;
    static DBCollection blogPostCollection;
    static BlogPostDAO blogPostDAO;

    // this method called before all test (once), to initialize DB variables
    @BeforeClass
    public static void setUp() throws UnknownHostException {
        mongoClient = new MongoClient();
        blogDatabase = mongoClient.getDB("blog");
        blogPostCollection = blogDatabase.getCollection("posts");
        blogPostDAO = new BlogPostDAO(blogDatabase);
        blogPostCollection.drop();
    }

    //this method calls after every test to clean usersCollection
    @After
    public void dropCollection() {
        blogPostCollection.drop();
    }

    @Test
    public void addUserTest() {

        System.out.println("Starting test on adding post");
        String body = "this is body of post";

        blogPostDAO.addPost("hello post", body, Arrays.asList(new String[]{"Awesome tag", "Tag 2", "Tag 3"}), "b1102");

        DBObject find = new BasicDBObject("body", "this is body of post");

        DBObject post = blogPostCollection.find(find).next();

        assertTrue(post.get("body").equals(body));

        System.out.println("Post successfully added");
        System.out.println("");

    }

    @Test
    public void findPostByPermalinkTest() {
        System.out.println("Starting test on finding post by permalink");

        String body = "this is body of post";

        String permalink = blogPostDAO
                .addPost("hello post", body, Arrays.asList(new String[]{"Awesome tag", "Tag 2", "Tag 3"}), "b1102");
        DBObject foundPost = blogPostCollection.findOne();

        assertEquals(foundPost, blogPostDAO.findByPermalink(permalink));

        System.out.println("Test on finding post by permalink successfully finished");
        System.out.println("");

    }

    @Test
    public void addCommentTest() {
        System.out.println("Starting test on adding comment with email");

        String body = "this is body of post";

        String permalink = blogPostDAO.
                addPost("Post number: ", body, Arrays.asList(new String[]{"Awesome tag", "Tag 2", "Tag 3"}), "b1102");

        String theSecondCommentName = "The second comment";
        String theFirstCommentBody = "Comment 1 body";


        blogPostDAO.addPostComment("The first comment", "b1102@pochta.ru", theFirstCommentBody, permalink);
        blogPostDAO.addPostComment(theSecondCommentName, null, "Comment 2 body", permalink);


        DBObject commentedPost = blogPostDAO.findByPermalink(permalink);

        List<BasicDBObject> comments = (List<BasicDBObject>) commentedPost.get("comments");

        assertEquals(comments.size(), 2);

        Assert.assertEquals(comments.get(1).get("author"), theSecondCommentName);


        System.out.println("Test on adding comment with email successfully finished");
        System.out.println("");

    }

    // pretty long tests (takes 20 second), because wait 5 seconds after adding post, to get create posts in different time
    //comment if you don't want to use it and wait so long every build

    @Test
    public void findByDateDescendingTest() throws InterruptedException {
        System.out.println("Starting test on finding posts by date descending");

        String body = "this is body of post";

        for (int i = 1; i < 5; i++) {
            String permalink = blogPostDAO.
                    addPost("Post number: " + i, body, Arrays.asList(new String[]{"Awesome tag", "Tag 2", "Tag 3"}), "b1102");
            //waiting 10 seconds
            System.out.println("Waiting 5 seconds before adding new post");
            Thread.sleep(5000);
        }

        List<DBObject> foundedList = blogPostDAO.findByDateDescending(3);

        assertEquals(3, foundedList.size());

        System.out.println(foundedList.get(1).get("title"));

        assertEquals(foundedList.get(1).get("title"), "Post number: 3");

        System.out.println("Test on finding post by permalink successfully finished");
        System.out.println("");

    }

}

No comments:

Post a Comment