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.krms.impl.ui;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.krad.data.DataObjectService;
22  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
23  import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
24  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
25  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
26  import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
27  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
28  import org.kuali.rice.krms.impl.repository.ActionBoService;
29  import org.kuali.rice.krms.impl.repository.ActionBoServiceImpl;
30  import org.kuali.rice.krms.impl.repository.AgendaBo;
31  import org.kuali.rice.krms.impl.repository.AgendaBoService;
32  import org.kuali.rice.krms.impl.repository.AgendaBoServiceImpl;
33  import org.kuali.rice.krms.impl.repository.AgendaItemBo;
34  import org.kuali.rice.krms.impl.repository.ContextBoServiceImpl;
35  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
36  import org.kuali.rice.krms.impl.repository.PropositionBoService;
37  import org.kuali.rice.krms.impl.repository.PropositionBoServiceImpl;
38  import org.kuali.rice.krms.impl.repository.RuleBoService;
39  import org.kuali.rice.krms.impl.repository.RuleBoServiceImpl;
40  import org.kuali.rice.krms.impl.repository.TermBoService;
41  import org.kuali.rice.krms.impl.repository.TermBoServiceImpl;
42  import org.kuali.rice.krms.test.AbstractAgendaBoTest;
43  import org.kuali.rice.krms.test.AbstractBoTest;
44  import org.kuali.rice.test.BaselineTestCase.BaselineMode;
45  import org.kuali.rice.test.BaselineTestCase.Mode;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.HashMap;
50  import java.util.Map;
51  
52  import static org.junit.Assert.assertEquals;
53  import static org.junit.Assert.assertNotNull;
54  import static org.junit.Assert.assertNull;
55  
56  /**
57   * Validation Integration Test
58   * @author Kuali Rice Team (rice.collab@kuali.org)
59   */
60  @BaselineMode(Mode.CLEAR_DB)
61  public class AgendaEditorMaintainableIntegrationTest extends AbstractAgendaBoTest {
62  
63      static final String NAMESPACEX = "AEMIT_KRMS_TEST_1";
64      static final String CONTEXTX = "AEMIT_Context1";
65  
66      static final String CONTEXTX_QUALIFIER = "Context1Qualifier";
67      static final String CONTEXTX_QUALIFIER_VALUE = "BLAH1";
68      static final String AGENDAX = "TestAgenda1";
69  
70      private PropositionBoService propositionBoService;
71      private TermBoService termBoService;
72      private AgendaBoService agendaBoService;
73      private RuleBoService ruleBoService;
74      private ActionBoService actionBoService;
75  
76      @Before
77      public void setup() {
78  
79          super.setup();
80  
81          krmsAttributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
82          krmsTypeRepository = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
83  
84          // like RepositoryCreateAndExecuteIntegrationTest
85          propositionBoService = new PropositionBoServiceImpl();
86          ((PropositionBoServiceImpl)propositionBoService).setDataObjectService(getDataObjectService());
87          termBoService = new TermBoServiceImpl();
88  
89          // TODO: fix
90          ((TermBoServiceImpl)termBoService).setDataObjectService(GlobalResourceLoader.<DataObjectService>getService(
91                  "dataObjectService"));
92  
93          contextRepository = new ContextBoServiceImpl();
94          ((ContextBoServiceImpl)contextRepository).setDataObjectService(getDataObjectService());
95          agendaBoService = new AgendaBoServiceImpl();
96          ((AgendaBoServiceImpl)agendaBoService).setDataObjectService(getDataObjectService());
97          ((AgendaBoServiceImpl)agendaBoService).setAttributeDefinitionService(krmsAttributeDefinitionService);
98          ruleBoService = new RuleBoServiceImpl();
99          ((RuleBoServiceImpl)ruleBoService).setDataObjectService(getDataObjectService());
100         actionBoService = new ActionBoServiceImpl();
101         ((ActionBoServiceImpl)actionBoService).setDataObjectService(getDataObjectService());
102     }
103 
104     @Test
105     public void testEmptyAgendaDelete() {
106         ContextDefinition contextDefintion1 = createContextDefinition(NAMESPACEX, CONTEXTX, Collections.singletonMap(
107                 CONTEXTX_QUALIFIER, CONTEXTX_QUALIFIER_VALUE));
108         createAgendaDefinition(AGENDAX, "AgendaLabel", contextDefintion1);
109         AgendaDefinition agendaDefinition = agendaBoService.getAgendaByNameAndContextId(AGENDAX,
110                 contextDefintion1.getId());
111 
112         lookupAndSaveDataObject(agendaDefinition);
113     }
114 
115     @Test
116     public void testOrphanAgendaItems() {
117         AgendaEditorMaintainable aem = new AgendaEditorMaintainable();
118         AgendaEditor ae = new AgendaEditor();
119         AgendaDefinitionDataWrapper agendaWrapper = new AgendaDefinitionDataWrapper();
120 
121         AgendaBo agendaBoToUpdate = findAgendaByPrimaryKey(agendaWrapper.agenda);
122 
123         //  Before we change anything, verify everything is as expected
124         AgendaItemDefinition toBeDeletedItem1 = agendaBoService.getAgendaItemById( agendaWrapper.firstItem.getId() );
125         AgendaItemDefinition toBeDeletedItem2 = agendaBoService.getAgendaItemById( agendaWrapper.secondItem.getId() );
126         AgendaItemDefinition toBeDeletedItem3 = agendaBoService.getAgendaItemById( agendaWrapper.thirdItem.getId() );
127         AgendaItemDefinition toBeDeletedItem4 = agendaBoService.getAgendaItemById( agendaWrapper.fourthItem.getId() );
128 
129         RuleDefinition toBeDeletedRule1 = ruleBoService.getRuleByRuleId( agendaWrapper.firstItem.getRuleId() );
130         RuleDefinition toBeDeletedRule2 = ruleBoService.getRuleByRuleId( agendaWrapper.secondItem.getRuleId() );
131         RuleDefinition toBeDeletedRule3 = ruleBoService.getRuleByRuleId( agendaWrapper.thirdItem.getRuleId() );
132         RuleDefinition toBeDeletedRule4 = ruleBoService.getRuleByRuleId( agendaWrapper.fourthItem.getRuleId() );
133 
134         PropositionDefinition toBeDeletedProp1 = propositionBoService.getPropositionById( agendaWrapper.firstItem.getRule().getPropId() );
135         PropositionDefinition toBeDeletedProp2 = propositionBoService.getPropositionById( agendaWrapper.secondItem.getRule().getPropId() );
136         PropositionDefinition toBeDeletedProp3 = propositionBoService.getPropositionById( agendaWrapper.thirdItem.getRule().getPropId() );
137         PropositionDefinition toBeDeletedProp4 = propositionBoService.getPropositionById( agendaWrapper.fourthItem.getRule().getPropId() );
138 
139         assertEquals(agendaBoToUpdate.getItems().size(), 4);
140         assertEquals(agendaBoToUpdate.getName(), "TestAgenda1");
141 
142         assertNotNull("First item should be present", toBeDeletedItem1);
143         assertNotNull("Second item should be present", toBeDeletedItem2);
144         assertNotNull("Third item should be present", toBeDeletedItem3);
145         assertNotNull("Fourth item should be present", toBeDeletedItem4);
146 
147         assertNotNull("First Item's rule should be present", toBeDeletedRule1);
148         assertNotNull("Second Item's rule should be present", toBeDeletedRule2);
149         assertNotNull("Third Item's rule should be present", toBeDeletedRule3);
150         assertNotNull("Fourth Item's rule should be present", toBeDeletedRule4);
151 
152         assertNotNull("First Item's proposition should be present", toBeDeletedProp1);
153         assertNotNull("Second Item's proposition should be present", toBeDeletedProp2);
154         assertNotNull("Third Item's proposition should be present", toBeDeletedProp3);
155         assertNotNull("Fourth Item's proposition should be present", toBeDeletedProp4);
156 
157         // Change the agenda!
158         agendaBoToUpdate.setName("Updated Agenda Name!");
159         agendaBoToUpdate.setFirstItem(null);
160         agendaBoToUpdate.setFirstItemId(null);
161         agendaBoToUpdate.setItems(new ArrayList<AgendaItemBo>());
162 
163         ae.setAgenda(agendaBoToUpdate);
164         aem.setDataObject(ae);
165         aem.saveDataObject();
166 
167         // Get the updated agenda and verify agenda items, rule, propositions, etc were updated or deleted.
168         AgendaBo agendaBoUpdated = findAgendaByPrimaryKey(agendaWrapper.agenda);
169 
170         AgendaItemDefinition deletedItem1 = agendaBoService.getAgendaItemById( agendaWrapper.firstItem.getId() );
171         AgendaItemDefinition deletedItem2 = agendaBoService.getAgendaItemById( agendaWrapper.secondItem.getId() );
172         AgendaItemDefinition deletedItem3 = agendaBoService.getAgendaItemById( agendaWrapper.thirdItem.getId() );
173         AgendaItemDefinition deletedItem4 = agendaBoService.getAgendaItemById( agendaWrapper.fourthItem.getId() );
174 
175         RuleDefinition deletedRule1 = ruleBoService.getRuleByRuleId( agendaWrapper.firstItem.getRuleId() );
176         RuleDefinition deletedRule2 = ruleBoService.getRuleByRuleId( agendaWrapper.secondItem.getRuleId() );
177         RuleDefinition deletedRule3 = ruleBoService.getRuleByRuleId( agendaWrapper.thirdItem.getRuleId() );
178         RuleDefinition deletedRule4 = ruleBoService.getRuleByRuleId( agendaWrapper.fourthItem.getRuleId() );
179 
180         PropositionDefinition deletedProp1 = propositionBoService.getPropositionById( agendaWrapper.firstItem.getRule().getPropId() );
181         PropositionDefinition deletedProp2 = propositionBoService.getPropositionById( agendaWrapper.secondItem.getRule().getPropId() );
182         PropositionDefinition deletedProp3 = propositionBoService.getPropositionById( agendaWrapper.thirdItem.getRule().getPropId() );
183         PropositionDefinition deletedProp4 = propositionBoService.getPropositionById( agendaWrapper.fourthItem.getRule().getPropId() );
184 
185         assertEquals(agendaBoUpdated.getItems().size(), 0);
186         assertEquals(agendaBoUpdated.getName(), "Updated Agenda Name!");
187 
188         assertNull("First item should be deleted", deletedItem1);
189         assertNull("Second item should be deleted", deletedItem2);
190         assertNull("Third item should be deleted", deletedItem3);
191         assertNull("Fourth item should be deleted", deletedItem4);
192 
193         assertNull("First Item's rule should be deleted", deletedRule1);
194         assertNull("Second Item's rule should be deleted", deletedRule2);
195         assertNull("Third Item's rule should be deleted", deletedRule3);
196         assertNull("Fourth Item's rule should be deleted", deletedRule4);
197 
198         assertNull("First Item's proposition should be deleted", deletedProp1);
199         assertNull("Second Item's proposition should be deleted", deletedProp2);
200         assertNull("Third Item's proposition should be deleted", deletedProp3);
201         assertNull("Fourth Item's proposition should be deleted", deletedProp4);
202     }
203 
204 /*
205    This test requires:
206    1. changing the BaselineMode to (Mode.NONE).
207    2. Setting a breakpoint on the first line.
208    3. Debug the test.
209    4. When you reach the breakpoint, re-impex (and then apply the db test script) your database.
210    5. Continue.  No exceptions should be thrown.
211     @Test
212     public void testDbAgendaItems() {
213         AgendaDefinition agendaDefinition = agendaBoService.getAgendaByNameAndContextId("My Fabulous Agenda", "CONTEXT1"); // values from populated test db
214         lookupAndSaveDataObject(agendaDefinition);
215         agendaDefinition = agendaBoService.getAgendaByNameAndContextId("SimpleAgendaCompoundProp", "CONTEXT1"); // values from populated test db
216         lookupAndSaveDataObject(agendaDefinition);
217         agendaDefinition = agendaBoService.getAgendaByNameAndContextId("One Big Rule", "CONTEXT1"); // values from populated test db
218         lookupAndSaveDataObject(agendaDefinition);
219     }
220 */
221 
222     private void lookupAndSaveDataObject(AgendaDefinition agendaDefinition) {
223         AgendaBo agendaBo = findAgendaByPrimaryKey(agendaDefinition);
224 
225         AgendaEditorMaintainable aem = new AgendaEditorMaintainable();
226         AgendaEditor ae = new AgendaEditor();
227         ae.setAgenda(agendaBo);
228         aem.setDataObject(ae);
229         aem.saveDataObject();
230     }
231 
232     private AgendaBo findAgendaByPrimaryKey(AgendaDefinition agendaDefinition) {
233         return getDataObjectService().find(AgendaBo.class, agendaDefinition.getId());
234     }
235 
236     private void createAgendaDefinition(String agendaName, String agendaLabel, ContextDefinition contextDefinition) {
237         KrmsTypeDefinition krmsGenericTypeDefinition = createKrmsGenericTypeDefinition(contextDefinition.getNamespace(),
238                 "testAgendaTypeService", agendaName, agendaLabel);
239 
240         AgendaDefinition agendaDefinition = AgendaDefinition.Builder.create(null, agendaName,
241                 krmsGenericTypeDefinition.getId(), contextDefinition.getId()).build();
242         agendaDefinition = agendaBoService.createAgenda(agendaDefinition);
243         agendaBoService.updateAgenda(agendaDefinition);
244 
245         AgendaBo agendaBo = findAgendaByPrimaryKey(agendaDefinition);
246 
247         AgendaDefinition.Builder agendaDefBuilder1 = AgendaDefinition.Builder.create(agendaBoService.to(agendaBo));
248         agendaDefinition = agendaDefBuilder1.build();
249 
250         agendaBoService.updateAgenda(agendaDefinition);
251     }
252 
253     private class AgendaDefinitionDataWrapper {
254         private ContextDefinition context;
255         private AgendaDefinition agenda;
256         private AgendaItemDefinition firstItem;
257         private AgendaItemDefinition secondItem;
258         private AgendaItemDefinition thirdItem;
259         private AgendaItemDefinition fourthItem;
260 
261         private RuleDefinition firstItemRule;
262 
263         AgendaDefinitionDataWrapper() {
264             context = getContextRepository().getContextByNameAndNamespace(CONTEXT1, NAMESPACE1);
265             assertNotNull("context " + CONTEXT1 + " not found", context);
266             agenda = getAgendaBoService().getAgendaByNameAndContextId(AGENDA1, context.getId());
267             assertNotNull("agenda " + AGENDA1 + " not found", agenda);
268 
269             firstItem = getAgendaBoService().getAgendaItemById( agenda.getFirstItemId() );
270             assertNotNull("agenda item " + agenda.getFirstItemId() + " not found", firstItem);
271 
272             secondItem = firstItem.getAlways();
273             thirdItem = secondItem.getAlways();
274             fourthItem = thirdItem.getAlways();
275 
276             firstItemRule = firstItem.getRule();
277         }
278     }
279 }