use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method doNullInputsForFunctionsTest.
// nullInputsForFunctionsTest() helpers
/**
* Checks for reaction to null input in {@code methodName} in {@link FrontendRestRequestService}.
* @param methodName the name of the method to invoke.
* @throws Exception
*/
private void doNullInputsForFunctionsTest(String methodName) throws Exception {
Method method = FrontendRestRequestService.class.getDeclaredMethod(methodName, RestRequest.class, RestResponseChannel.class);
RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
responseHandler.reset();
try {
method.invoke(frontendRestRequestService, null, restResponseChannel);
fail("Method [" + methodName + "] should have failed because RestRequest is null");
} catch (InvocationTargetException e) {
assertEquals("Unexpected exception class", IllegalArgumentException.class, e.getTargetException().getClass());
}
responseHandler.reset();
try {
method.invoke(frontendRestRequestService, restRequest, null);
fail("Method [" + methodName + "] should have failed because RestResponseChannel is null");
} catch (InvocationTargetException e) {
assertEquals("Unexpected exception class", IllegalArgumentException.class, e.getTargetException().getClass());
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method postBlobAndVerify.
/**
* Posts a blob with the given {@code headers} and {@code content}.
* @param headers the headers of the new blob that get converted to blob properties.
* @param content the content of the blob.
* @param expectedAccount the expected {@link Account} instance injected into the {@link RestRequest}.
* @param expectedContainer the expected {@link Container} instance injected into the {@link RestRequest}.
* @return the blob ID of the blob.
* @throws Exception
*/
private String postBlobAndVerify(JSONObject headers, ByteBuffer content, Account expectedAccount, Container expectedContainer) throws Exception {
List<ByteBuffer> contents = new LinkedList<>();
contents.add(content);
contents.add(null);
RestRequest restRequest = createRestRequest(RestMethod.POST, "/", headers, contents);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
return verifyPostAndReturnBlobId(restRequest, restResponseChannel, expectedAccount, expectedContainer);
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class AmbrySecurityServiceTest method testHeadBlob.
/**
* Tests {@link AmbrySecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for
* {@link RestMethod#HEAD}.
* @param blobInfo the {@link BlobInfo} of the blob for which {@link RestMethod#HEAD} is required.
* @param range the {@link ByteRange} used for a range request, or {@code null} for non-ranged requests.
* @throws Exception
*/
private void testHeadBlob(BlobInfo blobInfo, ByteRange range) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
JSONObject headers = range != null ? new JSONObject().put(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range)) : null;
RestRequest restRequest = createRestRequest(RestMethod.HEAD, "/", headers);
Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
Assert.assertEquals("ProcessResponse status should have been set", range == null ? ResponseStatus.Ok : ResponseStatus.PartialContent, restResponseChannel.getStatus());
verifyHeadersForHead(blobInfo.getBlobProperties(), range, restResponseChannel);
verifyAccountAndContainerHeaders(restResponseChannel, accountAndContainer.getFirst(), accountAndContainer.getSecond());
Assert.assertEquals("LifeVersion mismatch", Short.toString(blobInfo.getLifeVersion()), restResponseChannel.getHeader(RestUtils.Headers.LIFE_VERSION));
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class AmbrySecurityServiceTest method testGetSubResource.
/**
* Tests GET of sub-resources.
* @param subResource the {@link RestUtils.SubResource} to test.
* @throws Exception
*/
private void testGetSubResource(BlobInfo blobInfo, RestUtils.SubResource subResource) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
RestRequest restRequest = createRestRequest(RestMethod.GET, "/sampleId/" + subResource, null);
Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
Assert.assertEquals("ProcessResponse status should have been set ", ResponseStatus.Ok, restResponseChannel.getStatus());
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
Assert.assertEquals("Last Modified does not match creation time", RestUtils.toSecondsPrecisionInMs(blobInfo.getBlobProperties().getCreationTimeInMs()), RestUtils.getTimeFromDateString(restResponseChannel.getHeader(RestUtils.Headers.LAST_MODIFIED)).longValue());
if (subResource.equals(RestUtils.SubResource.BlobInfo)) {
verifyBlobPropertiesHeaders(blobInfo.getBlobProperties(), restResponseChannel);
verifyAccountAndContainerHeaders(restResponseChannel, accountAndContainer.getFirst(), accountAndContainer.getSecond());
Assert.assertEquals("LifeVersion mismatch", Short.toString(blobInfo.getLifeVersion()), restResponseChannel.getHeader(RestUtils.Headers.LIFE_VERSION));
} else {
verifyAbsenceOfHeaders(restResponseChannel, RestUtils.Headers.PRIVATE, RestUtils.Headers.TTL, RestUtils.Headers.SERVICE_ID, RestUtils.Headers.OWNER_ID, RestUtils.Headers.AMBRY_CONTENT_TYPE, RestUtils.Headers.CREATION_TIME, RestUtils.Headers.BLOB_SIZE, RestUtils.Headers.ACCEPT_RANGES, RestUtils.Headers.CONTENT_RANGE, RestUtils.Headers.LIFE_VERSION);
}
Map<String, String> userMetadata = blobInfo.getUserMetadata() != null ? RestUtils.buildUserMetadata(blobInfo.getUserMetadata()) : null;
if (userMetadata == null && !(blobInfo.getUserMetadata().length == 0)) {
Assert.assertTrue("Internal key " + RestUtils.InternalKeys.SEND_USER_METADATA_AS_RESPONSE_BODY + " should be set", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_USER_METADATA_AS_RESPONSE_BODY));
} else if (!(blobInfo.getUserMetadata().length == 0)) {
USER_METADATA.forEach((key, value) -> Assert.assertEquals("Value of " + key + " not as expected", value, restResponseChannel.getHeader(key)));
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getNotModifiedBlobAndVerify.
/**
* Gets the blob with blob ID {@code blobId} and verifies that the blob is not returned as blob is not modified
* @param blobId the blob ID of the blob to GET.
* @param getOption the options to use while getting the blob.
* @throws Exception
*/
private void getNotModifiedBlobAndVerify(String blobId, GetOption getOption) throws Exception {
JSONObject headers = new JSONObject();
if (getOption != null) {
headers.put(RestUtils.Headers.GET_OPTION, getOption.toString());
}
SimpleDateFormat dateFormat = new SimpleDateFormat(RestUtils.HTTP_DATE_FORMAT, Locale.ENGLISH);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date(System.currentTimeMillis());
String dateStr = dateFormat.format(date);
headers.put(RestUtils.Headers.IF_MODIFIED_SINCE, dateStr);
RestRequest restRequest = createRestRequest(RestMethod.GET, blobId, headers, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status", ResponseStatus.NotModified, restResponseChannel.getStatus());
assertNotNull("Date header expected", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertNotNull("Last-Modified header expected", restResponseChannel.getHeader(RestUtils.Headers.LAST_MODIFIED));
assertNull(RestUtils.Headers.BLOB_SIZE + " should have been null ", restResponseChannel.getHeader(RestUtils.Headers.BLOB_SIZE));
assertNull("Content-Type should have been null", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertNull("Content-Length should have been null", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH));
assertEquals("No content expected as blob is not modified", 0, restResponseChannel.getResponseBody().length);
assertNull("Accept-Ranges should not be set", restResponseChannel.getHeader(RestUtils.Headers.ACCEPT_RANGES));
assertNull("Content-Range header should not be set", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_RANGE));
}
Aggregations