View Javadoc
1   package fr.ifremer.dali.dao.referential.taxon;
2   
3   /*
4    * #%L
5    * Dali :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import fr.ifremer.dali.service.DaliServiceLocator;
28  import fr.ifremer.quadrige3.core.test.AbstractDaoTest;
29  import fr.ifremer.dali.dao.DaliDatabaseResource;
30  import fr.ifremer.dali.dto.referential.TaxonDTO;
31  import org.apache.commons.lang3.builder.ToStringBuilder;
32  import org.apache.commons.lang3.builder.ToStringStyle;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.junit.Assert;
36  import org.junit.Before;
37  import org.junit.ClassRule;
38  import org.junit.Test;
39  import org.springframework.dao.DataRetrievalFailureException;
40  
41  import java.time.LocalDate;
42  import java.util.List;
43  
44  import static org.junit.Assert.*;
45  
46  public class TaxonNameDaoReadTest extends AbstractDaoTest {
47  
48      private static final Log log = LogFactory.getLog(TaxonNameDaoReadTest.class);
49  
50      @ClassRule
51      public static final DaliDatabaseResource dbResource = DaliDatabaseResource.readDb();
52  
53      private DaliTaxonNameDao dao;
54  
55      @Before
56      @Override
57      public void setUp() throws Exception {
58          super.setUp();
59          dao = DaliServiceLocator.instance().getService("daliTaxonNameDao", DaliTaxonNameDao.class);
60      }
61      
62      @Test
63      public void getAllTaxonName() {
64          List<TaxonDTO> taxons = dao.getAllTaxonNames();
65          assertNotNull(taxons);
66          Assert.assertTrue(taxons.size() > 15);
67      }
68  
69      @Test
70      public void getTaxonNameByTaxonGroupId() {
71          LocalDate refDate = LocalDate.now();
72          List<TaxonDTO> taxons = (List<TaxonDTO>) dao.getAllTaxonNamesMapByTaxonGroupId(refDate).get(6);
73          assertNotNull(taxons);
74          assertEquals(2, taxons.size());
75          if (log.isDebugEnabled()) {
76              log.debug("taxons for taxon group id = 6");
77              for (TaxonDTO dto : taxons) {
78                  log.debug(ToStringBuilder.reflectionToString(dto, ToStringStyle.SHORT_PREFIX_STYLE));
79              }
80          }
81          taxons = (List<TaxonDTO>) dao.getAllTaxonNamesMapByTaxonGroupId(refDate).get(7);
82          assertNotNull(taxons);
83          assertEquals(0, taxons.size());
84          if (log.isDebugEnabled()) {
85              log.debug("no taxon for taxon group id = 7 because historical record has an end date");
86          }
87          taxons = (List<TaxonDTO>) dao.getAllTaxonNamesMapByTaxonGroupId(refDate).get(9);
88          assertNotNull(taxons);
89          assertEquals(1, taxons.size());
90          if (log.isDebugEnabled()) {
91              log.debug("taxons for taxon group id = 9");
92              for (TaxonDTO dto : taxons) {
93                  log.debug(ToStringBuilder.reflectionToString(dto, ToStringStyle.SHORT_PREFIX_STYLE));
94              }
95          }
96          taxons = (List<TaxonDTO>) dao.getAllTaxonNamesMapByTaxonGroupId(refDate).get(1);
97          assertNotNull(taxons);
98          assertEquals(0, taxons.size());
99          if (log.isDebugEnabled()) {
100             log.debug("no taxons for taxon group id = 1");
101         }
102     }
103 
104     @Test
105     public void getTaxonNameById() {
106         TaxonDTO taxon = dao.getTaxonNameByReferenceId(12);
107         assertNotNull(taxon);
108         assertEquals("Clitellata", taxon.getName());
109         
110         try {
111             dao.getTaxonNameByReferenceId(20);
112             fail("should throw a DataRetrievalFailureException");
113         } catch (DataRetrievalFailureException ex) {
114             assertNotNull(ex);
115         }
116         
117     }
118 }