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 shouldDecodeEmptySuccessResponse() throws Exception {
    String response = Resources.read("success_0.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 ww w .j  a va  2s . c  o m
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success",
            FAKE_SIGNATURE, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf byteBuf) {
                    fail("no result expected");
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 0));
}

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

License:Apache License

@Test
public void shouldDecodeOneRowResponse() throws Exception {
    String response = Resources.read("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  w w  .  j av  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",
            FAKE_SIGNATURE, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String response = buf.toString(CharsetUtil.UTF_8);
                    buf.release();
                    try {
                        Map found = mapper.readValue(response, Map.class);
                        assertEquals(12, found.size());
                        assertEquals("San Francisco", found.get("city"));
                        assertEquals("United States", found.get("country"));
                        Map geo = (Map) found.get("geo");
                        assertNotNull(geo);
                        assertEquals(3, geo.size());
                        assertEquals("ROOFTOP", geo.get("accuracy"));
                    } catch (IOException e) {
                        fail("no result expected");
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    String error = buf.toString(CharsetUtil.UTF_8);
                    buf.release();
                    fail("no error expected, got " + error);
                }
            }, expectedMetricsCounts(0, 1));
    assertEquals(1, invokeCounter1.get());
}

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

License:Apache License

@Test
public void shouldDecodeNRowResponse() throws Exception {
    String response = Resources.read("success_5.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);/*ww  w  .  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 AtomicInteger found = new AtomicInteger(0);
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success",
            FAKE_SIGNATURE, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf row) {
                    found.incrementAndGet();
                    String content = row.toString(CharsetUtil.UTF_8);
                    row.release();
                    assertNotNull(content);
                    assertTrue(!content.isEmpty());
                    try {
                        Map decoded = mapper.readValue(content, Map.class);
                        assertTrue(decoded.size() > 0);
                        assertTrue(decoded.containsKey("name"));
                    } catch (Exception e) {
                        assertTrue(false);
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 5));
    assertEquals(5, found.get());
}

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

License:Apache License

@Test
public void shouldDecodeNRowResponseChunked() throws Exception {
    String response = Resources.read("success_5.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk1 = new DefaultHttpContent(
            Unpooled.copiedBuffer(response.substring(0, 300), CharsetUtil.UTF_8));
    HttpContent responseChunk2 = new DefaultHttpContent(
            Unpooled.copiedBuffer(response.substring(300, 950), CharsetUtil.UTF_8));
    HttpContent responseChunk3 = new DefaultHttpContent(
            Unpooled.copiedBuffer(response.substring(950, 1345), CharsetUtil.UTF_8));
    HttpContent responseChunk4 = new DefaultHttpContent(
            Unpooled.copiedBuffer(response.substring(1345, 3000), CharsetUtil.UTF_8));
    HttpContent responseChunk5 = new DefaultLastHttpContent(
            Unpooled.copiedBuffer(response.substring(3000), CharsetUtil.UTF_8));

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

    final AtomicInteger found = new AtomicInteger(0);
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, FAKE_CLIENTID, "success",
            FAKE_SIGNATURE, 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("name"));
                    } catch (Exception e) {
                        assertTrue(false);
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 5));
    assertEquals(5, found.get());
}

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

License:Apache License

@Test
public void shouldDecodeOneRowResponseWithQuotesInClientIdAndResults() throws Exception {
    String expectedClientIdWithQuotes = "ThisIsA\\\"Client\\\"Id";

    String response = Resources.read("with_escaped_quotes.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  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, expectedClientIdWithQuotes, "success",
            FAKE_SIGNATURE, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String response = buf.toString(CharsetUtil.UTF_8);
                    ReferenceCountUtil.releaseLater(buf);
                    try {
                        Map found = mapper.readValue(response, Map.class);
                        assertEquals(12, found.size());
                        assertEquals("San Francisco", found.get("city"));
                        assertEquals("United States", found.get("country"));
                        Map geo = (Map) found.get("geo");
                        assertNotNull(geo);
                        assertEquals(3, geo.size());
                        assertEquals("ROOFTOP", geo.get("accuracy"));
                        //TODO check the quote in the result
                    } catch (IOException e) {
                        assertFalse(true);
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 1));
    assertEquals(1, invokeCounter1.get());
}

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

License:Apache License

@Test
public void shouldDecodeOneRowResponseWithShortClientID() throws Exception {
    String response = Resources.read("short_client_id.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  ava 2 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, "123456789", "success",
            FAKE_SIGNATURE, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String response = buf.toString(CharsetUtil.UTF_8);
                    ReferenceCountUtil.releaseLater(buf);
                    try {
                        Map found = mapper.readValue(response, Map.class);
                        assertEquals(12, found.size());
                        assertEquals("San Francisco", found.get("city"));
                        assertEquals("United States", found.get("country"));
                        Map geo = (Map) found.get("geo");
                        assertNotNull(geo);
                        assertEquals(3, geo.size());
                        assertEquals("ROOFTOP", geo.get("accuracy"));
                    } catch (IOException e) {
                        assertFalse(true);
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 1));
    assertEquals(1, invokeCounter1.get());
}

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

