public void Test_1_9_Upload_FailByCloudException()
        {
            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(new CloudException(400, ""));

            // 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"]);
        }
        public void Test_1_2_AsyncUpload_PartialSuccess()
        {
            // create events
            KiiEvent ev1 = KiiAnalytics.NewEvent("type1");

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

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

            ev1 ["page"]   = 1;
            ev1 ["labels"] = new string[] {};

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

            KiiAnalytics.Upload((Exception e) => {
                Interlocked.Exchange(ref exp, e);
                cd.Signal();
            }, new KiiEvent[] { ev1, ev2 });

            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;

            Assert.AreEqual(200, ue.Status);

            // check response body
            JsonObject json = new JsonObject(ue.Body);

            Assert.AreEqual("PARTIAL_SUCCESS", json.GetString("errorCode"));
            JsonArray invalidEvents = json.GetJsonArray("invalidEvents");
            int       length        = invalidEvents.Length();

            Assert.AreEqual(1, length);
            JsonObject invalidEvent = invalidEvents.GetJsonObject(0);

            Assert.AreEqual(0, invalidEvent.GetInt("index"));
        }
        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"]);
        }