public void Test_Status403ASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new ForbiddenException("", null, ""));
            string         topicName = "test_topic";
            KiiTopic       topic     = Kii.Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            bool?          existence = null;
            Exception      exception = null;

            topic.Exists((bool?b, Exception e) => {
                existence = b;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(existence.HasValue);
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(typeof(ForbiddenException), exception);
            Assert.AreEqual(KiiHttpMethod.HEAD, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics", topicName), client.RequestUrl [0]);
        }
        public void Test_NotLoggedInSync()
        {
            string   topicName = "test_topic";
            KiiTopic topic     = Kii.Topic(topicName);

            topic.ListAclEntries();
        }
        public void Test_AppScopeSync()
        {
            LogIn("test-user-00001");
            client.AddResponse(200, RESPONSE_BODY);
            string   topicName = "test_topic";
            KiiTopic topic     = Kii.Topic(topicName);
            IList <KiiACLEntry <KiiTopic, TopicAction> > entries = topic.ListAclEntries();

            Assert.AreEqual(6, entries.Count);
            Assert.AreEqual(TopicAction.SEND_MESSAGE_TO_TOPIC, entries [0].Action);
            Assert.AreEqual("UUUU-5555-6666-7777-8888", ((KiiUser)entries [0].Subject).ID);
            Assert.AreEqual(TopicAction.SEND_MESSAGE_TO_TOPIC, entries [1].Action);
            Assert.AreEqual("GGGG-5555-6666-7777-8888", ((KiiGroup)entries[1].Subject).ID);
            Assert.AreEqual(TopicAction.SUBSCRIBE_TO_TOPIC, entries [2].Action);
            Assert.AreEqual(KiiAnonymousUser.Get(), entries [2].Subject);
            Assert.AreEqual(TopicAction.SUBSCRIBE_TO_TOPIC, entries [3].Action);
            Assert.AreEqual(KiiAnyAuthenticatedUser.Get(), entries [3].Subject);
            Assert.AreEqual(TopicAction.SUBSCRIBE_TO_TOPIC, entries [4].Action);
            Assert.AreEqual("UUUU-1111-2222-3333-4444", ((KiiUser)entries [4].Subject).ID);
            Assert.AreEqual(TopicAction.SUBSCRIBE_TO_TOPIC, entries [5].Action);
            Assert.AreEqual("GGGG-1111-2222-3333-4444", ((KiiGroup)entries[5].Subject).ID);

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics", topicName, "acl"), client.RequestUrl [0]);
        }
        public void Test_AppScope404ASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new NotFoundException("", null, "", NotFoundException.Reason.ACL_NOT_FOUND));
            string         topicName = "test_topic";
            KiiTopic       topic     = Kii.Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            IList <KiiACLEntry <KiiTopic, TopicAction> > entries = null;
            Exception exception = null;

            topic.ListAclEntries((IList <KiiACLEntry <KiiTopic, TopicAction> > result, Exception e) => {
                entries   = result;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(entries);
            Assert.IsInstanceOfType(typeof(NotFoundException), exception);
            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics", topicName, "acl"), client.RequestUrl [0]);
        }
        public void Test_AnonymousSync()
        {
            KiiUser.LogOut();
            string   topicName = "test_topic";
            KiiTopic topic     = Kii.Topic(topicName);

            topic.Exists();
        }
        public void Test_Status403Sync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new ForbiddenException("", null, ""));
            string   topicName = "test_topic";
            KiiTopic topic     = Kii.Topic(topicName);

            topic.Exists();
        }
        public void Test_AppScope404Sync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new NotFoundException("", null, "", NotFoundException.Reason.ACL_NOT_FOUND));
            string   topicName = "test_topic";
            KiiTopic topic     = Kii.Topic(topicName);

            topic.ListAclEntries();
        }
        public void Test_AppScopeFalseSync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new NotFoundException("", null, "", NotFoundException.Reason.TOPIC_NOT_FOUND));
            string   topicName = "test_topic";
            KiiTopic topic     = Kii.Topic(topicName);

            Assert.IsFalse(topic.Exists());
            Assert.AreEqual(KiiHttpMethod.HEAD, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics", topicName), client.RequestUrl [0]);
        }
        public void Test_AppScopeTrueSync()
        {
            LogIn("test-user-00001");
            client.AddResponse(204, null);
            string   topicName = "test_topic";
            KiiTopic topic     = Kii.Topic(topicName);

            Assert.IsTrue(topic.Exists());
            Assert.AreEqual(KiiHttpMethod.HEAD, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics", topicName), client.RequestUrl [0]);
        }
        public void KiiTopicAclTest()
        {
            this.LogIn();

            KiiTopic appTopicA  = Kii.Topic("A");
            KiiTopic appTopicA_ = Kii.Topic("A");
            KiiTopic appTopicB  = Kii.Topic("B");
            KiiTopic usrTopicA  = KiiUser.CreateByUri(new Uri("kiicloud://users/user1234")).Topic("A");

            KiiTopicACL acl1 = null;
            KiiTopicACL acl2 = null;

            // same object
            acl1 = new KiiTopicACL(appTopicA);
            acl2 = new KiiTopicACL(appTopicA_);
            Assert.IsTrue(acl1.Equals(acl2));
            Assert.IsTrue(acl1.GetHashCode() == acl2.GetHashCode());
            Assert.IsFalse(acl1 == acl2);
            // same object and action
            acl1 = new KiiTopicACL(appTopicA, TopicAction.SEND_MESSAGE_TO_TOPIC);
            acl2 = new KiiTopicACL(appTopicA_, TopicAction.SEND_MESSAGE_TO_TOPIC);
            Assert.IsTrue(acl1.Equals(acl2));
            Assert.IsTrue(acl1.GetHashCode() == acl2.GetHashCode());
            Assert.IsFalse(acl1 == acl2);
            // different action
            acl1 = new KiiTopicACL(appTopicA, TopicAction.SEND_MESSAGE_TO_TOPIC);
            acl2 = new KiiTopicACL(appTopicA_, TopicAction.SUBSCRIBE_TO_TOPIC);
            Assert.IsFalse(acl1.Equals(acl2));
            Assert.IsFalse(acl1.GetHashCode() == acl2.GetHashCode());
            Assert.IsFalse(acl1 == acl2);
            // different topic name
            acl1 = new KiiTopicACL(appTopicA, TopicAction.SEND_MESSAGE_TO_TOPIC);
            acl2 = new KiiTopicACL(appTopicB, TopicAction.SEND_MESSAGE_TO_TOPIC);
            Assert.IsFalse(acl1.Equals(acl2));
            Assert.IsFalse(acl1.GetHashCode() == acl2.GetHashCode());
            Assert.IsFalse(acl1 == acl2);
            // different scope of bucket
            acl1 = new KiiTopicACL(appTopicA, TopicAction.SEND_MESSAGE_TO_TOPIC);
            acl2 = new KiiTopicACL(usrTopicA, TopicAction.SEND_MESSAGE_TO_TOPIC);
            Assert.IsFalse(acl1.Equals(acl2));
            Assert.IsFalse(acl1.GetHashCode() == acl2.GetHashCode());
            Assert.IsFalse(acl1 == acl2);
        }
Example #11
0
        private static void ExecListTopics(KiiHttpClientFactory factory, string paginationKey, KiiGenericsCallback <KiiListResult <KiiTopic> > callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("KiiGenericsCallback must not be null");
            }
            try
            {
                Utils.CheckInitialize(true);
            }
            catch (Exception e)
            {
                callback(null, e);
                return;
            }
            String url = Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics");

            if (!String.IsNullOrEmpty(paginationKey))
            {
                url = url + "?paginationKey=" + Uri.EscapeUriString(paginationKey);
            }
            KiiHttpClient client = factory.Create(url, Kii.AppId, Kii.AppKey, KiiHttpMethod.GET);

            KiiCloudEngine.SetAuthBearer(client);
            client.SendRequest((ApiResponse response, Exception e) => {
                if (e != null)
                {
                    callback(null, e);
                    return;
                }
                JsonObject json         = new JsonObject(response.Body);
                String newPaginationKey = json.OptString("paginationKey", null);
                JsonArray array         = json.GetJsonArray("topics");
                List <KiiTopic> topics  = new List <KiiTopic>();
                for (int i = 0; i < array.Length(); i++)
                {
                    topics.Add(Kii.Topic(array.GetJsonObject(i).GetString("topicID")));
                }
                callback(new KiiListResult <KiiTopic>(topics, newPaginationKey), null);
            });
        }
        public void Test_NotLoggedInASync()
        {
            string         topicName = "test_topic";
            KiiTopic       topic     = Kii.Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            IList <KiiACLEntry <KiiTopic, TopicAction> > entries = null;
            Exception exception = null;

            topic.ListAclEntries((IList <KiiACLEntry <KiiTopic, TopicAction> > result, Exception e) => {
                entries   = result;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(entries);
            Assert.IsInstanceOfType(typeof(InvalidOperationException), exception);
        }
        public void Test_AnonymousASync()
        {
            KiiUser.LogOut();
            string         topicName = "test_topic";
            KiiTopic       topic     = Kii.Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            bool?          existence = null;
            Exception      exception = null;

            topic.Exists((bool?b, Exception e) => {
                existence = b;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(existence.HasValue);
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(typeof(InvalidOperationException), exception);
        }
        public void Test_AppScopeASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(200, RESPONSE_BODY);
            string         topicName = "test_topic";
            KiiTopic       topic     = Kii.Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            IList <KiiACLEntry <KiiTopic, TopicAction> > entries = null;
            Exception exception = null;

            topic.ListAclEntries((IList <KiiACLEntry <KiiTopic, TopicAction> > result, Exception e) => {
                entries   = result;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }

            Assert.IsNull(exception);
            Assert.AreEqual(6, entries.Count);
            Assert.AreEqual(TopicAction.SEND_MESSAGE_TO_TOPIC, entries [0].Action);
            Assert.AreEqual("UUUU-5555-6666-7777-8888", ((KiiUser)entries [0].Subject).ID);
            Assert.AreEqual(TopicAction.SEND_MESSAGE_TO_TOPIC, entries [1].Action);
            Assert.AreEqual("GGGG-5555-6666-7777-8888", ((KiiGroup)entries[1].Subject).ID);
            Assert.AreEqual(TopicAction.SUBSCRIBE_TO_TOPIC, entries [2].Action);
            Assert.AreEqual(KiiAnonymousUser.Get(), entries [2].Subject);
            Assert.AreEqual(TopicAction.SUBSCRIBE_TO_TOPIC, entries [3].Action);
            Assert.AreEqual(KiiAnyAuthenticatedUser.Get(), entries [3].Subject);
            Assert.AreEqual(TopicAction.SUBSCRIBE_TO_TOPIC, entries [4].Action);
            Assert.AreEqual("UUUU-1111-2222-3333-4444", ((KiiUser)entries [4].Subject).ID);
            Assert.AreEqual(TopicAction.SUBSCRIBE_TO_TOPIC, entries [5].Action);
            Assert.AreEqual("GGGG-1111-2222-3333-4444", ((KiiGroup)entries[5].Subject).ID);

            Assert.AreEqual(KiiHttpMethod.GET, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics", topicName, "acl"), client.RequestUrl [0]);
        }
        public void Test_AppScopeFalseASync()
        {
            LogIn("test-user-00001");
            client.AddResponse(new NotFoundException("", null, "", NotFoundException.Reason.TOPIC_NOT_FOUND));
            string         topicName = "test_topic";
            KiiTopic       topic     = Kii.Topic(topicName);
            CountDownLatch cd        = new CountDownLatch(1);
            bool?          existence = null;
            Exception      exception = null;

            topic.Exists((bool?b, Exception e) => {
                existence = b;
                exception = e;
                cd.Signal();
            });
            if (!cd.Wait(new TimeSpan(0, 0, 0, 3)))
            {
                Assert.Fail("Callback not fired.");
            }
            Assert.IsFalse(existence.Value);
            Assert.IsNull(exception);
            Assert.AreEqual(KiiHttpMethod.HEAD, client.RequestMethod [0]);
            Assert.AreEqual(Utils.Path(Kii.BaseUrl, "apps", Kii.AppId, "topics", topicName), client.RequestUrl [0]);
        }
        public void KiiTopicAclTest()
        {
            KiiTopicACL acl1 = new KiiTopicACL(Kii.Topic("A"));
            KiiTopicACL acl2 = new KiiTopicACL(Kii.Topic("A"));

            KiiUser userA  = KiiUser.CreateByUri(new Uri("kiicloud://users/userA"));
            KiiUser userA_ = KiiUser.CreateByUri(new Uri("kiicloud://users/userA"));
            KiiUser userB  = KiiUser.CreateByUri(new Uri("kiicloud://users/userB"));
            KiiUser userC  = KiiUser.CreateByUri(new Uri("kiicloud://users/C"));

            KiiGroup groupA  = KiiGroup.CreateByUri(new Uri("kiicloud://groups/groupA"));
            KiiGroup groupA_ = KiiGroup.CreateByUri(new Uri("kiicloud://groups/groupA"));
            KiiGroup groupB  = KiiGroup.CreateByUri(new Uri("kiicloud://groups/groupB"));
            KiiGroup groupC  = KiiGroup.CreateByUri(new Uri("kiicloud://groups/C"));

            KiiACLEntry <KiiTopic, TopicAction> aclEntry1 = null;
            KiiACLEntry <KiiTopic, TopicAction> aclEntry2 = null;

            // same user
            aclEntry1 = new KiiACLEntry <KiiTopic, TopicAction>(acl1, userA);
            aclEntry2 = new KiiACLEntry <KiiTopic, TopicAction>(acl2, userA_);
            Assert.IsTrue(aclEntry1.Equals(aclEntry2));
            Assert.IsTrue(aclEntry1.GetHashCode() == aclEntry2.GetHashCode());
            Assert.IsFalse(aclEntry1 == aclEntry2);
            // same user
            aclEntry1 = new KiiACLEntry <KiiTopic, TopicAction>(acl1, KiiAnonymousUser.Get());
            aclEntry2 = new KiiACLEntry <KiiTopic, TopicAction>(acl2, KiiAnonymousUser.Get());
            Assert.IsTrue(aclEntry1.Equals(aclEntry2));
            Assert.IsTrue(aclEntry1.GetHashCode() == aclEntry2.GetHashCode());
            Assert.IsFalse(aclEntry1 == aclEntry2);
            // different user
            aclEntry1 = new KiiACLEntry <KiiTopic, TopicAction>(acl1, userA);
            aclEntry2 = new KiiACLEntry <KiiTopic, TopicAction>(acl2, userB);
            Assert.IsFalse(aclEntry1.Equals(aclEntry2));
            Assert.IsFalse(aclEntry1.GetHashCode() == aclEntry2.GetHashCode());
            Assert.IsFalse(aclEntry1 == aclEntry2);
            // different user
            aclEntry1 = new KiiACLEntry <KiiTopic, TopicAction>(acl1, KiiAnonymousUser.Get());
            aclEntry2 = new KiiACLEntry <KiiTopic, TopicAction>(acl2, KiiAnyAuthenticatedUser.Get());
            Assert.IsFalse(aclEntry1.Equals(aclEntry2));
            Assert.IsFalse(aclEntry1.GetHashCode() == aclEntry2.GetHashCode());
            Assert.IsFalse(aclEntry1 == aclEntry2);
            // same group
            aclEntry1 = new KiiACLEntry <KiiTopic, TopicAction>(acl1, groupA);
            aclEntry2 = new KiiACLEntry <KiiTopic, TopicAction>(acl2, groupA_);
            Assert.IsTrue(aclEntry1.Equals(aclEntry2));
            Assert.IsTrue(aclEntry1.GetHashCode() == aclEntry2.GetHashCode());
            Assert.IsFalse(aclEntry1 == aclEntry2);
            // different group
            aclEntry1 = new KiiACLEntry <KiiTopic, TopicAction>(acl1, groupA);
            aclEntry2 = new KiiACLEntry <KiiTopic, TopicAction>(acl2, groupB);
            Assert.IsFalse(aclEntry1.Equals(aclEntry2));
            Assert.IsFalse(aclEntry1.GetHashCode() == aclEntry2.GetHashCode());
            Assert.IsFalse(aclEntry1 == aclEntry2);
            // different subject kind
            aclEntry1 = new KiiACLEntry <KiiTopic, TopicAction>(acl1, userC);
            aclEntry2 = new KiiACLEntry <KiiTopic, TopicAction>(acl2, groupC);
            Assert.IsFalse(aclEntry1.Equals(aclEntry2));
            Assert.IsFalse(aclEntry1.GetHashCode() == aclEntry2.GetHashCode());
            Assert.IsFalse(aclEntry1 == aclEntry2);
        }