Search in sources :

Example 1 with DataStorageFile

use of com.epam.pipeline.entity.datastorage.DataStorageFile in project cloud-pipeline by epam.

the class NFSStorageProviderTest method testMoveDeleteFile.

@Test
public void testMoveDeleteFile() {
    NFSDataStorage dataStorage = new NFSDataStorage(0L, "testStorage", TEST_PATH + 3 + ":/test");
    nfsProvider.createStorage(dataStorage);
    String testFileName = "testFile.txt";
    String testFolderName = "testFolder";
    String testFolder2Name = "testFolder2";
    nfsProvider.createFile(dataStorage, testFileName, "testContent".getBytes());
    nfsProvider.createFolder(dataStorage, testFolderName);
    nfsProvider.createFolder(dataStorage, testFolder2Name);
    File dataStorageRoot = new File(testMountPoint, TEST_PATH + 3 + "/test");
    String newFilePath = testFolderName + "/" + testFileName;
    DataStorageFile file = nfsProvider.moveFile(dataStorage, testFileName, newFilePath);
    Assert.assertEquals(newFilePath, file.getPath());
    File oldFileLocation = new File(dataStorageRoot, testFileName);
    File newFileLocation = new File(dataStorageRoot, newFilePath);
    Assert.assertTrue(newFileLocation.exists());
    Assert.assertFalse(oldFileLocation.exists());
    String newFolder2Path = testFolderName + "/" + testFolder2Name;
    DataStorageFolder folder = nfsProvider.moveFolder(dataStorage, testFolder2Name, newFolder2Path);
    Assert.assertEquals(newFolder2Path, folder.getPath());
    File oldFolderLocation = new File(dataStorageRoot, testFolder2Name);
    File newFolderLocation = new File(dataStorageRoot, newFolder2Path);
    Assert.assertTrue(newFolderLocation.exists());
    Assert.assertFalse(oldFolderLocation.exists());
    nfsProvider.deleteFile(dataStorage, newFilePath, null, true);
    Assert.assertFalse(newFileLocation.exists());
    nfsProvider.deleteFolder(dataStorage, newFolder2Path, true);
    Assert.assertFalse(newFolderLocation.exists());
}
Also used : DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) NFSDataStorage(com.epam.pipeline.entity.datastorage.nfs.NFSDataStorage) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) DataStorageFolder(com.epam.pipeline.entity.datastorage.DataStorageFolder) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test)

Example 2 with DataStorageFile

use of com.epam.pipeline.entity.datastorage.DataStorageFile in project cloud-pipeline by epam.

the class NFSStorageProviderTest method testCreateFileFolderAndList.

@Test
public void testCreateFileFolderAndList() {
    NFSDataStorage dataStorage = new NFSDataStorage(0L, TEST_STORAGE_NAME, TEST_PATH + 2 + ":/test");
    nfsProvider.createStorage(dataStorage);
    String testFileName = "testFile.txt";
    nfsProvider.createFile(dataStorage, testFileName, "testContent".getBytes());
    File dataStorageRoot = new File(testMountPoint, TEST_PATH + 2 + "/test");
    File testFile = new File(dataStorageRoot, testFileName);
    Assert.assertTrue(testFile.exists());
    String testFolderName = "testFolder";
    nfsProvider.createFolder(dataStorage, testFolderName);
    File testFolder = new File(dataStorageRoot, testFolderName);
    Assert.assertTrue(testFolder.exists());
    DataStorageListing listing = nfsProvider.getItems(dataStorage, null, false, DEFAULT_PAGE_SIZE, null);
    Assert.assertFalse(listing.getResults().isEmpty());
    Optional<AbstractDataStorageItem> loadedFile = listing.getResults().stream().filter(i -> i.getType() == DataStorageItemType.File).findFirst();
    Assert.assertTrue(loadedFile.isPresent());
    Assert.assertEquals(testFileName, loadedFile.get().getName());
    Assert.assertEquals(testFileName, loadedFile.get().getPath());
    Assert.assertNotNull(((DataStorageFile) loadedFile.get()).getChanged());
    Optional<AbstractDataStorageItem> loadedFolder = listing.getResults().stream().filter(i -> i.getType() == DataStorageItemType.Folder).findFirst();
    Assert.assertTrue(loadedFolder.isPresent());
    Assert.assertEquals(testFolderName, loadedFolder.get().getName());
    Assert.assertEquals(testFolderName + "/", loadedFolder.get().getPath());
    Assert.assertNull(listing.getNextPageMarker());
    listing = nfsProvider.getItems(dataStorage, null, false, 1, null);
    Assert.assertEquals("2", listing.getNextPageMarker());
    listing = nfsProvider.getItems(dataStorage, null, false, 1, listing.getNextPageMarker());
    Assert.assertNull(listing.getNextPageMarker());
    Assert.assertFalse(listing.getResults().isEmpty());
}
Also used : DataStorageListing(com.epam.pipeline.entity.datastorage.DataStorageListing) BeforeClass(org.junit.BeforeClass) DataStorageDao(com.epam.pipeline.dao.datastorage.DataStorageDao) Mock(org.mockito.Mock) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Matchers.anyString(org.mockito.Matchers.anyString) DataStorageFolder(com.epam.pipeline.entity.datastorage.DataStorageFolder) MockitoAnnotations(org.mockito.MockitoAnnotations) Propagation(org.springframework.transaction.annotation.Propagation) DataStorageListing(com.epam.pipeline.entity.datastorage.DataStorageListing) Before(org.junit.Before) AfterClass(org.junit.AfterClass) DataStorageItemType(com.epam.pipeline.entity.datastorage.DataStorageItemType) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) File(java.io.File) NFSDataStorage(com.epam.pipeline.entity.datastorage.nfs.NFSDataStorage) AbstractDataStorageItem(com.epam.pipeline.entity.datastorage.AbstractDataStorageItem) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) Whitebox(org.mockito.internal.util.reflection.Whitebox) Optional(java.util.Optional) CmdExecutor(com.epam.pipeline.manager.CmdExecutor) Assert(org.junit.Assert) Transactional(org.springframework.transaction.annotation.Transactional) NFSDataStorage(com.epam.pipeline.entity.datastorage.nfs.NFSDataStorage) AbstractDataStorageItem(com.epam.pipeline.entity.datastorage.AbstractDataStorageItem) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test)

