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.repository;
17  
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  
22  import java.util.Iterator;
23  import java.util.LinkedList;
24  import java.util.List;
25  import java.util.concurrent.atomic.AtomicLong;
26  
27  import org.junit.Test;
28  import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
29  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
30  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
31  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
32  import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
33  import org.kuali.rice.krms.test.KRMSTestCase;
34  import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
35  
36  public class AgendaBoTest extends KRMSTestCase {
37  
38      public static final String TEST_PREFIX = "AgendaBoTest";
39  
40      @Test
41      public void testCopy() {
42          AgendaBo.agendaIdIncrementer.setDataFieldMaxValueIncrementer(new MockDataFieldMaxValueIncrementer());
43          AgendaItemBo.agendaItemIdIncrementer.setDataFieldMaxValueIncrementer(new MockDataFieldMaxValueIncrementer());
44  
45          // RuleBo has multiple incrementers to set
46          RuleBo.ruleIdIncrementer.setDataFieldMaxValueIncrementer(new MockDataFieldMaxValueIncrementer());
47          RuleBo.actionAttributeIdIncrementer.setDataFieldMaxValueIncrementer(new MockDataFieldMaxValueIncrementer());
48          RuleBo.actionIdIncrementer.setDataFieldMaxValueIncrementer(new MockDataFieldMaxValueIncrementer());
49          RuleBo.ruleAttributeIdIncrementer.setDataFieldMaxValueIncrementer(new MockDataFieldMaxValueIncrementer());
50  
51          // PropositionBo has multiple incrementers to set too
52          PropositionBo.propositionIdIncrementer.setDataFieldMaxValueIncrementer(new MockDataFieldMaxValueIncrementer());
53          PropositionBo.propositionParameterIdIncrementer.setDataFieldMaxValueIncrementer(new MockDataFieldMaxValueIncrementer());
54  
55          ContextBo contextBo = ContextBo.from(ContextDefinition.Builder.create(TEST_PREFIX + "ContextName", TEST_PREFIX + "Namespace").build());
56          contextBo.setId(TEST_PREFIX + "ContextId");
57  
58  //        AgendaBo agendaBo = AgendaBo.from(AgendaDefinition.Builder.create(TEST_PREFIX + "AgendaId00", TEST_PREFIX + "AgendaName", null, contextBo.getId()));
59          AgendaBo agendaBo = new AgendaBo();
60          agendaBo.setId(TEST_PREFIX + "AgendaId00");
61          agendaBo.setName(TEST_PREFIX + "AgendaName");
62          agendaBo.setContext(contextBo);
63  
64          RuleBo ruleBo00 = newTestRuleBo("00");
65          RuleBo ruleBo01 = newTestRuleBo("01");
66          RuleBo ruleBo02 = newTestRuleBo("02");
67          RuleBo ruleBo03 = newTestRuleBo("03");
68          RuleBo ruleBo04 = newTestRuleBo("04");
69          RuleBo ruleBo05 = newTestRuleBo("05");
70          RuleBo ruleBo06 = newTestRuleBo("06");
71  
72          List<AgendaItemBo> agendaItemBos = new LinkedList<AgendaItemBo>();
73          AgendaItemBo agendaItemBo00 = newTestAgendaItemBo("00", ruleBo00, agendaBo.getId());
74          agendaItemBos.add(agendaItemBo00);
75          AgendaItemBo agendaItemBo01 = newTestAgendaItemBo("01", ruleBo01, agendaBo.getId());
76          agendaItemBos.add(agendaItemBo01);
77          AgendaItemBo agendaItemBo02 = newTestAgendaItemBo("02", ruleBo02, agendaBo.getId());
78          agendaItemBos.add(agendaItemBo02);
79          AgendaItemBo agendaItemBo03 = newTestAgendaItemBo("03", ruleBo03, agendaBo.getId());
80          agendaItemBos.add(agendaItemBo03);
81          AgendaItemBo agendaItemBo04 = newTestAgendaItemBo("04", ruleBo04, agendaBo.getId());
82          agendaItemBos.add(agendaItemBo04);
83          AgendaItemBo agendaItemBo05 = newTestAgendaItemBo("05", ruleBo05, agendaBo.getId());
84          agendaItemBos.add(agendaItemBo05);
85          AgendaItemBo agendaItemBo06 = newTestAgendaItemBo("06", ruleBo06, agendaBo.getId());
86          agendaItemBos.add(agendaItemBo06);
87  
88          // My Fabulous Agenda
89          agendaItemBo00.setWhenTrue(agendaItemBo01);
90          agendaItemBo00.setWhenFalse(agendaItemBo04);
91          agendaItemBo00.setAlways(agendaItemBo05);
92  
93          agendaItemBo01.setAlways(agendaItemBo02);
94  
95          agendaItemBo02.setWhenFalse(agendaItemBo06);
96          agendaItemBo02.setAlways(agendaItemBo03);
97  
98          agendaBo.setFirstItemId(agendaItemBo00.getId());
99          agendaBo.setFirstItem(agendaItemBo00);
100 
101         agendaBo.setItems(agendaItemBos);
102 
103         AgendaBo copiedAgenda = agendaBo.copyAgenda("NewAgendaCopy", "dts123");
104 
105         assertFalse(agendaItemBo00.getAgendaId() == null);
106         assertTrue("agendaBo.getItems().size() of " + agendaBo.getItems().size() + " is not equal to copiedAgenda.getItems().size() of "+ copiedAgenda.getItems().size(), agendaBo.getItems().size() == copiedAgenda.getItems().size());
107 
108         // Assert that the agenda item ids are 0 - 6
109         boolean[] ids = {false, false, false, false, false, false, false};
110         assertTrue("agendaBo.getItems().size() of " + agendaBo.getItems().size() + " does not match ids.length" + ids.length, agendaBo.getItems().size() == ids.length);
111         Iterator<AgendaItemBo> copiedItems = copiedAgenda.getItems().iterator();
112         int index = 0;
113         while (copiedItems.hasNext()) {
114             try {
115                 index = Integer.parseInt(copiedItems.next().getId());
116                 ids[index] = true;
117             } catch (IndexOutOfBoundsException e) {
118                 fail("copied agenda item id " +  index + " is higher than " + ids.length);
119             }
120         }
121         for (int i = 0; i < ids.length; i++) {
122             assertTrue("agenda item id verification of " + i + " is false", ids[i]);
123         }
124 
125         // Assert agenda item version numbers are all the same always 0 in unit tests, but IT would have real values
126         copiedItems = copiedAgenda.getItems().iterator();
127         long lastVersion = -1L;
128         long version = -1L;
129         while (copiedItems.hasNext()) {
130             AgendaItemBo agendaItemBo = copiedItems.next();
131             if (agendaItemBo.getVersionNumber() != null) {  // if null throws nullpointer in unboxing
132                 version = agendaItemBo.getVersionNumber();
133                 if (lastVersion == -1L) {
134                     lastVersion = version;
135                 } else {
136                     assertTrue(lastVersion == version);
137                     lastVersion = version;
138                 }
139             }
140         }
141     }
142 
143 
144     private RuleBo newTestRuleBo(String testId) {
145         String ruleId = TEST_PREFIX + "RuleId" + testId;
146         PropositionDefinition.Builder prop = PropositionDefinition.Builder.create(TEST_PREFIX + "PropositionId" + testId, PropositionType.SIMPLE.getCode(), ruleId, null, null);
147         RuleDefinition.Builder builder = RuleDefinition.Builder.create(ruleId, ruleId + "Name", TEST_PREFIX + "Namespace", null, prop.getId());
148         builder.setProposition(prop);
149         return RuleBo.from(builder.build());
150     }
151 
152     private AgendaItemDefinition.Builder newTestAgendaItem(String testId, RuleBo ruleBo, String agendaId) {
153         return AgendaItemDefinition.Builder.create(TEST_PREFIX + "AgendaItemId" + testId, agendaId);
154     }
155 
156     private AgendaItemBo newTestAgendaItemBo(String testId, RuleBo ruleBo, String agendaId) {
157         return AgendaItemBo.from(newTestAgendaItem(testId, ruleBo, agendaId).build());
158     }
159 }
160 
161 /**
162  * mock incrementer used to get "sequence values" when there is no live database
163  */
164 class MockDataFieldMaxValueIncrementer extends AbstractDataFieldMaxValueIncrementer {
165 
166     AtomicLong value = new AtomicLong(-1);
167 
168     @Override
169     protected long getNextKey() {
170         return value.incrementAndGet();
171     }
172 }