Skip to content

Commit 9044df2

Browse files
committed
replace custom workaround with call to cache refresh endpoint
1 parent cec73ea commit 9044df2

File tree

8 files changed

+34
-145
lines changed

8 files changed

+34
-145
lines changed

src/TestingExample.Website/Testing/ClearScenarioController.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.

test/TestingExample.ManagementApiClient.UnitTests/ScenarioBuilderAddTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ScenarioBuilderAddTests
2020

2121
public ScenarioBuilderAddTests()
2222
{
23-
_scenario = new(new Uri("https://example.com"), new DocumentClientDummy());
23+
_scenario = new(new Uri("https://example.com"), new DocumentClientDummy(), new CacheClientStub());
2424
}
2525

2626
[Fact]

test/TestingExample.ManagementApiClient.UnitTests/ScenarioBuilderBuildTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ScenarioBuilderBuildTests
2424
public ScenarioBuilderBuildTests()
2525
{
2626
_spy = new DocumentClientSpy();
27-
_scenario = new ScenarioBuilder(new Uri("https://example.com"), _spy);
27+
_scenario = new ScenarioBuilder(new Uri("https://example.com"), _spy, new CacheClientStub());
2828
}
2929

3030
[Fact]
@@ -168,7 +168,7 @@ public async Task ShouldAssignUrlToContentAfterPublishing()
168168
// given
169169
var pageId = Guid.Parse("1a235bf2-b566-4e83-8984-2cba3411601e");
170170
var scenario = new ScenarioBuilder(new Uri("https://example.com"), new DocumentClientMock()
171-
.WithUrlFor(pageId, "en-US", "https://example.com/"));
171+
.WithUrlFor(pageId, "en-US", "https://example.com/"), new CacheClientStub());
172172
var page = scenario.Add<ExampleContentModel>(id: pageId, contentType: DefaultContentType)
173173
.HasVariation(Variation.Culture("en-US"), "English variant");
174174

@@ -185,7 +185,7 @@ public async Task ShouldInferAbsoluteUrlWithBaseUrl()
185185
// given
186186
var pageId = Guid.Parse("1a235bf2-b566-4e83-8984-2cba3411601e");
187187
var scenario = new ScenarioBuilder(new Uri("https://example.com"), new DocumentClientMock()
188-
.WithUrlFor(pageId, "en-US", "/privacy-policy"));
188+
.WithUrlFor(pageId, "en-US", "/privacy-policy"), new CacheClientStub());
189189
var page = scenario.Add<ExampleContentModel>(id: pageId, contentType: DefaultContentType)
190190
.HasVariation(Variation.Culture("en-US"), "English variant");
191191

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace TestingExample.ManagementApiClient.UnitTests.TestDoubles;
2+
3+
public class CacheClientStub : ICacheClient
4+
{
5+
public Task PostPublishedCacheReloadAsync(CancellationToken cancellationToken)
6+
{
7+
return Task.CompletedTask;
8+
}
9+
}

test/TestingExample.ManagementApiClient/ManagementApiClient.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public async Task<Guid> GetTemplateAsync(string alias, CancellationToken cancell
2828
}
2929
}
3030

31+
public interface ICacheClient
32+
{
33+
Task PostPublishedCacheReloadAsync(CancellationToken cancellationToken);
34+
}
35+
36+
public partial class Published_CacheClient : ICacheClient;
37+
3138
public interface IDocumentClient
3239
{
3340
Task PostDocumentAsync(CreateDocumentRequestModel? body, CancellationToken cancellationToken);
@@ -39,6 +46,13 @@ public interface IDocumentClient
3946

4047
public partial class DocumentClient : IDocumentClient
4148
{
42-
public Task ClearDocumentsAsync(CancellationToken cancellationToken)
43-
=> ClearAsync(cancellationToken);
49+
public async Task ClearDocumentsAsync(CancellationToken cancellationToken)
50+
{
51+
var documents = await GetTreeDocumentRootAsync(skip: 0, take: 100, dataTypeId: null, cancellationToken: cancellationToken);
52+
53+
foreach (var document in documents.Items)
54+
{
55+
await DeleteDocumentByIdAsync(document.Id, cancellationToken: cancellationToken);
56+
}
57+
}
4458
}

test/TestingExample.ManagementApiClient/ManagementApiClient.generated.cs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13784,85 +13784,6 @@ public virtual async System.Threading.Tasks.Task<SubsetDocumentRecycleBinItemRes
1378413784
}
1378513785
}
1378613786

