Search in sources :

Example 16 with ByteString

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

the class TestArrayTemplate method testBytesArray.

@Test
public void testBytesArray() {
    ArrayDataSchema schema = (ArrayDataSchema) DataTemplateUtil.parseSchema("{ \"type\" : \"array\", \"items\" : \"bytes\" }");
    List<ByteString> input = Arrays.asList(ByteString.copyAvroString("1", false), ByteString.copyAvroString("3", false), ByteString.copyAvroString("5", false), ByteString.copyAvroString("7", false), ByteString.copyAvroString("11", false));
    List<ByteString> adds = Arrays.asList(ByteString.copyAvroString("13", false), ByteString.copyAvroString("17", false), ByteString.copyAvroString("19", false));
    List<Object> badInput = asList(true, 99, 999L, 88.0f, 888.0, "\u0100", new StringMap(), new StringArray(), null);
    List<Object> badOutput = asList(true, 99, 999L, 88.0f, 888.0, "\u0100", new DataMap(), new DataList());
    testArray(BytesArray.class, schema, input, adds);
    testArrayBadInput(BytesArray.class, schema, input, badInput, badOutput);
}
Also used : ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) DataList(com.linkedin.data.DataList) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 17 with ByteString

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

the class TestRecordAndUnionTemplate method testBytesField.

@Test
public void testBytesField() {
    List<ByteString> good = Arrays.asList(ByteString.copyAvroString("11", false), ByteString.copyAvroString("22", false), ByteString.copyAvroString("33", false));
    Foo foo = new Foo();
    for (ByteString byteString : good) {
        foo.setBytes(byteString);
        assertEquals(foo.getBytes(), byteString);
        assertTrue(foo.hasBytes());
        assertEquals(foo.toString(), "{bytes=" + byteString + '}');
        Exception exc = null;
        try {
            // clone
            Foo fooClone = foo.clone();
            assertEquals(foo, fooClone);
            fooClone.removeBytes();
            assertTrue(foo.hasBytes());
            assertEquals(foo.getBytes(), byteString);
            assertFalse(fooClone.hasBytes());
            // copy
            Foo fooCopy = foo.copy();
            assertEquals(foo, fooCopy);
            assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
            fooCopy.removeBytes();
            assertTrue(foo.hasBytes());
            assertEquals(foo.getBytes(), byteString);
            assertFalse(fooCopy.hasBytes());
        } catch (CloneNotSupportedException e) {
            exc = e;
        }
        assertTrue(exc == null);
        foo.removeBytes();
        assertFalse(foo.hasBytes());
        assertEquals(foo.toString(), "{}");
    }
    List<?> badInput = asList(false, 33, "\u0100", new DataList());
    DataMap map = new DataMap();
    foo = new Foo(map);
    for (Object bad : badInput) {
        map.put("bytes", bad);
        Exception exc = null;
        try {
            foo.getBytes();
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
    List<?> castFrom = asList("88");
    List<?> castTo = asList(ByteString.copyAvroString("88", false));
    for (int i = 0; i < castFrom.size(); ++i) {
        map.put("bytes", castFrom.get(i));
        ByteString result = foo.getBytes();
        assertEquals(result, castTo.get(i));
    }
    // legacy test
    ByteString[] t = { ByteString.copyAvroString("apple", false), ByteString.copyAvroString("orange", false), ByteString.copyAvroString("banana", false) };
    foo = new Foo();
    foo.set1Bytes(t[0]);
    assertEquals(foo.getBytes(), t[0]);
    foo.set2Bytes(t[1]);
    assertEquals(foo.getBytes(), t[1]);
    foo.set2Bytes(t[2], SetMode.DISALLOW_NULL);
    assertEquals(foo.getBytes(), t[2]);
    foo.set2Bytes(null, SetMode.REMOVE_IF_NULL);
    assertFalse(foo.hasBytes());
}
Also used : DataList(com.linkedin.data.DataList) ByteString(com.linkedin.data.ByteString) TestUtil.asReadOnlyDataMap(com.linkedin.data.TestUtil.asReadOnlyDataMap) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 18 with ByteString

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

the class TestRecordAndUnionTemplate method testFixedField.

@Test
public void testFixedField() {
    List<FixedType> good = Arrays.asList(new FixedType(ByteString.copyAvroString("abcd", false)), new FixedType(ByteString.copyAvroString("1234", false)));
    for (FixedType fixed : good) {
        Exception exc = null;
        try {
            FixedTemplate clone = fixed.clone();
            assertSame(clone.data(), fixed.data());
            assertSame(clone.schema(), fixed.schema());
        } catch (CloneNotSupportedException e) {
            exc = e;
        }
        assert exc == null;
    }
    Foo foo = new Foo();
    for (FixedType fixedType : good) {
        foo.setFixed(fixedType);
        assertEquals(foo.getFixed(), fixedType);
        assertEquals(foo.getFixed().bytes(), fixedType.bytes());
        assertSame(foo.getFixed().bytes(), fixedType.bytes());
        assertTrue(foo.hasFixed());
        assertEquals(foo.toString(), "{fixed=" + fixedType.bytes() + '}');
        Exception exc = null;
        try {
            // clone
            Foo fooClone = foo.clone();
            assertEquals(foo, fooClone);
            assertSame(fooClone.getFixed(), foo.getFixed());
            fooClone.removeFixed();
            assertTrue(foo.hasFixed());
            assertEquals(foo.getFixed(), fixedType);
            assertFalse(fooClone.hasFixed());
            // copy
            Foo fooCopy = foo.copy();
            assertEquals(foo, fooCopy);
            assertTrue(TestUtil.noCommonDataComplex(fooCopy.data(), foo.data()));
            assertNotSame(fooCopy.getFixed(), foo.getFixed());
            fooCopy.removeFixed();
            assertTrue(foo.hasFixed());
            assertEquals(foo.getFixed(), fixedType);
            assertFalse(fooCopy.hasFixed());
        } catch (CloneNotSupportedException e) {
            exc = e;
        }
        assertTrue(exc == null);
        foo.removeFixed();
        assertFalse(foo.hasFixed());
        assertEquals(foo.toString(), "{}");
    }
    List<?> badInput = asList(false, "abc", new DataMap(), ByteString.empty(), ByteString.copyAvroString("abc", false));
    DataMap map = new DataMap();
    foo = new Foo(map);
    for (Object bad : badInput) {
        map.put("fixed", bad);
        Exception exc = null;
        try {
            foo.getFixed();
        } catch (Exception e) {
            exc = e;
        }
        assertTrue(exc != null);
        assertTrue(exc instanceof TemplateOutputCastException);
    }
    List<?> castFrom = asList("8888");
    List<?> castTo = asList(ByteString.copyAvroString("8888", false));
    for (int i = 0; i < castFrom.size(); ++i) {
        map.put("fixed", castFrom.get(i));
        ByteString result = foo.getFixed().bytes();
        assertEquals(result, castTo.get(i));
    }
}
Also used : ByteString(com.linkedin.data.ByteString) TestUtil.asReadOnlyDataMap(com.linkedin.data.TestUtil.asReadOnlyDataMap) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 19 with ByteString

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

the class FullEntityReader method onDone.

public void onDone() {
    ByteString entity = _builder.build();
    _callback.onSuccess(entity);
}
Also used : ByteString(com.linkedin.data.ByteString)

Example 20 with ByteString

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

the class FullEntityObserver method onDone.

@Override
public void onDone() {
    final ByteString entity = _builder.build();
    _callback.onSuccess(entity);
}
Also used : ByteString(com.linkedin.data.ByteString)

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