Example 3 with DataStorageFile

use of com.epam.pipeline.entity.datastorage.DataStorageFile in project cloud-pipeline by epam.

the class AttachmentFileManagerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    attachmentFileManager = new AttachmentFileManager(dataStorageManager, preferenceManager, attachmentManager, messageHelper, authManager);
    Preference systemDataStorage = SystemPreferences.DATA_STORAGE_SYSTEM_DATA_STORAGE_NAME.toPreference();
    systemDataStorage.setName(TEST_SYSTEM_DATA_STORAGE);
    when(preferenceManager.getPreference(SystemPreferences.DATA_STORAGE_SYSTEM_DATA_STORAGE_NAME)).thenReturn(TEST_SYSTEM_DATA_STORAGE);
    when(dataStorageManager.loadByNameOrId(TEST_SYSTEM_DATA_STORAGE)).thenReturn(testSystemDataStorage);
    when(dataStorageManager.createDataStorageFile(Mockito.eq(1L), Mockito.anyString(), Mockito.anyString(), Mockito.any(InputStream.class))).then((Answer<DataStorageFile>) invocation -> {
        String path = invocation.getArgumentAt(1, String.class);
        String name = invocation.getArgumentAt(2, String.class);
        DataStorageFile file = new DataStorageFile();
        file.setPath(path + "/" + name);
        return file;
    });
    when(attachmentManager.load(Mockito.anyLong())).thenAnswer(invocation -> {
        Attachment attachment = new Attachment();
        attachment.setId(invocation.getArgumentAt(0, Long.class));
        attachment.setName(TEST_ATTACHMENT_NAME);
        attachment.setPath(TEST_ATTACHMENT_PATH);
        return attachment;
    });
    DataStorageStreamingContent content = new DataStorageStreamingContent(new ByteArrayInputStream(new byte[] { 1 }), TEST_ATTACHMENT_NAME);
    when(dataStorageManager.getStreamingContent(testSystemDataStorage.getId(), TEST_ATTACHMENT_PATH, null)).thenReturn(content);
    when(authManager.getAuthorizedUser()).thenReturn(TEST_USER);
}
Also used : PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Mock(org.mockito.Mock) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Attachment(com.epam.pipeline.entity.issue.Attachment) Mockito.verify(org.mockito.Mockito.verify) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) Mockito(org.mockito.Mockito) MockitoAnnotations(org.mockito.MockitoAnnotations) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteArrayInputStream(java.io.ByteArrayInputStream) MessageHelper(com.epam.pipeline.common.MessageHelper) DataStorageStreamingContent(com.epam.pipeline.entity.datastorage.DataStorageStreamingContent) Matchers.eq(org.mockito.Matchers.eq) Preference(com.epam.pipeline.entity.preference.Preference) S3bucketDataStorage(com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage) AuthManager(com.epam.pipeline.manager.security.AuthManager) Assert(org.junit.Assert) DataStorageManager(com.epam.pipeline.manager.datastorage.DataStorageManager) InputStream(java.io.InputStream) Before(org.junit.Before) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) Preference(com.epam.pipeline.entity.preference.Preference) DataStorageStreamingContent(com.epam.pipeline.entity.datastorage.DataStorageStreamingContent) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(com.epam.pipeline.entity.issue.Attachment) Before(org.junit.Before)

