Exemplo n.º 1
0
        public async Task CanDeleteExistingFile()
        {
            var          config                = serviceProvider.GetRequiredService <IConfigurationStore>();
            var          dataOceanBaseUrl      = config.GetValueString("DATA_OCEAN_URL");
            var          dataOceanRootFolderId = config.GetValueString("DATA_OCEAN_ROOT_FOLDER_ID");
            const string folderName            = "unittest";
            var          expectedFolderResult  = new DataOceanDirectory {
                Id = Guid.NewGuid(), Name = folderName, ParentId = Guid.Parse(dataOceanRootFolderId)
            };
            var expectedFolderBrowseResult =
                new BrowseDirectoriesResult {
                Directories = new List <DataOceanDirectory> {
                    expectedFolderResult
                }
            };

            const string fileName           = "dummy.dxf";
            var          expectedFileResult = new DataOceanFile {
                Id = Guid.NewGuid(), Name = fileName, ParentId = expectedFolderResult.Id
            };
            var expectedFileBrowseResult = new BrowseFilesResult {
                Files = new List <DataOceanFile> {
                    expectedFileResult
                }
            };

            var browseFolderUrl = $"{dataOceanBaseUrl}/api/browse/keyset_directories?name={folderName}&owner=true&parent_id={dataOceanRootFolderId}";
            var browseFileUrl   = $"{dataOceanBaseUrl}/api/browse/keyset_files?name={fileName}&owner=true&parent_id={expectedFolderResult.Id}";
            var deleteFileUrl   = $"{dataOceanBaseUrl}/api/files/{expectedFileResult.Id}";

            var gracefulMock = new Mock <IWebRequest>();

            gracefulMock
            .Setup(g => g.ExecuteRequest <BrowseDirectoriesResult>(browseFolderUrl, null, null, HttpMethod.Get, null, 0, false))
            .Returns(Task.FromResult(expectedFolderBrowseResult));
            gracefulMock
            .Setup(g => g.ExecuteRequest <BrowseFilesResult>(browseFileUrl, null, null, HttpMethod.Get, null, 0, false))
            .Returns(Task.FromResult(expectedFileBrowseResult));
            gracefulMock
            .Setup(g => g.ExecuteRequest(deleteFileUrl, null, null, HttpMethod.Delete, null, 0, false))
            .Returns(Task.FromResult(HttpStatusCode.Accepted));

            serviceCollection.AddTransient(g => gracefulMock.Object);
            var serviceProvider2 = serviceCollection.BuildServiceProvider();
            var client           = serviceProvider2.GetRequiredService <IDataOceanClient>();
            var success          =
                await client.DeleteFile($"{DataOceanUtil.PathSeparator}{dataOceanRootFolderId}{DataOceanUtil.PathSeparator}{folderName}{DataOceanUtil.PathSeparator}{fileName}", null);

            Assert.True(success);
        }
Exemplo n.º 2
0
        public async Task CanGetNonExistingSingleFile()
        {
            const string folderName           = "unittest";
            var          expectedFolderResult = new DataOceanDirectory {
                Id = Guid.NewGuid(), Name = folderName
            };
            var expectedFolderBrowseResult =
                new BrowseDirectoriesResult {
                Directories = new List <DataOceanDirectory> {
                    expectedFolderResult
                }
            };

            const string fileName = "dummy.dxf";
            var          expectedFileBrowseResult = new BrowseFilesResult {
                Files = new List <DataOceanFile>()
            };

            var config           = serviceProvider.GetRequiredService <IConfigurationStore>();
            var dataOceanBaseUrl = config.GetValueString("DATA_OCEAN_URL");
            var browseFolderUrl  = $"{dataOceanBaseUrl}/api/browse/keyset_directories?name={folderName}&owner=true";
            var browseFileUrl    = $"{dataOceanBaseUrl}/api/browse/keyset_files?name={fileName}&owner=true&parent_id={expectedFolderResult.Id}";

            var gracefulMock = new Mock <IWebRequest>();

            gracefulMock
            .Setup(g => g.ExecuteRequest <BrowseDirectoriesResult>(browseFolderUrl, null, null, HttpMethod.Get, null, 0, false))
            .Returns(Task.FromResult(expectedFolderBrowseResult));
            gracefulMock
            .Setup(g => g.ExecuteRequest <BrowseFilesResult>(browseFileUrl, null, null, HttpMethod.Get, null, 0, false))
            .Returns(Task.FromResult(expectedFileBrowseResult));

            serviceCollection.AddTransient(g => gracefulMock.Object);
            var serviceProvider2 = serviceCollection.BuildServiceProvider();
            var client           = serviceProvider2.GetRequiredService <IDataOceanClient>();

            var resultStream = await client.GetFile($"{DataOceanUtil.PathSeparator}{folderName}{DataOceanUtil.PathSeparator}{fileName}", null);

            Assert.Null(resultStream);
        }
