Search in sources :

Example 16 with JacksonDataCodec

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

the class TestCloudPerformance method run.

@Test
public void run() throws IOException {
    DataCodec[] codecs = { new JacksonDataCodec(), new PsonDataCodec().setOptions(new PsonDataCodec.Options().setEncodeCollectionCount(false).setEncodeStringLength(false)), new PsonDataCodec().setOptions(new PsonDataCodec.Options().setEncodeCollectionCount(false).setEncodeStringLength(true)), new PsonDataCodec().setOptions(new PsonDataCodec.Options().setEncodeCollectionCount(true).setEncodeStringLength(false)), new PsonDataCodec().setOptions(new PsonDataCodec.Options().setEncodeCollectionCount(true).setEncodeStringLength(true)) };
    if (_testOnly == false) {
        out.println("Number of elements " + _numElements);
        out.println("Number of iterations " + _numIterations);
    }
    for (DataCodec codec : codecs) {
        testSerializeDataMap(codec);
        testDeserializeDataMap(codec);
    }
    testEdgeListAddAll();
    testEdgeListEfficientAddAll();
}
Also used : PsonDataCodec(com.linkedin.data.codec.PsonDataCodec) DataCodec(com.linkedin.data.codec.DataCodec) JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) PsonDataCodec(com.linkedin.data.codec.PsonDataCodec) Test(org.testng.annotations.Test)

Example 17 with JacksonDataCodec

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

the class ParSeqRestClientTest method mockClient.

/**
 * @return a mock ParSeqRestClient that gives an error
 */
@SuppressWarnings("deprecation")
private ParSeqRestClient mockClient(final String errKey, final String errValue, final String errMsg, final int httpCode, final int appCode, String code, String docUrl, String requestId, final ProtocolVersion protocolVersion, final String errorResponseHeaderName) {
    final ErrorResponse er = new ErrorResponse();
    final DataMap errMap = new DataMap();
    errMap.put(errKey, errValue);
    er.setErrorDetails(new ErrorDetails(errMap));
    er.setErrorDetailType(EmptyRecord.class.getCanonicalName());
    er.setStatus(httpCode);
    er.setMessage(errMsg);
    er.setServiceErrorCode(appCode);
    er.setCode(code);
    er.setDocUrl(docUrl);
    er.setRequestId(requestId);
    final byte[] mapBytes;
    try {
        mapBytes = new JacksonDataCodec().mapToBytes(er.data());
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    headers.put(errorResponseHeaderName, RestConstants.HEADER_VALUE_ERROR);
    RestClient restClient = new RestClient(new MockClient(httpCode, headers, mapBytes), "http://localhost");
    return new ParSeqRestliClientBuilder().setClient(restClient).setConfig(new ParSeqRestliClientConfigBuilder().build()).build();
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) HashMap(java.util.HashMap) ErrorDetails(com.linkedin.restli.common.ErrorDetails) IOException(java.io.IOException) ErrorResponse(com.linkedin.restli.common.ErrorResponse) DataMap(com.linkedin.data.DataMap)

Example 18 with JacksonDataCodec

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

the class ParSeqRestClientTest method mockClient.

/**
 * @return a mock ParSeqRestClient that returns a TestRecord with the given id.
 */
private ParSeqRestClient mockClient(final long id, final int httpCode, final ProtocolVersion protocolVersion) {
    final TestRecord record = new TestRecord().setId(id);
    byte[] mapBytes;
    try {
        mapBytes = new JacksonDataCodec().mapToBytes(record.data());
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    RestClient restClient = new RestClient(new MockClient(httpCode, headers, mapBytes), "http://localhost");
    return new ParSeqRestliClientBuilder().setClient(restClient).setConfig(new ParSeqRestliClientConfigBuilder().build()).build();
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) HashMap(java.util.HashMap) IOException(java.io.IOException) TestRecord(com.linkedin.restli.client.test.TestRecord)

Example 19 with JacksonDataCodec

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

the class ExporterTestUtils method compareFiles.

/**
 * Compares two JSON files and throws an AssertionError if the files are semantically different. Assumes that the
 * root data object in the file is a map. Used mainly to compare the content of generated IDL and snapshot files.
 *
 * @param actualFileName filename of the generated JSON file.
 * @param expectedFileName filename of the reference JSON file.
 * @throws IOException if file read or file parse fails.
 */
public static void compareFiles(String actualFileName, String expectedFileName) throws IOException {
    String actualContent = ExporterTestUtils.readFile(actualFileName);
    String expectedContent = ExporterTestUtils.readFile(expectedFileName);
    // Compare using a map as opposed to line by line
    final JacksonDataCodec jacksonDataCodec = new JacksonDataCodec();
    final DataMap actualContentMap = jacksonDataCodec.stringToMap(actualContent);
    final DataMap expectedContentMap = jacksonDataCodec.stringToMap(expectedContent);
    if (!actualContentMap.equals(expectedContentMap)) {
        // Ugh... gradle
        PrintStream actualStdout = new PrintStream(new FileOutputStream(FileDescriptor.out));
        actualStdout.println("ERROR " + actualFileName + " does not match " + expectedFileName + " . Printing diff...");
        try {
            // TODO environment dependent, not cross platform
            ProcessBuilder pb = new ProcessBuilder("diff", expectedFileName, actualFileName);
            pb.redirectErrorStream();
            Process p = pb.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                actualStdout.println(line);
            }
        } catch (Exception e) {
            // TODO Setup log4j, find appropriate test harness used in R2D2
            actualStdout.println("Error printing diff: " + e.getMessage());
        }
        Assert.fail(actualFileName + " does not match " + expectedFileName);
    }
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Example 20 with JacksonDataCodec

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

the class TestBackupRequestsClient method createBackupRequestsConfiguration.

@SuppressWarnings("unchecked")
private final Map<String, Object> createBackupRequestsConfiguration(int cost, String operation) throws JsonParseException, JsonMappingException, IOException {
    BackupRequestsConfiguration brc = new BackupRequestsConfiguration();
    BoundedCostBackupRequests bcbr = new BoundedCostBackupRequests();
    bcbr.setCost(cost);
    brc.setOperation(operation);
    brc.setStrategy(BackupRequestsConfiguration.Strategy.create(bcbr));
    String json = new JacksonDataCodec().mapToString(brc.data());
    return JacksonUtil.getObjectMapper().readValue(json, Map.class);
}
Also used : JacksonDataCodec(com.linkedin.data.codec.JacksonDataCodec) BackupRequestsConfiguration(com.linkedin.d2.BackupRequestsConfiguration) BoundedCostBackupRequests(com.linkedin.d2.BoundedCostBackupRequests) ByteString(com.linkedin.data.ByteString)

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