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  package org.kuali.rice.location.web;
17  
18  import org.junit.Assert;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.kuali.rice.kew.api.document.Document;
22  import org.kuali.rice.kew.api.exception.WorkflowException;
23  import org.kuali.rice.kns.service.KNSServiceLocator;
24  import org.kuali.rice.krad.exception.ValidationException;
25  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28  import org.kuali.rice.krad.util.KRADConstants;
29  import org.kuali.rice.location.api.services.LocationApiServiceLocator;
30  import org.kuali.rice.location.impl.country.CountryBo;
31  import org.kuali.rice.location.impl.county.CountyBo;
32  import org.kuali.rice.location.impl.state.StateBo;
33  import org.kuali.rice.krad.test.BaseMaintenanceDocumentTest;
34  
35  import static org.junit.Assert.assertNotNull;
36  
37  /**
38   * Tests creating, editing, and savingn County maintenance doc
39   */
40  public class CountyMaintenanceDocumentTest extends BaseMaintenanceDocumentTest {
41      /**
42       * Make sure we have a test country and state
43       */
44      @Before
45      public void insertTestCountryAndState() {
46          CountryBo country = new CountryBo();
47          country.setCode("CC");
48          country.setName("New Country");
49          country.setActive(true);
50          KNSServiceLocator.getBusinessObjectService().save(country);
51  
52          StateBo state = new StateBo();
53          state.setCode("SS");
54          state.setCountryCode("CC");
55          state.setName("New State");
56          state.setActive(true);
57          KNSServiceLocator.getBusinessObjectService().save(state);
58      }
59  
60      @Override
61      protected Object getNewMaintainableObject()  {
62          CountyBo county = new CountyBo();
63          county.setName("Tompkins");
64          county.setCode("TOMPKINS");
65          county.setCountryCode("US");
66          county.setStateCode("CA");
67          return county;
68      }
69  
70      @Override
71      protected String getDocumentTypeName() {
72          return "CountyMaintenanceDocument";
73      }
74  
75      @Override
76      protected String getInitiatorPrincipalName() {
77          return "quickstart";
78      }
79  
80      @Override
81      protected Object getOldMaintainableObject() {
82          return getNewMaintainableObject();
83      }
84  
85      @Test(expected = ValidationException.class)
86      /**
87       * test that a validation error occurs when a business object is missing required fields
88       */
89      public void test_MismatchedStateCountry() throws WorkflowException {
90          assertNotNull(LocationApiServiceLocator.getCountryService().getCountry("CC"));
91          assertNotNull(LocationApiServiceLocator.getStateService().getState("CC", "SS"));
92  
93          CountyBo county = new CountyBo();
94          county.setName("Tompkins");
95          county.setCode("TOMPKINS");
96          county.setCountryCode("US");
97          // our test state is not in the US country
98          // by virtue of the country/state join on State reference
99          // and the State reference existence check
100         // this will yield a validation error
101         county.setStateCode("SS");
102 
103         MaintenanceDocument document = getDocument();
104         document.getOldMaintainableObject().setDataObject(null);
105         document.getOldMaintainableObject().setDataObjectClass(county.getClass());
106         document.getNewMaintainableObject().setDataObject(county);
107         document.getNewMaintainableObject().setDataObjectClass(county.getClass());
108 
109         document.getNewMaintainableObject().setMaintenanceAction(KRADConstants.MAINTENANCE_NEW_ACTION);
110 
111         KRADServiceLocatorWeb.getDocumentService().routeDocument(getDocument(), "submit", null);
112         Assert.assertTrue("ValidationException should have been thrown instead of this assertion failure", getDocument().getDocumentHeader().getWorkflowDocument().isEnroute());
113     }
114 }