Пример #1
0
        public void SmartObjectBuilderTest()
        {
            DateTime now      = TestUtils.GetNowIgnoringMilis();
            Guid     eventId  = Guid.NewGuid();
            Guid     objectId = Guid.NewGuid();
            Dictionary <string, object> attributes = new Dictionary <string, object>();

            attributes.Add("String", "text");

            SmartObject myObject = new SmartObject.Builder()
            {
                EventId          = eventId,
                ObjectId         = objectId,
                ObjectType       = "objectType",
                RegistrationDate = now,
                Attributes       = new Dictionary <string, object>()
                {
                    { "String", "text" }
                },
                DeviceId = "deviceId",
                Username = "******"
            };

            Assert.AreEqual(myObject.EventId, eventId);
            Assert.AreEqual(myObject.ObjectId, objectId);
            Assert.AreEqual(myObject.ObjectType, "objectType");
            Assert.AreEqual(myObject.RegistrationDate, now);
            CollectionAssert.AreEqual(myObject.Attributes, attributes);
            Assert.AreEqual(myObject.DeviceId, "deviceId");
            Assert.AreEqual(myObject.Username, "username");
        }
        public void EventBuilderTestWithObject()
        {
            DateTime now     = TestUtils.GetNowIgnoringMilis();
            Guid     eventId = Guid.NewGuid();
            Dictionary <string, object> timeseries = new Dictionary <string, object>();

            timeseries.Add("String", "text");

            SmartObject myObject = new SmartObject.Builder()
            {
                DeviceId = "Device Id"
            };

            Event eventBuilt = new Event.Builder()
            {
                EventId    = eventId,
                EventType  = "event type",
                DeviceId   = myObject.DeviceId,
                Timestamp  = now,
                Timeseries = new Dictionary <string, object>()
                {
                    { "String", "text" }
                }
            };

            Assert.AreEqual(eventBuilt.EventId, eventId);
            Assert.True(eventBuilt.EventType.Equals("event type"));
            Assert.True(eventBuilt.DeviceId.Equals("Device Id"));
            Assert.AreEqual(eventBuilt.Timestamp, now);
            CollectionAssert.AreEqual(eventBuilt.Timeseries, timeseries);
        }
        public void SmartObjectSerializeTestWithDeprecatedDate()
        {
            DateTimeOffset now = new DateTimeOffset(2017, 06, 12, 18, 05, 05, TimeSpan.FromHours(5d));

            Dictionary <string, object> attributes = new Dictionary <string, object>();

            attributes.Add("string", "stringValue");
            attributes.Add("double", 10d);
            attributes.Add("float", 10.5f);

            SmartObject myObject = new SmartObject.Builder()
            {
                DeviceId         = "test",
                ObjectType       = "type",
                RegistrationDate = now.UtcDateTime,
                Attributes       = new SortedDictionary <string, object>()
                {
                    { "string", "stringValue" },
                    { "double", 10d },
                    { "float", 10.5f }
                }
            };

            string json = ObjectSerializer.SerializeObject(myObject);

            TestUtils.AssertJsonEquals(json, new List <string>
            {
                "\"x_device_id\":\"test\"",
                "\"x_registration_date\":\"2017-06-12T13:05:05Z\"",
                "\"x_object_type\":\"type\"",
                "\"float\":10.5",
                "\"double\":10.0",
                "\"string\":\"stringValue\""
            });
        }
Пример #4
0
        public void TestObjectsCreate()
        {
            String      uuid        = Guid.NewGuid().ToString();
            String      value1      = "value-" + uuid;
            SmartObject smartObject = new SmartObject.Builder()
            {
                DeviceId   = "deviceId-" + uuid,
                ObjectType = "object_type1",
                Attributes = new Dictionary <string, object>()
                {
                    { "object_text_attribute", value1 }
                }
            };

            client.Objects.Create(smartObject);
            try {
                client.Objects.Create(new SmartObject.Builder()
                {
                    Username = "******" + uuid
                });
                Assert.Fail("Invalid smartObject (no password here) creation should fail");
            } catch {
                //expected
            }

            SmartObject updated = new SmartObject.Builder()
            {
                Attributes = new Dictionary <string, object>()
                {
                    { "object_text_attribute", "newValue" }
                }
            };

            client.Objects.Update(updated, smartObject.DeviceId);
        }
