View Javadoc
1   /**
2    * Copyright 2005-2016 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.repository;
17  
18  import groovy.mock.interceptor.MockFor;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
23  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
24  import org.kuali.rice.core.framework.persistence.jta.Jta;
25  import org.kuali.rice.krad.data.CopyOption;
26  import org.kuali.rice.krad.data.DataObjectService;
27  import org.kuali.rice.krms.api.KrmsConstants;
28  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
29  import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
30  import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
31  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
32  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
33  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
34  import org.kuali.rice.krms.api.repository.typerelation.RelationshipType;
35  import org.kuali.rice.krms.api.repository.typerelation.TypeTypeRelation;
36  import org.mockito.Mock;
37  import org.mockito.MockitoAnnotations;
38  import org.mockito.runners.MockitoJUnitRunner;
39  import org.kuali.rice.krad.data.CopyOption;
40  
41  import java.util.ArrayList;
42  import java.util.HashMap;
43  import java.util.HashSet;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.Set;
47  
48  import static org.junit.Assert.assertNull;
49  import static org.junit.Assert.assertNotNull;
50  import static org.junit.Assert.assertEquals;
51  
52  import static org.mockito.Mockito.any;
53  import static org.mockito.Mockito.when;
54  
55  /**
56   * @author Kuali Rice Team (rice.collab@kuali.org)
57   */
58  @RunWith(MockitoJUnitRunner.class)
59  public class AgendaItemBoTest {
60      private AgendaItemBo testObject;
61      @Mock
62      KrmsTypeRepositoryService mockKrmsTypeRepositoryService;
63      @Mock
64      DataObjectService mockDataObjectService;
65      @Mock
66      RepositoryBoIncrementer mockRepositoryBoIncrementer;
67      @Mock
68      RuleBo mockRuleBo;
69  
70      @Before
71      public void setUp() {
72          MockitoAnnotations.initMocks(this);
73          testObject = new AgendaItemBo();
74          when(mockKrmsTypeRepositoryService.getTypeById(any(String.class))).thenReturn(KrmsTypeDefinition.Builder.create("name","namespace").build());
75          when(mockDataObjectService.copyInstance(any(AgendaItemBo.class), any(CopyOption.class), any(CopyOption.class)))
76                  .thenReturn(getAgendaItemBoFull("A", "B", "C", "D"))
77                  .thenReturn(getAgendaItemBo("A", "B", "C", getRuleBo("H", "I", "J", "K", null)));
78          when(mockRepositoryBoIncrementer.getNewId()).thenReturn("NEW_ID");
79          testObject.setKrmsTypeRepositoryService(mockKrmsTypeRepositoryService);
80          testObject.setDataObjectService(mockDataObjectService);
81          testObject.agendaItemIdIncrementer = mockRepositoryBoIncrementer;
82      }
83  
84      @Test
85      public void testFromNullParameter() {
86          assertNull(testObject.from(null));
87      }
88  
89      @Test
90      public void testToNullParameter() {
91          assertNull(testObject.to(null));
92      }
93  
94      @Test
95      public void testTo() {
96          AgendaItemBo data = new AgendaItemBo();
97          data.setId("A");
98          data.setAgendaId("B");
99          data.setVersionNumber(0L);
100         data.setRule(getRuleBo("C", "D", "E", "F", null));
101         data.setSubAgendaId("G");
102         data.setWhenTrue(getAgendaItemBo("H", "I", "J", getRuleBo("K", "L", "M", "N", null)));
103         data.setWhenFalse(getAgendaItemBo("O", "P", "Q", getRuleBo("R", "S", "T", "U", null)));
104         data.setAlways(getAgendaItemBo("V", "W", "X", getRuleBo("Y", "Z", "AA", "AB", null)));
105 
106         AgendaItemDefinition result = testObject.to(data);
107 
108         assertEquals("A",result.getId());
109         assertEquals("B",result.getAgendaId());
110         assertEquals("C",result.getRuleId());
111         assertEquals("G",result.getSubAgendaId());
112         assertEquals("J",result.getWhenTrueId());
113         assertEquals("Q",result.getWhenFalseId());
114         assertEquals("X",result.getAlwaysId());
115         assertEquals(0L,(long)result.getVersionNumber());
116         assertNotNull(result.getRule());
117         assertNotNull(result.getWhenTrue());
118         assertNotNull(result.getWhenFalse());
119         assertNotNull(result.getAlways());
120     }
121 
122     @Test
123     public void testFrom() {
124         AgendaItemBo data = testObject.from(getAgendItemDefinitionBuilder());
125 
126         assertEquals("A",data.getId());
127         assertEquals("B",data.getAgendaId());
128         assertEquals("H",data.getRuleId());
129         assertEquals("D",data.getSubAgendaId());
130         assertEquals("Q",data.getWhenTrueId());
131         assertEquals("S",data.getWhenFalseId());
132         assertEquals("U",data.getAlwaysId());
133         assertEquals(0L,(long)data.getVersionNumber());
134         assertNotNull(data.getRule());
135         assertNotNull(data.getWhenTrue());
136         assertNotNull(data.getWhenFalse());
137         assertNotNull(data.getAlways());
138     }
139 
140     @Test
141     public void testGetUl() {
142         AgendaItemBo obj = new AgendaItemBo();
143         AgendaItemBo agenda = getAgendaItemBo("A", "B", "C", getRuleBo("H", "I", "J", "K", null));
144         testObject.setRule(agenda.getRule());
145         testObject.setWhenTrue(agenda.getWhenTrue());
146         testObject.setWhenFalse(agenda.getWhenFalse());
147         testObject.setAlways(agenda.getAlways());
148         String result = testObject.getUl(obj);
149         assertEquals("<ul><li>H</li></ul>", result);
150     }
151 
152     @Test
153     public void testGetUlHelper() {
154         AgendaItemBo obj = new AgendaItemBo();
155         testObject.setAgendaId("B");
156         testObject.setId("A");
157         testObject.setVersionNumber(0L);
158         testObject.setRule(getRuleBo("ID", "NAME", "NAMESPACE", "DESCRIPTION", null));
159         testObject.setSubAgendaId("D");
160         //Note : for this test to pass these next 3 lines must be commented out or null
161         //testObj.setWhenTrue(getAgendaItemBo())
162         //testObj.setWhenFalse(getAgendaItemBo())
163         //testObj.setAlways(getAgendaItemBo())
164         String result = testObject.getUlHelper(obj);
165         assertEquals("<li>ID</li>",result);
166     }
167 
168     @Test(expected = IllegalStateException.class)
169     public void testGetRuleTextInvalidParameter(){
170          testObject.getRuleText();
171     }
172 
173     @Test
174     public void testGetRuleTextValidParameterUnnamedRule(){
175         List<ActionBo> actionList = new ArrayList<ActionBo>();
176         ActionBo action = new ActionBo();
177         action.setName("A");
178         action.setTypeId("B");
179         action.setNamespace("C");
180         action.setSequenceNumber(1);
181         actionList.add(action);
182         testObject.setRule(getRuleBo("D","","E","F", actionList));
183         String result = testObject.getRuleText();
184         assertEquals("- unnamed rule -: F   [name: A]", result);
185     }
186 
187     @Test
188     public void testGetRuleTextValidParameterSingleAction(){
189         List<ActionBo> actionList = new ArrayList<ActionBo>();
190         ActionBo action = new ActionBo();
191         action.setName("A");
192         action.setTypeId("B");
193         action.setNamespace("C");
194         action.setSequenceNumber(1);
195         actionList.add(action);
196 
197         testObject.setRule(getRuleBo("D","E","F","G", actionList));
198 
199         String result = testObject.getRuleText();
200         assertEquals("E: G   [name: A]", result);
201     }
202 
203     @Test
204     public void testGetRuleTextValidParameterMultipleAction(){
205         List<ActionBo> actionList = new ArrayList<ActionBo>();
206         ActionBo action1 = new ActionBo();
207         action1.setName("ActionName1");
208         action1.setTypeId("ActionType1");
209         action1.setNamespace("ActionNamespace1");
210         action1.setSequenceNumber(1);
211         actionList.add(action1);
212         ActionBo action2 = new ActionBo();
213         action2.setName("ActionName2");
214         action2.setTypeId("ActionType2");
215         action2.setNamespace("ActionNamespace2");
216         action2.setSequenceNumber(2);
217         actionList.add(action2);
218 
219         testObject.setRule(getRuleBo("ID","NAME","NAMESPACE","DESCRIPTION", actionList));
220 
221         String result = testObject.getRuleText();
222         assertEquals("NAME: DESCRIPTION   [name: ActionName1 ... ]", result);
223     }
224 
225     @Test
226     public void testCopyAgendaItemValidParameter1() {
227         RuleBo ruleBo = getRuleBo("ID","NAME","NAMESPACE","DESCRIPTION", null);
228         ContextBo contextBo = createContext("NAME", "NAMEPSACE");
229         KrmsAttributeDefinitionBo eventAttributeDefinition = createEventAttributeDefinition("NAME", "NAMESPACE");
230         AgendaBo copiedAgenda = createAgenda(ruleBo, contextBo, eventAttributeDefinition);
231         Map<String, RuleBo> oldRuleIdToNew = new HashMap<String, RuleBo>();
232         Map<String, AgendaItemBo> oldAgendaItemIdToNew = new HashMap<String, AgendaItemBo>();
233         List<AgendaItemBo> copiedAgendaItems = new ArrayList<AgendaItemBo>();
234         String dts = "";
235 
236         when(mockRuleBo.getId()).thenReturn("ID");
237         when(mockRuleBo.copyRule(any(String.class))).thenReturn(getRuleBo("ID","NAME","NAMESPACE","DESCRIPTION", null));
238         testObject.setRule(mockRuleBo);
239         testObject.setId(mockRuleBo.getId());
240         final AgendaItemBo agendaItemBo1 = getAgendaItemBo("A", "B", "C", mockRuleBo);
241         final AgendaItemBo agendaItemBo2 = getAgendaItemBo("D", "E", "F", mockRuleBo);
242         final AgendaItemBo agendaItemBo3 = getAgendaItemBo("G", "H", "I", mockRuleBo);
243         testObject.setWhenFalse(agendaItemBo1);
244         testObject.setWhenFalseId(agendaItemBo1.getId());
245         testObject.setWhenTrue(agendaItemBo2);
246         testObject.setWhenTrueId(agendaItemBo2.getId());
247         testObject.setAlways(agendaItemBo3);
248         testObject.setAlwaysId(agendaItemBo3.getId());
249         testObject.copyAgendaItem(copiedAgenda, oldRuleIdToNew, oldAgendaItemIdToNew, copiedAgendaItems, dts);
250 
251         AgendaItemBo result = testObject.copyAgendaItem(copiedAgenda, oldRuleIdToNew, oldAgendaItemIdToNew, copiedAgendaItems, dts);
252 
253         assertNotNull(result);
254         assertNull(result.getAlways());
255         assertNull(result.getWhenTrue());
256         assertNull(result.getWhenFalse());
257         assertNotNull(result.getRule());
258         assertEquals("NEW_ID",result.getId());
259         assertEquals("B",result.getSubAgendaId());
260     }
261 
262     @Test
263     public void testCopyAgendaItemValidParameter2() {
264         RuleBo ruleBo = getRuleBo("ID","NAME","NAMESPACE","DESCRIPTION", null);
265         ContextBo contextBo = createContext("NAME", "NAMEPSACE");
266         KrmsAttributeDefinitionBo eventAttributeDefinition = createEventAttributeDefinition("NAME", "NAMESPACE");
267         AgendaBo copiedAgenda = createAgenda(ruleBo, contextBo, eventAttributeDefinition);
268         Map<String, RuleBo> oldRuleIdToNew = new HashMap<String, RuleBo>();
269         Map<String, AgendaItemBo> oldAgendaItemIdToNew = new HashMap<String, AgendaItemBo>();
270         List<AgendaItemBo> copiedAgendaItems = new ArrayList<AgendaItemBo>();
271         String dts = "";
272 
273         when(mockRuleBo.getId()).thenReturn("C");
274         when(mockRuleBo.copyRule(any(String.class))).thenReturn(getRuleBo("C","NAME","NAMESPACE","DESCRIPTION", null));
275         testObject.setRule(mockRuleBo);
276         testObject.setId(mockRuleBo.getId());
277         final AgendaItemBo agendaItemBo1 = getAgendaItemBo("A", "B", "C", mockRuleBo);
278         final AgendaItemBo agendaItemBo2 = getAgendaItemBo("A", "D", "C", mockRuleBo);
279         final AgendaItemBo agendaItemBo3 = getAgendaItemBo("A", "B", "C", mockRuleBo);
280         testObject.setWhenFalse(agendaItemBo1);
281         testObject.setWhenFalseId(agendaItemBo1.getId());
282         testObject.setWhenTrue(agendaItemBo2);
283         testObject.setWhenTrueId(agendaItemBo2.getId());
284         testObject.setAlways(agendaItemBo3);
285         testObject.setAlwaysId(agendaItemBo3.getId());
286 
287         AgendaItemBo result = testObject.copyAgendaItem(copiedAgenda, oldRuleIdToNew, oldAgendaItemIdToNew, copiedAgendaItems, dts);
288 
289         assertNotNull(result);
290         assertNotNull(result.getAlways());
291         assertNotNull(result.getWhenTrue());
292         assertNotNull(result.getWhenFalse());
293         assertNotNull(result.getRule());
294         assertEquals("NEW_ID",result.getId());
295         assertEquals("NEW_ID",result.getAlwaysId());
296         assertEquals("NEW_ID",result.getWhenTrueId());
297         assertEquals("NEW_ID",result.getWhenFalseId());
298         assertEquals("D",result.getSubAgendaId());
299     }
300 
301     @Test
302     public void testGetAlwaysList() {
303         final AgendaItemBo agendaItemBo1 = getAgendaItemBo("A", "B", "C", null);
304         final AgendaItemBo agendaItemBo2 = getAgendaItemBo("D", "E", "F", null);
305         agendaItemBo2.setAlways(agendaItemBo1);
306         final AgendaItemBo agendaItemBo3 = getAgendaItemBo("G", "H", "I", null);
307         agendaItemBo3.setAlways(agendaItemBo2);
308         testObject.setAlways(agendaItemBo3);
309 
310         List<AgendaItemBo> result = testObject.getAlwaysList();
311 
312         assertNotNull(result);
313         assertEquals(3,result.size());
314     }
315 
316     private ContextBo createContext(String name, String namespace) {
317         KrmsTypeDefinition.Builder typeDefinition = KrmsTypeDefinition.Builder.create(name, namespace);
318         typeDefinition.setId("ID");
319         KrmsTypeDefinition defaultContextType = typeDefinition.build();
320 
321         ContextBo contextBo = new ContextBo();
322         contextBo.setNamespace(KrmsConstants.KRMS_NAMESPACE);
323         contextBo.setName("MyContext");
324         contextBo.setTypeId(defaultContextType.getId());
325         return contextBo;
326     }
327 
328     private KrmsAttributeDefinitionBo createEventAttributeDefinition(String name, String namespace) {
329         KrmsAttributeDefinitionBo attributeDefinitionBo = new KrmsAttributeDefinitionBo();
330         attributeDefinitionBo.setNamespace(namespace);
331         attributeDefinitionBo.setName(name);
332         attributeDefinitionBo.setLabel("Event");
333         attributeDefinitionBo.setActive(true);
334         return attributeDefinitionBo;
335     }
336 
337     private AgendaBo createAgenda(RuleBo ruleBo, ContextBo contextBo, KrmsAttributeDefinitionBo eventAttributeDefinition) {
338         AgendaBo agendaBo = new AgendaBo();
339         agendaBo.setActive(true);
340         agendaBo.setContextId(contextBo.getId());
341         agendaBo.setName("MyAgenda");
342         agendaBo.setTypeId(null);
343 
344         AgendaItemBo agendaItemBo = getAgendaItemBo("A", "B", "C", getRuleBo("H", "I", "J", "K", null));
345         List<AgendaItemBo> agendaItems = new ArrayList<AgendaItemBo>();
346         agendaItems.add(agendaItemBo);
347 
348         agendaBo.setItems(agendaItems);
349         agendaBo.setFirstItemId(agendaItemBo.getId());
350         agendaBo.setFirstItem(agendaItemBo);
351 
352         Set<AgendaAttributeBo> agendaAttributes = new HashSet<AgendaAttributeBo>();
353         agendaBo.setAttributeBos(agendaAttributes);
354         AgendaAttributeBo agendaAttribute = new AgendaAttributeBo();
355         agendaAttributes.add(agendaAttribute);
356         agendaAttribute.setAttributeDefinition(eventAttributeDefinition);
357         agendaAttribute.setValue("workflow");
358         agendaAttribute.setAgenda(agendaBo);
359 
360         contextBo.getAgendas().add(agendaBo);
361 
362         return agendaBo;
363     }
364 
365     private RuleBo getRuleBo(String id, String name, String namespace, String description, List<ActionBo> actionList) {
366         RuleBo obj = new RuleBo();
367         obj.setId(id);
368         obj.setName(name);
369         obj.setNamespace(namespace);
370         obj.setDescription(description);
371         obj.setActions(actionList);
372         return obj;
373     }
374 
375     private AgendaItemBo getAgendaItemBoFull(String id, String name, String namespace, String description) {
376         AgendaItemBo obj = new AgendaItemBo();
377         obj.setAgendaId("B");
378         obj.setId("A");
379         obj.setVersionNumber(0L);
380         obj.setRule(getRuleBo(id, name, namespace, description, null));
381         obj.setSubAgendaId("D");
382         obj.setWhenTrue(getAgendaItemBo("A", "B", "C", getRuleBo("H", "I", "J", "K", null)));
383         obj.setWhenFalse(getAgendaItemBo("A", "B", "C", getRuleBo("H", "I", "J", "K", null)));
384         obj.setAlways(getAgendaItemBo("A", "B", "C", getRuleBo("H", "I", "J", "K", null)));
385         return obj;
386     }
387 
388     private AgendaItemBo getAgendaItemBo(String agendaId, String sunAgendaId, String id, RuleBo rule) {
389         AgendaItemBo obj = new AgendaItemBo();
390         obj.setAgendaId(agendaId);
391         obj.setId(id);
392         obj.setVersionNumber(0L);
393         obj.setRule(rule);
394         obj.setSubAgendaId(sunAgendaId);
395         obj.setWhenTrue(null);
396         obj.setWhenFalse(null);
397         obj.setAlways(null);
398         obj.setDataObjectService(mockDataObjectService);
399         return obj;
400     }
401 
402     private AgendaItemDefinition getAgendItemDefinitionBuilder() {
403         AgendaItemDefinition.Builder itemDefinition = AgendaItemDefinition.Builder.create("A", "B");
404         itemDefinition.setRuleId("C");
405         itemDefinition.setSubAgendaId("D");
406         itemDefinition.setWhenTrueId("E");
407         itemDefinition.setWhenFalseId("F");
408         itemDefinition.setAlwaysId("G");
409         itemDefinition.setRule(RuleDefinition.Builder.create("H", "I", "J", "K", "L"));
410         itemDefinition.setSubAgenda(AgendaDefinition.Builder.create("M", "N", "O", "P"));
411         itemDefinition.setWhenTrue(AgendaItemDefinition.Builder.create("Q", "R"));
412         itemDefinition.setWhenFalse(AgendaItemDefinition.Builder.create("S", "T"));
413         itemDefinition.setAlways(AgendaItemDefinition.Builder.create("U", "V"));
414         itemDefinition.setVersionNumber(0L);
415         return itemDefinition.build();
416     }
417 }