Example 4 with DataStorageFile

use of com.epam.pipeline.entity.datastorage.DataStorageFile in project cloud-pipeline by epam.

the class NFSStorageProvider method getItems.

@Override
public DataStorageListing getItems(NFSDataStorage dataStorage, String path, Boolean showVersion, Integer pageSize, String marker) {
    File dataStorageRoot = mount(dataStorage);
    File dir = path != null ? new File(dataStorageRoot, path) : dataStorageRoot;
    long offset = StringUtils.isNumeric(marker) ? Long.parseLong(marker) : 1;
    try (Stream<Path> dirStream = Files.walk(dir.toPath(), 1)) {
        List<AbstractDataStorageItem> dataStorageItems = dirStream.sorted().skip(// First element is a directory itself
        offset).limit(pageSize).map(p -> {
            File file = p.toFile();
            AbstractDataStorageItem item;
            if (file.isDirectory()) {
                item = new DataStorageFolder();
            } else {
                // set size if it's a file
                DataStorageFile dataStorageFile = new DataStorageFile();
                dataStorageFile.setSize(file.length());
                dataStorageFile.setChanged(S3Constants.getAwsDateFormat().format(new Date(file.lastModified())));
                item = dataStorageFile;
            }
            item.setName(file.getName());
            item.setPath(dataStorageRoot.toURI().relativize(file.toURI()).getPath());
            return item;
        }).collect(Collectors.toList());
        DataStorageListing listing = new DataStorageListing();
        listing.setResults(dataStorageItems);
        Long nextOffset = offset + pageSize;
        try (Stream<Path> nextStream = Files.walk(dir.toPath(), 1)) {
            if (nextStream.skip(nextOffset).findFirst().isPresent()) {
                listing.setNextPageMarker(nextOffset.toString());
            }
        }
        return listing;
    } catch (IOException e) {
        throw new DataStorageException(e);
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) Date(java.util.Date) S3Constants(com.epam.pipeline.manager.datastorage.providers.aws.s3.S3Constants) LoggerFactory(org.slf4j.LoggerFactory) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) Matcher(java.util.regex.Matcher) MessageHelper(com.epam.pipeline.common.MessageHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) DataStorageItemContent(com.epam.pipeline.entity.datastorage.DataStorageItemContent) DataStorageListing(com.epam.pipeline.entity.datastorage.DataStorageListing) Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) DataStorageDownloadFileUrl(com.epam.pipeline.entity.datastorage.DataStorageDownloadFileUrl) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Stream(java.util.stream.Stream) CmdExecutionException(com.epam.pipeline.exception.CmdExecutionException) DataStorageStreamingContent(com.epam.pipeline.entity.datastorage.DataStorageStreamingContent) CmdExecutor(com.epam.pipeline.manager.CmdExecutor) Pattern(java.util.regex.Pattern) FileContentUtils(com.epam.pipeline.utils.FileContentUtils) DataStorageDao(com.epam.pipeline.dao.datastorage.DataStorageDao) MessageConstants(com.epam.pipeline.common.MessageConstants) DataStorageFolder(com.epam.pipeline.entity.datastorage.DataStorageFolder) BufferedOutputStream(java.io.BufferedOutputStream) Value(org.springframework.beans.factory.annotation.Value) Service(org.springframework.stereotype.Service) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Logger(org.slf4j.Logger) Files(java.nio.file.Files) SystemUtils(org.apache.commons.lang3.SystemUtils) FileOutputStream(java.io.FileOutputStream) AbstractDataStorage(com.epam.pipeline.entity.datastorage.AbstractDataStorage) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) DataStorageType(com.epam.pipeline.entity.datastorage.DataStorageType) FileInputStream(java.io.FileInputStream) NFSDataStorage(com.epam.pipeline.entity.datastorage.nfs.NFSDataStorage) File(java.io.File) AbstractDataStorageItem(com.epam.pipeline.entity.datastorage.AbstractDataStorageItem) Paths(java.nio.file.Paths) Collections(java.util.Collections) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) IOException(java.io.IOException) Date(java.util.Date) DataStorageFolder(com.epam.pipeline.entity.datastorage.DataStorageFolder) DataStorageListing(com.epam.pipeline.entity.datastorage.DataStorageListing) DataStorageException(com.epam.pipeline.entity.datastorage.DataStorageException) AbstractDataStorageItem(com.epam.pipeline.entity.datastorage.AbstractDataStorageItem) DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) File(java.io.File)

