001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.labor.service;
017:
018: import java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.kuali.core.service.BusinessObjectService;
026: import org.kuali.kfs.KFSPropertyConstants;
027: import org.kuali.kfs.context.KualiTestBase;
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.module.gl.web.TestDataGenerator;
030: import org.kuali.module.labor.bo.LedgerEntry;
031: import org.kuali.module.labor.util.ObjectUtil;
032: import org.kuali.module.labor.util.TestDataPreparator;
033: import org.kuali.module.labor.util.testobject.LedgerEntryForTesting;
034: import org.kuali.test.ConfigureContext;
035:
036: @ConfigureContext
037: public class LaborLedgerEntryServiceTest extends KualiTestBase {
038:
039: private Properties properties;
040: private String fieldNames;
041: private String deliminator;
042: private List<String> keyFieldList;
043: private Map fieldValues;
044:
045: private LaborLedgerEntryService laborLedgerEntryService;
046: private BusinessObjectService businessObjectService;
047:
048: @Override
049: public void setUp() throws Exception {
050: super .setUp();
051: String messageFileName = "test/src/org/kuali/module/labor/testdata/message.properties";
052: String propertiesFileName = "test/src/org/kuali/module/labor/testdata/laborLedgerEntryService.properties";
053:
054: properties = (new TestDataGenerator(propertiesFileName,
055: messageFileName)).getProperties();
056: fieldNames = properties.getProperty("fieldNames");
057: deliminator = properties.getProperty("deliminator");
058: keyFieldList = Arrays.asList(StringUtils.split(fieldNames,
059: deliminator));
060:
061: laborLedgerEntryService = SpringContext
062: .getBean(LaborLedgerEntryService.class);
063: businessObjectService = SpringContext
064: .getBean(BusinessObjectService.class);
065:
066: LedgerEntry cleanup = new LedgerEntry();
067: ObjectUtil.populateBusinessObject(cleanup, properties,
068: "dataCleanup", fieldNames, deliminator);
069: fieldValues = ObjectUtil.buildPropertyMap(cleanup, Arrays
070: .asList(StringUtils.split(fieldNames, deliminator)));
071: businessObjectService.deleteMatching(LedgerEntry.class,
072: fieldValues);
073: }
074:
075: public void testSave() throws Exception {
076: String testTarget = "save.";
077: LedgerEntry input1 = new LedgerEntry();
078: ObjectUtil.populateBusinessObject(input1, properties,
079: testTarget + "testData1", fieldNames, deliminator);
080:
081: LedgerEntry expected1 = new LedgerEntry();
082: ObjectUtil.populateBusinessObject(expected1, properties,
083: testTarget + "expected1", fieldNames, deliminator);
084: Map fieldValues = ObjectUtil.buildPropertyMap(expected1,
085: keyFieldList);
086:
087: businessObjectService.deleteMatching(LedgerEntry.class,
088: fieldValues);
089: assertEquals(0, businessObjectService.countMatching(
090: LedgerEntry.class, fieldValues));
091:
092: laborLedgerEntryService.save(input1);
093: assertEquals(1, businessObjectService.countMatching(
094: LedgerEntry.class, fieldValues));
095:
096: LedgerEntry input2 = new LedgerEntry();
097: ObjectUtil.populateBusinessObject(input2, properties,
098: testTarget + "testData2", fieldNames, deliminator);
099: try {
100: laborLedgerEntryService.save(input2);
101: fail();
102: } catch (Exception e) {
103: }
104: }
105:
106: public void testGetMaxSequenceNumber() throws Exception {
107: String testTarget = "maxSeqNumber.";
108: LedgerEntry input1 = new LedgerEntry();
109: ObjectUtil.populateBusinessObject(input1, properties,
110: testTarget + "testData1", fieldNames, deliminator);
111:
112: Map fieldValues = ObjectUtil.buildPropertyMap(input1,
113: keyFieldList);
114: fieldValues
115: .remove(KFSPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER);
116: businessObjectService.deleteMatching(LedgerEntry.class,
117: fieldValues);
118:
119: Integer maxSeqNumber = laborLedgerEntryService
120: .getMaxSequenceNumber(input1);
121: assertEquals(Integer.valueOf(0), maxSeqNumber);
122:
123: LedgerEntry ledgerEntryExpected1 = new LedgerEntry();
124: String expectedSeqNumber1 = properties.getProperty(testTarget
125: + "expected1");
126:
127: laborLedgerEntryService.save(input1);
128: maxSeqNumber = laborLedgerEntryService
129: .getMaxSequenceNumber(input1);
130: assertEquals(Integer.valueOf(expectedSeqNumber1), maxSeqNumber);
131:
132: LedgerEntry input2 = new LedgerEntry();
133: ObjectUtil.populateBusinessObject(input2, properties,
134: testTarget + "testData2", fieldNames, deliminator);
135:
136: LedgerEntry expected2 = new LedgerEntry();
137: String expectedSeqNumber2 = properties.getProperty(testTarget
138: + "expected2");
139:
140: laborLedgerEntryService.save(input2);
141: maxSeqNumber = laborLedgerEntryService
142: .getMaxSequenceNumber(input1);
143: assertEquals(Integer.valueOf(expectedSeqNumber2), maxSeqNumber);
144:
145: maxSeqNumber = laborLedgerEntryService
146: .getMaxSequenceNumber(input2);
147: assertEquals(Integer.valueOf(expectedSeqNumber2), maxSeqNumber);
148: }
149:
150: public void testFind() throws Exception {
151: String testTarget = "find.";
152: int numberOfTestData = Integer.valueOf(properties
153: .getProperty(testTarget + "numOfData"));
154: int expectedNumOfData = Integer.valueOf(properties
155: .getProperty(testTarget + "expectedNumOfData"));
156:
157: List inputDataList = TestDataPreparator.buildTestDataList(
158: LedgerEntry.class, properties, testTarget + "testData",
159: numberOfTestData);
160: businessObjectService.save(inputDataList);
161:
162: Iterator<LedgerEntry> ledgerEntries = laborLedgerEntryService
163: .find(fieldValues);
164: int counter = 0;
165: List expectedDataList = TestDataPreparator
166: .buildExpectedValueList(LedgerEntryForTesting.class,
167: properties, testTarget + "expected",
168: fieldNames, deliminator, expectedNumOfData);
169: while (ledgerEntries != null && ledgerEntries.hasNext()) {
170: LedgerEntry entry = ledgerEntries.next();
171: LedgerEntryForTesting ledgerEntryForTesting = new LedgerEntryForTesting();
172: ObjectUtil.buildObject(ledgerEntryForTesting, entry);
173: assertTrue(expectedDataList.contains(ledgerEntryForTesting));
174: counter++;
175: }
176: assertEquals(expectedNumOfData, counter);
177: }
178: }
|