diff --git a/src/main/java/com/basho/riak/client/api/commands/buckets/ListBuckets.java b/src/main/java/com/basho/riak/client/api/commands/buckets/ListBuckets.java index 522e7052a..802a22310 100644 --- a/src/main/java/com/basho/riak/client/api/commands/buckets/ListBuckets.java +++ b/src/main/java/com/basho/riak/client/api/commands/buckets/ListBuckets.java @@ -85,6 +85,12 @@ private ListBucketsOperation buildCoreOperation() { builder.withTimeout(timeout); } + + if (type != null) + { + builder.withBucketType(type); + } + return builder.build(); } diff --git a/src/test/java/com/basho/riak/client/api/commands/buckets/ListBucketsTest.java b/src/test/java/com/basho/riak/client/api/commands/buckets/ListBucketsTest.java new file mode 100644 index 000000000..6d0c5d3bd --- /dev/null +++ b/src/test/java/com/basho/riak/client/api/commands/buckets/ListBucketsTest.java @@ -0,0 +1,86 @@ +package com.basho.riak.client.api.commands.buckets; + +import com.basho.riak.client.api.RiakClient; +import com.basho.riak.client.core.FutureOperation; +import com.basho.riak.client.core.RiakCluster; +import com.basho.riak.client.core.RiakFuture; +import com.basho.riak.client.core.operations.ListBucketsOperation; +import com.basho.riak.client.core.query.Location; +import com.basho.riak.client.core.query.Namespace; +import com.basho.riak.client.core.util.BinaryValue; +import com.basho.riak.protobuf.RiakKvPB; +import com.google.protobuf.ByteString; +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.internal.util.reflection.Whitebox; + +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * @author empovit + * @since 2.0.3 + */ +public class ListBucketsTest +{ + + @Mock RiakCluster mockCluster; + @Mock RiakFuture mockFuture; + @Mock ListBucketsOperation.Response mockResponse; + RiakClient client; + + @Before + @SuppressWarnings("unchecked") + public void init() throws Exception + { + MockitoAnnotations.initMocks(this); + when(mockResponse.getBuckets()).thenReturn(new ArrayList()); + when(mockFuture.get()).thenReturn(mockResponse); + when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(mockResponse); + when(mockFuture.getNow()).thenReturn(mockResponse); + when(mockFuture.isCancelled()).thenReturn(false); + when(mockFuture.isDone()).thenReturn(true); + when(mockFuture.isSuccess()).thenReturn(true); + when(mockCluster.execute(any(FutureOperation.class))).thenReturn(mockFuture); + client = new RiakClient(mockCluster); + } + + private void testListBuckets(String bucketType) throws Exception + { + + final BinaryValue type = BinaryValue.createFromUtf8(bucketType); + ListBuckets.Builder list = new ListBuckets.Builder(type); + client.execute(list.build()); + + ArgumentCaptor captor = + ArgumentCaptor.forClass(ListBucketsOperation.class); + verify(mockCluster).execute(captor.capture()); + + ListBucketsOperation operation = captor.getValue(); + RiakKvPB.RpbListBucketsReq.Builder builder = + (RiakKvPB.RpbListBucketsReq.Builder) Whitebox.getInternalState(operation, "reqBuilder"); + + Assert.assertEquals(ByteString.copyFrom(type.unsafeGetValue()), builder.getType()); + } + + @Test + public void bucketTypeBuiltCorrectly() throws Exception + { + testListBuckets("bucket_type"); + } + + @Test + public void defaultBucketTypeBuiltCorrectly() throws Exception + { + testListBuckets(Namespace.DEFAULT_BUCKET_TYPE); + } +} diff --git a/src/test/java/com/basho/riak/client/api/commands/buckets/itest/ITestListBuckets.java b/src/test/java/com/basho/riak/client/api/commands/buckets/itest/ITestListBuckets.java new file mode 100644 index 000000000..2d71142f4 --- /dev/null +++ b/src/test/java/com/basho/riak/client/api/commands/buckets/itest/ITestListBuckets.java @@ -0,0 +1,72 @@ +package com.basho.riak.client.api.commands.buckets.itest; + +import com.basho.riak.client.api.RiakClient; +import com.basho.riak.client.api.commands.buckets.ListBuckets; +import com.basho.riak.client.api.commands.kv.StoreValue; +import com.basho.riak.client.core.operations.itest.ITestBase; +import com.basho.riak.client.core.query.Location; +import com.basho.riak.client.core.query.Namespace; +import com.basho.riak.client.core.query.RiakObject; +import com.basho.riak.client.core.util.BinaryValue; +import org.junit.Test; + +import java.util.Iterator; +import java.util.concurrent.ExecutionException; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +/** + * @author empovit + * @since 2.0.3 + */ +public class ITestListBuckets extends ITestBase +{ + + private final RiakClient client = new RiakClient(cluster); + + @Test + public void testListBucketsDefaultType() throws InterruptedException, ExecutionException + { + testListBuckets(Namespace.DEFAULT_BUCKET_TYPE); + } + + @Test + public void testListBucketsTestType() throws InterruptedException, ExecutionException + { + assumeTrue(testBucketType); + testListBuckets(bucketType.toString()); + } + + private void testListBuckets(String bucketType) throws InterruptedException, ExecutionException + { + // Empty buckets do not show up + final BinaryValue key = BinaryValue.unsafeCreate("temp_key".getBytes()); + + RiakObject value = new RiakObject().setValue(BinaryValue.create("{\"value\":\"value\"}")); + + // Since bucket type in response is populated from the command's context, + // need a way to make sure the type is indeed as expected - use bucket type for bucket name + Location location = new Location(new Namespace(bucketType, bucketType), key); + StoreValue storeCommand = new StoreValue.Builder(value).withLocation(location).build(); + + client.execute(storeCommand); + + final BinaryValue typeBinary = BinaryValue.createFromUtf8(bucketType); + + ListBuckets listBucketsCommand = new ListBuckets.Builder(typeBinary).build(); + + final ListBuckets.Response listResponse = client.execute(listBucketsCommand); + + Iterator iterator = listResponse.iterator(); + assertTrue(iterator.hasNext()); + boolean found = false; + + while (!found && iterator.hasNext()) + { + found = iterator.next().getBucketName().equals(typeBinary); + } + + assertTrue(found); + } +}