public async Task DigitalTwins_DeleteTwinSucceedsIfMatchProvidesCorrectEtag() { DigitalTwinsClient client = GetClient(); string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); try { // arrange // create room model string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); await CreateAndListModelsAsync(client, new List <string> { roomModel }).ConfigureAwait(false); // act // create room twin BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); // update twin JsonPatchDocument updateTwinPatchDocument = new JsonPatchDocument(); updateTwinPatchDocument.AppendAdd("/Humidity", 30); updateTwinPatchDocument.AppendReplace("/Temperature", 70); updateTwinPatchDocument.AppendRemove("/EmployeeId"); await client.UpdateDigitalTwinAsync(roomTwinId, updateTwinPatchDocument, ETag.All).ConfigureAwait(false); // get twin ETag?correctETag = (await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomTwinId).ConfigureAwait(false)).Value.ETag; Assert.IsNotNull(correctETag); try { // since the ETag is not out of date, this call should not throw a 412 await client.DeleteDigitalTwinAsync(roomTwinId, correctETag).ConfigureAwait(false); } catch (RequestFailedException ex) when(ex.Status == (int)HttpStatusCode.PreconditionFailed) { throw new AssertionException("UpdateRelationship should not have thrown PreconditionFailed because the ETag was up to date", ex); } } catch (Exception ex) { Assert.Fail($"Failure in executing a step in the test case: {ex.Message}."); } finally { // cleanup try { if (!string.IsNullOrWhiteSpace(roomModelId)) { await client.DeleteModelAsync(roomModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public static async Task Main(string[] args) { Console.WriteLine("Hello World!"); // Create the Azure Digital Twins client for API calls string adtInstanceUrl = "https://<your-instance-hostname>"; var credentials = new DefaultAzureCredential(); var client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credentials); Console.WriteLine($"Service client created – ready to go"); // Upload models Console.WriteLine($"Upload a model"); string dtdl = File.ReadAllText("<path-to>/Room.json"); var models = new List <string> { dtdl }; // Upload the model to the service await client.CreateModelsAsync(models); // Create new digital twin // <CreateTwin_withHelper> string twinId = "myTwinID"; var initData = new BasicDigitalTwin { Id = twinId, Metadata = { ModelId = "dtmi:example:Room;1" }, // Initialize properties Contents = { { "Temperature", 25.0 }, { "Humidity", 50.0 }, }, }; // <CreateTwinCall> await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(twinId, initData); // </CreateTwinCall> // </CreateTwin_withHelper> Console.WriteLine("Twin created successfully"); //Print twin Console.WriteLine("--- Printing twin details:"); await CustomMethod_FetchAndPrintTwinAsync(twinId, client); Console.WriteLine("--------"); //Update twin data var updateTwinData = new JsonPatchDocument(); updateTwinData.AppendAdd("/Temperature", 30.0); // <UpdateTwinCall> await client.UpdateDigitalTwinAsync(twinId, updateTwinData); // </UpdateTwinCall> Console.WriteLine("Twin properties updated"); Console.WriteLine(); //Print twin again Console.WriteLine("--- Printing twin details (after update):"); await CustomMethod_FetchAndPrintTwinAsync(twinId, client); Console.WriteLine("--------"); Console.WriteLine(); //Delete twin await CustomMethod_DeleteTwinAsync(client, twinId); }
public async Task DigitalTwins_DeleteTwinFailsIfMatchProvidesOutdatedEtag() { DigitalTwinsClient client = GetClient(); string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); try { // arrange // create room model string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); await CreateAndListModelsAsync(client, new List <string> { roomModel }).ConfigureAwait(false); // act // create room twin BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); // get twin ETag?etagBeforeUpdate = (await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomTwinId).ConfigureAwait(false)).Value.ETag; // update twin JsonPatchDocument updateTwinPatchDocument = new JsonPatchDocument(); updateTwinPatchDocument.AppendAdd("/Humidity", 30); updateTwinPatchDocument.AppendReplace("/Temperature", 70); updateTwinPatchDocument.AppendRemove("/EmployeeId"); await client.UpdateDigitalTwinAsync(roomTwinId, updateTwinPatchDocument, ETag.All).ConfigureAwait(false); // assert Func <Task> act = async() => { // since the ETag is out of date, this call should throw a 412 await client.DeleteDigitalTwinAsync(roomTwinId, etagBeforeUpdate).ConfigureAwait(false); }; act.Should().Throw <RequestFailedException>() .And.Status.Should().Be((int)HttpStatusCode.PreconditionFailed); } catch (Exception ex) { Assert.Fail($"Failure in executing a step in the test case: {ex.Message}."); } finally { // cleanup try { if (!string.IsNullOrWhiteSpace(roomModelId)) { await client.DeleteModelAsync(roomModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }