Я не знаю, как смоделировать раздел, в котором я меняю владельца файла со строки Path path = newFile.toPath();
до конца.
Вот моя функция:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String uploadEndpoint(@RequestParam("file") MultipartFile file,
@RequestParam("usernameSession") String usernameSession,
@RequestHeader("current-folder") String folder) throws IOException {
String[] pathArray = file.getOriginalFilename().split("[\\\\\\/]");
String originalName = pathArray[pathArray.length-1];
LOGGER.info("Upload triggerred with : {} , filename : {}", originalName, file.getName());
String workingDir = URLDecoder.decode(folder.replace("!", "."),
StandardCharsets.UTF_8.name())
.replace("|", File.separator);
LOGGER.info("The file will be moved to : {}", workingDir);
File newFile = new File(workingDir + File.separator + originalName);
//UserPrincipal owner = Files.getOwner(newFile.toPath());
file.transferTo(newFile);
Path path = newFile.toPath();
FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
UserPrincipal owner = foav.getOwner();
System.out.format("Original owner of %s is %s%n", path, owner.getName());
FileSystem fs = FileSystems.getDefault();
UserPrincipalLookupService upls = fs.getUserPrincipalLookupService();
UserPrincipal newOwner = upls.lookupPrincipalByName(usernameSession);
foav.setOwner(newOwner);
UserPrincipal changedOwner = foav.getOwner();
System.out.format("New owner of %s is %s%n", path,
changedOwner.getName());
return "ok";
}
И воттест:
@Test
public void uploadEndpointTest() throws Exception {
PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(file);
Mockito.when(multipartFile.getOriginalFilename()).thenReturn("src/test/resources/download/test.txt");
assertEquals("ok", fileExplorerController.uploadEndpoint(multipartFile, "userName", "src/test/resources/download"));
}
Я получил исключение, потому что "userName" не является пользователем.Я хотел бы посмеяться над вызовом, где он ищет совпадения у пользователей Windows.Это работает, когда я устанавливаю имя пользователя моего окна вместо «userName», но я не могу разрешить имя пользователя моего окна.
Я пытался издеваться fs.getUserPrincipalLookupService()
;и upls.lookupPrincipalByName(usernameSession);
но я не знаю, что вернуться, чтобы высмеять вызов.
Большое спасибо!