Skip to content

Commit

Permalink
Added Correct JSON Responses
Browse files Browse the repository at this point in the history
 - Put Record
 - Create Stream
 - Delete Stream
  • Loading branch information
ptimson committed Feb 15, 2017
1 parent 6eeeb9a commit 7436cff
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/main/java/io/timson/firehose/request/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import io.timson.firehose.response.CreateDeliveryStreamResponse;
import io.timson.firehose.response.DeleteDeliveryStreamResponse;
import io.timson.firehose.response.PutResponse;
import io.timson.firehose.stream.DeliveryStreamService;
import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -30,39 +33,53 @@ public void handlePutRequest(HttpServletRequest request, HttpServletResponse res
}

response.setStatus(HttpServletResponse.SC_OK);
addBody(response, new PutResponse().body());
}

public void handleCreateStreamRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
CreateDeliveryStreamRequest createDeliveryStreamRequest;

try {
String json = extractRequestBody(request);
CreateDeliveryStreamRequest createDeliveryStreamRequest = CreateDeliveryStreamRequest.fromJson(json);
createDeliveryStreamRequest = CreateDeliveryStreamRequest.fromJson(json);

deliveryStreamService.createStream(createDeliveryStreamRequest);
} catch (IllegalArgumentException | JsonParseException | JsonMappingException e) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}

response.setStatus(HttpServletResponse.SC_OK);
String streamName = createDeliveryStreamRequest.getName();
addBody(response, new CreateDeliveryStreamResponse(streamName).body());
}

public void handleDeleteStreamRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
DeleteDeliveryStreamRequest deleteDeliveryStreamRequest;

try {
String json = extractRequestBody(request);
DeleteDeliveryStreamRequest deleteDeliveryStreamRequest = DeleteDeliveryStreamRequest.fromJson(json);
deleteDeliveryStreamRequest = DeleteDeliveryStreamRequest.fromJson(json);
deliveryStreamService.deleteStream(deleteDeliveryStreamRequest.getName());
} catch (IllegalArgumentException | JsonParseException | JsonMappingException e) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}

response.setStatus(HttpServletResponse.SC_OK);
String streamName = deleteDeliveryStreamRequest.getName();
addBody(response, new DeleteDeliveryStreamResponse(streamName).body());
}

private String extractRequestBody(HttpServletRequest request) throws IOException {
final BufferedReader reader = request.getReader();
return IOUtils.toString(reader);
}

