View Javadoc

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