package com.njcn.gather.tool.parsepqdif.component;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.util.ReflectionTestUtils;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PqdifFileStorageServiceTest {

    @Test
    public void saveUsesPqdifFilesDirectoryUnderFilesRoot() throws Exception {
        Path filesRoot = Files.createTempDirectory("files-root");
        PqdifFileStorageService service = new PqdifFileStorageService();
        ReflectionTestUtils.setField(service, "filesPath", filesRoot.toString());

        String storedPath = service.save(new MockMultipartFile("pqdifFile", "sample.pqd",
                "application/octet-stream", "content".getBytes("UTF-8")));

        Path expectedRoot = filesRoot.resolve("pqdif-files").toAbsolutePath().normalize();
        Path actualPath = Paths.get(storedPath).toAbsolutePath().normalize();
        Assert.assertTrue(actualPath.startsWith(expectedRoot));
        Assert.assertTrue(Files.exists(actualPath));
    }
}
