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.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
public void shouldDecodeSuccessfulGetDesignDocumentResponse() throws Exception {
    String response = Resources.read("designdoc_success.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    GetDesignDocumentRequest requestMock = mock(GetDesignDocumentRequest.class);
    when(requestMock.name()).thenReturn("name");
    when(requestMock.development()).thenReturn(true);
    queue.add(requestMock);/*from  ww w.  jav  a  2  s.co m*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GetDesignDocumentResponse inbound = (GetDesignDocumentResponse) firedEvents.get(0);

    assertTrue(inbound.status().isSuccess());
    assertEquals("name", inbound.name());
    assertEquals(true, inbound.development());
    assertEquals(response, inbound.content().toString(CharsetUtil.UTF_8));
    ReferenceCountUtil.releaseLater(inbound);
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
public void shouldDecodeWithDebugViewQueryResponse() throws Exception {
    String response = Resources.read("query_debug.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);/*from   ww w .ja  va2 s  .c  o  m*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);

    assertTrue(inbound.status().isSuccess());
    assertEquals(5, countAndRelease(inbound.rows()));

    inbound.info().toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            try {
                Map found = mapper.readValue(byteBuf.toString(CharsetUtil.UTF_8), Map.class);
                assertEquals(2, found.size());
                assertTrue(found.containsKey("debug_info"));
                assertTrue(found.containsKey("total_rows"));
            } catch (IOException e) {
                e.printStackTrace();
                assertFalse(true);
            }
            ReferenceCountUtil.releaseLater(byteBuf);
        }
    });
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
public void shouldDecodeEmptyViewQueryResponse() throws Exception {
    String response = Resources.read("query_empty.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);/*www  .  j a  v  a 2 s. co m*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);

    assertTrue(inbound.status().isSuccess());
    assertTrue(inbound.rows().toList().toBlocking().single().isEmpty());

    final AtomicInteger called = new AtomicInteger();
    inbound.info().toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            called.incrementAndGet();
            assertEquals("{\"total_rows\":7303}", byteBuf.toString(CharsetUtil.UTF_8));
            ReferenceCountUtil.releaseLater(byteBuf);
        }
    });
    assertEquals(1, called.get());
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
public void shouldDecodeOneViewQueryResponse() throws Exception {
    String response = Resources.read("query_one.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);//  w w w  .ja va  2 s.  co m
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);

    assertTrue(inbound.status().isSuccess());
    assertEquals(200, inbound.responseCode());
    assertEquals("OK", inbound.responsePhrase());

    final AtomicInteger calledRow = new AtomicInteger();
    inbound.rows().toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            calledRow.incrementAndGet();
            try {
                Map found = mapper.readValue(byteBuf.toString(CharsetUtil.UTF_8), Map.class);
                assertEquals(3, found.size());
                assertTrue(found.containsKey("id"));
                assertTrue(found.containsKey("key"));
                assertTrue(found.containsKey("value"));
            } catch (IOException e) {
                e.printStackTrace();
                assertFalse(true);
            }
            ReferenceCountUtil.releaseLater(byteBuf);
        }
    });
    assertEquals(1, calledRow.get());

    final AtomicInteger called = new AtomicInteger();
    inbound.info().toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            called.incrementAndGet();
            assertEquals("{\"total_rows\":7303}", byteBuf.toString(CharsetUtil.UTF_8));
            ReferenceCountUtil.releaseLater(byteBuf);
        }
    });
    assertEquals(1, called.get());
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
public void shouldDecodeManyViewQueryResponse() throws Exception {
    String response = Resources.read("query_many.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk1 = new DefaultHttpContent(
            Unpooled.copiedBuffer(response.substring(0, 500), CharsetUtil.UTF_8));
    HttpContent responseChunk2 = new DefaultHttpContent(
            Unpooled.copiedBuffer(response.substring(500, 1234), CharsetUtil.UTF_8));
    HttpContent responseChunk3 = new DefaultLastHttpContent(
            Unpooled.copiedBuffer(response.substring(1234), CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);//w  ww .  j a v a 2  s  .  co  m
    channel.writeInbound(responseHeader, responseChunk1, responseChunk2, responseChunk3);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);

    assertTrue(inbound.status().isSuccess());

    final AtomicInteger calledRow = new AtomicInteger();
    inbound.rows().toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            calledRow.incrementAndGet();
            try {
                Map found = mapper.readValue(byteBuf.toString(CharsetUtil.UTF_8), Map.class);
                assertEquals(3, found.size());
                assertTrue(found.containsKey("id"));
                assertTrue(found.containsKey("key"));
                assertTrue(found.containsKey("value"));
            } catch (IOException e) {
                e.printStackTrace();
                assertFalse(true);
            }
            ReferenceCountUtil.releaseLater(byteBuf);

        }
    });
    assertEquals(500, calledRow.get());

    final AtomicInteger called = new AtomicInteger();
    inbound.info().toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            called.incrementAndGet();
            assertEquals("{\"total_rows\":7303}", byteBuf.toString(CharsetUtil.UTF_8));
            ReferenceCountUtil.releaseLater(byteBuf);
        }
    });
    assertEquals(1, called.get());
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
@SuppressWarnings("unchecked")
public void shouldParseErrorWithEmptyRows() throws Exception {
    String response = Resources.read("error_empty_rows.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk1 = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);/*ww w . j a  v  a2  s  .  c o  m*/
    channel.writeInbound(responseHeader, responseChunk1);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);
    assertTrue(inbound.status().isSuccess());

    assertEquals(0, countAndRelease(inbound.rows()));

    String error = inbound.error().toBlocking().single();
    Map<String, Object> parsed = mapper.readValue(error, Map.class);
    assertEquals(1, parsed.size());
    assertNotNull(parsed.get("errors"));
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
@SuppressWarnings("unchecked")
public void shouldParseErrorAfterRows() throws Exception {
    String response = Resources.read("error_rows.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk1 = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);/*from w ww.j  a v a 2  s.c  om*/
    channel.writeInbound(responseHeader, responseChunk1);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);
    assertTrue(inbound.status().isSuccess());

    assertEquals(10, countAndRelease(inbound.rows()));

    String error = inbound.error().toBlocking().single();
    Map<String, Object> parsed = mapper.readValue(error, Map.class);
    assertEquals(1, parsed.size());
    assertNotNull(parsed.get("errors"));
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
public void shouldParseErrorWithDesignNotFound() throws Exception {
    String response = Resources.read("designdoc_notfound.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(404, "Object Not Found"));
    HttpContent responseChunk1 = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);/*from ww w .  j  a v a  2  s .c om*/
    channel.writeInbound(responseHeader, responseChunk1);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);
    assertFalse(inbound.status().isSuccess());
    assertEquals(ResponseStatus.NOT_EXISTS, inbound.status());

    assertEquals(0, countAndRelease(inbound.rows()));

    String error = inbound.error().toBlocking().single();
    assertEquals(
            "{\"errors\":[{\"error\":\"not_found\",\"reason\":\"Design document _design/designdoc not found\"}]}",
            error);

}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
public void shouldParseRowWithBracketInIdKeysAndValue() throws Exception {
    String response = Resources.read("query_brackets.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);//from   www  .j a va  2  s .com
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);

    assertTrue(inbound.status().isSuccess());
    assertEquals(200, inbound.responseCode());
    assertEquals("OK", inbound.responsePhrase());

    ByteBuf singleRow = inbound.rows().toBlocking().single(); //single will blow up if not exactly one
    String singleRowData = singleRow.toString(CharsetUtil.UTF_8);
    ReferenceCountUtil.releaseLater(singleRow);
    Map found = null;
    try {
        found = mapper.readValue(singleRowData, Map.class);
    } catch (IOException e) {
        e.printStackTrace();
        fail("Failed parsing JSON on data " + singleRowData);
    }

    assertEquals(3, found.size());
    assertTrue(found.containsKey("id"));
    assertTrue(found.containsKey("key"));
    assertTrue(found.containsKey("value"));
    assertEquals("IdClosing}BracketId", found.get("id"));
    assertEquals(Arrays.asList("KeyClosing}BracketKey", "KeySquareClosing]SquareBracketKey"), found.get("key"));
    assertEquals("ValueClosing}BracketValue", found.get("value"));

    final AtomicInteger called = new AtomicInteger();
    inbound.info().toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            called.incrementAndGet();
            assertEquals("{\"total_rows\":1}", byteBuf.toString(CharsetUtil.UTF_8));
            ReferenceCountUtil.releaseLater(byteBuf);
        }
    });
    assertEquals(1, called.get());
}

From source file:com.couchbase.client.core.message.kv.subdoc.simple.SubCounterRequest.java

License:Apache License

private static ByteBuf deltaToFragment(long delta) {
    String sDelta = "" + delta;
    return Unpooled.copiedBuffer(sDelta, CharsetUtil.UTF_8);
}