Exemple #1
0
        public void Test_4_24_CreateWithQuery_InCloud_Patch_Overwrite_EtagNotMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{" +
                               "\"results\" : [" +
                               "{" +
                               "\"_created\" : 1," +
                               "\"_modified\" : 1," +
                               "\"key\" : \"value\"," +
                               "\"_id\" : \"abcd-1234\"," +
                               "\"_version\" : \"1\" " +
                               "}]" +
                               "}");

            KiiQueryResult <KiiObject> result = Kii.Bucket("test").Query(null);

            Assert.AreEqual(1, result.Count);
            KiiObject obj = result[0];

            string mockResponseBody = "{\"errorCode\" : \"OBJECT_VERSION_IN_STALE\"}";

            client.AddResponse(new CloudException(409, mockResponseBody));
            obj["key1"] = "value1";
            CloudException exp = null;

            try {
                obj.Save(true);
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(409, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", "abcd-1234");

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));

            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_2_15_CreateWithUri_InCloud_NoPatch_NotOverwrite_EtagMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // save object to cloud
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/" + objId));

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // server send error response (assume object already updated in server side)
            string mockResponseBody = "{\"errorCode\" : \"OBJECT_VERSION_IN_STALE\"}";

            client.AddResponse(new CloudException(409, mockResponseBody));
            obj["key1"] = "value1";
            CloudException exp = null;

            try {
                obj.SaveAllFields(false);
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(409, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);


            // request contains if-match
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("1", headerList["If-Match"]);

            string     reqBody          = "{ \"key\" : \"value\", \"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
Exemple #3
0
        public void RegisterGroupWithIDWithoutMembersSyncTest()
        {
            string groupID   = GetGroupID();
            string groupName = "group-" + DateTime.Now.Ticks.ToString();

            client.AddResponse(200, "{\"groupID\":\"" + groupID + "\"}");

            KiiGroup group = KiiGroup.RegisterGroupWithID(groupID, groupName, null);

            Assert.AreEqual(groupID, group.ID);
            Assert.AreEqual(groupName, group.Name);
            Assert.AreEqual(KiiUser.CurrentUser.ID, group.Owner.ID);

            JsonObject expectedRequestBody = new JsonObject();
            JsonArray  expectedMembers     = new JsonArray();

            expectedMembers.Put("user-member-0001");
            expectedMembers.Put("user-member-0002");
            expectedRequestBody.Put("owner", KiiUser.CurrentUser.ID);
            expectedRequestBody.Put("name", groupName);

            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod [0]);
            Assert.AreEqual("https://api.kii.com/api/apps/" + AppID + "/groups/" + groupID, client.RequestUrl [0]);
            KiiAssertion.AssertJson(expectedRequestBody, new JsonObject(client.RequestBody [0]));
            Assert.AreEqual("application/vnd.kii.GroupCreationRequest+json", client.RequestHeader[0]["content-type"]);
        }
        public void Test_2_6_CountWithUnsupportedQuery()
        {
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client           = factory.Client;
            string         mockResponseBody = "{\"errorCode\" : \"QUERY_NOT_SUPPORTED\"}";

            client.AddResponse(new CloudException(400, mockResponseBody));

            string    bucketName = "TestBucket";
            KiiBucket bucket     = Kii.Bucket(bucketName);
            KiiClause clause     = KiiClause.Equals("key", "value");
            KiiQuery  query      = new KiiQuery(clause);

            query.NextPaginationKey = "pkey";
            CloudException exp = null;

            try {
                bucket.Count(query);
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(400, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);

            // check request.
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "TestBucket", "query");

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]);
            MockHttpHeaderList headerList = client.RequestHeader[0];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            String queryStr = "{ " +
                              "\"bucketQuery\" : {" +
                              "\"clause\" : {" +
                              "\"type\" : \"eq\"," +
                              "\"field\" : \"key\"," +
                              "\"value\" : \"value\"" +
                              "}," +
                              "\"aggregations\" : [ {" +
                              "\"type\" : \"COUNT\"," +
                              "\"putAggregationInto\" : \"count_field\"" +
                              "}]" +
                              "}, " +
                              "\"paginationKey\" : \"pkey\"" +
                              "}";
            JsonObject expectedBodyJson = new JsonObject(queryStr);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[0]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_2_5_CountWithOrQuery()
        {
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            client.AddResponse(200, "{\"aggregations\" : { \"count_field\" : 5 } }");

            string    bucketName = "TestBucket";
            KiiBucket bucket     = Kii.Bucket(bucketName);
            KiiClause clause1    = KiiClause.Equals("key1", "value1");
            KiiClause clause2    = KiiClause.Equals("key2", "value2");
            KiiClause clause     = KiiClause.Or(clause1, clause2);
            KiiQuery  query      = new KiiQuery(clause);
            int       count      = bucket.Count(query);

            Assert.AreEqual(5, count);

            // check request.
            Console.WriteLine(client.RequestBody[0]);
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "TestBucket", "query");

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]);
            MockHttpHeaderList headerList = client.RequestHeader[0];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            String queryStr = "{ " +
                              "\"bucketQuery\" : {" +
                              "\"clause\" : {" +
                              "\"type\" : \"or\"," +
                              "\"clauses\" :[ {" +
                              "\"type\" : \"eq\"," +
                              "\"field\" : \"key1\"," +
                              "\"value\" : \"value1\"" +
                              "}," +
                              "{" +
                              "\"type\" : \"eq\"," +
                              "\"field\" : \"key2\"," +
                              "\"value\" : \"value2\"" +
                              "}]" +
                              "}," +
                              "\"aggregations\" : [ {" +
                              "\"type\" : \"COUNT\"," +
                              "\"putAggregationInto\" : \"count_field\"" +
                              "}]" +
                              "}" +
                              "}";
            JsonObject expectedBodyJson = new JsonObject(queryStr);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[0]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