private void addBody(HttpServletResponse response, String body) throws IOException {
response.getWriter().write(body);
response.getWriter().flush();
response.getWriter().close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.timson.firehose.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CreateDeliveryStreamResponse {

private static final ObjectMapper mapper = new ObjectMapper();

private static final String ARN_TEMPLATE = "arn:aws:firehose:us-east-1:123456789012:deliverystream/%s";

@JsonProperty("DeliveryStreamARN")
private final String deliveryStreamArn;

public CreateDeliveryStreamResponse(String deliveryStreamName) {
this.deliveryStreamArn = String.format(ARN_TEMPLATE, deliveryStreamName);
}

public String body() throws JsonProcessingException {
return mapper.writeValueAsString(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.timson.firehose.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DeleteDeliveryStreamResponse {

private static final ObjectMapper mapper = new ObjectMapper();

@JsonProperty("DeliveryStreamName")
private final String deliveryStreamName;

public DeleteDeliveryStreamResponse(String deliveryStreamName) {
this.deliveryStreamName = deliveryStreamName;
}

public String body() throws JsonProcessingException {
return mapper.writeValueAsString(this);
}

}
30 changes: 30 additions & 0 deletions src/main/java/io/timson/firehose/response/PutResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.timson.firehose.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.timson.firehose.util.FirehoseUtil;

public class PutResponse {

private static final ObjectMapper mapper = new ObjectMapper();

private static final String recordIdChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/+";
private static final int recordIdLength = 224;

@JsonProperty("RecordId")
private final String recordId;

public PutResponse() {
recordId = FirehoseUtil.randomString(recordIdChars, recordIdLength);
}

public String body() throws JsonProcessingException {
return mapper.writeValueAsString(this);
}

public String getRecordId() {
return recordId;
}

}
11 changes: 11 additions & 0 deletions src/main/java/io/timson/firehose/util/FirehoseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.ServerSocket;
import java.util.Random;

public class FirehoseUtil {

Expand All @@ -18,4 +19,14 @@ public static boolean isEmpty(String str) {
return (str == null || str.equals(""));
}

public static String randomString(String chars, int length) {
StringBuilder sb = new StringBuilder();
Random random = new Random();
while (sb.length() < length) {
int index = (int) (random.nextFloat() * chars.length());
sb.append(chars.charAt(index));
}
return sb.toString();
}

}
6 changes: 4 additions & 2 deletions src/test-int/java/io/timson/firehose/FirehoseMockIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.amazonaws.services.kinesisfirehose.AmazonKinesisFirehose;
import com.amazonaws.services.kinesisfirehose.model.CompressionFormat;
import com.amazonaws.services.kinesisfirehose.model.CreateDeliveryStreamRequest;
import com.amazonaws.services.kinesisfirehose.model.CreateDeliveryStreamResult;
import com.amazonaws.services.kinesisfirehose.model.DeleteDeliveryStreamRequest;
import com.amazonaws.services.kinesisfirehose.model.ExtendedS3DestinationConfiguration;
import com.amazonaws.services.kinesisfirehose.model.PutRecordRequest;
import com.amazonaws.services.kinesisfirehose.model.PutRecordResult;
import io.timson.firehose.aws.AWSFirehoseUtil;
import io.timson.firehose.test.TestUtil;
import org.junit.After;
Expand Down Expand Up @@ -75,10 +77,10 @@ public void shouldWriteToS3WhenReachedBufferSize() throws Exception {
.withS3Prefix("kfh/")
.build();
CreateDeliveryStreamRequest createStreamRequest = AWSFirehoseUtil.createDeliveryStreamRequest(streamName, s3StreamConfig);
firehoseClient.createDeliveryStream(createStreamRequest);
final CreateDeliveryStreamResult deliveryStream = firehoseClient.createDeliveryStream(createStreamRequest);
String data = TestUtil.createStringOfSize(512 * KILOBYTES);
PutRecordRequest putRequest = AWSFirehoseUtil.createPutRequest(streamName, data);
firehoseClient.putRecord(putRequest);
final PutRecordResult putRecordResult = firehoseClient.putRecord(putRequest);
firehoseClient.putRecord(putRequest);
firehoseClient.putRecord(putRequest);
}
Expand Down
22 changes: 21 additions & 1 deletion src/test/java/io/timson/firehose/request/RequestHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

import static com.jcabi.matchers.RegexMatchers.matchesPattern;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.isNull;
Expand All @@ -30,6 +36,12 @@ public class RequestHandlerTest {
@Mock
private HttpServletResponse response;

@Mock
private PrintWriter responseWriter;

@Captor
private ArgumentCaptor<String> body;

private RequestHandler requestHandler;

@Before
Expand All @@ -41,10 +53,13 @@ public void setUp() throws Exception {
public void shouldSetResponseTo200Success_WhenValidPutRequest() throws Exception {
final String JSON = "{\"DeliveryStreamName\":\"myDeliveryStream\",\"Record\":{\"Data\":\"bXlEYXRh\"}}";
when(request.getReader()).thenReturn(TestUtil.createBufferedReader(JSON));
when(response.getWriter()).thenReturn(responseWriter);

requestHandler.handlePutRequest(request, response);

verify(response).setStatus(200);
verify(responseWriter).write(body.capture());
assertThat(body.getValue(), matchesPattern("\\{\"RecordId\":\"[A-Za-z0-9\\/\\+]{224}\"\\}"));
}

@Test
Expand Down Expand Up @@ -76,10 +91,13 @@ public void shouldSetResponseTo200Success_WhenValidCreateStreamRequest() throws
+ "\"CompressionFormat\":\"GZIP\"}}";

when(request.getReader()).thenReturn(TestUtil.createBufferedReader(JSON));
when(response.getWriter()).thenReturn(responseWriter);

requestHandler.handleCreateStreamRequest(request, response);

verify(response).setStatus(200);
verify(responseWriter).write(body.capture());
assertThat(body.getValue(), is("{\"DeliveryStreamARN\":\"arn:aws:firehose:us-east-1:123456789012:deliverystream/myDeliveryStream\"}"));
}

@Test
Expand All @@ -106,12 +124,14 @@ public void shouldSetResponseTo400BadRequest_WhenCreateStreamRequestMissingParam
@Test
public void shouldSetResponseTo200Success_WhenValidDeleteStreamRequest() throws Exception {
final String JSON = "{\"DeliveryStreamName\":\"myDeliveryStream\"}";

when(request.getReader()).thenReturn(TestUtil.createBufferedReader(JSON));
when(response.getWriter()).thenReturn(responseWriter);

requestHandler.handleDeleteStreamRequest(request, response);

verify(response).setStatus(200);
verify(responseWriter).write(body.capture());
assertThat(body.getValue(), is("{\"DeliveryStreamName\":\"myDeliveryStream\"}"));
}

@Test
Expand Down

0 comments on commit 7436cff

Please sign in to comment.