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);
}
}
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);
}
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");
}
}));
}
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);
}
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);
}
Aggregations