public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) { Map<String, Object> results = new HashMap<String, Object>(); String documentPath = (String) workItem.getParameter("DocumentPath"); try { RequiredParameterValidator.validate(this.getClass(), workItem); Drive drive = auth.getDriveService(appName, clientSecret); Drive.Files.Get request = drive.files().get(documentPath); request.getMediaHttpDownloader().setProgressListener(new MediaDownloadProgressListener()); request.getMediaHttpDownloader().setDirectDownloadEnabled(true); InputStream docInputStream = request.executeMediaAsInputStream(); Document doc = new DocumentImpl(); String docBaseName = FilenameUtils.getBaseName(documentPath); String docExtension = FilenameUtils.getExtension(documentPath); doc.setName(docBaseName + "." + docExtension); doc.setIdentifier(documentPath); doc.setLastModified(new Date()); doc.setContent(IOUtils.toByteArray(docInputStream)); results.put(RESULTS_DOCUMENT, doc); workItemManager.completeWorkItem(workItem.getId(), results); } catch (Exception e) { handleException(e); } }
@Before public void setUp() { try { InputStream testInputStream = IOUtils.toInputStream("test doc content", "UTF-8"); MediaHttpUploader mediaHttpUploader = PowerMockito.mock(MediaHttpUploader.class); MediaHttpDownloader mediaHttpDownloader = PowerMockito.mock(MediaHttpDownloader.class); Drive.Files.Insert gdriveFilesInsert = PowerMockito.mock(Drive.Files.Insert.class); Drive.Files.Get gdriveFilesGet = PowerMockito.mock(Drive.Files.Get.class); when(auth.getDriveService(anyString(), anyString())).thenReturn(gdriveService); when(gdriveService.files()).thenReturn(gdriveFiles); when(gdriveFiles.insert(any(File.class), any(FileContent.class))).thenReturn(gdriveFilesInsert); when(gdriveFiles.get(anyString())).thenReturn(gdriveFilesGet); when(gdriveFilesInsert.getMediaHttpUploader()).thenReturn(mediaHttpUploader); when(gdriveFilesInsert.execute()).thenReturn(new File()); when(gdriveFilesGet.getMediaHttpDownloader()).thenReturn(mediaHttpDownloader); when(gdriveFilesGet.executeMediaAsInputStream()).thenReturn(testInputStream); } catch (Exception e) { fail(e.getMessage()); } }