View Javadoc

1   /*
2    * #%L
3    * Wao :: Business
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2009 - 2011 Ifremer
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Affero General Public License as published by
12   * the Free Software Foundation, either version 3 of the License, or
13   * (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Public License for more details.
19   * 
20   * You should have received a copy of the GNU Affero General Public License
21   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22   * #L%
23   */
24  package fr.ifremer.wao.business.fixtures;
25  
26  import fr.ifremer.wao.WaoBusinessException;
27  import fr.ifremer.wao.WaoDAOHelper;
28  import fr.ifremer.wao.WaoServices;
29  import fr.ifremer.wao.bean.ConnectedUser;
30  import fr.ifremer.wao.bean.ObsProgram;
31  import fr.ifremer.wao.bean.UserRole;
32  import fr.ifremer.wao.entity.Boat;
33  import fr.ifremer.wao.entity.Company;
34  import fr.ifremer.wao.entity.CompanyImpl;
35  import fr.ifremer.wao.entity.SampleRow;
36  import fr.ifremer.wao.entity.SampleRowDAO;
37  import fr.ifremer.wao.entity.TerrestrialLocation;
38  import fr.ifremer.wao.entity.TerrestrialLocationDAO;
39  import fr.ifremer.wao.entity.UserProfile;
40  import fr.ifremer.wao.entity.UserProfileImpl;
41  import fr.ifremer.wao.entity.WaoUser;
42  import fr.ifremer.wao.entity.WaoUserImpl;
43  import fr.ifremer.wao.service.ServiceBoat;
44  import org.junit.rules.ExternalResource;
45  import org.nuiton.topia.TopiaContext;
46  import org.nuiton.topia.TopiaException;
47  import org.nuiton.topia.TopiaRuntimeException;
48  
49  import java.io.InputStream;
50  import java.util.HashMap;
51  import java.util.Map;
52  
53  /**
54   * Fixtures are little sets of data, this class help you to load those data
55   * in the database in order to help you initializing all needed date, allowing
56   * you to write test focused on the business (without copy-pasted 100 LoC at
57   * the beginning of each test method).
58   *
59   * This classes gathers fixtures that may be used while testing all programs,
60   * sub-classes may provide program-specific fixtures.
61   */
62  public abstract class Fixtures extends ExternalResource {
63  
64      protected WaoServices manager;
65  
66      protected Map<String, Company> companies = new HashMap<String, Company>();
67  
68      protected Map<String, WaoUser> waoUsers = new HashMap<String, WaoUser>();
69  
70      protected Map<Integer, Boat> boats;
71  
72      protected Map<String, SampleRow> sampleRows;
73  
74      protected Map<String, TerrestrialLocation> districts = new HashMap<String, TerrestrialLocation>();
75  
76      protected TopiaContext transaction;
77  
78      public Fixtures(WaoServices manager) {
79          if (manager == null) {
80              throw new NullPointerException();
81          }
82          this.manager = manager;
83      }
84  
85      @Override
86      public void before() {}
87  
88      @Override
89      public void after() {
90          if (transaction != null && ! transaction.isClosed()) {
91              try {
92                  transaction.closeContext();
93              } catch (TopiaException e) {
94                  throw new RuntimeException(e);
95              }
96          }
97      }
98  
99      protected TopiaContext getTransaction() {
100         if (transaction == null) {
101             try {
102                 transaction = manager.getContext().beginTransaction();
103             } catch (TopiaException e) {
104                 throw new RuntimeException(e);
105             }
106         }
107         return transaction;
108     }
109 
110     protected Company getCompany(String name) {
111         Company company = companies.get(name);
112         if (company == null) {
113             company = new CompanyImpl();
114             company.setName(name);
115             company.setActive(true);
116             manager.getServiceUser().createUpdateCompany(company);
117             companies.put(name, company);
118         }
119         return company;
120     }
121 
122     protected WaoUser getWaoUser(String login, String password, String firstName, String lastName, Company company) {
123         WaoUser waoUser = waoUsers.get(login);
124         if (waoUser == null) {
125             waoUser = new WaoUserImpl();
126             waoUser.setLogin(login);
127             waoUser.setPassword(manager.getContext().encodeString(password));
128             waoUser.setFirstName(firstName);
129             waoUser.setLastName(lastName);
130             waoUser.setCompany(company);
131             waoUser.setActive(true);
132             UserProfile adminProfile = new UserProfileImpl(getObsProgram(), UserRole.ADMIN, true);
133             UserProfile coordinatorProfile = new UserProfileImpl(getObsProgram(), UserRole.COORDINATOR, true);
134             UserProfile observerProfile = new UserProfileImpl(getObsProgram(), UserRole.OBSERVER, true);
135             waoUser.addUserProfile(adminProfile);
136             waoUser.addUserProfile(coordinatorProfile);
137             waoUser.addUserProfile(observerProfile);
138             try {
139                 manager.getServiceUser().createUpdateUser(waoUser, false);
140             } catch (WaoBusinessException e) {
141                 throw new RuntimeException(e);
142             }
143             waoUser.setPassword(password);
144             waoUsers.put(waoUser.getLogin(), waoUser);
145         }
146         return waoUser;
147     }
148 
149     protected ConnectedUser connectedUser(WaoUser user, UserRole userRole) {
150         // first, log in
151         ConnectedUser connectedUser;
152         try {
153             connectedUser = manager.getServiceUser().connect(user.getLogin(), user.getPassword());
154         } catch (WaoBusinessException e) {
155             throw new RuntimeException(e);
156         }
157         // then choose profile
158         for (UserProfile userProfile : user.getUserProfile()) {
159             if (userProfile.getUserRole() == userRole) {
160                 connectedUser.setProfile(userProfile);
161             }
162         }
163         return connectedUser;
164     }
165 
166     public void boats() {
167         if (boats == null) {
168             boats = new HashMap<Integer, Boat>();
169             ServiceBoat serviceBoat = manager.getServiceBoat();
170             InputStream input = getClass().getResourceAsStream("/import/navires.csv");
171             try {
172                 serviceBoat.importBoatCsv(input);
173             } catch (WaoBusinessException e) {
174                 throw new RuntimeException(e);
175             }
176         }
177     }
178 
179     protected Boat getBoat(Integer immatriculation) {
180         boats();
181         Boat boat = boats.get(immatriculation);
182         if (boat == null) {
183             ServiceBoat serviceBoat = manager.getServiceBoat();
184             try {
185                 boat = serviceBoat.getBoat(immatriculation);
186             } catch (WaoBusinessException e) {
187                 throw new RuntimeException(e);
188             }
189         }
190         return boat;
191     }
192 
193     /** 174258 */
194     public Boat samourai() {
195         return getBoat(174258);
196     }
197 
198     /** 273129 */
199     public Boat moise() {
200         return getBoat(273129);
201     }
202 
203     protected abstract ObsProgram getObsProgram();
204 
205     protected abstract void importSamplingPlan();
206 
207     public void samplingPlan() {
208         if (sampleRows == null) {
209             sampleRows = new HashMap<String, SampleRow>();
210             importSamplingPlan();
211         }
212     }
213 
214     protected SampleRow getSampleRow(String code) {
215         samplingPlan();
216         SampleRow sampleRow = sampleRows.get(code);
217         if (sampleRow == null) {
218             try {
219                 TopiaContext transaction = manager.getContext().beginTransaction();
220                 SampleRowDAO rowDAO = WaoDAOHelper.getSampleRowDAO(transaction);
221                 sampleRow = rowDAO.findByCode(code);
222                 sampleRow.getProfession();
223                 sampleRow.sizeElligibleBoat();
224                 sampleRow.sizeSampleMonth();
225                 sampleRow.sizeSampleRowLog();
226                 sampleRow.getCompany();
227                 transaction.closeContext();
228                 return sampleRow;
229             } catch (TopiaException e) {
230                 throw new RuntimeException(e);
231             }
232         }
233         return sampleRow;
234     }
235 
236     protected TerrestrialLocation getDistrict(String districtCode) {
237         TerrestrialLocation district = districts.get(districtCode);
238         if (district == null) {
239             try {
240                 TerrestrialLocationDAO dao = WaoDAOHelper.getTerrestrialLocationDAO(getTransaction());
241                 district = dao.findDistrictByCode(districtCode);
242                 districts.put(districtCode, district);
243             } catch (TopiaException e) {
244                 throw new TopiaRuntimeException(e);
245             }
246         }
247         return district;
248     }
249     public TerrestrialLocation districtPv() {
250         return getDistrict("PV");
251     }
252 }