public static string GetRoomTwinUpdatePayload()
        {
            var uou = new UpdateOperationsUtility();

            uou.AppendAddOp("/Humidity", 30);
            uou.AppendReplaceOp("/Temperature", 70);
            uou.AppendRemoveOp("/EmployeeId");
            return(RemoveNewLines(uou.Serialize()));
        }
        public void UpdateOperationsUtility_BuildAdd()
        {
            // arrange

            const string addOp     = "add";
            const string replaceOp = "replace";
            const string removeOp  = "remove";

            const string addPath      = "/addComponentPath123";
            const string addValue     = "value123";
            const string replacePath  = "/replaceComponentPath123";
            const string replaceValue = "value456";
            const string removePath   = "/removeComponentPath123";

            var dtUpdateUtility = new UpdateOperationsUtility();

            dtUpdateUtility.AppendAddOp(addPath, addValue);
            dtUpdateUtility.AppendReplaceOp(replacePath, replaceValue);
            dtUpdateUtility.AppendRemoveOp(removePath);

            // act
            string actual = dtUpdateUtility.Serialize();

            // assert

            JsonDocument parsed = JsonDocument.Parse(actual);

            parsed.RootElement.ValueKind.Should().Be(JsonValueKind.Array, "operations should be nested in an array");
            parsed.RootElement.GetArrayLength().Should().Be(3, "three operations were included");

            JsonElement addElement = parsed.RootElement[0];

            addElement.GetProperty("op").GetString().Should().Be(addOp);
            addElement.GetProperty("path").GetString().Should().Be(addPath);
            addElement.GetProperty("value").GetString().Should().Be(addValue);

            JsonElement replaceElement = parsed.RootElement[1];

            replaceElement.GetProperty("op").GetString().Should().Be(replaceOp);
            replaceElement.GetProperty("path").GetString().Should().Be(replacePath);
            replaceElement.GetProperty("value").GetString().Should().Be(replaceValue);

            JsonElement removeElement = parsed.RootElement[2];

            removeElement.GetProperty("op").GetString().Should().Be(removeOp);
            removeElement.GetProperty("path").GetString().Should().Be(removePath);
            removeElement.TryGetProperty("value", out _).Should().BeFalse();
        }
        private async Task UpdateCurrentTemperaturePropertyAsync()
        {
            // Choose a random value to assign to the currentTemperature property
            int currentTemperature = Random.Next(0, 100);

            const string currentTemperaturePropertyName = "currentTemperature";
            var          updateOperation = new UpdateOperationsUtility();

            // First, add the property to the digital twin
            updateOperation.AppendAddPropertyOp($"/{currentTemperaturePropertyName}", currentTemperature);
            _logger.LogDebug($"Add the {currentTemperaturePropertyName} property on the {_digitalTwinId} digital twin " +
                             $"with a value of {currentTemperature}.");
            HttpOperationHeaderResponse <DigitalTwinUpdateHeaders> addPropertyToDigitalTwinResponse = await _digitalTwinClient
                                                                                                      .UpdateDigitalTwinAsync(_digitalTwinId, updateOperation.Serialize());

            _logger.LogDebug($"Update {_digitalTwinId} digital twin response: {addPropertyToDigitalTwinResponse.Response.StatusCode}.");

            // Print the Thermostat digital twin
            await GetAndPrintDigitalTwinAsync <ThermostatTwin>();

            // Second, replace the property to a different value
            int newCurrentTemperature = Random.Next(0, 100);

            updateOperation.AppendReplacePropertyOp($"/{currentTemperaturePropertyName}", newCurrentTemperature);
            _logger.LogDebug($"Replace the {currentTemperaturePropertyName} property on the {_digitalTwinId} digital twin " +
                             $"with a value of {newCurrentTemperature}.");
            HttpOperationHeaderResponse <DigitalTwinUpdateHeaders> replacePropertyInDigitalTwinResponse = await _digitalTwinClient
                                                                                                          .UpdateDigitalTwinAsync(_digitalTwinId, updateOperation.Serialize());

            _logger.LogDebug($"Update {_digitalTwinId} digital twin response: {replacePropertyInDigitalTwinResponse.Response.StatusCode}.");

            // Print the Thermostat digital twin
            await GetAndPrintDigitalTwinAsync <ThermostatTwin>();

            // Third, remove the currentTemperature property
            updateOperation.AppendRemoveOp($"/{currentTemperaturePropertyName}");
            _logger.LogDebug($"Remove the {currentTemperaturePropertyName} property on the {_digitalTwinId} digital twin.");
            HttpOperationHeaderResponse <DigitalTwinUpdateHeaders> removePropertyInDigitalTwinResponse = await _digitalTwinClient
                                                                                                         .UpdateDigitalTwinAsync(_digitalTwinId, updateOperation.Serialize());

            _logger.LogDebug($"Update {_digitalTwinId} digital twin response: {removePropertyInDigitalTwinResponse.Response.StatusCode}.");

            // Print the Thermostat digital twin
            await GetAndPrintDigitalTwinAsync <ThermostatTwin>();
        }
Example #4
0
        public void UpdateUtilityAppendsRemoveOp()
        {
            var    op   = new UpdateOperationsUtility();
            string path = "testPath";

            op.AppendRemoveOp(path);
            string operations = op.Serialize();

            // There should be a single operation added.
            var jArray = JArray.Parse(operations);

            jArray.Count.Should().Be(1);

            // The patch operation added should be a "remove" operation.
            JToken jObject = jArray.First;

            jObject.Value <string>(Op).Should().Be(Remove);
        }