Skip to content

Commit

Permalink
Fix #7804: Remove HasChanged (#7908)
Browse files Browse the repository at this point in the history
* Fix #7804: Remove HasChanged

* Remove unused field
  • Loading branch information
AlexanderSher authored Oct 4, 2019
1 parent c467128 commit 48c7209
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -851,86 +851,6 @@ private Request CreateGetRevisionsRequest(SettingSelector selector, string pageL
return request;
}

/// <summary>
/// </summary>
/// <param name="setting"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<Response<bool>> HasChangedAsync(ConfigurationSetting setting, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope("Azure.Data.AppConfiguration.ConfigurationClient.HasChanged");
scope.AddAttribute("key", setting.Key);
scope.Start();

try
{
using Request request = CreateHeadRequest(setting.Key, setting.Label);
Response response = await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false);

switch (response.Status)
{
case 200:
case 404:
string etag = default;
if (response.Headers.TryGetValue(ETag, out etag))
{
etag = etag.Trim('\"');
}

return Response.FromValue(setting.ETag != new ETag(etag), response);

default:
throw await response.CreateRequestFailedExceptionAsync().ConfigureAwait(false);
};
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

/// <summary>
/// </summary>
/// <param name="setting"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual Response<bool> HasChanged(ConfigurationSetting setting, CancellationToken cancellationToken = default)
{
using DiagnosticScope scope = _pipeline.Diagnostics.CreateScope("Azure.Data.AppConfiguration.ConfigurationClient.HasChanged");
scope.AddAttribute(nameof(setting.Key), setting.Key);
scope.Start();

try
{
using (Request request = CreateHeadRequest(setting.Key, setting.Label))
{
Response response = _pipeline.SendRequest(request, cancellationToken);

switch (response.Status)
{
case 200:
case 404:
string etag = default;
if (response.Headers.TryGetValue(ETag, out etag))
{
etag = etag.Trim('\"');
}

return Response.FromValue(setting.ETag != new ETag(etag), response);

default:
throw response.CreateRequestFailedException();
}
}
}
catch (Exception e)
{
scope.Failed(e);
throw;
}
}

private Request CreateHeadRequest(string key, string label)
{
if (string.IsNullOrEmpty(key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public partial class ConfigurationClient
private const string KeyQueryFilter = "key";
private const string LabelQueryFilter = "label";
private const string FieldsQueryFilter = "$select";
private const string ETag = "ETag";

private static readonly char[] s_reservedCharacters = new char[] { ',', '\\' };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,103 +869,6 @@ public async Task GetBatchSettingWithAllFields()
}
}

[Test]
public async Task HasChangedNoValueNotFound()
{
ConfigurationClient service = GetClient();
ConfigurationSetting setting0 = CreateSetting();

bool hasChanged = await service.HasChangedAsync(setting0);

Assert.IsFalse(hasChanged);
}

[Test]
public async Task HasChangedNoValueNewValue()
{
ConfigurationClient service = GetClient();
ConfigurationSetting setting0 = CreateSetting();

try
{
ConfigurationSetting setting1 = await service.SetAsync(setting0);

// Test
bool hasChanged = await service.HasChangedAsync(setting0);

Assert.IsTrue(hasChanged);
}
finally
{
await service.DeleteAsync(setting0.Key, setting0.Label);
}
}

[Test]
public async Task HasChangedValuesMatch()
{
ConfigurationClient service = GetClient();
ConfigurationSetting setting0 = CreateSetting();

try
{
ConfigurationSetting setting1 = await service.SetAsync(setting0);

// Test
bool hasChanged = await service.HasChangedAsync(setting1);

Assert.IsFalse(hasChanged);
}
finally
{
await service.DeleteAsync(setting0.Key, setting0.Label);
}
}

[Test]
public async Task HasChangedValuesDontMatch()
{
ConfigurationClient service = GetClient();
ConfigurationSetting setting0 = CreateSetting();

try
{
ConfigurationSetting setting1 = await service.SetAsync(setting0);
setting0.Value = "test_value2";
ConfigurationSetting setting2 = await service.SetAsync(setting0);

// Test
bool hasChanged = await service.HasChangedAsync(setting1);

Assert.IsTrue(hasChanged);
}
finally
{
await service.DeleteAsync(setting0.Key, setting0.Label);
}
}

[Test]
public async Task HasChangedValueNotFound()
{
ConfigurationClient service = GetClient();
ConfigurationSetting setting0 = CreateSetting();

try
{
ConfigurationSetting setting1 = await service.SetAsync(setting0);
var response = await service.DeleteAsync(setting1.Key, setting1.Label);

// Test
bool hasChanged = await service.HasChangedAsync(setting1);

Assert.IsTrue(hasChanged);
}
finally
{
await service.DeleteAsync(setting0.Key, setting0.Label);
}
}

[Test]
public async Task SetReadOnlyOnSetting()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,131 +636,6 @@ public async Task AuthorizationHeaderFormat()
Assert.True(Regex.IsMatch(authHeader, expectedSyntax));
}

