View Javadoc
1   /*
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.kuali.rice.location.impl.data;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.List;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.kuali.rice.core.api.criteria.QueryByCriteria;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  import org.kuali.rice.location.api.campus.Campus;
31  import org.kuali.rice.location.api.campus.CampusQueryResults;
32  import org.kuali.rice.location.api.campus.CampusType;
33  import org.kuali.rice.location.api.campus.CampusTypeQueryResults;
34  import org.kuali.rice.location.api.country.Country;
35  import org.kuali.rice.location.api.country.CountryQueryResults;
36  import org.kuali.rice.location.api.county.County;
37  import org.kuali.rice.location.api.county.CountyQueryResults;
38  import org.kuali.rice.location.api.postalcode.PostalCode;
39  import org.kuali.rice.location.api.postalcode.PostalCodeQueryResults;
40  import org.kuali.rice.location.api.services.LocationApiServiceLocator;
41  import org.kuali.rice.location.api.state.State;
42  import org.kuali.rice.location.api.state.StateQueryResults;
43  import org.kuali.rice.location.impl.campus.CampusBo;
44  import org.kuali.rice.location.impl.campus.CampusTypeBo;
45  import org.kuali.rice.location.impl.country.CountryBo;
46  import org.kuali.rice.location.impl.county.CountyBo;
47  import org.kuali.rice.location.impl.county.CountyId;
48  import org.kuali.rice.location.impl.postalcode.PostalCodeBo;
49  import org.kuali.rice.location.impl.postalcode.PostalCodeId;
50  import org.kuali.rice.location.impl.state.StateBo;
51  import org.kuali.rice.location.impl.state.StateId;
52  import org.kuali.rice.test.BaselineTestCase;
53  
54  /**
55   * Tests to confirm JPA mapping for the Location module data objects
56   */
57  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
58  public class LocationDataJPATest extends LocationTestCase {
59  
60      /**
61       * This overridden method ...
62       *
63       * @see org.kuali.rice.krad.test.KRADTestCase#setUp()
64       */
65      @Override
66      public void setUp() throws Exception {
67          super.setUp();
68  
69          setupPostalCodeBoDataObjectAndSave();
70          setupCampusBoDataObjectAndSave();
71  
72          // If we don't do this, then some of the objects are created without
73          // their reference object proxies in place.  This forces all the objects which
74          // were created by the above methods to be reloaded as needed during the test.
75          KRADServiceLocator.getDataObjectService().flush(PostalCodeBo.class);
76          KRADServiceLocator.getDataObjectService().flush(CountryBo.class);
77          KRADServiceLocator.getDataObjectService().flush(CountyBo.class);
78          KRADServiceLocator.getDataObjectService().flush(StateBo.class);
79          KRADServiceLocator.getDataObjectService().flush(CampusTypeBo.class);
80          KRADServiceLocator.getDataObjectService().flush(CampusBo.class);
81      }
82  
83      @Test
84      public void testPostalCodeBoDataObject() throws Exception {
85          assertTrue("PostalCodeBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(
86                  PostalCodeBo.class));
87          PostalCodeBo postalCodeBo = KRADServiceLocator.getDataObjectService().find(PostalCodeBo.class, new PostalCodeId(
88                  "US", "47203"));
89          assertTrue("PostalCode BO fetched after save", postalCodeBo != null && StringUtils.equals(
90                  postalCodeBo.getCode(), "47203"));
91          assertTrue("PostalCode BO fetched Country BO correctly",
92                  postalCodeBo.getCountry() != null && StringUtils.equals(postalCodeBo.getCountry().getAlternateCode(),
93                          "USA"));
94          assertTrue("PostalCode BO fetched State BO correctly", postalCodeBo.getState() != null && StringUtils.equals(
95                  postalCodeBo.getState().getCode(), "IN"));
96          assertTrue("PostalCode BO fetched County BO correctly", postalCodeBo.getCounty() != null && StringUtils.equals(
97                  postalCodeBo.getCounty().getCode(), "MON"));
98      }
99  
100     @Test
101     public void testCountyBoDataObject() throws Exception {
102         assertTrue("CountyBO is not mapped in JPA", KRADServiceLocator.getDataObjectService().supports(CountryBo.class));
103         CountyBo countyBo = KRADServiceLocator.getDataObjectService().find(CountyBo.class, new CountyId("MON", "US", "IN"));
104 
105         Assert.assertNotNull( "County BO not retrieved after save", countyBo );
106         Assert.assertEquals( "County name incorrect upon retrieve", "Monroe", countyBo.getName() );
107         Assert.assertEquals( "State code incorrect upon retrieve", "IN", countyBo.getStateCode() );
108 
109         Assert.assertNotNull( "State on County BO should not be null", countyBo.getState() );
110         Assert.assertEquals( "State name on county incorrect", "Indiana", countyBo.getState().getName() );
111 
112         Assert.assertNotNull( "Country on County BO should not be null", countyBo.getCountry() );
113         Assert.assertEquals( "Country code on county incorrect", "USA", countyBo.getCountry().getAlternateCode() );
114     }
115 
116     @Test
117     public void testStateBoDataObject() throws Exception {
118         assertTrue("StateBO is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(StateBo.class));
119 
120         StateBo stateBo = KRADServiceLocator.getDataObjectService().find(StateBo.class, new StateId("IN", "US"));
121         assertTrue("State BO fetched after save", stateBo != null && StringUtils.equals(stateBo.getName(), "Indiana"));
122         assertTrue("State BO fetched Country BO correctly", stateBo.getCountry() != null && StringUtils.equals(
123                 stateBo.getCountry().getAlternateCode(), "USA"));
124     }
125 
126 
127     @Test
128     public void testCountryBoDataObject() throws Exception {
129         assertTrue("CountryBO is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(CountryBo.class));
130 
131         CountryBo countryBo = KRADServiceLocator.getDataObjectService().find(CountryBo.class, "CA");
132         assertNotNull("Country BO unable to be retrieved", countryBo);
133         assertEquals("Country BO Data incorrect", "CAN",  countryBo.getAlternateCode() );
134     }
135 
136     @Test
137     public void testCampusBoDataObject() throws Exception {
138         assertTrue("CampusBO is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(CampusBo.class));
139 
140         CampusBo campusBo = KRADServiceLocator.getDataObjectService().find(CampusBo.class, "SE");
141         assertTrue("Campus BO fetched after save", campusBo != null && StringUtils.equals(campusBo.getName(),
142                 "SouthEast"));
143         assertTrue("Campus Type Bo fetched from Campus BO relationship",
144                 campusBo.getCampusType() != null && StringUtils.equals(campusBo.getCampusType().getName(), "Commuter"));
145     }
146 
147     @Test
148     public void testCampusTypeBoDataObject() throws Exception {
149         assertTrue("CampusTypeBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(
150                 CampusTypeBo.class));
151 
152         CampusTypeBo campusTypeBoFetched = KRADServiceLocator.getDataObjectService().find(CampusTypeBo.class, "C");
153         assertTrue("Campus Type BO refetched after save",
154                 campusTypeBoFetched != null && campusTypeBoFetched.getName().equals("Commuter"));
155     }
156 
157     @Test
158     public void testCampusServiceImplJPA() throws Exception {
159         Campus campusBo = LocationApiServiceLocator.getCampusService().getCampus("SE");
160         assertTrue("getCampusService retrieved correct call", campusBo != null && StringUtils.equals(campusBo.getCode(),
161                 "SE"));
162         List<Campus> activeCampuses = LocationApiServiceLocator.getCampusService().findAllCampuses();
163         assertTrue("findAllCampuses returned result", activeCampuses.size() > 0);
164 
165         CampusType campusType = LocationApiServiceLocator.getCampusService().getCampusType("C");
166         assertTrue("getCampusType retrieved correctly", campusType != null && StringUtils.equals(campusType.getName(),
167                 "Commuter"));
168 
169         List<CampusType> campusTypeList = LocationApiServiceLocator.getCampusService().findAllCampusTypes();
170         assertTrue("findAllCampusTypes retrieved correctly", campusTypeList.size() > 0);
171 
172         CampusQueryResults results = LocationApiServiceLocator.getCampusService().
173                 findCampuses(QueryByCriteria.Builder.forAttribute("code", "SE").build());
174         assertTrue("findCampuses retrieved correctly", results != null && results.getResults().size() == 1);
175 
176         CampusTypeQueryResults resultsType = LocationApiServiceLocator.getCampusService().
177                 findCampusTypes(QueryByCriteria.Builder.forAttribute("code", "C").build());
178         assertTrue("findCampuses retrieved correctly", resultsType != null && resultsType.getResults().size() == 1);
179     }
180 
181     @Test
182     public void testCountryServiceImplJPA() throws Exception {
183         Country countryBo = LocationApiServiceLocator.getCountryService().getCountry("US");
184 
185         assertNotNull("Country BO unable to be retrieved", countryBo);
186         assertEquals("Country BO Data incorrect", "USA",  countryBo.getAlternateCode() );
187 
188         countryBo = LocationApiServiceLocator.getCountryService().getCountryByAlternateCode("USA");
189 
190         assertNotNull("Country BO unable to be retrieved", countryBo);
191         assertEquals("Country BO Data incorrect", "US",  countryBo.getCode() );
192 
193         List<Country> countryList = LocationApiServiceLocator.getCountryService().findAllCountries();
194         assertNotNull("Returned country list should not have been null", countryList);
195         assertEquals("Find all countries returned wrong number of results", 2, countryList.size());
196 
197         countryList = LocationApiServiceLocator.getCountryService().findAllCountriesNotRestricted();
198         assertNotNull("Returned country list should not have been null", countryList);
199         assertEquals("findAllCountriesNotRestricted returned wrong number of results", 1, countryList.size());
200 
201         CountryQueryResults results = LocationApiServiceLocator.getCountryService().
202                 findCountries(QueryByCriteria.Builder.forAttribute("code", "US").build());
203         assertNotNull("findCountries country list should not have been null", results);
204         assertNotNull("findCountries.getResults() should not have been null", results.getResults() );
205         assertEquals("findAllCountriesNotRestricted returned wrong number of results", 1, results.getResults().size());
206     }
207 
208     @Test
209     public void testCountyServiceImplJPA() throws Exception {
210         County county = LocationApiServiceLocator.getCountyService().getCounty("US", "IN", "MON");
211         assertTrue("getCounty retrieved correctly", county != null && StringUtils.equals("MON", county.getCode()));
212 
213         CountyQueryResults results = LocationApiServiceLocator.getCountyService().findCounties(
214                 QueryByCriteria.Builder.forAttribute("code", "MON").build());
215         assertTrue("findCounties retrieved correctly", results != null && results.getResults().size() == 1);
216 
217         List<County> counties = LocationApiServiceLocator.getCountyService().
218                 findAllCountiesInCountryAndState("US", "IN");
219         assertTrue("findAllCountiesInCountryAndState retrieved correctly", counties != null && counties.size() == 1);
220     }
221 
222     @Test
223     public void testStateServiceImplJPA() throws Exception {
224         State state = LocationApiServiceLocator.getStateService().getState("US", "IN");
225         assertTrue("getState retrieved correctly", state != null && StringUtils.equals("IN", state.getCode()));
226         List<State> stateList = LocationApiServiceLocator.getStateService().findAllStatesInCountry("US");
227         assertTrue("findAllStatesInCountry retrieved correctly", stateList != null && stateList.size() == 1);
228         stateList = LocationApiServiceLocator.getStateService().findAllStatesInCountryByAltCode("USA");
229         assertTrue("findAllStatesInCountryByAltCode retrieved correctly", stateList != null && stateList.size() == 1);
230         StateQueryResults results = LocationApiServiceLocator.getStateService().findStates(
231                 QueryByCriteria.Builder.forAttribute("code", "IN").build());
232         assertTrue("findStates retrieved correctly", results != null && results.getResults().size() == 1);
233     }
234 
235     @Test
236     public void testPostalCodeServiceImplJPA() throws Exception {
237         PostalCode postalCode = LocationApiServiceLocator.getPostalCodeService().getPostalCode("US", "47203");
238         assertTrue("getPostalCode retrieved correctly", postalCode != null && StringUtils.equals(postalCode.getCode(),
239                 "47203"));
240         List<PostalCode> postalCodeList = LocationApiServiceLocator.getPostalCodeService().
241                 findAllPostalCodesInCountry("US");
242         assertTrue("findAllPostalCodesInCountry retrieved correctly",
243                 postalCodeList != null && postalCodeList.size() == 1);
244         PostalCodeQueryResults results = LocationApiServiceLocator.getPostalCodeService().
245                 findPostalCodes(QueryByCriteria.Builder.forAttribute("code", "47203").build());
246         assertTrue("findPostalCodes retrieved correctly", results != null && results.getResults().size() == 1);
247     }
248 
249     private void setupPostalCodeBoDataObjectAndSave() {
250         KRADServiceLocator.getDataObjectService().flush(PostalCodeBo.class);
251         if ( KRADServiceLocator.getDataObjectService().find(PostalCodeBo.class, new PostalCodeId("US", "47203")) == null ) {
252             setupCountyBoDataObjectAndSave();
253             PostalCodeBo postalCodeBo = new PostalCodeBo();
254             postalCodeBo.setActive(true);
255             postalCodeBo.setCityName("Bloomington");
256             postalCodeBo.setCode("47203");
257             postalCodeBo.setCountryCode("US");
258             postalCodeBo.setCountyCode("MON");
259             postalCodeBo.setStateCode("IN");
260 
261             KRADServiceLocator.getDataObjectService().save(postalCodeBo);
262         }
263     }
264 
265     private void setupCountyBoDataObjectAndSave() {
266         KRADServiceLocator.getDataObjectService().flush(CountyBo.class);
267         if ( KRADServiceLocator.getDataObjectService().find(CountyBo.class, new CountyId("MON", "US", "IN")) == null ) {
268             setupStateBoDataObjectAndSave();
269             CountyBo countyBo = new CountyBo();
270             countyBo.setActive(true);
271             countyBo.setCode("MON");
272             countyBo.setCountryCode("US");
273             countyBo.setName("Monroe");
274             countyBo.setStateCode("IN");
275 
276             countyBo = KRADServiceLocator.getDataObjectService().save(countyBo);
277         }
278     }
279 
280     private void setupStateBoDataObjectAndSave() {
281         setupCountryBoDataObjectAndSave();
282         KRADServiceLocator.getDataObjectService().flush(StateBo.class);
283         if ( KRADServiceLocator.getDataObjectService().find(StateBo.class, new StateId( "IN", "US" )) == null ) {
284             StateBo stateBo = new StateBo();
285             stateBo.setActive(true);
286             stateBo.setCode("IN");
287             stateBo.setCountryCode("US");
288             stateBo.setName("Indiana");
289 
290             KRADServiceLocator.getDataObjectService().save(stateBo);
291         }
292     }
293 
294     private void setupCountryBoDataObjectAndSave() {
295         KRADServiceLocator.getDataObjectService().flush(CountryBo.class);
296         if ( KRADServiceLocator.getDataObjectService().find(CountryBo.class, "US") == null ) {
297             CountryBo countryBo = new CountryBo();
298             countryBo.setActive(true);
299             countryBo.setAlternateCode("USA");
300             countryBo.setCode("US");
301             countryBo.setName("UNITED STATES");
302             countryBo.setRestricted(false);
303 
304             KRADServiceLocator.getDataObjectService().save(countryBo);
305 
306             countryBo = new CountryBo();
307             countryBo.setActive(true);
308             countryBo.setAlternateCode("CAN");
309             countryBo.setCode("CA");
310             countryBo.setName("Canada");
311             countryBo.setRestricted(true);
312 
313             KRADServiceLocator.getDataObjectService().save(countryBo);
314         }
315     }
316 
317     private void setupCampusBoDataObjectAndSave() {
318         KRADServiceLocator.getDataObjectService().flush(CampusBo.class);
319         if ( KRADServiceLocator.getDataObjectService().find(CampusBo.class, "SE") == null ) {
320             setupCampusTypeBoDataObjectAndSave();
321             CampusBo campusBo = new CampusBo();
322             campusBo.setActive(true);
323             campusBo.setCampusTypeCode("C");
324             campusBo.setCode("SE");
325             campusBo.setName("SouthEast");
326             campusBo.setShortName("SouthE");
327 
328             KRADServiceLocator.getDataObjectService().save(campusBo);
329         }
330     }
331 
332     private void setupCampusTypeBoDataObjectAndSave() {
333         KRADServiceLocator.getDataObjectService().flush(CampusTypeBo.class);
334         if ( KRADServiceLocator.getDataObjectService().find(CampusTypeBo.class, "C") == null ) {
335             CampusTypeBo campusTypeBo = new CampusTypeBo();
336             campusTypeBo.setActive(true);
337             campusTypeBo.setCode("C");
338             campusTypeBo.setName("Commuter");
339 
340             KRADServiceLocator.getDataObjectService().save(campusTypeBo);
341         }
342     }
343 
344 
345 
346 }