use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.
the class DefaultStreamManager method send.
@Override
public void send(File file, String contentType) throws Exception {
String path = String.format("/v3/namespaces/%s/streams/%s/batch", streamId.getNamespaceId(), streamId.getId());
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
request.setHeader(HttpHeaders.Names.CONTENT_TYPE, contentType);
final MockResponder responder = new MockResponder();
final BodyConsumer bodyConsumer = streamHandler.batch(request, responder, streamId.getNamespaceId(), streamId.getId());
Preconditions.checkNotNull(bodyConsumer, "BodyConsumer from stream batch load call should not be null");
ByteStreams.readBytes(Files.newInputStreamSupplier(file), new ByteProcessor<BodyConsumer>() {
@Override
public boolean processBytes(byte[] buf, int off, int len) throws IOException {
bodyConsumer.chunk(ChannelBuffers.wrappedBuffer(buf, off, len), responder);
return true;
}
@Override
public BodyConsumer getResult() {
bodyConsumer.finished(responder);
return bodyConsumer;
}
});
Preconditions.checkState(HttpResponseStatus.OK.equals(responder.getStatus()), "Failed to load events to stream %s in batch", streamId);
}
use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.
the class MapReduceRunnerTestBase method writeToStream.
protected void writeToStream(Id.Stream streamId, String body) throws IOException {
String path = String.format("/v3/namespaces/%s/streams/%s", streamId.getNamespaceId(), streamId.getId());
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
ChannelBuffer content = ChannelBuffers.wrappedBuffer(ByteBuffer.wrap(Bytes.toBytes(body)));
httpRequest.setContent(content);
httpRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
MockResponder responder = new MockResponder();
try {
streamHandler.enqueue(httpRequest, responder, streamId.getNamespaceId(), streamId.getId());
} catch (Exception e) {
Throwables.propagateIfPossible(e, IOException.class);
throw Throwables.propagate(e);
}
if (responder.getStatus() != HttpResponseStatus.OK) {
throw new IOException("Failed to write to stream. Status = " + responder.getStatus());
}
}
use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.
the class DefaultStreamManager method getEvents.
private List<StreamEvent> getEvents(Id.Stream streamId, String startTime, String endTime, int limit) throws IOException {
long start = TimeMathParser.parseTime(startTime, TimeUnit.MILLISECONDS);
long end = TimeMathParser.parseTime(endTime, TimeUnit.MILLISECONDS);
String path = String.format("/v3/namespaces/%s/streams/%s/events?start=%d&end=%d&limit=%d", streamId.getNamespaceId(), streamId.getId(), start, end, limit);
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
MockResponder responder = new MockResponder();
try {
streamFetchHandler.fetch(httpRequest, responder, streamId.getNamespaceId(), streamId.getId(), startTime, endTime, limit);
} catch (Exception e) {
Throwables.propagateIfPossible(e, IOException.class);
throw Throwables.propagate(e);
}
if (responder.getStatus() != HttpResponseStatus.OK) {
throw new IOException("Failed to read from stream. Status = " + responder.getStatus());
}
return responder.decodeResponseContent(STREAM_EVENT_LIST_TYPE, GSON);
}
use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.
the class DefaultStreamManager method send.
@Override
public void send(Map<String, String> headers, ByteBuffer buffer) throws IOException {
String path = String.format("/v3/namespaces/%s/streams/%s", streamId.getNamespaceId(), streamId.getId());
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpRequest.setHeader(streamId.getId() + "." + entry.getKey(), entry.getValue());
}
ChannelBuffer content = ChannelBuffers.wrappedBuffer(buffer);
httpRequest.setContent(content);
httpRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
MockResponder responder = new MockResponder();
try {
streamHandler.enqueue(httpRequest, responder, streamId.getNamespaceId(), streamId.getId());
} catch (Exception e) {
Throwables.propagateIfPossible(e, IOException.class);
throw Throwables.propagate(e);
}
if (responder.getStatus() != HttpResponseStatus.OK) {
throw new IOException("Failed to write to stream. Status = " + responder.getStatus());
}
}
use of co.cask.cdap.internal.MockResponder in project cdap by caskdata.
the class DefaultStreamManager method createStream.
@Override
public void createStream() throws IOException {
String path = String.format("/v3/namespaces/%s/streams/%s", streamId.getNamespaceId(), streamId.getId());
HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, path);
MockResponder responder = new MockResponder();
try {
streamHandler.create(httpRequest, responder, streamId.getNamespaceId(), streamId.getId());
} catch (Exception e) {
Throwables.propagateIfPossible(e, IOException.class);
throw Throwables.propagate(e);
}
if (responder.getStatus() != HttpResponseStatus.OK) {
throw new IOException("Failed to create stream. Status = " + responder.getStatus());
}
}
Aggregations