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.query.QueryHandlerTest.java

License:Apache License

@Test
public void shouldDecodeRawJsonResults() throws Exception {
    String response = Resources.read("raw_success_8.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 www.j  a  va2 s .c  o m
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    final List<String> items = Collections.synchronizedList(new ArrayList<String>(11));
    final AtomicInteger invokeCounter1 = new AtomicInteger();
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success", "\"json\"",
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    String item = buf.toString(CharsetUtil.UTF_8).trim();
                    System.out.println("item #" + invokeCounter1.incrementAndGet() + " = " + item);
                    items.add(item);
                    buf.release();
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    fail("no error expected");
                }
            },
            //no metrics in this json sample
            expectedMetricsCounts(0, 8));
    List<String> expectedItems = Arrays.asList("\"usertable:userAA\"", "\"usertable:user1\"",
            "\"usertable:user,2]\"", "null", "\"usertable:user3\"", "\"u,s,e,r,t,a,\\\"b,l,e:userBBB\\\\\"",
            "123", "\"usertable:user4\"");
    assertEquals(8, invokeCounter1.get());
    assertEquals(expectedItems, items);
}

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

License:Apache License

@Test
public void shouldDecodeRawJsonWithOneResult() throws Exception {
    String response = Resources.read("raw_success_1.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 .ja  v  a  2  s .  co m*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    final List<String> items = Collections.synchronizedList(new ArrayList<String>(11));
    final AtomicInteger invokeCounter1 = new AtomicInteger();
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success", "\"json\"",
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    String item = buf.toString(CharsetUtil.UTF_8).trim();
                    System.out.println("item #" + invokeCounter1.incrementAndGet() + " = " + item);
                    items.add(item);
                    buf.release();
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 1));
    List<String> expectedItems = Arrays.asList("\"u,s,e,r,t,a,\\\"b,l,e:userBBB\\\\\"");
    assertEquals(1, invokeCounter1.get());
    assertEquals(expectedItems, items);
}

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

License:Apache License

@Test
public void shouldDecodeSuccess1NoMetrics() throws Exception {
    String response = Resources.read("success_1_noMetrics.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);//  w w w . ja v a 2s . c om
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    final List<String> items = Collections.synchronizedList(new ArrayList<String>(11));
    final AtomicInteger invokeCounter1 = new AtomicInteger();
    assertResponse(inbound, true, ResponseStatus.SUCCESS, "ff226b49-9d4c-415b-8428-263cb080e184", "", "success",
            "{\"*\":\"*\"}", new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    String item = buf.toString(CharsetUtil.UTF_8).trim();
                    System.out.println("item #" + invokeCounter1.incrementAndGet() + " = " + item);
                    items.add(item);
                    buf.release();
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    fail("no error expected");
                }
            }, null);
    assertEquals(1, invokeCounter1.get());
    assertEquals("{\"adHoc_N1qlQuery492841478131758\":{\"item\":\"value\"}}",
            items.get(0).replaceAll("\\s", ""));
}

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

License:Apache License

private void shouldDecodeChunkedWithRaw(final int expectedResults, final int expectedErrors, String... chunks)
        throws Exception {
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    Object[] httpChunks = new Object[chunks.length + 1];
    httpChunks[0] = responseHeader;/*from ww  w.j a v a  2 s.  c o m*/
    for (int i = 1; i <= chunks.length; i++) {
        String chunk = chunks[i - 1];
        if (i == chunks.length) {
            httpChunks[i] = new DefaultLastHttpContent(Unpooled.copiedBuffer(chunk, CharsetUtil.UTF_8));
        } else {
            httpChunks[i] = new DefaultHttpContent(Unpooled.copiedBuffer(chunk, CharsetUtil.UTF_8));
        }
    }

    Subject<CouchbaseResponse, CouchbaseResponse> obs = AsyncSubject.create();
    GenericQueryRequest requestMock = mock(GenericQueryRequest.class);
    when(requestMock.observable()).thenReturn(obs);
    queue.add(requestMock);
    channel.writeInbound(httpChunks);
    GenericQueryResponse inbound = (GenericQueryResponse) obs.timeout(1, TimeUnit.SECONDS).toBlocking().last();

    final AtomicInteger found = new AtomicInteger(0);
    final AtomicInteger errors = new AtomicInteger(0);
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID,
            "1234567890123456789012345678901234567890123456789012345678901234", "success", "\"json\"",
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf byteBuf) {
                    found.incrementAndGet();
                    String content = byteBuf.toString(CharsetUtil.UTF_8);
                    byteBuf.release();
                    assertNotNull(content);
                    assertTrue(!content.isEmpty());
                    try {
                        Object object = mapper.readValue(content, Object.class);
                        boolean expected = object instanceof Integer || object == null
                                || (object instanceof String && ((String) object).startsWith("usertable"))
                                || (object instanceof String && ((String) object).startsWith("u,s,e,r"));
                        assertTrue(expected);

                    } catch (Exception e) {
                        e.printStackTrace();
                        fail();
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    errors.incrementAndGet();
                }
            }, expectedMetricsCounts(expectedErrors, expectedResults) //these are the numbers parsed from metrics object, not real count
    );
    assertEquals(expectedResults, found.get());
    assertEquals(expectedErrors, errors.get());
}

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