License:Apache License

@Test
public void shouldDecodeOneRowResponseWithNoClientID() throws Exception {
    String response = Resources.read("no_client_id.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  va 2 s.co  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, "", "success", FAKE_SIGNATURE,
            new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String response = buf.toString(CharsetUtil.UTF_8);
                    try {
                        Map found = mapper.readValue(response, Map.class);
                        assertEquals(12, found.size());
                        assertEquals("San Francisco", found.get("city"));
                        assertEquals("United States", found.get("country"));
                        Map geo = (Map) found.get("geo");
                        assertNotNull(geo);
                        assertEquals(3, geo.size());
                        assertEquals("ROOFTOP", geo.get("accuracy"));
                    } catch (IOException e) {
                        assertFalse(true);
                    }
                    ReferenceCountUtil.releaseLater(buf);
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 1));
    assertEquals(1, invokeCounter1.get());
}

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

License:Apache License

@Test
public void shouldDecodeOneRowResponseWithoutPrettyPrint() throws Exception {
    String response = Resources.read("no_pretty.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 .j  a  va  2 s.  com*/
    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",
            FAKE_SIGNATURE, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    invokeCounter1.incrementAndGet();
                    String response = buf.toString(CharsetUtil.UTF_8);
                    buf.release();
                    try {
                        Map found = mapper.readValue(response, Map.class);
                        assertEquals(12, found.size());
                        assertEquals("San Francisco", found.get("city"));
                        assertEquals("United States", found.get("country"));
                        Map geo = (Map) found.get("geo");
                        assertNotNull(geo);
                        assertEquals(3, geo.size());
                        assertEquals("ROOFTOP", geo.get("accuracy"));
                    } catch (IOException e) {
                        assertFalse(true);
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    fail("no error expected");
                }
            }, expectedMetricsCounts(0, 1));
    assertEquals(1, invokeCounter1.get());
}

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

License:Apache License

@Test
public void shouldGroupErrorsAndWarnings() throws InterruptedException {
    String response = Resources.read("errors_and_warnings.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 av  a  2s  .  c  o m*/
    channel.writeInbound(responseHeader, responseChunk);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    GenericQueryResponse inbound = (GenericQueryResponse) firedEvents.get(0);

    Map<String, Object> expectedMetrics = expectedMetricsCounts(1, 0);
    expectedMetrics.put("warningCount", 1);

    final AtomicInteger count = new AtomicInteger(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) {
                    count.incrementAndGet();
                    String response = buf.toString(CharsetUtil.UTF_8);
                    buf.release();
                    try {
                        Map error = mapper.readValue(response, Map.class);
                        assertEquals(5, error.size());
                        if (count.get() == 1) {
                            assertEquals(new Integer(4100), error.get("code"));
                            assertEquals(Boolean.FALSE, error.get("temp"));
                            assertEquals("Parse Error", error.get("msg"));
                        } else if (count.get() == 2) {
                            assertEquals(3, error.get("sev"));
                            assertEquals(201, error.get("code"));
                            assertEquals(Boolean.TRUE, error.get("temp"));
                            assertEquals("Nothing to do", error.get("msg"));
                            assertEquals("nothingToDo", error.get("name"));
                        }
                    } catch (IOException e) {
                        fail();
                    }
                }
            }, expectedMetrics);
    assertEquals(2, count.get());
}

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

License:Apache License

private void shouldDecodeChunked(boolean metrics, 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;/*w w  w  .ja  v  a 2  s  .  c  om*/
    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();
    Map<String, Object> expectedMetrics;
    if (metrics) {
        expectedMetrics = expectedMetricsCounts(5678, 1234); //these are the numbers parsed from metrics object, not real count
    } else {
        expectedMetrics = null;
    }

    final AtomicInteger found = new AtomicInteger(0);
    final AtomicInteger errors = new AtomicInteger(0);
    assertResponse(inbound, true, ResponseStatus.SUCCESS, FAKE_REQUESTID, "123456\\\"78901234567890", "success",
            "{\"horseName\":\"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 {
                        Map decoded = mapper.readValue(content, Map.class);
                        assertTrue(decoded.size() > 0);
                        assertTrue(decoded.containsKey("horseName"));
                    } catch (Exception e) {
                        assertTrue(false);
                    }
                }
            }, new Action1<ByteBuf>() {
                @Override
                public void call(ByteBuf buf) {
                    buf.release();
                    errors.incrementAndGet();
                }
            }, expectedMetrics);
    assertEquals(5, found.get());
    assertEquals(4, errors.get());
}