public async Task CreateAndDeleteAssetWorkAsExpectedAsync() { // Arrange var externalIdString = Guid.NewGuid().ToString(); var newAsset = new AssetCreate { ExternalId = externalIdString, Name = "Create Assets c# sdk test", Description = "Just a test" }; var deletes = new AssetDelete { Items = new List <Identity>() { Identity.Create(externalIdString) } }; // Act var res = await WriteClient.Assets.CreateAsync(new List <AssetCreate>() { newAsset }); await WriteClient.Assets.DeleteAsync(deletes); // Assert var resCount = res.Count(); Assert.True(1 == resCount, $"Expected 1 created asset but got {resCount}"); Assert.True(externalIdString == res.First().ExternalId, "Created externalId doesnt match expected"); }
public async Task UpdatedAssetsPerformsExpectedChangesAsync() { // Arrange var externalIdString = Guid.NewGuid().ToString(); var newMetadata = new Dictionary <string, string>() { { "key1", "value1" }, { "key2", "value2" } }; var newAsset = new AssetCreate { ExternalId = externalIdString, Name = "Update Assets c# sdk test", Description = "Just a test", Metadata = new Dictionary <string, string>() { { "oldkey1", "oldvalue1" }, { "oldkey2", "oldvalue2" } } }; var newName = "Updated update asset"; var update = new List <AssetUpdateItem> { new AssetUpdateItem(externalId: externalIdString) { Update = new AssetUpdate() { Name = new Update <string>(newName), Metadata = new UpdateDictionary <string>(add: newMetadata, remove: new List <string> { "oldkey1" }) } } }; // Act await WriteClient.Assets.CreateAsync(new List <AssetCreate>() { newAsset }); await WriteClient.Assets.UpdateAsync(update); var getRes = await WriteClient.Assets.RetrieveAsync(new List <string>() { externalIdString }); await WriteClient.Assets.DeleteAsync(new List <string>() { externalIdString }); // Assert var resCount = getRes.Count(); Assert.True(resCount == 1, $"Expected a single Asset but got {resCount}"); var resAsset = getRes.First(); Assert.True(externalIdString == resAsset.ExternalId, $"Asset doest have expected ExternalId. Was '{resAsset.ExternalId}' but expected '{externalIdString}'"); Assert.True(newName == resAsset.Name, $"Expected the Asset name to update to '{newName}' but was '{resAsset.Name}'"); Assert.True(resAsset.Metadata.ContainsKey("key1") && resAsset.Metadata.ContainsKey("key2"), "Asset wasnt update with new metadata values"); Assert.True(resAsset.Metadata.ContainsKey("oldkey2") && !resAsset.Metadata.ContainsKey("oldkey1"), "Asset update changed unintended metadata values"); }
public async Task UpdatedAssetsLabelPerformsExpectedChangesAsync() { // Arrange var externalIdString = Guid.NewGuid().ToString(); var initialAsset = new AssetCreate { ExternalId = externalIdString, Name = "Update Assets Label c# sdk test", Description = "Just a test", Labels = new List <CogniteExternalId> { new CogniteExternalId("AssetTestUpdateLabel1") } }; var newName = "Updated asset name"; var newLabels = new List <CogniteExternalId> { new CogniteExternalId("AssetTestUpdateLabel2") }; var update = new List <AssetUpdateItem> { new AssetUpdateItem(externalId: externalIdString) { Update = new AssetUpdate() { Name = new Update <string>(newName), Labels = new UpdateLabels <IEnumerable <CogniteExternalId> >(addLabels: newLabels, removeLabels: new List <CogniteExternalId> { new CogniteExternalId("AssetTestUpdateLabel1") }) } } }; // Act _ = await WriteClient.Assets.CreateAsync(new List <AssetCreate>() { initialAsset }).ConfigureAwait(false); await WriteClient.Assets.UpdateAsync(update); var getRes = await WriteClient.Assets.RetrieveAsync(new List <string>() { externalIdString }); await WriteClient.Assets.DeleteAsync(new List <string>() { externalIdString }); // Assert var resCount = getRes.Count(); Assert.True(resCount == 1, $"Expected a single Asset but got {resCount}"); var resAsset = getRes.First(); Assert.True(externalIdString == resAsset.ExternalId, $"Asset doest have expected ExternalId. Was '{resAsset.ExternalId}' but expected '{externalIdString}'"); Assert.True(resAsset.Labels.Count() == 1, $"Expected asset to have one label but was '{resAsset.Labels.Count()}'"); Assert.True(resAsset.Labels.ElementAt(0).ExternalId == newLabels.ElementAt(0).ExternalId, $"Expected label to be '{newLabels.ElementAt(0).ExternalId}' but was '{resAsset.Labels.ElementAt(0).ExternalId}'"); }
static async Task CreateAssetsExample(Client client, string externalId, string name) { var asset = new AssetCreate { ExternalId = externalId, Name = name }; var assets = new List <AssetCreate> { asset }; var result = await client.Assets.CreateAsync(assets).ConfigureAwait(false); var newAsset = result.FirstOrDefault(); Console.WriteLine(newAsset.Name); }