public void Test_0313_Upload_50()
        {
            KiiAnalytics.Initialize("appID005", "appKey", KiiAnalytics.Site.JP, "dev005");

            KiiEvent[] eventList = new KiiEvent[50];
            for (int i = 0; i < eventList.Length; ++i)
            {
                KiiEvent ev = KiiAnalytics.NewEvent("type1");
                ev["page"]   = i;
                eventList[i] = ev;
            }

            // set mock
            KiiAnalytics.HttpClientFactory = new MockHttpClientFactory();
            MockHttpClient client = ((MockHttpClientFactory)KiiAnalytics.HttpClientFactory).Client;

            client.AddResponse(204, "");

            KiiAnalytics.Upload(eventList);
        }
        public void Test_0341_Upload_CloudException_broken_json()
        {
            KiiAnalytics.Initialize("appID004", "appKey", KiiAnalytics.Site.JP, "dev004");
            KiiEvent[] events = CreateEvents();

            // set mock
            KiiAnalytics.HttpClientFactory = new MockHttpClientFactory();
            MockHttpClient client = ((MockHttpClientFactory)KiiAnalytics.HttpClientFactory).Client;

            client.AddResponse(new CloudException(400, "broken"));

            try
            {
                KiiAnalytics.Upload(events);
                Assert.Fail("Exception must be thrown");
            }
            catch (EventUploadException e)
            {
                Assert.AreEqual(3, e.ErrorEvents.Count);
            }
        }
        public void TestRequestUrl()
        {
            KiiAnalytics.Initialize("dummyAppId", "dummyAppKey", KiiAnalytics.Site.CN3, "dummyDeviceId");
            MockHttpClientFactory factory = new MockHttpClientFactory();

            KiiAnalytics.HttpClientFactory      = factory;
            KiiAnalytics.AsyncHttpClientFactory = factory;
            MockHttpClient client = (MockHttpClient)factory.Client;

            // send request
            client.AddResponse(200, "{" +
                               "\"snapshots\" : [ {" +
                               "\"name\" : \"Male\"," +
                               "\"data\" : [ 546.775207 ]," +
                               "\"pointStart\" : 1338044400000," +
                               "\"pointInterval\" : 86400000}]}");
            KiiAnalytics.GetResult("ruleId", null, null);

            // check request url.
            Assert.AreEqual("https://api-cn3.kii.com/api/apps/dummyAppId/analytics/ruleId/data", client.RequestUrl[0]);
        }
        public void Test_0332_Upload_Partial_last()
        {
            KiiAnalytics.Initialize("appID004", "appKey", KiiAnalytics.Site.JP, "dev004");
            KiiEvent[] events = CreateEvents();

            // set mock
            KiiAnalytics.HttpClientFactory = new MockHttpClientFactory();
            MockHttpClient client = ((MockHttpClientFactory)KiiAnalytics.HttpClientFactory).Client;

            client.AddResponse(200,
                               "{" +
                               "\"errorCode\" : \"PARTIAL_SUCCESS\"," +
                               "\"message\" : \"There are some errors in event data\"," +
                               "\"invalidEvents\" : [ {" +
                               "\"index\" : 2," +
                               "\"errorDetails\" : [ {" +
                               "\"fieldName\" : \"_uploadedAt\"," +
                               "\"errorCode\" : \"UPLOADED_AT_MISSING\"," +
                               "\"message\" : \"You must provide event upload time\"}]" +
                               "}]" +
                               "}");

            try
            {
                KiiAnalytics.Upload(events);
                Assert.Fail("Exception must be thrown");
            }
            catch (EventUploadException e)
            {
                IList <KiiEvent> errorList = e.ErrorEvents;
                Assert.IsTrue(events[0].Sent);
                Assert.IsTrue(events[1].Sent);
                Assert.IsFalse(events[2].Sent);
                Assert.AreEqual(1, errorList.Count);
                KiiEvent error1 = errorList[0];
                Assert.AreEqual(3, error1["page"]);
                Assert.AreEqual("Next", error1["label"]);
            }
        }
        public void Test_0301_Upload_2()
        {
            KiiAnalytics.Initialize("appID004", "appKey", KiiAnalytics.Site.JP, "dev004");
            KiiEvent ev1 = KiiAnalytics.NewEvent("type1");

            ev1["page"]  = 1;
            ev1["label"] = "OK";

            KiiEvent ev2 = KiiAnalytics.NewEvent("type1");

            ev2["page"]  = 2;
            ev2["label"] = "Cancel";

            // set mock
            KiiAnalytics.HttpClientFactory = new MockHttpClientFactory();
            MockHttpClient client = ((MockHttpClientFactory)KiiAnalytics.HttpClientFactory).Client;

            client.AddResponse(204, "");

            KiiAnalytics.Upload(ev1, ev2);

            Assert.AreEqual("https://api-jp.kii.com/api/apps/appID004/events", client.RequestUrl[0]);
            Assert.AreEqual("application/vnd.kii.EventRecordList+json", client.RequestContentType[0]);
            JsonArray array = new JsonArray(client.RequestBody[0]);

            Assert.AreEqual(2, array.Length());
            JsonObject json = array.GetJsonObject(1);

            int count = ToCount(json.Keys());

            Assert.AreEqual(6, count);
            Assert.AreEqual(2, json.GetInt("page"));
            Assert.AreEqual("Cancel", json.GetString("label"));
            Assert.AreEqual("dev004", json.GetString("_deviceID"));
            Assert.AreEqual("type1", json.GetString("_type"));
            Assert.IsTrue(json.Has("_triggeredAt"));
            Assert.IsTrue(json.Has("_uploadedAt"));
        }
        public void Test_0322_Upload_sent_2_2()
        {
            KiiAnalytics.Initialize("appID004", "appKey", KiiAnalytics.Site.JP, "dev004");
            KiiEvent ev1 = KiiAnalytics.NewEvent("type1");

            ev1["page"]  = 1;
            ev1["label"] = "OK";

            // set mock
            KiiAnalytics.HttpClientFactory = new MockHttpClientFactory();
            MockHttpClient client = ((MockHttpClientFactory)KiiAnalytics.HttpClientFactory).Client;

            client.AddResponse(204, "");

            KiiAnalytics.Upload(ev1);

            KiiEvent ev2 = KiiAnalytics.NewEvent("type1");

            ev2["page"]  = 1;
            ev2["label"] = "OK";

            // set sent event
            KiiAnalytics.Upload(ev2, ev1);
        }
