1 package org.kuali.ole;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.kuali.ole.deliver.bo.OlePatronDocument;
6 import org.kuali.ole.deliver.bo.OlePatronIngestSummaryRecord;
7 import org.kuali.ole.ingest.FileUtil;
8 import org.kuali.ole.ingest.OlePatronXMLSchemaValidator;
9 import org.kuali.ole.service.OlePatronConverterService;
10 import org.kuali.rice.core.api.impex.xml.FileXmlDocCollection;
11 import org.kuali.rice.core.api.impex.xml.XmlDocCollection;
12 import org.mockito.Mock;
13 import org.mockito.Mockito;
14 import org.mockito.MockitoAnnotations;
15
16 import java.io.ByteArrayInputStream;
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.IOException;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26
27
28
29 public class PatronsIngesterServiceTest {
30
31 @Mock
32 private FileUtil mockFileUtil;
33
34 @Mock
35 private File mockFile;
36
37 @Mock
38 private OlePatronXMLSchemaValidator mockOlePatronXMLSchemaValidator;
39
40 @Mock
41 private OlePatronIngestSummaryRecord mockOlePatronIngestSummaryRecord;
42
43 private OlePatronConverterService mockOlePatronConverterService;
44
45 @Mock
46 ByteArrayInputStream mockByteArrayInputStream;
47
48 @Before
49 public void setUp() throws Exception {
50 mockOlePatronConverterService = new MockOlePatronConverterService();
51 MockitoAnnotations.initMocks(this);
52 }
53
54 @Test
55 public void ingest() throws Exception {
56 PatronsIngesterService patronsIngesterService = new MockPatronsIngesterService();
57 Mockito.when(mockFile.getName()).thenReturn("test-file");
58 String mockContent = "mockContent";
59 Mockito.when(mockFileUtil.readFile(mockFile)).thenReturn(mockContent);
60
61 Map validateResultMap = new HashMap();
62 validateResultMap.put(OLEConstants.OlePatron.PATRON_XML_ISVALID, true);
63 validateResultMap.put(OLEConstants.OlePatron.PATRON_POLLERSERVICE_ERROR_MESSAGE,null);
64 Mockito.when(mockOlePatronXMLSchemaValidator.validateContentsAgainstSchema(mockByteArrayInputStream)).thenReturn(validateResultMap);
65 ArrayList<OlePatronDocument> successOlePatronDocuments = new ArrayList<>();
66
67 patronsIngesterService.setFileUtil(mockFileUtil);
68 patronsIngesterService.setOlePatronConverterService(mockOlePatronConverterService);
69 patronsIngesterService.setOlePatronXMLSchemaValidator(mockOlePatronXMLSchemaValidator);
70
71 ArrayList<XmlDocCollection> xmlDocCollections = new ArrayList<>();
72 FileXmlDocCollection fileXmlDocCollection = null;
73 xmlDocCollections.add(fileXmlDocCollection);
74
75 patronsIngesterService.ingest(xmlDocCollections);
76 }
77
78 class MockPatronsIngesterService extends PatronsIngesterService {
79 @Override
80 protected ByteArrayInputStream getByteArrayInputStream(String fileContent) {
81 return mockByteArrayInputStream;
82 }
83
84 @Override
85 protected OlePatronIngestSummaryRecord getOlePatronIngestSummaryRecord() {
86 return mockOlePatronIngestSummaryRecord;
87 }
88 }
89
90 class MockOlePatronConverterService extends OlePatronConverterService {
91 private OlePatronIngestSummaryRecord olePatronIngestSummaryRecord;
92
93 @Override
94 public List<OlePatronDocument> persistPatronFromFileContent(String fileContent, boolean addUnMatchedPatronFlag, String fileName, OlePatronIngestSummaryRecord olePatronIngestSummaryRecord, String addressSource, String principalName) throws IOException, URISyntaxException {
95 this.olePatronIngestSummaryRecord = olePatronIngestSummaryRecord;
96 olePatronIngestSummaryRecord.setFailureRecords("filed records");
97 List<OlePatronDocument> olePatronDocuments = new ArrayList<>();
98 olePatronDocuments.add(new OlePatronDocument());
99 return olePatronDocuments;
100 }
101 }
102
103 }