Exemplo n.º 3
0
        public async Task CanGetNonExistingMultiFile()
        {
            var fileName               = $"{DataOceanUtil.PathSeparator}tiles{DataOceanUtil.PathSeparator}15{DataOceanUtil.PathSeparator}18756{DataOceanUtil.PathSeparator}2834.png";
            var downloadUrl            = TestConstants.DownloadUrl;
            var substitutedDownloadUrl = downloadUrl.Replace("{path}", fileName.Substring(1));

            const string folderName           = "unittest";
            var          expectedFolderResult = new DataOceanDirectory {
                Id = Guid.NewGuid(), Name = folderName
            };
            var expectedFolderBrowseResult =
                new BrowseDirectoriesResult {
                Directories = new List <DataOceanDirectory> {
                    expectedFolderResult
                }
            };

            const string multiFileName      = "dummy.dxf_Tiles$";
            var          expectedFileResult = new DataOceanFile
            {
                Id                = Guid.NewGuid(),
                Name              = multiFileName,
                ParentId          = expectedFolderResult.Id,
                Multifile         = true,
                RegionPreferences = new List <string> {
                    "us1"
                },
                Status            = "AVAILABLE",
                DataOceanDownload = new DataOceanTransfer {
                    Url = downloadUrl
                }
            };
            var expectedFileBrowseResult = new BrowseFilesResult {
                Files = new List <DataOceanFile> {
                    expectedFileResult
                }
            };

            var config           = serviceProvider.GetRequiredService <IConfigurationStore>();
            var dataOceanBaseUrl = config.GetValueString("DATA_OCEAN_URL");
            var browseFolderUrl  = $"{dataOceanBaseUrl}/api/browse/keyset_directories?name={folderName}&owner=true";
            var browseFileUrl    = $"{dataOceanBaseUrl}/api/browse/keyset_files?name={multiFileName}&owner=true&parent_id={expectedFolderResult.Id}";

            var gracefulMock = new Mock <IWebRequest>();

            gracefulMock
            .Setup(g => g.ExecuteRequest <BrowseDirectoriesResult>(browseFolderUrl, null, null, HttpMethod.Get, null, 0, false))
            .Returns(Task.FromResult(expectedFolderBrowseResult));
            gracefulMock
            .Setup(g => g.ExecuteRequest <BrowseFilesResult>(browseFileUrl, null, null, HttpMethod.Get, null, 0, false))
            .Returns(Task.FromResult(expectedFileBrowseResult));
            gracefulMock
            .Setup(g => g.ExecuteRequestAsStreamContent(substitutedDownloadUrl, HttpMethod.Get, null, null, null, 0, false))
            .ReturnsAsync((HttpContent)null);

            serviceCollection.AddTransient(g => gracefulMock.Object);
            var serviceProvider2 = serviceCollection.BuildServiceProvider();
            var client           = serviceProvider2.GetRequiredService <IDataOceanClient>();

            var fullFileName = $"{DataOceanUtil.PathSeparator}{folderName}{DataOceanUtil.PathSeparator}{multiFileName}{fileName}";
            var resultStream = await client.GetFile(fullFileName, null);

            Assert.Null(resultStream);
        }
