From a1bf4aba7e185f516974c377983e29c4ca22dd7a Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 12 May 2020 22:05:51 +0200 Subject: [PATCH] Fix loaders. --- .../Assets/Queries/AssetLoader.cs | 9 +++++---- .../Contents/Queries/ContentLoader.cs | 9 +++++---- .../Assets/Queries/AssetLoaderTests.cs | 11 +++++++++++ .../Contents/Queries/ContentLoaderTests.cs | 11 +++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/Queries/AssetLoader.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/Queries/AssetLoader.cs index 82adbe7db..cfdaf9cec 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/Queries/AssetLoader.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Assets/Queries/AssetLoader.cs @@ -28,16 +28,17 @@ namespace Squidex.Domain.Apps.Entities.Assets.Queries { using (Profiler.TraceMethod()) { - var grain = grainFactory.GetGrain(id); + var assetGrain = grainFactory.GetGrain(id); + var assetState = await assetGrain.GetStateAsync(version); - var content = await grain.GetStateAsync(version); + var asset = assetState.Value; - if (content.Value == null || content.Value.Version != version) + if (asset == null || asset.Version <= EtagVersion.Empty || (version > EtagVersion.Any && asset.Version != version)) { throw new DomainObjectNotFoundException(id.ToString(), typeof(IAssetEntity)); } - return content.Value; + return asset; } } } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/ContentLoader.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/ContentLoader.cs index 5abb7f687..c29116b5d 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/ContentLoader.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/ContentLoader.cs @@ -28,16 +28,17 @@ namespace Squidex.Domain.Apps.Entities.Contents.Queries { using (Profiler.TraceMethod()) { - var grain = grainFactory.GetGrain(id); + var contentGrain = grainFactory.GetGrain(id); + var contentState = await contentGrain.GetStateAsync(version); - var content = await grain.GetStateAsync(version); + var content = contentState.Value; - if (content.Value == null || (version > EtagVersion.Any && content.Value.Version != version)) + if (content == null || content.Version <= EtagVersion.Empty || (version > EtagVersion.Any && content.Version != version)) { throw new DomainObjectNotFoundException(id.ToString(), typeof(IContentEntity)); } - return content.Value; + return content; } } } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/Queries/AssetLoaderTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/Queries/AssetLoaderTests.cs index b04d6c395..99f522b6b 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/Queries/AssetLoaderTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/Queries/AssetLoaderTests.cs @@ -39,6 +39,17 @@ namespace Squidex.Domain.Apps.Entities.Assets.Queries await Assert.ThrowsAsync(() => sut.GetAsync(id, 10)); } + [Fact] + public async Task Should_throw_exception_if_state_empty() + { + var content = new AssetEntity { Version = EtagVersion.Empty }; + + A.CallTo(() => grain.GetStateAsync(10)) + .Returns(J.Of(content)); + + await Assert.ThrowsAsync(() => sut.GetAsync(id, 10)); + } + [Fact] public async Task Should_throw_exception_if_state_has_other_version() { diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Queries/ContentLoaderTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Queries/ContentLoaderTests.cs index 09a1dd703..3e6657b55 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Queries/ContentLoaderTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Queries/ContentLoaderTests.cs @@ -39,6 +39,17 @@ namespace Squidex.Domain.Apps.Entities.Contents.Queries await Assert.ThrowsAsync(() => sut.GetAsync(id, 10)); } + [Fact] + public async Task Should_throw_exception_if_state_empty() + { + var content = new ContentEntity { Version = EtagVersion.Empty }; + + A.CallTo(() => grain.GetStateAsync(10)) + .Returns(J.Of(content)); + + await Assert.ThrowsAsync(() => sut.GetAsync(id, 10)); + } + [Fact] public async Task Should_throw_exception_if_state_has_other_version() {