License:Apache License

@Test
public void shouldDecodeSimpleStringAsSignature() throws Exception {
    String response = Resources.read("signature_simple_string.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);/*w  ww  . j a  v a2s . c  o m*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    final AtomicInteger invokeCounter1 = new AtomicInteger();
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success", "\"json\"",
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String item = buf.toString(CharsetUtil.UTF_8);
                    buf.release();
                    fail("no result expected, got " + item);
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    fail("no error expected");
                }
            },
            //no metrics in this json sample
            expectedMetricsCounts(0, 1));
    assertEquals(0, invokeCounter1.get());
}

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

License:Apache License

@Test
public void shouldDecodeNullAsSignature() throws Exception {
    String response = Resources.read("signature_null.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);//w w  w.  ja v a2  s  . c  o  m
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    final AtomicInteger invokeCounter1 = new AtomicInteger();
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success", "null",
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String item = buf.toString(CharsetUtil.UTF_8);
                    buf.release();
                    fail("no result expected, got " + item);
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    fail("no error expected");
                }
            },
            //no metrics in this json sample
            expectedMetricsCounts(0, 1));
    assertEquals(0, invokeCounter1.get());
}

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

License:Apache License

@Test
public void shouldDecodeBooleanAsSignature() throws Exception {
    String response = Resources.read("signature_scalar.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 w w .j  a  v  a 2 s.  c  om*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    final AtomicInteger invokeCounter1 = new AtomicInteger();
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success", "true",
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String item = buf.toString(CharsetUtil.UTF_8);
                    buf.release();
                    fail("no result expected, got " + item);
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    fail("no error expected");
                }
            },
            //no metrics in this json sample
            expectedMetricsCounts(0, 1));
    assertEquals(0, invokeCounter1.get());
}

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

License:Apache License

@Test
public void shouldDecodeArrayAsSignature() throws Exception {
    String response = Resources.read("signature_array.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 w  w  .j a  v  a2  s .c o  m
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    final AtomicInteger invokeCounter1 = new AtomicInteger();
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success",
            "[\"json\",\"array\",[\"sub\",true]]", new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String item = buf.toString(CharsetUtil.UTF_8);
                    buf.release();
                    fail("no result expected, got " + item);
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    fail("no error expected");
                }
            },
            //no metrics in this json sample
            expectedMetricsCounts(0, 1));
    assertEquals(0, invokeCounter1.get());
}

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

License:Apache License

/**
 * See JVMCBC-239./*from   ww w  . j  a  v a  2  s  .  c om*/
 */
