Search in sources :

Example 11 with SimpleTestHttpClient

use of com.linkedin.databus.core.test.netty.SimpleTestHttpClient in project databus by linkedin.

the class TestRelayCommandsLocal method testRegisterV4CommandHappyPath.

@Test
public void testRegisterV4CommandHappyPath() throws Exception {
    LOG.debug("\n\nstarting testRegisterV4CommandHappyPath()\n");
    // /register?protocolVersion=4
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/register?sources=4002&" + DatabusHttpHeaders.PROTOCOL_VERSION_PARAM + "=4");
    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("/register v4 returned unexpected error: " + respObj.getHeader(DatabusHttpHeaders.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", "4", registerResponseProtocolVersionStr);
    byte[] respBytes = respHandler.getReceivedBytes();
    if (LOG.isDebugEnabled()) {
        LOG.debug("/register response: " + new String(respBytes));
    }
    ByteArrayInputStream in = new ByteArrayInputStream(respBytes);
    ObjectMapper objMapper = new ObjectMapper();
    Map<String, List<Object>> resMap = objMapper.readValue(in, new TypeReference<Map<String, List<Object>>>() {
    });
    assertNotNull("no result", resMap);
    List<Object> sourceSchemasObjectsList = resMap.get(RegisterResponseEntry.SOURCE_SCHEMAS_KEY);
    assertNotNull("missing required sourceSchemas key in response", sourceSchemasObjectsList);
    assertEquals("expected one source schema", 1, sourceSchemasObjectsList.size());
    // ObjectMapper encodes plain Object as LinkedHashMap with (apparently) String keys and
    // either String or Integer values (currently).  We must construct RegisterResponseEntry
    // manually.
    assertTrue("sourceSchemas deserialization error: 'Object' type = " + sourceSchemasObjectsList.get(0).getClass().getName() + ", not LinkedHashMap", sourceSchemasObjectsList.get(0) instanceof LinkedHashMap);
    // just obj
    @SuppressWarnings("unchecked") LinkedHashMap<String, Object> obj = (LinkedHashMap<String, Object>) sourceSchemasObjectsList.get(0);
    assertTrue("sourceSchemas deserialization error: missing \"id\" key", obj.containsKey("id"));
    assertTrue("sourceSchemas deserialization error: missing \"version\" key", obj.containsKey("version"));
    assertTrue("sourceSchemas deserialization error: missing \"schema\" key", obj.containsKey("schema"));
    assertTrue("obj.get(\"id\") type = " + obj.get("id").getClass().getName() + ", not Integer", obj.get("id") instanceof Integer);
    assertTrue("obj.get(\"version\") type = " + obj.get("version").getClass().getName() + ", not Integer", obj.get("version") instanceof Integer);
    RegisterResponseEntry rre = new RegisterResponseEntry((Integer) obj.get("id"), ((Integer) obj.get("version")).shortValue(), (String) obj.get("schema"));
    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());
    // There's no guarantee of a metadataSchemas entry in general, but we pre-stuffed our
    // VersionedSchemaSetBackedRegistryService with one in the test's constructor, so we
    // expect the relay to hand it back to us.  Or else.
    /* disabled for now since simplistic relay implementation has been disabled; reenable/update/modify as part of DDSDBUS-2093/2096 (TODO)
    List<Object> metadataSchemasObjectsList = resMap.get(RegisterResponseMetadataEntry.METADATA_SCHEMAS_KEY);
    assertNotNull("missing expected metadataSchemas key in response", metadataSchemasObjectsList);
    assertEquals("expected one metadata schema", 1, metadataSchemasObjectsList.size());

    // As above, we must construct RegisterResponseMetadataEntry manually.
    assertTrue("metadataSchemas deserialization error: 'Object' type = " +
               metadataSchemasObjectsList.get(0).getClass().getName() + ", not LinkedHashMap",
               metadataSchemasObjectsList.get(0) instanceof LinkedHashMap);
    @SuppressWarnings("unchecked") // just obj2
    LinkedHashMap<String, Object> obj2 = (LinkedHashMap<String, Object>)metadataSchemasObjectsList.get(0);
    assertTrue("metadataSchemas deserialization error: missing \"version\" key", obj2.containsKey("version"));
    assertTrue("metadataSchemas deserialization error: missing \"schema\" key", obj2.containsKey("schema"));

    assertTrue("obj2.get(\"version\") type = " + obj2.get("version").getClass().getName() + ", not Integer",
               obj2.get("version") instanceof Integer);
    RegisterResponseMetadataEntry rrme = new RegisterResponseMetadataEntry(((Integer)obj2.get("version")).shortValue(),
                                                                          (String)obj2.get("schema"));
    assertEquals("unexpected metadata version", 1, rrme.getVersion());
    resSchema = Schema.parse(rrme.getSchema());
    assertEquals("unexpected metadata schema name", "test_namespace.metadata", resSchema.getFullName());
 */
    LOG.debug("\n\ndone with testRegisterV4CommandHappyPath()\n");
}
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) LinkedHashMap(java.util.LinkedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) RegisterResponseEntry(com.linkedin.databus2.core.container.request.RegisterResponseEntry) SimpleTestHttpClient(com.linkedin.databus.core.test.netty.SimpleTestHttpClient) List(java.util.List) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Example 12 with SimpleTestHttpClient

use of com.linkedin.databus.core.test.netty.SimpleTestHttpClient in project databus by linkedin.

the class TestRelayCommandsLocal method doTestOneDataClientVerStreamCommand.

/** Validates the version checks in the stream calls. */
private void doTestOneDataClientVerStreamCommand(int ver, boolean expectFail) throws Exception {
    //try to read it
    Checkpoint cp = Checkpoint.createFlexibleCheckpoint();
    String maxev = "&" + DatabusHttpHeaders.MAX_EVENT_VERSION + "=" + ver;
    // protocol version 2 (versions >= 3 use "subs=")
    String streamRequest = "/stream?sources=100&size=100000&output=json&checkPoint=" + cp.toString() + maxev;
    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();
    if (expectFail) {
        assertNotNull("/stream failed to return expected error", respObj.getHeader(DatabusHttpHeaders.DATABUS_ERROR_CLASS_HEADER));
    } else {
        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)

Aggregations

SimpleHttpResponseHandler (com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler)12 SimpleTestHttpClient (com.linkedin.databus.core.test.netty.SimpleTestHttpClient)12 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)12 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)10 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)7 Test (org.testng.annotations.Test)7 List (java.util.List)6 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)4 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 Schema (org.apache.avro.Schema)4 IdNamePair (com.linkedin.databus.core.util.IdNamePair)3 HashSet (java.util.HashSet)3 Checkpoint (com.linkedin.databus.core.Checkpoint)2 InvalidRequestParamValueException (com.linkedin.databus2.core.container.request.InvalidRequestParamValueException)1 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)1 TypeReference (org.codehaus.jackson.type.TypeReference)1