public void Test_UserScopeWithEmptyResultASync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] {}, null);
            string                   userID    = "test_user";
            CountDownLatch           cd        = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics    = null;
            Exception                exception = null;

            KiiUser.UserWithID(userID).ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(0, topics.Result.Count);

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);
        }
        public void Test_AppScope()
        {
            // app 'd92535a1' has over 50 app scope topics.
            CountDownLatch           cd     = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics = null;
            Exception exception             = null;

            Kii.ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);
            Assert.IsTrue(topics.HasNext);
            Assert.IsNotNull(topics.PaginationKey);
            Assert.AreEqual(50, topics.Result.Count);

            Kii.ListTopics(topics.PaginationKey, (KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsTrue(topics.Result.Count > 0);
        }
        public void Test_AppScopeWithPaginationSync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] { "Topic1", "Topic2" }, "ab=12/12+");
            AddMockResponse(200, new string[] { "Topic3" }, null);
            KiiListResult <KiiTopic> topics = Kii.ListTopics();

            Assert.IsTrue(topics.HasNext);
            Assert.AreEqual("ab=12/12+", topics.PaginationKey);
            Assert.AreEqual(2, topics.Result.Count);
            Assert.AreEqual("Topic1", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("topics", "Topic1"), topics.Result[0].Uri.ToString());
            Assert.AreEqual("Topic2", topics.Result[1].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("topics", "Topic2"), topics.Result[1].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);

            topics = Kii.ListTopics(topics.PaginationKey);
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(1, topics.Result.Count);
            Assert.AreEqual("Topic3", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("topics", "Topic3"), topics.Result[0].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [1]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics") + "?paginationKey=" + Uri.EscapeUriString("ab=12/12+"), client.RequestUrl [1]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [1]["Authorization"]);
        }
        public void Test_GroupScopeASync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] { "Topic1", "Topic2" }, null);
            string                   groupID   = "test_group";
            CountDownLatch           cd        = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics    = null;
            Exception                exception = null;

            KiiGroup.GroupWithID(groupID).ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(2, topics.Result.Count);
            Assert.AreEqual("Topic1", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("groups", groupID, "topics", "Topic1"), topics.Result[0].Uri.ToString());
            Assert.AreEqual("Topic2", topics.Result[1].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("groups", groupID, "topics", "Topic2"), topics.Result[1].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "groups", groupID, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);
        }
        public void Test_UserScopeWithPaginationASync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] { "Topic1", "Topic2" }, "ab=12/12+");
            AddMockResponse(200, new string[] { "Topic3" }, null);
            string                   userID    = "test_user";
            CountDownLatch           cd        = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics    = null;
            Exception                exception = null;

            KiiUser.UserWithID(userID).ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsTrue(topics.HasNext);
            Assert.AreEqual("ab=12/12+", topics.PaginationKey);
            Assert.AreEqual(2, topics.Result.Count);
            Assert.AreEqual("Topic1", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("users", userID, "topics", "Topic1"), topics.Result[0].Uri.ToString());
            Assert.AreEqual("Topic2", topics.Result[1].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("users", userID, "topics", "Topic2"), topics.Result[1].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);

            String paginationKey = topics.PaginationKey;

            cd        = new CountDownLatch(1);
            topics    = null;
            exception = null;

            KiiUser.UserWithID(userID).ListTopics(paginationKey, (KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(1, topics.Result.Count);
            Assert.AreEqual("Topic3", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("users", userID, "topics", "Topic3"), topics.Result[0].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [1]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics") + "?paginationKey=" + Uri.EscapeUriString("ab=12/12+"), client.RequestUrl [1]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [1]["Authorization"]);
        }
        /// <summary>
        /// Gets the list of next page of topics in this group scope.
        /// </summary>
        /// <param name="paginationKey">
        /// Specifies the pagination key that is obtained by <see cref="KiiListResult{T}.PaginationKey"/>.
        /// If specified null or empty, it's same as the <see cref="ListTopics()"/>.
        /// </param>
        /// <returns>A list of the topics in this group scope.</returns>
        /// <exception cref='CloudException'>
        /// Is thrown when server sends error response.
        /// </exception>
        /// <exception cref='UnauthorizedException'>
        /// Is thrown when this method called by anonymous user.
        /// </exception>
        /// <remarks>
        /// This api sends a request to server.
        /// </remarks>
        public KiiListResult <KiiTopic> ListTopics(string paginationKey)
        {
            KiiListResult <KiiTopic> result = null;

            ExecListTopics(Kii.HttpClientFactory, paginationKey, (KiiListResult <KiiTopic> topics, Exception e) => {
                if (e != null)
                {
                    throw e;
                }
                result = topics;
            });
            return(result);
        }
        public void Test_AppScopeWithEmptyResultSync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] {}, null);
            KiiListResult <KiiTopic> topics = Kii.ListTopics();

            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(0, topics.Result.Count);

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);
        }
        public void Test_GroupScope()
        {
            KiiGroup group = Kii.Group("test_group");

            group.Save();
            for (int i = 0; i < 51; i++)
            {
                group.Topic("user_topic" + i).Save();
            }

            CountDownLatch           cd     = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics = null;
            Exception exception             = null;

            group.ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(exception);
            Assert.IsTrue(topics.HasNext);
            Assert.IsNotNull(topics.PaginationKey);
            Assert.AreEqual(50, topics.Result.Count);

            string paginationKey = topics.PaginationKey;

            cd        = new CountDownLatch(1);
            topics    = null;
            exception = null;

            group.ListTopics(paginationKey, (KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 20)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(1, topics.Result.Count);
        }
        public void Test_UserScopeSync()
        {
            LogIn("test-user-00001");
            AddMockResponse(200, new string[] { "Topic1", "Topic2" }, null);
            string userID = "test_user";
            KiiListResult <KiiTopic> topics = KiiUser.UserWithID(userID).ListTopics();

            Assert.IsFalse(topics.HasNext);
            Assert.IsNull(topics.PaginationKey);
            Assert.AreEqual(2, topics.Result.Count);
            Assert.AreEqual("Topic1", topics.Result[0].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("users", userID, "topics", "Topic1"), topics.Result[0].Uri.ToString());
            Assert.AreEqual("Topic2", topics.Result[1].Name);
            Assert.AreEqual("kiicloud://" + Utils.Path("users", userID, "topics", "Topic2"), topics.Result[1].Uri.ToString());

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "users", userID, "topics"), client.RequestUrl [0]);
            Assert.AreEqual("Bearer " + KiiUser.AccessToken, client.RequestHeader [0]["Authorization"]);
        }
        public void Test_UserScopeNoIDASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new UnauthorizedException("", null, ""));
            CountDownLatch           cd     = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics = null;
            Exception exception             = null;

            KiiUser.BuilderWithName("user").Build().ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(topics);
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(typeof(InvalidOperationException), exception);
        }
        public void Test_AnonymousASync()
        {
            KiiUser.LogOut();
            AddMockResponse(200, new string[] {}, null);
            CountDownLatch           cd     = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics = null;
            Exception exception             = null;

            Kii.ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(topics);
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(typeof(InvalidOperationException), exception);
        }
        public void Test_Status403ASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new ForbiddenException("", null, ""));
            CountDownLatch           cd     = new CountDownLatch(1);
            KiiListResult <KiiTopic> topics = null;
            Exception exception             = null;

            Kii.ListTopics((KiiListResult <KiiTopic> t, Exception e) => {
                topics    = t;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsNull(topics);
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(typeof(ForbiddenException), exception);
            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics"), client.RequestUrl [0]);
        }