Пример #5
0
        public void SmartObjectBuilderAddAttributesTest()
        {
            Dictionary <string, object> attributes = new Dictionary <string, object>();

            attributes.Add("String", "text");
            attributes.Add("int", 10);
            attributes.Add("double", 10.7);
            attributes.Add("boolean", true);

            SmartObject myObject = new SmartObject.Builder()
            {
                Attributes = new Dictionary <string, object>()
                {
                    { "String", "text" },
                    { "int", 10 },
                    { "double", 10.7 },
                    { "boolean", true }
                }
            };

            Assert.IsNull(myObject.EventId);
            Assert.IsNull(myObject.DeviceId);
            Assert.IsNull(myObject.ObjectId);
            Assert.IsNull(myObject.Username);
            Assert.IsNull(myObject.RegistrationDate);
            Assert.IsNull(myObject.ObjectType);
            CollectionAssert.AreEqual(myObject.Attributes, attributes);
        }
        public void SmartObjectSerializeTestWithEmptyObject()
        {
            SmartObject myObject = new SmartObject.Builder().Build();

            string json = ObjectSerializer.SerializeObject(myObject);

            Assert.AreEqual("{}", json);
        }
Пример #7
0
        public void ClientSmartObjectSyncCreateNotDeviceID()
        {
            SmartObject smartObject = new SmartObject.Builder().Build();

            Assert.That(() => objectClient.Create(smartObject),
                        Throws.TypeOf <ArgumentException>()
                        .With.Message.EqualTo("x_device_id cannot be blank."));
        }
Пример #8
0
        public void TestBatchClaimUnclaim()
        {
            String uuid   = Guid.NewGuid().ToString();
            String value1 = "value-" + uuid;
            Owner  owner  = new Owner.Builder()
            {
                Username   = "******" + uuid,
                Password   = "******" + uuid,
                Attributes = new Dictionary <string, object>()
                {
                    { "owner_text_attribute", value1 }
                }
            };

            SmartObject smartObject = new SmartObject.Builder()
            {
                DeviceId   = "deviceId-" + uuid,
                ObjectType = "object_type1",
                Attributes = new Dictionary <string, object>()
                {
                    { "object_text_attribute", value1 }
                }
            };


            Dictionary <string, object> body = new Dictionary <string, object>()
            {
                { "x_timestamp", "2017-04-24T16:13:11+00:00" }
            };
            IEnumerable <ClaimOrUnclaim> valid = new List <ClaimOrUnclaim>()
            {
                new ClaimOrUnclaim(owner.Username, smartObject.DeviceId, body)
            };
            IEnumerable <ClaimOrUnclaim> unknownUsername = new List <ClaimOrUnclaim>()
            {
                new ClaimOrUnclaim(uuid, smartObject.DeviceId, body)
            };
            IEnumerable <ClaimOrUnclaim> unknownDeviceId = new List <ClaimOrUnclaim>()
            {
                new ClaimOrUnclaim(owner.Username, uuid, body)
            };

            client.Owners.Create(owner);
            client.Objects.Create(smartObject);

            ITTestHelper.AllFailed(client.Owners.BatchClaim(unknownUsername));
            ITTestHelper.AllFailed(client.Owners.BatchClaim(unknownDeviceId));

            ITTestHelper.AllSuccess(client.Owners.BatchClaim(valid));

            ITTestHelper.AllFailed(client.Owners.BatchUnclaim(unknownUsername));
            ITTestHelper.AllFailed(client.Owners.BatchUnclaim(unknownDeviceId));

            ITTestHelper.AllSuccess(client.Owners.BatchUnclaim(valid));

            //Already unclaimed
            ITTestHelper.AllFailed(client.Owners.BatchUnclaim(valid));
        }
Пример #9
0
        public void SmartObjectBuilderOwnerEmpty()
        {
            SmartObject myObject = new SmartObject.Builder().Build();

            Assert.IsNull(myObject.DeviceId);
            Assert.IsNull(myObject.ObjectId);
            Assert.IsNull(myObject.Username);
            Assert.IsNull(myObject.RegistrationDate);
            Assert.IsNull(myObject.ObjectType);
            Assert.True(myObject.Attributes.Count == 0);
        }
Пример #10
0
        public void ClientSmartObjectSyncCreateEmptyDeviceID(string deviceId, string errorMessage, string objectType)
        {
            SmartObject smartObject = new SmartObject.Builder()
            {
                DeviceId   = deviceId,
                ObjectType = objectType
            };

            Assert.That(() => objectClient.Create(smartObject),
                        Throws.TypeOf <ArgumentException>()
                        .With.Message.EqualTo(errorMessage));
        }
