package com.github.ricardobaumann.resources;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.hamcrest.core.Is.*;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import com.github.ricardobaumann.api.CommentDTO;
import com.github.ricardobaumann.db.Comment;
import com.github.ricardobaumann.db.CommentDAO;
import com.github.ricardobaumann.db.Post;
import com.github.ricardobaumann.db.PostDAO;
import com.github.ricardobaumann.providers.NotFoundExceptionProvider;
import com.github.ricardobaumann.providers.ValidationExceptionProvider;

import io.dropwizard.testing.junit.ResourceTestRule;

public class CommentResourceTest {
    
    private static PostDAO postDAO = mock(PostDAO.class);
    private static CommentDAO commentDAO = mock(CommentDAO.class);
    
    private static CommentResource CommentResource = new CommentResource(commentDAO, postDAO);
    
    @ClassRule
    public static final ResourceTestRule resources = ResourceTestRule
    .builder()
    .addResource(CommentResource)
    .addProvider(new ValidationExceptionProvider())
    .addProvider(new NotFoundExceptionProvider())
    .build();

    @SuppressWarnings("unchecked")
    @Before
    public void setUp() throws Exception {
        reset(postDAO,commentDAO);
    }

    @Test
    public void testCreateSucessfully() {
        Long postid = 1L;
        String title = "post";
        String content = "content";
        String commentContent = "comment";
        Long id = 1L;
        
        Post post = new Post(postid, title, content);
        
        Comment comment = new Comment();
        comment.setContent(commentContent);
        comment.setPost(post);
        
        when(postDAO.find(postid)).thenReturn(post);
        when(commentDAO.save(comment)).thenReturn(new Comment(id,post,commentContent));
        
        CommentDTO commentDTO = resources.client().target(String.format("/posts/%s/comments", postid))
                .request().post(Entity.entity(new CommentDTO(null, commentContent), MediaType.APPLICATION_JSON), CommentDTO.class);
                
        assertThat(commentDTO.getId(), is(id));
        
        verify(postDAO).find(postid);
        verify(commentDAO).save(comment);
    }

}

@@ -106,24 +106,56 @@
 .Mockito.*;%0A
+%0Aimport java.util.Collections;%0A%0A
 import stati
@@ -177,24 +177,78 @@
 core.Is.*;%0A%0A
+import javax.validation.ConstraintViolationException;%0A
 import javax
@@ -303,16 +303,50 @@
 diaType;
+%0Aimport javax.ws.rs.core.Response;
 %0A%0Aimport
@@ -1047,17 +1047,17 @@
 esource 
-C
+c
 ommentRe
@@ -1229,17 +1229,17 @@
 esource(
-C
+c
 ommentRe
@@ -1453,16 +1453,16 @@
 ption %7B%0A
-
         
@@ -1493,16 +1493,862 @@
 );%0A    %7D
+%0A    %0A    @SuppressWarnings(%22unchecked%22)%0A    @Test%0A    public void testValidationErrorsOnCreate() %7B%0A        Long postId = 1L;%0A        String title = %22post%22;%0A        String content = %22content%22;%0A        Post post = new Post(postId, title, content);%0A        when(postDAO.find(postId)).thenReturn(post);%0A        Comment comment;%0A        when(commentDAO.save(comment = new Comment(null, post, content))).thenThrow(new ConstraintViolationException(Collections.EMPTY_SET));%0A        %0A        Response response = resources.client().target(String.format(%22/posts/%25s/comments%22, postId))%0A        .request().post(Entity.entity(new CommentDTO(null, content), MediaType.APPLICATION_JSON));%0A        %0A        assertThat(response.getStatus(), is(422));%0A        %0A        verify(postDAO).find(postId);%0A        verify(commentDAO).save(comment);%0A        %0A        %0A    %7D
 %0A%0A    @T

comment resource validation unit test