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");
}
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>>() {
});
}
Aggregations