|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using SIL.Harmony.Resource; |
| 3 | + |
| 4 | +namespace SIL.Harmony.Tests.ResourceTests; |
| 5 | + |
| 6 | +public class RemoteResourcesTests : DataModelTestBase |
| 7 | +{ |
| 8 | + private RemoteServiceMock _remoteServiceMock = new(); |
| 9 | + |
| 10 | + public RemoteResourcesTests() |
| 11 | + { |
| 12 | + } |
| 13 | + |
| 14 | + private string CreateFile(string contents, [CallerMemberName] string fileName = "") |
| 15 | + { |
| 16 | + var filePath = Path.GetFullPath(fileName + ".txt"); |
| 17 | + File.WriteAllText(filePath, contents); |
| 18 | + return filePath; |
| 19 | + } |
| 20 | + |
| 21 | + private async Task<(Guid resourceId, string remoteId)> SetupRemoteResource(string fileContents) |
| 22 | + { |
| 23 | + var remoteId = _remoteServiceMock.CreateRemoteResource(fileContents); |
| 24 | + var resourceId = Guid.NewGuid(); |
| 25 | + await DataModel.AddChange(_localClientId, new CreateRemoteResourceChange(resourceId, remoteId)); |
| 26 | + return (resourceId, remoteId); |
| 27 | + } |
| 28 | + |
| 29 | + private async Task<(Guid resourceId, string localPath)> SetupLocalFile(string contents, [CallerMemberName] string fileName = "") |
| 30 | + { |
| 31 | + var file = CreateFile(contents, fileName); |
| 32 | + //because resource service is null the file is not uploaded |
| 33 | + var resourceId = await DataModel.AddLocalResource(file, _localClientId, resourceService: null); |
| 34 | + return (resourceId, file); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public async Task CreatingAResourceResultsInPendingLocalResources() |
| 39 | + { |
| 40 | + var (_, file) = await SetupLocalFile("contents"); |
| 41 | + |
| 42 | + //act |
| 43 | + var pending = await DataModel.ListResourcesPendingUpload(); |
| 44 | + |
| 45 | + |
| 46 | + pending.Should().ContainSingle().Which.LocalPath.Should().Be(file); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task ResourcesNotLocalShouldShowUpAsNotDownloaded() |
| 51 | + { |
| 52 | + var (resourceId, remoteId) = await SetupRemoteResource("test"); |
| 53 | + |
| 54 | + //act |
| 55 | + var pending = await DataModel.ListResourcesPendingDownload(); |
| 56 | + |
| 57 | + |
| 58 | + var remoteResource = pending.Should().ContainSingle().Subject; |
| 59 | + remoteResource.RemoteId.Should().Be(remoteId); |
| 60 | + remoteResource.Id.Should().Be(resourceId); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task CanUploadFileToRemote() |
| 65 | + { |
| 66 | + var fileContents = "resource"; |
| 67 | + var localFile = CreateFile(fileContents); |
| 68 | + |
| 69 | + //act |
| 70 | + var resourceId = |
| 71 | + await DataModel.AddLocalResource(localFile, _localClientId, resourceService: _remoteServiceMock); |
| 72 | + |
| 73 | + |
| 74 | + var resource = await DataModel.GetLatest<RemoteResource>(resourceId); |
| 75 | + ArgumentNullException.ThrowIfNull(resource); |
| 76 | + ArgumentNullException.ThrowIfNull(resource.RemoteId); |
| 77 | + _remoteServiceMock.ReadFile(resource.RemoteId).Should().Be(fileContents); |
| 78 | + var pendingUpload = await DataModel.ListResourcesPendingUpload(); |
| 79 | + pendingUpload.Should().BeEmpty(); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public async Task WillUploadMultiplePendingLocalFilesAtOnce() |
| 84 | + { |
| 85 | + await SetupLocalFile("file1", "file1"); |
| 86 | + await SetupLocalFile("file2", "file2"); |
| 87 | + |
| 88 | + //act |
| 89 | + await DataModel.UploadPendingResources(_localClientId, _remoteServiceMock); |
| 90 | + |
| 91 | + |
| 92 | + _remoteServiceMock.ListFiles() |
| 93 | + .Select(Path.GetFileName) |
| 94 | + .Should() |
| 95 | + .Contain(["file1.txt", "file2.txt"]); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public async Task CanDownloadFileFromRemote() |
| 100 | + { |
| 101 | + var fileContents = "resource"; |
| 102 | + var (resourceId, _) = await SetupRemoteResource(fileContents); |
| 103 | + |
| 104 | + //act |
| 105 | + var localResource = await DataModel.DownloadResource(resourceId, _remoteServiceMock); |
| 106 | + |
| 107 | + |
| 108 | + localResource.Id.Should().Be(resourceId); |
| 109 | + var actualFileContents = await File.ReadAllTextAsync(localResource.LocalPath); |
| 110 | + actualFileContents.Should().Be(fileContents); |
| 111 | + var pendingDownloads = await DataModel.ListResourcesPendingDownload(); |
| 112 | + pendingDownloads.Should().BeEmpty(); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public async Task CanGetALocalResourceGivenAnId() |
| 117 | + { |
| 118 | + var file = CreateFile("resource"); |
| 119 | + //because resource service is null the file is not uploaded |
| 120 | + var resourceId = await DataModel.AddLocalResource(file, _localClientId, resourceService: null); |
| 121 | + |
| 122 | + //act |
| 123 | + var localResource = await DataModel.GetLocalResource(resourceId); |
| 124 | + |
| 125 | + |
| 126 | + localResource.Should().NotBeNull(); |
| 127 | + localResource!.LocalPath.Should().Be(file); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public async Task LocalResourceIsNullIfNotDownloaded() |
| 132 | + { |
| 133 | + var (resourceId, _) = await SetupRemoteResource("test"); |
| 134 | + var localResource = await DataModel.GetLocalResource(resourceId); |
| 135 | + localResource.Should().BeNull(); |
| 136 | + } |
| 137 | +} |
0 commit comments