Exemplo n.º 1
0
        public void EventSerializeTestAttributeListAsInt()
        {
            Dictionary <string, object> timeseries = new Dictionary <string, object>();

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

            Event eventBuilt = new Event.Builder()
            {
                Timeseries = new Dictionary <string, object>()
                {
                    { "list", new List <int>()
                      {
                          1, 2
                      } }
                },
                EventType = "test"
            };

            string json = EventSerializer.SerializeEvent(eventBuilt);

            Assert.AreEqual("{\"x_event_type\":\"test\",\"list\":[1,2]}", json);
        }
        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 EventBuilderAddTimeseriesTest()
        {
            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);

            Event eventBuilt = new Event.Builder()
            {
                EventType  = "event type",
                Timeseries = new Dictionary <string, object>()
                {
                    { "String", "text" },
                    { "int", 10 },
                    { "double", 10.7 },
                    { "boolean", true }
                }
            };

            Assert.IsNull(eventBuilt.EventId);
            Assert.True(eventBuilt.EventType.Equals("event type"));
            Assert.IsNull(eventBuilt.DeviceId);
            Assert.IsNull(eventBuilt.Timestamp);
            CollectionAssert.AreEqual(eventBuilt.Timeseries, attributes);
        }
Exemplo n.º 4
0
        public void EventSerializeTestWithEmptyObject()
        {
            Event eventBuilt = new Event.Builder()
            {
                EventType = "test"
            };

            string json = EventSerializer.SerializeEvent(eventBuilt);

            Assert.AreEqual("{\"x_event_type\":\"test\"}", json);
        }
        public TaskCompletionSource <string> DispatchEventWithResponseCallback()
        {
            stringOutput = new TaskCompletionSource <string>();
            var data = new Dictionary <string, Java.Lang.Object>();

            data.Add("testEvent", true);
            Event sdkEvent = new Event.Builder("eventName", "eventType", "eventSource").SetEventData(data).Build();

            stringOutput.SetResult(ACPCore.DispatchEventWithResponseCallback(sdkEvent, new StringCallback(), new ErrorCallback()).ToString());
            return(stringOutput);
        }
        public void EventBuilderEventTypeEmpty()
        {
            Event eventBuilt;

            Assert.That(() => eventBuilt = new Event.Builder()
            {
                EventType = "",
            },
                        Throws.TypeOf <ArgumentException>()
                        .With.Message.EqualTo("x_event_type cannot be null or empty"));
        }
        public void EventBuilderEventEmpty()
        {
            Event eventBuilt = new Event.Builder()
            {
                EventType = "event type"
            };

            Assert.IsNull(eventBuilt.EventId);
            Assert.IsNull(eventBuilt.DeviceId);
            Assert.IsNull(eventBuilt.Timestamp);
            Assert.True(eventBuilt.Timeseries.Count == 0);
            Assert.True(eventBuilt.EventType.Equals("event type"));
        }
        public void DispatchValidSDKEventWithCallback_Returns_True()
        {
            // setup
            var data = new Dictionary <string, Java.Lang.Object>();

            data.Add("testEvent", true);
            Event sdkEvent = new Event.Builder("eventName", "eventType", "eventSource").SetEventData(data).Build();
            // test
            var status = ACPCore.DispatchEventWithResponseCallback(sdkEvent, new StringCallback(), new ErrorCallback());

            // verify
            Assert.That(status, Is.EqualTo(true));
        }
Exemplo n.º 9
0
        public void EventSerializeTestWithOwner()
        {
            Event eventBuilt = new Event.Builder()
            {
                EventType = "test",
                DeviceId  = "deviceId"
            };

            string json = EventSerializer.SerializeEvent(eventBuilt);

            Assert.AreEqual(
                "{\"x_event_type\":\"test\",\"x_object\":{\"x_device_id\":\"deviceId\"}}",
                json);
        }
