Commit 4186e759 authored by nfcalabria's avatar nfcalabria
Browse files

Fixed PutFileControllerTest

parent 5aba6ce0
Loading
Loading
Loading
Loading
+10 −67
Original line number Original line Diff line number Diff line
@@ -80,7 +80,7 @@ public class PutFileControllerTest {
        when(fileDao.getRemainingQuota(any())).thenReturn(null);
        when(fileDao.getRemainingQuota(any())).thenReturn(null);


        String randomFileName = UUID.randomUUID().toString();
        String randomFileName = UUID.randomUUID().toString();
        createBaseFileInfo(randomFileName);
        FileInfo fileInfo = createBaseFileInfo(randomFileName);


        MockMultipartFile fakeFile = new MockMultipartFile("file", "test.txt", "text/plain", "content".getBytes());
        MockMultipartFile fakeFile = new MockMultipartFile("file", "test.txt", "text/plain", "content".getBytes());


@@ -89,71 +89,16 @@ public class PutFileControllerTest {
                .andDo(print())
                .andDo(print())
                .andExpect(status().isOk());
                .andExpect(status().isOk());


        File file = Path.of(getTestFilePath(randomFileName)).toFile();
        File file = Path.of(fileInfo.getFilePath()).toFile();


        assertTrue(file.exists());
        assertTrue(file.exists());
        assertEquals("content", Files.contentOf(file, StandardCharsets.UTF_8));
        assertEquals("content", Files.contentOf(file, StandardCharsets.UTF_8));


        // TODO: refactor test
        verify(fileDao, times(1)).updateFileAttributes(anyInt(), any(), eq("text/plain"), any(), eq(7l), eq("9A0364B9E99BB480DD25E1F0284C8555"));
        // verify(fileDao, times(1)).updateFileAttributes(anyInt(), eq("text/plain"), any(), eq(7l), eq("9A0364B9E99BB480DD25E1F0284C8555"));


        assertTrue(file.delete());
        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
    @Test
    public void putGenericFileWithJobId() throws Exception {
    public void putGenericFileWithJobId() throws Exception {
@@ -164,7 +109,7 @@ public class PutFileControllerTest {
        when(jobDAO.isJobExisting("pippo5")).thenReturn(true);
        when(jobDAO.isJobExisting("pippo5")).thenReturn(true);


        String randomFileName = UUID.randomUUID().toString();
        String randomFileName = UUID.randomUUID().toString();
        createBaseFileInfo(randomFileName);
        FileInfo fileInfo = createBaseFileInfo(randomFileName);


        MockMultipartFile fakeFile = new MockMultipartFile("file", "test.txt", "text/plain", "content".getBytes());
        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)).isJobExisting(eq("pippo5"));
        verify(jobDAO, times(1)).updateJobPhase(eq(ExecutionPhase.COMPLETED), any());
        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());
        assertTrue(file.exists());
        assertEquals("content", Files.contentOf(file, StandardCharsets.UTF_8));
        assertEquals("content", Files.contentOf(file, StandardCharsets.UTF_8));
        assertTrue(file.delete());
        assertTrue(file.delete());
    }
    }


    
    @Test
    @Test
    public void testPutFileWithoutNodeInDatabase() throws Exception {
    public void testPutFileWithoutNodeInDatabase() throws Exception {


@@ -203,6 +149,7 @@ public class PutFileControllerTest {
                .andExpect(status().isNotFound());
                .andExpect(status().isNotFound());
    }
    }


    
    @Test
    @Test
    public void testPutWithInputStream() throws Exception {
    public void testPutWithInputStream() throws Exception {


@@ -299,7 +246,7 @@ public class PutFileControllerTest {


    private FileInfo createBaseFileInfo(String fileName) {
    private FileInfo createBaseFileInfo(String fileName) {
        FileInfo fileInfo = new FileInfo();
        FileInfo fileInfo = new FileInfo();
        fileInfo.setOsPath(getTestFilePath(fileName));
        fileInfo.setActualBasePath(temporaryDirectory.getAbsolutePath());
        fileInfo.setVirtualPath("/path/to/" + fileName);
        fileInfo.setVirtualPath("/path/to/" + fileName);
        fileInfo.setPublic(false);
        fileInfo.setPublic(false);


@@ -309,10 +256,6 @@ public class PutFileControllerTest {
        return fileInfo;
        return fileInfo;
    }
    }


    private String getTestFilePath(String fileName) {
        return temporaryDirectory.toPath().resolve("subdir").resolve(fileName).toFile().getAbsolutePath();
    }

    private MockMultipartHttpServletRequestBuilder putMultipart(String uri) {
    private MockMultipartHttpServletRequestBuilder putMultipart(String uri) {
        MockMultipartHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(uri);
        MockMultipartHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(uri);
        builder.with(new RequestPostProcessor() {
        builder.with(new RequestPostProcessor() {