001 /** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krms.impl.repository; 017 018 import org.junit.Test; 019 020 import org.kuali.rice.krad.bo.BusinessObject; 021 import org.kuali.rice.krad.service.SequenceAccessorService; 022 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition; 023 import org.kuali.rice.krms.api.repository.context.ContextDefinition; 024 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition; 025 import org.kuali.rice.krms.api.repository.proposition.PropositionType; 026 import org.kuali.rice.krms.api.repository.rule.RuleDefinition; 027 028 import java.util.HashMap; 029 import java.util.Iterator; 030 import java.util.LinkedList; 031 import java.util.List; 032 import java.util.Map; 033 034 import static org.junit.Assert.*; 035 036 public class AgendaBoTest { 037 038 public static final String TEST_PREFIX = "AgendaBoTest"; 039 @Test 040 public void testCopy() { 041 SequenceAccessorService mapSas = new IncrementalSas(); 042 AgendaBo.setSequenceAccessorService(mapSas); 043 AgendaItemBo.setSequenceAccessorService(mapSas); 044 RuleBo.setSequenceAccessorService(mapSas); 045 PropositionBo.setSequenceAccessorService(mapSas); 046 047 ContextBo contextBo = ContextBo.from(ContextDefinition.Builder.create(TEST_PREFIX + "ContextName", TEST_PREFIX + "Namespace").build()); 048 contextBo.setId(TEST_PREFIX + "ContextId"); 049 050 // AgendaBo agendaBo = AgendaBo.from(AgendaDefinition.Builder.create(TEST_PREFIX + "AgendaId00", TEST_PREFIX + "AgendaName", null, contextBo.getId())); 051 AgendaBo agendaBo = new AgendaBo(); 052 agendaBo.setId(TEST_PREFIX + "AgendaId00"); 053 agendaBo.setName(TEST_PREFIX + "AgendaName"); 054 agendaBo.setContext(contextBo); 055 056 RuleBo ruleBo00 = newTestRuleBo("00"); 057 RuleBo ruleBo01 = newTestRuleBo("01"); 058 RuleBo ruleBo02 = newTestRuleBo("02"); 059 RuleBo ruleBo03 = newTestRuleBo("03"); 060 RuleBo ruleBo04 = newTestRuleBo("04"); 061 RuleBo ruleBo05 = newTestRuleBo("05"); 062 RuleBo ruleBo06 = newTestRuleBo("06"); 063 064 List<AgendaItemBo> agendaItemBos = new LinkedList<AgendaItemBo>(); 065 AgendaItemBo agendaItemBo00 = newTestAgendaItemBo("00", ruleBo00, agendaBo.getId()); 066 agendaItemBos.add(agendaItemBo00); 067 AgendaItemBo agendaItemBo01 = newTestAgendaItemBo("01", ruleBo01, agendaBo.getId()); 068 agendaItemBos.add(agendaItemBo01); 069 AgendaItemBo agendaItemBo02 = newTestAgendaItemBo("02", ruleBo02, agendaBo.getId()); 070 agendaItemBos.add(agendaItemBo02); 071 AgendaItemBo agendaItemBo03 = newTestAgendaItemBo("03", ruleBo03, agendaBo.getId()); 072 agendaItemBos.add(agendaItemBo03); 073 AgendaItemBo agendaItemBo04 = newTestAgendaItemBo("04", ruleBo04, agendaBo.getId()); 074 agendaItemBos.add(agendaItemBo04); 075 AgendaItemBo agendaItemBo05 = newTestAgendaItemBo("05", ruleBo05, agendaBo.getId()); 076 agendaItemBos.add(agendaItemBo05); 077 AgendaItemBo agendaItemBo06 = newTestAgendaItemBo("06", ruleBo06, agendaBo.getId()); 078 agendaItemBos.add(agendaItemBo06); 079 080 // My Fabulous Agenda 081 agendaItemBo00.setWhenTrue(agendaItemBo01); 082 agendaItemBo00.setWhenFalse(agendaItemBo04); 083 agendaItemBo00.setAlways(agendaItemBo05); 084 085 agendaItemBo01.setAlways(agendaItemBo02); 086 087 agendaItemBo02.setWhenFalse(agendaItemBo06); 088 agendaItemBo02.setAlways(agendaItemBo03); 089 090 agendaBo.setFirstItemId(agendaItemBo00.getId()); 091 092 agendaBo.setItems(agendaItemBos); 093 094 AgendaBo copiedAgenda = agendaBo.copyAgenda("NewAgendaCopy", "dts123"); 095 096 assertFalse(agendaItemBo00.getAgendaId() == null); 097 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()); 098 099 // 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 }