Esempio n. 7
0
        public void Test_1_2_GetResult_ConditionNull()
        {
            // set mock
            MockHttpClientFactory factory = new MockHttpClientFactory();

            KiiAnalytics.AsyncHttpClientFactory = factory;
            MockHttpClient client = factory.Client;

            client.AddResponse(200, "{" +
                               "\"snapshots\" : [ {" +
                               "\"name\" : \"Male\"," +
                               "\"data\" : [ 546.775207 ]," +
                               "\"pointStart\" : 1338044400000," +
                               "\"pointInterval\" : 86400000}, " +
                               "{" +
                               "\"name\" : \"Female\"," +
                               "\"data\" : [ 325.216381 ]," +
                               "\"pointStart\" : 1338044400000," +
                               "\"pointInterval\" : 86400000}" +
                               "]}");

            GroupedResult   result    = null;
            string          ruleId    = null;
            ResultCondition condition = null;
            Exception       exp       = null;
            CountDownLatch  cd        = new CountDownLatch(1);

            KiiAnalytics.GetResult("a", null, (string id, ResultCondition c, GroupedResult r, Exception e) => {
                Interlocked.Exchange(ref result, r);
                Interlocked.Exchange(ref ruleId, id);
                Interlocked.Exchange(ref condition, c);
                Interlocked.Exchange(ref exp, e);
                cd.Signal();
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.AreEqual(ruleId, "a");
            Assert.IsNull(condition);
            Assert.IsNotNull(result);
            Assert.IsNull(exp);

            Assert.IsNotNull(result.SnapShots);
            IList <GroupedSnapShot> snapshots = result.SnapShots;

            Assert.AreEqual(2, snapshots.Count);

            GroupedSnapShot snapShot1 = snapshots[0];

            Assert.AreEqual("Male", snapShot1.Name);
            Assert.AreEqual(1338044400000, snapShot1.PointStart);
            Assert.AreEqual(86400000, snapShot1.PointInterval);
            JsonArray data = snapShot1.Data;

            Assert.AreEqual(1, data.Length());

            GroupedSnapShot snapShot2 = snapshots[1];

            Assert.AreEqual("Female", snapShot2.Name);
            Assert.AreEqual(1338044400000, snapShot2.PointStart);
            Assert.AreEqual(86400000, snapShot2.PointInterval);
            data = snapShot2.Data;
            Assert.AreEqual(1, data.Length());
        }
        public void Test_1_8_Upload_PartialSuccess()
        {
            KiiAnalytics.Initialize("appID004", "appKey", KiiAnalytics.Site.JP, "dev004");
            KiiEvent[] events = CreateEvents();

            // set mock
            MockHttpClientFactory factory = new MockHttpClientFactory();

            KiiAnalytics.AsyncHttpClientFactory = factory;
            MockHttpClient client = factory.Client;

            client.AddResponse(200,
                               "{" +
                               "\"errorCode\" : \"PARTIAL_SUCCESS\"," +
                               "\"message\" : \"There are some errors in event data\"," +
                               "\"invalidEvents\" : [" +
                               "{" +
                               "\"index\" : 0," +
                               "\"errorDetails\" : [ {" +
                               "\"fieldName\" : \"_uploadedAt\"," +
                               "\"errorCode\" : \"UPLOADED_AT_MISSING\"," +
                               "\"message\" : \"You must provide event upload time\"}]" +
                               "}," +
                               "{" +
                               "\"index\" : 1," +
                               "\"errorDetails\" : [ {" +
                               "\"fieldName\" : \"_uploadedAt\"," +
                               "\"errorCode\" : \"UPLOADED_AT_MISSING\"," +
                               "\"message\" : \"You must provide event upload time\"}]" +
                               "}," +
                               "{" +
                               "\"index\" : 2," +
                               "\"errorDetails\" : [ {" +
                               "\"fieldName\" : \"_uploadedAt\"," +
                               "\"errorCode\" : \"UPLOADED_AT_MISSING\"," +
                               "\"message\" : \"You must provide event upload time\"}]" +
                               "}" +
                               "]" +
                               "}");

            // async upload
            Exception      exp = null;
            CountDownLatch cd  = new CountDownLatch(1);

            KiiAnalytics.Upload((Exception e) => {
                exp = e;
                cd.Signal();
            }, events);

            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.NotNull(exp);
            Assert.That(exp, Is.InstanceOf <EventUploadException> ());
            EventUploadException ue        = (EventUploadException)exp;
            IList <KiiEvent>     errorList = ue.ErrorEvents;

            Assert.IsFalse(events [0].Sent);
            Assert.IsFalse(events [1].Sent);
            Assert.IsFalse(events [2].Sent);
            Assert.AreEqual(3, errorList.Count);

            KiiEvent error1 = errorList [0];

            Assert.AreEqual(1, error1 ["page"]);
            Assert.AreEqual("OK", error1 ["label"]);

            KiiEvent error2 = errorList [1];

            Assert.AreEqual(2, error2 ["page"]);
            Assert.AreEqual("Cancel", error2 ["label"]);

            KiiEvent error3 = errorList [2];

            Assert.AreEqual(3, error3 ["page"]);
            Assert.AreEqual("Next", error3 ["label"]);
        }