001package org.kuali.ole.deliver.service;
002
003import org.junit.Before;
004import org.junit.Test;
005import org.kuali.ole.OLEConstants;
006import org.kuali.ole.OLEParameterConstants;
007import org.kuali.ole.deliver.bo.OleLoanDocument;
008import org.kuali.ole.deliver.bo.OlePatronDocument;
009import org.kuali.ole.deliver.processor.LoanProcessor;
010import org.kuali.ole.service.OlePatronHelperServiceImpl;
011import org.kuali.rice.kim.impl.identity.entity.EntityBo;
012import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
013import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
014import org.kuali.rice.krad.service.BusinessObjectService;
015import org.mockito.Mock;
016import org.mockito.Mockito;
017import org.mockito.MockitoAnnotations;
018
019import java.io.File;
020import java.text.SimpleDateFormat;
021import java.util.ArrayList;
022import java.util.Date;
023import java.util.Iterator;
024import java.util.List;
025
026import static org.junit.Assert.assertNotNull;
027
028/**
029 * Created by premkb on 4/8/15.
030 */
031public class NoticePDFContentFormatterTest {
032
033    @Mock
034    private OlePatronHelperServiceImpl mockOlePatronHelperServiceImpl;
035
036    @Mock
037    private LoanProcessor mockLoanProcessor;
038    @Mock
039    private BusinessObjectService mockBusinessObjectService;
040    @Mock
041    private ParameterValueResolver mockParameterResolverInstance;
042    @Mock
043    NoticesExecutor mockDeliverNoticesExecutor;
044    @Mock
045    private NoticePDFContentFormatter mockNoticePDFContentFormatter;
046
047    @Before
048    public void setUp() throws Exception {
049        MockitoAnnotations.initMocks(this);
050    }
051
052    @Test
053    public void testGenerateOverDueNoticeContent() throws Exception {
054        OleLoanDocument oleLoanDocument = new OleLoanDocument();
055        //oleLoanDocument.setItemId("113");
056        mockNoticePDFContentFormatter = new MockNoticePDFContentFormatter();
057
058        Mockito.when(mockParameterResolverInstance.getParameter(OLEConstants
059                .APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,OLEParameterConstants.OVERDUE_TITLE)).thenReturn("TITLE");
060        Mockito.when(mockParameterResolverInstance.getParameter(OLEConstants
061                .APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,OLEConstants.OleDeliverRequest.OVERDUE_NOTICE_CONTENT))
062                .thenReturn
063                        ("CONTENT");
064        Mockito.when(mockParameterResolverInstance.getParameter(OLEConstants
065                .APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLEParameterConstants.DEFAULT_CIRCULATION_DESK))
066                .thenReturn
067                        ("BL_EDUC");
068        Mockito.when(mockParameterResolverInstance.getParameter(OLEConstants
069                .APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLEParameterConstants.NOTICE_FROM_MAIL))
070                .thenReturn
071                        ("dev.ole@kuali.org");
072        Mockito.when(mockParameterResolverInstance.getParameter(OLEConstants.APPL_ID_OLE, OLEConstants
073                .DLVR_NMSPC, OLEConstants.DLVR_CMPNT,
074                "OVERDUE_NOTICE_TO_DATE"))
075                .thenReturn
076                        ("dd/MM/yyyy hh:mm a;");
077        List<OleLoanDocument> loanDocuments = new ArrayList<>();
078        mockDeliverNoticesExecutor = new OverdueNoticesExecutor(loanDocuments);
079
080        OlePatronDocument olePatron = new OlePatronDocument();
081        olePatron.setBarcode("123125");
082        EntityBo entity = new EntityBo();
083        ArrayList<EntityNameBo> entityNameBos = new ArrayList<EntityNameBo>();
084        EntityNameBo entityNameBo = new EntityNameBo();
085        entityNameBo.setFirstName("FirstName");
086        entityNameBos.add(entityNameBo);
087        entity.setNames(entityNameBos);
088        ArrayList<EntityTypeContactInfoBo> entityTypeContactInfos = new ArrayList<EntityTypeContactInfoBo>();
089        entityTypeContactInfos.add(new EntityTypeContactInfoBo());
090        entity.setEntityTypeContactInfos(entityTypeContactInfos);
091        olePatron.setEntity(entity);
092
093        oleLoanDocument.setOlePatron(olePatron);
094
095
096        mockNoticePDFContentFormatter.setOlePatronHelperService(mockOlePatronHelperServiceImpl);
097        mockNoticePDFContentFormatter.setSimpleDateFormat(new SimpleDateFormat());
098        mockNoticePDFContentFormatter.setUrlProperty("http://localhost:8080/olefs");
099        mockNoticePDFContentFormatter.setParameterResolverInstance(mockParameterResolverInstance);
100
101
102        String fileName = mockNoticePDFContentFormatter.generateOverDueNoticeContent(oleLoanDocument);
103        assertNotNull(fileName);
104    }
105
106    private class MockNoticePDFContentFormatter extends NoticePDFContentFormatter {
107        @Override
108        public String createPDFFile(String title, String itemId) throws Exception {
109            String pdfLocationSystemParam=System.getProperty("user.home")+"/kuali/main/"+System.getProperty("environment")+"/olefs-webapp/work/staging/";
110            boolean locationExist = new File(pdfLocationSystemParam).exists();
111            boolean fileCreated = false;
112            try {
113                if (!locationExist) {
114                    fileCreated = new File(pdfLocationSystemParam).mkdirs();
115                    if (!fileCreated) {
116                        throw new RuntimeException("Unable to create directory :" + pdfLocationSystemParam);
117                    }
118                }
119            } catch (Exception e) {
120                throw e;
121            }
122
123            if (title == null || title.trim().isEmpty()) {
124                title = OLEConstants.ITEM_TITLE;
125            }
126            title = title.replaceAll(" ", "_");
127
128            if (itemId == null || itemId.trim().isEmpty()) {
129                itemId = OLEConstants.ITEM_ID;
130            }
131            String fileName = pdfLocationSystemParam + title + "_" + itemId + "_" + (new Date(System.currentTimeMillis())).toString().replaceAll(":", "_") + ".pdf";
132            return fileName;
133        }
134    }
135}