<commit_before>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);
    }

}
<commit_msg>comment resource validation unit test<commit_after>package com.github.ricardobaumann.resources;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.util.Collections;

import static org.hamcrest.core.Is.*;

import javax.validation.ConstraintViolationException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

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);
    }
    
    @SuppressWarnings("unchecked")
    @Test
    public void testValidationErrorsOnCreate() {
        Long postId = 1L;
        String title = "post";
        String content = "content";
        Post post = new Post(postId, title, content);
        when(postDAO.find(postId)).thenReturn(post);
        Comment comment;
        when(commentDAO.save(comment = new Comment(null, post, content))).thenThrow(new ConstraintViolationException(Collections.EMPTY_SET));
        
        Response response = resources.client().target(String.format("/posts/%s/comments", postId))
        .request().post(Entity.entity(new CommentDTO(null, content), MediaType.APPLICATION_JSON));
        
        assertThat(response.getStatus(), is(422));
        
        verify(postDAO).find(postId);
        verify(commentDAO).save(comment);
        
        
    }

    @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);
    }

}

{'fraction_non_alphanumeric': 0.10825463448758307, 'fraction_numerical': 0.0013990905911157748, 'mean_word_length': 3.379019908116386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5109272', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1630, 'n_words': 303}