View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   
7   package fr.ifremer.dali.dao.system.filter;
8   
9   /*
10   * #%L
11   * Dali :: Core
12   * $Id:$
13   * $HeadURL:$
14   * %%
15   * Copyright (C) 2014 - 2015 Ifremer
16   * %%
17   * This program is free software: you can redistribute it and/or modify
18   * it under the terms of the GNU Affero General Public License as published by
19   * the Free Software Foundation, either version 3 of the License, or
20   * (at your option) any later version.
21   * 
22   * This program is distributed in the hope that it will be useful,
23   * but WITHOUT ANY WARRANTY; without even the implied warranty of
24   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25   * GNU General Public License for more details.
26   * 
27   * You should have received a copy of the GNU Affero General Public License
28   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29   * #L%
30   */
31  
32  import com.google.common.collect.ImmutableList;
33  import com.google.common.collect.Lists;
34  import fr.ifremer.quadrige3.core.test.AbstractDaoTest;
35  import fr.ifremer.dali.dao.DaliDatabaseResource;
36  import fr.ifremer.dali.dto.DaliBeanFactory;
37  import fr.ifremer.dali.dto.configuration.filter.FilterDTO;
38  import fr.ifremer.dali.dto.enums.FilterTypeValues;
39  import fr.ifremer.dali.dto.referential.LocationDTO;
40  import fr.ifremer.dali.service.DaliServiceLocator;
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  import org.junit.Assert;
44  import org.junit.Before;
45  import org.junit.ClassRule;
46  import org.junit.Test;
47  
48  import java.util.List;
49  
50  /**
51   * @author Lionel Touseau <lionel.touseau@e-is.pro>
52   */
53  public class FilterDaoWriteTest extends AbstractDaoTest {
54  
55      private static final Log log = LogFactory.getLog(FilterDaoWriteTest.class);
56  
57      @ClassRule
58      public static final DaliDatabaseResource dbResource = DaliDatabaseResource.writeDb();
59  
60      private DaliFilterDao filterDao;
61  
62      @Before
63      @Override
64      public void setUp() throws Exception {
65          super.setUp();
66          filterDao = DaliServiceLocator.instance().getService("daliFilterDao", DaliFilterDao.class);
67      }
68  
69      @Test
70      public void saveFilter() {
71  
72          // test new one
73          FilterDTO f = newFilterDTO();
74          f.setFilterTypeId(FilterTypeValues.LOCATION.getFilterTypeId());
75          f.setDirty(true);
76  
77          filterDao.saveFilter(f, 1); // user 1
78          Integer newFilterId = f.getId();
79          Assert.assertNotNull(newFilterId);
80          log.info("Filter saved with id " + newFilterId);
81  
82          // test update
83          f = filterDao.getFilterById(newFilterId); // new Filter
84          Assert.assertNotNull(f);
85          Assert.assertNotNull(f.getId());
86          f.setName("test update");
87  
88          filterDao.saveFilter(f, 1);
89  
90          Assert.assertEquals("test update", f.getName());
91  
92          filterDao.deleteFilters(ImmutableList.of(f.getId()));
93          // deleted filter should not exist anymore
94          f = filterDao.getFilterById(newFilterId);
95          Assert.assertNull(f);
96  
97          // TODO try to save other filter types (program, ...)
98  
99      }
100 
101     @Test
102     public void deleteFilter() {
103         List<FilterDTO> filters = filterDao.getAllContextFilters(null, FilterTypeValues.DEPARTMENT.getFilterTypeId());
104         Assert.assertNotNull(filters);
105         Assert.assertTrue(filters.size() >= 1);
106         for (FilterDTO f : filters) {
107             Integer filterId = f.getId();
108             if (filterId > 1000) {
109                 filterDao.deleteFilters(ImmutableList.of(f.getId()));
110                 f = filterDao.getFilterById(filterId);
111                 Assert.assertNull(f);
112             }
113         }
114 
115     }
116 
117     private FilterDTO newFilterDTO() {
118         FilterDTO f = DaliBeanFactory.newFilterDTO();
119         LocationDTO l1 = DaliBeanFactory.newLocationDTO();
120         l1.setId(2); // Digue du Braek
121         l1.setLabel("L1");
122 
123         f.setFilterTypeId(FilterTypeValues.LOCATION.getFilterTypeId());
124         f.setName("test new");
125 
126         f.setElements(Lists.newArrayList(l1));
127 
128         return f;
129     }
130 
131 }