public async Task ShouldSetCustomTypeAsync() { ResourceIdentifier typeResourceIdentifier = new ResourceIdentifier { Id = _testType.Id, TypeId = commercetools.Common.ReferenceType.Type }; string fieldName = _testType.FieldDefinitions[0].Name; JObject fields = new JObject(); fields.Add(fieldName, "Here is the value of my field."); SetCustomTypeAction setCustomTypeAction = new SetCustomTypeAction { Type = typeResourceIdentifier, Fields = fields }; Response <Cart> cartResponse = await _client.Carts().UpdateCartAsync(_testCarts[1], setCustomTypeAction); Assert.IsTrue(cartResponse.Success); _testCarts[1] = cartResponse.Result; Assert.NotNull(_testCarts[1].Custom.Fields); Assert.AreEqual(fields[fieldName], _testCarts[1].Custom.Fields[fieldName]); }
public async Task ShouldSetCustomTypeAndSetCustomFieldAsync() { // Arrange, create a type TypeDraft typeDraft = Helper.GetTypeDraftForInventory(_project); Task <Response <Type> > typeTask = _client.Types().CreateTypeAsync(typeDraft); typeTask.Wait(); Assert.IsTrue(typeTask.Result.Success, "CreateType failed"); Type testType = typeTask.Result.Result; ResourceIdentifier typeResourceIdentifier = new ResourceIdentifier { Id = testType.Id, TypeId = commercetools.Common.ReferenceType.Type }; string fieldName = testType.FieldDefinitions[0].Name; JObject fields = new JObject(); fields.Add(fieldName, Helper.GetRandomString(10)); SetCustomTypeAction setCustomTypeAction = new SetCustomTypeAction { Type = typeResourceIdentifier, Fields = fields, }; // Act 1, try set custom type Response <InventoryEntry> responseCustomType = await _client.Inventories().UpdateInventoryEntryAsync(_testInventories[0], setCustomTypeAction); // Assert Assert.IsTrue(responseCustomType.Success); _testInventories[0] = responseCustomType.Result; Assert.NotNull(_testInventories[0].Custom.Fields); Assert.AreEqual(fields[fieldName], _testInventories[0].Custom.Fields[fieldName]); // Arrange, change value fields[fieldName] = Helper.GetRandomString(10); SetCustomFieldAction setCustomFieldAction = new SetCustomFieldAction(fieldName); setCustomFieldAction.Value = fields[fieldName]; // Act 2, try update custom field Response <InventoryEntry> cartResponse = await _client.Inventories().UpdateInventoryEntryAsync(_testInventories[0], setCustomFieldAction); // Assert Assert.IsTrue(cartResponse.Success); _testInventories[0] = cartResponse.Result; Assert.NotNull(_testInventories[0].Custom.Fields); Assert.AreEqual(fields[fieldName], _testInventories[0].Custom.Fields[fieldName]); }