Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ private ListBucketsOperation buildCoreOperation()
{
builder.withTimeout(timeout);
}

if (type != null)
{
builder.withBucketType(type);
}

return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<BinaryValue>());
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.<ListBucketsOperation, Location>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<ListBucketsOperation> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Namespace> iterator = listResponse.iterator();
assertTrue(iterator.hasNext());
boolean found = false;

while (!found && iterator.hasNext())
{
found = iterator.next().getBucketName().equals(typeBinary);
}

assertTrue(found);
}
}