Example usage for io.netty.util CharsetUtil UTF_8

List of usage examples for io.netty.util CharsetUtil UTF_8

Introduction

In this page you can find the example usage for io.netty.util CharsetUtil UTF_8.

Prototype

Charset UTF_8

To view the source code for io.netty.util CharsetUtil UTF_8.

Click Source Link

Document

8-bit UTF (UCS Transformation Format)

Usage

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldNotReleaseStoreRequestContentOnRetry() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);//from  w  w w  .j av  a 2  s  .  com
    response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code());

    UpsertRequest requestMock = mock(UpsertRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldReleaseAppendRequestContentOnSuccess() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);/*  w ww .  j ava2 s.  c  o m*/
    response.setStatus(BinaryMemcacheResponseStatus.SUCCESS);

    AppendRequest requestMock = mock(AppendRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(0, requestContent.refCnt());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldNotReleaseAppendRequestContentOnRetry() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);// w  w w . j av  a2  s  .c om
    response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code());

    AppendRequest requestMock = mock(AppendRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldReleasePrependRequestContentOnSuccess() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);/*w ww  . java2  s  .  com*/
    response.setStatus(BinaryMemcacheResponseStatus.SUCCESS);

    PrependRequest requestMock = mock(PrependRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(0, requestContent.refCnt());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldNotReleasePrependRequestContentOnRetry() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);// www  .ja v a 2s . c o  m
    response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code());

    PrependRequest requestMock = mock(PrependRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    when(requestMock.observable()).thenReturn(AsyncSubject.<CouchbaseResponse>create());
    when(requestMock.content()).thenReturn(requestContent);
    requestQueue.add(requestMock);

    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
    channel.writeInbound(response);
    assertEquals(1, content.refCnt());
    assertEquals(1, requestContent.refCnt());
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test(expected = CouchbaseException.class)
public void shouldFailWhenOpaqueDoesNotMatch() throws Exception {
    ByteBuf content = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(KEY, Unpooled.EMPTY_BUFFER,
            content);/*from   w  w w  .  ja va  2 s. c o m*/
    response.setStatus(BinaryMemcacheResponseStatus.SUCCESS);
    response.setOpaque(1);

    PrependRequest requestMock = mock(PrependRequest.class);
    ByteBuf requestContent = Unpooled.copiedBuffer("content", CharsetUtil.UTF_8);
    when(requestMock.bucket()).thenReturn("bucket");
    AsyncSubject<CouchbaseResponse> responseSubject = AsyncSubject.<CouchbaseResponse>create();
    when(requestMock.observable()).thenReturn(responseSubject);
    when(requestMock.content()).thenReturn(requestContent);
    when(requestMock.opaque()).thenReturn(3);
    requestQueue.add(requestMock);

    channel.writeInbound(response);
    assertEquals(0, content.refCnt());
    responseSubject.toBlocking().single();
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

private void assertGenericQueryRequest(GenericQueryRequest request, boolean jsonExpected) {
    channel.writeOutbound(request);//from w ww. j av a2 s  . c  o  m
    HttpRequest outbound = (HttpRequest) channel.readOutbound();

    assertEquals(HttpMethod.POST, outbound.getMethod());
    assertEquals(HttpVersion.HTTP_1_1, outbound.getProtocolVersion());
    assertEquals("/query", outbound.getUri());
    assertTrue(outbound.headers().contains(HttpHeaders.Names.AUTHORIZATION));
    assertEquals("Couchbase Client Mock", outbound.headers().get(HttpHeaders.Names.USER_AGENT));
    assertTrue(outbound instanceof FullHttpRequest);
    assertEquals("query", ((FullHttpRequest) outbound).content().toString(CharsetUtil.UTF_8));
    if (jsonExpected) {
        assertEquals("application/json", outbound.headers().get(HttpHeaders.Names.CONTENT_TYPE));
    } else {
        assertNotEquals("application/json", outbound.headers().get(HttpHeaders.Names.CONTENT_TYPE));
    }
    assertTrue(outbound.headers().contains(HttpHeaders.Names.HOST));
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

/**
 *
 * @param inbound/* w  w w.j  av  a 2s.c o m*/
 * @param expectedSuccess
 * @param expectedStatus
 * @param expectedRequestId
 * @param expectedClientId
 * @param expectedFinalStatus
 * @param expectedSignature
 * @param assertRows
 * @param assertErrors
 * @param metricsToCheck null to expect no metrics
 */
private void assertResponse(GenericQueryResponse inbound, boolean expectedSuccess,
        ResponseStatus expectedStatus, String expectedRequestId, String expectedClientId,
        String expectedFinalStatus, String expectedSignature, Action1<ByteBuf> assertRows,
        Action1<ByteBuf> assertErrors, Map<String, Object> metricsToCheck) {
    assertEquals(expectedSuccess, inbound.status().isSuccess());
    assertEquals(expectedStatus, inbound.status());
    assertEquals(expectedRequestId, inbound.requestId());
    assertEquals(expectedClientId, inbound.clientRequestId());

    assertEquals(expectedFinalStatus, inbound.queryStatus().timeout(1, TimeUnit.SECONDS).toBlocking().single());

    inbound.rows().timeout(5, TimeUnit.SECONDS).toBlocking().forEach(assertRows);

    List<ByteBuf> metricList = inbound.info().timeout(1, TimeUnit.SECONDS).toList().toBlocking().single();
    if (metricsToCheck == null) {
        assertEquals(0, metricList.size());
    } else {
        assertEquals(1, metricList.size());
        String metricsJson = metricList.get(0).toString(CharsetUtil.UTF_8);
        metricList.get(0).release();
        try {
            Map metrics = mapper.readValue(metricsJson, Map.class);
            assertEquals(7, metrics.size());

            for (Map.Entry<String, Object> entry : metricsToCheck.entrySet()) {
                assertNotNull(metrics.get(entry.getKey()));
                assertEquals(entry.getKey(), entry.getValue(), metrics.get(entry.getKey()));
            }
        } catch (IOException e) {
            fail(e.toString());
        }
    }

    inbound.errors().timeout(1, TimeUnit.SECONDS).toBlocking().forEach(assertErrors);

    List<ByteBuf> signatureList = inbound.signature().timeout(1, TimeUnit.SECONDS).toList().toBlocking()
            .single();
    if (expectedSignature != null) {
        assertEquals(1, signatureList.size());
        String signatureJson = signatureList.get(0).toString(CharsetUtil.UTF_8);
        assertNotNull(signatureJson);
        assertEquals(expectedSignature, signatureJson.replaceAll("\\s", ""));
        ReferenceCountUtil.releaseLater(signatureList.get(0));
    } else {
        assertEquals(0, signatureList.size());
    }
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

@Test
public void shouldDecodeErrorResponse() throws Exception {
    String response = Resources.read("parse_error.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));

    GenericQueryRequest requestMock = mock(GenericQueryRequest.class);
    queue.add(requestMock);/*from   w ww. j a va  2s .  c  om*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    assertResponse(inbound, false, ResponseStatus.FAILURE, FAKE_REQUESTID, FAKE_CLIENTID, "fatal", null,
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf byteBuf) {
                    fail("no result expected");
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    String response = buf.toString(CharsetUtil.UTF_8);
                    try {
                        Map error = mapper.readValue(response, Map.class);
                        assertEquals(5, error.size());
                        assertEquals(new Integer(4100), error.get("code"));
                        assertEquals(Boolean.FALSE, error.get("temp"));
                        assertEquals("Parse Error", error.get("msg"));
                    } catch (IOException e) {
                        fail();
                    }
                    ReferenceCountUtil.releaseLater(buf);
                }
            }, expectedMetricsCounts(1, 0));
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

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

    GenericQueryRequest requestMock = mock(GenericQueryRequest.class);
    queue.add(requestMock);/*www .j av  a  2  s  . c  om*/
    channel.writeInbound(responseHeader, responseChunk1, responseChunk2);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    assertResponse(inbound, false, ResponseStatus.FAILURE, FAKE_REQUESTID, FAKE_CLIENTID, "fatal", null,
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf byteBuf) {
                    ReferenceCountUtil.releaseLater(byteBuf);
                    fail("no result expected");
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    String response = buf.toString(CharsetUtil.UTF_8);
                    ReferenceCountUtil.releaseLater(buf);
                    try {
                        Map error = mapper.readValue(response, Map.class);
                        assertEquals(5, error.size());
                        assertEquals(new Integer(4100), error.get("code"));
                        assertEquals(Boolean.FALSE, error.get("temp"));
                        assertEquals("Parse Error", error.get("msg"));
                    } catch (IOException e) {
                        fail();
                    }
                }
            }, expectedMetricsCounts(1, 0));
}