Пример #11
0
        public void ClientSmartObjectAsyncCreateNotDeviceID()
        {
            withSuccessfulResults(client =>
            {
                SmartObject smartObject = new SmartObject.Builder().Build();

                Assert.That(() => client.CreateAsync(smartObject).Wait(),
                            Throws.TypeOf <AggregateException>()
                            .With.InnerException.TypeOf <ArgumentException>()
                            .With.InnerException.Message.EqualTo("x_device_id cannot be blank."));
            });
        }
        public void SmartObjectSerializeTestWithSmartObject()
        {
            SmartObject myObject = new SmartObject.Builder()
            {
                DeviceId = "test",
                Username = "******"
            };

            string json = ObjectSerializer.SerializeObject(myObject);

            Assert.AreEqual(
                "{\"x_device_id\":\"test\",\"x_owner\":{\"username\":\"owner\"}}",
                json);
        }
Пример #13
0
        public void SmartObjectSerializeTestWithSmartObject()
        {
            SmartObject myObject = new SmartObject.Builder()
            {
                EventId  = Guid.Parse("671c8315-952b-4c69-8c37-d2d58a64af9e"),
                DeviceId = "test",
                Username = "******"
            };

            string json = ObjectSerializer.SerializeObject(myObject);

            Assert.AreEqual(
                "{\"x_device_id\":\"test\",\"event_id\":\"671c8315-952b-4c69-8c37-d2d58a64af9e\",\"x_owner\":{\"username\":\"owner\"}}",
                json);
        }
Пример #14
0
        public void ClientSmartObjectAsyncCreateEmptyDeviceID(string deviceId, string errorMessage, string objectType)
        {
            withSuccessfulResults(client =>
            {
                SmartObject smartObject = new SmartObject.Builder()
                {
                    DeviceId   = deviceId,
                    ObjectType = objectType
                };

                Assert.That(() => client.CreateAsync(smartObject).Wait(),
                            Throws.TypeOf <AggregateException>()
                            .With.InnerException.TypeOf <ArgumentException>()
                            .With.InnerException.Message.EqualTo(errorMessage));
            });
        }
Пример #15
0
        public void TestObjectDelete()
        {
            String      uuid           = Guid.NewGuid().ToString();
            SmartObject objectToDelete = new SmartObject.Builder()
            {
                DeviceId   = "deviceId-" + uuid,
                ObjectType = "object_type1"
            };

            client.Objects.Create(objectToDelete);

            client.Objects.Delete(objectToDelete.DeviceId);
            try {
                client.Objects.Delete(objectToDelete.Username);
                Assert.Fail("Cannot delete on an smart object that does not exists");
            } catch {
                //expected
            }
        }
Пример #16
0
        public void SmartObjectSerializeTest()
        {
            DateTime now = TestUtils.GetNowIgnoringMilis();

            Dictionary <string, object> attributes = new Dictionary <string, object>();

            attributes.Add("string", "stringValue");
            attributes.Add("double", 10d);
            attributes.Add("float", 10.5f);

            SmartObject myObject = new SmartObject.Builder()
            {
                EventId          = Guid.Parse("98c62f5c-ad48-4ef8-8d70-dbe3a1e8b17f"),
                DeviceId         = "test",
                ObjectType       = "type",
                RegistrationDate = now,
                Attributes       = new Dictionary <string, object>()
                {
                    { "string", "stringValue" },
                    { "double", 10d },
                    { "float", 10.5f }
                }
            };

            string json = ObjectSerializer.SerializeObject(myObject);

            Assert.AreEqual(
                "{\"x_device_id\":\"test\"," +
                "\"x_registration_date\":\"" + now.ToString(EventSerializerTest.DatetimeFormat) + "\"," +
                "\"x_object_type\":\"type\"," +
                "\"event_id\":\"98c62f5c-ad48-4ef8-8d70-dbe3a1e8b17f\"," +
                "\"float\":10.5," +
                "\"double\":10.0," +
                "\"string\":\"stringValue\"}",
                json);
        }
        public void SmartObjectSerializeTestAttributeList()
        {
            Dictionary <string, object> attributes = new Dictionary <string, object>();

            attributes.Add("list", new List <string>()
            {
                "1", "2"
            });

            SmartObject myObject = new SmartObject.Builder()
            {
                Attributes = new Dictionary <string, object>()
                {
                    { "list", new List <string>()
                      {
                          "1", "2"
                      } }
                }
            };

            string json = ObjectSerializer.SerializeObject(myObject);

            Assert.AreEqual("{\"list\":[\"1\",\"2\"]}", json);
        }
