package com.vst.service;
import java.util.List;
import java.util.ArrayList;
import com.vst.service.BaseManagerTestCase;
import com.vst.dao.ObjectAnswerDao;
import com.vst.model.ObjectAnswer;
import com.vst.service.impl.ObjectAnswerManagerImpl;
import org.jmock.Mock;
import org.springframework.orm.ObjectRetrievalFailureException;
public class ObjectAnswerManagerTest extends BaseManagerTestCase {
private final String objectAnswerId = "1";
private ObjectAnswerManagerImpl objectAnswerManager = new ObjectAnswerManagerImpl();
private Mock objectAnswerDao = null;
protected void setUp() throws Exception {
super.setUp();
objectAnswerDao = new Mock(ObjectAnswerDao.class);
objectAnswerManager.setObjectAnswerDao((ObjectAnswerDao) objectAnswerDao.proxy());
}
protected void tearDown() throws Exception {
super.tearDown();
objectAnswerManager = null;
}
public void testGetObjectAnswers() throws Exception {
List results = new ArrayList();
ObjectAnswer objectAnswer = new ObjectAnswer();
results.add(objectAnswer);
// set expected behavior on dao
objectAnswerDao.expects(once()).method("getObjectAnswers")
.will(returnValue(results));
List objectAnswers = objectAnswerManager.getObjectAnswers(null);
assertTrue(objectAnswers.size() == 1);
objectAnswerDao.verify();
}
public void testGetObjectAnswer() throws Exception {
// set expected behavior on dao
objectAnswerDao.expects(once()).method("getObjectAnswer")
.will(returnValue(new ObjectAnswer()));
ObjectAnswer objectAnswer = objectAnswerManager.getObjectAnswer(objectAnswerId);
assertTrue(objectAnswer != null);
objectAnswerDao.verify();
}
public void testSaveObjectAnswer() throws Exception {
ObjectAnswer objectAnswer = new ObjectAnswer();
// set expected behavior on dao
objectAnswerDao.expects(once()).method("saveObjectAnswer")
.with(same(objectAnswer)).isVoid();
objectAnswerManager.saveObjectAnswer(objectAnswer);
objectAnswerDao.verify();
}
public void testAddAndRemoveObjectAnswer() throws Exception {
ObjectAnswer objectAnswer = new ObjectAnswer();
// set required fields
// set expected behavior on dao
objectAnswerDao.expects(once()).method("saveObjectAnswer")
.with(same(objectAnswer)).isVoid();
objectAnswerManager.saveObjectAnswer(objectAnswer);
objectAnswerDao.verify();
// reset expectations
objectAnswerDao.reset();
objectAnswerDao.expects(once()).method("removeObjectAnswer").with(eq(new Long(objectAnswerId)));
objectAnswerManager.removeObjectAnswer(objectAnswerId);
objectAnswerDao.verify();
// reset expectations
objectAnswerDao.reset();
// remove
Exception ex = new ObjectRetrievalFailureException(ObjectAnswer.class, objectAnswer.getObjectAnswerId());
objectAnswerDao.expects(once()).method("removeObjectAnswer").isVoid();
objectAnswerDao.expects(once()).method("getObjectAnswer").will(throwException(ex));
objectAnswerManager.removeObjectAnswer(objectAnswerId);
try {
objectAnswerManager.getObjectAnswer(objectAnswerId);
fail("ObjectAnswer with identifier '" + objectAnswerId + "' found in database");
} catch (ObjectRetrievalFailureException e) {
assertNotNull(e.getMessage());
}
objectAnswerDao.verify();
}
}