View Javadoc
1   package fr.ifremer.reefdb.dao.referential.taxon;
2   
3   /*
4    * #%L
5    * Reef DB :: 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.quadrige3.core.test.AbstractDaoTest;
28  import fr.ifremer.reefdb.dao.ReefDbDatabaseResource;
29  import fr.ifremer.reefdb.dao.data.survey.ReefDbSurveyDao;
30  import fr.ifremer.reefdb.dto.data.measurement.MeasurementDTO;
31  import fr.ifremer.reefdb.dto.data.sampling.SamplingOperationDTO;
32  import fr.ifremer.reefdb.dto.data.survey.SurveyDTO;
33  import fr.ifremer.reefdb.service.ReefDbServiceLocator;
34  import org.apache.commons.collections4.CollectionUtils;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.junit.Before;
38  import org.junit.ClassRule;
39  import org.junit.Test;
40  
41  import java.util.Objects;
42  
43  import static org.junit.Assert.*;
44  
45  public class TaxonGroupDaoWriteTest extends AbstractDaoTest {
46  
47      private static final Log log = LogFactory.getLog(TaxonGroupDaoWriteTest.class);
48  
49      @ClassRule
50      public static final ReefDbDatabaseResource dbResource = ReefDbDatabaseResource.writeDb();
51  
52      private ReefDbTaxonGroupDao dao;
53      private ReefDbSurveyDao surveyDao;
54  
55      @Before
56      @Override
57      public void setUp() throws Exception {
58          super.setUp();
59          dao = ReefDbServiceLocator.instance().getService("reefDbTaxonGroupDao", ReefDbTaxonGroupDao.class);
60          surveyDao = ReefDbServiceLocator.instance().getService("reefDbSurveyDao", ReefDbSurveyDao.class);
61      }
62  
63      @Test
64      public void replaceTaxonGroup() {
65  
66          Integer SOURCE_TAXON_GROUP_ID = 1;
67          Integer TARGET_TAXON_GROUP_ID = 2;
68  
69          assertTrue(dao.isTaxonGroupUsedInReferential(SOURCE_TAXON_GROUP_ID));
70          assertTrue(dao.isTaxonGroupUsedInData(SOURCE_TAXON_GROUP_ID));
71          assertTrue(dao.isTaxonGroupUsedInValidatedData(SOURCE_TAXON_GROUP_ID));
72  
73          try {
74              dao.replaceTemporaryTaxonGroup(SOURCE_TAXON_GROUP_ID, TARGET_TAXON_GROUP_ID, true);
75              fail("should throw exception");
76          } catch (Exception e) {
77              assertNotNull(e);
78          }
79  
80          dao.replaceTemporaryTaxonGroup(SOURCE_TAXON_GROUP_ID, TARGET_TAXON_GROUP_ID, false);
81  
82          assertFalse(dao.isTaxonGroupUsedInReferential(SOURCE_TAXON_GROUP_ID));
83          assertTrue(dao.isTaxonGroupUsedInData(SOURCE_TAXON_GROUP_ID));
84          assertTrue(dao.isTaxonGroupUsedInValidatedData(SOURCE_TAXON_GROUP_ID));
85  
86          // load unvalidated survey
87          {
88              SurveyDTO unvalidatedSurvey = surveyDao.getSurveyById(dbResource.getFixtures().getUnvalidatedSurveyId(), false);
89              assertNotNull(unvalidatedSurvey);
90              assertNull(unvalidatedSurvey.getValidationDate());
91  
92              boolean targetTaxonGroupFound = false;
93              assertTrue(CollectionUtils.isNotEmpty(unvalidatedSurvey.getSamplingOperations()));
94              for (SamplingOperationDTO samplingOperation : unvalidatedSurvey.getSamplingOperations()) {
95  
96                  if (CollectionUtils.isNotEmpty(samplingOperation.getMeasurements())) {
97                      for (MeasurementDTO measurement : samplingOperation.getMeasurements()) {
98                          assertNull(measurement.getValidationDate());
99                          if (measurement.getTaxonGroup() != null) {
100                             assertNotEquals(SOURCE_TAXON_GROUP_ID, measurement.getTaxonGroup());
101                             if (Objects.equals(measurement.getTaxonGroup().getId(), TARGET_TAXON_GROUP_ID)) {
102                                 targetTaxonGroupFound = true;
103                             }
104                         }
105                     }
106                 }
107                 if (CollectionUtils.isNotEmpty(samplingOperation.getIndividualMeasurements())) {
108                     for (MeasurementDTO measurement : samplingOperation.getIndividualMeasurements()) {
109                         assertNull(measurement.getValidationDate());
110                         if (measurement.getTaxonGroup() != null) {
111                             assertNotEquals(SOURCE_TAXON_GROUP_ID, measurement.getTaxonGroup().getId());
112                             if (Objects.equals(measurement.getTaxonGroup().getId(), TARGET_TAXON_GROUP_ID)) {
113                                 targetTaxonGroupFound = true;
114                             }
115                         }
116                     }
117                 }
118             }
119             assertTrue(targetTaxonGroupFound);
120         }
121 
122         // load validated survey
123         {
124             SurveyDTO validatedSurvey = surveyDao.getSurveyById(dbResource.getFixtures().getValidatedSurveyId(), false);
125             assertNotNull(validatedSurvey);
126             assertNotNull(validatedSurvey.getValidationDate());
127 
128             boolean sourceTaxonGroupFound = false;
129             assertTrue(CollectionUtils.isNotEmpty(validatedSurvey.getSamplingOperations()));
130             for (SamplingOperationDTO samplingOperation : validatedSurvey.getSamplingOperations()) {
131 
132                 if (CollectionUtils.isNotEmpty(samplingOperation.getMeasurements())) {
133                     for (MeasurementDTO measurement : samplingOperation.getMeasurements()) {
134                         assertNotNull(measurement.getValidationDate());
135                         if (measurement.getTaxonGroup() != null) {
136                             assertNotEquals(TARGET_TAXON_GROUP_ID, measurement.getTaxonGroup().getId());
137                             if (Objects.equals(measurement.getTaxonGroup().getId(), SOURCE_TAXON_GROUP_ID)) {
138                                 sourceTaxonGroupFound = true;
139                             }
140                         }
141                     }
142                 }
143                 if (CollectionUtils.isNotEmpty(samplingOperation.getIndividualMeasurements())) {
144                     for (MeasurementDTO measurement : samplingOperation.getIndividualMeasurements()) {
145                         assertNotNull(measurement.getValidationDate());
146                         if (measurement.getTaxonGroup() != null) {
147                             assertNotEquals(TARGET_TAXON_GROUP_ID, measurement.getTaxonGroup().getId());
148                             if (Objects.equals(measurement.getTaxonGroup().getId(), SOURCE_TAXON_GROUP_ID)) {
149                                 sourceTaxonGroupFound = true;
150                             }
151                         }
152                     }
153                 }
154             }
155             assertTrue(sourceTaxonGroupFound);
156         }
157 
158     }
159 
160 }