View Javadoc
1   package fr.ifremer.dali.dao.administration.strategy;
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.dao.DaliDatabaseResource;
28  import fr.ifremer.dali.dto.configuration.programStrategy.AppliedStrategyDTO;
29  import fr.ifremer.dali.dto.configuration.programStrategy.PmfmStrategyDTO;
30  import fr.ifremer.dali.dto.configuration.programStrategy.StrategyDTO;
31  import fr.ifremer.dali.service.DaliServiceLocator;
32  import fr.ifremer.quadrige3.core.test.AbstractDaoTest;
33  import org.apache.commons.lang3.builder.ToStringBuilder;
34  import org.apache.commons.lang3.builder.ToStringStyle;
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.time.LocalDate;
42  import java.util.List;
43  
44  import static org.junit.Assert.*;
45  
46  /**
47   *
48   * @author Ludovic
49   */
50  public class StrategyDaoReadTest extends AbstractDaoTest {
51  
52      private static final Log log = LogFactory.getLog(StrategyDaoReadTest.class);
53  
54      @ClassRule
55      public static final DaliDatabaseResource dbResource = DaliDatabaseResource.readDb();
56  
57      private DaliStrategyDao strategyDao;
58  
59      @Before
60      @Override
61      public void setUp() throws Exception {
62          super.setUp();
63          strategyDao = DaliServiceLocator.instance().getService("daliStrategyDao", DaliStrategyDao.class);
64      }
65  
66      @Test
67      public void getStrategiesByProgramCode() {
68  
69          if (log.isDebugEnabled()) {
70              log.debug("getStrategiesByProgramCode");
71          }
72  
73          List<StrategyDTO> strategies = strategyDao.getStrategiesByProgramCode("REMIS");
74          assertNotNull(strategies);
75          assertEquals(2, strategies.size());
76  
77          if (log.isDebugEnabled()) {
78              for (StrategyDTO dto : strategies) {
79                  log.debug(ToStringBuilder.reflectionToString(dto, ToStringStyle.SHORT_PREFIX_STYLE));
80              }
81          }
82  
83          // TODO inspect deeply
84          strategies = strategyDao.getStrategiesByProgramCode("REBENT");
85          assertNotNull(strategies);
86          assertEquals(0, strategies.size());
87  
88      }
89  
90      @Test
91      public void getAppliedStrategiesByProgramCode() {
92  
93          if (log.isDebugEnabled()) {
94              log.debug("getAppliedStrategiesByProgramCode");
95          }
96  
97          List<AppliedStrategyDTO> locations = strategyDao.getAppliedStrategiesByProgramCode("REMIS");
98          assertNotNull(locations);
99          assertEquals(3, locations.size());
100 
101         if (log.isDebugEnabled()) {
102             for (AppliedStrategyDTO dto : locations) {
103                 log.debug(ToStringBuilder.reflectionToString(dto, ToStringStyle.SHORT_PREFIX_STYLE));
104             }
105         }
106 
107         int nbLocation1 = 0;
108         int nbLocation2 = 0;
109         for (AppliedStrategyDTO location : locations) {
110             if (location.getId() == 1) {
111                 nbLocation1++;
112             }
113             else if (location.getId() == 2) {
114                 nbLocation2++;
115             }
116         }
117         assertEquals(2, nbLocation1);
118         assertEquals(1, nbLocation2);
119     }
120 
121     @Test
122     public void getAppliedStrategiesByStrategyId() {
123 
124         if (log.isDebugEnabled()) {
125             log.debug("getAppliedStrategiesByStrategyId");
126         }
127 
128         List<AppliedStrategyDTO> locations = strategyDao.getAppliedStrategiesByStrategyId(3);
129         assertNotNull(locations);
130         assertEquals(1, locations.size());
131 
132         if (log.isDebugEnabled()) {
133             for (AppliedStrategyDTO dto : locations) {
134                 log.debug(ToStringBuilder.reflectionToString(dto, ToStringStyle.SHORT_PREFIX_STYLE));
135             }
136         }
137 
138         assertEquals((Integer) 1, locations.get(0).getId());
139         assertNull(locations.get(0).getStartDate());
140         assertNull(locations.get(0).getEndDate());
141         assertNotNull(locations.get(0).getSamplingDepartment());
142         assertEquals((Integer) 5, locations.get(0).getSamplingDepartment().getId());
143     }
144 
145     @Test
146     public void getAppliedPeriodsByStrategyId() {
147 
148         if (log.isDebugEnabled()) {
149             log.debug("getAppliedPeriodsByStrategyId");
150         }
151 
152         List<AppliedStrategyDTO> locations = strategyDao.getAppliedPeriodsByStrategyId(3);
153         assertNotNull(locations);
154         assertEquals(1, locations.size());
155 
156         if (log.isDebugEnabled()) {
157             for (AppliedStrategyDTO dto : locations) {
158                 log.debug(ToStringBuilder.reflectionToString(dto, ToStringStyle.SHORT_PREFIX_STYLE));
159             }
160         }
161 
162         assertEquals((Integer) 1, locations.get(0).getId());
163         assertEquals(LocalDate.of(2004,9,1), locations.get(0).getStartDate());
164         assertEquals(LocalDate.of(2010,7,1), locations.get(0).getEndDate());
165         assertNotNull(locations.get(0).getSamplingDepartment());
166         assertEquals((Integer) 5, locations.get(0).getSamplingDepartment().getId());
167     }
168 
169     @Test
170     public void getPmfmsByStrategy() {
171 
172         if (log.isDebugEnabled()) {
173             log.debug("getPmfmsByStrategy");
174         }
175 
176         List<PmfmStrategyDTO> pmfms = strategyDao.getPmfmsAppliedStrategy(1);
177         assertNotNull(pmfms);
178         assertEquals(4, pmfms.size());
179 
180         if (log.isDebugEnabled()) {
181             for (PmfmStrategyDTO dto : pmfms) {
182                 log.debug(ToStringBuilder.reflectionToString(dto, ToStringStyle.SHORT_PREFIX_STYLE));
183             }
184         }
185 
186         PmfmStrategyDTO pmfm = null;
187         for (PmfmStrategyDTO dto: pmfms) {
188             if (dto.getId() == 1) {
189                 pmfm = dto;
190                 break;
191             }
192         }
193         assertNotNull(pmfm);
194         
195         // TODO test booleans
196         pmfm.isSurvey();
197         pmfm.isSampling();
198         pmfm.isGrouping();
199         pmfm.isUnique();
200 
201         //TODO remplacer ?      
202 //        assertNotNull(pmfm.getParemetre());
203 //        assertEquals("ACEPHTE", pmfm.getParemetre().getCode());
204 //        assertNotNull(pmfm.getSupport());
205 //        assertEquals((Integer) 1, pmfm.getSupport().getId());
206 //        assertNotNull(pmfm.getFraction());
207 //        assertEquals((Integer) 1, pmfm.getFraction().getId());
208 //        assertNotNull(pmfm.getMethode());
209 //        assertEquals((Integer) 1, pmfm.getMethode().getId());
210 
211         // TODO add unit
212 //        assertNotNull(pmfm.getUnite());
213 //        assertEquals((Integer) 9, pmfm.getUnite().getId()); 
214         
215     }
216 
217 }