public void Test_0005_GeoBoxQuery()
        {
            KiiBucket   bucket = testUser.Bucket("aBucket");
            KiiObject   obj    = bucket.NewKiiObject();
            KiiGeoPoint point  = new KiiGeoPoint(35.667983, 139.739356);

            obj.SetGeoPoint("myloc", point);
            obj.Save();
            //not in the box
            KiiObject   obj2   = bucket.NewKiiObject();
            KiiGeoPoint point2 = new KiiGeoPoint();

            obj2.SetGeoPoint("myloc", point2);
            obj2.Save();

            KiiGeoPoint sw = new KiiGeoPoint(35.52105, 139.699402);
            KiiGeoPoint ne = new KiiGeoPoint(36.069082, 140.07843);

            KiiClause clause = KiiClause.GeoBox("myloc", ne, sw);
            KiiQuery  query  = new KiiQuery(clause);
            KiiQueryResult <KiiObject> result = bucket.Query(query);

            Assert.AreEqual(result.Count, 1);
            KiiObject   retObj = result [0];
            KiiGeoPoint retPoint;

            retPoint = retObj.GetGeoPoint("myloc");
            Assert.AreEqual(point.Latitude, retPoint.Latitude);
            Assert.AreEqual(point.Longitude, retPoint.Longitude);
        }
