diff --git a/src/test/java/it/inaf/ia2/transfer/controller/PutFileControllerTest.java b/src/test/java/it/inaf/ia2/transfer/controller/PutFileControllerTest.java index 94363c8552de6961205bddd9fc0c406d0a71a93b..58dbe07fd29b009311247f182a63d9367e26f30f 100644 --- a/src/test/java/it/inaf/ia2/transfer/controller/PutFileControllerTest.java +++ b/src/test/java/it/inaf/ia2/transfer/controller/PutFileControllerTest.java @@ -80,7 +80,7 @@ public class PutFileControllerTest { when(fileDao.getRemainingQuota(any())).thenReturn(null); String randomFileName = UUID.randomUUID().toString(); - createBaseFileInfo(randomFileName); + FileInfo fileInfo = createBaseFileInfo(randomFileName); MockMultipartFile fakeFile = new MockMultipartFile("file", "test.txt", "text/plain", "content".getBytes()); @@ -89,72 +89,17 @@ public class PutFileControllerTest { .andDo(print()) .andExpect(status().isOk()); - File file = Path.of(getTestFilePath(randomFileName)).toFile(); + File file = Path.of(fileInfo.getFilePath()).toFile(); assertTrue(file.exists()); assertEquals("content", Files.contentOf(file, StandardCharsets.UTF_8)); - // TODO: refactor test - // verify(fileDao, times(1)).updateFileAttributes(anyInt(), eq("text/plain"), any(), eq(7l), eq("9A0364B9E99BB480DD25E1F0284C8555")); + verify(fileDao, times(1)).updateFileAttributes(anyInt(), any(), eq("text/plain"), any(), eq(7l), eq("9A0364B9E99BB480DD25E1F0284C8555")); assertTrue(file.delete()); } - @Test - public void putGenericFileWithNameConflictExtension() throws Exception { - putGenericFileWithNameConflict("test.txt", "test-1.txt", "test-2.txt"); - } - - @Test - public void putGenericFileWithNameConflictNoExtension() throws Exception { - putGenericFileWithNameConflict("test", "test-1", "test-2"); - } - - private void putGenericFileWithNameConflict(String name1, String name2, String name3) throws Exception { - - when(fileDao.getRemainingQuota(any())).thenReturn(null); - - createBaseFileInfo(name1); - - MockMultipartFile fakeFile = new MockMultipartFile("file", "test.txt", "text/plain", "content".getBytes()); - - mockMvc.perform(putMultipart("/path/to/test.txt") - .file(fakeFile)) - .andDo(print()) - .andExpect(status().isOk()); - - File file = Path.of(getTestFilePath(name1)).toFile(); - - assertTrue(file.exists()); - assertEquals("content", Files.contentOf(file, StandardCharsets.UTF_8)); - - MockMultipartFile fakeFile2 = new MockMultipartFile("file", "test.txt", "text/plain", "content2".getBytes()); - - mockMvc.perform(putMultipart("/path/to/test.txt") - .file(fakeFile2)) - .andDo(print()) - .andExpect(status().isOk()); - - File file2 = Path.of(getTestFilePath(name2)).toFile(); - assertTrue(file2.exists()); - assertEquals("content2", Files.contentOf(file2, StandardCharsets.UTF_8)); - - MockMultipartFile fakeFile3 = new MockMultipartFile("file", "test.txt", "text/plain", "content3".getBytes()); - - mockMvc.perform(putMultipart("/path/to/test.txt") - .file(fakeFile3)) - .andDo(print()) - .andExpect(status().isOk()); - - File file3 = Path.of(getTestFilePath(name3)).toFile(); - assertTrue(file3.exists()); - assertEquals("content3", Files.contentOf(file3, StandardCharsets.UTF_8)); - - assertTrue(file.delete()); - assertTrue(file2.delete()); - assertTrue(file3.delete()); - } - + @Test public void putGenericFileWithJobId() throws Exception { @@ -164,7 +109,7 @@ public class PutFileControllerTest { when(jobDAO.isJobExisting("pippo5")).thenReturn(true); String randomFileName = UUID.randomUUID().toString(); - createBaseFileInfo(randomFileName); + FileInfo fileInfo = createBaseFileInfo(randomFileName); MockMultipartFile fakeFile = new MockMultipartFile("file", "test.txt", "text/plain", "content".getBytes()); @@ -185,13 +130,14 @@ public class PutFileControllerTest { verify(jobDAO, times(1)).isJobExisting(eq("pippo5")); verify(jobDAO, times(1)).updateJobPhase(eq(ExecutionPhase.COMPLETED), any()); - File file = Path.of(getTestFilePath(randomFileName)).toFile(); + File file = Path.of(fileInfo.getFilePath()).toFile(); assertTrue(file.exists()); assertEquals("content", Files.contentOf(file, StandardCharsets.UTF_8)); assertTrue(file.delete()); } + @Test public void testPutFileWithoutNodeInDatabase() throws Exception { @@ -203,6 +149,7 @@ public class PutFileControllerTest { .andExpect(status().isNotFound()); } + @Test public void testPutWithInputStream() throws Exception { @@ -212,7 +159,7 @@ public class PutFileControllerTest { .andDo(print()) .andExpect(status().isOk()); } - + @Test public void testJobError() throws Exception { @@ -299,7 +246,7 @@ public class PutFileControllerTest { private FileInfo createBaseFileInfo(String fileName) { FileInfo fileInfo = new FileInfo(); - fileInfo.setOsPath(getTestFilePath(fileName)); + fileInfo.setActualBasePath(temporaryDirectory.getAbsolutePath()); fileInfo.setVirtualPath("/path/to/" + fileName); fileInfo.setPublic(false); @@ -309,10 +256,6 @@ public class PutFileControllerTest { return fileInfo; } - private String getTestFilePath(String fileName) { - return temporaryDirectory.toPath().resolve("subdir").resolve(fileName).toFile().getAbsolutePath(); - } - private MockMultipartHttpServletRequestBuilder putMultipart(String uri) { MockMultipartHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(uri); builder.with(new RequestPostProcessor() {