[Suiviobsmer-commits] r476 - in trunk: . wao-business/src/main/java/fr/ifremer/wao/service wao-business/src/test/java/fr/ifremer/wao wao-business/src/test/java/fr/ifremer/wao/service wao-ui/src/main/java/fr/ifremer/wao/ui/base wao-ui/src/main/java/fr/ifremer/wao/ui/pages wao-ui/src/main/java/fr/ifremer/wao/ui/services wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages
Author: fdesbois Date: 2010-05-03 19:57:42 +0000 (Mon, 03 May 2010) New Revision: 476 Log: Ano #2286 : error on observer filter when initialize abstractFiltered page from Contacts Added: trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactInternalTest.java trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ContactsTest.java Modified: trunk/changelog.txt trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceContactImpl.java trunk/wao-business/src/test/java/fr/ifremer/wao/AbstractServiceTest.java trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactImplTest.java trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/base/AbstractFilteredPage.java trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Contacts.java trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/services/RequiresAuthenticationFilter.java Modified: trunk/changelog.txt =================================================================== --- trunk/changelog.txt 2010-05-03 17:48:07 UTC (rev 475) +++ trunk/changelog.txt 2010-05-03 19:57:42 UTC (rev 476) @@ -9,6 +9,7 @@ Anomalies +++++++++ +- [fdesbois] Ano #2287 : Problème de sauvegarde de l'observateur sur un contact. - [fdesbois] Ano #2282 : Doublon sur les navires éligibles pour une ligne. Mise à jour librairies Modified: trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceContactImpl.java =================================================================== --- trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceContactImpl.java 2010-05-03 17:48:07 UTC (rev 475) +++ trunk/wao-business/src/main/java/fr/ifremer/wao/service/ServiceContactImpl.java 2010-05-03 19:57:42 UTC (rev 476) @@ -171,6 +171,12 @@ Contact contact, boolean delete) throws TopiaException, NullSampleMonthException { + // Ano #2287 Temporary fix to avoid saving a null observer +// if (contact.getObserver() == null) { +// throw new NullPointerException("L'observateur n'a pas été" + +// " renseigné correctement pour la sauvegarde du contact"); +// } + ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); if (log.isDebugEnabled()) { Modified: trunk/wao-business/src/test/java/fr/ifremer/wao/AbstractServiceTest.java =================================================================== --- trunk/wao-business/src/test/java/fr/ifremer/wao/AbstractServiceTest.java 2010-05-03 17:48:07 UTC (rev 475) +++ trunk/wao-business/src/test/java/fr/ifremer/wao/AbstractServiceTest.java 2010-05-03 19:57:42 UTC (rev 476) @@ -1,20 +1,28 @@ package fr.ifremer.wao; +import fr.ifremer.wao.bean.ContactState; +import fr.ifremer.wao.bean.UserRole; import fr.ifremer.wao.entity.Boat; import fr.ifremer.wao.entity.BoatDAO; import fr.ifremer.wao.entity.Company; import fr.ifremer.wao.entity.CompanyDAO; +import fr.ifremer.wao.entity.Contact; +import fr.ifremer.wao.entity.ContactDAO; import fr.ifremer.wao.entity.ElligibleBoat; import fr.ifremer.wao.entity.ElligibleBoatDAO; import fr.ifremer.wao.entity.SampleRow; import fr.ifremer.wao.entity.SampleRowDAO; +import fr.ifremer.wao.entity.WaoUser; +import fr.ifremer.wao.entity.WaoUserDAO; import fr.ifremer.wao.service.ServiceSamplingImpl; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * AbstractServiceTest @@ -25,6 +33,9 @@ */ public abstract class AbstractServiceTest { + private static final Logger logger = + LoggerFactory.getLogger(AbstractServiceTest.class); + protected static TestManager manager; protected ServiceSamplingImpl serviceSampling; @@ -33,6 +44,9 @@ protected static final int BOAT_DEFAULT_IMMATRICULATION = 123456; protected static final String SAMPLE_ROW_DEFAULT_CODE = "2010_0001"; protected static final String COMPANY_DEFAULT_NAME = "TARTANPION"; + protected static final String OBSERVER_DEFAULT_FIRST_NAME = "Homer"; + protected static final String OBSERVER_DEFAULT_LAST_NAME = "Simpson"; + protected static final String OBSERVER_DEFAULT_LOGIN = "homer"; @BeforeClass public static void setUpClass() throws Exception { @@ -41,8 +55,12 @@ @Before public void setUp() throws Exception { + if (logger.isDebugEnabled()) { + logger.debug("Start manager"); + } manager.start(); serviceSampling = manager.getServiceSampling(); + //initialize(); } @After @@ -51,8 +69,89 @@ } /** + * Initialize data before each test. By default do nothing. + */ +// protected void initialize() { +// } + + /** * Create a company : <br /> * <ul> + * <li>first name : Homer</li> + * <li>last name : Simpson</li> + * <li>login : homer</li> + * <li>company : ref to TARTANPION</li> + * <li>role : OBSERVER</li> + * </ul> + * + * @return the new Company + * @throws TopiaException + */ + protected WaoUser createDefaultObserver() throws TopiaException { + TopiaContext transaction = manager.getContext().beginTransaction(); + WaoUser observer = null; + try { + WaoUserDAO userDAO = WaoDAOHelper.getWaoUserDAO(transaction); + observer = userDAO.create( + WaoUser.COMPANY, findDefaultCompany(), + WaoUser.LOGIN, OBSERVER_DEFAULT_LOGIN, + WaoUser.FIRST_NAME, OBSERVER_DEFAULT_FIRST_NAME, + WaoUser.LAST_NAME, OBSERVER_DEFAULT_LAST_NAME, + WaoUser.ROLE, UserRole.OBSERVER.ordinal()); + if (logger.isDebugEnabled()) { + logger.debug("Create default observer : " + observer); + } + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + return observer; + } + + /** + * Create a user : <br /> + * <ul> + * <li>first name : null</li> + * <li>last name : null</li> + * <li>login : defined with {@code login} argument</li> + * <li>company : defined with {@code company} argument</li> + * <li>role : defined with {@code role} argument</li> + * </ul> + * + * @param login String unique login for the new user + * @param role UserRole of the user + * @param company Company parent of the user + * @return the new WaoUser + * @throws TopiaException + */ + protected WaoUser createUser(String login, UserRole role, + Company company) throws TopiaException { + TopiaContext transaction = manager.getContext().beginTransaction(); + WaoUser observer = null; + try { + WaoUserDAO userDAO = WaoDAOHelper.getWaoUserDAO(transaction); + observer = userDAO.create( + WaoUser.COMPANY, company, + WaoUser.LOGIN, login, + WaoUser.FIRST_NAME, null, + WaoUser.LAST_NAME, null, + WaoUser.ROLE, role.ordinal()); + + if (logger.isDebugEnabled()) { + logger.debug("Create observer : " + observer); + } + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + return observer; + } + + /** + * Create a company : <br /> + * <ul> * <li>name : TARTANPION</li> * </ul> * @@ -127,7 +226,10 @@ row = dao.create(); row.setCode(code); row.setCompany(company); - + if (logger.isDebugEnabled()) { + logger.debug("Create sampleRow : " + row); + } + transaction.commitTransaction(); } finally { transaction.closeContext(); @@ -176,6 +278,9 @@ boat.setImmatriculation(immatriculation); boat.setName(name); boat.setActive(true); + if (logger.isDebugEnabled()) { + logger.debug("Create boat : " + boat); + } transaction.commitTransaction(); @@ -226,6 +331,44 @@ return elligibleBoat; } + /** + * Create a default contact : <br /> + * <ul> + * <li>boat : SPRINGFIELD</li> + * <li>observer : homer</li> + * <li>sampleRow : 2010_0001</li> + * <li>state : CONTACT_START</li> + * <li>companyValidation : null</li> + * <li>programValidation : null</li> + * </ul> + * + * @return the new Contact + * @throws TopiaException + */ + protected Contact createDefaultContact() throws TopiaException { + TopiaContext transaction = manager.getContext().beginTransaction(); + Contact contact = null; + try { + ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); + + // CREATE + contact = dao.create(); + contact.setBoat(findDefaultBoat()); + contact.setContactState(ContactState.CONTACT_START); + contact.setObserver(findDefaultObserver()); + contact.setSampleRow(findDefaultSampleRow()); + if (logger.isDebugEnabled()) { + logger.debug("Create contact : " + contact); + } + + transaction.commitTransaction(); + + } finally { + transaction.closeContext(); + } + return contact; + } + protected Company findDefaultCompany() throws TopiaException { Company findCompany = findCompanyByName(BOAT_DEFAULT_NAME); if (findCompany == null) { @@ -258,6 +401,16 @@ return findRow; } + /** + * Find a sampleRow existing using his unique {@code code}. Some + * {@code properties} can be added to load them if the sampleRow is + * corretly retrieve. + * + * @param code SampleRow unique code + * @param properties authorized properties : ElligibleBoat + * @return the existing SampleRow if found, null otherwise + * @throws TopiaException + */ protected SampleRow findSampleRowByCode(String code, String... properties) throws TopiaException { SampleRow row = null; @@ -280,4 +433,56 @@ } return row; } + + protected Boat findDefaultBoat() throws TopiaException { + Boat findBoat = + findBoatByImmatriculation(BOAT_DEFAULT_IMMATRICULATION); + if (findBoat == null) { + findBoat = createDefaultBoat(); + } + return findBoat; + } + + protected Boat findBoatByImmatriculation(int immatriculation) + throws TopiaException { + Boat boat = null; + TopiaContext transaction = manager.getContext().beginTransaction(); + try { + BoatDAO dao = + WaoDAOHelper.getBoatDAO(transaction); + + boat = dao.findByImmatriculation(immatriculation); + } finally { + transaction.closeContext(); + } + return boat; + } + + protected WaoUser findDefaultObserver() throws TopiaException { + WaoUser findObserver = + findUserByLogin(OBSERVER_DEFAULT_LOGIN); + if (findObserver == null) { + findObserver = createDefaultObserver(); + } + return findObserver; + } + + protected WaoUser findUserByLogin(String login) + throws TopiaException { + WaoUser user = null; + TopiaContext transaction = manager.getContext().beginTransaction(); + try { + WaoUserDAO dao = + WaoDAOHelper.getWaoUserDAO(transaction); + + user = dao.findByLogin(login); + // Load company + if (user != null) { + user.getCompany(); + } + } finally { + transaction.closeContext(); + } + return user; + } } Modified: trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactImplTest.java =================================================================== --- trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactImplTest.java 2010-05-03 17:48:07 UTC (rev 475) +++ trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactImplTest.java 2010-05-03 19:57:42 UTC (rev 476) @@ -1,812 +1,67 @@ package fr.ifremer.wao.service; -import com.csvreader.CsvReader; -import fr.ifremer.wao.TestManager; -import fr.ifremer.wao.WaoBusinessException; -import fr.ifremer.wao.WaoDAOHelper; -import fr.ifremer.wao.bean.ContactState; -import fr.ifremer.wao.bean.ImportResults; +import fr.ifremer.wao.AbstractServiceTest; +import fr.ifremer.wao.bean.ContactFilter; +import fr.ifremer.wao.bean.ContactFilterImpl; import fr.ifremer.wao.bean.UserRole; -import fr.ifremer.wao.entity.Boat; -import fr.ifremer.wao.entity.BoatDAO; -import fr.ifremer.wao.entity.Company; -import fr.ifremer.wao.entity.CompanyDAO; import fr.ifremer.wao.entity.Contact; -import fr.ifremer.wao.entity.ContactDAO; -import fr.ifremer.wao.entity.SampleMonth; -import fr.ifremer.wao.entity.SampleMonthDAO; -import fr.ifremer.wao.entity.SampleRow; -import fr.ifremer.wao.entity.SampleRowDAO; import fr.ifremer.wao.entity.WaoUser; -import fr.ifremer.wao.entity.WaoUserDAO; -import fr.ifremer.wao.io.ContactInput; -import fr.ifremer.wao.io.ImportRefusedException; -import fr.ifremer.wao.io.WaoCsvHeader.BOAT; -import fr.ifremer.wao.io.WaoCsvHeader.CONTACT; -import fr.ifremer.wao.io.WaoCsvHeader.SAMPLING; -import fr.ifremer.wao.bean.ContactStatus.NullSampleMonthException; -import java.io.IOException; -import java.io.InputStream; -import java.text.ParseException; -import java.util.Date; -import java.util.List; -import org.junit.After; +import java.util.Map; +import org.apache.commons.collections.CollectionUtils; +import org.junit.Assert; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; -import org.nuiton.topia.framework.TopiaQuery; -import org.nuiton.util.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; /** * ServiceContactImplTest * - * Created: 16 avr. 2010 + * Created: 3 mai 2010 * * @author fdesbois - * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ + * $Id$ */ -public class ServiceContactImplTest { +public class ServiceContactImplTest extends AbstractServiceTest { - private static TestManager manager; - private static final Logger logger = LoggerFactory.getLogger(ServiceContactImplTest.class); - private ServiceContactImpl service; + private ServiceContact service; - @BeforeClass - public static void setUpClass() throws Exception { - manager = new TestManager(); - } - @Before - public void setUp() throws Exception { - manager.start(); - MockitoAnnotations.initMocks(this); + public void initialize() { + logger.info("initialize ServiceContactImplTest"); service = manager.getServiceContact(); - sampleMonth = null; - sampleRow = null; - boat = null; - company = null; - observer = null; } - @After - public void tearDown() throws Exception { - manager.stop(); - } - - // Real data - protected SampleRow sampleRow; - protected SampleMonth sampleMonth; - protected Boat boat; - protected Company company; - protected WaoUser observer; - - // Mock data - @Mock - protected Contact contact; - @Mock - protected Contact contactReference; - @Mock - protected WaoUser creator; - @Mock - protected CsvReader reader; - - protected void prepareData(boolean needBoat, boolean needObserver) - throws TopiaException { - logger.info("Prepare data :"); - TopiaContext transaction = manager.getContext().beginTransaction(); - try { - - if (needObserver) { - CompanyDAO companyDAO = WaoDAOHelper.getCompanyDAO(transaction); - company = companyDAO.create(Company.NAME, "TARTANPION"); - - WaoUserDAO userDAO = WaoDAOHelper.getWaoUserDAO(transaction); - observer = userDAO.create( - WaoUser.COMPANY, company, - WaoUser.LOGIN, "jmichmuche", - WaoUser.FIRST_NAME, "Jean", - WaoUser.LAST_NAME, "Michmuche", - WaoUser.ROLE, UserRole.OBSERVER.ordinal()); - - when(contact.getObserver()).thenReturn(observer); - } - - SampleRowDAO rowDAO = - WaoDAOHelper.getSampleRowDAO(transaction); - logger.info("SampleRow : 2010_0001"); - sampleRow = rowDAO.create(SampleRow.CODE, "2010_0001", - SampleRow.COMPANY, company); - - SampleMonthDAO monthDAO = - WaoDAOHelper.getSampleMonthDAO(transaction); - logger.info("SampleMonth : 03/2010 _ row 2010_0001"); - Date monthDate = DateUtils.createDate(1, 3, 2010); - sampleMonth = monthDAO.create(SampleMonth.SAMPLE_ROW, sampleRow, - SampleMonth.PERIOD_DATE, monthDate); - monthDate = DateUtils.createDate(1, 4, 2010); - monthDAO.create(SampleMonth.SAMPLE_ROW, sampleRow, - SampleMonth.PERIOD_DATE, monthDate); - - Date contactDate = DateUtils.createDate(18, 3, 2010); - when(contact.getTideBeginDate()).thenReturn(contactDate); - when(contact.getSampleRow()).thenReturn(sampleRow); - - if (needBoat) { - BoatDAO boatDAO = WaoDAOHelper.getBoatDAO(transaction); - boat = boatDAO.create(Boat.IMMATRICULATION, 175846); - when(contact.getBoat()).thenReturn(boat); - } - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - } - @Test - public void testGetExistingSampleMonthForContact() - throws TopiaException, NullSampleMonthException { - logger.info("testGetExistingSampleMonthForContact"); + public void testGetContactsFilteredByObserver() throws TopiaException { + logger.info("getContactsFilteredByObserver"); -// /** PREPARE DATA **/ -// prepareData(false, false); -// -// /** EXEC METHOD **/ -// logger.info("test 1 : SampleMonth exists for contact"); -// TopiaContext transaction = manager.getContext().beginTransaction(); -// try { -// SampleMonthDAO monthDAO = -// WaoDAOHelper.getSampleMonthDAO(transaction); -// -// // Contact is valid for the sampleMonth -// -// SampleMonth result = -// service.getExistingSampleMonthForContact(monthDAO, -// contact.getSampleRow(), contact.getTideBeginDate()); -// -// assertEquals(sampleMonth, result); -// -// } finally { -// transaction.closeContext(); -// } -// -// logger.info("test 2 : contact tideBeginDate doesn't match with any" + -// " SampleMonth"); -// transaction = manager.getContext().beginTransaction(); -// try { -// SampleMonthDAO monthDAO = -// WaoDAOHelper.getSampleMonthDAO(transaction); -// -// Date contactDate = DateUtils.createDate(18, 10, 2010); -// when(contact.getTideBeginDate()).thenReturn(contactDate); -// -// service.getExistingSampleMonthForContact(monthDAO, -// contact.getSampleRow(), contact.getTideBeginDate()); -// } catch (Exception eee) { -// logger.error("Error : " + eee.getMessage()); -// assertEquals(NullSampleMonthException.class, eee.getClass()); -// } finally { -// transaction.closeContext(); -// } - } - - @Test - public void testUpdateSampleMonthTidesValue() throws TopiaException, - NullSampleMonthException { - logger.info("testUpdateSampleMonthTidesValue"); - /** PREPARE DATA **/ - prepareData(false, false); - Date tideBeginDate = new Date(contact.getTideBeginDate().getTime()); - when(contactReference.getTideBeginDate()).thenReturn(tideBeginDate); - when(contactReference.getSampleRow()).thenReturn(sampleRow); - // WARN : by default mockito doesn't initialize Boolean type with null - when(contactReference.getValidationProgram()).thenReturn(null); - when(contact.getValidationProgram()).thenReturn(null); + Contact contact = createDefaultContact(); /** EXEC METHOD **/ - logger.info("test 1 : ContactState changed from null to BOARDING_DONE" + - " -> increment estimated tides value"); - TopiaContext transaction = manager.getContext().beginTransaction(); - try { - when(contact.getContactState()). - thenReturn(ContactState.BOARDING_DONE); + ContactFilter filter = new ContactFilterImpl(); + filter.setObserver(findDefaultObserver()); - service.updateSampleMonthTidesValue(transaction, null, - contact, false); + logger.info("test 1 : Ok good filter, contact is correctly returned"); + Map<String, Contact> results = service.getContacts(filter); - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } + Assert.assertEquals(1, results.size()); + Contact contactFind = results.get(contact.getTopiaId()); + Assert.assertEquals(contact, contactFind); - logger.info("test 1 : verification sampleMonth estimated tides = 1"); - checkTidesValue(1, false); - - logger.info("test 2 : ContactState changed from BOARDING_DONE to " + - "BOAT_REFUSED -> decrement estimated tides value"); - transaction = manager.getContext().beginTransaction(); - try { - when(contactReference.getContactState()). - thenReturn(ContactState.BOARDING_DONE); - - when(contact.getContactState()). - thenReturn(ContactState.BOAT_REFUSED); - - service.updateSampleMonthTidesValue(transaction, contactReference, - contact, false); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } + logger.info("test 2 : Bad filter, no contact is returned"); + WaoUser observer2 = createUser("marge", + UserRole.OBSERVER, findDefaultCompany()); - logger.info("test 2 : verification sampleMonth estimated tides = 0"); - checkTidesValue(0, false); + filter.setObserver(observer2); - // Reset contactState to BOARDING_DONE for next test - when(contact.getContactState()). - thenReturn(ContactState.BOARDING_DONE); - - logger.info("test 3 : CompanyValidation changed from null to " + - "FALSE -> do nothing"); - transaction = manager.getContext().beginTransaction(); - try { - when(contactReference.getValidationCompany()).thenReturn(null); - - when(contact.getValidationCompany()).thenReturn(Boolean.FALSE); - - service.updateSampleMonthTidesValue(transaction, contactReference, - contact, true); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - logger.info("test 3 : verification sampleMonth real tides = 0"); - checkTidesValue(0, true); - - logger.info("test 4 : CompanyValidation changed from FALSE to " + - "TRUE -> increment real tides value"); - transaction = manager.getContext().beginTransaction(); - try { - when(contactReference.getValidationCompany()). - thenReturn(Boolean.FALSE); - - when(contact.getValidationCompany()).thenReturn(Boolean.TRUE); - - service.updateSampleMonthTidesValue(transaction, contactReference, - contact, true); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - logger.info("test 4 : verification sampleMonth real tides = 1"); - checkTidesValue(1, true); - - // ContactReference after saving contact will normally have this value : - when(contactReference.getValidationCompany()).thenReturn(Boolean.TRUE); - - logger.info("test 5 : CompanyValidation doesn't changed but " + - "ProgramValidation is set to FALSE -> decrement real tides"); - transaction = manager.getContext().beginTransaction(); - try { - when(contact.getValidationProgram()).thenReturn(Boolean.FALSE); - - service.updateSampleMonthTidesValue(transaction, contactReference, - contact, true); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - logger.info("test 5 : verification sampleMonth real tides = 0"); - checkTidesValue(0, true); - - logger.info("test 6 : Prepare data for delete case"); - // PREPARE DATA : update estimated to 1 and real to 1 - transaction = manager.getContext().beginTransaction(); - try { - // Will update estimatedTides up to 1 - // contact has already BOARDING_DONE - when(contactReference.getContactState()). - thenReturn(ContactState.BOARDING_EXPECTED); - - // Will update realTides up to 1 - // contact has already validationCompany = TRUE - when(contactReference.getValidationCompany()).thenReturn(null); - when(contact.getValidationProgram()).thenReturn(Boolean.TRUE); - - service.updateSampleMonthTidesValue(transaction, contactReference, - contact, true); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - // ContactReference after saving contact will normally have this values - when(contactReference.getValidationProgram()).thenReturn(Boolean.TRUE); - when(contactReference.getValidationCompany()).thenReturn(Boolean.TRUE); - - logger.info("test 6 : Delete contact (new contact is null) -> " + - "decrement realTides and estimatedTides"); - transaction = manager.getContext().beginTransaction(); - try { - when(contactReference.getContactState()). - thenReturn(ContactState.BOARDING_DONE); - service.updateSampleMonthTidesValue(transaction, contactReference, - null, true); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - logger.info("test 6 : verification sampleMonth real tides = 0" + - "and estimated tides = 0"); - checkTidesValue(0, true); - checkTidesValue(0, false); - - logger.info("test 7 : SampleMonth will change from 03/2010 to 04/2010" + - " -> decrement estimated tides from oldMonth"); - transaction = manager.getContext().beginTransaction(); - try { - // contactReference BOARDING_DONE, validations true/true - // contact BOARDING_DONE, validations true/true - when(contactReference.getContactState()). - thenReturn(ContactState.BOARDING_DONE); - when(contact.getContactState()). - thenReturn(ContactState.BOARDING_DONE); - when(contact.getTideBeginDate()). - thenReturn(DateUtils.createDate(3, 4, 2010)); - - service.updateSampleMonthTidesValue(transaction, contactReference, - contact, true); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - logger.info("test 7 : verification old sampleMonth estimated tides = 0"); - checkTidesValue(0, false); - - logger.info("test 7 : verification new sampleMonth estimated " + - "tides = 1 and real tides = 1"); - transaction = manager.getContext().beginTransaction(); - try { - SampleMonthDAO dao = WaoDAOHelper.getSampleMonthDAO(transaction); - - TopiaQuery query = dao.createQuery(); - query.add(SampleMonth.PERIOD_DATE, DateUtils.createDate(1, 4, 2010)); - - SampleMonth month = dao.findByQuery(query); - - assertEquals(1, month.getEstimatedTidesValue()); - assertEquals(1, month.getRealTidesValue()); - - } finally { - transaction.closeContext(); - } + results = service.getContacts(filter); + Assert.assertEquals(0, results.size()); } - - @Test - public void testDecrementSampleMonthContactRefused() - throws TopiaException, NullSampleMonthException { - logger.info("testDecrementSampleMonthContactRefused"); - - /** PREPARE DATA **/ - prepareData(false, false); - - // contactReference as the same tideBeginDate and sampleRow as contact - Date tideBeginDate = new Date(contact.getTideBeginDate().getTime()); - when(contactReference.getTideBeginDate()).thenReturn(tideBeginDate); - when(contactReference.getSampleRow()).thenReturn(sampleRow); - - // Initialize correct old status in contactReference - when(contactReference.getValidationCompany()).thenReturn(Boolean.TRUE); - when(contactReference.getContactState()). - thenReturn(ContactState.BOARDING_DONE); - when(contactReference.getValidationProgram()).thenReturn(null); - - - // Initialize data in sampleMonth - TopiaContext transaction = manager.getContext().beginTransaction(); - try { - SampleMonthDAO dao = WaoDAOHelper.getSampleMonthDAO(transaction); - - sampleMonth.addEstimatedTideTime(1); - sampleMonth.addRealTideTime(1); - - dao.update(sampleMonth); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - // both estimated and real as value = 1 - checkTidesValue(1, false); - checkTidesValue(1, true); - - /** EXEC METHOD **/ - logger.info("test 1 : validationProgram null -> FALSE, estimated -1" + - " and real -1"); - transaction = manager.getContext().beginTransaction(); - try { - when(contact.getValidationCompany()).thenReturn(Boolean.TRUE); - when(contact.getContactState()). - thenReturn(ContactState.BOARDING_DONE); - when(contact.getValidationProgram()).thenReturn(Boolean.FALSE); - - service.updateSampleMonthTidesValue(transaction, contactReference, - contact, true); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - checkTidesValue(0, false); - checkTidesValue(0, true); - - logger.info("test 2 : validationProgram FALSE -> null, estimated +1" + - " and real +1"); - transaction = manager.getContext().beginTransaction(); - try { - when(contactReference.getValidationProgram()). - thenReturn(Boolean.FALSE); - when(contact.getValidationProgram()).thenReturn(null); - - service.updateSampleMonthTidesValue(transaction, contactReference, - contact, true); - - transaction.commitTransaction(); - } finally { - transaction.closeContext(); - } - - checkTidesValue(1, false); - checkTidesValue(1, true); - } - - protected void checkTidesValue(int expected, boolean real) - throws TopiaException { - TopiaContext transaction = manager.getContext().beginTransaction(); - try { - SampleMonthDAO dao = WaoDAOHelper.getSampleMonthDAO(transaction); - - SampleMonth result = dao.findByTopiaId(sampleMonth.getTopiaId()); - int test = real ? result.getRealTidesValue() : - result.getEstimatedTidesValue(); - assertEquals(expected, test); - } finally { - transaction.closeContext(); - } - } - - @Test - public void testLoadContactCsv() throws TopiaException, - WaoBusinessException, - IOException, - ImportRefusedException, - ImportRefusedException, - ParseException { - logger.info("testLoadContactCsv"); - - /** PREPARE DATA **/ - prepareData(true, true); - - when(reader.get(CONTACT.CONT_ETAT.name())). - thenReturn(ContactState.BOARDING_DONE.libelle()); - when(reader.get(CONTACT.OBSERV_ID.name())). - thenReturn(observer.getLogin()); - when(reader.get(SAMPLING.PLAN_CODE.name())). - thenReturn(sampleRow.getCode()); - when(reader.get(BOAT.NAVS_COD.name())). - thenReturn(String.valueOf(boat.getImmatriculation())); - when(reader.get(CONTACT.CONT_DEBUT_MAREE.name())). - thenReturn("22/03/2010"); - when(reader.get(CONTACT.CONT_FIN_MAREE.name())). - thenReturn("22/03/10"); - when(reader.get(CONTACT.CONT_NB_OBSERV.name())). - thenReturn("1"); - when(reader.get(CONTACT.CONT_MAM_CAPT.name())). - thenReturn(""); - when(reader.get(CONTACT.CONT_MAM_OBS.name())). - thenReturn("X"); - when(reader.get(CONTACT.CONT_ALLEGRO.name())). - thenReturn("25/03/2010"); - when(reader.get(CONTACT.CONT_SOCIETE_VALID.name())). - thenReturn("A"); - when(reader.get(CONTACT.CONT_PROGRAM_VALID.name())). - thenReturn(""); - when(reader.get(CONTACT.CONT_CREATION.name())). - thenReturn("18/03/10"); - when(reader.get(CONTACT.CONT_CODE.name())). - thenReturn(""); - - when(creator.getCompany()).thenReturn(company); - when(creator.isAdmin()).thenReturn(false); - when(creator.getLogin()).thenReturn(observer.getLogin()); - - /** EXEC METHOD **/ - - logger.info("test 1 : load contact by Creator jmichmuche -> no error"); - ContactInput result = new ContactInput(); - TopiaContext transaction = manager.getContext().beginTransaction(); - try { - result.initialize(transaction, creator, 1); - service.loadContactCsv(reader, result, false); - Date createDate = DateUtils.createDate(18,3,2010); - assertEquals(createDate, result.getTopiaCreateDate()); - assertEquals(ContactState.BOARDING_DONE, result.getContactState()); - assertEquals(observer, result.getObserver()); - assertEquals(sampleRow, result.getSampleRow()); - assertEquals(boat, result.getBoat()); - assertEquals(DateUtils.createDate(22,3,2010), - result.getTideBeginDate()); - assertEquals(DateUtils.createDate(22,3,2010), - result.getTideEndDate()); - assertEquals(1, result.getNbObservants()); - assertFalse(result.getMammalsCapture()); - assertTrue(result.getMammalsObservation()); - assertEquals(DateUtils.createDate(25,3,2010), - result.getDataInputDate()); - // No validation update : creator is not an admin - assertNull(result.getValidationCompany()); - assertNull(result.getValidationProgram()); - } finally { - transaction.closeContext(); - } - - logger.info("test 2 : Error on boat"); - when(reader.get(BOAT.NAVS_COD.name())).thenReturn("2222"); - checkImportError(Contact.BOAT); - // Reinit boat - when(reader.get(BOAT.NAVS_COD.name())). - thenReturn(String.valueOf(boat.getImmatriculation())); - - logger.info("test 3 : Error on company"); - when(creator.getCompany()).thenReturn(null); - checkImportError(WaoUser.COMPANY); - // Reinit creator company - when(creator.getCompany()).thenReturn(company); - - logger.info("test 4 : Error on observer"); - when(reader.get(CONTACT.OBSERV_ID.name())).thenReturn("FAKE"); - checkImportError(Contact.OBSERVER); - // Reinit observer - when(reader.get(CONTACT.OBSERV_ID.name())). - thenReturn(observer.getLogin()); - - logger.info("test 5 : Error on sampleRow"); - when(reader.get(SAMPLING.PLAN_CODE.name())).thenReturn("FAKE"); - checkImportError(Contact.SAMPLE_ROW); - // Reinit sampleRow - when(reader.get(SAMPLING.PLAN_CODE.name())). - thenReturn(sampleRow.getCode()); - - logger.info("test 6 : Error on state"); - when(reader.get(CONTACT.CONT_ETAT.name())).thenReturn("FAKE"); - checkImportError(Contact.STATE); - // Reinit state - when(reader.get(CONTACT.CONT_ETAT.name())). - thenReturn(ContactState.BOARDING_DONE.libelle()); - } - - protected void checkImportError(String field) throws TopiaException { - ContactInput result = new ContactInput(); - TopiaContext transaction = manager.getContext().beginTransaction(); - try { - result.initialize(transaction, creator, 1); - service.loadContactCsv(reader, result, false); - } catch (Exception eee) { - logger.error("Error : " + eee.getMessage()); - assertEquals(ImportRefusedException.class, eee.getClass()); - ImportRefusedException ex = (ImportRefusedException)eee; - assertEquals(field, ex.getField()); - } finally { - transaction.closeContext(); - } - } - - @Test - public void testCreateOrUpdateContact() - throws TopiaException, NullSampleMonthException { - logger.info("testCreateOrUpdateContact"); - - /** PREPARE DATA **/ - prepareData(true, true); - - // Don't use a mock in this case - Contact newContact = service.getNewContact(observer, sampleRow, boat); - newContact.setTopiaCreateDate(DateUtils.createDate(2, 3, 2010)); - - /** EXEC METHOD **/ - - logger.info("test 1 : Create new contact with state CONTACT_START"); - TopiaContext transaction = null; - String newTopiaId = null; - try { - transaction = manager.getContext().beginTransaction(); - - ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); - - service.createOrUpdateContact(dao, newContact, null, false); - transaction.commitTransaction(); - - newTopiaId = newContact.getTopiaId(); - assertNotNull(newTopiaId); - } finally { - if (transaction != null) { - transaction.closeContext(); - } - } - - logger.info("test 1 : Check contact existence"); - try { - transaction = manager.getContext().beginTransaction(); - - ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); - - Contact existingContact = dao.findByTopiaId(newTopiaId); - assertNotNull(existingContact); - assertEquals(boat, existingContact.getBoat()); - assertEquals(sampleRow, existingContact.getSampleRow()); - assertEquals(observer, existingContact.getObserver()); - assertEquals(ContactState.CONTACT_START, - existingContact.getContactState()); - } finally { - if (transaction != null) { - transaction.closeContext(); - } - } - - logger.info("test 2 : Update the contact with state BOARDING_DONE, " + - "tideBeginDate, tideEndDate and NbObservants"); - Date tideBeginDate = DateUtils.createDate(18,3,2010); - Date tideEndDate = DateUtils.createDate(19,3,2010); - try { - transaction = manager.getContext().beginTransaction(); - - newContact.setContactState(ContactState.BOARDING_DONE); - newContact.setTideBeginDate(tideBeginDate); - newContact.setTideEndDate(tideEndDate); - newContact.setNbObservants(1); - - ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); - Contact oldContact = dao.findByTopiaId(newTopiaId); - - service.createOrUpdateContact(dao, newContact, oldContact, false); - transaction.commitTransaction(); - } finally { - if (transaction != null) { - transaction.closeContext(); - } - } - - logger.info("test 2 : Check update contact"); - try { - transaction = manager.getContext().beginTransaction(); - - ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); - - Contact existingContact = dao.findByTopiaId(newTopiaId); - assertNotNull(existingContact); - assertEquals(1, existingContact.getNbObservants()); - assertEquals(tideBeginDate, existingContact.getTideBeginDate()); - assertEquals(tideEndDate, existingContact.getTideEndDate()); - assertEquals(ContactState.BOARDING_DONE, - existingContact.getContactState()); - - // Will have an update on sampleMonth 03/2010 for estimatedTides - SampleMonthDAO monthDAO = - WaoDAOHelper.getSampleMonthDAO(transaction); - SampleMonth existingMonth = - monthDAO.findByTopiaId(sampleMonth.getTopiaId()); - assertNotNull(existingMonth); - assertEquals(1, existingMonth.getEstimatedTidesValue()); - } finally { - if (transaction != null) { - transaction.closeContext(); - } - } - } - - @Test - public void testImportContactCsv() - throws TopiaException, WaoBusinessException, IOException { - logger.info("testImportContactCsv"); - - /** PREPARE DATA **/ - prepareData(true, true); - - when(creator.getCompany()).thenReturn(company); - when(creator.isAdmin()).thenReturn(false); - when(creator.getLogin()).thenReturn(observer.getLogin()); - - /** EXEC METHOD **/ - logger.info("test 1 : Import new contact with observer creator"); - InputStream input = - getClass().getResourceAsStream("/import/contacts.csv"); - try { - ImportResults results = service.importContactCsv(creator, input); - assertEquals(1, results.getNbRowsImported()); - assertEquals(0, results.getNbRowsRefused()); - assertTrue(results.getErrors().isEmpty()); - } finally { - input.close(); - } - - logger.info("test 1 : Check creation of the contact"); - TopiaContext transaction = null; - try { - transaction = manager.getContext().beginTransaction(); - - ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); - - List<Contact> contactsFound = dao.findAll(); - assertEquals(1, contactsFound.size()); - Contact contactFound = contactsFound.get(0); - assertNull(contactFound.getValidationCompany()); - assertNull(contactFound.getValidationProgram()); - } finally { - if (transaction != null) { - transaction.closeContext(); - } - } - - logger.info("test 2 : Import new contact with super admin creator"); - when(creator.getCompany()).thenReturn(company); - when(creator.isAdmin()).thenReturn(true); - when(creator.getLogin()).thenReturn("admin"); - - input = getClass().getResourceAsStream("/import/contacts.csv"); - try { - ImportResults results = service.importContactCsv(creator, input); - assertEquals(1, results.getNbRowsImported()); - assertEquals(0, results.getNbRowsRefused()); - assertTrue(results.getErrors().isEmpty()); - } finally { - input.close(); - } - - logger.info("test 2 : Check update validations of the contact"); - try { - transaction = manager.getContext().beginTransaction(); - - ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); - - List<Contact> contactsFound = dao.findAll(); - assertEquals(1, contactsFound.size()); - Contact contactFound = contactsFound.get(0); - assertTrue(contactFound.getValidationCompany()); - assertTrue(contactFound.getValidationProgram()); - } finally { - if (transaction != null) { - transaction.closeContext(); - } - } - } } Copied: trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactInternalTest.java (from rev 472, trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactImplTest.java) =================================================================== --- trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactInternalTest.java (rev 0) +++ trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactInternalTest.java 2010-05-03 19:57:42 UTC (rev 476) @@ -0,0 +1,812 @@ + +package fr.ifremer.wao.service; + +import com.csvreader.CsvReader; +import fr.ifremer.wao.TestManager; +import fr.ifremer.wao.WaoBusinessException; +import fr.ifremer.wao.WaoDAOHelper; +import fr.ifremer.wao.bean.ContactState; +import fr.ifremer.wao.bean.ImportResults; +import fr.ifremer.wao.bean.UserRole; +import fr.ifremer.wao.entity.Boat; +import fr.ifremer.wao.entity.BoatDAO; +import fr.ifremer.wao.entity.Company; +import fr.ifremer.wao.entity.CompanyDAO; +import fr.ifremer.wao.entity.Contact; +import fr.ifremer.wao.entity.ContactDAO; +import fr.ifremer.wao.entity.SampleMonth; +import fr.ifremer.wao.entity.SampleMonthDAO; +import fr.ifremer.wao.entity.SampleRow; +import fr.ifremer.wao.entity.SampleRowDAO; +import fr.ifremer.wao.entity.WaoUser; +import fr.ifremer.wao.entity.WaoUserDAO; +import fr.ifremer.wao.io.ContactInput; +import fr.ifremer.wao.io.ImportRefusedException; +import fr.ifremer.wao.io.WaoCsvHeader.BOAT; +import fr.ifremer.wao.io.WaoCsvHeader.CONTACT; +import fr.ifremer.wao.io.WaoCsvHeader.SAMPLING; +import fr.ifremer.wao.bean.ContactStatus.NullSampleMonthException; +import java.io.IOException; +import java.io.InputStream; +import java.text.ParseException; +import java.util.Date; +import java.util.List; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.util.DateUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import static org.mockito.Mockito.*; +import static org.junit.Assert.*; + +/** + * ServiceContactImplTest + * + * Created: 16 avr. 2010 + * + * @author fdesbois + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class ServiceContactInternalTest { + + private static TestManager manager; + + private static final Logger logger = + LoggerFactory.getLogger(ServiceContactInternalTest.class); + + private ServiceContactImpl service; + + @BeforeClass + public static void setUpClass() throws Exception { + manager = new TestManager(); + } + + @Before + public void setUp() throws Exception { + manager.start(); + MockitoAnnotations.initMocks(this); + service = manager.getServiceContact(); + sampleMonth = null; + sampleRow = null; + boat = null; + company = null; + observer = null; + } + + @After + public void tearDown() throws Exception { + manager.stop(); + } + + // Real data + protected SampleRow sampleRow; + protected SampleMonth sampleMonth; + protected Boat boat; + protected Company company; + protected WaoUser observer; + + // Mock data + @Mock + protected Contact contact; + @Mock + protected Contact contactReference; + @Mock + protected WaoUser creator; + @Mock + protected CsvReader reader; + + protected void prepareData(boolean needBoat, boolean needObserver) + throws TopiaException { + logger.info("Prepare data :"); + TopiaContext transaction = manager.getContext().beginTransaction(); + try { + + if (needObserver) { + CompanyDAO companyDAO = WaoDAOHelper.getCompanyDAO(transaction); + company = companyDAO.create(Company.NAME, "TARTANPION"); + + WaoUserDAO userDAO = WaoDAOHelper.getWaoUserDAO(transaction); + observer = userDAO.create( + WaoUser.COMPANY, company, + WaoUser.LOGIN, "jmichmuche", + WaoUser.FIRST_NAME, "Jean", + WaoUser.LAST_NAME, "Michmuche", + WaoUser.ROLE, UserRole.OBSERVER.ordinal()); + + when(contact.getObserver()).thenReturn(observer); + } + + SampleRowDAO rowDAO = + WaoDAOHelper.getSampleRowDAO(transaction); + logger.info("SampleRow : 2010_0001"); + sampleRow = rowDAO.create(SampleRow.CODE, "2010_0001", + SampleRow.COMPANY, company); + + SampleMonthDAO monthDAO = + WaoDAOHelper.getSampleMonthDAO(transaction); + logger.info("SampleMonth : 03/2010 _ row 2010_0001"); + Date monthDate = DateUtils.createDate(1, 3, 2010); + sampleMonth = monthDAO.create(SampleMonth.SAMPLE_ROW, sampleRow, + SampleMonth.PERIOD_DATE, monthDate); + monthDate = DateUtils.createDate(1, 4, 2010); + monthDAO.create(SampleMonth.SAMPLE_ROW, sampleRow, + SampleMonth.PERIOD_DATE, monthDate); + + Date contactDate = DateUtils.createDate(18, 3, 2010); + when(contact.getTideBeginDate()).thenReturn(contactDate); + when(contact.getSampleRow()).thenReturn(sampleRow); + + if (needBoat) { + BoatDAO boatDAO = WaoDAOHelper.getBoatDAO(transaction); + boat = boatDAO.create(Boat.IMMATRICULATION, 175846); + when(contact.getBoat()).thenReturn(boat); + } + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + } + + @Test + public void testGetExistingSampleMonthForContact() + throws TopiaException, NullSampleMonthException { + logger.info("testGetExistingSampleMonthForContact"); + +// /** PREPARE DATA **/ +// prepareData(false, false); +// +// /** EXEC METHOD **/ +// logger.info("test 1 : SampleMonth exists for contact"); +// TopiaContext transaction = manager.getContext().beginTransaction(); +// try { +// SampleMonthDAO monthDAO = +// WaoDAOHelper.getSampleMonthDAO(transaction); +// +// // Contact is valid for the sampleMonth +// +// SampleMonth result = +// service.getExistingSampleMonthForContact(monthDAO, +// contact.getSampleRow(), contact.getTideBeginDate()); +// +// assertEquals(sampleMonth, result); +// +// } finally { +// transaction.closeContext(); +// } +// +// logger.info("test 2 : contact tideBeginDate doesn't match with any" + +// " SampleMonth"); +// transaction = manager.getContext().beginTransaction(); +// try { +// SampleMonthDAO monthDAO = +// WaoDAOHelper.getSampleMonthDAO(transaction); +// +// Date contactDate = DateUtils.createDate(18, 10, 2010); +// when(contact.getTideBeginDate()).thenReturn(contactDate); +// +// service.getExistingSampleMonthForContact(monthDAO, +// contact.getSampleRow(), contact.getTideBeginDate()); +// } catch (Exception eee) { +// logger.error("Error : " + eee.getMessage()); +// assertEquals(NullSampleMonthException.class, eee.getClass()); +// } finally { +// transaction.closeContext(); +// } + } + + @Test + public void testUpdateSampleMonthTidesValue() throws TopiaException, + NullSampleMonthException { + logger.info("testUpdateSampleMonthTidesValue"); + + /** PREPARE DATA **/ + prepareData(false, false); + Date tideBeginDate = new Date(contact.getTideBeginDate().getTime()); + when(contactReference.getTideBeginDate()).thenReturn(tideBeginDate); + when(contactReference.getSampleRow()).thenReturn(sampleRow); + // WARN : by default mockito doesn't initialize Boolean type with null + when(contactReference.getValidationProgram()).thenReturn(null); + when(contact.getValidationProgram()).thenReturn(null); + + /** EXEC METHOD **/ + logger.info("test 1 : ContactState changed from null to BOARDING_DONE" + + " -> increment estimated tides value"); + TopiaContext transaction = manager.getContext().beginTransaction(); + try { + when(contact.getContactState()). + thenReturn(ContactState.BOARDING_DONE); + + service.updateSampleMonthTidesValue(transaction, null, + contact, false); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + logger.info("test 1 : verification sampleMonth estimated tides = 1"); + checkTidesValue(1, false); + + logger.info("test 2 : ContactState changed from BOARDING_DONE to " + + "BOAT_REFUSED -> decrement estimated tides value"); + transaction = manager.getContext().beginTransaction(); + try { + when(contactReference.getContactState()). + thenReturn(ContactState.BOARDING_DONE); + + when(contact.getContactState()). + thenReturn(ContactState.BOAT_REFUSED); + + service.updateSampleMonthTidesValue(transaction, contactReference, + contact, false); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + logger.info("test 2 : verification sampleMonth estimated tides = 0"); + checkTidesValue(0, false); + + // Reset contactState to BOARDING_DONE for next test + when(contact.getContactState()). + thenReturn(ContactState.BOARDING_DONE); + + logger.info("test 3 : CompanyValidation changed from null to " + + "FALSE -> do nothing"); + transaction = manager.getContext().beginTransaction(); + try { + when(contactReference.getValidationCompany()).thenReturn(null); + + when(contact.getValidationCompany()).thenReturn(Boolean.FALSE); + + service.updateSampleMonthTidesValue(transaction, contactReference, + contact, true); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + logger.info("test 3 : verification sampleMonth real tides = 0"); + checkTidesValue(0, true); + + logger.info("test 4 : CompanyValidation changed from FALSE to " + + "TRUE -> increment real tides value"); + transaction = manager.getContext().beginTransaction(); + try { + when(contactReference.getValidationCompany()). + thenReturn(Boolean.FALSE); + + when(contact.getValidationCompany()).thenReturn(Boolean.TRUE); + + service.updateSampleMonthTidesValue(transaction, contactReference, + contact, true); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + logger.info("test 4 : verification sampleMonth real tides = 1"); + checkTidesValue(1, true); + + // ContactReference after saving contact will normally have this value : + when(contactReference.getValidationCompany()).thenReturn(Boolean.TRUE); + + logger.info("test 5 : CompanyValidation doesn't changed but " + + "ProgramValidation is set to FALSE -> decrement real tides"); + transaction = manager.getContext().beginTransaction(); + try { + when(contact.getValidationProgram()).thenReturn(Boolean.FALSE); + + service.updateSampleMonthTidesValue(transaction, contactReference, + contact, true); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + logger.info("test 5 : verification sampleMonth real tides = 0"); + checkTidesValue(0, true); + + logger.info("test 6 : Prepare data for delete case"); + // PREPARE DATA : update estimated to 1 and real to 1 + transaction = manager.getContext().beginTransaction(); + try { + // Will update estimatedTides up to 1 + // contact has already BOARDING_DONE + when(contactReference.getContactState()). + thenReturn(ContactState.BOARDING_EXPECTED); + + // Will update realTides up to 1 + // contact has already validationCompany = TRUE + when(contactReference.getValidationCompany()).thenReturn(null); + when(contact.getValidationProgram()).thenReturn(Boolean.TRUE); + + service.updateSampleMonthTidesValue(transaction, contactReference, + contact, true); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + // ContactReference after saving contact will normally have this values + when(contactReference.getValidationProgram()).thenReturn(Boolean.TRUE); + when(contactReference.getValidationCompany()).thenReturn(Boolean.TRUE); + + logger.info("test 6 : Delete contact (new contact is null) -> " + + "decrement realTides and estimatedTides"); + transaction = manager.getContext().beginTransaction(); + try { + when(contactReference.getContactState()). + thenReturn(ContactState.BOARDING_DONE); + service.updateSampleMonthTidesValue(transaction, contactReference, + null, true); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + logger.info("test 6 : verification sampleMonth real tides = 0" + + "and estimated tides = 0"); + checkTidesValue(0, true); + checkTidesValue(0, false); + + logger.info("test 7 : SampleMonth will change from 03/2010 to 04/2010" + + " -> decrement estimated tides from oldMonth"); + transaction = manager.getContext().beginTransaction(); + try { + // contactReference BOARDING_DONE, validations true/true + // contact BOARDING_DONE, validations true/true + when(contactReference.getContactState()). + thenReturn(ContactState.BOARDING_DONE); + when(contact.getContactState()). + thenReturn(ContactState.BOARDING_DONE); + when(contact.getTideBeginDate()). + thenReturn(DateUtils.createDate(3, 4, 2010)); + + service.updateSampleMonthTidesValue(transaction, contactReference, + contact, true); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + logger.info("test 7 : verification old sampleMonth estimated tides = 0"); + checkTidesValue(0, false); + + logger.info("test 7 : verification new sampleMonth estimated " + + "tides = 1 and real tides = 1"); + transaction = manager.getContext().beginTransaction(); + try { + SampleMonthDAO dao = WaoDAOHelper.getSampleMonthDAO(transaction); + + TopiaQuery query = dao.createQuery(); + query.add(SampleMonth.PERIOD_DATE, DateUtils.createDate(1, 4, 2010)); + + SampleMonth month = dao.findByQuery(query); + + assertEquals(1, month.getEstimatedTidesValue()); + assertEquals(1, month.getRealTidesValue()); + + } finally { + transaction.closeContext(); + } + } + + @Test + public void testDecrementSampleMonthContactRefused() + throws TopiaException, NullSampleMonthException { + logger.info("testDecrementSampleMonthContactRefused"); + + /** PREPARE DATA **/ + prepareData(false, false); + + // contactReference as the same tideBeginDate and sampleRow as contact + Date tideBeginDate = new Date(contact.getTideBeginDate().getTime()); + when(contactReference.getTideBeginDate()).thenReturn(tideBeginDate); + when(contactReference.getSampleRow()).thenReturn(sampleRow); + + // Initialize correct old status in contactReference + when(contactReference.getValidationCompany()).thenReturn(Boolean.TRUE); + when(contactReference.getContactState()). + thenReturn(ContactState.BOARDING_DONE); + when(contactReference.getValidationProgram()).thenReturn(null); + + + // Initialize data in sampleMonth + TopiaContext transaction = manager.getContext().beginTransaction(); + try { + SampleMonthDAO dao = WaoDAOHelper.getSampleMonthDAO(transaction); + + sampleMonth.addEstimatedTideTime(1); + sampleMonth.addRealTideTime(1); + + dao.update(sampleMonth); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + // both estimated and real as value = 1 + checkTidesValue(1, false); + checkTidesValue(1, true); + + /** EXEC METHOD **/ + logger.info("test 1 : validationProgram null -> FALSE, estimated -1" + + " and real -1"); + transaction = manager.getContext().beginTransaction(); + try { + when(contact.getValidationCompany()).thenReturn(Boolean.TRUE); + when(contact.getContactState()). + thenReturn(ContactState.BOARDING_DONE); + when(contact.getValidationProgram()).thenReturn(Boolean.FALSE); + + service.updateSampleMonthTidesValue(transaction, contactReference, + contact, true); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + checkTidesValue(0, false); + checkTidesValue(0, true); + + logger.info("test 2 : validationProgram FALSE -> null, estimated +1" + + " and real +1"); + transaction = manager.getContext().beginTransaction(); + try { + when(contactReference.getValidationProgram()). + thenReturn(Boolean.FALSE); + when(contact.getValidationProgram()).thenReturn(null); + + service.updateSampleMonthTidesValue(transaction, contactReference, + contact, true); + + transaction.commitTransaction(); + } finally { + transaction.closeContext(); + } + + checkTidesValue(1, false); + checkTidesValue(1, true); + } + + protected void checkTidesValue(int expected, boolean real) + throws TopiaException { + TopiaContext transaction = manager.getContext().beginTransaction(); + try { + SampleMonthDAO dao = WaoDAOHelper.getSampleMonthDAO(transaction); + + SampleMonth result = dao.findByTopiaId(sampleMonth.getTopiaId()); + int test = real ? result.getRealTidesValue() : + result.getEstimatedTidesValue(); + assertEquals(expected, test); + } finally { + transaction.closeContext(); + } + } + + @Test + public void testLoadContactCsv() throws TopiaException, + WaoBusinessException, + IOException, + ImportRefusedException, + ImportRefusedException, + ParseException { + logger.info("testLoadContactCsv"); + + /** PREPARE DATA **/ + prepareData(true, true); + + when(reader.get(CONTACT.CONT_ETAT.name())). + thenReturn(ContactState.BOARDING_DONE.libelle()); + when(reader.get(CONTACT.OBSERV_ID.name())). + thenReturn(observer.getLogin()); + when(reader.get(SAMPLING.PLAN_CODE.name())). + thenReturn(sampleRow.getCode()); + when(reader.get(BOAT.NAVS_COD.name())). + thenReturn(String.valueOf(boat.getImmatriculation())); + when(reader.get(CONTACT.CONT_DEBUT_MAREE.name())). + thenReturn("22/03/2010"); + when(reader.get(CONTACT.CONT_FIN_MAREE.name())). + thenReturn("22/03/10"); + when(reader.get(CONTACT.CONT_NB_OBSERV.name())). + thenReturn("1"); + when(reader.get(CONTACT.CONT_MAM_CAPT.name())). + thenReturn(""); + when(reader.get(CONTACT.CONT_MAM_OBS.name())). + thenReturn("X"); + when(reader.get(CONTACT.CONT_ALLEGRO.name())). + thenReturn("25/03/2010"); + when(reader.get(CONTACT.CONT_SOCIETE_VALID.name())). + thenReturn("A"); + when(reader.get(CONTACT.CONT_PROGRAM_VALID.name())). + thenReturn(""); + when(reader.get(CONTACT.CONT_CREATION.name())). + thenReturn("18/03/10"); + when(reader.get(CONTACT.CONT_CODE.name())). + thenReturn(""); + + when(creator.getCompany()).thenReturn(company); + when(creator.isAdmin()).thenReturn(false); + when(creator.getLogin()).thenReturn(observer.getLogin()); + + /** EXEC METHOD **/ + + logger.info("test 1 : load contact by Creator jmichmuche -> no error"); + ContactInput result = new ContactInput(); + TopiaContext transaction = manager.getContext().beginTransaction(); + try { + result.initialize(transaction, creator, 1); + service.loadContactCsv(reader, result, false); + Date createDate = DateUtils.createDate(18,3,2010); + assertEquals(createDate, result.getTopiaCreateDate()); + assertEquals(ContactState.BOARDING_DONE, result.getContactState()); + assertEquals(observer, result.getObserver()); + assertEquals(sampleRow, result.getSampleRow()); + assertEquals(boat, result.getBoat()); + assertEquals(DateUtils.createDate(22,3,2010), + result.getTideBeginDate()); + assertEquals(DateUtils.createDate(22,3,2010), + result.getTideEndDate()); + assertEquals(1, result.getNbObservants()); + assertFalse(result.getMammalsCapture()); + assertTrue(result.getMammalsObservation()); + assertEquals(DateUtils.createDate(25,3,2010), + result.getDataInputDate()); + // No validation update : creator is not an admin + assertNull(result.getValidationCompany()); + assertNull(result.getValidationProgram()); + } finally { + transaction.closeContext(); + } + + logger.info("test 2 : Error on boat"); + when(reader.get(BOAT.NAVS_COD.name())).thenReturn("2222"); + checkImportError(Contact.BOAT); + // Reinit boat + when(reader.get(BOAT.NAVS_COD.name())). + thenReturn(String.valueOf(boat.getImmatriculation())); + + logger.info("test 3 : Error on company"); + when(creator.getCompany()).thenReturn(null); + checkImportError(WaoUser.COMPANY); + // Reinit creator company + when(creator.getCompany()).thenReturn(company); + + logger.info("test 4 : Error on observer"); + when(reader.get(CONTACT.OBSERV_ID.name())).thenReturn("FAKE"); + checkImportError(Contact.OBSERVER); + // Reinit observer + when(reader.get(CONTACT.OBSERV_ID.name())). + thenReturn(observer.getLogin()); + + logger.info("test 5 : Error on sampleRow"); + when(reader.get(SAMPLING.PLAN_CODE.name())).thenReturn("FAKE"); + checkImportError(Contact.SAMPLE_ROW); + // Reinit sampleRow + when(reader.get(SAMPLING.PLAN_CODE.name())). + thenReturn(sampleRow.getCode()); + + logger.info("test 6 : Error on state"); + when(reader.get(CONTACT.CONT_ETAT.name())).thenReturn("FAKE"); + checkImportError(Contact.STATE); + // Reinit state + when(reader.get(CONTACT.CONT_ETAT.name())). + thenReturn(ContactState.BOARDING_DONE.libelle()); + } + + protected void checkImportError(String field) throws TopiaException { + ContactInput result = new ContactInput(); + TopiaContext transaction = manager.getContext().beginTransaction(); + try { + result.initialize(transaction, creator, 1); + service.loadContactCsv(reader, result, false); + } catch (Exception eee) { + logger.error("Error : " + eee.getMessage()); + assertEquals(ImportRefusedException.class, eee.getClass()); + ImportRefusedException ex = (ImportRefusedException)eee; + assertEquals(field, ex.getField()); + } finally { + transaction.closeContext(); + } + } + + @Test + public void testCreateOrUpdateContact() + throws TopiaException, NullSampleMonthException { + logger.info("testCreateOrUpdateContact"); + + /** PREPARE DATA **/ + prepareData(true, true); + + // Don't use a mock in this case + Contact newContact = service.getNewContact(observer, sampleRow, boat); + newContact.setTopiaCreateDate(DateUtils.createDate(2, 3, 2010)); + + /** EXEC METHOD **/ + + logger.info("test 1 : Create new contact with state CONTACT_START"); + TopiaContext transaction = null; + String newTopiaId = null; + try { + transaction = manager.getContext().beginTransaction(); + + ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); + + service.createOrUpdateContact(dao, newContact, null, false); + transaction.commitTransaction(); + + newTopiaId = newContact.getTopiaId(); + assertNotNull(newTopiaId); + } finally { + if (transaction != null) { + transaction.closeContext(); + } + } + + logger.info("test 1 : Check contact existence"); + try { + transaction = manager.getContext().beginTransaction(); + + ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); + + Contact existingContact = dao.findByTopiaId(newTopiaId); + assertNotNull(existingContact); + assertEquals(boat, existingContact.getBoat()); + assertEquals(sampleRow, existingContact.getSampleRow()); + assertEquals(observer, existingContact.getObserver()); + assertEquals(ContactState.CONTACT_START, + existingContact.getContactState()); + } finally { + if (transaction != null) { + transaction.closeContext(); + } + } + + logger.info("test 2 : Update the contact with state BOARDING_DONE, " + + "tideBeginDate, tideEndDate and NbObservants"); + Date tideBeginDate = DateUtils.createDate(18,3,2010); + Date tideEndDate = DateUtils.createDate(19,3,2010); + try { + transaction = manager.getContext().beginTransaction(); + + newContact.setContactState(ContactState.BOARDING_DONE); + newContact.setTideBeginDate(tideBeginDate); + newContact.setTideEndDate(tideEndDate); + newContact.setNbObservants(1); + + ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); + Contact oldContact = dao.findByTopiaId(newTopiaId); + + service.createOrUpdateContact(dao, newContact, oldContact, false); + transaction.commitTransaction(); + } finally { + if (transaction != null) { + transaction.closeContext(); + } + } + + logger.info("test 2 : Check update contact"); + try { + transaction = manager.getContext().beginTransaction(); + + ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); + + Contact existingContact = dao.findByTopiaId(newTopiaId); + assertNotNull(existingContact); + assertEquals(1, existingContact.getNbObservants()); + assertEquals(tideBeginDate, existingContact.getTideBeginDate()); + assertEquals(tideEndDate, existingContact.getTideEndDate()); + assertEquals(ContactState.BOARDING_DONE, + existingContact.getContactState()); + + // Will have an update on sampleMonth 03/2010 for estimatedTides + SampleMonthDAO monthDAO = + WaoDAOHelper.getSampleMonthDAO(transaction); + SampleMonth existingMonth = + monthDAO.findByTopiaId(sampleMonth.getTopiaId()); + assertNotNull(existingMonth); + assertEquals(1, existingMonth.getEstimatedTidesValue()); + } finally { + if (transaction != null) { + transaction.closeContext(); + } + } + } + + @Test + public void testImportContactCsv() + throws TopiaException, WaoBusinessException, IOException { + logger.info("testImportContactCsv"); + + /** PREPARE DATA **/ + prepareData(true, true); + + when(creator.getCompany()).thenReturn(company); + when(creator.isAdmin()).thenReturn(false); + when(creator.getLogin()).thenReturn(observer.getLogin()); + + /** EXEC METHOD **/ + logger.info("test 1 : Import new contact with observer creator"); + InputStream input = + getClass().getResourceAsStream("/import/contacts.csv"); + try { + ImportResults results = service.importContactCsv(creator, input); + assertEquals(1, results.getNbRowsImported()); + assertEquals(0, results.getNbRowsRefused()); + assertTrue(results.getErrors().isEmpty()); + } finally { + input.close(); + } + + logger.info("test 1 : Check creation of the contact"); + TopiaContext transaction = null; + try { + transaction = manager.getContext().beginTransaction(); + + ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); + + List<Contact> contactsFound = dao.findAll(); + assertEquals(1, contactsFound.size()); + Contact contactFound = contactsFound.get(0); + assertNull(contactFound.getValidationCompany()); + assertNull(contactFound.getValidationProgram()); + } finally { + if (transaction != null) { + transaction.closeContext(); + } + } + + logger.info("test 2 : Import new contact with super admin creator"); + when(creator.getCompany()).thenReturn(company); + when(creator.isAdmin()).thenReturn(true); + when(creator.getLogin()).thenReturn("admin"); + + input = getClass().getResourceAsStream("/import/contacts.csv"); + try { + ImportResults results = service.importContactCsv(creator, input); + assertEquals(1, results.getNbRowsImported()); + assertEquals(0, results.getNbRowsRefused()); + assertTrue(results.getErrors().isEmpty()); + } finally { + input.close(); + } + + logger.info("test 2 : Check update validations of the contact"); + try { + transaction = manager.getContext().beginTransaction(); + + ContactDAO dao = WaoDAOHelper.getContactDAO(transaction); + + List<Contact> contactsFound = dao.findAll(); + assertEquals(1, contactsFound.size()); + Contact contactFound = contactsFound.get(0); + assertTrue(contactFound.getValidationCompany()); + assertTrue(contactFound.getValidationProgram()); + } finally { + if (transaction != null) { + transaction.closeContext(); + } + } + } +} Property changes on: trunk/wao-business/src/test/java/fr/ifremer/wao/service/ServiceContactInternalTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: svn:mergeinfo + Modified: trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/base/AbstractFilteredPage.java =================================================================== --- trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/base/AbstractFilteredPage.java 2010-05-03 17:48:07 UTC (rev 475) +++ trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/base/AbstractFilteredPage.java 2010-05-03 19:57:42 UTC (rev 476) @@ -358,6 +358,7 @@ public void onSelectedFromSearch() throws WaoException { if (log.isDebugEnabled()) { log.debug("REFRESH FILTERS DATA"); + log.debug("observerSelect : " + observerSelect); } SampleRow row = getSampleRowSelectModel().findObject(sampleRowId); getFilter().setSampleRow(row); @@ -370,6 +371,10 @@ if (observerSelect) { WaoUser observer = getObserverSelectModel().findObject(observerId); + if (log.isDebugEnabled()) { + log.debug("Observer selected : " + + (observer != null ? observer.getFullName() : "null")); + } getFilter().setObserver(observer); } } Modified: trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Contacts.java =================================================================== --- trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Contacts.java 2010-05-03 17:48:07 UTC (rev 475) +++ trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/pages/Contacts.java 2010-05-03 19:57:42 UTC (rev 476) @@ -37,9 +37,7 @@ import fr.ifremer.wao.entity.Contact; import fr.ifremer.wao.entity.SampleRow; import fr.ifremer.wao.entity.WaoUser; -import fr.ifremer.wao.service.ServiceBoat; import fr.ifremer.wao.service.ServiceContact; -import fr.ifremer.wao.service.ServiceSampling; import fr.ifremer.wao.service.ServiceUser; import fr.ifremer.wao.ui.base.AbstractFilteredPage; import fr.ifremer.wao.ui.data.ContactDataSource; @@ -113,7 +111,7 @@ contactsForm.clearErrors(); // Initialize filters if needed if (isFiltersVisible()) { - initSelectFilters(true, false, true); + initSelectFilters(true, true, true); } // Initialize fullView to true for admin user if (fullView == null) { @@ -171,7 +169,7 @@ Object onActionFromShowFilters() { // Initialize filters - initSelectFilters(true, false, true); + initSelectFilters(true, true, true); return filtersZone.getBody(); } Modified: trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/services/RequiresAuthenticationFilter.java =================================================================== --- trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/services/RequiresAuthenticationFilter.java 2010-05-03 17:48:07 UTC (rev 475) +++ trunk/wao-ui/src/main/java/fr/ifremer/wao/ui/services/RequiresAuthenticationFilter.java 2010-05-03 19:57:42 UTC (rev 476) @@ -47,8 +47,8 @@ this.response = response; this.serviceAuthentication = serviceAuthentication; - if (logger.isDebugEnabled()) { - logger.debug("Construct"); + if (logger.isTraceEnabled()) { + logger.trace("Construct"); } } @@ -57,8 +57,8 @@ ComponentEventRequestParameters parameters, ComponentRequestHandler handler) throws IOException { - if (logger.isDebugEnabled()) { - logger.debug("handleComponentEvent"); + if (logger.isTraceEnabled()) { + logger.trace("handleComponentEvent"); } if (dispatchedToLoginPage(parameters.getActivePageName())) { @@ -73,8 +73,8 @@ public void handlePageRender(PageRenderRequestParameters parameters, ComponentRequestHandler handler) throws IOException { - if (logger.isDebugEnabled()) { - logger.debug("handlePageRender"); + if (logger.isTraceEnabled()) { + logger.trace("handlePageRender"); } if (dispatchedToLoginPage(parameters.getLogicalPageName())) { @@ -88,12 +88,12 @@ Component page = componentSource.getPage(pageName); - if (logger.isDebugEnabled()) { - logger.debug("Page name : " + pageName); - logger.debug("Page class : " + page.getClass()); - logger.debug("RequiresLogin annotation : " + + if (logger.isTraceEnabled()) { + logger.trace("Page name : " + pageName); + logger.trace("Page class : " + page.getClass()); + logger.trace("RequiresLogin annotation : " + page.getClass().isAnnotationPresent(RequiresAuthentication.class)); - logger.debug("User in session : " + + logger.trace("User in session : " + serviceAuthentication.isUserConnected()); } @@ -105,10 +105,10 @@ if (serviceAuthentication.isUserConnected()) { WaoUser user = serviceAuthentication.getUserConnected(); - if (logger.isDebugEnabled()) { - logger.debug("User connected : " + user); - logger.debug("User role : " + user.getUserRole().name()); - logger.debug("User allowed : " + + if (logger.isTraceEnabled()) { + logger.trace("User connected : " + user); + logger.trace("User role : " + user.getUserRole().name()); + logger.trace("User allowed : " + serviceAuthentication.isAllowed(page.getClass())); } @@ -122,8 +122,8 @@ Link link = pageRender.createPageRenderLinkWithContext(redirectPage, pageName); - if (logger.isDebugEnabled()) { - logger.debug("Redirection to " + redirectPage.getSimpleName() + + if (logger.isTraceEnabled()) { + logger.trace("Redirection to " + redirectPage.getSimpleName() + " page..."); } Added: trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ContactsTest.java =================================================================== --- trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ContactsTest.java (rev 0) +++ trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ContactsTest.java 2010-05-03 19:57:42 UTC (rev 476) @@ -0,0 +1,77 @@ + +package test.fr.ifremer.wao.ui.pages; + +import com.formos.tapestry.testify.core.ForComponents; +import fr.ifremer.wao.bean.ContactFilter; +import fr.ifremer.wao.entity.Contact; +import fr.ifremer.wao.entity.WaoUser; +import fr.ifremer.wao.service.ServiceContact; +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import test.fr.ifremer.wao.ui.AbstractApplicationTest; + +/** + * ContactsTest + * + * Created: 3 mai 2010 + * + * @author fdesbois + * $Id$ + */ +public class ContactsTest extends AbstractApplicationTest { + + private static final Logger logger = + LoggerFactory.getLogger(ContactsTest.class); + + @Mock + protected WaoUser observer; + + @Mock + protected ContactFilter filter; + + @Mock + @ForComponents + protected ServiceContact serviceContact; + + @Test + public void testObserverFilter() { + logger.info("## testObserverFilter"); + + /** PREPARE DATA **/ + Mockito.when(observer.getFullName()).thenReturn("Jean Michmuche"); + Mockito.when(observer.getTopiaId()).thenReturn("ID1"); + + + WaoUser observer2 = Mockito.mock(WaoUser.class); + Mockito.when(observer2.getFullName()).thenReturn("Paul Newman"); + Mockito.when(observer2.getTopiaId()).thenReturn("ID2"); + + + Contact contact1 = Mockito.mock(Contact.class); + Contact contact2 = Mockito.mock(Contact.class); + + Mockito.when(contact1.getObserver()).thenReturn(observer); + Mockito.when(contact1.getTopiaId()).thenReturn("C1"); + Mockito.when(contact2.getObserver()).thenReturn(observer2); + Mockito.when(contact1.getTopiaId()).thenReturn("C2"); + + Map<String, Contact> contacts = new HashMap<String, Contact>(); + contacts.put("C1", contact1); + contacts.put("C2", contact2); + + Mockito.when(filter.getObserver()).thenReturn(observer); + + + Mockito.when(serviceContact.getNbContacts(filter)).thenReturn(2); + + Mockito.when(serviceContact.getContacts(filter)).thenReturn(contacts); + + // TODO-FD20100503 : to test page render need more data + } + +} Property changes on: trunk/wao-ui/src/test/java/test/fr/ifremer/wao/ui/pages/ContactsTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL
participants (1)
-
fdesbois@users.labs.libre-entreprise.org