Exemplo n.º 4
0
        public async Task CanGetExistingMultiFile()
        {
            var fileName               = $"{DataOceanUtil.PathSeparator}tiles{DataOceanUtil.PathSeparator}tiles.json";
            var downloadUrl            = TestConstants.DownloadUrl;
            var substitutedDownloadUrl = downloadUrl.Replace("{path}", fileName.Substring(1));
            var expectedResult         = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3 };
            var stream = new MemoryStream(expectedResult);
            var expectedDownloadResult = new StreamContent(stream);

            var          config                = serviceProvider.GetRequiredService <IConfigurationStore>();
            var          dataOceanBaseUrl      = config.GetValueString("DATA_OCEAN_URL");
            var          dataOceanRootFolderId = config.GetValueString("DATA_OCEAN_ROOT_FOLDER_ID");
            const string folderName            = "unittest";
            var          expectedFolderResult  = new DataOceanDirectory {
                Id = Guid.NewGuid(), Name = folderName, ParentId = Guid.Parse(dataOceanRootFolderId)
            };
            var expectedFolderBrowseResult =
                new BrowseDirectoriesResult {
                Directories = new List <DataOceanDirectory> {
                    expectedFolderResult
                }
            };

            const string multiFileName      = "dummy.dxf_Tiles$";
            var          fileUid            = Guid.NewGuid();
            var          updatedAt          = DateTime.UtcNow.AddHours(-2);
            var          expectedFileResult = new DataOceanFile
            {
                Id                = fileUid,
                Name              = multiFileName,
                ParentId          = expectedFolderResult.Id,
                Multifile         = true,
                RegionPreferences = new List <string> {
                    "us1"
                },
                Status            = "AVAILABLE",
                DataOceanDownload = new DataOceanTransfer {
                    Url = downloadUrl
                },
                UpdatedAt = updatedAt
            };
            var otherFileResult = new DataOceanFile
            {
                Id                = fileUid,
                Name              = multiFileName,
                ParentId          = expectedFolderResult.Id,
                Multifile         = true,
                RegionPreferences = new List <string> {
                    "us1"
                },
                Status            = "AVAILABLE",
                DataOceanDownload = new DataOceanTransfer {
                    Url = downloadUrl
                },
                UpdatedAt = updatedAt.AddHours(-5)
            };
            var expectedFileBrowseResult = new BrowseFilesResult {
                Files = new List <DataOceanFile> {
                    expectedFileResult, otherFileResult
                }
            };

            var browseFolderUrl = $"{dataOceanBaseUrl}/api/browse/keyset_directories?name={folderName}&owner=true&parent_id={dataOceanRootFolderId}";
            var browseFileUrl   = $"{dataOceanBaseUrl}/api/browse/keyset_files?name={multiFileName}&owner=true&parent_id={expectedFolderResult.Id}";

            var gracefulMock = new Mock <IWebRequest>();

            gracefulMock
            .Setup(g => g.ExecuteRequest <BrowseDirectoriesResult>(browseFolderUrl, null, null, HttpMethod.Get, null, 0, false))
            .Returns(Task.FromResult(expectedFolderBrowseResult));
            gracefulMock
            .Setup(g => g.ExecuteRequest <BrowseFilesResult>(browseFileUrl, null, null, HttpMethod.Get, null, 0, false))
            .Returns(Task.FromResult(expectedFileBrowseResult));
            gracefulMock
            .Setup(g => g.ExecuteRequestAsStreamContent(substitutedDownloadUrl, HttpMethod.Get, null, null, null, 0, false))
            .ReturnsAsync(expectedDownloadResult);

            serviceCollection.AddTransient(g => gracefulMock.Object);
            var serviceProvider2 = serviceCollection.BuildServiceProvider();
            var client           = serviceProvider2.GetRequiredService <IDataOceanClient>();

            var fullFileName = $"{DataOceanUtil.PathSeparator}{dataOceanRootFolderId}{DataOceanUtil.PathSeparator}{folderName}{DataOceanUtil.PathSeparator}{multiFileName}{fileName}";
            var resultStream = await client.GetFile(fullFileName, null);

            using (var ms = new MemoryStream())
            {
                resultStream.CopyTo(ms);
                var result = ms.ToArray();
                Assert.Equal(expectedResult, result);
            }
        }