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

License:Apache License

@Test
public void shouldUrlEncodeShortKeys() {
    String urlEncodedKeys = "%5B%221%22%2C%222%22%2C%223%22%5D";
    String keys = "[\"1\",\"2\",\"3\"]";
    String query = "stale=false&endKey=test";
    ViewQueryRequest request = new ViewQueryRequest("design", "view", true, query, keys, "bucket", "password");
    channel.writeOutbound(request);/*  w ww  .ja  v a 2s  .c om*/
    DefaultFullHttpRequest outbound = (DefaultFullHttpRequest) channel.readOutbound();
    String failMsg = outbound.getUri();
    assertEquals(HttpMethod.GET, outbound.getMethod());
    assertTrue(failMsg, outbound.getUri().contains("keys="));
    assertTrue(failMsg, outbound.getUri().endsWith("?stale=false&endKey=test&keys=" + urlEncodedKeys));
    String content = outbound.content().toString(CharsetUtil.UTF_8);
    assertTrue(content.isEmpty());
    ReferenceCountUtil.releaseLater(outbound); //NO-OP since content is empty but still...
}

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

License:Apache License

@Test
public void shouldProduceValidUrlIfShortKeysAndNoOtherQueryParam() {
    String urlEncodedKeys = "%5B%221%22%2C%222%22%2C%223%22%5D";
    String keys = "[\"1\",\"2\",\"3\"]";
    String query = "";
    ViewQueryRequest request = new ViewQueryRequest("design", "view", true, query, keys, "bucket", "password");
    channel.writeOutbound(request);/*  w  w w  . j  a v  a  2 s  . c o  m*/
    DefaultFullHttpRequest outbound = (DefaultFullHttpRequest) channel.readOutbound();
    String failMsg = outbound.getUri();
    assertEquals(HttpMethod.GET, outbound.getMethod());
    assertTrue(failMsg, outbound.getUri().endsWith("?keys=" + urlEncodedKeys));
    String content = outbound.content().toString(CharsetUtil.UTF_8);
    assertTrue(content.isEmpty());
    ReferenceCountUtil.releaseLater(outbound); //NO-OP since content is empty but still...
}

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

License:Apache License

@Test
public void shouldDoNothingOnNullKeys() {
    String keys = null;/*w ww. j  a v a2  s.c  o m*/
    String query = "stale=false&endKey=test";
    ViewQueryRequest request = new ViewQueryRequest("design", "view", true, query, keys, "bucket", "password");
    channel.writeOutbound(request);
    DefaultFullHttpRequest outbound = (DefaultFullHttpRequest) channel.readOutbound();
    assertEquals(HttpMethod.GET, outbound.getMethod());
    assertFalse(outbound.getUri().contains("keys="));
    assertTrue(outbound.getUri().endsWith("?stale=false&endKey=test"));
    String content = outbound.content().toString(CharsetUtil.UTF_8);
    assertTrue(content.isEmpty());
    ReferenceCountUtil.releaseLater(outbound); //NO-OP since content is empty but still...
}

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

License:Apache License

@Test
public void shouldDoNothingOnEmptyKeys() {
    String keys = "";
    String query = "stale=false&endKey=test";
    ViewQueryRequest request = new ViewQueryRequest("design", "view", true, query, keys, "bucket", "password");
    channel.writeOutbound(request);//from  ww  w  . j  a  v  a2  s  . c om
    DefaultFullHttpRequest outbound = (DefaultFullHttpRequest) channel.readOutbound();
    assertEquals(HttpMethod.GET, outbound.getMethod());
    assertFalse(outbound.getUri().contains("keys="));
    assertTrue(outbound.getUri().endsWith("?stale=false&endKey=test"));
    String content = outbound.content().toString(CharsetUtil.UTF_8);
    assertTrue(content.isEmpty());
    ReferenceCountUtil.releaseLater(outbound); //NO-OP since content is empty but still...
}

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);/*from  w  w w  .  j av  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 w 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(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);/*  ww  w . ja  v  a2  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);//w  ww.j  av  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());
    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.AbstractKeyValueRequest.java

License:Apache License

/**
 * Creates a new {@link AbstractKeyValueRequest}.
 *
 * @param key        the key of the document.
 * @param bucket     the bucket of the document.
 * @param password   the optional password of the bucket.
 * @param observable the observable which receives responses.
 *///from ww  w .  jav a2 s  . c om
protected AbstractKeyValueRequest(String key, String bucket, String password,
        Subject<CouchbaseResponse, CouchbaseResponse> observable) {
    super(bucket, password, observable);
    this.key = key;
    this.keyBytes = key == null || key.isEmpty() ? new byte[] {} : key.getBytes(CharsetUtil.UTF_8);
    opaque = GLOBAL_OPAQUE++;
}

From source file:com.couchbase.client.core.message.kv.GetResponse.java

License:Apache License

@Override
public String toString() {
    return new StringBuilder().append("GetResponse{").append("bucket='").append(bucket()).append('\'')
            .append(", status=").append(status()).append(" (").append(serverStatusCode()).append(')')
            .append(", cas=").append(cas).append(", flags=").append(flags).append(", request=")
            .append(request()).append(", content=").append(content().toString(CharsetUtil.UTF_8)).append('}')
            .toString();/* w  w  w  .j  a v  a2 s  .  co m*/
}