From f174358973caefc9262df480208341fd8233dc7f Mon Sep 17 00:00:00 2001 From: Mateusz Woda Date: Wed, 11 May 2022 16:59:06 +0200 Subject: [PATCH] feat: add `version_number` to `BoxFileVersion` (#820) --- .../BoxFilesManagerIntegrationTest.cs | 17 ++++++++++ .../Configuration/Commands/ICommand.cs | 13 +++++++ .../CreateNewFileVersion.cs | 34 +++++++++++++++++++ .../Configuration/IntegrationTestBase.cs | 13 +++++++ Box.V2.Test/Box.V2.Test.csproj | 3 ++ Box.V2.Test/BoxFilesManagerTest.cs | 6 ++-- .../Fixtures/BoxFiles/ViewVersions200.json | 21 ++++++++++++ Box.V2/Models/BoxFileVersion.cs | 7 ++++ 8 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 Box.V2.Test.Integration/Configuration/Commands/NonDisposableCommands/CreateNewFileVersion.cs create mode 100644 Box.V2.Test/Fixtures/BoxFiles/ViewVersions200.json diff --git a/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs b/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs index b0c5f39ba..e861a3fa0 100644 --- a/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs +++ b/Box.V2.Test.Integration/BoxFilesManagerIntegrationTest.cs @@ -249,6 +249,23 @@ public async Task UploadNewVersionOfBigFileInSession_ShouldUploadNewVersionOfFil Assert.AreNotEqual(file.FileVersion.Id, response.FileVersion.Id); } + [TestMethod] + public async Task ViewVersions_ShouldReturnCorrectVersionNumber_WhenFileVersionIsChangedByUpload() + { + var file = await CreateSmallFile(); + await CreateNewFileVersion(file.Id); + + var response = await UserClient.FilesManager.ViewVersionsAsync(file.Id, new List() { BoxFileVersion.FieldVersionNumber }); + + Assert.AreEqual("1", response.Entries[0].VersionNumber); + + await CreateNewFileVersion(file.Id); + + response = await UserClient.FilesManager.ViewVersionsAsync(file.Id, new List() { BoxFileVersion.FieldVersionNumber }); + + Assert.AreEqual("2", response.Entries[0].VersionNumber); + } + private int GetNumberOfParts(long totalSize, long partSize) { if (partSize == 0) diff --git a/Box.V2.Test.Integration/Configuration/Commands/ICommand.cs b/Box.V2.Test.Integration/Configuration/Commands/ICommand.cs index 7588c54ee..50388624a 100644 --- a/Box.V2.Test.Integration/Configuration/Commands/ICommand.cs +++ b/Box.V2.Test.Integration/Configuration/Commands/ICommand.cs @@ -43,4 +43,17 @@ public interface ICleanupCommand : ICommand /// Task Execute(IBoxClient client); } + + /// + /// Interface used to create a resource. It does not perform a remove/dispose action. Used in cases such as new file version upload. + /// + public interface INonDisposableCommand : ICommand + { + /// + /// Creates resources required for the test. Returns the identifier of the resource. + /// + /// Box Client + /// Resource Id + Task Execute(IBoxClient client); + } } diff --git a/Box.V2.Test.Integration/Configuration/Commands/NonDisposableCommands/CreateNewFileVersion.cs b/Box.V2.Test.Integration/Configuration/Commands/NonDisposableCommands/CreateNewFileVersion.cs new file mode 100644 index 000000000..a896deedf --- /dev/null +++ b/Box.V2.Test.Integration/Configuration/Commands/NonDisposableCommands/CreateNewFileVersion.cs @@ -0,0 +1,34 @@ +using System.IO; +using System.Threading.Tasks; +using Box.V2.Models; + +namespace Box.V2.Test.Integration.Configuration.Commands.DisposableCommands +{ + public class CreateNewFileVersion : CommandBase, INonDisposableCommand + { + private readonly string _fileName; + private readonly string _filePath; + private readonly string _fileId; + + public string FileId; + public BoxFile File; + + public CreateNewFileVersion(string fileName, string filePath, string fileId, CommandScope scope = CommandScope.Test, CommandAccessLevel accessLevel = CommandAccessLevel.User) : base(scope, accessLevel) + { + _fileName = fileName; + _filePath = filePath; + _fileId = fileId; + } + + public async Task Execute(IBoxClient client) + { + using (var fileStream = new FileStream(_filePath, FileMode.OpenOrCreate)) + { + var response = await client.FilesManager.UploadNewVersionAsync(_fileName, _fileId, fileStream); + File = response; + FileId = File.Id; + return FileId; + } + } + } +} diff --git a/Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs b/Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs index 914bd5eda..965c97b2b 100644 --- a/Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs +++ b/Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs @@ -155,6 +155,12 @@ public static async Task ExecuteCommand(ICleanupCommand command) await command.Execute(client); } + public static async Task ExecuteCommand(INonDisposableCommand command) + { + IBoxClient client = GetClient(command); + await command.Execute(client); + } + public static async Task ExecuteCommand(IDisposableCommand command) { IBoxClient client = GetClient(command); @@ -332,6 +338,13 @@ public static async Task AddCollaborationE return addCollaborationExemptCommand.WhitelistTargetEntry; } + public static async Task CreateNewFileVersion(string fileId) + { + var createNewFileVersionCommand = new CreateNewFileVersion(GetUniqueName("file"), GetSmallFilePath(), fileId); + await ExecuteCommand(createNewFileVersionCommand); + return createNewFileVersionCommand.File; + } + public static async Task Retry(Func action, int retries = 3, int sleep = 1000) { var retryCount = 0; diff --git a/Box.V2.Test/Box.V2.Test.csproj b/Box.V2.Test/Box.V2.Test.csproj index a55c3abaa..0407a5482 100644 --- a/Box.V2.Test/Box.V2.Test.csproj +++ b/Box.V2.Test/Box.V2.Test.csproj @@ -47,6 +47,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/Box.V2.Test/BoxFilesManagerTest.cs b/Box.V2.Test/BoxFilesManagerTest.cs index d80625861..77f43e6e3 100644 --- a/Box.V2.Test/BoxFilesManagerTest.cs +++ b/Box.V2.Test/BoxFilesManagerTest.cs @@ -298,16 +298,15 @@ public async Task UploadNewVersion_ValidResponse_ValidFile() public async Task ViewVersions_ValidResponse_ValidFileVersions() { /*** Arrange ***/ - var responseString = "{ \"total_count\": 1, \"entries\": [ { \"type\": \"file_version\", \"id\": \"672259576\", \"sha1\": \"359c6c1ed98081b9a69eb3513b9deced59c957f9\", \"name\": \"Dragons.js\", \"size\": 92556, \"created_at\": \"2012-08-20T10:20:30-07:00\", \"modified_at\": \"2012-11-28T13:14:58-08:00\", \"modified_by\": { \"type\": \"user\", \"id\": \"183732129\", \"name\": \"sean rose\", \"login\": \"sean+apitest@box.com\" } } ] }"; Handler.Setup(h => h.ExecuteAsync>(It.IsAny())) .Returns(Task.FromResult>>(new BoxResponse>() { Status = ResponseStatus.Success, - ContentString = responseString + ContentString = LoadFixtureFromJson("Fixtures/BoxFiles/ViewVersions200.json") })); /*** Act ***/ - BoxCollection c = await _filesManager.ViewVersionsAsync("0"); + BoxCollection c = await _filesManager.ViewVersionsAsync("0", new List() { BoxFileVersion.FieldVersionNumber }); /*** Assert ***/ Assert.AreEqual(c.TotalCount, 1); @@ -324,6 +323,7 @@ public async Task ViewVersions_ValidResponse_ValidFileVersions() Assert.AreEqual("183732129", f.ModifiedBy.Id); Assert.AreEqual("sean rose", f.ModifiedBy.Name); Assert.AreEqual("sean+apitest@box.com", f.ModifiedBy.Login); + Assert.AreEqual("1", f.VersionNumber); } [TestMethod] diff --git a/Box.V2.Test/Fixtures/BoxFiles/ViewVersions200.json b/Box.V2.Test/Fixtures/BoxFiles/ViewVersions200.json new file mode 100644 index 000000000..962457e59 --- /dev/null +++ b/Box.V2.Test/Fixtures/BoxFiles/ViewVersions200.json @@ -0,0 +1,21 @@ +{ + "total_count": 1, + "entries": [ + { + "type": "file_version", + "id": "672259576", + "sha1": "359c6c1ed98081b9a69eb3513b9deced59c957f9", + "name": "Dragons.js", + "size": 92556, + "created_at": "2012-08-20T10:20:30-07:00", + "modified_at": "2012-11-28T13:14:58-08:00", + "modified_by": { + "type": "user", + "id": "183732129", + "name": "sean rose", + "login": "sean+apitest@box.com" + }, + "version_number" : "1" + } + ] +} diff --git a/Box.V2/Models/BoxFileVersion.cs b/Box.V2/Models/BoxFileVersion.cs index 6f1a1468c..227a1f543 100644 --- a/Box.V2/Models/BoxFileVersion.cs +++ b/Box.V2/Models/BoxFileVersion.cs @@ -21,6 +21,7 @@ public class BoxFileVersion : BoxEntity public const string FieldRestoredAt = "restored_at"; public const string FieldRestoredBy = "restored_by"; public const string FieldFileVersion = "file_version"; + public const string FieldVersionNumber = "version_number"; /// /// The sha1 hash of this file @@ -102,5 +103,11 @@ public class BoxFileVersion : BoxEntity /// [JsonProperty(PropertyName = FieldFileVersion)] public virtual BoxFileVersion FileVersion { get; private set; } + + /// + /// The version number of the file version + /// + [JsonProperty(PropertyName = FieldVersionNumber)] + public virtual string VersionNumber { get; private set; } } }