Skip to content

Commit

Permalink
feat: add Id to MetadataTemplateField (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwwoda authored Feb 17, 2023
1 parent 100b722 commit b7fe214
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Box.V2.Exceptions;
using Box.V2.Models;
Expand Down Expand Up @@ -61,11 +62,10 @@ public async Task CreateRetentionPolicyAsync_WithDescription_ShouldSucceed()
}

[TestMethod]
public async Task CreateRetentionPolicyAssignmentAsync_WithStartDateField_ShouldSuccess()
public async Task CreateRetentionPolicyAssignmentAsync_WithUploadedDateStartField_ShouldSuccess()
{
var retentionPolicy = await CreateRetentionPolicy();
var metadataFields = new Dictionary<string, object> { { "upload_date", "2024-02-14" } };
var metadataTemplate = await CreateMetadataTemplate(metadataFields);
var metadataTemplate = await CreateMetadataTemplate();
var policyAssignmentReq = new BoxRetentionPolicyAssignmentRequest()
{
PolicyId = retentionPolicy.Id,
Expand All @@ -76,6 +76,42 @@ public async Task CreateRetentionPolicyAssignmentAsync_WithStartDateField_Should
},
StartDateField = "upload_date"
};

var policyAssignment = await AdminClient.RetentionPoliciesManager.CreateRetentionPolicyAssignmentAsync(policyAssignmentReq);
Assert.AreEqual(policyAssignmentReq.StartDateField, policyAssignment.StartDateField);

var result = await AdminClient.RetentionPoliciesManager.DeleteRetentionPolicyAssignmentAsync(policyAssignment.Id);
Assert.IsTrue(result);
}

[TestMethod]
public async Task CreateRetentionPolicyAssignmentAsync_WithCustomStartDateField_ShouldSuccess()
{
var retentionPolicy = await CreateRetentionPolicy();
var customFieldName = "custom_field";
var metadataFields = new List<BoxMetadataTemplateField>()
{
new BoxMetadataTemplateField
{
Key = customFieldName,
DisplayName = customFieldName,
Type = "date"
}
};

var metadataTemplate = await CreateMetadataTemplate(metadataFields);

var policyAssignmentReq = new BoxRetentionPolicyAssignmentRequest()
{
PolicyId = retentionPolicy.Id,
AssignTo = new BoxRequestEntity()
{
Id = metadataTemplate.Id,
Type = BoxType.metadata_template
},
StartDateField = metadataTemplate.Fields.First(x => x.Key == customFieldName).Id
};

var policyAssignment = await AdminClient.RetentionPoliciesManager.CreateRetentionPolicyAssignmentAsync(policyAssignmentReq);
Assert.AreEqual(policyAssignmentReq.StartDateField, policyAssignment.StartDateField);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading.Tasks;
using Box.V2.Models;
using Box.V2.Models.Request;
Expand Down Expand Up @@ -29,31 +28,8 @@ public async Task<string> Execute(IBoxClient client)
DispositionAction = DispositionAction.permanently_delete.ToString(),
RetentionType = BoxRetentionType.modifiable
};
try
{
var response = await client.RetentionPoliciesManager.CreateRetentionPolicyAsync(retentionPolicyRequest);
Policy = response;
}
catch (Exception ex)
{
// TODO: 12-09-2022, @mcong
// There is an error on backend side, which will return 409 status code "conflict"
// but retention policy still created.
// Delete this try-catch after the issue is fixed.
var policies = await client.RetentionPoliciesManager.GetRetentionPoliciesAsync(_policyName);
if (policies.Entries.Count == 1)
{
// Retention policy already created.
var policy = policies.Entries[0];
var response = await client.RetentionPoliciesManager.GetRetentionPolicyAsync(policy.Id);
Policy = response;
}
else
{
throw ex;
}

}
var response = await client.RetentionPoliciesManager.CreateRetentionPolicyAsync(retentionPolicyRequest);
Policy = response;
PolicyId = Policy.Id;

if (_folderId != null)
Expand All @@ -80,17 +56,8 @@ public async Task Dispose(IBoxClient client)
{
Status = "retired"
};
try
{
await client.RetentionPoliciesManager.UpdateRetentionPolicyAsync(PolicyId, retentionPolicyRequest);
}
catch
{
// TODO: 12-09-2022, @mcong
// There is an error on backend side, which will return 500 status code "Internal Server Error"
// but retention policy still updated.
// Delete this try-catch after the issue is fixed.
}

await client.RetentionPoliciesManager.UpdateRetentionPolicyAsync(PolicyId, retentionPolicyRequest);
}
}
}
4 changes: 2 additions & 2 deletions Box.V2.Test.Integration/Configuration/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ public static async Task<BoxRetentionPolicy> CreateRetentionPolicy(string folder
return createRetentionPolicyCommand.Policy;
}

