Search in sources :

Example 1 with JacksonDataCodec

use of com.linkedin.data.codec.JacksonDataCodec in project rest.li by linkedin.

the class TestData method testJacksonDataCodec.

@Test
public void testJacksonDataCodec() throws IOException {
    JacksonDataCodec codec = new JacksonDataCodec();
    testDataCodec(codec, referenceDataMap1);
    DataList list1 = codec.bytesToList("[7,27,279]".getBytes());
    assertEquals(list1, new DataList(Arrays.asList(7, 27, 279)));
    DataList list2 = new DataList(Arrays.asList(321, 21, 1));
    assertEquals(codec.listToBytes(list2), "[321,21,1]".getBytes());
    DataMap map3 = getMapFromJson(codec, "{ \"a\" : null }");
    // out.println(map3.getError());
    assertSame(map3.get("a"), Data.NULL);
    DataMap map4 = getMapFromJson(codec, "{ \"b\" : 123456789012345678901234567890 }");
    // out.println(map4.getError());
    assertTrue(map4.getError().indexOf(" value: 123456789012345678901234567890, token: VALUE_NUMBER_INT, number type: BIG_INTEGER not parsed.") != -1);
    DataMap map5 = getMapFromJson(codec, "{ \"a\" : null, \"b\" : 123456789012345678901234567890 }");
    // out.println(map5.getError());
    assertTrue(map5.getError().indexOf(" value: 123456789012345678901234567890, token: VALUE_NUMBER_INT, number type: BIG_INTEGER not parsed.") != -1);
    // Test comments
    codec.setAllowComments(true);
    DataMap map6 = getMapFromJson(codec, "/* abc */ { \"a\" : \"b\" }");
    assertEquals(map6.get("a"), "b");
    // Test getStringEncoding
    String encoding = codec.getStringEncoding();
    assertEquals(encoding, "UTF-8");
    assertEquals(encoding, JsonEncoding.UTF8.getJavaName());
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with JacksonDataCodec

use of com.linkedin.data.codec.JacksonDataCodec in project rest.li by linkedin.

the class TestData method testJacksonDataCodecErrorEmptyInput.

@Test(expectedExceptions = IOException.class)
public void testJacksonDataCodecErrorEmptyInput() throws IOException {
    final JacksonDataCodec codec = new JacksonDataCodec();
    final ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
    codec.readMap(in);
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with JacksonDataCodec

use of com.linkedin.data.codec.JacksonDataCodec in project rest.li by linkedin.

the class TestData method testJacksonDataCodecErrorToMap.

@Test(expectedExceptions = DataDecodingException.class)
public void testJacksonDataCodecErrorToMap() throws IOException {
    final JacksonDataCodec codec = new JacksonDataCodec();
    codec.bytesToMap("[1, 2, 3]".getBytes());
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with JacksonDataCodec

use of com.linkedin.data.codec.JacksonDataCodec in project rest.li by linkedin.

the class TestBackupRequestsStrategyFactory method testBoundedCostBackupRequestsWithDefaultsDeser.

@Test
public void testBoundedCostBackupRequestsWithDefaultsDeser() throws IOException {
    BackupRequestsConfiguration brc = new BackupRequestsConfiguration();
    BoundedCostBackupRequests bcbr = new BoundedCostBackupRequests();
    bcbr.setCost(3);
    brc.setOperation("BATCH_GET");
    brc.setStrategy(BackupRequestsConfiguration.Strategy.create(bcbr));
    String json = new JacksonDataCodec().mapToString(brc.data());
    @SuppressWarnings("unchecked") Map<String, Object> map = JacksonUtil.getObjectMapper().readValue(json, Map.class);
    BackupRequestsStrategy strategy = BackupRequestsStrategyFactory.tryCreate(map);
    assertNotNull(strategy);
    assertTrue(strategy instanceof BoundedCostBackupRequestsStrategy);
    BoundedCostBackupRequestsStrategy boundedCostStrategy = (BoundedCostBackupRequestsStrategy) strategy;
    assertEquals(boundedCostStrategy.getHistoryLength(), (int) bcbr.getHistoryLength());
    assertEquals(boundedCostStrategy.getMinBackupDelayNano(), (long) bcbr.getMinBackupDelayMs() * 1000L * 1000L);
    assertEquals(boundedCostStrategy.getRequiredHistory(), (int) bcbr.getRequiredHistoryLength());
    assertEquals(boundedCostStrategy.getPercent(), (double) bcbr.getCost());
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) BackupRequestsConfiguration(com.linkedin.d2.BackupRequestsConfiguration) BoundedCostBackupRequests(com.linkedin.d2.BoundedCostBackupRequests) Test(org.testng.annotations.Test)

Example 5 with JacksonDataCodec

use of com.linkedin.data.codec.JacksonDataCodec in project rest.li by linkedin.

the class TestBackupRequestsStrategyFactory method testBoundedCostBackupRequestsDeser.

@Test
public void testBoundedCostBackupRequestsDeser() throws IOException {
    BackupRequestsConfiguration brc = new BackupRequestsConfiguration();
    BoundedCostBackupRequests bcbr = new BoundedCostBackupRequests();
    bcbr.setCost(3);
    bcbr.setHistoryLength(4096);
    bcbr.setMaxBurst(16);
    bcbr.setMinBackupDelayMs(5);
    bcbr.setRequiredHistoryLength(65536);
    brc.setOperation("BATCH_GET");
    brc.setStrategy(BackupRequestsConfiguration.Strategy.create(bcbr));
    String json = new JacksonDataCodec().mapToString(brc.data());
    @SuppressWarnings("unchecked") Map<String, Object> map = JacksonUtil.getObjectMapper().readValue(json, Map.class);
    BackupRequestsStrategy strategy = BackupRequestsStrategyFactory.tryCreate(map);
    assertNotNull(strategy);
    assertTrue(strategy instanceof BoundedCostBackupRequestsStrategy);
    BoundedCostBackupRequestsStrategy boundedCostStrategy = (BoundedCostBackupRequestsStrategy) strategy;
    assertEquals(boundedCostStrategy.getHistoryLength(), (int) bcbr.getHistoryLength());
    assertEquals(boundedCostStrategy.getMinBackupDelayNano(), (long) bcbr.getMinBackupDelayMs() * 1000L * 1000L);
    assertEquals(boundedCostStrategy.getRequiredHistory(), (int) bcbr.getRequiredHistoryLength());
    assertEquals(boundedCostStrategy.getPercent(), (double) bcbr.getCost());
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) BackupRequestsConfiguration(com.linkedin.d2.BackupRequestsConfiguration) BoundedCostBackupRequests(com.linkedin.d2.BoundedCostBackupRequests) Test(org.testng.annotations.Test)

Aggregations

JacksonDataCodec (com.linkedin.data.codec.JacksonDataCodec)27 Test (org.testng.annotations.Test)16 IOException (java.io.IOException)7 DataMap (com.linkedin.data.DataMap)6 BeforeTest (org.testng.annotations.BeforeTest)5 BackupRequestsConfiguration (com.linkedin.d2.BackupRequestsConfiguration)3 BoundedCostBackupRequests (com.linkedin.d2.BoundedCostBackupRequests)3 DataCodec (com.linkedin.data.codec.DataCodec)3 ByteString (com.linkedin.data.ByteString)2 BsonDataCodec (com.linkedin.data.codec.BsonDataCodec)2 PsonDataCodec (com.linkedin.data.codec.PsonDataCodec)2 EmptyRecord (com.linkedin.restli.common.EmptyRecord)2 BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStreamReader (java.io.InputStreamReader)2 PrintStream (java.io.PrintStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)1