Esempio n. 2
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_0001_GeoDistanceQuery()
        {
            KiiBucket   bucket = testUser.Bucket("aBucket");
            KiiObject   obj    = bucket.NewKiiObject();
            KiiGeoPoint point  = new KiiGeoPoint(35.667983, 139.739356);

            obj.SetGeoPoint("myLoc", point);
            obj.Save();

            KiiGeoPoint center = new KiiGeoPoint(35.677379, 139.702148);
            KiiClause   clause = KiiClause.GeoDistance("myloc", center, 4000, "distanceToMyLoc");
            KiiQuery    query  = new KiiQuery(clause);
            KiiQueryResult <KiiObject> result = bucket.Query(query);
            KiiObject   retObj = result [0];
            KiiGeoPoint retPoint;

            retPoint = retObj.GetGeoPoint("myLoc");
            Assert.AreEqual(point.Latitude, retPoint.Latitude);
            Assert.AreEqual(point.Longitude, retPoint.Longitude);
            JsonObject jObj             = retObj.GetJsonObject("_calculated");
            double     retDistance      = jObj.GetDouble("distanceToMyLoc");
            double     expectedDistance = distanceInMeter(point, center);

            Assert.IsTrue(approximateEqual(expectedDistance, retDistance, 0.00001));
        }
        public void NotPrefixTest()
        {
            KiiBucket bucket = KiiUser.CurrentUser.Bucket("my_bucket");
            KiiObject obj1   = bucket.NewKiiObject();

            obj1["name"] = "foo";
            obj1.Save();
            KiiObject obj2 = bucket.NewKiiObject();

            obj2["name"] = "fool";
            obj2.Save();
            KiiObject obj3 = bucket.NewKiiObject();

            obj3["name"] = "hoge";
            obj3.Save();

            KiiQuery query = new KiiQuery(KiiClause.Not(KiiClause.StartsWith("name", "foo")));

            query.SortByAsc("name");

            KiiQueryResult <KiiObject> results = bucket.Query(query);

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("hoge", results[0].GetString("name"));
        }
        public void Test_2_6_CountWithUnsupportedQuery()
        {
            string    bucketName = "bucket" + CurrentTimeMillis();
            KiiBucket bucket     = Kii.Bucket(bucketName);

            for (int i = 0; i < 2; i++)
            {
                KiiObject obj = bucket.NewKiiObject();
                obj ["intField"] = i;
                obj.Save();
            }


            KiiQuery query = new KiiQuery(null);

            query.Limit = 1;

            KiiQueryResult <KiiObject> result = bucket.Query(query);

            Assert.AreEqual(1, result.Count);
            query = result.NextKiiQuery;
            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);
            JsonObject body = new JsonObject(exp.Body);

            Assert.AreEqual("QUERY_NOT_SUPPORTED", body.GetString("errorCode"));
        }
        public void NotHasFieldTest()
        {
            KiiBucket bucket = KiiUser.CurrentUser.Bucket("my_bucket");
            KiiObject obj1   = bucket.NewKiiObject();

            obj1["name"] = "foo";
            obj1["age"]  = 20;
            obj1.Save();
            KiiObject obj2 = bucket.NewKiiObject();

            obj2["name"] = "bar";
            obj2["age"]  = 33;
            obj2.Save();
            KiiObject obj3 = bucket.NewKiiObject();

            obj3["name"] = "hoge";
            obj3.Save();

            KiiQuery query = new KiiQuery(KiiClause.Not(KiiClause.HasField("age", FieldType.INTEGER)));

            query.SortByAsc("name");

            KiiQueryResult <KiiObject> results = bucket.Query(query);

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("hoge", results[0].GetString("name"));
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        private void ExecQuery(KiiQuery query, KiiHttpClientFactory factory, KiiQueryCallback <KiiObject> callback)
        {
            Utils.CheckInitialize(false);

            KiiHttpClient client = factory.Create(QueryUrl, Kii.AppId, Kii.AppKey, KiiHttpMethod.POST);

            KiiCloudEngine.SetAuthBearer(client);
            client.ContentType = "application/vnd.kii.QueryRequest+json";

            if (query == null)
            {
                query = new KiiQuery(null);
            }

            // send request
            client.SendRequest(query.ToString(), (ApiResponse response, Exception e) =>
            {
                if (e != null)
                {
                    if (callback != null)
                    {
                        callback(null, e);
                    }
                    return;
                }
                // parse response
                KiiQueryResult <KiiObject> queryResult = null;
                try
                {
                    JsonObject obj           = new JsonObject(response.Body);
                    JsonArray array          = obj.GetJsonArray("results");
                    String nextPaginationKey = obj.OptString("nextPaginationKey");
                    queryResult = new KiiQueryResult <KiiObject>(query, nextPaginationKey, this, false);

                    for (int i = 0; i < array.Length(); i++)
                    {
                        JsonObject entry    = array.GetJsonObject(i);
                        KiiObject kiiObject = new KiiObject(mParent, mBucketName, entry);
                        queryResult.Add(kiiObject);
                    }
                }
                catch (JsonException e2)
                {
                    if (callback != null)
                    {
                        callback(null, new IllegalKiiBaseObjectFormatException(e2.Message));
                    }
                    return;
                }
                if (callback != null)
                {
                    callback(queryResult, null);
                }
            });
        }
        public void Test_0004_GeoDistanceQuery_sort_asc()
        {
            KiiBucket   bucket = testUser.Bucket("aBucket");
            KiiObject   obj1   = bucket.NewKiiObject();
            KiiGeoPoint point1 = new KiiGeoPoint(35.672568, 139.723606);

            obj1.SetGeoPoint("myLoc", point1);
            obj1.Save();

            KiiObject   obj2   = bucket.NewKiiObject();
            KiiGeoPoint point2 = new KiiGeoPoint(35.667983, 139.739356);

            obj2.SetGeoPoint("myLoc", point2);
            obj2.Save();
            // not in radius
            KiiObject   obj3   = bucket.NewKiiObject();
            KiiGeoPoint point3 = new KiiGeoPoint();

            obj3.SetGeoPoint("myLoc", point3);
            obj3.Save();

            KiiGeoPoint center = new KiiGeoPoint(35.677379, 139.702148);
            KiiClause   clause = KiiClause.GeoDistance("myloc", center, 4000, "distanceToMyLoc");
            KiiQuery    query  = new KiiQuery(clause);

            query.SortByAsc("_calculated.distanceToMyLoc");

            KiiQueryResult <KiiObject> result = bucket.Query(query);

            Assert.AreEqual(result.Count, 2);
            KiiObject   retObj1   = result [0];
            KiiGeoPoint retPoint1 = retObj1.GetGeoPoint("myLoc");

            Assert.AreEqual(point1.Latitude, retPoint1.Latitude);
            Assert.AreEqual(point1.Longitude, retPoint1.Longitude);
            JsonObject jObj1 = retObj1.GetJsonObject("_calculated");

            KiiObject   retObj2   = result [1];
            KiiGeoPoint retPoint2 = retObj2.GetGeoPoint("myLoc");

            Assert.AreEqual(point2.Latitude, retPoint2.Latitude);
            Assert.AreEqual(point2.Longitude, retPoint2.Longitude);
            JsonObject jObj2 = retObj2.GetJsonObject("_calculated");

            double retDistance1      = jObj1.GetDouble("distanceToMyLoc");
            double retDistance2      = jObj2.GetDouble("distanceToMyLoc");
            double expectedDistance1 = distanceInMeter(point1, center);
            double expectedDistance2 = distanceInMeter(point2, center);

            Assert.IsTrue(approximateEqual(expectedDistance1, retDistance1, 0.00001));
            Assert.IsTrue(approximateEqual(expectedDistance2, retDistance2, 0.00001));
        }
Esempio n. 10
0
        /// <summary>
        /// Query KiiObjects in this bucket.
        /// </summary>
        /// <remarks>
        /// Query KiiObjects in this bucket with conditions given by argument.
        /// </remarks>
        /// <param name='query'>
        /// Query conditions.
        /// </param>
        /// <returns>
        /// List of KiiObject. If number of result is big, please call GetNextQueryResult()
        /// </returns>
        /// <exception cref='IllegalKiiBaseObjectFormatException'>
        /// Is thrown when server sends broken Json.
        /// </exception>
        /// <exception cref='CloudException'>
        /// Is thrown when server sends error response.
        /// </exception>
        public KiiQueryResult <KiiObject> Query(KiiQuery query)
        {
            KiiQueryResult <KiiObject> result = null;

            ExecQuery(query, Kii.HttpClientFactory, (KiiQueryResult <KiiObject> r, Exception e) =>
            {
                if (e != null)
                {
                    throw e;
                }
                result = r;
            });
            return(result);
        }
        public void Test_0100_query_null()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = (MockHttpClient)factory.Client;

            this.SetDefaultQueryResult(client);

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

            Assert.AreEqual(1, result.Count);
        }
        public void Test_0103_query_broken_json()
        {
            // set response
            client.AddResponse(200, "{}");

            KiiBucket bucket = Kii.Bucket("test");
            bool      done   = false;
            KiiQueryResult <KiiObject> result = null;
            Exception exception = null;

            bucket.Query(null, (KiiQueryResult <KiiObject> list, Exception e) =>
            {
                done      = true;
                result    = list;
                exception = e;
            });
            Assert.IsTrue(done);
            Assert.IsNull(result);
            Assert.IsNotNull(exception);
            Assert.IsTrue(exception is IllegalKiiBaseObjectFormatException);
        }
        public void Test_0104_query_server_error()
        {
            // set response
            client.AddResponse(new CloudException(400, "{}"));

            KiiBucket bucket = Kii.Bucket("test");
            bool      done   = false;
            KiiQueryResult <KiiObject> result = null;
            Exception exception = null;

            bucket.Query(null, (KiiQueryResult <KiiObject> list, Exception e) =>
            {
                done      = true;
                result    = list;
                exception = e;
            });
            Assert.IsTrue(done);
            Assert.IsNull(result);
            Assert.IsNotNull(exception);
            Assert.IsTrue(exception is CloudException);
        }
        public void Test_0002_GeoDistanceQuery_calculatedDistance_nil()
        {
            KiiBucket   bucket = testUser.Bucket("aBucket");
            KiiObject   obj    = bucket.NewKiiObject();
            KiiGeoPoint point  = new KiiGeoPoint(35.667983, 139.739356);

            obj.SetGeoPoint("myLoc", point);
            obj.Save();

            KiiGeoPoint center = new KiiGeoPoint(35.677379, 139.702148);
            KiiClause   clause = KiiClause.GeoDistance("myloc", center, 4000, "");
            KiiQuery    query  = new KiiQuery(clause);
            KiiQueryResult <KiiObject> result = bucket.Query(query);
            KiiObject   retObj = result [0];
            KiiGeoPoint retPoint;

            retPoint = retObj.GetGeoPoint("myLoc");
            Assert.AreEqual(point.Latitude, retPoint.Latitude);
            Assert.AreEqual(point.Longitude, retPoint.Longitude);

            Assert.IsFalse(retObj.Has("_calculated"));
        }
        public void Test_0100_query_null()
        {
            // set response
            this.SetDefaultQueryResult();

            KiiBucket bucket = Kii.Bucket("test");
            bool      done   = false;
            KiiQueryResult <KiiObject> result = null;
            Exception exception = null;

            bucket.Query(null, (KiiQueryResult <KiiObject> list, Exception e) =>
            {
                done      = true;
                result    = list;
                exception = e;
            });
            Assert.IsTrue(done);
            Assert.IsNotNull(result);
            Assert.IsNull(exception);

            Assert.AreEqual(1, result.Count);
        }
