@Test void testUploadMtaFile() throws Exception { String fileName = "test.mtar"; FileEntry fileEntry = createFileEntry(fileName); Mockito.when(servletFileUpload.getItemIterator(Mockito.eq(request))) .thenReturn(fileItemIterator); Mockito.when(fileItemIterator.hasNext()) .thenReturn(true, true, false); Mockito.when(fileItemIterator.next()) .thenReturn(fileItemStream); Mockito.when(fileItemStream.isFormField()) .thenReturn(false, true); Mockito.when(fileItemStream.openStream()) .thenReturn(Mockito.mock(InputStream.class)); Mockito.when(fileItemStream.getName()) .thenReturn(fileName); Mockito.when(fileService.addFile(Mockito.eq(SPACE_GUID), Mockito.eq(NAMESPACE_GUID), Mockito.eq(fileName), Mockito.any(InputStream.class))) .thenReturn(fileEntry); ResponseEntity<FileMetadata> response = testedClass.uploadFile(request, SPACE_GUID, NAMESPACE_GUID); Mockito.verify(servletFileUpload) .setSizeMax(Mockito.eq(new Configuration().getMaxUploadSize())); Mockito.verify(fileItemIterator, Mockito.times(3)) .hasNext(); Mockito.verify(fileItemStream) .openStream(); Mockito.verify(fileService) .addFile(Mockito.eq(SPACE_GUID), Mockito.eq(NAMESPACE_GUID), Mockito.eq(fileName), Mockito.any(InputStream.class)); FileMetadata fileMetadata = response.getBody(); assertMetadataMatches(fileEntry, fileMetadata); }
@Override public ResponseEntity<FileMetadata> uploadFile(HttpServletRequest request, String spaceGuid, String namespace) { try { StopWatch stopWatch = StopWatch.createStarted(); LOGGER.trace("Received upload request on URI: {}", ServletUtil.decodeUri(request)); FileEntry fileEntry = uploadFiles(request, spaceGuid, namespace).get(0); FileMetadata file = parseFileEntry(fileEntry); AuditLoggingProvider.getFacade() .logConfigCreate(file); stopWatch.stop(); LOGGER.trace("Uploaded file \"{}\" with name {}, size {} and digest {} (algorithm {}) for {} ms.", file.getId(), file.getName(), file.getSize(), file.getDigest(), file.getDigestAlgorithm(), stopWatch.getTime()); return ResponseEntity.status(HttpStatus.CREATED) .body(file); } catch (FileUploadException | IOException | FileStorageException e) { throw new SLException(e, Messages.COULD_NOT_UPLOAD_FILE_0, e.getMessage()); } }
FilesApiServiceImpl implements FilesApiService { @Override public ResponseEntity<FileMetadata> uploadFile(HttpServletRequest request, String spaceGuid, String namespace) { try { StopWatch stopWatch = StopWatch.createStarted(); LOGGER.trace("Received upload request on URI: {}", ServletUtil.decodeUri(request)); FileEntry fileEntry = uploadFiles(request, spaceGuid, namespace).get(0); FileMetadata file = parseFileEntry(fileEntry); AuditLoggingProvider.getFacade() .logConfigCreate(file); stopWatch.stop(); LOGGER.trace("Uploaded file \"{}\" with name {}, size {} and digest {} (algorithm {}) for {} ms.", file.getId(), file.getName(), file.getSize(), file.getDigest(), file.getDigestAlgorithm(), stopWatch.getTime()); return ResponseEntity.status(HttpStatus.CREATED) .body(file); } catch (FileUploadException | IOException | FileStorageException e) { throw new SLException(e, Messages.COULD_NOT_UPLOAD_FILE_0, e.getMessage()); } } }
FilesApiServiceImpl implements FilesApiService { @Override public ResponseEntity<FileMetadata> uploadFile(HttpServletRequest request, String spaceGuid, String namespace) { try { StopWatch stopWatch = StopWatch.createStarted(); LOGGER.trace("Received upload request on URI: {}", ServletUtil.decodeUri(request)); FileEntry fileEntry = uploadFiles(request, spaceGuid, namespace).get(0); FileMetadata file = parseFileEntry(fileEntry); AuditLoggingProvider.getFacade() .logConfigCreate(file); stopWatch.stop(); LOGGER.trace("Uploaded file \"{}\" with name {}, size {} and digest {} (algorithm {}) for {} ms.", file.getId(), file.getName(), file.getSize(), file.getDigest(), file.getDigestAlgorithm(), stopWatch.getTime()); return ResponseEntity.status(HttpStatus.CREATED) .body(file); } catch (FileUploadException | IOException | FileStorageException e) { throw new SLException(e, Messages.COULD_NOT_UPLOAD_FILE_0, e.getMessage()); } }  }
FilesApiServiceImpl implements FilesApiService { @Override public ResponseEntity<FileMetadata> uploadFile(HttpServletRequest request, String spaceGuid, String namespace) { try { StopWatch stopWatch = StopWatch.createStarted(); LOGGER.trace("Received upload request on URI: {}", ServletUtil.decodeUri(request)); FileEntry fileEntry = uploadFiles(request, spaceGuid, namespace).get(0); FileMetadata file = parseFileEntry(fileEntry); AuditLoggingProvider.getFacade() .logConfigCreate(file); stopWatch.stop(); LOGGER.trace("Uploaded file \"{}\" with name {}, size {} and digest {} (algorithm {}) for {} ms.", file.getId(), file.getName(), file.getSize(), file.getDigest(), file.getDigestAlgorithm(), stopWatch.getTime()); return ResponseEntity.status(HttpStatus.CREATED) .body(file); } catch (FileUploadException | IOException | FileStorageException e) { throw new SLException(e, Messages.COULD_NOT_UPLOAD_FILE_0, e.getMessage()); } }  @Override ResponseEntity<List<FileMetadata>> getFiles(String spaceGuid, String namespace); @Override ResponseEntity<FileMetadata> uploadFile(HttpServletRequest request, String spaceGuid, String namespace); }
FilesApiServiceImpl implements FilesApiService { @Override public ResponseEntity<FileMetadata> uploadFile(HttpServletRequest request, String spaceGuid, String namespace) { try { StopWatch stopWatch = StopWatch.createStarted(); LOGGER.trace("Received upload request on URI: {}", ServletUtil.decodeUri(request)); FileEntry fileEntry = uploadFiles(request, spaceGuid, namespace).get(0); FileMetadata file = parseFileEntry(fileEntry); AuditLoggingProvider.getFacade() .logConfigCreate(file); stopWatch.stop(); LOGGER.trace("Uploaded file \"{}\" with name {}, size {} and digest {} (algorithm {}) for {} ms.", file.getId(), file.getName(), file.getSize(), file.getDigest(), file.getDigestAlgorithm(), stopWatch.getTime()); return ResponseEntity.status(HttpStatus.CREATED) .body(file); } catch (FileUploadException | IOException | FileStorageException e) { throw new SLException(e, Messages.COULD_NOT_UPLOAD_FILE_0, e.getMessage()); } }  @Override ResponseEntity<List<FileMetadata>> getFiles(String spaceGuid, String namespace); @Override ResponseEntity<FileMetadata> uploadFile(HttpServletRequest request, String spaceGuid, String namespace);  }