Showing posts with label jUnit. Show all posts
Showing posts with label jUnit. Show all posts

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("");

    }

}

Tuesday, 13 August 2013

UserDAO covered by jUnit test example

import com.mongodb.*;
import course.UserDAO;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;

import java.net.UnknownHostException;

import static org.junit.Assert.*;

public class UserDAOTest {
    static MongoClient mongoClient;
    static DB blogDatabase;
    static DBCollection usersCollection;
    static UserDAO userDAO;

    // 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");
        usersCollection = blogDatabase.getCollection("users");
        userDAO = new UserDAO(blogDatabase);
        usersCollection.drop();
    }

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

    @Test
    public void addUserTest()  {
        System.out.println("Starting test on adding user");

        String email =  "b1102@email.ru";

        userDAO.addUser("b1102" , "1111" , email );

        DBObject find   = new BasicDBObject("_id" , "b1102" );

        DBObject user = usersCollection.find(find).next();

        assertTrue(user.get("email").equals(email));

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

    }

    @Test
    public void addUserThatExistsTest() throws UnknownHostException {
        System.out.println("Starting test on adding user that already exists");

        String email =  "b1102@email.ru";

        userDAO.addUser("b1102", "1111", email);
        assertFalse(userDAO.addUser("b1102", "dedede", "e3r3r"));

        System.out.println("Test on adding user that already exists passed");
        System.out.println("");

    }

    @Test
    public void CheckUserPassPositive() throws UnknownHostException {
        System.out.println("Starting positive test on checking user's pass");
        String login = "b1102";

        String email =  "b1102@email.ru";
        userDAO.addUser(login, "1111", email);

        DBObject validatedUser = userDAO.validateLogin(login , "1111");
        DBObject foundUser = usersCollection.find(new BasicDBObject("_id" , login)).next();

        assertEquals(foundUser, validatedUser);

        System.out.println("Positive test on checking user's pass passed");
        System.out.println("");

    }

    @Test
    public void CheckUserPassNegative() throws UnknownHostException {
        System.out.println("Starting negative test on checking user's pass");
        String login = "b1102";

        String email =  "b1102@email.ru";
        userDAO.addUser(login, "1111", email);

        assertNull(userDAO.validateLogin(login , "1112"));

        System.out.println("Negative test on checking user's pass passed");
        System.out.println("");

    }


    @Test
    public void GetNotExistUser() throws UnknownHostException {
        System.out.println("Starting test on getting not exists user");
        String login = "b1102";

        String email =  "b1102@email.ru";
        userDAO.addUser(login, "1111", email);

        assertNull(userDAO.validateLogin(login+"1" , "1111"));

        System.out.println("Test on getting not exists user passed");
        System.out.println("");

    }

}