View Javadoc
1   package fr.ifremer.reefdb.dao.administration.user;
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  import fr.ifremer.quadrige3.core.test.AbstractDaoTest;
27  import fr.ifremer.reefdb.dao.ReefDbDatabaseResource;
28  import fr.ifremer.reefdb.dao.data.survey.ReefDbSurveyDao;
29  import fr.ifremer.reefdb.dto.data.measurement.MeasurementDTO;
30  import fr.ifremer.reefdb.dto.data.sampling.SamplingOperationDTO;
31  import fr.ifremer.reefdb.dto.data.survey.SurveyDTO;
32  import fr.ifremer.reefdb.service.ReefDbServiceLocator;
33  import org.apache.commons.collections4.CollectionUtils;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.hibernate.exception.ConstraintViolationException;
37  import org.junit.Before;
38  import org.junit.ClassRule;
39  import org.junit.Test;
40  
41  import static org.junit.Assert.*;
42  
43  public class DepartementDaoWriteTest extends AbstractDaoTest {
44  
45      private static final Log log = LogFactory.getLog(DepartementDaoWriteTest.class);
46  
47      @ClassRule
48      public static final ReefDbDatabaseResource dbResource = ReefDbDatabaseResource.writeDb();
49  
50      private ReefDbDepartmentDao departmentDao;
51      private ReefDbSurveyDao surveyDao;
52  
53      @Before
54      @Override
55      public void setUp() throws Exception {
56          super.setUp();
57          departmentDao = ReefDbServiceLocator.instance().getService("reefDbDepartmentDao", ReefDbDepartmentDao.class);
58          surveyDao = ReefDbServiceLocator.instance().getService("reefDbSurveyDao", ReefDbSurveyDao.class);
59      }
60  
61      @Test
62      public void replaceDepartment() {
63  
64          Integer SOURCE_DEP_ID = 10;
65          Integer TARGET_DEP_ID = 11;
66  
67          assertTrue(departmentDao.isDepartmentUsedInProgram(SOURCE_DEP_ID));
68          assertTrue(departmentDao.isDepartmentUsedInRules(SOURCE_DEP_ID));
69          assertTrue(departmentDao.isDepartmentUsedInData(SOURCE_DEP_ID));
70          assertTrue(departmentDao.isDepartmentUsedInValidatedData(SOURCE_DEP_ID));
71  
72          try {
73              departmentDao.replaceTemporaryDepartmentFks(SOURCE_DEP_ID, TARGET_DEP_ID, true);
74              fail("should throw ConstraintViolationException");
75          } catch (ConstraintViolationException e) {
76              assertNotNull(e);
77          }
78  
79          departmentDao.replaceTemporaryDepartmentFks(SOURCE_DEP_ID, TARGET_DEP_ID, false);
80  
81          assertTrue(departmentDao.isDepartmentUsedInProgram(SOURCE_DEP_ID));
82          assertTrue(departmentDao.isDepartmentUsedInRules(SOURCE_DEP_ID));
83          assertTrue(departmentDao.isDepartmentUsedInData(SOURCE_DEP_ID));
84          assertTrue(departmentDao.isDepartmentUsedInValidatedData(SOURCE_DEP_ID));
85  
86  
87          // load unvalidated survey to assert new department
88          SurveyDTO unvalidatedSurvey = surveyDao.getSurveyById(dbResource.getFixtures().getUnvalidatedSurveyId(), false);
89          assertNotNull(unvalidatedSurvey);
90          assertNull(unvalidatedSurvey.getValidationDate());
91          assertNotNull(unvalidatedSurvey.getRecorderDepartment());
92          assertEquals(TARGET_DEP_ID, unvalidatedSurvey.getRecorderDepartment().getId());
93  
94          assertTrue(CollectionUtils.isNotEmpty(unvalidatedSurvey.getSamplingOperations()));
95          for (SamplingOperationDTO samplingOperation: unvalidatedSurvey.getSamplingOperations()) {
96              assertNotNull(samplingOperation.getSamplingDepartment());
97              assertEquals(TARGET_DEP_ID, samplingOperation.getSamplingDepartment().getId());
98  
99              if (CollectionUtils.isNotEmpty(samplingOperation.getMeasurements())) {
100                 for (MeasurementDTO measurement: samplingOperation.getMeasurements()) {
101                     assertNull(measurement.getValidationDate());
102                     assertNotNull(measurement.getAnalyst());
103                     assertEquals(TARGET_DEP_ID, measurement.getAnalyst().getId());
104                 }
105             }
106             if (CollectionUtils.isNotEmpty(samplingOperation.getIndividualMeasurements())) {
107                 for (MeasurementDTO measurement: samplingOperation.getIndividualMeasurements()) {
108                     assertNull(measurement.getValidationDate());
109                     assertNotNull(measurement.getAnalyst());
110                     assertEquals(TARGET_DEP_ID, measurement.getAnalyst().getId());
111                 }
112             }
113         }
114 
115         // load validated survey to assert new department
116         SurveyDTO validatedSurvey = surveyDao.getSurveyById(dbResource.getFixtures().getValidatedSurveyId(), false);
117         assertNotNull(validatedSurvey);
118         assertNotNull(validatedSurvey.getValidationDate());
119         assertNotNull(validatedSurvey.getRecorderDepartment());
120         assertEquals(SOURCE_DEP_ID, validatedSurvey.getRecorderDepartment().getId());
121 
122         assertTrue(CollectionUtils.isNotEmpty(validatedSurvey.getSamplingOperations()));
123         for (SamplingOperationDTO samplingOperation: validatedSurvey.getSamplingOperations()) {
124             assertNotNull(samplingOperation.getSamplingDepartment());
125             assertEquals(SOURCE_DEP_ID, samplingOperation.getSamplingDepartment().getId());
126 
127             if (CollectionUtils.isNotEmpty(samplingOperation.getMeasurements())) {
128                 for (MeasurementDTO measurement: samplingOperation.getMeasurements()) {
129                     assertNotNull(measurement.getValidationDate());
130                     assertNotNull(measurement.getAnalyst());
131                     assertEquals(SOURCE_DEP_ID, measurement.getAnalyst().getId());
132                 }
133             }
134             if (CollectionUtils.isNotEmpty(samplingOperation.getIndividualMeasurements())) {
135                 for (MeasurementDTO measurement: samplingOperation.getIndividualMeasurements()) {
136                     assertNotNull(measurement.getValidationDate());
137                     assertNotNull(measurement.getAnalyst());
138                     assertEquals(SOURCE_DEP_ID, measurement.getAnalyst().getId());
139                 }
140             }
141         }
142 
143     }
144 
145 }