Exemple #6
0
        public void Test_4_20_CreateWithQuery_InCloud_Patch_NotOverwrite_EtagMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{" +
                               "\"results\" : [" +
                               "{" +
                               "\"_created\" : 1," +
                               "\"_modified\" : 1," +
                               "\"key\" : \"value\"," +
                               "\"_id\" : \"abcd-1234\"," +
                               "\"_version\" : \"1\" " +
                               "}]" +
                               "}");

            KiiQueryResult <KiiObject> result = Kii.Bucket("test").Query(null);

            Assert.AreEqual(1, result.Count);
            KiiObject obj = result[0];

            client.AddResponse(201, "{\"_created\" : 1, \"_modified\" : 1, \"_id\" : \"abcd-1234\"}", "1");
            obj["key1"] = "value1";
            obj.Save(false);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", "abcd-1234");

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.AreEqual("1", headerList["If-Match"]);
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv=")); Assert.AreEqual("1", headerList["If-Match"]);

            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_2_22_CreateWithUri_InCloud_Patch_Overwrite_EtagNone()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save object to cloud
            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/" + objId));

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // set etag to null
            SDKTestHack.SetField(obj, "mEtag", null);

            client.AddResponse(201, "{\"_created\" : 1, \"_modified\" : 1}");
            obj["key1"] = "value1";

            // object save successfully as Overwrite is true.
            obj.Save(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);

            // request contains x-http-method-override
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_1_4_CountWhenBucketParentNotExists()
        {
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client           = factory.Client;
            string         mockResponseBody = "{\"errorCode\" : \"USER_NOT_FOUND\"}";

            client.AddResponse(new CloudException(404, mockResponseBody));

            string         bucketName = "TestBucket";
            KiiUser        user       = KiiUser.CreateByUri(new Uri("kiicloud://users/dummyUserId"));
            KiiBucket      bucket     = user.Bucket(bucketName);
            CloudException exp        = null;

            try {
                bucket.Count();
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(404, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);

            // check request.
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "users", "dummyUserId", "buckets", "TestBucket", "query");

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]);
            MockHttpHeaderList headerList = client.RequestHeader[0];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            string queryStr = "{ " +
                              "\"bucketQuery\" : {" +
                              "\"clause\" : {" +
                              "\"type\" : \"all\"" +
                              "}," +
                              "\"aggregations\" : [ {" +
                              "\"type\" : \"COUNT\"," +
                              "\"putAggregationInto\" : \"count_field\"" +
                              "}]" +
                              "}" +
                              "}";
            JsonObject expectedBodyJson = new JsonObject(queryStr);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[0]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_2_13_CreateWithUri_InCloud_NoPatch_NotOverwrite_EtagNone()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // set response
            MockHttpClient client           = factory.Client;
            string         mockResponseBody = "{" +
                                              "\"errorCode\" : \"OBJECT_ALREADY_EXISTS\"," +
                                              "\"message\" : \"The object with specified id already exists\" " +
                                              "}";

            client.AddResponse(new CloudException(404, mockResponseBody));


            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/" + objId));

            obj["key"] = "value";
            CloudException exp = null;

            try {
                obj.SaveAllFields(false);
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(404, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod[0]);
            MockHttpHeaderList headerList = client.RequestHeader[0];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.AreEqual("*", headerList["If-None-Match"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));

            string     reqBody          = "{ \"key\" : \"value\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[0]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_1_16_CreateWithId_InCloud_NoPatch_Overwrite_EtagNone()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save object to cloud
            KiiObject obj = Kii.Bucket("test").NewKiiObject(objId);

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // Set Etag to null
            SDKTestHack.GetField(obj, "mEtag");
            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save successful as overwrite is true.
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            string     reqBody          = "{ \"key\" : \"value\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_3_20_CreateWithNoId_InCloud_Patch_NoOverwrite_EtagMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"objectID\" : \"abcd-1234\", \"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            KiiObject obj = Kii.Bucket("test").NewKiiObject();

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);


            client.AddResponse(201, "{\"_created\" : 1, \"_modified\" : 1}");

            obj["key1"] = "value1";
            obj.Save(false);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);

            // request contains x-http-method-override, if-match header
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            Assert.AreEqual("1", headerList["If-Match"]);
            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_1_10_CreateWithId_notInCloud_Patch_overwrite_etagNone()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // set response
            MockHttpClient client           = factory.Client;
            string         mockResponseBody = "{" +
                                              "\"errorCode\" : \"OBJECT_NOT_FOUND\"," +
                                              "\"message\" : \"The object with specified id not found\" " +
                                              "}";

            client.AddResponse(new CloudException(404, mockResponseBody));

            KiiObject obj = Kii.Bucket("test").NewKiiObject(objId);

            obj["key"] = "value";
            CloudException exp = null;

            try {
                obj.Save(true);
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(404, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]);
            MockHttpHeaderList headerList = client.RequestHeader[0];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));

            string     reqBody          = "{ \"key\" : \"value\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[0]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_2_14_CreateWithUri_InCloud_NoPatch_NotOverwrite_EtagMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // save object to cloud
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            KiiObject obj = KiiObject.CreateByUri(new Uri("kiicloud://buckets/test/objects/" + objId));

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);

            // update object
            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");
            obj["key1"] = "value1";
            obj.SaveAllFields(false);

            // check request
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("1", headerList["If-Match"]);

            string     reqBody          = "{ \"key\" : \"value\", \"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
