From 7436cffe49a08c25e39be35e67d799546590ecb5 Mon Sep 17 00:00:00 2001 From: ptimson Date: Tue, 14 Feb 2017 21:56:20 +0000 Subject: [PATCH] Added Correct JSON Responses - Put Record - Create Stream - Delete Stream --- .../firehose/request/RequestHandler.java | 21 +++++++++++-- .../CreateDeliveryStreamResponse.java | 24 +++++++++++++++ .../DeleteDeliveryStreamResponse.java | 22 ++++++++++++++ .../timson/firehose/response/PutResponse.java | 30 +++++++++++++++++++ .../io/timson/firehose/util/FirehoseUtil.java | 11 +++++++ .../io/timson/firehose/FirehoseMockIT.java | 6 ++-- .../firehose/request/RequestHandlerTest.java | 22 +++++++++++++- 7 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/timson/firehose/response/CreateDeliveryStreamResponse.java create mode 100644 src/main/java/io/timson/firehose/response/DeleteDeliveryStreamResponse.java create mode 100644 src/main/java/io/timson/firehose/response/PutResponse.java diff --git a/src/main/java/io/timson/firehose/request/RequestHandler.java b/src/main/java/io/timson/firehose/request/RequestHandler.java index 5cb7a3a..d5bc85c 100644 --- a/src/main/java/io/timson/firehose/request/RequestHandler.java +++ b/src/main/java/io/timson/firehose/request/RequestHandler.java @@ -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; @@ -30,13 +33,16 @@ 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); @@ -44,13 +50,16 @@ public void handleCreateStreamRequest(HttpServletRequest request, HttpServletRes } 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); @@ -58,6 +67,8 @@ public void handleDeleteStreamRequest(HttpServletRequest request, HttpServletRes } response.setStatus(HttpServletResponse.SC_OK); + String streamName = deleteDeliveryStreamRequest.getName(); + addBody(response, new DeleteDeliveryStreamResponse(streamName).body()); } private String extractRequestBody(HttpServletRequest request) throws IOException { @@ -65,4 +76,10 @@ private String extractRequestBody(HttpServletRequest request) throws IOException return IOUtils.toString(reader); } + private void addBody(HttpServletResponse response, String body) throws IOException { + response.getWriter().write(body); + response.getWriter().flush(); + response.getWriter().close(); + } + } diff --git a/src/main/java/io/timson/firehose/response/CreateDeliveryStreamResponse.java b/src/main/java/io/timson/firehose/response/CreateDeliveryStreamResponse.java new file mode 100644 index 0000000..91ad0e2 --- /dev/null +++ b/src/main/java/io/timson/firehose/response/CreateDeliveryStreamResponse.java @@ -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); + } + +} diff --git a/src/main/java/io/timson/firehose/response/DeleteDeliveryStreamResponse.java b/src/main/java/io/timson/firehose/response/DeleteDeliveryStreamResponse.java new file mode 100644 index 0000000..dc1d0e1 --- /dev/null +++ b/src/main/java/io/timson/firehose/response/DeleteDeliveryStreamResponse.java @@ -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); + } + +} diff --git a/src/main/java/io/timson/firehose/response/PutResponse.java b/src/main/java/io/timson/firehose/response/PutResponse.java new file mode 100644 index 0000000..daff964 --- /dev/null +++ b/src/main/java/io/timson/firehose/response/PutResponse.java @@ -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; + } + +} diff --git a/src/main/java/io/timson/firehose/util/FirehoseUtil.java b/src/main/java/io/timson/firehose/util/FirehoseUtil.java index a3730ec..e1e04b4 100644 --- a/src/main/java/io/timson/firehose/util/FirehoseUtil.java +++ b/src/main/java/io/timson/firehose/util/FirehoseUtil.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.net.ServerSocket; +import java.util.Random; public class FirehoseUtil { @@ -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(); + } + } diff --git a/src/test-int/java/io/timson/firehose/FirehoseMockIT.java b/src/test-int/java/io/timson/firehose/FirehoseMockIT.java index 97ed78e..3713f17 100644 --- a/src/test-int/java/io/timson/firehose/FirehoseMockIT.java +++ b/src/test-int/java/io/timson/firehose/FirehoseMockIT.java @@ -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; @@ -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); } diff --git a/src/test/java/io/timson/firehose/request/RequestHandlerTest.java b/src/test/java/io/timson/firehose/request/RequestHandlerTest.java index fdb350b..47c25f5 100644 --- a/src/test/java/io/timson/firehose/request/RequestHandlerTest.java +++ b/src/test/java/io/timson/firehose/request/RequestHandlerTest.java @@ -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; @@ -30,6 +36,12 @@ public class RequestHandlerTest { @Mock private HttpServletResponse response; + @Mock + private PrintWriter responseWriter; + + @Captor + private ArgumentCaptor body; + private RequestHandler requestHandler; @Before @@ -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 @@ -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 @@ -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