|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using SIL.Harmony.Resource; |
| 4 | + |
| 5 | +namespace SIL.Harmony.Tests.ResourceTests; |
| 6 | + |
| 7 | +public class RemoteResourcesTests : DataModelTestBase |
| 8 | +{ |
| 9 | + private RemoteServiceMock _remoteServiceMock = new(); |
| 10 | + private ResourceService _resourceService => _services.GetRequiredService<ResourceService>(); |
| 11 | + |
| 12 | + public RemoteResourcesTests() |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + private string CreateFile(string contents, [CallerMemberName] string fileName = "") |
| 17 | + { |
| 18 | + var filePath = Path.GetFullPath(fileName + ".txt"); |
| 19 | + File.WriteAllText(filePath, contents); |
| 20 | + return filePath; |
| 21 | + } |
| 22 | + |
| 23 | + private async Task<(Guid resourceId, string remoteId)> SetupRemoteResource(string fileContents) |
| 24 | + { |
| 25 | + var remoteId = _remoteServiceMock.CreateRemoteResource(fileContents); |
| 26 | + var resourceId = Guid.NewGuid(); |
| 27 | + await DataModel.AddChange(_localClientId, new CreateRemoteResourceChange(resourceId, remoteId)); |
| 28 | + return (resourceId, remoteId); |
| 29 | + } |
| 30 | + |
| 31 | + private async Task<(Guid resourceId, string localPath)> SetupLocalFile(string contents, [CallerMemberName] string fileName = "") |
| 32 | + { |
| 33 | + var file = CreateFile(contents, fileName); |
| 34 | + //because resource service is null the file is not uploaded |
| 35 | + var crdtResource = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null); |
| 36 | + return (crdtResource.Id, file); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public async Task CreatingAResourceResultsInPendingLocalResources() |
| 41 | + { |
| 42 | + var (_, file) = await SetupLocalFile("contents"); |
| 43 | + |
| 44 | + //act |
| 45 | + var pending = await _resourceService.ListResourcesPendingUpload(); |
| 46 | + |
| 47 | + |
| 48 | + pending.Should().ContainSingle().Which.LocalPath.Should().Be(file); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task ResourcesNotLocalShouldShowUpAsNotDownloaded() |
| 53 | + { |
| 54 | + var (resourceId, remoteId) = await SetupRemoteResource("test"); |
| 55 | + |
| 56 | + //act |
| 57 | + var pending = await _resourceService.ListResourcesPendingDownload(); |
| 58 | + |
| 59 | + |
| 60 | + var remoteResource = pending.Should().ContainSingle().Subject; |
| 61 | + remoteResource.RemoteId.Should().Be(remoteId); |
| 62 | + remoteResource.Id.Should().Be(resourceId); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task CanUploadFileToRemote() |
| 67 | + { |
| 68 | + var fileContents = "resource"; |
| 69 | + var localFile = CreateFile(fileContents); |
| 70 | + |
| 71 | + //act |
| 72 | + var crdtResource = await _resourceService.AddLocalResource(localFile, _localClientId, resourceService: _remoteServiceMock); |
| 73 | + |
| 74 | + |
| 75 | + var resource = await DataModel.GetLatest<RemoteResource>(crdtResource.Id); |
| 76 | + ArgumentNullException.ThrowIfNull(resource); |
| 77 | + ArgumentNullException.ThrowIfNull(resource.RemoteId); |
| 78 | + _remoteServiceMock.ReadFile(resource.RemoteId).Should().Be(fileContents); |
| 79 | + var pendingUpload = await _resourceService.ListResourcesPendingUpload(); |
| 80 | + pendingUpload.Should().BeEmpty(); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public async Task IfUploadingFailsTheResourceIsStillAddedAsPendingUpload() |
| 85 | + { |
| 86 | + var fileContents = "resource"; |
| 87 | + var localFile = CreateFile(fileContents); |
| 88 | + |
| 89 | + //todo setup a mock that throws an exception when uploading |
| 90 | + _remoteServiceMock.ThrowOnUpload(localFile); |
| 91 | + |
| 92 | + //act |
| 93 | + var crdtResource = await _resourceService.AddLocalResource(localFile, _localClientId, resourceService: _remoteServiceMock); |
| 94 | + |
| 95 | + var harmonyResource = await _resourceService.GetResource(crdtResource.Id); |
| 96 | + harmonyResource.Should().NotBeNull(); |
| 97 | + harmonyResource.Id.Should().Be(crdtResource.Id); |
| 98 | + harmonyResource.RemoteId.Should().BeNull(); |
| 99 | + harmonyResource.LocalPath.Should().Be(localFile); |
| 100 | + var pendingUpload = await _resourceService.ListResourcesPendingUpload(); |
| 101 | + pendingUpload.Should().ContainSingle().Which.Id.Should().Be(crdtResource.Id); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public async Task WillUploadMultiplePendingLocalFilesAtOnce() |
| 106 | + { |
| 107 | + await SetupLocalFile("file1", "file1"); |
| 108 | + await SetupLocalFile("file2", "file2"); |
| 109 | + |
| 110 | + //act |
| 111 | + await _resourceService.UploadPendingResources(_localClientId, _remoteServiceMock); |
| 112 | + |
| 113 | + |
| 114 | + _remoteServiceMock.ListRemoteFiles() |
| 115 | + .Select(Path.GetFileName) |
| 116 | + .Should() |
| 117 | + .Contain(["file1.txt", "file2.txt"]); |
| 118 | + } |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public async Task CanDownloadFileFromRemote() |
| 122 | + { |
| 123 | + var fileContents = "resource"; |
| 124 | + var (resourceId, _) = await SetupRemoteResource(fileContents); |
| 125 | + |
| 126 | + //act |
| 127 | + var localResource = await _resourceService.DownloadResource(resourceId, _remoteServiceMock); |
| 128 | + |
| 129 | + |
| 130 | + localResource.Id.Should().Be(resourceId); |
| 131 | + var actualFileContents = await File.ReadAllTextAsync(localResource.LocalPath); |
| 132 | + actualFileContents.Should().Be(fileContents); |
| 133 | + var pendingDownloads = await _resourceService.ListResourcesPendingDownload(); |
| 134 | + pendingDownloads.Should().BeEmpty(); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public async Task CanGetALocalResourceGivenAnId() |
| 139 | + { |
| 140 | + var file = CreateFile("resource"); |
| 141 | + //because resource service is null the file is not uploaded |
| 142 | + var crdtResource = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null); |
| 143 | + |
| 144 | + //act |
| 145 | + var localResource = await _resourceService.GetLocalResource(crdtResource.Id); |
| 146 | + |
| 147 | + |
| 148 | + localResource.Should().NotBeNull(); |
| 149 | + localResource!.LocalPath.Should().Be(file); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public async Task LocalResourceIsNullIfNotDownloaded() |
| 154 | + { |
| 155 | + var (resourceId, _) = await SetupRemoteResource("test"); |
| 156 | + var localResource = await _resourceService.GetLocalResource(resourceId); |
| 157 | + localResource.Should().BeNull(); |
| 158 | + } |
| 159 | + |
| 160 | + [Fact] |
| 161 | + public async Task CanListAllResources() |
| 162 | + { |
| 163 | + var (localResourceId, localResourcePath) = await SetupLocalFile("localOnly", "localOnly.txt"); |
| 164 | + var (remoteResourceId, remoteId) = await SetupRemoteResource("remoteOnly"); |
| 165 | + var localAndRemoteResource = await _resourceService.AddLocalResource(CreateFile("localAndRemove"), _localClientId, resourceService: _remoteServiceMock); |
| 166 | + |
| 167 | + var crdtResources = await _resourceService.AllResources(); |
| 168 | + crdtResources.Should().BeEquivalentTo( |
| 169 | + [ |
| 170 | + new HarmonyResource |
| 171 | + { |
| 172 | + Id = localResourceId, |
| 173 | + LocalPath = localResourcePath, |
| 174 | + RemoteId = null |
| 175 | + }, |
| 176 | + new HarmonyResource |
| 177 | + { |
| 178 | + Id = remoteResourceId, |
| 179 | + LocalPath = null, |
| 180 | + RemoteId = remoteId |
| 181 | + }, |
| 182 | + localAndRemoteResource |
| 183 | + ]); |
| 184 | + } |
| 185 | + |
| 186 | + [Fact] |
| 187 | + public async Task CanGetAResourceGivenAnId() |
| 188 | + { |
| 189 | + var (localResourceId, localResourcePath) = await SetupLocalFile("localOnly", "localOnly.txt"); |
| 190 | + var (remoteResourceId, remoteId) = await SetupRemoteResource("remoteOnly"); |
| 191 | + var localAndRemoteResource = await _resourceService.AddLocalResource(CreateFile("localAndRemove"), |
| 192 | + _localClientId, |
| 193 | + resourceService: _remoteServiceMock); |
| 194 | + |
| 195 | + (await _resourceService.GetResource(localResourceId)).Should().BeEquivalentTo(new HarmonyResource |
| 196 | + { |
| 197 | + Id = localResourceId, |
| 198 | + LocalPath = localResourcePath, |
| 199 | + RemoteId = null |
| 200 | + }); |
| 201 | + (await _resourceService.GetResource(remoteResourceId)).Should().BeEquivalentTo(new HarmonyResource |
| 202 | + { |
| 203 | + Id = remoteResourceId, |
| 204 | + LocalPath = null, |
| 205 | + RemoteId = remoteId |
| 206 | + }); |
| 207 | + (await _resourceService.GetResource(localAndRemoteResource.Id)).Should().BeEquivalentTo(localAndRemoteResource); |
| 208 | + (await _resourceService.GetResource(Guid.NewGuid())).Should().BeNull(); |
| 209 | + } |
| 210 | +} |
0 commit comments