### Question:
JSONRecordAdapter { public JSONObject translateFromRecord(PutRecordRequest source) { if (null == source) { LOGGER.warn("The Record provided was null"); return null; } if (source.getData() == null || source.getPartitionKey() == null || source.getPartitionKey().isEmpty() || source.getStreamName() == null || source.getStreamName().isEmpty()) { throw new AmazonClientException( "RecordRequests must specify a partition key, stream name, and data"); } if (!source.getData().hasArray()) { throw new AmazonClientException("ByteBuffer must be based on array for proper storage"); } final JSONObject recordJson = new JSONObject(); try { recordJson.put(DATA_FIELD_KEY, Base64.encodeToString(source.getData().array(), Base64.DEFAULT)); recordJson.put(STREAM_NAME_FIELD, source.getStreamName()); recordJson.put(PARTITION_KEY_FIELD, source.getPartitionKey()); recordJson.putOpt(EXPLICIT_HASH_FIELD, source.getExplicitHashKey()); recordJson.putOpt(SEQUENCE_NUMBER_FIELD, source.getSequenceNumberForOrdering()); } catch (final JSONException e) { throw new AmazonClientException("Unable to convert KinesisRecord to JSON " + e.getMessage()); } return recordJson; }  static ByteBuffer getData(JSONObject jsonObject); static String getPartitionKey(JSONObject jsonObject); static String getStreamName(JSONObject jsonObject); JSONObject translateFromRecord(PutRecordRequest source);  }### Answer:
@Test public void convertRequestToJSONAllElementsIntact() { PutRecordRequest putRequest = new PutRecordRequest(); putRequest.withStreamName(stream).withPartitionKey(partitionKey) .withExplicitHashKey(explicitHashKey).withData(data) .withSequenceNumberForOrdering(sequenceNumberForOrdering); JSONRecordAdapter adapter = new JSONRecordAdapter(); JSONObject json = adapter.translateFromRecord(putRequest); try { assertTrue(json.getString(JSONRecordAdapter.STREAM_NAME_FIELD).equalsIgnoreCase(stream)); assertTrue(json.getString(JSONRecordAdapter.DATA_FIELD_KEY).equalsIgnoreCase( Base64.encodeToString("TestData".getBytes(StringUtils.UTF8), Base64.DEFAULT))); assertTrue(json.getString(JSONRecordAdapter.PARTITION_KEY_FIELD).equalsIgnoreCase( partitionKey)); assertTrue(json.getString(JSONRecordAdapter.SEQUENCE_NUMBER_FIELD).equalsIgnoreCase( sequenceNumberForOrdering)); assertTrue(json.getString(JSONRecordAdapter.EXPLICIT_HASH_FIELD).equalsIgnoreCase( explicitHashKey)); } catch (JSONException e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void nullRequestReturnsNull() { JSONRecordAdapter adapter = new JSONRecordAdapter(); assertNull(adapter.translateFromRecord(null)); }
@Test(expected = AmazonClientException.class) public void convertRequestWhenStreamNull() { PutRecordRequest putRequest = new PutRecordRequest(); putRequest.withPartitionKey(partitionKey).withData(data); JSONRecordAdapter adapter = new JSONRecordAdapter(); adapter.translateFromRecord(putRequest); }
@Test(expected = AmazonClientException.class) public void convertRequestWhenStreamEmpty() { PutRecordRequest putRequest = new PutRecordRequest(); String streamName = ""; putRequest.withStreamName(streamName).withPartitionKey(partitionKey).withData(data); JSONRecordAdapter adapter = new JSONRecordAdapter(); adapter.translateFromRecord(putRequest); }
@Test(expected = AmazonClientException.class) public void convertRequestWhenDataNull() { PutRecordRequest putRequest = new PutRecordRequest(); putRequest.withStreamName(stream).withPartitionKey(partitionKey); JSONRecordAdapter adapter = new JSONRecordAdapter(); adapter.translateFromRecord(putRequest); }
@Test(expected = AmazonClientException.class) public void convertRequestWhenPartitionKeyNull() { PutRecordRequest putRequest = new PutRecordRequest(); putRequest.withStreamName(stream).withData(data); JSONRecordAdapter adapter = new JSONRecordAdapter(); adapter.translateFromRecord(putRequest); }
@Test(expected = AmazonClientException.class) public void convertRequestWhenPartitionKeyEmpty() { PutRecordRequest putRequest = new PutRecordRequest(); String partitionKey = ""; putRequest.withStreamName(stream).withData(data).withPartitionKey(partitionKey); JSONRecordAdapter adapter = new JSONRecordAdapter(); adapter.translateFromRecord(putRequest); }