@Test
public void testEarlyChunkInSignatureDoesntFail() throws Exception {
    String chunk1 = "{\n" + "    \"requestID\": \"7cde0ed9-1844-436d-85b2-a7b9cd12361c\",\n"
            + "    \"clientContextID\": \"sdkd-java\",\n" + "    \"signature\": {\n" + "  ";
    String chunk2 = "      \"*\": \"*\"\n" + "    },\n" + "    \"results\": [\n" + "        {\n"
            + "            \"default\": {\n" + "                \"id\": 375,\n"
            + "                \"tag\": \"n1ql\",\n" + "                \"type\": \"n1qldoc\"\n"
            + "            }\n" + "        }\n" + "    ],\n" + "    \"status\": \"success\",\n"
            + "    \"metrics\": {\n" + " ";
    String chunk3 = "       \"elapsedTime\": \"1m18.410321814s\",\n"
            + "        \"executionTime\": \"1m18.410092882s\",\n" + "        \"resultCount\": 1,\n"
            + "        \"resultSize\": 100,\n" + "        \"mutationCount\": 0,\n"
            + "        \"errorCount\": 0,\n" + "        \"warningCount\": 0\n" + "    }\n" + "}";

    String[] chunks = new String[] { chunk1, chunk2, chunk3 };
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    Object[] httpChunks = new Object[chunks.length + 1];
    httpChunks[0] = responseHeader;
    for (int i = 1; i <= chunks.length; i++) {
        String chunk = chunks[i - 1];
        if (i == chunks.length) {
            httpChunks[i] = new DefaultLastHttpContent(Unpooled.copiedBuffer(chunk, CharsetUtil.UTF_8));
        } else {
            httpChunks[i] = new DefaultHttpContent(Unpooled.copiedBuffer(chunk, CharsetUtil.UTF_8));
        }
    }

    Subject<CouchbaseResponse, CouchbaseResponse> obs = AsyncSubject.create();
    GenericQueryRequest requestMock = mock(GenericQueryRequest.class);
    when(requestMock.observable()).thenReturn(obs);
    queue.add(requestMock);
    channel.writeInbound(httpChunks);
    GenericQueryResponse inbound = (GenericQueryResponse) obs.timeout(1, TimeUnit.SECONDS).toBlocking().last();

    final AtomicInteger found = new AtomicInteger(0);
    final AtomicInteger errors = new AtomicInteger(0);
    assertResponse(inbound, true, ResponseStatus.SUCCESS, "7cde0ed9-1844-436d-85b2-a7b9cd12361c", "sdkd-java",
            "success", "{\"*\":\"*\"}", new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf byteBuf) {
                    found.incrementAndGet();
                    String content = byteBuf.toString(CharsetUtil.UTF_8);
                    byteBuf.release();
                    assertNotNull(content);
                    assertTrue(!content.isEmpty());
                    try {
                        Map decoded = mapper.readValue(content, Map.class);
                        assertTrue(decoded.size() > 0);
                        assertTrue(decoded.containsKey("default"));
                    } catch (Exception e) {
                        assertTrue(false);
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    errors.incrementAndGet();
                }
            }, expectedMetricsCounts(0, 1) //these are the numbers parsed from metrics object, not real count
    );
    assertEquals(1, found.get());
    assertEquals(0, errors.get());
}

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

License:Apache License

@Test
public void testBigChunkedResponseWithEscapedBackslashInRowObject() throws Exception {
    String response = Resources.read("chunkedResponseWithDoubleBackslashes.txt", this.getClass());
    String[] chunks = response.split("(?m)^[0-9a-f]+");

    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    responseHeader.headers().add("Transfer-Encoding", "chunked");
    responseHeader.headers().add("Content-Type", "application/json; version=1.0.0");
    Object[] httpChunks = new Object[chunks.length];
    httpChunks[0] = responseHeader;//ww  w  .  j  a v a 2 s.  co m
    for (int i = 1; i < chunks.length; i++) {
        String chunk = chunks[i];
        if (i == chunks.length - 1) {
            httpChunks[i] = new DefaultLastHttpContent(Unpooled.copiedBuffer(chunk, CharsetUtil.UTF_8));
        } else {
            httpChunks[i] = new DefaultHttpContent(Unpooled.copiedBuffer(chunk, CharsetUtil.UTF_8));
        }
    }

    Subject<CouchbaseResponse, CouchbaseResponse> obs = AsyncSubject.create();
    GenericQueryRequest requestMock = mock(GenericQueryRequest.class);
    when(requestMock.observable()).thenReturn(obs);
    queue.add(requestMock);
    channel.writeInbound(httpChunks);
    GenericQueryResponse inbound = (GenericQueryResponse) obs.timeout(1, TimeUnit.SECONDS).toBlocking().last();

    final AtomicInteger found = new AtomicInteger(0);
    final AtomicInteger errors = new AtomicInteger(0);
    inbound.rows().timeout(5, TimeUnit.SECONDS).toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf byteBuf) {
            int rowNumber = found.incrementAndGet();
            String content = byteBuf.toString(CharsetUtil.UTF_8);
            byteBuf.release();
            assertNotNull(content);
            assertFalse(content.isEmpty());
        }
    });

    inbound.errors().timeout(5, TimeUnit.SECONDS).toBlocking().forEach(new Action1<ByteBuf>() {
        @Override
        public void call(ByteBuf buf) {
            buf.release();
            errors.incrementAndGet();
        }
    });

    //ignore signature
    ReferenceCountUtil.release(inbound.signature().timeout(5, TimeUnit.SECONDS).toBlocking().single());

    String status = inbound.queryStatus().timeout(5, TimeUnit.SECONDS).toBlocking().single();

    List<ByteBuf> metricList = inbound.info().timeout(1, TimeUnit.SECONDS).toList().toBlocking().single();
    assertEquals(1, metricList.size());
    ByteBuf metricsBuf = metricList.get(0);
    ReferenceCountUtil.releaseLater(metricsBuf);
    Map metrics = mapper.readValue(metricsBuf.toString(CharsetUtil.UTF_8), Map.class);

    assertEquals("success", status);

    assertEquals(5, found.get());
    assertEquals(0, errors.get());

    assertEquals(found.get(), metrics.get("resultCount"));
}