Пример #18
0
        /// <summary>
        /// deserialize a json string to a smartObject instance
        /// </summary>
        /// <param name="obj">json string</param>
        /// <returns>Smart Object instance</returns>
        public static SmartObject DeserializeSmartObject(string obj)
        {
            SmartObject.Builder         builder    = new SmartObject.Builder();
            Dictionary <string, object> attributes = new Dictionary <string, object>();

            foreach (KeyValuePair <string, object> token in JsonConvert.DeserializeObject <Dictionary <string, object> >(obj))
            {
                switch (token.Key)
                {
                case DeviceIdProperty:
                {
                    if (!(token.Value is string))
                    {
                        throw new InvalidOperationException("Field 'x_device_id' does not match TYPE 'TEXT'");
                    }
                    builder.DeviceId = token.Value as string;
                    break;
                }

                case RegistrationDateProperty:
                {
                    if (token.Value == null || !(token.Value is DateTime))
                    {
                        throw new InvalidOperationException("Field 'x_registration_date' does not match TYPE 'DATETIME'");
                    }
                    builder.RegistrationDate = ((DateTime)token.Value).ToUniversalTime();
                    break;
                }

                case ObjectTypeProperty:
                {
                    if (!(token.Value is string))
                    {
                        throw new InvalidOperationException("Field 'x_object_type' does not match TYPE 'TEXT'");
                    }
                    builder.ObjectType = token.Value as string;
                    break;
                }

                case EventIdProperty:
                {
                    Guid guid;
                    if (!(token.Value is string) ||
                        string.IsNullOrEmpty(token.Value as string) ||
                        !(Guid.TryParse(token.Value as string, out guid)))
                    {
                        throw new InvalidOperationException("Field 'x_event_id' does not match TYPE 'GUID'");
                    }
                    builder.EventId = guid;
                    break;
                }

                case OwnerProperty:
                {
                    if (token.Value == null || !(token.Value is JObject))
                    {
                        throw new InvalidOperationException("Field 'x_owner' does not match TYPE 'OWNER'");
                    }

                    var username = (token.Value as JObject)[OwnerSerializer.UsernameProperty];
                    if (username != null)
                    {
                        if (username.Type != JTokenType.String)
                        {
                            throw new InvalidOperationException("Field 'x_owner' does not match TYPE 'OWNER'");
                        }
                        builder.Username = username.Value <string>();
                    }
                    break;
                }

                default:
                {
                    if (token.Value is JArray)
                    {
                        attributes.Add(token.Key, (token.Value as JArray).ToObject <string[]>());
                    }
                    else
                    {
                        attributes.Add(token.Key, token.Value);
                    }
                    break;
                }
                }
            }
            builder.Attributes = attributes;
            return(builder.Build());
        }
Пример #19
0
        public void TestClaimUnclaimWithBody()
        {
            String uuid   = Guid.NewGuid().ToString();
            String value1 = "value-" + uuid;
            Owner  owner  = new Owner.Builder()
            {
                Username   = "******" + uuid,
                Password   = "******" + uuid,
                Attributes = new Dictionary <string, object>()
                {
                    { "owner_text_attribute", value1 }
                }
            };
            Dictionary <string, object> body = new Dictionary <string, object>()
            {
                { "x_timestamp", "2017-04-24T16:13:11+00:00" }
            };

            SmartObject smartObject = new SmartObject.Builder()
            {
                DeviceId   = "deviceId-" + uuid,
                ObjectType = "object_type1",
                Attributes = new Dictionary <string, object>()
                {
                    { "object_text_attribute", value1 }
                }
            };

            client.Owners.Create(owner);
            client.Objects.Create(smartObject);

            try {
                client.Owners.Claim(uuid, smartObject.DeviceId, body);
                Assert.Fail("Claim on a non existent owner should fail");
            } catch {
                //expected
            }

            try {
                client.Owners.Claim(owner.Username, uuid, body);
                Assert.Fail("Claim on a non existent owner should fail");
            } catch {
                //expected
            }

            client.Owners.Claim(owner.Username, smartObject.DeviceId, body);

            try {
                client.Owners.Unclaim(uuid, smartObject.DeviceId, body);
                Assert.Fail("Unclaim on an non-existing owner should fail");
            } catch {
                //expected
            }
            try {
                client.Owners.Unclaim(owner.Username, uuid, body);
                Assert.Fail("Unclaim on an non-existing object should fail");
            } catch {
                //expected
            }

            client.Owners.Unclaim(owner.Username, smartObject.DeviceId, body);

            try {
                client.Owners.Unclaim(owner.Username, smartObject.DeviceId);
                Assert.Fail("Unclaim on an already unclaimed object should fail");
            } catch {
                //expected
            }
        }