public static async Task<BoxMetadataTemplate> CreateMetadataTemplate(Dictionary<string, object> metadata = null,
public static async Task<BoxMetadataTemplate> CreateMetadataTemplate(List<BoxMetadataTemplateField> fields = null,
CommandScope commandScope = CommandScope.Test, CommandAccessLevel accessLevel = CommandAccessLevel.Admin)
{
var createMetadataTemplateCommand = new CreateMetadataTemplateCommand(GetUniqueName("template_key", false), ToStringMetadataFields(metadata), commandScope, accessLevel);
var createMetadataTemplateCommand = new CreateMetadataTemplateCommand(GetUniqueName("template_key", false), fields, commandScope, accessLevel);
await ExecuteCommand(createMetadataTemplateCommand);

return createMetadataTemplateCommand.MetadataTemplate;
Expand Down
3 changes: 3 additions & 0 deletions Box.V2.Test/Box.V2.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<None Update="Fixtures\BoxFolders\CreateFolderSharedLink200.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fixtures\BoxMetadata\CreateMetadataTemplate200.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fixtures\BoxMetadata\ExecuteMetadataQuery200.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
51 changes: 51 additions & 0 deletions Box.V2.Test/BoxMetadataManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,5 +634,56 @@ public async Task ExecuteMetadataQueryWithoutUseIndexWithFields_ValidResponse()
var file = (BoxFile)items.Entries[0];
Assert.AreEqual(file.Metadata["enterprise_67890"]["catalogImages"]["photographer"].Value, "Bob Dylan");
}

[TestMethod]
public async Task CreateMetadataTemplate_ValidResponse()
{
/*** Arrange ***/
IBoxRequest boxRequest = null;
Handler.Setup(h => h.ExecuteAsync<BoxMetadataTemplate>(It.IsAny<IBoxRequest>()))
.Returns(Task.FromResult<IBoxResponse<BoxMetadataTemplate>>(new BoxResponse<BoxMetadataTemplate>()
{
Status = ResponseStatus.Success,
ContentString = LoadFixtureFromJson("Fixtures/BoxMetadata/CreateMetadataTemplate200.json")
}))
.Callback<IBoxRequest>(r => boxRequest = r);

/*** Act ***/
var metadataRequest = new BoxMetadataTemplate()
{
TemplateKey = "ProductInfo",
DisplayName = "Product Info",
Scope = "enterprise_123456",
Fields = new List<BoxMetadataTemplateField>()
{
new BoxMetadataTemplateField()
{
Type = "string",
Key = "category",
DisplayName = "Category",
Options = new List<BoxMetadataTemplateFieldOption>()
{
new BoxMetadataTemplateFieldOption() { Key = "Category 1" },
}
},
}
};

var template = await _metadataManager.CreateMetadataTemplate(metadataRequest);

/*** Assert ***/
Assert.IsNotNull(boxRequest);
Assert.AreEqual(RequestMethod.Post, boxRequest.Method);

Assert.AreEqual(template.Type, "metadata_template");
Assert.AreEqual(template.Id, "58063d82-4128-7b43-bba9-92f706befcdf");
Assert.AreEqual(template.TemplateKey, "productInfo");
Assert.AreEqual(template.Scope, "enterprise_123456");
Assert.AreEqual(template.Fields[0].Id, "822227e0-47a5-921b-88a8-494760b2e6d2");
Assert.AreEqual(template.Fields[0].Key, "category");
Assert.AreEqual(template.Fields[0].DisplayName, "Category");
Assert.AreEqual(template.Fields[0].Type, "string");
Assert.AreEqual(template.Fields[0].Options[0].Key, "Category 1");
}
}
}
25 changes: 25 additions & 0 deletions Box.V2.Test/Fixtures/BoxMetadata/CreateMetadataTemplate200.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": "58063d82-4128-7b43-bba9-92f706befcdf",
"type": "metadata_template",
"copyInstanceOnItemCopy": true,
"displayName": "Product Info",
"fields": [
{
"type": "string",
"key": "category",
"displayName": "Category",
"description": "The category",
"hidden": true,
"options": [
{
"key": "Category 1",
"id": "45dc2849-a4a7-40a9-a751-4a699a589190"
}
],
"id": "822227e0-47a5-921b-88a8-494760b2e6d2"
}
],
"hidden": true,
"scope": "enterprise_123456",
"templateKey": "productInfo"
}
7 changes: 7 additions & 0 deletions Box.V2/Models/BoxMetadataTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ public class BoxMetadataTemplate : BoxEntity
/// </summary>
public class BoxMetadataTemplateField
{
public const string FieldId = "id";
public const string FieldType = "type";
public const string FieldKey = "key";
public const string FieldDisplayName = "displayName";
public const string FieldOptions = "options";
public const string FieldHidden = "hidden";

/// <summary>
/// The unique ID of the metadata template field.
/// </summary>
[JsonProperty(PropertyName = FieldId)]
public virtual string Id { get; set; }

/// <summary>
/// The data type of the field's value. Currently, there are four attributes supported by templates: string, enum, float, and date (RFC 3339).
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion Box.V2/Models/BoxRetentionPolicyAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public class BoxRetentionPolicyAssignment : BoxEntity
public virtual List<BoxMetadataFieldFilter> FilterFields { get; set; }

/// <summary>
/// The Metadata field which will be used to specify the start date for the retention policy.
/// Id of Metadata field which will be used to specify the start date for the retention policy.
/// Alternatively, pass "upload_date" as value to use the date when the file was uploaded to Box.
/// </summary>
[JsonProperty(PropertyName = FieldStartDateField)]
public virtual string StartDateField { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion Box.V2/Models/Request/BoxRetentionPolicyAssignmentRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class BoxRetentionPolicyAssignmentRequest
public List<object> FilterFields { get; set; }

/// <summary>
/// The Metadata field which will be used to specify the start date for the retention policy
/// Id of Metadata field which will be used to specify the start date for the retention policy.
/// Alternatively, pass "upload_date" as value to use the date when the file was uploaded to Box.
/// </summary>
[JsonProperty(PropertyName = "start_date_field")]
public string StartDateField { get; set; }
Expand Down

0 comments on commit b7fe214

Please sign in to comment.