[Test]
public async Task HasChangedNoValueNotFound()
{
var response = new MockResponse(404);

var mockTransport = new MockTransport(response);
ConfigurationClient service = CreateTestService(mockTransport);

bool hasChanged = await service.HasChangedAsync(s_testSetting);

MockRequest request = mockTransport.SingleRequest;

AssertRequestCommon(request);
Assert.AreEqual(RequestMethod.Head, request.Method);
Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString());
Assert.IsFalse(response.Headers.TryGetValue("ETag", out string etag));
Assert.IsFalse(hasChanged);
}

[Test]
public async Task HasChangedNoValueNewValue()
{
var response = new MockResponse(200);

var unversionedSetting = s_testSetting.Clone();

string version = Guid.NewGuid().ToString();
var versionedSetting = s_testSetting.Clone();
versionedSetting.ETag = new ETag(version);

response.SetContent(SerializationHelpers.Serialize(versionedSetting, SerializeSetting));
response.AddHeader(new HttpHeader("ETag", versionedSetting.ETag.ToString()));

var mockTransport = new MockTransport(response);
ConfigurationClient service = CreateTestService(mockTransport);

bool hasChanged = await service.HasChangedAsync(unversionedSetting);
MockRequest request = mockTransport.SingleRequest;

AssertRequestCommon(request);
Assert.AreEqual(RequestMethod.Head, request.Method);
Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString());
Assert.IsTrue(response.Headers.TryGetValue("ETag", out string etag));
Assert.AreEqual(version, etag);
Assert.IsTrue(hasChanged);
}

[Test]
public async Task HasChangedValuesMatch()
{
var response = new MockResponse(200);

string version = Guid.NewGuid().ToString();
var versionedSetting = s_testSetting.Clone();
versionedSetting.ETag = new ETag(version);

response.SetContent(SerializationHelpers.Serialize(versionedSetting, SerializeSetting));
response.AddHeader(new HttpHeader("ETag", versionedSetting.ETag.ToString()));

var mockTransport = new MockTransport(response);
ConfigurationClient service = CreateTestService(mockTransport);

bool hasChanged = await service.HasChangedAsync(versionedSetting);
MockRequest request = mockTransport.SingleRequest;

AssertRequestCommon(request);
Assert.AreEqual(RequestMethod.Head, request.Method);
Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString());
Assert.IsTrue(response.Headers.TryGetValue("ETag", out string etag));
Assert.AreEqual(version, etag);
Assert.IsFalse(hasChanged);
}

[Test]
public async Task HasChangedValuesDontMatch()
{
var response = new MockResponse(200);

string version1 = Guid.NewGuid().ToString();
var version1Setting = s_testSetting.Clone();
version1Setting.ETag = new ETag(version1);

string version2 = Guid.NewGuid().ToString();
var version2Setting = s_testSetting.Clone();
version2Setting.ETag = new ETag(version2);

response.SetContent(SerializationHelpers.Serialize(version2Setting, SerializeSetting));
response.AddHeader(new HttpHeader("ETag", version2Setting.ETag.ToString()));

var mockTransport = new MockTransport(response);
ConfigurationClient service = CreateTestService(mockTransport);

bool hasChanged = await service.HasChangedAsync(version1Setting);
MockRequest request = mockTransport.SingleRequest;

AssertRequestCommon(request);
Assert.AreEqual(RequestMethod.Head, request.Method);
Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString());
Assert.IsTrue(response.Headers.TryGetValue("ETag", out string etag));
Assert.AreEqual(version2, etag);
Assert.IsTrue(hasChanged);
}

[Test]
public async Task HasChangedValueNotFound()
{
var response = new MockResponse(404);

string version = Guid.NewGuid().ToString();
var versionedSetting = s_testSetting.Clone();
versionedSetting.ETag = new ETag(version);

var mockTransport = new MockTransport(response);
ConfigurationClient service = CreateTestService(mockTransport);

bool hasChanged = await service.HasChangedAsync(versionedSetting);

MockRequest request = mockTransport.SingleRequest;

AssertRequestCommon(request);
Assert.AreEqual(RequestMethod.Head, request.Method);
Assert.AreEqual($"https://contoso.appconfig.io/kv/test_key?label=test_label&api-version={s_version}", request.Uri.ToString());
Assert.IsTrue(hasChanged);
}

[Test]
public async Task SetReadOnly()
{
Expand Down

0 comments on commit 48c7209

Please sign in to comment.