public void BucketExists_throws_when_rest_call_fails()
        {
            var restResponse = new RestResponse(HttpStatusCode.BadRequest, "");

            _restClient.Stub(r => r.Get(""))
                .IgnoreArguments()
                .Return(restResponse);

            Assert.Throws<MembaseManagementRestException>(() => _client.BucketExists("testbucket"));
        }
        public void BucketExists_returns_false_when_bucket_does_not_exist()
        {
            var restResponse = new RestResponse(HttpStatusCode.NotFound, "");

            _restClient.Stub(r => r.Get(""))
                .IgnoreArguments()
                .Return(restResponse);

            Assert.IsFalse(_client.BucketExists("testbucket"));
        }
        public void BucketExists_returns_true_when_bucket_exists()
        {
            var restResponse = new RestResponse(HttpStatusCode.OK, "");

            _restClient.Stub(r => r.Get(""))
                .IgnoreArguments()
                .Return(restResponse);

            Assert.IsTrue(_client.BucketExists("testbucket"));
        }
        public void CreateBucket_creates_the_correct_rest_uri()
        {
            const string expectedResourcePath = "pools/default/buckets";

            var parameters = new Dictionary<string, string>
            {
                {"name", "testbucket"},
                {"ramQuotaMB", 64.ToString()},
                {"authType", "sasl"},
                {"replicaNumber", "0"},
                {"bucketType", "memcached"},
                {"saslPassword", "testbucket"}
            };

            var restResponse = new RestResponse(HttpStatusCode.OK, "");

            _restClient.Expect(c => c.Post(expectedResourcePath, parameters))
                .Return(restResponse);

            var createRequest = BucketCreationDetails.PasswordProtectedMemcacheBucket("testbucket", 64);
            _client.CreateBucket(createRequest);
        }
        public void GetBucket_throws_when_rest_call_fails()
        {
            var errorResponse = new RestResponse(HttpStatusCode.BadRequest, "err");

            _restClient.Stub(o => o.Get(""))
                .IgnoreArguments()
                .Return(errorResponse);

            Assert.Throws<MembaseManagementRestException>(() => _client.GetBucket("testbucket"));
        }
        public void GetBucket_throws_when_json_deserialization_fails()
        {
            var garbageResponse = new RestResponse(HttpStatusCode.OK, "nfjk garbage [//");

            _restClient.Stub(o => o.Get(""))
                .IgnoreArguments()
                .Return(garbageResponse);

            Assert.Throws<MembaseManagementException>(() => _client.GetBucket("testbucket"));
        }
        public void DeleteBucket_throws_when_rest_call_fails()
        {
            var restError = new RestResponse(HttpStatusCode.BadRequest, "err");

            _restClient.Stub(c => c.Delete(""))
                .IgnoreArguments()
                .Return(restError);

            Assert.Throws<MembaseManagementRestException>(() => _client.DeleteBucket("testbucket"));
        }
        public void DeleteBucket_does_not_throw_when_bucket_does_not_exist()
        {
            var bucketNotFound = new RestResponse(HttpStatusCode.NotFound, "");

            _restClient.Stub(c => c.Delete(""))
                .IgnoreArguments()
                .Return(bucketNotFound);

            Assert.DoesNotThrow(() => _client.DeleteBucket("testbucket"));
        }
        public void DeleteBucket_creates_the_correct_rest_uri()
        {
            const string expectedResourcePath = "pools/default/buckets/testbucket";
            var restResponse = new RestResponse(HttpStatusCode.OK, "");

            _restClient.Expect(o => o.Delete(expectedResourcePath))
                .Return(restResponse);

            _client.DeleteBucket("testbucket");
        }
        public void CreateBucket_throws_when_rest_call_returns_error()
        {
            var errResponse = new RestResponse(
                HttpStatusCode.BadRequest, "err");

            _restClient.Stub(c => c.Post("", new Dictionary<string, string>()))
                .IgnoreArguments()
                .Return(errResponse);

            var createRequest = BucketCreationDetails.PasswordProtectedMemcacheBucket("testbucket", 64);
            Assert.Throws<MembaseManagementRestException>(() => _client.CreateBucket(createRequest));
        }
        public void CreateBucket_does_not_throw_when_bucket_already_exists()
        {
            var bucketAlreadyExistsResponse = new RestResponse(
                HttpStatusCode.BadRequest, "{\"errors\":{\"name\":\"Bucket with given name already exists\"},\"summaries\":{\"ramSummary\":{\"total\":536870912,\"otherBuckets\":104857600,\"nodesCount\":1,\"perNodeMegs\":100,\"thisAlloc\":104857600,\"thisUsed\":0,\"free\":327155712},\"hddSummary\":{\"total\":249620795392.0,\"otherData\":142283853373.0,\"otherBuckets\":0,\"thisUsed\":0,\"free\":107336942019.0}}}");

            _restClient.Stub(c => c.Post("", new Dictionary<string, string>()))
                .IgnoreArguments()
                .Return(bucketAlreadyExistsResponse);

            var createRequest = BucketCreationDetails.PasswordProtectedMemcacheBucket("testbucket", 64);
            Assert.DoesNotThrow(() => _client.CreateBucket(createRequest));
        }