Exemplo n.º 10
0
        public void TestSendEvents()
        {
            Guid   uuid1  = Guid.NewGuid();
            String value1 = "value-" + uuid1.ToString();
            Event  event1 = new Event.Builder()
            {
                EventId    = uuid1,
                EventType  = "event_type1",
                DeviceId   = "device-" + uuid1.ToString(),
                Timeseries = new Dictionary <string, object>()
                {
                    { "ts_text_attribute", value1 }
                }
            };

            Guid   uuid2  = Guid.NewGuid();
            String value2 = "value-" + uuid2.ToString();
            Event  event2 = new Event.Builder()
            {
                EventId    = uuid2,
                EventType  = "event_type1",
                DeviceId   = "device-" + uuid2.ToString(),
                Timeseries = new Dictionary <string, object>()
                {
                    { "ts_text_attribute", value2 }
                }
            };

            IEnumerable <EventResult> results = client.Events.Send(new List <Event>()
            {
                event1, event2
            });

            foreach (EventResult result in results)
            {
                Assert.AreEqual(EventResult.ResultStates.success, result.Result);
            }
        }
Exemplo n.º 11
0
        public void EventSerializeTest()
        {
            DateTime now = TestUtils.GetNowIgnoringMilis();

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

            timeseries.Add("string", "stringValue");
            timeseries.Add("double", 10d);
            timeseries.Add("float", 10.5f);
            timeseries.Add("boolean", false);

            Event eventBuilt = new Event.Builder()
            {
                EventId    = Guid.Parse("98c62f5c-ad48-4ef8-8d70-dbe3a1e8b17f"),
                Timestamp  = now,
                EventType  = "type",
                Timeseries = new Dictionary <string, object>()
                {
                    { "string", "stringValue" },
                    { "double", 10d },
                    { "float", 10.5f },
                    { "boolean", false }
                }
            };

            string json = EventSerializer.SerializeEvent(eventBuilt);

            TestUtils.AssertJsonEquals(json, new List <string>
            {
                "\"x_event_type\":\"type\"",
                $"\"x_timestamp\":\"{now.ToString(DatetimeFormat)}\"",
                "\"event_id\":\"98c62f5c-ad48-4ef8-8d70-dbe3a1e8b17f\"",
                "\"boolean\":false",
                "\"float\":10.5",
                "\"double\":10.0",
                "\"string\":\"stringValue\""
            });
        }
Exemplo n.º 12
0
        /// <summary>
        /// deserialize a json string to an event instance
        /// </summary>
        /// <param name="obj">json string</param>
        /// <returns>event instance</returns>
        public static Event DeserializeEvent(string obj)
        {
            Event.Builder builder = new Event.Builder();
            Dictionary <string, object> timeseries = new Dictionary <string, object>();

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

            foreach (KeyValuePair <string, object> token in rawEvent)
            {
                switch (token.Key)
                {
                case TimestampProperty:
                {
                    if (token.Value == null || !(token.Value is DateTime))
                    {
                        throw new InvalidOperationException("Field 'x_timestamp' does not match TYPE 'DATETIME'");
                    }
                    builder.Timestamp = (DateTime)token.Value;
                    break;
                }

                case EventTypeProperty:
                {
                    if (!(token.Value is string))
                    {
                        throw new InvalidOperationException("Field 'x_event_type' does not match TYPE 'TEXT'");
                    }
                    builder.EventType = 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 ObjectProperty:
                {
                    if (token.Value == null || !(token.Value is JObject))
                    {
                        throw new InvalidOperationException("Field 'x_object' does not match TYPE 'SMARTOBJECT'");
                    }

                    var deviceId = (token.Value as JObject)[ObjectSerializer.DeviceIdProperty];
                    if (deviceId != null)
                    {
                        if (deviceId.Type != JTokenType.String)
                        {
                            throw new InvalidOperationException("Field 'x_object' does not match TYPE 'SMARTOBJECT'");
                        }
                        builder.DeviceId = deviceId.Value <string>();
                    }
                    break;
                }

                default:
                {
                    if (token.Value is JArray)
                    {
                        timeseries.Add(token.Key, (token.Value as JArray).ToObject <string[]>());
                    }
                    else
                    {
                        timeseries.Add(token.Key, token.Value);
                    }
                    break;
                }
                }
            }
            builder.Timeseries = timeseries;
            return(builder.Build());
        }