Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Apis.Storage.v1.Data;
using Google.Cloud.ClientTesting;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -120,5 +122,36 @@ public void MetadataOverride()
Object fetched = _fixture.Client.GetObject(_fixture.SingleVersionBucket, destName);
Assert.Equal("text/html", fetched.ContentType);
}

[Fact]
public void CopyObjectWithObjectContexts()
{
string contextKey = "A\u00F1\u03A9\U0001F680";
string contextValue = "Ab\u00F1\u03A9\U0001F680";
var custom = new Dictionary<string, ObjectCustomContextPayload>
{
{ contextKey, new ObjectCustomContextPayload { Value = contextValue } }
};

var destination = new Object
{
Bucket = _fixture.SingleVersionBucket,
Name = IdGenerator.FromGuid(),
ContentType = "test/type",
ContentDisposition = "attachment",
Metadata = new Dictionary<string, string> { { "x", "y" } },
Contexts = new Object.ContextsData { Custom = custom }
};
var source = GenerateData(100);
var result = _fixture.Client.UploadObject(destination, source);
_fixture.Client.CopyObject(
_fixture.SingleVersionBucket, destination.Name,
_fixture.MultiVersionBucket, destination.Name);
Object fetched = _fixture.Client.GetObject(_fixture.MultiVersionBucket, destination.Name);
Assert.Equal(result.Contexts.Custom.Count, fetched.Contexts.Custom.Count);
var fetchedEntry = Assert.Single(fetched.Contexts.Custom);
Assert.Equal(contextKey, fetchedEntry.Key);
Assert.Equal(contextValue, fetchedEntry.Value.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Apis.Storage.v1.Data;
using Google.Cloud.ClientTesting;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using static Google.Cloud.Storage.V1.IntegrationTests.TestHelpers;

namespace Google.Cloud.Storage.V1.IntegrationTests
{
Expand Down Expand Up @@ -87,5 +91,37 @@ public async Task GetSoftDeleted()
Assert.Equal((ulong) _fixture.SmallContent.Length, softDeleted.Size);
Assert.NotNull(softDeleted.SoftDeleteTimeDateTimeOffset);
}

[Fact]
public void GetObjectContexts()
{
string contextKey = "A\u00F1\u03A9\U0001F680";
string contextValue = "Ab\u00F1\u03A9\U0001F680";
var custom = new Dictionary<string, ObjectCustomContextPayload>
{
{ contextKey, new ObjectCustomContextPayload { Value = contextValue } }
};

var destination = new Object
{
Bucket = _fixture.MultiVersionBucket,
Name = IdGenerator.FromGuid(),
ContentType = "test/type",
ContentDisposition = "attachment",
Metadata = new Dictionary<string, string> { { "x", "y" } },
Contexts = new Object.ContextsData { Custom = custom }
};
var source = GenerateData(100);
var result = _fixture.Client.UploadObject(destination, source);


var obj = _fixture.Client.GetObject(_fixture.MultiVersionBucket, destination.Name);
Assert.Equal(_fixture.MultiVersionBucket, obj.Bucket);
Assert.NotNull(obj.Contexts);
Assert.Equal(obj.Contexts.Custom.Count, result.Contexts.Custom.Count);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current assertion only checks the count of custom contexts. To ensure the object context was correctly retrieved, you should also assert that the actual key-value pairs within obj.Contexts.Custom match those in result.Contexts.Custom. This provides a more comprehensive validation of the retrieved context data.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current assertion only checks if the number of custom context entries is the same. This is a weak assertion as it doesn't verify the actual content (keys and values). A more robust test would compare the fetched context with the original data to ensure they are identical.

            Assert.Equal(custom, obj.Contexts.Custom);

var resultEntry = Assert.Single(obj.Contexts.Custom);
Assert.Equal(contextKey, resultEntry.Key);
Assert.Equal(contextValue, resultEntry.Value.Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Apis.Storage.v1.Data;
using Google.Cloud.ClientTesting;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using static Google.Cloud.Storage.V1.IntegrationTests.TestHelpers;
using Object = Google.Apis.Storage.v1.Data.Object;

namespace Google.Cloud.Storage.V1.IntegrationTests
Expand Down Expand Up @@ -121,6 +125,34 @@ public void PartialResponses()
}
}

[Fact]
public void ListObjectsMatchingContextKeyValuePair()
{
string contextKey = "A\u00F1\u03A9\U0001F680";
string contextValue = "Ab\u00F1\u03A9\U0001F680";
var custom = new Dictionary<string, ObjectCustomContextPayload>
{
{ contextKey, new ObjectCustomContextPayload { Value = contextValue } }

};
var destination = new Object
{
Bucket = _fixture.ReadBucket,
Name = IdGenerator.FromGuid(),
Contexts = new Object.ContextsData { Custom = custom }
};

var source = GenerateData(100);
_fixture.Client.UploadObject(destination, source);
string filter = $@"contexts.""{contextKey}""=""{contextValue}""";
var options = new ListObjectsOptions { Filter = filter };
var objects = _fixture.Client.ListObjects(_fixture.ReadBucket, options: options).ToList();
var obj = Assert.Single(objects);
var fetchedContext = Assert.Single(obj.Contexts.Custom);
Assert.Equal(contextKey, fetchedContext.Key);
Assert.Equal(contextValue, fetchedContext.Value.Value);
}

private async Task AssertObjects(string prefix, ListObjectsOptions options, params string[] expectedNames)
{
IEnumerable<Object> actual = _fixture.Client.ListObjects(_fixture.ReadBucket, prefix, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Apis.Storage.v1.Data;
using Google.Cloud.ClientTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using static Google.Cloud.Storage.V1.IntegrationTests.TestHelpers;
using Object = Google.Apis.Storage.v1.Data.Object;

namespace Google.Cloud.Storage.V1.IntegrationTests
Expand Down Expand Up @@ -50,5 +54,35 @@ public void MissingProperties()
Assert.Throws<ArgumentException>(() => client.PatchObject(new Object { Bucket = _fixture.SingleVersionBucket }));
Assert.Throws<ArgumentException>(() => client.PatchObject(new Object { Name = IdGenerator.FromGuid() }));
}

[Fact]
public void ClearAllObjectContexts()
{
var client = _fixture.Client;
var custom = new Dictionary<string, ObjectCustomContextPayload>
{
{ "A\u00F1\u03A9\U0001F680", new ObjectCustomContextPayload { Value = "Ab\u00F1\u03A9\U0001F680" } }
};

var destination = new Object
{
Bucket = _fixture.SingleVersionBucket,
Name = IdGenerator.FromGuid(),
ContentType = "test/type",
ContentDisposition = "attachment",
Metadata = new Dictionary<string, string> { { "x", "y" } },
Contexts = new Object.ContextsData { Custom = custom }
};
var source = GenerateData(100);
_fixture.Client.UploadObject(destination, source);

var modifiedCustom = new Dictionary<string, ObjectCustomContextPayload>
{
};

Object obj = new Object { Name = destination.Name, Bucket = destination.Bucket, ContentType = "text/plain", Contexts = new Object.ContextsData { Custom = modifiedCustom } };
var updated = client.PatchObject(obj);
Assert.Null(updated.Contexts);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Apis.Storage.v1.Data;
using Google.Cloud.ClientTesting;
using System.Collections.Generic;
using System.IO;
using Xunit;
using static Google.Cloud.Storage.V1.IntegrationTests.TestHelpers;

namespace Google.Cloud.Storage.V1.IntegrationTests
{
Expand All @@ -39,5 +41,50 @@ public void Success()
var updated = client.UpdateObject(obj);
Assert.Equal("value", updated.Metadata["key"]);
}

[Fact]
public void UpdateObjectWithObjectContexts()
{
var client = _fixture.Client;
var source = GenerateData(100);
string contextKey = "A\u00F1\u03A9\U0001F680";
string contextValue = "Ab\u00F1\u03A9\U0001F680";
var custom = new Dictionary<string, ObjectCustomContextPayload>
{
{ contextKey, new ObjectCustomContextPayload { Value = contextValue } }
};

var destination = new Object
{
Bucket = _fixture.MultiVersionBucket,
Name = IdGenerator.FromGuid(),
ContentType = "test/type",
ContentDisposition = "attachment",
Metadata = new Dictionary<string, string> { { "x", "y" } },
Contexts = new Object.ContextsData { Custom = custom }
};

var result = client.UploadObject(destination, source);
string modifiedContextValue = "AAb\u00F1\u03A9\U0001F680";
var modifiedCustom = new Dictionary<string, ObjectCustomContextPayload>
{
{ contextKey, new ObjectCustomContextPayload { Value = modifiedContextValue } }

};

var modifiedDestination = new Object
{
Bucket = _fixture.MultiVersionBucket,
Name = destination.Name,
Contexts = new Object.ContextsData { Custom = modifiedCustom }
};
var updated = client.UpdateObject(modifiedDestination);
Assert.NotNull(updated.Contexts.Custom);
Assert.Equal(modifiedCustom.Count, updated.Contexts.Custom.Count);
var resultEntry = Assert.Single(result.Contexts.Custom);
var updatedEntry = Assert.Single(updated.Contexts.Custom);
Assert.Equal(modifiedContextValue, updatedEntry.Value.Value);
Assert.NotEqual(updatedEntry.Value.UpdateTimeRaw, resultEntry.Value.UpdateTimeRaw);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
Expand Down Expand Up @@ -91,6 +92,33 @@ public void UploadWithObject()
ValidateData(_fixture.MultiVersionBucket, destination.Name, source);
}

[Fact]
public void UploadObjectWithObjectContexts()
{
string contextKey = "A\u00F1\u03A9\U0001F680";
string contextValue = "Ab\u00F1\u03A9\U0001F680";
var custom = new Dictionary<string, ObjectCustomContextPayload>
{
{ contextKey, new ObjectCustomContextPayload { Value = contextValue } }
};
var destination = new Object
{
Bucket = _fixture.MultiVersionBucket,
Name = IdGenerator.FromGuid(),
Contexts = new Object.ContextsData { Custom = custom }
};
var source = GenerateData(100);
var result = _fixture.Client.UploadObject(destination, source);
Assert.Equal(destination.Name, result.Name);
Assert.Equal(destination.Bucket, result.Bucket);
var resultEntry = Assert.Single(result.Contexts.Custom);
Assert.Equal(contextKey, resultEntry.Key);
Assert.Equal(contextValue, resultEntry.Value.Value);
Assert.NotNull(resultEntry.Value.CreateTimeRaw);
Assert.NotNull(resultEntry.Value.UpdateTimeRaw);
ValidateData(_fixture.MultiVersionBucket, destination.Name, source);
}

[Fact]
public async Task UploadAsyncWithProgress()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void ModifyRequest_DefaultOptions()
Assert.Null(request.StartOffset);
Assert.Null(request.EndOffset);
Assert.Null(request.MatchGlob);
Assert.Null(request.Filter);
}

[Fact]
Expand All @@ -58,7 +59,8 @@ public void ModifyRequest_AllOptions()
Fields = "items(name),nextPageToken",
StartOffset = "start",
EndOffset = "end",
MatchGlob = "a/*.txt"
MatchGlob = "a/*.txt",
Filter = "contexts.\"key\":*\""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The filter string contexts."key":*" appears to have a typo with an extra double quote at the end. Based on the filter syntax, it should likely be contexts."key":*.

                Filter = "contexts.\"key\":*"

};
options.ModifyRequest(request);
Assert.Equal(10, request.MaxResults);
Expand All @@ -74,6 +76,7 @@ public void ModifyRequest_AllOptions()
Assert.Equal("start", request.StartOffset);
Assert.Equal("end", request.EndOffset);
Assert.Equal("a/*.txt", request.MatchGlob);
Assert.Equal("contexts.\"key\":*\"", request.Filter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ public sealed class ListObjectsOptions
/// </summary>
public string EndOffset { get; set; }

/// <summary>
/// If set, filters results to include only objects to which the specified context is attached.
/// If delimiter is set, the returned prefixes are exempt from this filter.
/// </summary>
public string Filter { get; set; }

/// <summary>
/// Options to pass custom retry configuration for each API request.
/// </summary>
Expand Down Expand Up @@ -169,6 +175,10 @@ internal void ModifyRequest(ListRequest request)
{
request.MatchGlob = MatchGlob;
}
if (Filter != null)
{
request.Filter = Filter;
}
}
}
}
Loading