Example usage for io.netty.buffer Unpooled copiedBuffer

List of usage examples for io.netty.buffer Unpooled copiedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled copiedBuffer.

Prototype

private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) 

Source Link

Usage

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldNotProposeInvalidConfigFromTaintedPoller() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    BucketConfig config = mock(BucketConfig.class);

    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.provider(provider);/*from www.ja va 2 s  .co  m*/

    when(config.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();
    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "localhost:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    ByteBuf content = Unpooled.copiedBuffer("", CharsetUtil.UTF_8);
    when(cluster.send(any(GetBucketConfigRequest.class)))
            .thenReturn(Observable.just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.FAILURE,
                    KeyValueStatus.ERR_NOT_FOUND.code(), "bucket", content,
                    InetAddress.getByName("localhost"))));

    refresher.markTainted(config);

    Thread.sleep(1500);

    verify(provider, never()).proposeBucketConfig("bucket", "");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldRefreshWithValidClusterConfig() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.registerBucket("bucket", "");
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    refresher.provider(provider);/*from  w  w w .  j av a2  s .c  o m*/

    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(bucketConfig.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();
    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "localhost:8091", ports));
    when(bucketConfig.nodes()).thenReturn(nodeInfos);
    Map<String, BucketConfig> bucketConfigs = new HashMap<String, BucketConfig>();
    bucketConfigs.put("bucket", bucketConfig);

    when(clusterConfig.bucketConfigs()).thenReturn(bucketConfigs);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    when(cluster.send(any(GetBucketConfigRequest.class)))
            .thenReturn(Observable.just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("localhost"))));

    refresher.refresh(clusterConfig);

    Thread.sleep(200);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldNotRefreshWithInvalidClusterConfig() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.registerBucket("bucket", "");
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    refresher.provider(provider);//from ww w  .  j  av  a 2 s .  co  m

    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(bucketConfig.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "localhost:8091", ports));
    when(bucketConfig.nodes()).thenReturn(nodeInfos);
    Map<String, BucketConfig> bucketConfigs = new HashMap<String, BucketConfig>();
    bucketConfigs.put("bucket", bucketConfig);

    when(clusterConfig.bucketConfigs()).thenReturn(bucketConfigs);

    ByteBuf content = Unpooled.copiedBuffer("", CharsetUtil.UTF_8);
    when(cluster.send(any(GetBucketConfigRequest.class)))
            .thenReturn(Observable.just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.FAILURE,
                    KeyValueStatus.ERR_NOT_FOUND.code(), "bucket", content,
                    InetAddress.getByName("localhost"))));

    refresher.refresh(clusterConfig);

    Thread.sleep(200);

    verify(provider, never()).proposeBucketConfig("bucket", "");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldFallbackToNextOnRefreshWhenFirstFails() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.registerBucket("bucket", "");
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    refresher.provider(provider);/*from  ww  w  .  j a  va2s.  c o m*/

    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(bucketConfig.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "1.2.3.4:8091", ports));
    nodeInfos.add(new DefaultNodeInfo(null, "2.3.4.5:8091", ports));
    when(bucketConfig.nodes()).thenReturn(nodeInfos);
    Map<String, BucketConfig> bucketConfigs = new HashMap<String, BucketConfig>();
    bucketConfigs.put("bucket", bucketConfig);

    when(clusterConfig.bucketConfigs()).thenReturn(bucketConfigs);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> goodResponse = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("1.2.3.4")));
    Observable<CouchbaseResponse> badResponse = Observable.error(new CouchbaseException("Woops.."));
    when(cluster.send(any(GetBucketConfigRequest.class))).thenReturn(badResponse, goodResponse);

    refresher.refresh(clusterConfig);

    Thread.sleep(1500);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldFallbackToNextOnPollWhenFirstFails() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    BucketConfig config = mock(BucketConfig.class);

    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.provider(provider);//  w  w  w.  j  a  v a  2 s  .  c  o m

    when(config.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "1.2.3.4:8091", ports));
    nodeInfos.add(new DefaultNodeInfo(null, "2.3.4.5:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> goodResponse = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("1.2.3.4")));
    Observable<CouchbaseResponse> badResponse = Observable.error(new CouchbaseException("Failure"));
    when(cluster.send(any(GetBucketConfigRequest.class))).thenReturn(badResponse, goodResponse);
    refresher.markTainted(config);

    Thread.sleep(1500);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.config.refresher.CarrierRefresherTest.java

License:Apache License

@Test
public void shouldIgnoreNodeWithoutKVServiceEnabled() throws Exception {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ConfigurationProvider provider = mock(ConfigurationProvider.class);
    BucketConfig config = mock(BucketConfig.class);

    CarrierRefresher refresher = new CarrierRefresher(ENVIRONMENT, cluster);
    refresher.provider(provider);//  w  ww  .  j  a v  a2  s .c o m

    when(config.name()).thenReturn("bucket");
    List<NodeInfo> nodeInfos = new ArrayList<NodeInfo>();

    Map<String, Integer> ports = new HashMap<String, Integer>();
    ports.put("direct", 11210);
    nodeInfos.add(new DefaultNodeInfo(null, "1.2.3.4:8091", ports));
    nodeInfos.add(new DefaultNodeInfo(null, "6.7.8.9:8091", new HashMap<String, Integer>()));
    nodeInfos.add(new DefaultNodeInfo(null, "2.3.4.5:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> goodResponse = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, InetAddress.getByName("1.2.3.4")));
    Observable<CouchbaseResponse> badResponse = Observable.error(new CouchbaseException("Failure"));
    when(cluster.send(any(GetBucketConfigRequest.class))).thenReturn(badResponse, goodResponse);
    refresher.markTainted(config);

    Thread.sleep(1500);

    verify(provider, times(1)).proposeBucketConfig("bucket", "{\"config\": true}");
    assertEquals(0, content.refCnt());
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryAuthHandler.java

License:Open Source License

/**
 * Handles an incoming SASL AUTH response and - if needed - dispatches the SASL STEPs.
 *
 * @param ctx the handler context./*from  w  w w. j  a va  2s  . c o m*/
 * @param msg the incoming message to investigate.
 * @throws Exception
 */
private void handleAuthResponse(ChannelHandlerContext ctx, FullBinaryMemcacheResponse msg) throws Exception {
    if (saslClient.isComplete()) {
        checkIsAuthed(msg);
        return;
    }

    byte[] response = new byte[msg.content().readableBytes()];
    msg.content().readBytes(response);
    byte[] evaluatedBytes = saslClient.evaluateChallenge(response);

    if (evaluatedBytes != null) {
        String[] evaluated = new String(evaluatedBytes).split(" ");
        ByteBuf content = Unpooled.copiedBuffer(username + "\0" + evaluated[1], CharsetUtil.UTF_8);

        FullBinaryMemcacheRequest stepRequest = new DefaultFullBinaryMemcacheRequest(selectedMechanism,
                Unpooled.EMPTY_BUFFER, content);
        stepRequest.setOpcode(SASL_STEP_OPCODE).setKeyLength((short) selectedMechanism.length())
                .setTotalBodyLength(content.readableBytes() + selectedMechanism.length());

        ctx.writeAndFlush(stepRequest);
    } else {
        throw new AuthenticationException("SASL Challenge evaluation returned null.");
    }
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldEncodeInsertRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    InsertRequest request = new InsertRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);//w  w w.j a v a2 s.  com
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new InsertRequest("key", content.copy(), 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new InsertRequest("key", content.copy(), 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new InsertRequest("key", content.copy(), 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_INSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldEncodeUpsertRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    UpsertRequest request = new UpsertRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);/*from  w w w.  j  av  a  2  s. c o  m*/
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new UpsertRequest("key", content.copy(), 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new UpsertRequest("key", content.copy(), 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new UpsertRequest("key", content.copy(), 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_UPSERT, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    ReferenceCountUtil.release(content);
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandlerTest.java

License:Open Source License

@Test
public void shouldEncodeReplaceRequest() {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);

    ReplaceRequest request = new ReplaceRequest("key", content.copy(), "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);//  ww  w  .ja v a2s  .c o  m
    FullBinaryMemcacheRequest outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals(0, outbound.getCAS());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new ReplaceRequest("key", content.copy(), 0, 10, 0, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(10, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new ReplaceRequest("key", content.copy(), 0, 0, 5, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(5, outbound.getExtras().readInt());
    assertEquals(0, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    request = new ReplaceRequest("key", content.copy(), 0, 30, 99, "bucket");
    request.partition((short) 512);
    channel.writeOutbound(request);
    outbound = (FullBinaryMemcacheRequest) channel.readOutbound();
    assertNotNull(outbound);
    assertEquals("key", outbound.getKey());
    assertEquals("key".length(), outbound.getKeyLength());
    assertEquals(512, outbound.getReserved());
    assertEquals(BinaryHandler.OP_REPLACE, outbound.getOpcode());
    assertEquals(8, outbound.getExtrasLength());
    assertEquals(99, outbound.getExtras().readInt());
    assertEquals(30, outbound.getExtras().readInt());
    assertEquals(18, outbound.getTotalBodyLength());
    assertEquals("content", outbound.content().toString(CharsetUtil.UTF_8));

    ReferenceCountUtil.release(content);
}