<commit_before>package org.mamute.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mamute.dao.QuestionDAO.PAGE_SIZE;

import java.util.ArrayList;
import java.util.List;

import javax.validation.ConstraintViolationException;

import net.vidageek.mirror.dsl.Mirror;

import org.hamcrest.Matchers;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.mamute.builder.QuestionBuilder;
import org.mamute.model.LoggedUser;
import org.mamute.model.Question;
import org.mamute.model.Tag;
import org.mamute.model.User;
import org.mamute.model.interfaces.RssContent;

import br.com.caelum.timemachine.Block;
import br.com.caelum.timemachine.TimeMachine;

public class QuestionDAOTest extends DatabaseTestCase {

	private static final String INVALID_TITLE = "Tiny title";
	private static final String INVALID_DESC = "Tiny desc";
	private static final String VALID_DESC = "Description with more than 30 characters";
	private static final String VALID_TITLE = "Title with more than 15 characters";
	private QuestionDAO questionsBeingAuthor;
	private QuestionBuilder question = new QuestionBuilder();
	private User author;
	private Tag java = tag("java");
	private Tag defaultTag = tag("defaultTag");
	private QuestionDAO questionsBeingModerator;
	private User moderator;
	private User anyone;
	private QuestionDAO questionsForAnyone;

	@Before
	public void setup() {
		author = user("Leonardo", "<EMAIL>");
		moderator = user("Leonardo", "<EMAIL>").asModerator();
		anyone = user("Leonardo", "<EMAIL>");
		session.save(author);
		session.save(moderator);
		session.save(anyone);
		session.save(java);
		session.save(defaultTag);
		this.questionsBeingAuthor = new QuestionDAO(session, new InvisibleForUsersRule(new LoggedUser(author, null)));
		this.questionsBeingModerator = new QuestionDAO(session, new InvisibleForUsersRule(new LoggedUser(moderator, null)));
		this.questionsForAnyone = new QuestionDAO(session, new InvisibleForUsersRule(new LoggedUser(anyone, null)));
	}
	
	
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_description_is_null() {
		Question myQuestion = question.withTitle(VALID_TITLE).withDescription(null).withAuthor(author).build();
		questionsBeingAuthor.save(myQuestion );
	}
	
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_description_has_less_than_30_chars() {
		Question myQuestion = question.withTitle(VALID_TITLE).withDescription(INVALID_DESC).withAuthor(author).build();
		questionsBeingAuthor.save(myQuestion);
	}
	
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_title_is_null() {
		Question myQuestion = question.withTitle(null).withDescription(VALID_DESC).withAuthor(author).build();
		questionsBeingAuthor.save(myQuestion);
	}
	
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_title_has_less_than_15_chars() {
		Question myQuestion = question.withTitle(INVALID_TITLE).withDescription(VALID_DESC).withAuthor(author).build();
		questionsBeingAuthor.save(myQuestion );
	}
	
    
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_tags_is_empty() {
		Question myQuestion = question.withTags(new ArrayList<Tag>()).withTitle(VALID_TITLE).withDescription(VALID_DESC).withAuthor(author).build();                                                                                                                    
		questionsBeingAuthor.save(myQuestion );
	}
	
	@Test
	public void should_return_only_questions_with_the_provided_tag() {
		Question javaQuestion = javaQuestion();
		Question javaEEQuestion = javaEEQuestion();
		Question androidQuestion = androidQuestion();
		
		List<Question> questionsAboutJava = questionsBeingAuthor.withTagVisible(java, 1);

		assertTrue(questionsAboutJava.contains(javaQuestion));
		assertFalse(questionsAboutJava.contains(javaEEQuestion));
		assertFalse(questionsAboutJava.contains(androidQuestion));
	}

	@Test
	public void should_not_ignore_invisible_ones_if_user_is_author() {<commit_msg>removing test that did make sense anymore<commit_after>package org.mamute.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mamute.dao.QuestionDAO.PAGE_SIZE;

import java.util.ArrayList;
import java.util.List;

import javax.validation.ConstraintViolationException;

import net.vidageek.mirror.dsl.Mirror;

import org.hamcrest.Matchers;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.mamute.builder.QuestionBuilder;
import org.mamute.model.LoggedUser;
import org.mamute.model.Question;
import org.mamute.model.Tag;
import org.mamute.model.User;
import org.mamute.model.interfaces.RssContent;

import br.com.caelum.timemachine.Block;
import br.com.caelum.timemachine.TimeMachine;

public class QuestionDAOTest extends DatabaseTestCase {

	private static final String INVALID_TITLE = "Tiny title";
	private static final String INVALID_DESC = "Tiny desc";
	private static final String VALID_DESC = "Description with more than 30 characters";
	private static final String VALID_TITLE = "Title with more than 15 characters";
	private QuestionDAO questionsBeingAuthor;
	private QuestionBuilder question = new QuestionBuilder();
	private User author;
	private Tag java = tag("java");
	private Tag defaultTag = tag("defaultTag");
	private QuestionDAO questionsBeingModerator;
	private User moderator;
	private User anyone;
	private QuestionDAO questionsForAnyone;

	@Before
	public void setup() {
		author = user("Leonardo", "<EMAIL>");
		moderator = user("Leonardo", "<EMAIL>").asModerator();
		anyone = user("Leonardo", "<EMAIL>");
		session.save(author);
		session.save(moderator);
		session.save(anyone);
		session.save(java);
		session.save(defaultTag);
		this.questionsBeingAuthor = new QuestionDAO(session, new InvisibleForUsersRule(new LoggedUser(author, null)));
		this.questionsBeingModerator = new QuestionDAO(session, new InvisibleForUsersRule(new LoggedUser(moderator, null)));
		this.questionsForAnyone = new QuestionDAO(session, new InvisibleForUsersRule(new LoggedUser(anyone, null)));
	}
	
	
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_description_is_null() {
		Question myQuestion = question.withTitle(VALID_TITLE).withDescription(null).withAuthor(author).build();
		questionsBeingAuthor.save(myQuestion );
	}
	
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_description_has_less_than_30_chars() {
		Question myQuestion = question.withTitle(VALID_TITLE).withDescription(INVALID_DESC).withAuthor(author).build();
		questionsBeingAuthor.save(myQuestion);
	}
	
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_title_is_null() {
		Question myQuestion = question.withTitle(null).withDescription(VALID_DESC).withAuthor(author).build();
		questionsBeingAuthor.save(myQuestion);
	}
	
	@Test(expected=ConstraintViolationException.class)
	public void should_throw_constraint_exception_if_title_has_less_than_15_chars() {
		Question myQuestion = question.withTitle(INVALID_TITLE).withDescription(VALID_DESC).withAuthor(author).build();
		questionsBeingAuthor.save(myQuestion );
	}
	
    
	@Test
	public void should_return_only_questions_with_the_provided_tag() {
		Question javaQuestion = javaQuestion();
		Question javaEEQuestion = javaEEQuestion();
		Question androidQuestion = androidQuestion();
		
		List<Question> questionsAboutJava = questionsBeingAuthor.withTagVisible(java, 1);

		assertTrue(questionsAboutJava.contains(javaQuestion));
		assertFalse(questionsAboutJava.contains(javaEEQuestion));
		assertFalse(questionsAboutJava.contains(androidQuestion));
	}

	@Test
	public void should_not_ignore_invisible_ones_if_user_is_author() {
{'fraction_non_alphanumeric': 0.0961444607125427, 'fraction_numerical': 0.0021961932650073207, 'mean_word_length': 6.520183486238532, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4888238', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2374, 'n_words': 447}