13787-
/// <returns>No Content</returns>
13788-
/// <exception cref="ManagementApiException">A server side error occurred.</exception>
13789-
public virtual System.Threading.Tasks.Task ClearAsync()
13790-
{
13791-
return ClearAsync(System.Threading.CancellationToken.None);
13792-
}
13793-
13794-
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
13795-
/// <returns>No Content</returns>
13796-
/// <exception cref="ManagementApiException">A server side error occurred.</exception>
13797-
public virtual async System.Threading.Tasks.Task ClearAsync(System.Threading.CancellationToken cancellationToken)
13798-
{
13799-
var client_ = _httpClient;
13800-
var disposeClient_ = false;
13801-
try
13802-
{
13803-
using (var request_ = new System.Net.Http.HttpRequestMessage())
13804-
{
13805-
request_.Content = new System.Net.Http.StringContent(string.Empty, System.Text.Encoding.UTF8, "application/json");
13806-
request_.Method = new System.Net.Http.HttpMethod("POST");
13807-
13808-
var urlBuilder_ = new System.Text.StringBuilder();
13809-
13810-
// Operation Path: "umbraco/management/api/v1/scenario/clear"
13811-
urlBuilder_.Append("umbraco/management/api/v1/scenario/clear");
13812-
13813-
await PrepareRequestAsync(client_, request_, urlBuilder_, cancellationToken).ConfigureAwait(false);
13814-
13815-
var url_ = urlBuilder_.ToString();
13816-
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
13817-
13818-
await PrepareRequestAsync(client_, request_, url_, cancellationToken).ConfigureAwait(false);
13819-
13820-
var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
13821-
var disposeResponse_ = true;
13822-
try
13823-
{
13824-
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
13825-
foreach (var item_ in response_.Headers)
13826-
headers_[item_.Key] = item_.Value;
13827-
if (response_.Content != null && response_.Content.Headers != null)
13828-
{
13829-
foreach (var item_ in response_.Content.Headers)
13830-
headers_[item_.Key] = item_.Value;
13831-
}
13832-
13833-
await ProcessResponseAsync(client_, response_, cancellationToken).ConfigureAwait(false);
13834-
13835-
var status_ = (int)response_.StatusCode;
13836-
if (status_ == 204)
13837-
{
13838-
return;
13839-
}
13840-
else
13841-
if (status_ == 401)
13842-
{
13843-
string responseText_ = ( response_.Content == null ) ? string.Empty : await ReadAsStringAsync(response_.Content, cancellationToken).ConfigureAwait(false);
13844-
throw new ManagementApiException("The resource is protected and requires an authentication token", status_, responseText_, headers_, null);
13845-
}
13846-
else
13847-
{
13848-
var responseData_ = response_.Content == null ? null : await ReadAsStringAsync(response_.Content, cancellationToken).ConfigureAwait(false);
13849-
throw new ManagementApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
13850-
}
13851-
}
13852-
finally
13853-
{
13854-
if (disposeResponse_)
13855-
response_.Dispose();
13856-
}
13857-
}
13858-
}
13859-
finally
13860-
{
13861-
if (disposeClient_)
13862-
client_.Dispose();
13863-
}
13864-
}
13865-
1386613787
/// <returns>OK</returns>
1386713788
/// <exception cref="ManagementApiException">A server side error occurred.</exception>
1386813789
public virtual System.Threading.Tasks.Task<System.Collections.Generic.ICollection<DocumentTreeItemResponseModel>> GetTreeDocumentAncestorsAsync(System.Guid? descendantId)

test/TestingExample.ManagementApiClient/Scenario/ScenarioBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace TestingExample.ManagementApiClient.Scenario;
44

5-
public class ScenarioBuilder(Uri baseUri, IDocumentClient documentClient)
5+
public class ScenarioBuilder(Uri baseUri, IDocumentClient documentClient, ICacheClient cacheClient)
66
{
77
private readonly Dictionary<Guid, PageModel> _pages = [];
88
private readonly IDocumentClient _documentClient = documentClient;
9+
private readonly ICacheClient _cacheClient = cacheClient;
910

1011
public Uri BaseUri { get; } = baseUri;
1112

@@ -27,6 +28,7 @@ public PageModel<T> Add<T>(PageModel<T> page)
2728
public async Task BuildAsync(CancellationToken cancellationToken = default)
2829
{
2930
await _documentClient.ClearDocumentsAsync(cancellationToken);
31+
await _cacheClient.PostPublishedCacheReloadAsync(cancellationToken);
3032

3133
foreach (var page in _pages.Values.OrderBy(page => page.Level))
3234
{

test/TestingExample.Website.FunctionalTests/UmbracoWebsiteFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public UmbracoWebsiteFixture()
3434
public ScenarioBuilder NewScenario()
3535
{
3636
var documentClient = new DocumentClient(_tokenManager, HttpClient);
37-
return new ScenarioBuilder(BaseAddress, documentClient);
37+
var cacheClient = new Published_CacheClient(_tokenManager, HttpClient);
38+
return new ScenarioBuilder(BaseAddress, documentClient, cacheClient);
3839
}
3940

4041
protected virtual void Dispose(bool disposing)

0 commit comments

Comments
 (0)