Skip to content

Commit

Permalink
test AllResources api, change AddLocalResource to return a CrdtReso…
Browse files Browse the repository at this point in the history
…urce
  • Loading branch information
hahn-kev committed Nov 29, 2024
1 parent 5ed1756 commit f8d670a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
39 changes: 32 additions & 7 deletions src/SIL.Harmony.Tests/ResourceTests/RemoteResourcesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ private string CreateFile(string contents, [CallerMemberName] string fileName =
{
var file = CreateFile(contents, fileName);
//because resource service is null the file is not uploaded
var resourceId = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null);
return (resourceId, file);
var crdtResource = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null);
return (crdtResource.Id, file);
}

[Fact]
Expand Down Expand Up @@ -69,11 +69,10 @@ public async Task CanUploadFileToRemote()
var localFile = CreateFile(fileContents);

//act
var resourceId =
await _resourceService.AddLocalResource(localFile, _localClientId, resourceService: _remoteServiceMock);
var crdtResource = await _resourceService.AddLocalResource(localFile, _localClientId, resourceService: _remoteServiceMock);


var resource = await DataModel.GetLatest<RemoteResource>(resourceId);
var resource = await DataModel.GetLatest<RemoteResource>(crdtResource.Id);
ArgumentNullException.ThrowIfNull(resource);
ArgumentNullException.ThrowIfNull(resource.RemoteId);
_remoteServiceMock.ReadFile(resource.RemoteId).Should().Be(fileContents);
Expand Down Expand Up @@ -119,10 +118,10 @@ public async Task CanGetALocalResourceGivenAnId()
{
var file = CreateFile("resource");
//because resource service is null the file is not uploaded
var resourceId = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null);
var crdtResource = await _resourceService.AddLocalResource(file, _localClientId, resourceService: null);

//act
var localResource = await _resourceService.GetLocalResource(resourceId);
var localResource = await _resourceService.GetLocalResource(crdtResource.Id);


localResource.Should().NotBeNull();
Expand All @@ -136,4 +135,30 @@ public async Task LocalResourceIsNullIfNotDownloaded()
var localResource = await _resourceService.GetLocalResource(resourceId);
localResource.Should().BeNull();
}

[Fact]
public async Task CanListAllResources()
{
var (localResourceId, localResourcePath) = await SetupLocalFile("localOnly", "localOnly.txt");
var (remoteResourceId, remoteId) = await SetupRemoteResource("remoteOnly");
var localAndRemoteResource = await _resourceService.AddLocalResource(CreateFile("localAndRemove"), _localClientId, resourceService: _remoteServiceMock);

var crdtResources = await _resourceService.AllResources();
crdtResources.Should().BeEquivalentTo(
[
new CrdtResource
{
Id = localResourceId,
LocalPath = localResourcePath,
RemoteId = null
},
new CrdtResource
{
Id = remoteResourceId,
LocalPath = null,
RemoteId = remoteId
},
localAndRemoteResource
]);
}
}
6 changes: 3 additions & 3 deletions src/SIL.Harmony.Tests/ResourceTests/WordResourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public async Task CanReferenceAResourceFromAWord()
var imageFile = CreateFile("not image data");
//set commit date for add local resource
MockTimeProvider.SetNextDateTime(NextDate());
var resourceId = await _resourceService.AddLocalResource(imageFile, Guid.NewGuid(), resourceService: _remoteServiceMock);
await WriteNextChange(new AddWordImageChange(_entity1Id, resourceId));
var resource = await _resourceService.AddLocalResource(imageFile, Guid.NewGuid(), resourceService: _remoteServiceMock);
await WriteNextChange(new AddWordImageChange(_entity1Id, resource.Id));

var word = await DataModel.GetLatest<Word>(_entity1Id);
word.Should().NotBeNull();
word!.ImageResourceId.Should().Be(resourceId);
word!.ImageResourceId.Should().Be(resource.Id);


var localResource = await _resourceService.GetLocalResource(word.ImageResourceId!.Value);
Expand Down
32 changes: 24 additions & 8 deletions src/SIL.Harmony/ResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ private void ValidateResourcesSetup()
if (!_crdtConfig.Value.RemoteResourcesEnabled) throw new RemoteResourceNotEnabledException();
}

public async Task<Guid> AddLocalResource(string resourcePath, Guid clientId, Guid id = default, IRemoteResourceService? resourceService = null)
public async Task<CrdtResource> AddLocalResource(string resourcePath,
Guid clientId,
Guid id = default,
IRemoteResourceService? resourceService = null)
{
ValidateResourcesSetup();
var localResource = new LocalResource
Expand All @@ -41,13 +44,23 @@ public async Task<Guid> AddLocalResource(string resourcePath, Guid clientId, Gui
{
var uploadResult = await resourceService.UploadResource(localResource.LocalPath);
await _dataModel.AddChange(clientId, new CreateRemoteResourceChange(localResource.Id, uploadResult.RemoteId));
await transaction.CommitAsync();
return new CrdtResource
{
Id = localResource.Id,
RemoteId = uploadResult.RemoteId,
LocalPath = localResource.LocalPath
};
}
else
{
await _dataModel.AddChange(clientId, new CreateRemoteResourcePendingUploadChange(localResource.Id));
}

await _dataModel.AddChange(clientId, new CreateRemoteResourcePendingUploadChange(localResource.Id));
await transaction.CommitAsync();
return localResource.Id;
return new CrdtResource
{
Id = localResource.Id,
RemoteId = null,
LocalPath = localResource.LocalPath
};
}

public async Task<LocalResource[]> ListResourcesPendingUpload()
Expand Down Expand Up @@ -106,7 +119,11 @@ public async Task<RemoteResource[]> ListResourcesPendingDownload()
public async Task<LocalResource> DownloadResource(Guid resourceId, IRemoteResourceService remoteResourceService)
{
ValidateResourcesSetup();
return await DownloadResource(await _dataModel.GetLatest<RemoteResource>(resourceId) ?? throw new EntityNotFoundException("Unable to find remote resource"), remoteResourceService);
return await DownloadResource(
await _dataModel.GetLatest<RemoteResource>(resourceId) ??
throw new EntityNotFoundException("Unable to find remote resource"),
remoteResourceService
);
}

public async Task<LocalResource> DownloadResource(RemoteResource remoteResource, IRemoteResourceService remoteResourceService)
Expand Down Expand Up @@ -141,6 +158,5 @@ public async Task<CrdtResource[]> AllResources()
RemoteId = r?.RemoteId,
LocalPath = l?.LocalPath
}).ToArray();

}
}

0 comments on commit f8d670a

Please sign in to comment.