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 org.apache.commons.lang.StringUtils;
20  import org.junit.Test;
21  import org.kuali.rice.core.api.CoreApiServiceLocator;
22  import org.kuali.rice.core.api.criteria.QueryByCriteria;
23  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
24  import org.kuali.rice.coreservice.impl.parameter.ParameterBo;
25  import org.kuali.rice.kns.service.KNSServiceLocator;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28  import org.kuali.rice.krad.test.KRADTestCase;
29  import org.kuali.rice.location.api.campus.Campus;
30  import org.kuali.rice.location.api.campus.CampusQueryResults;
31  import org.kuali.rice.location.api.campus.CampusType;
32  import org.kuali.rice.location.api.campus.CampusTypeQueryResults;
33  import org.kuali.rice.location.api.country.Country;
34  import org.kuali.rice.location.api.country.CountryQueryResults;
35  import org.kuali.rice.location.api.county.County;
36  import org.kuali.rice.location.api.county.CountyQueryResults;
37  import org.kuali.rice.location.api.postalcode.PostalCode;
38  import org.kuali.rice.location.api.postalcode.PostalCodeQueryResults;
39  import org.kuali.rice.location.api.services.LocationApiServiceLocator;
40  import org.kuali.rice.location.api.state.State;
41  import org.kuali.rice.location.api.state.StateQueryResults;
42  import org.kuali.rice.location.impl.campus.CampusBo;
43  import org.kuali.rice.location.impl.campus.CampusTypeBo;
44  import org.kuali.rice.location.impl.country.CountryBo;
45  import org.kuali.rice.location.impl.county.CountyBo;
46  import org.kuali.rice.location.impl.county.CountyId;
47  import org.kuali.rice.location.impl.postalcode.PostalCodeBo;
48  import org.kuali.rice.location.impl.postalcode.PostalCodeId;
49  import org.kuali.rice.location.impl.state.StateBo;
50  import org.kuali.rice.location.impl.state.StateId;
51  import org.kuali.rice.test.BaselineTestCase;
52  
53  import static org.junit.Assert.*;
54  
55  import javax.persistence.EntityManagerFactory;
56  import javax.validation.constraints.AssertTrue;
57  import java.util.List;
58  import java.util.UUID;
59  
60  /**
61   * Tests to confirm JPA mapping for the Location module data objects
62   */
63  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
64  public class LocationDataJPATest extends KRADTestCase {
65  
66      @Test
67      public void testPostalCodeBoDataObject() throws Exception{
68          assertTrue("PostalCodeBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(PostalCodeBo.class));
69          setupPostalCodeBoDataObjectAndSave();
70  
71          PostalCodeBo postalCodeBo = KRADServiceLocator.getDataObjectService().find(
72                                      PostalCodeBo.class,new PostalCodeId("US","47203"));
73          assertTrue("PostalCode BO fetched after save", postalCodeBo != null && StringUtils.equals(postalCodeBo.getCode(),"47203"));
74          assertTrue("PostalCode BO fetched Country BO correctly",postalCodeBo.getCountry()!=null
75                          && StringUtils.equals(postalCodeBo.getCountry().getAlternateCode(),"USA"));
76          assertTrue("PostalCode BO fetched State BO correctly",postalCodeBo.getState() != null
77                          && StringUtils.equals(postalCodeBo.getState().getCode(),"IN"));
78          assertTrue("PostalCode BO fetched County BO correctly",postalCodeBo.getCounty() != null
79                          && StringUtils.equals(postalCodeBo.getCounty().getCode(),"MON"));
80      }
81  
82      @Test
83      public void testCountyBoDataObject() throws Exception{
84          assertTrue("CountyBO is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(CountryBo.class));
85          setupCountyBoDataObjectAndSave();
86  
87          CountyBo countyBo = KRADServiceLocator.getDataObjectService().find(CountyBo.class,new CountyId("MON","US","IN"));
88          assertTrue("County BO fetched after save",countyBo != null && StringUtils.equals(countyBo.getName(),"Monroe"));
89          assertTrue("County Bo fetched State Bo correctly",countyBo.getState() != null
90                                          && StringUtils.equals(countyBo.getState().getName(),"Indiana"));
91          assertTrue("County Bo fetched Country Bo correctly",countyBo.getCountry() != null
92                  && StringUtils.equals(countyBo.getCountry().getAlternateCode(), "USA"));
93  
94      }
95  
96      @Test
97      public void testStateBoDataObject() throws Exception{
98          assertTrue("StateBO is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(StateBo.class));
99          setupStateBoDataObjectAndSave();
100 
101         StateBo stateBo = KRADServiceLocator.getDataObjectService().find(StateBo.class,new StateId("IN","US"));
102         assertTrue("State BO fetched after save", stateBo != null && StringUtils.equals(stateBo.getName(),"Indiana"));
103         assertTrue("State BO fetched Country BO correctly",stateBo.getCountry() != null && StringUtils.equals(stateBo.getCountry().getAlternateCode(),"USA"));
104     }
105 
106 
107     @Test
108     public void testCountryBoDataObject() throws Exception{
109         assertTrue("CountryBO is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(CountryBo.class));
110         setupCountryBoDataObjectAndSave();
111 
112         CountryBo countryBo = KRADServiceLocator.getDataObjectService().find(CountryBo.class,"US");
113         assertTrue("Country BO fetched after save", countryBo != null &&
114                                 StringUtils.equals(countryBo.getAlternateCode(),"USA"));
115     }
116 
117     @Test
118     public void testCampusBoDataObject() throws Exception{
119        assertTrue("CampusBO is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(CampusBo.class));
120        setupCampusBoDataObjectAndSave();
121 
122        CampusBo campusBo = KRADServiceLocator.getDataObjectService().find(CampusBo.class,"SE");
123        assertTrue("Campus BO fetched after save", campusBo != null && StringUtils.equals(campusBo.getName(),
124                "SouthEast"));
125        assertTrue("Campus Type Bo fetched from Campus BO relationship", campusBo.getCampusType() != null
126                             && StringUtils.equals(campusBo.getCampusType().getName(),"Commuter"));
127     }
128 
129     @Test
130     public void testCampusTypeBoDataObject() throws Exception{
131         assertTrue("CampusTypeBo is mapped in JPA", KRADServiceLocator.getDataObjectService().supports(
132                 CampusTypeBo.class));
133         setupCampusTypeBoDataObjectAndSave();
134 
135         CampusTypeBo campusTypeBoFetched = KRADServiceLocator.getDataObjectService().find(CampusTypeBo.class,"C");
136         assertTrue("Campus Type BO refetched after save", campusTypeBoFetched != null &&
137                             campusTypeBoFetched.getName().equals("Commuter"));
138     }
139 
140     @Test
141     public void testCampusServiceImplJPA() throws Exception{
142         setupCampusBoDataObjectAndSave();
143         Campus campusBo = LocationApiServiceLocator.getCampusService().getCampus("SE");
144         assertTrue("getCampusService retrieved correct call", campusBo != null
145                 && StringUtils.equals(campusBo.getCode(),"SE"));
146         List<Campus> activeCampuses = LocationApiServiceLocator.getCampusService().findAllCampuses();
147         assertTrue("findAllCampuses returned result", activeCampuses.size() > 0);
148 
149         CampusType campusType = LocationApiServiceLocator.getCampusService().getCampusType("C");
150         assertTrue("getCampusType retrieved correctly",campusType != null
151                    && StringUtils.equals(campusType.getName(), "Commuter"));
152 
153         List<CampusType> campusTypeList = LocationApiServiceLocator.getCampusService().findAllCampusTypes();
154         assertTrue("findAllCampusTypes retrieved correctly",campusTypeList.size() > 0);
155 
156         CampusQueryResults results = LocationApiServiceLocator.getCampusService().
157                 findCampuses(QueryByCriteria.Builder.forAttribute("code", "SE").build());
158         assertTrue("findCampuses retrieved correctly",results != null && results.getResults().size() == 1);
159 
160         CampusTypeQueryResults resultsType = LocationApiServiceLocator.getCampusService().
161                 findCampusTypes(QueryByCriteria.Builder.forAttribute("code", "C").build());
162         assertTrue("findCampuses retrieved correctly",resultsType != null && resultsType.getResults().size() == 1);
163     }
164 
165     @Test
166     public void testCountryServiceImplJPA() throws Exception{
167         setupCountryBoDataObjectAndSave();
168         Country countryBo = LocationApiServiceLocator.getCountryService().getCountry("US");
169         assertTrue("getCountry retrieved correct", countryBo != null &&
170                 StringUtils.equals(countryBo.getAlternateCode(),"USA"));
171         Country country = LocationApiServiceLocator.getCountryService().getCountryByAlternateCode("USA");
172         assertTrue("getCountryByAlternateCode retrieved correctly", country != null &&
173                   StringUtils.equals(country.getCode(), "US"));
174         List<Country> countryList = LocationApiServiceLocator.getCountryService().findAllCountries();
175         assertTrue("findAllCountries retrieved correctly",countryList != null && countryList.size() == 1);
176 
177         countryList = LocationApiServiceLocator.getCountryService().findAllCountriesNotRestricted();
178         assertTrue("findAllCountriesNotRestricted retrieved correctly",countryList != null && countryList.size() == 1);
179 
180         CountryQueryResults results = LocationApiServiceLocator.getCountryService().
181                 findCountries(QueryByCriteria.Builder.forAttribute("code", "US").build());
182         assertTrue("findCountries retrieved correctly",results != null && results.getResults().size() == 1);
183     }
184 
185     @Test
186     public void testCountyServiceImplJPA() throws Exception{
187         setupCountyBoDataObjectAndSave();
188 
189         County county = LocationApiServiceLocator.getCountyService().getCounty("US","IN","MON");
190         assertTrue("getCounty retrieved correctly",county != null && StringUtils.equals("MON", county.getCode()));
191 
192         CountyQueryResults results = LocationApiServiceLocator.getCountyService().findCounties(
193                     QueryByCriteria.Builder.forAttribute("code","MON").build());
194         assertTrue("findCounties retrieved correctly",results != null && results.getResults().size() == 1);
195 
196         List<County> counties = LocationApiServiceLocator.getCountyService().
197                             findAllCountiesInCountryAndState("US","IN");
198         assertTrue("findAllCountiesInCountryAndState retrieved correctly",counties != null && counties.size() == 1);
199     }
200 
201     @Test
202     public void testStateServiceImplJPA() throws Exception{
203         setupStateBoDataObjectAndSave();
204 
205         State state = LocationApiServiceLocator.getStateService().getState("US","IN");
206         assertTrue("getState retrieved correctly", state != null && StringUtils.equals("IN",state.getCode()));
207         List<State> stateList = LocationApiServiceLocator.getStateService().findAllStatesInCountry("US");
208         assertTrue("findAllStatesInCountry retrieved correctly", stateList != null && stateList.size() == 1);
209         stateList = LocationApiServiceLocator.getStateService().findAllStatesInCountryByAltCode("USA");
210         assertTrue("findAllStatesInCountryByAltCode retrieved correctly", stateList != null && stateList.size() == 1);
211         StateQueryResults results = LocationApiServiceLocator.getStateService().findStates(
212                         QueryByCriteria.Builder.forAttribute("code","IN").build());
213         assertTrue("findStates retrieved correctly",results != null && results.getResults().size() == 1);
214     }
215 
216     @Test
217     public void testPostalCodeServiceImplJPA() throws Exception{
218         setupPostalCodeBoDataObjectAndSave();
219 
220         PostalCode postalCode = LocationApiServiceLocator.getPostalCodeService().getPostalCode("US","47203");
221         assertTrue("getPostalCode retrieved correctly",postalCode != null &&
222                                 StringUtils.equals(postalCode.getCode(),"47203"));
223         List<PostalCode> postalCodeList = LocationApiServiceLocator.getPostalCodeService().
224                 findAllPostalCodesInCountry("US");
225         assertTrue("findAllPostalCodesInCountry retrieved correctly",
226                 postalCodeList != null && postalCodeList.size() == 1);
227         PostalCodeQueryResults results = LocationApiServiceLocator.getPostalCodeService().
228                 findPostalCodes(QueryByCriteria.Builder.forAttribute("code", "47203").build());
229         assertTrue("findPostalCodes retrieved correctly",results != null && results.getResults().size()==1);
230     }
231 
232     private void setupPostalCodeBoDataObjectAndSave(){
233         setupCountyBoDataObjectAndSave();
234         PostalCodeBo postalCodeBo = new PostalCodeBo();
235         postalCodeBo.setActive(true);
236         postalCodeBo.setCityName("Bloomington");
237         postalCodeBo.setCode("47203");
238         postalCodeBo.setCountryCode("US");
239         postalCodeBo.setCountyCode("MON");
240         postalCodeBo.setStateCode("IN");
241 
242         KRADServiceLocator.getDataObjectService().save(postalCodeBo);
243     }
244 
245     private void setupCountyBoDataObjectAndSave(){
246         setupStateBoDataObjectAndSave();
247         CountyBo countyBo = new CountyBo();
248         countyBo.setActive(true);
249         countyBo.setCode("MON");
250         countyBo.setCountryCode("US");
251         countyBo.setName("Monroe");
252         countyBo.setStateCode("IN");
253 
254         KRADServiceLocator.getDataObjectService().save(countyBo);
255     }
256 
257     private void setupStateBoDataObjectAndSave(){
258         setupCountryBoDataObjectAndSave();
259         StateBo stateBo = new StateBo();
260         stateBo.setActive(true);
261         stateBo.setCode("IN");
262         stateBo.setCountryCode("US");
263         stateBo.setName("Indiana");
264 
265         KRADServiceLocator.getDataObjectService().save(stateBo);
266     }
267 
268     private void setupCountryBoDataObjectAndSave(){
269         CountryBo countryBo = new CountryBo();
270         countryBo.setActive(true);
271         countryBo.setAlternateCode("USA");
272         countryBo.setCode("US");
273         countryBo.setName("United States of America");
274         countryBo.setRestricted(false);
275 
276         KRADServiceLocator.getDataObjectService().save(countryBo);
277     }
278 
279     private void setupCampusBoDataObjectAndSave(){
280         setupCampusTypeBoDataObjectAndSave();
281         CampusBo campusBo = new CampusBo();
282         campusBo.setActive(true);
283         campusBo.setCampusTypeCode("C");
284         campusBo.setCode("SE");
285         campusBo.setName("SouthEast");
286         campusBo.setShortName("SouthE");
287 
288         KRADServiceLocator.getDataObjectService().save(campusBo);
289     }
290 
291     private void setupCampusTypeBoDataObjectAndSave(){
292         CampusTypeBo campusTypeBo = new CampusTypeBo();
293         campusTypeBo.setActive(true);
294         campusTypeBo.setCode("C");
295         campusTypeBo.setName("Commuter");
296 
297         KRADServiceLocator.getDataObjectService().save(campusTypeBo);
298     }
299 
300 
301 
302 }