public void Test_1_3_GetResult_Fail() { // set mock MockHttpClientFactory factory = new MockHttpClientFactory(); KiiAnalytics.AsyncHttpClientFactory = factory; MockHttpClient client = factory.Client; client.AddResponse(new CloudException(400, "")); 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.IsNull(result); Assert.IsNotNull(exp); Assert.True(exp is CloudException); }
public void Test_1_2_GetResult_RuleIdNotExists() { GroupedResult result = null; string ruleId = null; ResultCondition condition = null; Exception exp = null; CountDownLatch cd = new CountDownLatch(1); KiiAnalytics.GetResult("0001", 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, "0001"); Assert.IsNull(condition); Assert.IsNull(result); Assert.IsNotNull(exp); Assert.That(exp, Is.InstanceOf <CloudException> ()); CloudException ex = (CloudException)exp; Assert.AreEqual(404, ex.Status); JsonObject json = new JsonObject(ex.Body); Assert.AreEqual("NOT_FOUND", json.GetString("errorCode")); }
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_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 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_1_GetResult() { GroupedResult result = null; string ruleId = null; ResultCondition condition = null; Exception exp = null; CountDownLatch cd = new CountDownLatch(1); KiiAnalytics.GetResult(RULE_ID, 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, RULE_ID); Assert.IsNull(condition); Assert.IsNotNull(result); Assert.IsNull(exp); Assert.IsNotNull(result.SnapShots); IList <GroupedSnapShot> snapshots = result.SnapShots; Assert.AreEqual(1, snapshots.Count); GroupedSnapShot snapShot1 = snapshots[0]; Assert.AreEqual("DEFAULT", snapShot1.Name); Assert.IsTrue(snapShot1.PointStart > 0); Assert.IsTrue(snapShot1.PointInterval > 0); JsonArray data = snapShot1.Data; Assert.AreEqual(1, data.Length()); }
public void Test_1_1_AsyncUpload() { // create events KiiEvent ev1 = KiiAnalytics.NewEvent("type1"); ev1 ["page"] = 1; ev1 ["label"] = "OK"; // async upload Exception exp = null; CountDownLatch cd = new CountDownLatch(1); KiiAnalytics.Upload((Exception e) => { Interlocked.Exchange(ref exp, e); cd.Signal(); }, new KiiEvent[] { ev1 }); if (!cd.Wait(new TimeSpan(0, 0, 0, 3))) { Assert.Fail("Callback not fired."); } Assert.Null(exp); }
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"]); }