1 package org.kuali.ole.select.document.service.impl;
2
3 import org.apache.commons.csv.CSVFormat;
4 import org.apache.commons.csv.CSVParser;
5 import org.apache.commons.csv.CSVRecord;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.kuali.ole.module.purap.businessobject.InvoiceAccount;
9 import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
10 import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
11 import org.kuali.ole.select.businessobject.OleRequestSourceType;
12 import org.kuali.ole.select.document.OlePurchaseOrderDocument;
13 import org.kuali.rice.core.api.util.type.KualiDecimal;
14 import org.kuali.rice.core.api.util.type.KualiInteger;
15 import org.kuali.rice.krad.bo.BusinessObject;
16 import org.kuali.rice.krad.bo.DocumentHeader;
17 import org.kuali.rice.krad.bo.PersistableBusinessObject;
18 import org.kuali.rice.krad.service.BusinessObjectService;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22
23 import java.io.File;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.io.Writer;
27 import java.nio.charset.Charset;
28 import java.util.*;
29
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
32
33
34
35
36 public class OLEPurchaseOrderBatchServiceImplTest {
37
38 @Mock
39 private FileWriter mockFileWriter;
40 @Mock
41 private OlePurchaseOrderDocument mockPurchaseOrderDocument;
42 @Mock
43 private DocumentHeader mockDocumentHeader;
44 @Mock
45 private OlePurchaseOrderItem mockItem1;
46 @Mock
47 private OlePurchaseOrderItem mockItem2;
48 @Mock
49 private OlePurchaseOrderItem mockItem3;
50
51
52 private String tempDir;
53 private String fileName;
54
55 @Before
56 public void setUp() throws Exception {
57 MockitoAnnotations.initMocks(this);
58
59 tempDir = System.getProperty("java.io.tmpdir");
60 fileName = tempDir + "poba.csv";
61 }
62
63 @Test
64 public void testDownloadCSV() throws Exception {
65 OLEPurchaseOrderBatchServiceImpl olePurchaseOrderBatchService = new OLEPurchaseOrderBatchServiceImpl();
66
67 olePurchaseOrderBatchService.setFileWriter(new FileWriter(fileName));
68 olePurchaseOrderBatchService.setBusinessObjectService(new CustomBusinessObjectService());
69
70 ArrayList items = new ArrayList();
71 Mockito.when(mockItem1.getItemLineNumber()).thenReturn(1);
72 ArrayList<PurApAccountingLine> purApAccountingLines = new ArrayList<>();
73 PurApAccountingLine purApAccountingLine = (PurApAccountingLine) getAccountingLineClass().newInstance();
74 purApAccountingLines.add(purApAccountingLine);
75 Mockito.when(mockItem1.getSourceAccountingLines()).thenReturn(purApAccountingLines);
76 Mockito.when(mockItem1.getFormatTypeName()).thenReturn(null);
77 Mockito.when(mockItem1.getCategory()).thenReturn(null);
78 Mockito.when(mockItem1.getItemPriceSource()).thenReturn(null);
79 Mockito.when(mockItem1.getOleRequestSourceType()).thenReturn(new OleRequestSourceType());
80 Mockito.when(mockItem1.isItemRouteToRequestorIndicator()).thenReturn(true);
81 Mockito.when(mockItem1.isItemPublicViewIndicator()).thenReturn(true);
82 Mockito.when(mockItem1.getItemQuantity()).thenReturn(new KualiDecimal(2.0));
83 Mockito.when(mockItem1.getItemListPrice()).thenReturn(new KualiDecimal(2.0));
84 Mockito.when(mockItem1.getItemNoOfParts()).thenReturn(new KualiInteger(12));
85
86 items.add(mockItem1);
87
88 Mockito.when(mockItem2.getItemLineNumber()).thenReturn(2);
89 Mockito.when(mockItem2.getSourceAccountingLines()).thenReturn(purApAccountingLines);
90 Mockito.when(mockItem2.getFormatTypeName()).thenReturn(null);
91 Mockito.when(mockItem2.getCategory()).thenReturn(null);
92 Mockito.when(mockItem2.getItemPriceSource()).thenReturn(null);
93 Mockito.when(mockItem2.getOleRequestSourceType()).thenReturn(new OleRequestSourceType());
94 Mockito.when(mockItem2.isItemRouteToRequestorIndicator()).thenReturn(true);
95 Mockito.when(mockItem2.isItemRouteToRequestorIndicator()).thenReturn(true);
96 Mockito.when(mockItem2.isItemPublicViewIndicator()).thenReturn(true);
97 Mockito.when(mockItem2.getItemQuantity()).thenReturn(new KualiDecimal(2.0));
98 Mockito.when(mockItem2.getItemListPrice()).thenReturn(new KualiDecimal(2.0));
99 Mockito.when(mockItem2.getItemNoOfParts()).thenReturn(new KualiInteger(12));
100 items.add(mockItem2);
101
102 Mockito.when(mockItem3.getItemLineNumber()).thenReturn(3);
103 Mockito.when(mockItem3.getSourceAccountingLines()).thenReturn(purApAccountingLines);
104 Mockito.when(mockItem3.getFormatTypeName()).thenReturn(null);
105 Mockito.when(mockItem3.getCategory()).thenReturn(null);
106 Mockito.when(mockItem3.getItemPriceSource()).thenReturn(null);
107 Mockito.when(mockItem3.getOleRequestSourceType()).thenReturn(new OleRequestSourceType());
108 Mockito.when(mockItem3.isItemRouteToRequestorIndicator()).thenReturn(true);
109 Mockito.when(mockItem3.isItemRouteToRequestorIndicator()).thenReturn(true);
110 Mockito.when(mockItem3.isItemPublicViewIndicator()).thenReturn(true);
111 Mockito.when(mockItem3.getItemQuantity()).thenReturn(new KualiDecimal(2.0));
112 Mockito.when(mockItem3.getItemListPrice()).thenReturn(new KualiDecimal(2.0));
113 Mockito.when(mockItem3.getItemNoOfParts()).thenReturn(new KualiInteger(12));
114 items.add(mockItem3);
115
116 Mockito.when(mockPurchaseOrderDocument.getItems()).thenReturn(items);
117 Mockito.when(mockPurchaseOrderDocument.getDocumentNumber()).thenReturn("121");
118 Mockito.when(mockPurchaseOrderDocument.getDocumentHeader()).thenReturn(mockDocumentHeader);
119 Mockito.when(mockDocumentHeader.getDocumentDescription()).thenReturn("Document Description");
120 Mockito.when(mockPurchaseOrderDocument.getPurapDocumentIdentifier()).thenReturn(12);
121 Mockito.when(mockPurchaseOrderDocument.getDeliveryBuildingCode()).thenReturn("Building Code");
122
123 ArrayList<String> poIds = new ArrayList<>();
124 poIds.add("1");
125 poIds.add("2");
126 poIds.add("3");
127 poIds.add("4");
128 poIds.add("5");
129
130 olePurchaseOrderBatchService.downloadCSV(poIds);
131
132
133
134
135
136
137 List<Map> list = new ArrayList<>();
138 File csvData = new File(fileName);
139 CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.EXCEL);
140 Iterator<CSVRecord> iterator = parser.getRecords().iterator();
141 CSVRecord headerData = iterator.next();
142 while (iterator.hasNext()) {
143 CSVRecord csvRecord = iterator.next();
144 Map map = new HashMap();
145 for(int x = 0; x < headerData.size(); x++){
146 map.put(headerData.get(x), csvRecord.get(x));
147 }
148 list.add(map);
149 }
150
151 assertTrue(!list.isEmpty());
152 for (Iterator<Map> mapIterator = list.iterator(); mapIterator.hasNext(); ) {
153 Map map = mapIterator.next();
154 for (Iterator iterator1 = map.keySet().iterator(); iterator1.hasNext(); ) {
155 String key = (String) iterator1.next();
156 System.out.println(key + " : " + map.get(key));
157 }
158 }
159
160 }
161
162 public Class getAccountingLineClass() {
163 return InvoiceAccount.class;
164 }
165
166 class MockFileWriter extends FileWriter {
167 @Override
168 public Writer append(CharSequence csq) throws IOException {
169 return super.append(csq);
170 }
171
172 public MockFileWriter(String fileName) throws IOException {
173 super(fileName);
174 }
175 }
176
177 class CustomBusinessObjectService implements BusinessObjectService {
178
179 @Override
180 public <T extends PersistableBusinessObject> T save(T t) {
181 return null;
182 }
183
184 @Override
185 public List<? extends PersistableBusinessObject> save(List<? extends PersistableBusinessObject> list) {
186 return null;
187 }
188
189 @Override
190 public PersistableBusinessObject linkAndSave(PersistableBusinessObject persistableBusinessObject) {
191 return null;
192 }
193
194 @Override
195 public List<? extends PersistableBusinessObject> linkAndSave(List<? extends PersistableBusinessObject> list) {
196 return null;
197 }
198
199 @Override
200 public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> aClass, Object o) {
201 return null;
202 }
203
204 @Override
205 public <T extends BusinessObject> T findByPrimaryKey(Class<T> aClass, Map<String, ?> map) {
206 return (T) mockPurchaseOrderDocument;
207 }
208
209 @Override
210 public PersistableBusinessObject retrieve(PersistableBusinessObject persistableBusinessObject) {
211 return null;
212 }
213
214 @Override
215 public <T extends BusinessObject> Collection<T> findAll(Class<T> aClass) {
216 return null;
217 }
218
219 @Override
220 public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> aClass, String s, boolean b) {
221 return null;
222 }
223
224 @Override
225 public <T extends BusinessObject> Collection<T> findMatching(Class<T> aClass, Map<String, ?> map) {
226 return null;
227 }
228
229 @Override
230 public int countMatching(Class aClass, Map<String, ?> map) {
231 return 0;
232 }
233
234 @Override
235 public int countMatching(Class aClass, Map<String, ?> map, Map<String, ?> map1) {
236 return 0;
237 }
238
239 @Override
240 public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> aClass, Map<String, ?> map, String s, boolean b) {
241 return null;
242 }
243
244 @Override
245 public void delete(PersistableBusinessObject persistableBusinessObject) {
246
247 }
248
249 @Override
250 public void delete(List<? extends PersistableBusinessObject> list) {
251
252 }
253
254 @Override
255 public void deleteMatching(Class aClass, Map<String, ?> map) {
256
257 }
258
259 @Override
260 public BusinessObject getReferenceIfExists(BusinessObject businessObject, String s) {
261 return null;
262 }
263
264 @Override
265 public void linkUserFields(PersistableBusinessObject persistableBusinessObject) {
266
267 }
268
269 @Override
270 public void linkUserFields(List<PersistableBusinessObject> list) {
271
272 }
273
274 @Override
275 public PersistableBusinessObject manageReadOnly(PersistableBusinessObject persistableBusinessObject) {
276 return null;
277 }
278 }
279
280 }