Пример #20
0
        /// <summary>
        /// deserialize a json string to a smartObject instance
        /// </summary>
        /// <param name="obj">json string</param>
        /// <returns>Smart Object instance</returns>
        public static SmartObject DeserializeObject(string obj)
        {
            SmartObject.Builder         builder    = new SmartObject.Builder();
            Dictionary <string, object> attributes = new Dictionary <string, object>();

            var rawObject = JsonConvert.DeserializeObject <Dictionary <string, object> >(obj,
                                                                                         new JsonSerializerSettings
            {
                NullValueHandling    = NullValueHandling.Ignore,
                DateFormatString     = EventSerializer.DatetimeFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            foreach (KeyValuePair <string, object> token in rawObject)
            {
                switch (token.Key)
                {
                case DeviceIdProperty:
                {
                    if (!(token.Value is string))
                    {
                        throw new InvalidOperationException("Field 'x_device_id' does not match TYPE 'TEXT'");
                    }
                    builder.DeviceId = token.Value as string;
                    break;
                }

                case RegistrationDateProperty:
                {
                    if (token.Value == null || !(token.Value is DateTime))
                    {
                        throw new InvalidOperationException("Field 'x_registration_date' does not match TYPE 'DATETIME'");
                    }
                    builder.RegistrationDateTime = new DateTimeOffset((DateTime)token.Value);
                    break;
                }

                case ObjectTypeProperty:
                {
                    if (!(token.Value is string))
                    {
                        throw new InvalidOperationException("Field 'x_object_type' does not match TYPE 'TEXT'");
                    }
                    builder.ObjectType = token.Value as string;
                    break;
                }

                case OwnerProperty:
                {
                    if (token.Value == null || !(token.Value is JObject))
                    {
                        throw new InvalidOperationException("Field 'x_owner' does not match TYPE 'OWNER'");
                    }

                    var username = (token.Value as JObject)[OwnerSerializer.UsernameProperty];
                    if (username != null)
                    {
                        if (username.Type != JTokenType.String)
                        {
                            throw new InvalidOperationException("Field 'x_owner' does not match TYPE 'OWNER'");
                        }
                        builder.Username = username.Value <string>();
                    }
                    break;
                }

                default:
                {
                    if (token.Value is JArray)
                    {
                        attributes.Add(token.Key, (token.Value as JArray).ToObject <string[]>());
                    }
                    else
                    {
                        attributes.Add(token.Key, token.Value);
                    }
                    break;
                }
                }
            }
            builder.Attributes = attributes;
            return(builder.Build());
        }
Пример #21
0
        public void TestClaimUnclaim()
        {
            String uuid   = Guid.NewGuid().ToString();
            String value1 = "value-" + uuid;
            Owner  owner  = new Owner.Builder()
            {
                Username   = "******" + uuid,
                Password   = "******" + uuid,
                Attributes = new Dictionary <string, object>()
                {
                    { "owner_text_attribute", value1 }
                }
            };

            SmartObject smartObject = new SmartObject.Builder()
            {
                DeviceId   = "deviceId-" + uuid,
                ObjectType = "object_type1",
                Attributes = new Dictionary <string, object>()
                {
                    { "object_text_attribute", value1 }
                }
            };

            client.Owners.Create(owner);
            client.Objects.Create(smartObject);

            try {
                client.Owners.Claim(uuid, smartObject.DeviceId);
                Assert.Fail("Claim on a non existent owner should fail");
            } catch {
                //expected
            }

            try {
                client.Owners.Claim(owner.Username, uuid);
                Assert.Fail("Claim on a non existent owner should fail");
            } catch {
                //expected
            }

            client.Owners.Claim(owner.Username, smartObject.DeviceId);

            ITTestHelper.EventuallyAssert(() => {
                ResultSet resultOwn = client.Restitution.Search(ITTestHelper.searchObjectByOwnerQuery(owner.Username));
                Assert.AreEqual(1, resultOwn.Rows.Count);
            });

            try {
                client.Owners.Unclaim(uuid, smartObject.DeviceId);
                Assert.Fail("Unclaim on an non-existing owner should fail");
            } catch {
                //expected
            }
            try {
                client.Owners.Unclaim(owner.Username, uuid);
                Assert.Fail("Unclaim on an non-existing object should fail");
            } catch {
                //expected
            }

            client.Owners.Unclaim(owner.Username, smartObject.DeviceId);

            try {
                client.Owners.Unclaim(owner.Username, smartObject.DeviceId);
                Assert.Fail("Unclaim on an already unclaimed object should fail");
            } catch {
                //expected
            }
        }