Search in sources :

Example 86 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestFixedTemplate method testFixedTemplate.

@Test
public void testFixedTemplate() {
    String[] goodObjects = { "12345", "ABCDF" };
    ByteString[] goodByteStrings = { ByteString.copyAvroString("qwert", false) };
    Object[] badObjects = { "", "1", "12", "123", "1234", "1234\u0100", "123456", 1, 2.0f, 3.0, 4L, new DataMap(), new DataList() };
    ByteString[] badByteStrings = { ByteString.copyAvroString("", false), ByteString.copyAvroString("a", false), ByteString.copyAvroString("ab", false), ByteString.copyAvroString("abc", false), ByteString.copyAvroString("abcd", false), ByteString.copyAvroString("abcdef", false) };
    Integer lastHashCode = null;
    ByteString lastByteString = null;
    for (String o : goodObjects) {
        Exception exc = null;
        Fixed5 fixed = null;
        try {
            fixed = new Fixed5(o);
        } catch (Exception e) {
            exc = e;
        }
        assertNull(exc);
        // equals
        ByteString expectedByteString = ByteString.copyAvroString(o, false);
        assertEquals(fixed.data(), expectedByteString);
        assertTrue(fixed.equals(new Fixed5(expectedByteString)));
        if (lastByteString != null) {
            assertFalse(fixed.equals(lastByteString));
        }
        assertFalse(fixed.equals(null));
        assertFalse(fixed.equals(new Object()));
        // hashCode
        int newHashCode = fixed.hashCode();
        if (lastHashCode != null) {
            assertTrue(newHashCode != lastHashCode);
        }
        // toString
        assertEquals(expectedByteString.toString(), fixed.toString());
        lastHashCode = newHashCode;
        lastByteString = expectedByteString;
        // clone and copy
        testCopiers(fixed);
    }
    for (ByteString o : goodByteStrings) {
        Exception exc = null;
        Fixed5 fixed = null;
        try {
            fixed = new Fixed5(o);
        } catch (Exception e) {
            exc = e;
        }
        assertNull(exc);
        // equals
        assertEquals(fixed.data(), o);
        assertTrue(fixed.equals(new Fixed5(o)));
        if (lastByteString != null) {
            assertFalse(fixed.equals(lastByteString));
        }
        assertFalse(fixed.equals(null));
        assertFalse(fixed.equals(new Object()));
        // hashCode
        int newHashCode = fixed.hashCode();
        if (lastHashCode != null) {
            assertTrue(newHashCode != lastHashCode);
        }
        // toString
        assertEquals(o.toString(), fixed.toString());
        lastHashCode = newHashCode;
        lastByteString = o;
        // clone and copy
        testCopiers(fixed);
    }
    for (Object o : badObjects) {
        Exception exc = null;
        try {
            new Fixed5(o);
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
    for (ByteString o : badByteStrings) {
        Exception exc = null;
        try {
            new Fixed5(o);
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
}
Also used : ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) DataList(com.linkedin.data.DataList) Test(org.testng.annotations.Test)

Example 87 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestFixedTemplate method testWrapping.

@Test
public void testWrapping() throws InstantiationException, IllegalAccessException {
    String input = "12345";
    ByteString input1 = ByteString.copyAvroString(input, false);
    Fixed5 fixed1 = DataTemplateUtil.wrap(input1, Fixed5.class);
    assertSame(input1, fixed1.data());
    ByteString input2 = ByteString.copyAvroString("67890", false);
    Fixed5 fixed2 = DataTemplateUtil.wrap(input2, Fixed5.SCHEMA, Fixed5.class);
    assertSame(input2, fixed2.data());
    Fixed5 fixed3 = DataTemplateUtil.wrap(input, Fixed5.class);
    assertEquals(fixed1, fixed3);
    Fixed5 fixed4 = DataTemplateUtil.wrap(input, Fixed5.SCHEMA, Fixed5.class);
    assertEquals(fixed3, fixed4);
}
Also used : ByteString(com.linkedin.data.ByteString) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 88 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestStreamFilterAdapters method testRequestFilterAdapterCallsOnResponse.

@Test
public void testRequestFilterAdapterCallsOnResponse() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onResponse(simpleRestResponse(req.getEntity().asString("UTF8")), requestContext, wireAttrs);
        }
    });
    fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    StreamResponse capturedReq = _beforeFilter.getResponse();
    capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("shouldn't have error");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "12345");
        }
    }));
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 89 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestStreamFilterAdapters method testResponseFilterAdapterChangeError.

@Test
public void testResponseFilterAdapterChangeError() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            if (ex instanceof RestException) {
                RestResponse res = ((RestException) ex).getResponse();
                String newEntityStr = res.getEntity().asString("UTF8").replace('1', '0');
                nextFilter.onError(new RestException((res.builder().setEntity(newEntityStr.getBytes()).build())), requestContext, wireAttrs);
            } else {
                nextFilter.onError(new IllegalStateException(), requestContext, wireAttrs);
            }
        }
    });
    fc.onStreamError(simpleStreamException("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    Throwable capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof StreamException);
    ((StreamException) capturedEx).getResponse().getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("should not happen");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "02345");
        }
    }));
    fc.onStreamError(new IllegalArgumentException(), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof IllegalStateException);
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) ByteString(com.linkedin.data.ByteString) StreamException(com.linkedin.r2.message.stream.StreamException) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 90 with ByteString

use of com.linkedin.data.ByteString in project rest.li by linkedin.

the class TestEntityStream method testObserversThrowUncheckedError.

@Test
public void testObserversThrowUncheckedError() {
    Observer observer = new TestObserver() {

        @Override
        public void onDone() {
            throw new Error("broken observer throws");
        }

        @Override
        public void onDataAvailable(ByteString data) {
            throw new Error("broken observer throws");
        }

        @Override
        public void onError(Throwable ex) {
            throw new Error("broken observer throws");
        }
    };
    Error ex = new Error("writer has problem");
    testObserverThrow(observer, ex);
}
Also used : ByteString(com.linkedin.data.ByteString) Observer(com.linkedin.r2.message.stream.entitystream.Observer) Test(org.testng.annotations.Test)

Aggregations

ByteString (com.linkedin.data.ByteString)152 Test (org.testng.annotations.Test)77 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 MimeMultipart (javax.mail.internet.MimeMultipart)31 MimeBodyPart (javax.mail.internet.MimeBodyPart)26 DataMap (com.linkedin.data.DataMap)25 RestResponse (com.linkedin.r2.message.rest.RestResponse)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)22 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)22 RestRequest (com.linkedin.r2.message.rest.RestRequest)21 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)21 URI (java.net.URI)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 RequestContext (com.linkedin.r2.message.RequestContext)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)18 Callback (com.linkedin.common.callback.Callback)17 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)14 RestException (com.linkedin.r2.message.rest.RestException)12 HashMap (java.util.HashMap)12 DataList (com.linkedin.data.DataList)11