Exemplo n.º 1
0
        public async Task UpdatedTest()
        {
            // Arrange
            IEntityStore <string, TwinStoreEntity> twinEntityStore = GetTwinEntityStore();
            ITwinStore twinStore = new TwinStore(twinEntityStore);

            var dbase = new TwinCollection
            {
                ["p1"]       = "v1",
                ["p2"]       = "v2",
                ["$version"] = 0
            };

            var rbase = new TwinCollection
            {
                ["p1"]       = "v1",
                ["p2"]       = "v2",
                ["$version"] = 0
            };

            var basetwin = new Twin
            {
                Properties = new TwinProperties
                {
                    Desired  = dbase,
                    Reported = rbase
                }
            };

            var desired1 = new TwinCollection
            {
                ["p1"]       = "vp1",
                ["p3"]       = "v3",
                ["$version"] = 1
            };

            var reported1 = new TwinCollection
            {
                ["p1"]       = "vp1",
                ["p3"]       = "v3",
                ["$version"] = 1
            };

            var twin1 = new Twin
            {
                Properties = new TwinProperties
                {
                    Desired  = desired1,
                    Reported = reported1
                }
            };

            var desired2 = new TwinCollection
            {
                ["p2"]       = "vp2",
                ["p3"]       = "v3",
                ["$version"] = 2
            };

            var reported2 = new TwinCollection
            {
                ["p1"]       = "vp1",
                ["p2"]       = "vp3",
                ["$version"] = 2
            };

            var twin2 = new Twin
            {
                Properties = new TwinProperties
                {
                    Desired  = desired2,
                    Reported = reported2
                }
            };

            var desired3 = new TwinCollection
            {
                ["p2"]       = "v10",
                ["p3"]       = "vp3",
                ["$version"] = 3
            };

            var reported3 = new TwinCollection
            {
                ["p1"]       = "vp1",
                ["p3"]       = "v10",
                ["$version"] = 3
            };

            var twin3 = new Twin
            {
                Properties = new TwinProperties
                {
                    Desired  = desired3,
                    Reported = reported1
                }
            };

            var twin4 = new Twin
            {
                Properties = new TwinProperties
                {
                    Desired  = desired2,
                    Reported = reported3
                }
            };

            var twin5 = new Twin
            {
                Properties = new TwinProperties
                {
                    Desired  = desired2,
                    Reported = reported2
                }
            };

            string id = "d1";

            // Act
            await twinStore.Update(id, basetwin);

            // Assert
            Option <Twin> twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p1\":\"v1\",\"p2\":\"v2\",\"$version\":0}");
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"v1\",\"p2\":\"v2\",\"$version\":0}");

            // Act
            await twinStore.Update(id, twin1);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p1\":\"vp1\",\"p3\":\"v3\",\"$version\":1}");
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"vp1\",\"p3\":\"v3\",\"$version\":1}");

            // Act
            await twinStore.Update(id, twin2);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p2\":\"vp2\",\"p3\":\"v3\",\"$version\":2}");
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"vp3\",\"$version\":2}");

            // Act
            await twinStore.Update(id, twin3);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p2\":\"vp2\",\"p3\":\"v3\",\"$version\":2}");
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"vp3\",\"$version\":2}");

            // Act
            await twinStore.Update(id, twin4);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p2\":\"vp2\",\"p3\":\"v3\",\"$version\":2}");
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"vp3\",\"$version\":2}");

            // Act
            await twinStore.Update(id, twin5);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p2\":\"vp2\",\"p3\":\"v3\",\"$version\":2}");
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"vp3\",\"$version\":2}");
        }
Exemplo n.º 2
0
        public async Task GetTwinTest()
        {
            // Arrange
            IEntityStore <string, TwinStoreEntity> twinEntityStore = GetTwinEntityStore();
            await twinEntityStore.Put(
                "d1",
                new TwinStoreEntity(
                    new Twin
            {
                Properties = new TwinProperties
                {
                    Desired = new TwinCollection
                    {
                        ["dp1"] = "d1"
                    },
                    Reported = new TwinCollection
                    {
                        ["rp1"] = "r1"
                    }
                }
            }));

            await twinEntityStore.Put(
                "d2",
                new TwinStoreEntity(
                    new TwinCollection
            {
                ["rp2"] = "d2"
            }));

            await twinEntityStore.Put(
                "d3",
                new TwinStoreEntity());

            await twinEntityStore.Put(
                "d4",
                new TwinStoreEntity(
                    new Twin
            {
                Properties = new TwinProperties
                {
                    Desired = new TwinCollection
                    {
                        ["dp4"] = "d4"
                    },
                    Reported = new TwinCollection
                    {
                        ["rp4"] = "r4"
                    }
                }
            },
                    new TwinCollection
            {
                ["rp4"] = "r4"
            }));

            ITwinStore twinStore = new TwinStore(twinEntityStore);

            // Act
            Option <Twin> t1 = await twinStore.Get("d1");

            // Assert
            Assert.True(t1.HasValue);
            Assert.Equal(t1.OrDefault().Properties.Desired.ToJson(), "{\"dp1\":\"d1\"}");
            Assert.Equal(t1.OrDefault().Properties.Reported.ToJson(), "{\"rp1\":\"r1\"}");

            // Act
            Option <Twin> t2 = await twinStore.Get("d2");

            // Assert
            Assert.False(t2.HasValue);

            // Act
            Option <Twin> t3 = await twinStore.Get("d3");

            // Assert
            Assert.False(t3.HasValue);

            // Act
            Option <Twin> t4 = await twinStore.Get("d4");

            // Assert
            Assert.True(t4.HasValue);
            Assert.Equal(t4.OrDefault().Properties.Desired.ToJson(), "{\"dp4\":\"d4\"}");
            Assert.Equal(t4.OrDefault().Properties.Reported.ToJson(), "{\"rp4\":\"r4\"}");
        }
