Search in sources :

Example 46 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project databus by linkedin.

the class TestRelayCommandsLocal method testSources1Command.

@Test
public void testSources1Command() throws Exception {
    LOG.debug("\n\nstarting testSources1Command()\n");
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/sources");
    SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
    SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
    assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
    ByteArrayInputStream in = new ByteArrayInputStream(respHandler.getReceivedBytes());
    ObjectMapper objMapper = new ObjectMapper();
    List<IdNamePair> res = objMapper.readValue(in, new TypeReference<List<IdNamePair>>() {
    });
    assertNotNull("no result", res);
    if (LOG.isDebugEnabled()) {
        LOG.debug("/sources response:" + new String(respHandler.getReceivedBytes()));
    }
    HashSet<IdNamePair> origSet = new HashSet<IdNamePair>(_staticConfig.getSourceIds());
    HashSet<IdNamePair> resSet = new HashSet<IdNamePair>(res);
    Assert.assertEquals(origSet, resSet);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SimpleHttpResponseHandler(com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SimpleTestHttpClient(com.linkedin.databus.core.test.netty.SimpleTestHttpClient) List(java.util.List) IdNamePair(com.linkedin.databus.core.util.IdNamePair) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 47 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project databus by linkedin.

the class TestRelayCommandsLocal method testRegisterV4CommandLessThanHappyPaths.

@Test
public void testRegisterV4CommandLessThanHappyPaths() throws Exception {
    LOG.debug("\n\nstarting testRegisterV4CommandLessThanHappyPaths()\n");
    // protocolVersion < 2
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=1");
    SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
    SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
    assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
    HttpResponse respObj = respHandler.getResponse();
    assertNotNull("/register failed to return expected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    LOG.debug("DATABUS_ERROR_CLASS_HEADER = " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    // protocolVersion > 4
    httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=5");
    respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
    assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
    respObj = respHandler.getResponse();
    assertNotNull("/register failed to return expected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    LOG.debug("DATABUS_ERROR_CLASS_HEADER = " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    // protocolVersion == 2:  this is a happy path, but explicitly specifying the version is
    // unusual in this case (default = version 2 or 3, which are identical for /register), so
    // check for expected response
    httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=2");
    respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
    assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
    respObj = respHandler.getResponse();
    assertNull("/register v2 returned unexpected error: " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER), respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    LOG.debug("DATABUS_ERROR_CLASS_HEADER = " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    String registerResponseProtocolVersionStr = respObj.getHeader(DatabusHttpHeaders.DBUS_CLIENT_RELAY_PROTOCOL_VERSION_HDR);
    assertNotNull("/register protocol-version response header not present", registerResponseProtocolVersionStr);
    assertEquals("client-relay protocol response version mismatch", "2", registerResponseProtocolVersionStr);
    byte[] respBytes = respHandler.getReceivedBytes();
    if (LOG.isDebugEnabled()) {
        LOG.debug("/register response: " + new String(respBytes));
    }
    ByteArrayInputStream in = new ByteArrayInputStream(respBytes);
    ObjectMapper objMapper = new ObjectMapper();
    List<RegisterResponseEntry> sourceSchemasList = null;
    try {
        sourceSchemasList = objMapper.readValue(in, new TypeReference<List<RegisterResponseEntry>>() {
        });
    } catch (JsonMappingException jmex) {
        Assert.fail("ObjectMapper failed unexpectedly");
    }
    assertNotNull("missing source schemas in response", sourceSchemasList);
    assertEquals("expected one source schema", 1, sourceSchemasList.size());
    RegisterResponseEntry rre = sourceSchemasList.get(0);
    assertEquals("unexpected source id", 4002, rre.getId());
    Schema resSchema = Schema.parse(rre.getSchema());
    assertEquals("unexpected source-schema name for source id 4002", "test4.source2_v1", resSchema.getFullName());
    // protocolVersion == 3:  as with v2 above; just do a quick sanity check
    httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=3");
    respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
    assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
    respObj = respHandler.getResponse();
    assertNull("/register v3 returned unexpected error: " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER), respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    LOG.debug("DATABUS_ERROR_CLASS_HEADER = " + respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    registerResponseProtocolVersionStr = respObj.getHeader(DatabusHttpHeaders.DBUS_CLIENT_RELAY_PROTOCOL_VERSION_HDR);
    assertNotNull("/register protocol-version response header not present", registerResponseProtocolVersionStr);
    assertEquals("client-relay protocol response version mismatch", "3", registerResponseProtocolVersionStr);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SimpleHttpResponseHandler(com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler) Schema(org.apache.avro.Schema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) RegisterResponseEntry(com.linkedin.databus2.core.container.request.RegisterResponseEntry) SimpleTestHttpClient(com.linkedin.databus.core.test.netty.SimpleTestHttpClient) TypeReference(org.codehaus.jackson.type.TypeReference) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Example 48 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project databus by linkedin.

the class TestRelayCommandsLocal method doTestOneDataStreamCommand.

private void doTestOneDataStreamCommand() throws Exception {
    //try to read it
    Checkpoint cp = Checkpoint.createFlexibleCheckpoint();
    String streamRequest = "/stream?sources=100&size=100000&output=json&checkPoint=" + cp.toString();
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, streamRequest);
    SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
    SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
    assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
    HttpResponse respObj = respHandler.getResponse();
    assertNull("/stream returned unexpected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    if (LOG.isDebugEnabled()) {
        LOG.debug("/stream response:" + new String(respHandler.getReceivedBytes()));
    }
    ObjectMapper objMapper = new ObjectMapper();
    ByteArrayInputStream in = new ByteArrayInputStream(respHandler.getReceivedBytes());
    objMapper.readValue(in, new TypeReference<Map<String, String>>() {
    });
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Checkpoint(com.linkedin.databus.core.Checkpoint) SimpleHttpResponseHandler(com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) SimpleTestHttpClient(com.linkedin.databus.core.test.netty.SimpleTestHttpClient) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 49 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project pinot by linkedin.

the class IndexingConfigTest method testIgnoreUnknown.

@Test
public void testIgnoreUnknown() throws JSONException, IOException {
    JSONObject json = new JSONObject();
    json.put("invertedIndexColumns", Arrays.asList("a", "b", "c"));
    json.put("sortedColumn", Arrays.asList("d", "e", "f"));
    json.put("loadMode", "MMAP");
    json.put("keyThatIsUnknown", "randomValue");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(json.toString());
    IndexingConfig indexingConfig = mapper.readValue(jsonNode, IndexingConfig.class);
    Assert.assertEquals("MMAP", indexingConfig.getLoadMode());
    List<String> invertedIndexColumns = indexingConfig.getInvertedIndexColumns();
    Assert.assertEquals(3, invertedIndexColumns.size());
    Assert.assertEquals("a", invertedIndexColumns.get(0));
    Assert.assertEquals("b", invertedIndexColumns.get(1));
    Assert.assertEquals("c", invertedIndexColumns.get(2));
    List<String> sortedIndexColumns = indexingConfig.getSortedColumn();
    Assert.assertEquals(3, sortedIndexColumns.size());
    Assert.assertEquals("d", sortedIndexColumns.get(0));
    Assert.assertEquals("e", sortedIndexColumns.get(1));
    Assert.assertEquals("f", sortedIndexColumns.get(2));
}
Also used : JSONObject(org.json.JSONObject) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Example 50 with ObjectMapper

use of org.codehaus.jackson.map.ObjectMapper in project pinot by linkedin.

the class TenantTest method testDeserializeFromJson.

@Test
public void testDeserializeFromJson() throws JSONException, IOException {
    JSONObject json = new JSONObject();
    json.put("tenantRole", "SERVER");
    json.put("tenantName", "newTenant");
    json.put("numberOfInstances", 10);
    json.put("offlineInstances", 5);
    json.put("realtimeInstances", 5);
    json.put("keyIDontKnow", "blahblahblah");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readTree(json.toString());
    Tenant tenant = mapper.readValue(jsonNode, Tenant.class);
    Assert.assertEquals(5, tenant.getOfflineInstances());
    Assert.assertEquals(10, tenant.getNumberOfInstances());
    Assert.assertEquals("newTenant", tenant.getTenantName());
    Assert.assertEquals(TenantRole.SERVER, tenant.getTenantRole());
}
Also used : JSONObject(org.json.JSONObject) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Aggregations

ObjectMapper (org.codehaus.jackson.map.ObjectMapper)386 IOException (java.io.IOException)85 Test (org.junit.Test)61 JsonNode (org.codehaus.jackson.JsonNode)50 HashMap (java.util.HashMap)46 ArrayList (java.util.ArrayList)45 Test (org.testng.annotations.Test)37 Map (java.util.Map)34 List (java.util.List)26 File (java.io.File)22 StringWriter (java.io.StringWriter)22 JSONObject (org.json.JSONObject)18 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)17 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 Version (org.codehaus.jackson.Version)14 JsonFactory (org.codehaus.jackson.JsonFactory)13 JSONObject (org.codehaus.jettison.json.JSONObject)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 JsonGenerator (org.codehaus.jackson.JsonGenerator)11