Example 5 with DataStorageFile

use of com.epam.pipeline.entity.datastorage.DataStorageFile in project cloud-pipeline by epam.

the class S3Helper method listVersions.

private DataStorageListing listVersions(AmazonS3 client, String bucket, String requestPath, Integer pageSize, String marker) {
    ListVersionsRequest request = new ListVersionsRequest().withBucketName(bucket).withPrefix(requestPath).withDelimiter(S3Constants.DELIMITER);
    if (StringUtils.hasValue(marker)) {
        request.setKeyMarker(marker);
    }
    if (pageSize != null) {
        request.setMaxResults(pageSize);
    }
    VersionListing versionListing;
    List<AbstractDataStorageItem> items = new ArrayList<>();
    Map<String, DataStorageFile> itemKeys = new HashMap<>();
    String previous = null;
    do {
        versionListing = client.listVersions(request);
        for (String commonPrefix : versionListing.getCommonPrefixes()) {
            if (checkListingSize(pageSize, items, itemKeys)) {
                items.addAll(itemKeys.values());
                return new DataStorageListing(previous, items);
            }
            previous = getPreviousKey(previous, commonPrefix);
            AbstractDataStorageItem folder = parseFolder(requestPath, commonPrefix);
            items.add(folder);
        }
        for (S3VersionSummary versionSummary : versionListing.getVersionSummaries()) {
            if (!pathMatch(requestPath, versionSummary.getKey())) {
                continue;
            }
            DataStorageFile file = AbstractS3ObjectWrapper.getWrapper(versionSummary).convertToStorageFile(requestPath);
            if (file == null) {
                continue;
            }
            if (!itemKeys.containsKey(file.getName())) {
                if (checkListingSize(pageSize, items, itemKeys)) {
                    items.addAll(itemKeys.values());
                    return new DataStorageListing(previous, items);
                }
                previous = getPreviousKey(previous, versionSummary.getKey());
                Map<String, AbstractDataStorageItem> versions = new LinkedHashMap<>();
                versions.put(file.getVersion(), file.copy(file));
                file.setVersions(versions);
                itemKeys.put(file.getName(), file);
            } else {
                DataStorageFile item = itemKeys.get(file.getName());
                Map<String, AbstractDataStorageItem> versions = item.getVersions();
                versions.put(file.getVersion(), file.copy(file));
                if (isLaterVersion(file.getChanged(), item.getChanged())) {
                    file.setVersions(versions);
                    itemKeys.put(file.getName(), file);
                }
            }
        }
        request.setKeyMarker(versionListing.getNextKeyMarker());
        request.setVersionIdMarker(versionListing.getNextVersionIdMarker());
    } while (versionListing.isTruncated());
    items.addAll(itemKeys.values());
    String returnToken = versionListing.isTruncated() ? previous : null;
    return new DataStorageListing(returnToken, items);
}
Also used : DataStorageFile(com.epam.pipeline.entity.datastorage.DataStorageFile) VersionListing(com.amazonaws.services.s3.model.VersionListing) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DataStorageListing(com.epam.pipeline.entity.datastorage.DataStorageListing) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) AbstractDataStorageItem(com.epam.pipeline.entity.datastorage.AbstractDataStorageItem) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest)

Aggregations

DataStorageFile (com.epam.pipeline.entity.datastorage.DataStorageFile)15 AbstractDataStorageItem (com.epam.pipeline.entity.datastorage.AbstractDataStorageItem)5 DataStorageListing (com.epam.pipeline.entity.datastorage.DataStorageListing)4 NFSDataStorage (com.epam.pipeline.entity.datastorage.nfs.NFSDataStorage)4 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)3 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)3 DataStorageFolder (com.epam.pipeline.entity.datastorage.DataStorageFolder)3 InputStream (java.io.InputStream)3 Matchers.anyString (org.mockito.Matchers.anyString)3 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)2 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)2 MessageHelper (com.epam.pipeline.common.MessageHelper)2 DataStorageDao (com.epam.pipeline.dao.datastorage.DataStorageDao)2 AbstractDataStorage (com.epam.pipeline.entity.datastorage.AbstractDataStorage)2 DataStorageException (com.epam.pipeline.entity.datastorage.DataStorageException)2 DataStorageStreamingContent (com.epam.pipeline.entity.datastorage.DataStorageStreamingContent)2 CmdExecutor (com.epam.pipeline.manager.CmdExecutor)2