View Javadoc
1   package org.kuali.ole.deliver.util.printSlip;
2   
3   import org.junit.Before;
4   import org.junit.Test;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.bo.OleCirculationDesk;
7   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
8   import org.kuali.ole.deliver.bo.OleItemSearch;
9   import org.kuali.ole.deliver.bo.OlePatronDocument;
10  import org.kuali.ole.deliver.drools.CheckedInItem;
11  import org.kuali.ole.deliver.form.CheckinForm;
12  import org.kuali.ole.deliver.util.ItemInfoUtil;
13  import org.kuali.ole.deliver.util.OleItemRecordForCirc;
14  import org.kuali.ole.deliver.util.OleRegularPrintSlipUtil;
15  import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
16  import org.kuali.ole.util.DocstoreUtil;
17  import org.mockito.Mock;
18  import org.mockito.Mockito;
19  import org.mockito.MockitoAnnotations;
20  
21  import javax.servlet.http.HttpServletResponse;
22  import java.io.*;
23  import java.sql.Date;
24  import java.sql.Timestamp;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import static junit.framework.TestCase.assertNotNull;
29  
30  /**
31   * Created by sheiksalahudeenm on 7/30/15.
32   */
33  public class OlePrintSlipUtilTest {
34  
35      @Mock
36      private HttpServletResponse mockResponse;
37  
38      @Mock
39      private OutputStream mockOutputStream;
40  
41      @Mock
42      private ItemInfoUtil mockItemInfoUtil;
43  
44      @Mock
45      private ItemRecord mockItemRecord;
46  
47      @Mock
48      private OleCirculationDesk mockOleCirculationDesk;
49  
50      @Mock
51      private OleDeliverRequestBo mockOleDeliverRequestBo;
52  
53      @Mock
54      private OleItemRecordForCirc mockOleItemRecordForCirc;
55  
56      @Mock
57      private CheckinForm mockCheckinForm;
58  
59      @Mock
60      private DocstoreUtil mockDocstoreUtil;
61  
62      @Mock
63      private OleItemSearch mockItemSearch;
64  
65      @Mock
66      private OlePatronDocument mockOlePatron;
67      @Mock
68      private List<CheckedInItem> mockCheckedInItemList;
69  
70      @Mock
71      private CheckedInItem mockCheckedInItem;
72  
73      @Before
74      public void setUp() throws Exception {
75          MockitoAnnotations.initMocks(this);
76      }
77  
78      @Test
79      public void printBillForInTransitTest() throws IOException {
80          printSlilp(OLEConstants.ITEM_STATUS_IN_TRANSIT);
81      }
82  
83      @Test
84      public void printBillForInTransitForHoldTest() throws IOException {
85          printSlilp(OLEConstants.ITEM_STATUS_IN_TRANSIT_HOLD);
86      }
87  
88      @Test
89      public void printBillForInTransitForStaffTest() throws IOException {
90          printSlilp(OLEConstants.ITEM_STATUS_IN_TRANSIT_STAFF);
91      }
92  
93      @Test
94      public void printBillForInTransitForLoanTest() throws IOException {
95          printSlilp(OLEConstants.ITEM_STATUS_IN_TRANSIT_LOAN);
96      }
97  
98      @Test
99      public void printBillForHoldTest() throws IOException {
100         printSlilp(OLEConstants.ITEM_STATUS_ON_HOLD);
101     }
102 
103     @Test
104     public void printBillForLoan() throws IOException {
105         String itemStatus = "INTRANSIT-FOR-LOAN";
106         Mockito.when(mockItemSearch.getTitle()).thenReturn("Test");
107         Mockito.when(mockItemSearch.getCallNumber()).thenReturn("c-12");
108         Mockito.when(mockItemSearch.getCopyNumber()).thenReturn("c-123213");
109 
110         Mockito.when(mockOleItemRecordForCirc.getItemUUID()).thenReturn("123");
111         Mockito.when(mockOleItemRecordForCirc.getItemStatusToBeUpdatedTo()).thenReturn(itemStatus);
112         Mockito.when(mockItemRecord.getEffectiveDate()).thenReturn(new Timestamp(System.currentTimeMillis()));
113         Mockito.when(mockOleItemRecordForCirc.getItemRecord()).thenReturn(mockItemRecord);
114 
115         Mockito.when(mockItemInfoUtil.getOleItemRecordForCirc(mockItemRecord, mockOleCirculationDesk)).thenReturn(mockOleItemRecordForCirc);
116         Mockito.when(mockDocstoreUtil.getOleItemSearchList("123")).thenReturn(mockItemSearch);
117 
118 
119         Mockito.when(mockCheckinForm.getItemBarcode()).thenReturn("123");
120         OleRegularPrintSlipUtil inTransiForLoanPrintSlipUtil = new InTransitForLoanRegularPrintSlipUtil();
121         setOutputStream(inTransiForLoanPrintSlipUtil, itemStatus);
122         inTransiForLoanPrintSlipUtil.setDocstoreUtil(mockDocstoreUtil);
123         inTransiForLoanPrintSlipUtil.createPdfForPrintingSlip(mockOleItemRecordForCirc, mockResponse);
124     }
125 
126     private void printSlilp(String itemStatus) {
127         Mockito.when(mockItemSearch.getTitle()).thenReturn("Test");
128         Mockito.when(mockItemSearch.getCallNumber()).thenReturn("c-12");
129         Mockito.when(mockItemSearch.getCopyNumber()).thenReturn("c-123213");
130         Mockito.when(mockItemSearch.getVolumeNumber()).thenReturn("v");
131 
132         Mockito.when(mockOleItemRecordForCirc.getItemUUID()).thenReturn("123");
133         Mockito.when(mockOleItemRecordForCirc.getItemStatusToBeUpdatedTo()).thenReturn(itemStatus);
134         Mockito.when(mockOleItemRecordForCirc.getItemRecord()).thenReturn(mockItemRecord);
135         Mockito.when(mockOleItemRecordForCirc.getOleDeliverRequestBo()).thenReturn(mockOleDeliverRequestBo);
136         Mockito.when(mockOleItemRecordForCirc.getCheckinLocation()).thenReturn(mockOleCirculationDesk);
137 
138         Mockito.when(mockItemRecord.getEffectiveDate()).thenReturn(new Timestamp(System.currentTimeMillis()));
139         Mockito.when(mockItemRecord.getBarCode()).thenReturn("222222");
140 
141         Mockito.when(mockItemInfoUtil.getOleItemRecordForCirc(mockItemRecord, mockOleCirculationDesk)).thenReturn(mockOleItemRecordForCirc);
142 
143         Mockito.when(mockDocstoreUtil.getOleItemSearchList("123")).thenReturn(mockItemSearch);
144 
145         Mockito.when(mockOleCirculationDesk.getCirculationDeskPublicName()).thenReturn("BL Education");
146         Mockito.when(mockOleCirculationDesk.getOnHoldDays()).thenReturn("1");
147         Mockito.when(mockOleCirculationDesk.getHoldFormat()).thenReturn(OLEConstants.RECEIPT_PRINTER);
148         //Mockito.when(mockOleCirculationDesk.getHoldFormat()).thenReturn(OLEConstants.NORMAL_PRINTER);
149 
150         Mockito.when(mockOlePatron.getPatronName()).thenReturn("Adam,Smith");
151         Mockito.when(mockOleDeliverRequestBo.getOlePatron()).thenReturn(mockOlePatron);
152 
153         Mockito.when(mockOleDeliverRequestBo.getHoldExpirationDate()).thenReturn(new Date(System.currentTimeMillis()));
154 
155         Mockito.when(mockCheckinForm.getItemBarcode()).thenReturn("123");
156 
157 
158         if (itemStatus.equals(OLEConstants.ITEM_STATUS_IN_TRANSIT)) {
159             OlePrintSlipUtil inTransitOleRegularPrintSlipUtil = new InTransitRegularPrintSlipUtil();
160             inTransitOleRegularPrintSlipUtil.setDocstoreUtil(mockDocstoreUtil);
161             setOutputStream(inTransitOleRegularPrintSlipUtil, OLEConstants.ITEM_STATUS_IN_TRANSIT);
162             inTransitOleRegularPrintSlipUtil.createPdfForPrintingSlip(mockOleItemRecordForCirc, mockResponse);
163         } else if (itemStatus.equals(OLEConstants.ITEM_STATUS_IN_TRANSIT_HOLD)) {
164             OlePrintSlipUtil inTransitForHoldOleRegularPrintSlipUtil = new InTransitForHoldRegularPrintSlipUtil();
165             setOutputStream(inTransitForHoldOleRegularPrintSlipUtil, OLEConstants.ITEM_STATUS_IN_TRANSIT_HOLD);
166             inTransitForHoldOleRegularPrintSlipUtil.setDocstoreUtil(mockDocstoreUtil);
167             inTransitForHoldOleRegularPrintSlipUtil.createPdfForPrintingSlip(mockOleItemRecordForCirc, mockResponse);
168         } else if (itemStatus.equals(OLEConstants.ITEM_STATUS_IN_TRANSIT_STAFF)) {
169             OlePrintSlipUtil inTransitForStaffRegularPrintSlipUtil = new InTransitForStaffRegularPrintSlipUtil();
170             setOutputStream(inTransitForStaffRegularPrintSlipUtil, OLEConstants.ITEM_STATUS_IN_TRANSIT_STAFF);
171             inTransitForStaffRegularPrintSlipUtil.setDocstoreUtil(mockDocstoreUtil);
172             inTransitForStaffRegularPrintSlipUtil.createPdfForPrintingSlip(mockOleItemRecordForCirc, mockResponse);
173         } else if (itemStatus.equals(OLEConstants.ITEM_STATUS_IN_TRANSIT_LOAN)) {
174             OlePrintSlipUtil inTransitForLoanRegularPrintSlipUtil = new InTransitForLoanRegularPrintSlipUtil();
175             setOutputStream(inTransitForLoanRegularPrintSlipUtil, OLEConstants.ITEM_STATUS_IN_TRANSIT_LOAN);
176             inTransitForLoanRegularPrintSlipUtil.setDocstoreUtil(mockDocstoreUtil);
177             inTransitForLoanRegularPrintSlipUtil.createPdfForPrintingSlip(mockOleItemRecordForCirc, mockResponse);
178         } else if (itemStatus.equals(OLEConstants.ITEM_STATUS_ON_HOLD)) {
179             OlePrintSlipUtil olePrintSlipUtil = null;
180             if (mockOleCirculationDesk != null && mockOleCirculationDesk.getHoldFormat() != null && mockOleCirculationDesk.getHoldFormat().equals(OLEConstants.RECEIPT_PRINTER)) {
181                 olePrintSlipUtil = new OnHoldRecieptPrintSlipUtil();
182             } else {
183                 olePrintSlipUtil = new OnHoldRegularPrintSlipUtil();
184             }
185             setOutputStream(olePrintSlipUtil, OLEConstants.ITEM_STATUS_ON_HOLD);
186             olePrintSlipUtil.setDocstoreUtil(mockDocstoreUtil);
187             olePrintSlipUtil.createPdfForPrintingSlip(mockOleItemRecordForCirc, mockResponse);
188         }
189     }
190 
191     public void setOutputStream(OlePrintSlipUtil olePrintSlipUtil, String itemStatusToBeUpdatedTo) {
192         String tempLocation = System.getProperty("java.io.tmpdir");
193         System.out.println("Temp dir : " + tempLocation);
194         FileOutputStream fileOutputStream = null;
195         try {
196             fileOutputStream = new FileOutputStream(tempLocation+ "/"+itemStatusToBeUpdatedTo + ".pdf");
197         } catch (FileNotFoundException e) {
198             e.printStackTrace();
199         }
200         olePrintSlipUtil.setOutputStream(fileOutputStream);
201     }
202 
203     @Test
204     public void createPdfForPrintingSlipTest() throws Exception {
205         OleRegularPrintSlipUtil printSlipUtil = new InTransitRegularPrintSlipUtil();
206         printSlipUtil.setDocstoreUtil(mockDocstoreUtil);
207         Mockito.when(mockOleItemRecordForCirc.getItemUUID()).thenReturn("123");
208         Mockito.when(mockDocstoreUtil.getOleItemSearchList("123")).thenReturn(mockItemSearch);
209         Mockito.when(mockItemRecord.getEffectiveDate()).thenReturn(new Timestamp(System.currentTimeMillis()));
210         Mockito.when(mockOleItemRecordForCirc.getItemRecord()).thenReturn(mockItemRecord);
211 
212         String tempLocation = System.getProperty("java.io.tmpdir");
213         System.out.println("Temp dir : " + tempLocation);
214         FileOutputStream fileOutputStream = new FileOutputStream(tempLocation+ "/printSlip.pdf");
215         printSlipUtil.setOutputStream(fileOutputStream);
216         printSlipUtil.createPdfForPrintingSlip(mockOleItemRecordForCirc, mockResponse);
217     }
218 
219 
220     @Test
221     public void createPdfForEndSessionRegularPrintingSlipTest() throws Exception {
222         OleRegularPrintSlipUtil printSlipUtil = new OnHoldRegularPrintSlipUtil();
223         printSlipUtil.setDocstoreUtil(mockDocstoreUtil);Mockito.when(mockItemSearch.getTitle()).thenReturn("Test");
224         Mockito.when(mockItemSearch.getCallNumber()).thenReturn("c-12");
225         Mockito.when(mockItemSearch.getCopyNumber()).thenReturn("c-123213");
226         Mockito.when(mockOleItemRecordForCirc.getItemUUID()).thenReturn("123");
227         Mockito.when(mockDocstoreUtil.getOleItemSearchList("123")).thenReturn(mockItemSearch);
228         Mockito.when(mockItemRecord.getEffectiveDate()).thenReturn(new Timestamp(System.currentTimeMillis()));
229         Mockito.when(mockOleItemRecordForCirc.getItemRecord()).thenReturn(mockItemRecord);
230         Mockito.when(mockOleItemRecordForCirc.getItemStatusToBeUpdatedTo()).thenReturn(OLEConstants.ITEM_STATUS_ON_HOLD);
231         Mockito.when(mockOleItemRecordForCirc.getOleDeliverRequestBo()).thenReturn(mockOleDeliverRequestBo);
232         Mockito.when(mockOleItemRecordForCirc.getCheckinLocation()).thenReturn(mockOleCirculationDesk);
233         Mockito.when(mockItemRecord.getEffectiveDate()).thenReturn(new Timestamp(System.currentTimeMillis()));
234         Mockito.when(mockItemRecord.getBarCode()).thenReturn("222222");
235         Mockito.when(mockItemInfoUtil.getOleItemRecordForCirc(mockItemRecord, mockOleCirculationDesk)).thenReturn(mockOleItemRecordForCirc);
236         Mockito.when(mockDocstoreUtil.getOleItemSearchList("123")).thenReturn(mockItemSearch);
237         Mockito.when(mockOleCirculationDesk.getCirculationDeskPublicName()).thenReturn("BL Education");
238         Mockito.when(mockOleCirculationDesk.getOnHoldDays()).thenReturn("1");
239         Mockito.when(mockOleCirculationDesk.getHoldFormat()).thenReturn(OLEConstants.RECEIPT_PRINTER);
240         Mockito.when(mockOlePatron.getPatronName()).thenReturn("Adam,Smith");
241         Mockito.when(mockOleDeliverRequestBo.getOlePatron()).thenReturn(mockOlePatron);
242         Mockito.when(mockOleDeliverRequestBo.getHoldExpirationDate()).thenReturn(new Date(System.currentTimeMillis()));
243 
244         List<CheckedInItem> checkedInItems = new ArrayList<>();
245         for(int index = 1; index <= 5; index++){
246             Mockito.when(mockCheckedInItem.getItemForCircRecord()).thenReturn(mockOleItemRecordForCirc);
247             checkedInItems.add(mockCheckedInItem);
248         }
249 
250         String tempLocation = System.getProperty("java.io.tmpdir");
251         System.out.println("Temp dir : " + tempLocation);
252         FileOutputStream fileOutputStream = new FileOutputStream(tempLocation+ "/regularPrintSlipForEndSession.pdf");
253         printSlipUtil.setOutputStream(fileOutputStream);
254         printSlipUtil.createPdfForEndSessionPrintSlip(checkedInItems, mockResponse);
255     }
256 
257     @Test
258     public void createPdfForEndSessionReceiptPrintingSlipTest() throws Exception {
259         OlePrintSlipUtil printSlipUtil = new OnHoldRecieptPrintSlipUtil();
260         printSlipUtil.setDocstoreUtil(mockDocstoreUtil);Mockito.when(mockItemSearch.getTitle()).thenReturn("Test");
261         Mockito.when(mockItemSearch.getCallNumber()).thenReturn("c-12");
262         Mockito.when(mockItemSearch.getCopyNumber()).thenReturn("c-123213");
263         Mockito.when(mockOleItemRecordForCirc.getItemUUID()).thenReturn("123");
264         Mockito.when(mockDocstoreUtil.getOleItemSearchList("123")).thenReturn(mockItemSearch);
265         Mockito.when(mockItemRecord.getEffectiveDate()).thenReturn(new Timestamp(System.currentTimeMillis()));
266         Mockito.when(mockOleItemRecordForCirc.getItemRecord()).thenReturn(mockItemRecord);
267         Mockito.when(mockOleItemRecordForCirc.getItemStatusToBeUpdatedTo()).thenReturn(OLEConstants.ITEM_STATUS_ON_HOLD);
268         Mockito.when(mockOleItemRecordForCirc.getOleDeliverRequestBo()).thenReturn(mockOleDeliverRequestBo);
269         Mockito.when(mockOleItemRecordForCirc.getCheckinLocation()).thenReturn(mockOleCirculationDesk);
270         Mockito.when(mockItemRecord.getEffectiveDate()).thenReturn(new Timestamp(System.currentTimeMillis()));
271         Mockito.when(mockItemRecord.getBarCode()).thenReturn("222222");
272         Mockito.when(mockItemInfoUtil.getOleItemRecordForCirc(mockItemRecord, mockOleCirculationDesk)).thenReturn(mockOleItemRecordForCirc);
273         Mockito.when(mockDocstoreUtil.getOleItemSearchList("123")).thenReturn(mockItemSearch);
274         Mockito.when(mockOleCirculationDesk.getCirculationDeskPublicName()).thenReturn("BL Education");
275         Mockito.when(mockOleCirculationDesk.getOnHoldDays()).thenReturn("1");
276         Mockito.when(mockOleCirculationDesk.getHoldFormat()).thenReturn(OLEConstants.RECEIPT_PRINTER);
277         Mockito.when(mockOlePatron.getPatronName()).thenReturn("Adam,Smith");
278         Mockito.when(mockOleDeliverRequestBo.getOlePatron()).thenReturn(mockOlePatron);
279         Mockito.when(mockOleDeliverRequestBo.getHoldExpirationDate()).thenReturn(new Date(System.currentTimeMillis()));
280 
281         List<CheckedInItem> checkedInItems = new ArrayList<>();
282         for(int index = 1; index <= 5; index++){
283             Mockito.when(mockCheckedInItem.getItemForCircRecord()).thenReturn(mockOleItemRecordForCirc);
284             checkedInItems.add(mockCheckedInItem);
285         }
286 
287         String tempLocation = System.getProperty("java.io.tmpdir");
288         System.out.println("Temp dir : " + tempLocation);
289         FileOutputStream fileOutputStream = new FileOutputStream(tempLocation+ "/receiptPrintSlipForEndSession.pdf");
290         printSlipUtil.setOutputStream(fileOutputStream);
291         printSlipUtil.createPdfForEndSessionPrintSlip(checkedInItems, mockResponse);
292     }
293 
294 }