Exemplo n.º 3
0
        public async Task UpdatedDesiredPropertiesTest()
        {
            // Arrange
            IEntityStore <string, TwinStoreEntity> twinEntityStore = GetTwinEntityStore();
            ITwinStore twinStore = new TwinStore(twinEntityStore);

            var dbase = new TwinCollection
            {
                ["p1"]       = "v1",
                ["p2"]       = "v2",
                ["$version"] = 0
            };

            var basetwin = new Twin
            {
                Properties = new TwinProperties
                {
                    Desired = dbase
                }
            };

            var dpatch1 = new TwinCollection
            {
                ["p1"]       = "vp1",
                ["p3"]       = "v3",
                ["$version"] = 1
            };

            var dpatch2 = new TwinCollection
            {
                ["p1"] = "vp1",
                ["p3"] = new
                {
                    p31 = "v31"
                },
                ["$version"] = 2
            };

            var dpatch3 = new TwinCollection
            {
                ["p2"] = "vp2",
                ["p3"] = new
                {
                    p31 = "v32"
                },
                ["$version"] = 3
            };

            var dpatch4 = new TwinCollection
            {
                ["p2"] = "vp4",
                ["p3"] = new
                {
                    p31 = "v50"
                },
                ["$version"] = 3
            };

            string id = "d1";

            // Act
            await twinStore.Update(id, basetwin);

            // Assert
            Option <Twin> twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p1\":\"v1\",\"p2\":\"v2\",\"$version\":0}");

            // Act
            await twinStore.UpdateDesiredProperties(id, dpatch1);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"v2\",\"$version\":1,\"p3\":\"v3\"}");

            // Act
            await twinStore.UpdateDesiredProperties(id, dpatch2);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"v2\",\"$version\":2,\"p3\":{\"p31\":\"v31\"}}");

            // Act
            await twinStore.UpdateDesiredProperties(id, dpatch3);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"vp2\",\"$version\":3,\"p3\":{\"p31\":\"v32\"}}");

            // Act
            await twinStore.UpdateDesiredProperties(id, dpatch4);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Desired.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"vp2\",\"$version\":3,\"p3\":{\"p31\":\"v32\"}}");
        }
Exemplo n.º 4
0
        public async Task UpdatedReportedPropertiesTest()
        {
            // Arrange
            IEntityStore <string, TwinStoreEntity> twinEntityStore = GetTwinEntityStore();
            ITwinStore twinStore = new TwinStore(twinEntityStore);

            var rbase = new TwinCollection
            {
                ["p1"] = "v1",
                ["p2"] = "v2"
            };

            var rpatch1 = new TwinCollection
            {
                ["p1"] = "vp1",
                ["p3"] = "v3"
            };

            var rpatch2 = new TwinCollection
            {
                ["p1"] = "vp1",
                ["p3"] = new
                {
                    p31 = "v31"
                }
            };

            var rpatch3 = new TwinCollection
            {
                ["p2"] = "vp2",
                ["p3"] = new
                {
                    p31 = "v32"
                }
            };

            string id = "d1";

            // Act
            await twinStore.UpdateReportedProperties(id, rbase);

            // Assert
            Option <Twin> twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"v1\",\"p2\":\"v2\"}");

            // Act
            await twinStore.UpdateReportedProperties(id, rpatch1);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"v2\",\"p3\":\"v3\"}");

            // Act
            await twinStore.UpdateReportedProperties(id, rpatch2);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"v2\",\"p3\":{\"p31\":\"v31\"}}");

            // Act
            await twinStore.UpdateReportedProperties(id, rpatch3);

            // Assert
            twin = await twinStore.Get(id);

            Assert.True(twin.HasValue);
            Assert.Equal(twin.OrDefault().Properties.Reported.ToJson(), "{\"p1\":\"vp1\",\"p2\":\"vp2\",\"p3\":{\"p31\":\"v32\"}}");
        }