Esempio n. 16
0
        public void NotAndTest()
        {
            KiiBucket bucket = KiiUser.CurrentUser.Bucket("my_bucket");
            KiiObject obj1   = bucket.NewKiiObject();

            obj1["name"] = "foo";
            obj1["age"]  = 20;
            obj1.Save();
            KiiObject obj2 = bucket.NewKiiObject();

            obj2["name"] = "foo";
            obj2["age"]  = 33;
            obj2.Save();

            KiiQuery query = new KiiQuery(KiiClause.Not(KiiClause.And(KiiClause.Equals("name", "foo"), KiiClause.LessThan("age", 30))));

            query.SortByAsc("name");

            KiiQueryResult <KiiObject> results = bucket.Query(query);

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual(33, results[0].GetInt("age"));
        }
Esempio n. 17
0
        public void NotGeoBoxTest()
        {
            KiiBucket bucket = KiiUser.CurrentUser.Bucket("my_bucket");
            KiiObject obj1   = bucket.NewKiiObject();

            obj1["name"] = "kii";
            obj1.SetGeoPoint("location", new KiiGeoPoint(35.668387, 139.739495));
            obj1.Save();
            KiiObject obj2 = bucket.NewKiiObject();

            obj2["name"] = "TDL";
            obj2.SetGeoPoint("location", new KiiGeoPoint(35.633114, 139.880405));
            obj2.Save();

            KiiQuery query = new KiiQuery(KiiClause.Not(KiiClause.GeoBox("location", new KiiGeoPoint(35.669494, 139.741727), new KiiGeoPoint(35.660934, 139.734957))));

            query.SortByAsc("name");

            KiiQueryResult <KiiObject> results = bucket.Query(query);

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("TDL", results[0].GetString("name"));
        }
        public void Test_0102_query_next()
        {
            Kii.Initialize("appId", "appKey", Kii.Site.US);
            MockHttpClientFactory factory = new MockHttpClientFactory();

            Kii.HttpClientFactory = factory;

            // set response
            MockHttpClient client = (MockHttpClient)factory.Client;

            client.AddResponse(200, "{" +
                               "\"queryDescription\" : \"WHERE ( 1 = 1 )\"," +
                               "\"results\":[" +
                               "{\"_id\":\"497fd6ff-9178-42ec-b6ec-14bce7b5c7c9\",\"name\":\"Nick\",\"age\":18," +
                               "\"_created\":1334505527480,\"_modified\":1334505527480,\"_owner\":\"789399f7-7552-47a8-a524-b9119056edd9\",\"_version\":1}" +
                               "]," +
                               "\"nextPaginationKey\":\"abcd\"" +
                               "}");

            KiiBucket bucket = Kii.Bucket("test");
            KiiQuery  query  = new KiiQuery();
            KiiQueryResult <KiiObject> result = bucket.Query(query);

            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.HasNext);
            Assert.AreEqual("Nick", result[0]["name"]);

            // set response
            this.SetDefaultQueryResult(client);

            // next result
            result = result.GetNextQueryResult();
            Assert.AreEqual(1, result.Count);
            Assert.IsFalse(result.HasNext);
            Assert.AreEqual("John Smith", result[0]["name"]);
        }