Exemple #14
0
        public void RegisterGroupWithIDASyncTest()
        {
            string         groupID   = GetGroupID();
            string         groupName = "group-" + DateTime.Now.Ticks.ToString();
            List <KiiUser> members   = new List <KiiUser>();

            members.Add(KiiUser.CreateByUri(new Uri("kiicloud://users/user-member-0001")));
            members.Add(KiiUser.CreateByUri(new Uri("kiicloud://users/user-member-0002")));

            client.AddResponse(200, "{\"groupID\":\"" + groupID + "\"}");

            CountDownLatch cd        = new CountDownLatch(1);
            KiiGroup       group     = null;
            Exception      exception = null;

            KiiGroup.RegisterGroupWithID(groupID, groupName, members, (KiiGroup result, Exception e) => {
                group     = result;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);

            Assert.AreEqual(groupID, group.ID);
            Assert.AreEqual(groupName, group.Name);
            Assert.AreEqual(KiiUser.CurrentUser.ID, group.Owner.ID);

            JsonObject expectedRequestBody = new JsonObject();
            JsonArray  expectedMembers     = new JsonArray();

            expectedMembers.Put("user-member-0001");
            expectedMembers.Put("user-member-0002");
            expectedRequestBody.Put("owner", KiiUser.CurrentUser.ID);
            expectedRequestBody.Put("name", groupName);
            expectedRequestBody.Put("members", expectedMembers);

            Assert.AreEqual(KiiHttpMethod.PUT, client.RequestMethod [0]);
            Assert.AreEqual("https://api.kii.com/api/apps/" + AppID + "/groups/" + groupID, client.RequestUrl [0]);
            KiiAssertion.AssertJson(expectedRequestBody, new JsonObject(client.RequestBody [0]));
            Assert.AreEqual("application/vnd.kii.GroupCreationRequest+json", client.RequestHeader[0]["content-type"]);
        }
        public void Test_1_3_CountAllWhenObjectExists()
        {
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"aggregations\" : { \"count_field\" : 10 } }");

            string    bucketName = "TestBucket";
            KiiBucket bucket     = Kii.Bucket(bucketName);
            int       count      = bucket.Count();

            Assert.AreEqual(10, count);

            // check request.
            Console.WriteLine(client.RequestBody[0]);
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "TestBucket", "query");

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]);
            MockHttpHeaderList headerList = client.RequestHeader[0];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            string queryStr = "{ " +
                              "\"bucketQuery\" : {" +
                              "\"clause\" : {" +
                              "\"type\" : \"all\"" +
                              "}," +
                              "\"aggregations\" : [ {" +
                              "\"type\" : \"COUNT\"," +
                              "\"putAggregationInto\" : \"count_field\"" +
                              "}]" +
                              "}" +
                              "}";
            JsonObject expectedBodyJson = new JsonObject(queryStr);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[0]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_1_2_CountAllAsync()
        {
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.AsyncHttpClientFactory = factory;

            // set response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"aggregations\" : { \"count_field\" : 10 } }");

            string    bucketName = "TestBucket";
            KiiBucket bucket     = Kii.Bucket(bucketName);

            KiiBucket      callbackBucket = null;
            KiiQuery       callbackQuery  = null;
            int            count          = -1;
            Exception      exp            = null;
            CountDownLatch cd             = new CountDownLatch(1);

            bucket.Count((KiiBucket b, KiiQuery q, int c, Exception e) => {
                callbackBucket = b;
                callbackQuery  = q;
                count          = c;
                exp            = e;
                cd.Signal();
            });

            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNotNull(callbackBucket);
            Assert.AreEqual(bucket.Name, callbackBucket.Name);
            Assert.IsNotNull(callbackQuery);
            KiiAssertion.AssertJson(new KiiQuery().toJson(), callbackQuery.toJson());
            Assert.IsNull(exp);
            Assert.AreEqual(10, count);

            // check request.
            Console.WriteLine(client.RequestBody[0]);
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "TestBucket", "query");

            Assert.AreEqual(url, client.RequestUrl[0]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]);
            MockHttpHeaderList headerList = client.RequestHeader[0];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            string queryStr = "{ " +
                              "\"bucketQuery\" : {" +
                              "\"clause\" : {" +
                              "\"type\" : \"all\"" +
                              "}," +
                              "\"aggregations\" : [ {" +
                              "\"type\" : \"COUNT\"," +
                              "\"putAggregationInto\" : \"count_field\"" +
                              "}]" +
                              "}" +
                              "}";
            JsonObject expectedBodyJson = new JsonObject(queryStr);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[0]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }
        public void Test_1_21_CreateWithId_InCloud_Patch_NotOverwrite_EtagNotMatch()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;
            string objId = "abcd-1234";

            // prepare response
            MockHttpClient client = factory.Client;

            client.AddResponse(201, "{\"createdAt\" : 1, \"modifiedAt\" : 1}", "1");

            // save object to cloud
            KiiObject obj = Kii.Bucket("test").NewKiiObject(objId);

            obj["key"] = "value";
            obj.SaveAllFields(true);
            Assert.AreEqual("abcd-1234", obj.ID);
            Assert.AreEqual(1, obj.CreatedTime);
            Assert.AreEqual(1, obj.ModifedTime);
            string etag = (string)SDKTestHack.GetField(obj, "mEtag");

            Assert.AreEqual("1", etag);


            string mockResponseBody = "{" +
                                      "\"errorCode\" : \"OBJECT_VERSION_IN_STALE\"," +
                                      "\"message\" : \"object version did not matched\" " +
                                      "}";

            client.AddResponse(new CloudException(409, mockResponseBody));


            obj["key1"] = "value1";
            CloudException exp = null;

            try {
                obj.Save(false);
                Assert.Fail("Exception not thrown");
            } catch (CloudException e) {
                exp = e;
            }
            Assert.IsNotNull(exp);
            Assert.AreEqual(409, exp.Status);
            Assert.AreEqual(mockResponseBody, exp.Body);

            // request contains x-http-method-override, if-match header
            string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "test", "objects", objId);

            Assert.AreEqual(url, client.RequestUrl[1]);
            Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[1]);
            MockHttpHeaderList headerList = client.RequestHeader[1];

            Assert.AreEqual("appId", headerList["X-Kii-AppID"]);
            Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]);
            Assert.AreEqual("PATCH", headerList["X-HTTP-Method-Override"]);
            Assert.IsTrue(headerList["X-Kii-SDK"].StartsWith("sn=cs;sv="));
            Assert.AreEqual("1", headerList["If-Match"]);
            string     reqBody          = "{\"key1\" : \"value1\"}";
            JsonObject expectedBodyJson = new JsonObject(reqBody);
            JsonObject actualBodyJson   = new JsonObject(client.RequestBody[1]);

            KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson);
        }