Beispiel #1
0
        public void Test_1_5_GetResult_CallbackNull()
        {
            // 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}]}");

            // call with null callback.
            KiiAnalytics.GetResult("a", null, null);
        }
        public void Test_1_7_Upload()
        {
            KiiAnalytics.Initialize("appID004", "appKey", KiiAnalytics.Site.JP, "dev004");
            KiiEvent ev = KiiAnalytics.NewEvent("type1");

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

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

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

            client.AddResponse(204, "");

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

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

            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.Null(exp);
            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]);
            JsonObject json  = array.GetJsonObject(0);
            int        count = ToCount(json.Keys());

            Assert.AreEqual(6, count);
            Assert.AreEqual(1, json.GetInt("page"));
            Assert.AreEqual("OK", 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 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]);
        }
Beispiel #4
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"]);
        }