package app.database.model.note;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.ytreza.data.table.exception.ItemException;
import org.ytreza.data.table.exception.UnableToInitializeException;
import org.ytreza.data.table.exception.UnableToSaveException;
import org.ytreza.data.table.exception.UnregisteredTableError;
import org.ytreza.tool.internationalization.Tr;
import app.ApplicationTest;
import app.database.TableFactory;
import app.database.model.tag.TableTag;
import app.database.model.tag.ModelTag;
public class ModelNoteTest {
private TableFactory factory;
private TableNote tableNote;
private TableTag tableTag;
@Before
public void setUp() throws Exception {
ApplicationTest application = ApplicationTest.getApplication();
factory = application.getFactoryJdbc();
tableNote = factory.getTableNote();
tableTag = factory.getTableTag();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testAddTagObject() {
ModelNote note = createNote();
ModelTag tag = createTag("test tag");
note.addTag(tag);
assertTrue(note.hasTag(tag));
assertTrue(note.hasTag(tag.getFullLabel()));
}
@Test
public void testAddTagString() {
ModelNote note = createNote();
note.addTag("toto");
assertTrue(note.hasTag("toto"));
}
private ModelTag createTag(String label) {
ModelTag tag = tableTag.createItem();
tag.setLabel(label);
return tag;
}
@Test
public void testAddParentTagAndHaveBoth() {
ModelNote note = createNote();
note.addTag(createTag("test:toto"));
assertTrue(note.hasTag("test"));
assertTrue(note.hasTag("test:toto"));
assertFalse(note.hasTag("toto"));
List<ModelTag> list = note.getTagList();
assertTrue(list.contains(createTag("test")));
assertTrue(list.contains(createTag("test:toto")));
assertEquals(2, list.size());
}
@Test
public void testGetOfficialList() {
ModelNote note = createNoteWithTag();
List<ModelTag> officialList = note.getOfficialTagList();
assertTrue(officialList.contains(createTag("test:toto")));
assertTrue(officialList.contains(createTag("test:papa")));
assertTrue(officialList.contains(createTag("maman")));
assertTrue(officialList.contains(createTag("chlo")));
assertFalse(officialList.contains(createTag("test")));
assertFalse(officialList.contains(createTag(Tr._("no_tag"))));
assertEquals(4, officialList.size());
}
@Test
public void testGetAllTagList() {
ModelNote note = createNoteWithTag();
List<ModelTag> list = note.getTagList();
assertTrue(list.contains(createTag("test")));
assertTrue(list.contains(createTag("test:toto")));
assertTrue(list.contains(createTag("test:papa")));
assertTrue(list.contains(createTag("maman")));
assertTrue(list.contains(createTag("chlo")));
assertFalse(list.contains(createTag(Tr._("no_tag"))));
assertEquals(5, list.size());
}
private ModelNote createNote() {
return tableNote.createItem();
}
@Test
public void testSave() throws ItemException, UnableToSaveException {
ModelNote note = createNoteWithTag();
note.save();
List<ModelTag> officialList = note.getOfficialTagList();
assertTrue(officialList.contains(createTag("test:toto")));
assertTrue(officialList.contains(createTag("test:papa")));
assertTrue(officialList.contains(createTag("maman")));
assertTrue(officialList.contains(createTag("chlo")));
assertFalse(officialList.contains(createTag("test")));
assertFalse(officialList.contains(createTag(Tr._("no_tag"))));
assertEquals(4, officialList.size());
}
private ModelNote createNoteWithTag() {
ModelNote note = createNote();
note.setSubject("hello");
note.addTag(createTag("test:toto"));
note.addTag(createTag("test:papa"));
note.addTag(createTag("maman"));
note.addTag(createTag("chlo"));
return note;
}
@Test
public void testSaveNoteAndGetNote() throws ItemException, UnableToSaveException, ItemException, UnregisteredTableError, UnableToInitializeException {
ModelNote note = createNoteWithTag();
note.save();
Integer idNote = note.getId();
ModelNote newNote = tableNote.getItem(idNote);
List<ModelTag> officialList = newNote.getOfficialTagList();
System.out.println(officialList);
assertTrue(officialList.contains(createTag("test:toto")));
assertTrue(officialList.contains(createTag("test:papa")));
assertTrue(officialList.contains(createTag("maman")));
assertTrue(officialList.contains(createTag("chlo")));
assertFalse(officialList.contains(createTag("test")));
assertFalse(officialList.contains(createTag(Tr._("no_tag"))));
assertEquals(4, officialList.size());
}
@Test
public void testAddTagAndRemoveIt() throws ItemException, UnableToSaveException {
ModelNote note = createNote();
note.setSubject("hello");
note.addTag("toto");
note.addTag("titi");
note.save();
note.removeTag("toto");
assertFalse(note.hasTag("toto"));
assertTrue(note.hasTag("titi"));
}
@Test
public void testAddTagAndRemoveAll() throws ItemException, UnableToSaveException {
ModelNote note = createNote();
note.setSubject("hello");
note.addTag("toto");
note.addTag("titi");
note.save();
note.removeAllTag();
assertFalse(note.hasTag("toto"));
assertFalse(note.hasTag("titi"));
}
@Test
public void testAddTagAndChangeIt() throws ItemException, UnableToSaveException, ItemException, UnregisteredTableError, UnableToInitializeException {
ModelNote note = createNote();
note.setSubject("hello");
note.addTag("toto");
note.addTag("titi");
note.save();
note.removeTag("toto");
note.save();
ModelNote newNote = tableNote.getItem(note.getId());
assertFalse(newNote.hasTag("toto"));
assertTrue(newNote.hasTag("titi"));
}
@Test
public void testSaveNoteAndSaveNoteWithoutUpdate() throws ItemException, UnableToSaveException, ItemException, UnregisteredTableError, UnableToInitializeException {
ModelNote note = createNoteWithTag();
note.save();
Integer idNote = note.getId();
ModelNote newNote = tableNote.getItem(idNote);
newNote.save();
ModelNote nextNote = tableNote.getItem(idNote);
List<ModelTag> officialList = nextNote.getOfficialTagList();
assertTrue(officialList.contains(createTag("test:toto")));
assertTrue(officialList.contains(createTag("test:papa")));
assertTrue(officialList.contains(createTag("maman")));
assertTrue(officialList.contains(createTag("chlo")));
assertFalse(officialList.contains(createTag("test")));
assertFalse(officialList.contains(createTag(Tr._("no_tag"))));
assertEquals(4, officialList.size());
}
}