public async Task DigitalTwins_Lifecycle() { DigitalTwinsClient client = GetClient(); string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetSettings.RoomTwinIdPrefix).ConfigureAwait(false); string floorModelId = await GetUniqueModelIdAsync(client, TestAssetSettings.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(client, TestAssetSettings.RoomModelIdPrefix).ConfigureAwait(false); try { // arrange // create room model string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); await client.CreateModelsAsync(new List <string> { roomModel }).ConfigureAwait(false); // act // create room twin string roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateDigitalTwinAsync(roomTwinId, roomTwin).ConfigureAwait(false); // get twin await client.GetDigitalTwinAsync(roomTwinId).ConfigureAwait(false); // update twin string updateTwin = TestAssetsHelper.GetRoomTwinUpdatePayload(); await client.UpdateDigitalTwinAsync(roomTwinId, updateTwin).ConfigureAwait(false); // delete a twin await client.DeleteDigitalTwinAsync(roomTwinId).ConfigureAwait(false); // assert Func <Task> act = async() => { await client.GetDigitalTwinAsync(roomTwinId).ConfigureAwait(false); }; act.Should().Throw <RequestFailedException>() .And.Status.Should().Be((int)HttpStatusCode.NotFound); } 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 async Task Models_Lifecycle() { DigitalTwinsClient client = GetClient(); string buildingModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.BuildingModelId).ConfigureAwait(false); string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelId).ConfigureAwait(false); string hvacModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.HvacModelId).ConfigureAwait(false); string wardModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.WardModelId).ConfigureAwait(false); try { string modelBuilding = TestAssetsHelper.GetBuildingModelPayload(buildingModelId, hvacModelId, floorModelId); string modelHvac = TestAssetsHelper.GetHvacModelPayload(hvacModelId, floorModelId); string modelWard = TestAssetsHelper.GetWardModelPayload(wardModelId); // CREATE models var modelsList = new List <string> { modelBuilding, modelHvac, modelWard }; await CreateAndListModelsAsync(client, modelsList).ConfigureAwait(false); // GET one created model Response <DigitalTwinsModelData> buildingModel = await client.GetModelAsync(buildingModelId).ConfigureAwait(false); Console.WriteLine($"Got {buildingModelId} as {buildingModel.Value.DtdlModel}"); // LIST all models AsyncPageable <DigitalTwinsModelData> models = client.GetModelsAsync(); await foreach (DigitalTwinsModelData model in models) { Console.WriteLine($"{model.Id}"); } // DECOMMISSION a model await client.DecommissionModelAsync(buildingModelId).ConfigureAwait(false); } catch (Exception ex) { Assert.Fail($"Failure in executing a step in the test case: {ex.Message}."); } finally { // Test DELETE all models. try { await client.DeleteModelAsync(buildingModelId).ConfigureAwait(false); await client.DeleteModelAsync(hvacModelId).ConfigureAwait(false); await client.DeleteModelAsync(wardModelId).ConfigureAwait(false); } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task DigitalTwins_CreateOrReplaceTwinFailsWhenIfNoneMatchStar() { 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); // act Func <Task> act = async() => { // "ifNoneMatch = *" header should cause the server to throw 412 since an entity does match await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomTwinId, roomTwin, ETag.All).ConfigureAwait(false); }; // assert 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}"); } } }
public async Task Query_GetTwinAliasing() { DigitalTwinsClient client = GetClient(); 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); // Create a room twin, with property "IsOccupied": true string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateOrReplaceDigitalTwinAsync(roomTwinId, roomTwin).ConfigureAwait(false); await WaitIfLiveAsync(TimeSpan.FromSeconds(10)); // Use aliasing in the query to test deserialization when each digital twin in the response will be wrapped by the alias name. string queryString = $"SELECT D FROM DIGITALTWINS D"; // act AsyncPageable <AliasedBasicDigitalTwin> asyncPageableResponse = client.QueryAsync <AliasedBasicDigitalTwin>(queryString); await foreach (AliasedBasicDigitalTwin twin in asyncPageableResponse) { twin.Twin.Id.Should().NotBeNull(); } } catch (Exception ex) { Assert.Fail($"Failure in executing a step in the test case: {ex.Message}."); } finally { // clean up try { if (!string.IsNullOrWhiteSpace(roomModelId)) { await client.DeleteModelAsync(roomModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task Query_ValidQuery_Success() { DigitalTwinsClient client = GetClient(); 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 client.CreateModelsAsync(new List <string> { roomModel }).ConfigureAwait(false); // Create a room twin, with property "IsOccupied": true string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); string roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateDigitalTwinAsync(roomTwinId, roomTwin).ConfigureAwait(false); string queryString = "SELECT * FROM digitaltwins where IsOccupied = true"; // act AsyncPageable <string> asyncPageableResponse = client.QueryAsync(queryString); // assert await foreach (string response in asyncPageableResponse) { JsonElement jsonElement = JsonSerializer.Deserialize <JsonElement>(response); JsonElement isOccupied = jsonElement.GetProperty("IsOccupied"); isOccupied.GetRawText().Should().Be("true"); } } finally { // clean up try { if (!string.IsNullOrWhiteSpace(roomModelId)) { await client.DeleteModelAsync(roomModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task DigitalTwins_CreateOrReplaceTwinSucceedsWithNoIfNoneMatchHeader() { 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); // Deliberately not passing in ifNoneMatch header, request should succeed because of that await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); } catch (RequestFailedException ex) when(ex.Status == (int)HttpStatusCode.PreconditionFailed) { throw new AssertionException("CreateOrReplaceDigitalTwin should not fail with PreconditionFailed when ifNoneMatch header wasn't set", 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}"); } } }
private async Task CreateModelsAndTwins(DigitalTwinsClient client, string wifiModelId, string roomWithWifiModelId, string wifiComponentName, string roomWithWifiTwinId) { // Generate the payload needed to create the WiFi component model. string wifiModel = TestAssetsHelper.GetWifiModelPayload(wifiModelId); // Generate the payload needed to create the room with WiFi model. string roomWithWifiModel = TestAssetsHelper.GetRoomWithWifiModelPayload(roomWithWifiModelId, wifiModelId, wifiComponentName); // Create the room and WiFi models. await client.CreateModelsAsync(new List <string> { roomWithWifiModel, wifiModel }).ConfigureAwait(false); // Generate the payload needed to create the room with WiFi twin. BasicDigitalTwin roomWithWifiTwin = TestAssetsHelper.GetRoomWithWifiTwinPayload(roomWithWifiModelId, wifiComponentName); // Create the room with WiFi component digital twin. await client.CreateDigitalTwinAsync <BasicDigitalTwin>(roomWithWifiTwinId, roomWithWifiTwin).ConfigureAwait(false); }
public async Task DigitalTwinOperationsWithCustomObjectSerializer_Succeeds() { // arrange var serializer = new TestObjectSerializer(); DigitalTwinsClientOptions options = new DigitalTwinsClientOptions { Serializer = serializer }; DigitalTwinsClient client = GetClient(options); serializer.WasDeserializeCalled.Should().BeFalse(); serializer.WasSerializeCalled.Should().BeFalse(); 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); // 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(roomTwinId, roomTwin).ConfigureAwait(false); roomTwin = await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomTwinId).ConfigureAwait(false); // assert roomTwin.Should().NotBeNull(); serializer.WasDeserializeCalled.Should().BeTrue(); serializer.WasSerializeCalled.Should().BeTrue(); }
public async Task ModelData_DisplayNameAndDescription_Deserializes() { // arrange DigitalTwinsClient client = GetClient(); string wardModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.WardModelId).ConfigureAwait(false); // add a model with a single value for displayName and for description, neither of which were defined as a map string modelWard = TestAssetsHelper.GetWardModelPayload(wardModelId); await client.CreateModelsAsync(new[] { modelWard }).ConfigureAwait(false); // act // should not throw on deserialization Response <DigitalTwinsModelData> wardModel = await client.GetModelAsync(wardModelId).ConfigureAwait(false); // assert wardModel.Value.DisplayName.Count.Should().Be(1, "Should have 1 entry for display name"); wardModel.Value.DisplayName.Keys.First().Should().Be("en"); }
public void PayloadHelper_BuildArrayPayload_ValidateArray() { // arrange string floorJson = TestAssetsHelper.GetFloorModelPayload( TestAssetSettings.Instance.FloorModelId, TestAssetSettings.Instance.RoomModelId, TestAssetSettings.Instance.HvacModelId); string roomJson = TestAssetsHelper.GetRoomModelPayload(TestAssetSettings.Instance.RoomModelId, TestAssetSettings.Instance.FloorModelId); var models = new List <string> { floorJson, roomJson }; // act string creationPayload = PayloadHelper.BuildArrayPayload(models); // assert creationPayload.Should().NotBeNullOrEmpty(); JsonDocument parsedJson = JsonDocument.Parse(creationPayload); parsedJson.RootElement.ValueKind.Should().Be(JsonValueKind.Array); parsedJson.RootElement.GetArrayLength().Should().Be(models.Count); parsedJson.RootElement[0].ToString().Should().Be(floorJson); parsedJson.RootElement[1].ToString().Should().Be(roomJson); }
public async Task Models_ModelAlreadyExists_ThrowsConflictException() { // arrange DigitalTwinsClient client = GetClient(); string wardModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.WardModelId).ConfigureAwait(false); string modelWard = TestAssetsHelper.GetWardModelPayload(wardModelId); var modelsList = new List <string> { modelWard }; // Create model once await client.CreateModelsAsync(modelsList).ConfigureAwait(false); // act Func <Task> act = async() => await client.CreateModelsAsync(modelsList).ConfigureAwait(false); // assert act.Should().Throw <RequestFailedException>() .And.Status.Should().Be((int)HttpStatusCode.Conflict); }
public async Task Component_Lifecycle() { // arrange DigitalTwinsClient client = GetClient(); string wifiModelId = await GetUniqueModelIdAsync(client, TestAssetSettings.WifiModelIdPrefix).ConfigureAwait(false); string roomWithWifiModelId = await GetUniqueModelIdAsync(client, TestAssetSettings.RoomWithWifiModelIdPrefix).ConfigureAwait(false); string roomWithWifiTwinId = await GetUniqueTwinIdAsync(client, TestAssetSettings.RoomWithWifiTwinIdPrefix).ConfigureAwait(false); string wifiComponentName = "wifiAccessPoint"; try { // CREATE // create roomWithWifi model string wifiModel = TestAssetsHelper.GetWifiModelPayload(wifiModelId); // create wifi model string roomWithWifiModel = TestAssetsHelper.GetRoomWithWifiModelPayload(roomWithWifiModelId, wifiModelId, wifiComponentName); await client.CreateModelsAsync(new List <string> { roomWithWifiModel, wifiModel }).ConfigureAwait(false); // create room digital twin string roomWithWifiTwin = TestAssetsHelper.GetRoomWithWifiTwinPayload(roomWithWifiModelId, wifiModelId, wifiComponentName); await client.CreateDigitalTwinAsync(roomWithWifiTwinId, roomWithWifiTwin); // Get the component Response <string> getComponentResponse = await client .GetComponentAsync( roomWithWifiTwinId, wifiComponentName) .ConfigureAwait(false); // The response to the GET request should be 200 (OK) getComponentResponse.GetRawResponse().Status.Should().Be((int)HttpStatusCode.OK); // Patch component Response <string> updateComponentResponse = await client .UpdateComponentAsync( roomWithWifiTwinId, wifiComponentName, TestAssetsHelper.GetWifiComponentUpdatePayload()) .ConfigureAwait(false); // The response to the Patch request should be 204 (No content) updateComponentResponse.GetRawResponse().Status.Should().Be((int)HttpStatusCode.NoContent); } finally { // clean up try { if (!string.IsNullOrWhiteSpace(roomWithWifiTwinId)) { await client.DeleteDigitalTwinAsync(roomWithWifiTwinId).ConfigureAwait(false); } if (!string.IsNullOrWhiteSpace(roomWithWifiModelId)) { await client.DeleteModelAsync(roomWithWifiModelId).ConfigureAwait(false); } if (!string.IsNullOrWhiteSpace(wifiModelId)) { await client.DeleteModelAsync(wifiModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task DigitalTwins_PatchTwinSucceedsIfCorrectETagProvided() { 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 once 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?etagBeforeUpdate = (await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomTwinId).ConfigureAwait(false)).Value.ETag; Assert.IsNotNull(etagBeforeUpdate); // update twin again, but with the correct etag JsonPatchDocument secondUpdateTwinPatchDocument = new JsonPatchDocument(); secondUpdateTwinPatchDocument.AppendReplace("/Humidity", 80); try { await client.UpdateDigitalTwinAsync(roomTwinId, secondUpdateTwinPatchDocument, etagBeforeUpdate).ConfigureAwait(false); } catch (RequestFailedException ex) when(ex.Status == (int)HttpStatusCode.PreconditionFailed) { throw new AssertionException("UpdateDigitalTwin 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 async Task Query_ValidQuery_Success() { DigitalTwinsClient client = GetClient(); 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); // Create a room twin, with property "IsOccupied": true string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateOrReplaceDigitalTwinAsync(roomTwinId, roomTwin).ConfigureAwait(false); // Construct a query string to find the twins with the EXACT model id and provided version. If EXACT is not specified, the query // call will get all twins with the same model id but that implement any version higher than the provided version string queryString = $"SELECT * FROM digitaltwins WHERE IS_OF_MODEL('{roomModelId}', EXACT) AND IsOccupied = true"; // act AsyncPageable <BasicDigitalTwin> asyncPageableResponse = client.QueryAsync <BasicDigitalTwin>(queryString); // assert // It takes a few seconds for the service to be able to fetch digital twins through queries after being created. Hence, adding the retry logic var digitalTwinFound = false; await TestRetryHelper.RetryAsync <AsyncPageable <BasicDigitalTwin> >(async() => { await foreach (BasicDigitalTwin response in asyncPageableResponse) { digitalTwinFound = true; bool isOccupied = ((JsonElement)response.Contents["IsOccupied"]).GetBoolean(); isOccupied.Should().BeTrue(); break; } if (!digitalTwinFound) { throw new Exception($"Digital twin based on model Id {roomModelId} not found"); } return(null); }, s_retryCount, s_retryDelay); digitalTwinFound.Should().BeTrue(); } catch (Exception ex) { Assert.Fail($"Failure in executing a step in the test case: {ex.Message}."); } finally { // clean up try { if (!string.IsNullOrWhiteSpace(roomModelId)) { await client.DeleteModelAsync(roomModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task Query_PaginationWorks() { DigitalTwinsClient client = GetClient(); int pageSize = 5; string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); TimeSpan QueryWaitTimeout = TimeSpan.FromMinutes(1); // Wait at most one minute for the created twins to become queryable try { // Create room model string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); await CreateAndListModelsAsync(client, new List <string> { roomModel }).ConfigureAwait(false); // Create a room twin, with property "IsOccupied": true BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); for (int i = 0; i < pageSize * 2; i++) { string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); } string queryString = "SELECT * FROM digitaltwins"; // act CancellationTokenSource queryTimeoutCancellationToken = new CancellationTokenSource(QueryWaitTimeout); bool queryHasExpectedCount = false; while (!queryHasExpectedCount) { if (queryTimeoutCancellationToken.IsCancellationRequested) { throw new AssertionException($"Timed out waiting for at least {pageSize + 1} twins to be queryable"); } AsyncPageable <BasicDigitalTwin> asyncPageableResponse = client.QueryAsync <BasicDigitalTwin>(queryString, queryTimeoutCancellationToken.Token); int count = 0; await foreach (Page <BasicDigitalTwin> queriedTwinPage in asyncPageableResponse.AsPages(pageSizeHint: pageSize)) { count += queriedTwinPage.Values.Count; } // Once at least (page + 1) twins are query-able, then page size control can be tested. queryHasExpectedCount = count >= pageSize + 1; } // assert // Test that page size hint works, and that all returned pages either have the page size hint amount of // elements, or have no continuation token (signaling that it is the last page) int pageCount = 0; await foreach (Page <BasicDigitalTwin> page in client.QueryAsync <BasicDigitalTwin>(queryString).AsPages(pageSizeHint: pageSize)) { pageCount++; if (page.ContinuationToken != null) { page.Values.Count.Should().Be(pageSize, "Unexpected page size for a non-terminal page"); } } pageCount.Should().BeGreaterThan(1, "Expected more than one page of query results"); } catch (Exception ex) { Assert.Fail($"Failure in executing a step in the test case: {ex.Message}."); } finally { // clean up try { if (!string.IsNullOrWhiteSpace(roomModelId)) { await client.DeleteModelAsync(roomModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task Component_UpdateComponentFailsWhenIfMatchHeaderOutOfDate() { // arrange DigitalTwinsClient client = GetClient(); string wifiModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.WifiModelIdPrefix).ConfigureAwait(false); string roomWithWifiModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomWithWifiModelIdPrefix).ConfigureAwait(false); string roomWithWifiTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomWithWifiTwinIdPrefix).ConfigureAwait(false); string wifiComponentName = "wifiAccessPoint"; try { // CREATE // create roomWithWifi model string wifiModel = TestAssetsHelper.GetWifiModelPayload(wifiModelId); // create wifi model string roomWithWifiModel = TestAssetsHelper.GetRoomWithWifiModelPayload(roomWithWifiModelId, wifiModelId, wifiComponentName); await CreateAndListModelsAsync(client, new List <string> { roomWithWifiModel, wifiModel }).ConfigureAwait(false); // create room digital twin BasicDigitalTwin roomWithWifiTwin = TestAssetsHelper.GetRoomWithWifiTwinPayload(roomWithWifiModelId, wifiComponentName); await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomWithWifiTwinId, roomWithWifiTwin); // Get the component Response <object> getComponentResponse = await client .GetComponentAsync <object>( roomWithWifiTwinId, wifiComponentName) .ConfigureAwait(false); ETag?etagBeforeUpdate = (await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomWithWifiTwinId)).Value.ETag; // Patch component JsonPatchDocument componentUpdatePatchDocument = new JsonPatchDocument(); componentUpdatePatchDocument.AppendReplace("/Network", "New Network"); Response updateComponentResponse = await client .UpdateComponentAsync( roomWithWifiTwinId, wifiComponentName, componentUpdatePatchDocument) .ConfigureAwait(false); // Patch component again, but with the now out of date ETag JsonPatchDocument secondComponentUpdatePatchDocument = new JsonPatchDocument(); secondComponentUpdatePatchDocument.AppendReplace("/Network", "Even newer Network"); Func <Task> act = async() => { await client .UpdateComponentAsync( roomWithWifiTwinId, wifiComponentName, secondComponentUpdatePatchDocument, 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 { // clean up try { if (!string.IsNullOrWhiteSpace(roomWithWifiTwinId)) { await client.DeleteDigitalTwinAsync(roomWithWifiTwinId).ConfigureAwait(false); } if (!string.IsNullOrWhiteSpace(roomWithWifiModelId)) { await client.DeleteModelAsync(roomWithWifiModelId).ConfigureAwait(false); } if (!string.IsNullOrWhiteSpace(wifiModelId)) { await client.DeleteModelAsync(wifiModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task Component_Lifecycle() { // arrange DigitalTwinsClient client = GetClient(); string wifiModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.WifiModelIdPrefix).ConfigureAwait(false); string roomWithWifiModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomWithWifiModelIdPrefix).ConfigureAwait(false); string roomWithWifiTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomWithWifiTwinIdPrefix).ConfigureAwait(false); string wifiComponentName = "wifiAccessPoint"; try { // CREATE // create roomWithWifi model string wifiModel = TestAssetsHelper.GetWifiModelPayload(wifiModelId); // create wifi model string roomWithWifiModel = TestAssetsHelper.GetRoomWithWifiModelPayload(roomWithWifiModelId, wifiModelId, wifiComponentName); await CreateAndListModelsAsync(client, new List <string> { roomWithWifiModel, wifiModel }).ConfigureAwait(false); // create room digital twin BasicDigitalTwin roomWithWifiTwin = TestAssetsHelper.GetRoomWithWifiTwinPayload(roomWithWifiModelId, wifiComponentName); await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomWithWifiTwinId, roomWithWifiTwin); // Get the component Response <object> getComponentResponse = await client .GetComponentAsync <object>( roomWithWifiTwinId, wifiComponentName) .ConfigureAwait(false); // The response to the GET request should be 200 (OK) getComponentResponse.GetRawResponse().Status.Should().Be((int)HttpStatusCode.OK); // Patch component JsonPatchDocument componentUpdatePatchDocument = new JsonPatchDocument(); componentUpdatePatchDocument.AppendReplace("/Network", "New Network"); Response updateComponentResponse = await client .UpdateComponentAsync( roomWithWifiTwinId, wifiComponentName, componentUpdatePatchDocument) .ConfigureAwait(false); // The response to the Patch request should be 204 (No content) updateComponentResponse.Status.Should().Be((int)HttpStatusCode.NoContent); } catch (Exception ex) { Assert.Fail($"Failure in executing a step in the test case: {ex.Message}."); } finally { // clean up try { if (!string.IsNullOrWhiteSpace(roomWithWifiTwinId)) { await client.DeleteDigitalTwinAsync(roomWithWifiTwinId).ConfigureAwait(false); } if (!string.IsNullOrWhiteSpace(roomWithWifiModelId)) { await client.DeleteModelAsync(roomWithWifiModelId).ConfigureAwait(false); } if (!string.IsNullOrWhiteSpace(wifiModelId)) { await client.DeleteModelAsync(wifiModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task Component_UpdateComponentSucceedsWhenIfMatchHeaderIsCorrect() { // arrange DigitalTwinsClient client = GetClient(); string wifiModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.WifiModelIdPrefix).ConfigureAwait(false); string roomWithWifiModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomWithWifiModelIdPrefix).ConfigureAwait(false); string roomWithWifiTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomWithWifiTwinIdPrefix).ConfigureAwait(false); string wifiComponentName = "wifiAccessPoint"; try { // CREATE // create roomWithWifi model string wifiModel = TestAssetsHelper.GetWifiModelPayload(wifiModelId); // create wifi model string roomWithWifiModel = TestAssetsHelper.GetRoomWithWifiModelPayload(roomWithWifiModelId, wifiModelId, wifiComponentName); await client.CreateModelsAsync(new List <string> { roomWithWifiModel, wifiModel }).ConfigureAwait(false); // create room digital twin BasicDigitalTwin roomWithWifiTwin = TestAssetsHelper.GetRoomWithWifiTwinPayload(roomWithWifiModelId, wifiComponentName); await client.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomWithWifiTwinId, roomWithWifiTwin); // Get the component Response <object> getComponentResponse = await client .GetComponentAsync <object>( roomWithWifiTwinId, wifiComponentName) .ConfigureAwait(false); // Patch component JsonPatchDocument componentUpdatePatchDocument = new JsonPatchDocument(); componentUpdatePatchDocument.AppendReplace("/Network", "New Network"); Response updateComponentResponse = await client .UpdateComponentAsync( roomWithWifiTwinId, wifiComponentName, componentUpdatePatchDocument) .ConfigureAwait(false); // Get the latest ETag ETag?etagBeforeUpdate = (await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomWithWifiTwinId)).Value.ETag; Assert.IsNotNull(etagBeforeUpdate); // Patch component again, but with the now out of date ETag JsonPatchDocument secondComponentUpdatePatchDocument = new JsonPatchDocument(); componentUpdatePatchDocument.AppendReplace("/Network", "Even newer Network"); try { await client .UpdateComponentAsync( roomWithWifiTwinId, wifiComponentName, secondComponentUpdatePatchDocument, etagBeforeUpdate) .ConfigureAwait(false); } catch (RequestFailedException ex) when(ex.Status == (int)HttpStatusCode.PreconditionFailed) { throw new AssertionException("UpdateComponent should not have thrown PreconditionFailed because the ETag was up to date", ex); } } finally { // clean up try { if (!string.IsNullOrWhiteSpace(roomWithWifiTwinId)) { await client.DeleteDigitalTwinAsync(roomWithWifiTwinId).ConfigureAwait(false); } if (!string.IsNullOrWhiteSpace(roomWithWifiModelId)) { await client.DeleteModelAsync(roomWithWifiModelId).ConfigureAwait(false); } if (!string.IsNullOrWhiteSpace(wifiModelId)) { await client.DeleteModelAsync(wifiModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
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 client.CreateModelsAsync(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); } } 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 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 client.CreateModelsAsync(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); } 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 async Task DigitalTwins_PatchTwinFailsIfInvalidETagProvided() { 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 client.CreateModelsAsync(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; Assert.IsNotNull(etagBeforeUpdate); // update twin once to make the previous etag fall out of date JsonPatchDocument updateTwinPatchDocument = new JsonPatchDocument(); updateTwinPatchDocument.AppendAdd("/Humidity", 30); updateTwinPatchDocument.AppendReplace("/Temperature", 70); updateTwinPatchDocument.AppendRemove("/EmployeeId"); await client.UpdateDigitalTwinAsync(roomTwinId, updateTwinPatchDocument, ETag.All).ConfigureAwait(false); // update twin again, but with an out of date etag, which should cause a 412 from service JsonPatchDocument secondUpdateTwinPatchDocument = new JsonPatchDocument(); secondUpdateTwinPatchDocument.AppendReplace("/Humidity", 80); Func <Task> act = async() => { await client.UpdateDigitalTwinAsync(roomTwinId, secondUpdateTwinPatchDocument, etagBeforeUpdate).ConfigureAwait(false); }; act.Should().Throw <RequestFailedException>() .And.Status.Should().Be((int)HttpStatusCode.PreconditionFailed); } 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 async Task Query_PaginationWorks() { DigitalTwinsClient client = GetClient(); int pageSize = 5; string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); try { // Create room model string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); await client.CreateModelsAsync(new List <string> { roomModel }).ConfigureAwait(false); // Create a room twin, with property "IsOccupied": true string roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); for (int i = 0; i < pageSize + 1; i++) { string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); await client.CreateDigitalTwinAsync(roomTwinId, roomTwin).ConfigureAwait(false); } string queryString = "SELECT * FROM digitaltwins"; // act var options = new QueryOptions(); options.MaxItemsPerPage = pageSize; AsyncPageable <string> asyncPageableResponse = client.QueryAsync(queryString, options); // assert // Test that page size hint works, and that all returned pages either have the page size hint amount of // elements, or have no continuation token (signaling that it is the last page) int pageCount = 0; await foreach (Page <string> page in asyncPageableResponse.AsPages()) { pageCount++; if (page.ContinuationToken != null) { page.Values.Count.Should().Be(pageSize, "Unexpected page size for a non-terminal page"); } } pageCount.Should().BeGreaterThan(1, "Expected more than one page of query results"); } finally { // clean up try { if (!string.IsNullOrWhiteSpace(roomModelId)) { await client.DeleteModelAsync(roomModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task Relationships_PaginationWorks() { DigitalTwinsClient client = GetClient(); string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); string hvacModelId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.HvacModelIdPrefix).ConfigureAwait(false); string floorTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.FloorTwinIdPrefix).ConfigureAwait(false); string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); string hvacTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.HvacTwinIdPrefix).ConfigureAwait(false); try { // create floor, room and hvac model string floorModel = TestAssetsHelper.GetFloorModelPayload(floorModelId, roomModelId, hvacModelId); string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); string hvacModel = TestAssetsHelper.GetHvacModelPayload(hvacModelId, floorModelId); await client.CreateModelsAsync(new List <string> { floorModel, roomModel, hvacModel }).ConfigureAwait(false); // create floor twin BasicDigitalTwin floorTwin = TestAssetsHelper.GetFloorTwinPayload(floorModelId); await client.CreateDigitalTwinAsync <BasicDigitalTwin>(floorTwinId, floorTwin).ConfigureAwait(false); // Create room twin BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateDigitalTwinAsync <BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); BasicRelationship floorContainsRoomPayload = TestAssetsHelper.GetRelationshipWithPropertyPayload(roomTwinId, ContainsRelationship, "isAccessRestricted", true); BasicRelationship floorTwinContainedInRelationshipPayload = TestAssetsHelper.GetRelationshipPayload(floorTwinId, ContainedInRelationship); // For the sake of test simplicity, we'll just add multiple relationships from the same floor to the same room. for (int i = 0; i < bulkRelationshipCount; i++) { var floorContainsRoomRelationshipId = $"FloorToRoomRelationship-{GetRandom()}"; // create Relationship from Floor -> Room await client .CreateRelationshipAsync <BasicRelationship>( floorTwinId, floorContainsRoomRelationshipId, floorContainsRoomPayload) .ConfigureAwait(false); } // For the sake of test simplicity, we'll just add multiple relationships from the same room to the same floor. for (int i = 0; i < bulkRelationshipCount; i++) { var roomContainedInFloorRelationshipId = $"RoomToFloorRelationship-{GetRandom()}"; // create Relationship from Room -> Floor await client .CreateRelationshipAsync <BasicRelationship>( roomTwinId, roomContainedInFloorRelationshipId, floorTwinContainedInRelationshipPayload) .ConfigureAwait(false); } // LIST incoming relationships by page AsyncPageable <IncomingRelationship> incomingRelationships = client.GetIncomingRelationshipsAsync(floorTwinId); int incomingRelationshipPageCount = 0; await foreach (Page <IncomingRelationship> incomingRelationshipPage in incomingRelationships.AsPages()) { incomingRelationshipPageCount++; if (incomingRelationshipPage.ContinuationToken != null) { incomingRelationshipPage.Values.Count.Should().Be(defaultRelationshipPageSize, "Unexpected page size for a non-terminal page"); } } incomingRelationshipPageCount.Should().BeGreaterThan(1, "Expected more than one page of incoming relationships"); // LIST outgoing relationships by page AsyncPageable <string> outgoingRelationships = client.GetRelationshipsAsync(floorTwinId); int outgoingRelationshipPageCount = 0; await foreach (Page <string> outgoingRelationshipPage in outgoingRelationships.AsPages()) { outgoingRelationshipPageCount++; if (outgoingRelationshipPage.ContinuationToken != null) { outgoingRelationshipPage.Values.Count.Should().Be(defaultRelationshipPageSize, "Unexpected page size for a non-terminal page"); } } outgoingRelationshipPageCount.Should().BeGreaterThan(1, "Expected more than one page of outgoing relationships"); } finally { // clean up try { await Task .WhenAll( client.DeleteDigitalTwinAsync(floorTwinId), client.DeleteDigitalTwinAsync(roomTwinId), client.DeleteDigitalTwinAsync(hvacTwinId), client.DeleteModelAsync(hvacModelId), client.DeleteModelAsync(floorModelId), client.DeleteModelAsync(roomModelId)) .ConfigureAwait(false); } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task DigitalTwins_Lifecycle() { 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 client.CreateModelsAsync(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 await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomTwinId).ConfigureAwait(false); // update twin JsonPatchDocument updateTwinPatchDocument = new JsonPatchDocument(); updateTwinPatchDocument.AppendAdd("/Humidity", 30); updateTwinPatchDocument.AppendReplace("/Temperature", 70); updateTwinPatchDocument.AppendRemove("/EmployeeId"); var requestOptions = new UpdateDigitalTwinOptions { IfMatch = "*" }; await client.UpdateDigitalTwinAsync(roomTwinId, updateTwinPatchDocument, requestOptions).ConfigureAwait(false); // delete a twin await client.DeleteDigitalTwinAsync(roomTwinId).ConfigureAwait(false); // assert Func <Task> act = async() => { await client.GetDigitalTwinAsync <BasicDigitalTwin>(roomTwinId).ConfigureAwait(false); }; act.Should().Throw <RequestFailedException>() .And.Status.Should().Be((int)HttpStatusCode.NotFound); } 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 async Task Relationships_Lifecycle() { // arrange DigitalTwinsClient client = GetClient(); var floorContainsRoomRelationshipId = "FloorToRoomRelationship"; var floorCooledByHvacRelationshipId = "FloorToHvacRelationship"; var hvacCoolsFloorRelationshipId = "HvacToFloorRelationship"; var roomContainedInFloorRelationshipId = "RoomToFloorRelationship"; string floorModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(client, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); string hvacModelId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.HvacModelIdPrefix).ConfigureAwait(false); string floorTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.FloorTwinIdPrefix).ConfigureAwait(false); string roomTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); string hvacTwinId = await GetUniqueTwinIdAsync(client, TestAssetDefaults.HvacTwinIdPrefix).ConfigureAwait(false); try { // create floor, room and hvac model string floorModel = TestAssetsHelper.GetFloorModelPayload(floorModelId, roomModelId, hvacModelId); string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); string hvacModel = TestAssetsHelper.GetHvacModelPayload(hvacModelId, floorModelId); await client.CreateModelsAsync(new List <string> { floorModel, roomModel, hvacModel }).ConfigureAwait(false); // create floor twin BasicDigitalTwin floorTwin = TestAssetsHelper.GetFloorTwinPayload(floorModelId); await client.CreateDigitalTwinAsync <BasicDigitalTwin>(floorTwinId, floorTwin).ConfigureAwait(false); // Create room twin BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await client.CreateDigitalTwinAsync <BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); // create hvac twin BasicDigitalTwin hvacTwin = TestAssetsHelper.GetHvacTwinPayload(hvacModelId); await client.CreateDigitalTwinAsync <BasicDigitalTwin>(hvacTwinId, hvacTwin).ConfigureAwait(false); BasicRelationship floorContainsRoomPayload = TestAssetsHelper.GetRelationshipWithPropertyPayload(roomTwinId, ContainsRelationship, "isAccessRestricted", true); BasicRelationship floorTwinCoolsRelationshipPayload = TestAssetsHelper.GetRelationshipPayload(floorTwinId, CoolsRelationship); BasicRelationship floorTwinContainedInRelationshipPayload = TestAssetsHelper.GetRelationshipPayload(floorTwinId, ContainedInRelationship); BasicRelationship floorCooledByHvacPayload = TestAssetsHelper.GetRelationshipPayload(hvacTwinId, CooledByRelationship); JsonPatchDocument floorContainsRoomUpdatePayload = new JsonPatchDocument(); floorContainsRoomUpdatePayload.AppendReplace("/isAccessRestricted", false); // CREATE relationships // create Relationship from Floor -> Room await client .CreateRelationshipAsync <BasicRelationship>( floorTwinId, floorContainsRoomRelationshipId, floorContainsRoomPayload) .ConfigureAwait(false); // create Relationship from Floor -> Hvac await client .CreateRelationshipAsync <BasicRelationship>( floorTwinId, floorCooledByHvacRelationshipId, floorCooledByHvacPayload) .ConfigureAwait(false); // create Relationship from Hvac -> Floor await client .CreateRelationshipAsync <BasicRelationship>( hvacTwinId, hvacCoolsFloorRelationshipId, floorTwinCoolsRelationshipPayload) .ConfigureAwait(false); // create Relationship from Room -> Floor await client .CreateRelationshipAsync <BasicRelationship>( roomTwinId, roomContainedInFloorRelationshipId, floorTwinContainedInRelationshipPayload) .ConfigureAwait(false); // UPDATE relationships // create Relationship from Floor -> Room await client .UpdateRelationshipAsync( floorTwinId, floorContainsRoomRelationshipId, floorContainsRoomUpdatePayload) .ConfigureAwait(false); // GET relationship Response <BasicRelationship> containsRelationshipId = await client .GetRelationshipAsync <BasicRelationship>( floorTwinId, floorContainsRoomRelationshipId) .ConfigureAwait(false); // LIST incoming relationships AsyncPageable <IncomingRelationship> incomingRelationships = client.GetIncomingRelationshipsAsync(floorTwinId); int numberOfIncomingRelationshipsToFloor = 0; await foreach (IncomingRelationship relationship in incomingRelationships) { ++numberOfIncomingRelationshipsToFloor; } numberOfIncomingRelationshipsToFloor.Should().Be(2, "floor has incoming relationships from room and hvac"); // LIST relationships AsyncPageable <string> floorRelationships = client.GetRelationshipsAsync(floorTwinId); int numberOfFloorRelationships = 0; await foreach (var relationship in floorRelationships) { ++numberOfFloorRelationships; } numberOfFloorRelationships.Should().Be(2, "floor has an relationship to room and hvac"); // LIST relationships by name AsyncPageable <string> roomTwinRelationships = client .GetRelationshipsAsync( roomTwinId, ContainedInRelationship); containsRelationshipId.Value.Id.Should().Be(floorContainsRoomRelationshipId); int numberOfRelationships = 0; await foreach (var relationship in roomTwinRelationships) { ++numberOfRelationships; } numberOfRelationships.Should().Be(1, "room has only one containedIn relationship to floor"); await client .DeleteRelationshipAsync( floorTwinId, floorContainsRoomRelationshipId) .ConfigureAwait(false); await client .DeleteRelationshipAsync( roomTwinId, roomContainedInFloorRelationshipId) .ConfigureAwait(false); await client .DeleteRelationshipAsync( floorTwinId, floorCooledByHvacRelationshipId) .ConfigureAwait(false); await client .DeleteRelationshipAsync( hvacTwinId, hvacCoolsFloorRelationshipId) .ConfigureAwait(false); Func <Task> act = async() => { await client .GetRelationshipAsync <BasicRelationship>( floorTwinId, floorContainsRoomRelationshipId) .ConfigureAwait(false); }; act.Should().Throw <RequestFailedException>() .And.Status.Should().Be((int)HttpStatusCode.NotFound); act = async() => { await client .GetRelationshipAsync <BasicRelationship>( roomTwinId, roomContainedInFloorRelationshipId) .ConfigureAwait(false); }; act.Should().Throw <RequestFailedException>(). And.Status.Should().Be((int)HttpStatusCode.NotFound); act = async() => { await client .GetRelationshipAsync <BasicRelationship>( floorTwinId, floorCooledByHvacRelationshipId) .ConfigureAwait(false); }; act.Should().Throw <RequestFailedException>(). And.Status.Should().Be((int)HttpStatusCode.NotFound); act = async() => { await client .GetRelationshipAsync <BasicRelationship>( hvacTwinId, hvacCoolsFloorRelationshipId) .ConfigureAwait(false); }; act.Should().Throw <RequestFailedException>() .And.Status.Should().Be((int)HttpStatusCode.NotFound); } finally { // clean up try { await Task .WhenAll( client.DeleteDigitalTwinAsync(floorTwinId), client.DeleteDigitalTwinAsync(roomTwinId), client.DeleteDigitalTwinAsync(hvacTwinId), client.DeleteModelAsync(hvacModelId), client.DeleteModelAsync(floorModelId), client.DeleteModelAsync(roomModelId)) .ConfigureAwait(false); } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }
public async Task TestNewtonsoftObjectSerializerWithDigitalTwins() { DigitalTwinsClient defaultClient = GetClient(); string roomTwinId = await GetUniqueTwinIdAsync(defaultClient, TestAssetDefaults.RoomTwinIdPrefix).ConfigureAwait(false); string floorModelId = await GetUniqueModelIdAsync(defaultClient, TestAssetDefaults.FloorModelIdPrefix).ConfigureAwait(false); string roomModelId = await GetUniqueModelIdAsync(defaultClient, TestAssetDefaults.RoomModelIdPrefix).ConfigureAwait(false); try { // arrange // create room model string roomModel = TestAssetsHelper.GetRoomModelPayload(roomModelId, floorModelId); await CreateAndListModelsAsync(defaultClient, new List <string> { roomModel }).ConfigureAwait(false); // act // create room twin BasicDigitalTwin roomTwin = TestAssetsHelper.GetRoomTwinPayload(roomModelId); await defaultClient.CreateOrReplaceDigitalTwinAsync <BasicDigitalTwin>(roomTwinId, roomTwin).ConfigureAwait(false); // Create a client with NewtonsoftJsonObjectSerializer configured as the serializer. DigitalTwinsClient testClient = GetClient( new DigitalTwinsClientOptions { Serializer = new NewtonsoftJsonObjectSerializer() }); // Get digital twin using the simple DigitalTwin model annotated with Newtonsoft attributes SimpleNewtonsoftDtModel getResponse = await testClient.GetDigitalTwinAsync <SimpleNewtonsoftDtModel>(roomTwinId).ConfigureAwait(false); getResponse.Id.Should().NotBeNullOrEmpty("Digital twin ID should not be null or empty"); // Query DigitalTwins using the simple DigitalTwin model annotated with Newtonsoft attributes AsyncPageable <SimpleNewtonsoftDtModel> queryResponse = testClient.QueryAsync <SimpleNewtonsoftDtModel>("SELECT * FROM DIGITALTWINS"); await foreach (SimpleNewtonsoftDtModel twin in queryResponse) { twin.Id.Should().NotBeNullOrEmpty("Digital twin Id should not be null or empty"); } } finally { // cleanup try { // delete twin if (!string.IsNullOrWhiteSpace(roomTwinId)) { await defaultClient.DeleteDigitalTwinAsync(roomTwinId).ConfigureAwait(false); } // delete models if (!string.IsNullOrWhiteSpace(roomModelId)) { await defaultClient.DeleteModelAsync(roomModelId).ConfigureAwait(false); } } catch (Exception ex) { Assert.Fail($"Test clean up failed: {ex.Message}"); } } }