1 package org.kuali.ole.ingest;
2
3 import org.apache.commons.io.output.ByteArrayOutputStream;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.kuali.ole.OLEConstants;
8 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.InstanceCollection;
9 import org.kuali.ole.editor.contoller.InstanceEditorController;
10 import org.kuali.ole.editor.form.InstanceEditorForm;
11 import org.kuali.ole.ingest.FileUtil;
12 import org.kuali.ole.ingest.OlePatronRecordHandler;
13 import org.kuali.ole.ingest.controller.OlePatronRecordController;
14 import org.kuali.ole.ingest.form.OlePatronRecordForm;
15 import org.kuali.ole.ingest.pojo.OlePatron;
16 import org.kuali.ole.ingest.pojo.OlePatronGroup;
17 import org.kuali.ole.patron.constant.OLEPatronConstant;
18 import org.kuali.ole.service.OlePatronConverterService;
19 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20 import org.kuali.rice.krad.web.form.UifFormBase;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.springframework.test.context.ContextConfiguration;
25 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26 import org.springframework.test.context.transaction.TransactionConfiguration;
27 import org.springframework.transaction.annotation.Transactional;
28 import org.springframework.validation.BindingResult;
29 import org.springframework.web.multipart.MultipartFile;
30 import org.springframework.web.servlet.ModelAndView;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import java.io.ByteArrayInputStream;
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.URL;
40 import java.util.List;
41
42 import static junit.framework.Assert.assertFalse;
43 import static junit.framework.Assert.assertNotNull;
44 import static junit.framework.Assert.assertTrue;
45
46
47
48
49
50
51
52
53
54 @ContextConfiguration(locations = {"classpath:/SpringBeans.xml"})
55 @RunWith(value = SpringJUnit4ClassRunner.class)
56 @TransactionConfiguration(defaultRollback = true)
57 public class OlePatronRecordController_UT {
58
59 @Mock
60 private OlePatronRecordForm mockUifFormBase;
61 @Mock
62 private BindingResult mockBindingResult;
63 @Mock
64 private HttpServletRequest mockRequest;
65 @Mock
66 private HttpServletResponse mockResponse;
67 @Mock
68 private ModelAndView mockModelView;
69
70 public MockOlePatronRecordController mockOlePatronRecordController;
71 @Before
72 public void setUp() throws Exception {
73
74 MockitoAnnotations.initMocks(this);
75 mockOlePatronRecordController = new MockOlePatronRecordController();
76
77 }
78 public String patronRecordContent ;
79
80 @Test
81 @Transactional
82 public void testUpload() throws Exception {
83 OlePatronConverterService olePatronRecordService = GlobalResourceLoader.getService(OLEConstants.PATRON_CONVERTER_SERVICE);
84 OlePatronRecordHandler olePatronRecordHandler = new OlePatronRecordHandler();
85 URL resource = getClass().getResource(OLEPatronConstant.PATRON_FILE_NAME);
86 File file = new File(resource.toURI());
87 patronRecordContent = new FileUtil().readFile(file);
88 OlePatronGroup olePatron = olePatronRecordHandler.buildPatronFromFileContent(patronRecordContent);
89 MockMultiPartFile mockMultiPartFile = new MockMultiPartFile();
90 Mockito.when(mockUifFormBase.getPatronFile()).thenReturn((mockMultiPartFile));
91 Mockito.when(mockUifFormBase.isAddUnmatchedPatron()).thenReturn(true);
92 MockOlePatronRecordController mockOlePatronRecordController = new MockOlePatronRecordController();
93 ModelAndView modelAndView =
94 mockOlePatronRecordController.upload(mockUifFormBase, mockBindingResult, mockRequest, mockResponse);
95
96 assertNotNull(modelAndView);
97 }
98
99 @Test
100 @Transactional
101 public void testValidateFile() throws Exception {
102 assertTrue(mockOlePatronRecordController.validateFile(OLEPatronConstant.PATRON_FILE_NAME));
103 }
104
105 private class MockOlePatronRecordController extends OlePatronRecordController {
106
107 }
108
109 public class MockMultiPartFile implements MultipartFile {
110
111 @Override
112 public String getName() {
113 return OLEPatronConstant.PATRON_MULTIPART_FIELD_NAME;
114 }
115
116 @Override
117 public String getOriginalFilename() {
118 return OLEPatronConstant.PATRON_FILE_NAME;
119 }
120
121 @Override
122 public String getContentType() {
123 return OLEPatronConstant.PATRON_MULTIPART_CONTENT_TYPE;
124 }
125
126 @Override
127 public boolean isEmpty() {
128 return false;
129 }
130
131 @Override
132 public long getSize() {
133 return 0;
134 }
135
136 @Override
137 public byte[] getBytes() throws IOException {
138 return patronRecordContent.getBytes();
139 }
140
141 @Override
142 public InputStream getInputStream() throws IOException {
143
144 return new ByteArrayInputStream(patronRecordContent.getBytes());
145 }
146
147 @Override
148 public void transferTo(File file) throws IOException, IllegalStateException {
149
150
151 }
152 }
153
154 }