/**
* @Created Nov 24, 2010 9:52:54 AM
* @author cry30
*/
package com.philip.journal.home.service;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import com.philip.journal.core.JournalTestCommon;
import com.philip.journal.core.exception.JournalException;
import com.philip.journal.core.service.AbstractSpringJournalTestService;
import com.philip.journal.home.bean.Branch;
import com.philip.journal.home.bean.Entry;
import com.philip.journal.home.dao.BranchDAO;
import com.philip.journal.home.dao.EntryDAO;
/**
* Test class for {@link EntryServiceImpl}.
*
* @author Royce
*/
public class EntryServiceImplTest extends AbstractSpringJournalTestService {
/** Test service instance. */
@Autowired
private transient EntryServiceImpl entryServiceImpl;
/** Refactored common functionality. */
private final JournalTestCommon common = new JournalTestCommon();
/**
* Test method for {@link com.philip.journal.home.service.EntryServiceImpl#getCategories(java.util.Map)}.
*/
@Test
public final void testGetCategories()
{
assertNotNull("Failed on the basic test.", entryServiceImpl.getCategories(null));
}
/**
* Test method for
* {@link com.philip.journal.home.service.EntryServiceImpl#saveEntry(java.util.Map, long, long, java.lang.String, java.lang.String)}
* .
*
* Case 1.1: Inserted entry has creator set. <br/>
* Case 1.2: Inserted entry has title set. <br/>
* Case 1.3: Inserted entry has description set. <br/>
* Case 1.4: Inserted entry has branch set.
*/
@Test
public final void testSaveEntry1()
{
final EntryDAO mockedEntryDAO = getDaoFacade().getEntryDAO();
final BranchDAO mockedBranchDAO = getDaoFacade().getBranchDAO();
final long branchId = 1L;
final Branch testParentBranch = new Branch("Test Parent", null);
when(mockedBranchDAO.read(branchId)).thenReturn(testParentBranch);
final String testTitle = "Test Title";
final String testDesc = "Test Description";
try {
entryServiceImpl.saveEntry(super.getSession(), branchId, 0, testTitle, testDesc);
verify(mockedEntryDAO, times(1)).save((Entry) Mockito.any());
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
final ArgumentCaptor<Entry> argument = ArgumentCaptor.forClass(Entry.class);
try {
verify(mockedEntryDAO).save(argument.capture());
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
assertEquals("Case 1.1: Inserted entry has creator set.", getTestUser(), argument.getValue().getCreator());
assertEquals("Case 1.2: Inserted entry has title set.", testTitle, argument.getValue().getTitle());
assertEquals("Case 1.3: Inserted entry has description set.", testDesc, argument.getValue().getDescription());
assertEquals("Case 1.4: Inserted entry has branch set.", testParentBranch, argument.getValue().getBranch());
}
/**
* Case 2.1: Updated entry has updater set. <br/>
* Case 2.2: Updated entry has title set. <br/>
* Case 2.3: Updated entry has description set. <br/>
* Case 2.4: Updated entry has branch set.
*/
@Test
public final void testSaveEntry2()
{
final EntryDAO mockedEntryDAO = getDaoFacade().getEntryDAO();
final BranchDAO mockedBranchDAO = getDaoFacade().getBranchDAO();
final long entryId = 1L;
final Entry testEntry = new Entry();
testEntry.setTitle("Test Title");
testEntry.setDescription("Test Description");
when(mockedEntryDAO.read(entryId)).thenReturn(testEntry);
final long newBranchId = 666L;
final String newTitle = "New Title";
final String newDescription = "New Description";
final Branch newParentBranch = new Branch("Test Parent", null);
newParentBranch.setBranchId(newBranchId);
when(mockedBranchDAO.read(newBranchId)).thenReturn(newParentBranch);
final ArgumentCaptor<Entry> argument = ArgumentCaptor.forClass(Entry.class);
try {
entryServiceImpl.saveEntry(super.getSession(), newBranchId, entryId, newTitle, newDescription);
verify(mockedEntryDAO, times(1)).save((Entry) Mockito.any());
verify(mockedEntryDAO).save(argument.capture());
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
assertEquals("Case 2.1: Updated entry has updater set.", getTestUser(), argument.getValue().getUpdater());
assertEquals("Case 2.2: Updated entry has title set.", newTitle, argument.getValue().getTitle());
assertEquals("Case 2.3: Updated entry has description set.", newDescription, argument.getValue()
.getDescription());
assertEquals("Case 2.4: Updated entry has branch set.", newBranchId, argument.getValue().getBranch()
.getBranchId());
}
/**
* Case 3: Invalid branch ID.
*/
@Test(expected = IllegalArgumentException.class)
public final void testSaveEntry3()
{
final BranchDAO mockedBranchDAO = getDaoFacade().getBranchDAO();
when(mockedBranchDAO.read(anyInt())).thenReturn(null);
entryServiceImpl.saveEntry(super.getSession(), 0, 0, "title", "desc");
}
/** Case 4: Null title. */
@Test(expected = IllegalArgumentException.class)
public final void testSaveEntry4()
{
final BranchDAO mockedBranchDAO = getDaoFacade().getBranchDAO();
when(mockedBranchDAO.read(anyInt())).thenReturn(new Branch());
try {
entryServiceImpl.saveEntry(super.getSession(), 0, 0, null, null);
fail("Case 4: Null title.");
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
}
/**
* Test method for {@link com.philip.journal.home.service.EntryServiceImpl#getEntries(java.util.Map, long)}.
*
* Case 1: Invalid Branch ID.
*/
@Test
public final void testGetEntries()
{
assertNotNull("Case 1: Invalid Branch ID.", entryServiceImpl.getEntries(null, -1));
}
/**
* Test method for {@link com.philip.journal.home.service.EntryServiceImpl#deleteEntry(java.util.Map, long)}.
*
* Case 1: Invalid Branch ID.
*/
@Test
public final void testDeleteEntry()
{
try {
entryServiceImpl.deleteEntry(null, -1);
} catch (final JournalException e) {
fail("Case 1: Invalid Branch ID.");
}
}
/**
* Test method for {@link com.philip.journal.home.service.EntryServiceImpl#getEntryDetail(java.util.Map, long)}.
*
* Case 1: Not found.
*/
@Test
public final void testGetEntryDetail1()
{
assertNull("Case 1: Not found.", entryServiceImpl.getEntryDetail(null, -1));
}
/** Case 2: Found. */
@Test
public final void testGetEntryDetail2()
{
when(getDaoFacade().getEntryDAO().read(anyInt())).thenReturn(new Entry());
assertNotNull("Case 2: Found.", entryServiceImpl.getEntryDetail(null, 1));
}
/**
* Test method for {@link com.philip.journal.home.service.EntryServiceImpl#moveEntry(java.util.Map, long, long)}.
*
* Case 1: Invalid new parent.
*/
@Test(expected = IllegalArgumentException.class)
public final void testMoveEntry1()
{
when(getDaoFacade().getBranchDAO().read(anyLong())).thenReturn(null);
final long entryId = 1;
final long notFoundBranchId = 666L;
try {
entryServiceImpl.moveEntry(super.getSession(), notFoundBranchId, entryId);
fail("Case 1: Invalid new parent.");
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
}
/** Case 2: Invalid Entry ID. */
@Test(expected = IllegalArgumentException.class)
public final void testMoveEntry2()
{
when(getDaoFacade().getEntryDAO().read(anyLong())).thenReturn(null);
final long notFoundEntryId = 666L;
final long branchId = 40001L;
try {
entryServiceImpl.moveEntry(super.getSession(), branchId, notFoundEntryId);
fail("Case 2: Invalid Entry ID.");
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
}
/**
* Case 3.1: Expected, description intact. <br/>
* Case 3.2: Expected, title intact. <br/>
* Case 3.3: Expected, Branch intact. <br/>
*
* @throws JournalException convenience, not subject of test.
*/
@Test
public final void testMoveEntry3()
{
final long newBranchId = 40002;
final Branch newBranch = new Branch("new branch", null);
newBranch.setBranchId(newBranchId);
final long oldBranchId = 40001;
final Branch oldBranch = new Branch("old branch", null);
oldBranch.setBranchId(oldBranchId);
final long entryId = 666L;
final String title = "title";
final String desc = "desc";
final Entry entry = new Entry(title, desc, oldBranch);
when(getDaoFacade().getEntryDAO().read(entryId)).thenReturn(entry);
when(getDaoFacade().getBranchDAO().read(newBranchId)).thenReturn(newBranch);
when(getDaoFacade().getBranchDAO().read(oldBranchId)).thenReturn(oldBranch);
entryServiceImpl.moveEntry(super.getSession(), newBranchId, entryId);
verify(getDaoFacade().getEntryDAO(), times(1)).save(entry);
final ArgumentCaptor<Entry> argument = ArgumentCaptor.forClass(Entry.class);
verify(getDaoFacade().getEntryDAO()).save(argument.capture());
assertEquals("Case 3.1: Expected, description intact.", desc, argument.getValue().getDescription());
assertEquals("Case 3.2: Expected, title intact.", title, argument.getValue().getTitle());
assertEquals("Case 3.3: Expected, Branch intact.", newBranch, argument.getValue().getBranch());
}
/**
* Test method for
* {@link com.philip.journal.home.service.EntryServiceImpl#renameEntry(java.util.Map, long, java.lang.String)} .
*
* Case 1: Entry not found.
*/
@Test(expected = IllegalArgumentException.class)
public final void testRenameEntry1()
{
when(getDaoFacade().getEntryDAO().read(anyInt())).thenReturn(null);
try {
entryServiceImpl.renameEntry(getSession(), 0, "newTitle");
fail("Case 1: Entry not found.");
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
}
/** Case 2: Null title. */
@Test(expected = IllegalArgumentException.class)
public final void testRenameEntry2()
{
entryServiceImpl.renameEntry(getSession(), 0, null);
fail("Case 2: Null title.");
}
/** Case 3: Zero length title. */
@Test(expected = IllegalArgumentException.class)
public final void testRenameEntry3()
{
entryServiceImpl.renameEntry(getSession(), 0, "");
fail("Case 3: Zero length title.");
}
/**
* Test method for
* {@link com.philip.journal.home.service.EntryServiceImpl#searchEntriesSimple(java.util.Map, java.lang.String)} .
*
* Case 1: null parameter.
*/
@Test(expected = IllegalArgumentException.class)
public final void testSearchEntriesSimple1()
{
entryServiceImpl.searchEntriesSimple(getSession(), null);
fail("Case 1: null parameter.");
}
/** Case 2: Too short search param. */
@Test(expected = IllegalArgumentException.class)
public final void testSearchEntriesSimple2()
{
entryServiceImpl.searchEntriesSimple(getSession(), "22");
fail("Case 2: Too short search param.");
}
/** Case 3: Not matched. */
@SuppressWarnings("unchecked")
@Test
public final void testSearchEntriesSimple3()
{
final List<Entry> entryList = new ArrayList<Entry>();
when(getDaoFacade().getEntryDAO().searchIlike(Matchers.anyMap())).thenReturn(entryList);
try {
assertNotNull("Case 3: Not matched.", entryServiceImpl.searchEntriesSimple(getSession(), "did not match")
.size());
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
}
/** Case 4: Matched both title and desc. */
@SuppressWarnings("unchecked")
@Test
public final void testSearchEntriesSimple4()
{
final Entry entry = new Entry("Title 333", "Desc 333", null);
final List<Entry> entryList = new ArrayList<Entry>();
entryList.add(entry);
when(getDaoFacade().getEntryDAO().searchIlike(Matchers.anyMap())).thenReturn(entryList);
try {
assertEquals("Case 4: Matched both title and desc.", 1,
entryServiceImpl.searchEntriesSimple(getSession(), "333").size());
} catch (final JournalException e) {
getCommon().failUnexpectedException(e);
}
}
@Override
protected JournalTestCommon getCommon()
{
return common;
}
}