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.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldReturnPathNotFoundOnArrayInsertOverSize() {
    SubArrayRequest.ArrayOperation arrayOp = SubArrayRequest.ArrayOperation.INSERT;
    String path = "sub.array[5]";

    ByteBuf fragment = Unpooled.copiedBuffer("99", CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(fragment);

    SubArrayRequest request = new SubArrayRequest(testSubKey, path, arrayOp, fragment, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();

    assertEquals(ResponseStatus.SUBDOC_PATH_NOT_FOUND, response.status());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldReturnPathInvalidOnArrayInsertAtNegativeIndex() {
    SubArrayRequest.ArrayOperation arrayOp = SubArrayRequest.ArrayOperation.INSERT;
    String path = "sub.array[-1]";

    ByteBuf fragment = Unpooled.copiedBuffer("99", CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(fragment);

    SubArrayRequest request = new SubArrayRequest(testSubKey, path, arrayOp, fragment, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();

    assertEquals(ResponseStatus.SUBDOC_PATH_INVALID, response.status());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldAddUniqueIfValueNotAlreadyInArray() {
    SubArrayRequest.ArrayOperation arrayOp = SubArrayRequest.ArrayOperation.ADD_UNIQUE;
    String path = "sub.array";

    ByteBuf fragment = Unpooled.copiedBuffer("99", CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(fragment);

    SubArrayRequest request = new SubArrayRequest(testSubKey, path, arrayOp, fragment, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();

    assertEquals(ResponseStatus.SUCCESS, response.status());
    assertMutation(testSubKey, jsonContent.replace("]", ", 99]"));
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldReturnPathExistOnArrayAddUniqueWithDuplicateValue() {
    SubArrayRequest.ArrayOperation arrayOp = SubArrayRequest.ArrayOperation.ADD_UNIQUE;
    String path = "sub.array";

    ByteBuf fragment = Unpooled.copiedBuffer("2", CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(fragment);

    SubArrayRequest request = new SubArrayRequest(testSubKey, path, arrayOp, fragment, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();

    assertEquals(ResponseStatus.SUBDOC_PATH_EXISTS, response.status());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldReturnPathMismatchOnArrayAddUniqueIfArrayIsNotPrimitive() {
    SubArrayRequest.ArrayOperation arrayOp = SubArrayRequest.ArrayOperation.ADD_UNIQUE;
    String path = "complexArray";

    ByteBuf fragment = Unpooled.copiedBuffer("\"arrayElement\"", CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(fragment);

    SubArrayRequest request = new SubArrayRequest(testComplexSubArray, path, arrayOp, fragment, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();

    assertEquals(ResponseStatus.SUBDOC_PATH_MISMATCH, response.status());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldApplyAllMultiMutationsAndReleaseCommandFragments() {
    ByteBuf counterFragment = Unpooled.copiedBuffer("-404", CharsetUtil.UTF_8);
    counterFragment.retain(2);//  ww  w  .  ja va  2s  .c  om

    ByteBuf stringFragment = Unpooled.copiedBuffer("\"mutated\"", CharsetUtil.UTF_8);
    stringFragment.retain(2);

    ByteBuf arrayInsertedFragment = Unpooled.copiedBuffer("\"inserted\"", CharsetUtil.UTF_8);
    ByteBuf arrayFirstFragment = Unpooled.copiedBuffer("\"first\"", CharsetUtil.UTF_8);
    ByteBuf arrayLastFragment = Unpooled.copiedBuffer("\"last\"", CharsetUtil.UTF_8);
    ByteBuf uniqueFragment = Unpooled.copiedBuffer("\"unique\"", CharsetUtil.UTF_8);

    MutationCommand[] commands = new MutationCommand[] {
            new MutationCommand(Mutation.COUNTER, "counter", counterFragment, false),
            new MutationCommand(Mutation.COUNTER, "another.counter", counterFragment, true),
            new MutationCommand(Mutation.COUNTER, "another.counter", counterFragment, false),
            new MutationCommand(Mutation.DICT_ADD, "sub.value2", stringFragment),
            new MutationCommand(Mutation.DICT_UPSERT, "sub.value3", stringFragment),
            new MutationCommand(Mutation.REPLACE, "value", stringFragment),
            new MutationCommand(Mutation.ARRAY_INSERT, "sub.array[1]", arrayInsertedFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_FIRST, "sub.array", arrayFirstFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_LAST, "sub.array", arrayLastFragment),
            new MutationCommand(Mutation.ARRAY_ADD_UNIQUE, "sub.array", uniqueFragment),
            new MutationCommand(Mutation.DELETE, "sub.value") };

    SubMultiMutationRequest request = new SubMultiMutationRequest(testSubKey, bucket(), commands);
    MultiMutationResponse response = cluster().<MultiMutationResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    assertEquals(Unpooled.EMPTY_BUFFER, response.content());
    assertEquals(-1, response.firstErrorIndex());
    assertEquals(ResponseStatus.SUCCESS, response.firstErrorStatus());

    assertEquals(0, stringFragment.refCnt());
    assertEquals(0, counterFragment.refCnt());
    assertEquals(0, arrayInsertedFragment.refCnt());
    assertEquals(0, arrayFirstFragment.refCnt());
    assertEquals(0, arrayLastFragment.refCnt());
    assertEquals(0, uniqueFragment.refCnt());

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < commands.length; i++) {
        MutationCommand command = commands[i];
        MultiResult result = response.responses().get(i);
        assertEquals(command.path(), result.path());
        assertEquals(command.mutation(), result.operation());

        sb.append('\n').append(result);
        ReferenceCountUtil.releaseLater(result.value());
    }
    if (sb.length() > 1)
        sb.deleteCharAt(0);

    String expectedResponse = "COUNTER(counter): SUCCESS = -404" + "\nCOUNTER(another.counter): SUCCESS = -404"
            + "\nCOUNTER(another.counter): SUCCESS = -808" +
            //values below have no content
            "\nDICT_ADD(sub.value2): SUCCESS" + "\nDICT_UPSERT(sub.value3): SUCCESS"
            + "\nREPLACE(value): SUCCESS" + "\nARRAY_INSERT(sub.array[1]): SUCCESS"
            + "\nARRAY_PUSH_FIRST(sub.array): SUCCESS" + "\nARRAY_PUSH_LAST(sub.array): SUCCESS"
            + "\nARRAY_ADD_UNIQUE(sub.array): SUCCESS" + "\nDELETE(sub.value): SUCCESS";
    assertEquals(expectedResponse, sb.toString());

    String expected = "{\"value\":\"mutated\"," + "\"sub\":{" +
    //                "\"value\":\"subStringValue\"," + //DELETED
            "\"array\":[\"first\",\"array1\",\"inserted\",2,true,\"last\",\"unique\"]"
            + ",\"value2\":\"mutated\"" + ",\"value3\":\"mutated\"}," + "\"counter\":-404,"
            + "\"another\":{\"counter\":-808}}";
    assertMutation(testSubKey, expected);
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldFailSomeMultiMutationsAndReleaseCommandFragments() {
    ByteBuf counterFragment = Unpooled.copiedBuffer("-404", CharsetUtil.UTF_8);
    counterFragment.retain();/* w  w  w . ja  va2s .com*/

    ByteBuf stringFragment = Unpooled.copiedBuffer("\"mutated\"", CharsetUtil.UTF_8);
    stringFragment.retain(2);

    ByteBuf arrayInsertedFragment = Unpooled.copiedBuffer("\"inserted\"", CharsetUtil.UTF_8);
    ByteBuf arrayFirstFragment = Unpooled.copiedBuffer("\"first\"", CharsetUtil.UTF_8);
    ByteBuf arrayLastFragment = Unpooled.copiedBuffer("\"last\"", CharsetUtil.UTF_8);
    ByteBuf uniqueFragment = Unpooled.copiedBuffer("\"unique\"", CharsetUtil.UTF_8);

    SubMultiMutationRequest request = new SubMultiMutationRequest(testSubKey, bucket(),
            new MutationCommand(Mutation.COUNTER, "counter", counterFragment, false),
            new MutationCommand(Mutation.COUNTER, "another.counter", counterFragment, true),
            new MutationCommand(Mutation.DICT_ADD, "sub.value2", stringFragment),
            new MutationCommand(Mutation.DICT_UPSERT, "sub.value3", stringFragment),
            new MutationCommand(Mutation.REPLACE, "value", stringFragment),
            //this one fails
            new MutationCommand(Mutation.ARRAY_INSERT, "sub.array[5]", arrayInsertedFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_FIRST, "sub.array", arrayFirstFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_LAST, "sub.array", arrayLastFragment),
            new MutationCommand(Mutation.ARRAY_ADD_UNIQUE, "sub.array", uniqueFragment),
            //this one would also fail, but server stops at first failure
            new MutationCommand(Mutation.DELETE, "path.not.found"));
    MultiMutationResponse response = cluster().<MultiMutationResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUBDOC_MULTI_PATH_FAILURE, response.status());
    assertEquals(Unpooled.EMPTY_BUFFER, response.content());
    assertEquals(5, response.firstErrorIndex());
    assertEquals(ResponseStatus.SUBDOC_PATH_NOT_FOUND, response.firstErrorStatus());
    assertEquals(0, response.responses().size());

    assertEquals(0, stringFragment.refCnt());
    assertEquals(0, counterFragment.refCnt());
    assertEquals(0, arrayInsertedFragment.refCnt());
    assertEquals(0, arrayFirstFragment.refCnt());
    assertEquals(0, arrayLastFragment.refCnt());
    assertEquals(0, uniqueFragment.refCnt());

    //no mutation happened
    String expected = jsonContent;
    assertMutation(testSubKey, expected);
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldFailAllMultiMutationsAndReleaseCommandFragments() {
    ByteBuf counterFragment = Unpooled.copiedBuffer("-404", CharsetUtil.UTF_8);
    ByteBuf stringFragment = Unpooled.copiedBuffer("\"mutated\"", CharsetUtil.UTF_8);
    ByteBuf arrayFirstFragment = Unpooled.copiedBuffer("\"first\"", CharsetUtil.UTF_8);

    SubMultiMutationRequest request = new SubMultiMutationRequest(testInsertionSubKey, bucket(),
            new MutationCommand(Mutation.COUNTER, "counter", counterFragment, false),
            new MutationCommand(Mutation.DICT_UPSERT, "sub.value3", stringFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_FIRST, "sub.array", arrayFirstFragment),
            new MutationCommand(Mutation.DELETE, "some.paht"));
    MultiMutationResponse response = cluster().<MultiMutationResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.NOT_EXISTS, response.status());
    assertEquals(Unpooled.EMPTY_BUFFER, response.content());
    assertEquals(-1, response.firstErrorIndex());
    assertEquals(ResponseStatus.FAILURE, response.firstErrorStatus());
    assertEquals(0, response.responses().size());

    assertEquals(0, stringFragment.refCnt());
    assertEquals(0, counterFragment.refCnt());
    assertEquals(0, arrayFirstFragment.refCnt());
}

From source file:com.couchbase.client.core.config.loader.CarrierLoaderTest.java

License:Apache License

@Test
public void shouldDiscoverConfig() {
    ClusterFacade cluster = mock(ClusterFacade.class);
    ByteBuf content = Unpooled.copiedBuffer("myconfig", CharsetUtil.UTF_8);
    Observable<CouchbaseResponse> response = Observable
            .just((CouchbaseResponse) new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                    KeyValueStatus.SUCCESS.code(), "bucket", content, host));
    when(cluster.send(isA(GetBucketConfigRequest.class))).thenReturn(response);

    CarrierLoader loader = new CarrierLoader(cluster, environment);
    Observable<String> configObservable = loader.discoverConfig("bucket", "password", host);
    assertEquals("myconfig", configObservable.toBlocking().single());
}

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

License:Apache License

@Test
public void shouldProposeConfigFromTaintedPoller() 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 ww w.j  a  v  a2s  . 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, "localhost:8091", ports));
    when(config.nodes()).thenReturn(nodeInfos);

    final AtomicReference<ByteBuf> bufRef = new AtomicReference<ByteBuf>(null);
    when(cluster.send(any(GetBucketConfigRequest.class)))
            .thenAnswer(new Answer<Observable<GetBucketConfigResponse>>() {
                @Override
                public Observable<GetBucketConfigResponse> answer(InvocationOnMock invocation)
                        throws Throwable {
                    ByteBuf content = Unpooled.copiedBuffer("{\"config\": true}", CharsetUtil.UTF_8);
                    ByteBuf oldContent = bufRef.getAndSet(content);
                    if (oldContent != null) {
                        assertEquals(0, oldContent.refCnt());
                    }
                    GetBucketConfigResponse response = new GetBucketConfigResponse(ResponseStatus.SUCCESS,
                            KeyValueStatus.SUCCESS.code(), "bucket", content,
                            InetAddress.getByName("localhost"));
                    return Observable.just(response);
                }
            });

    refresher.markTainted(config);

    Thread.sleep(1500);

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