use of com.github.ambry.rest.RestRequest 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.RestRequest in project ambry by linkedin.
the class AmbrySecurityServiceTest method createRestRequest.
/**
* Method to easily create {@link RestRequest} objects containing a specific request.
* @param restMethod the {@link RestMethod} desired.
* @param uri string representation of the desired URI.
* @param headers any associated headers as a {@link JSONObject}.
* @return A {@link RestRequest} object that defines the request required by the input.
* @throws org.json.JSONException
* @throws java.io.UnsupportedEncodingException
* @throws java.net.URISyntaxException
*/
private RestRequest createRestRequest(RestMethod restMethod, String uri, JSONObject headers) throws JSONException, UnsupportedEncodingException, URISyntaxException, RestServiceException {
JSONObject request = new JSONObject();
request.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
request.put(MockRestRequest.URI_KEY, uri);
if (headers != null) {
request.put(MockRestRequest.HEADERS_KEY, headers);
}
RestRequest restRequest = new MockRestRequest(request, null);
restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(restRequest, FRONTEND_CONFIG.pathPrefixesToRemove, CLUSTER_NAME));
return restRequest;
}
use of com.github.ambry.rest.RestRequest 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));
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getReplicasWithBadInputTest.
/**
* Tests reactions of the {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)} operation to bad input -
* specifically if we do not include required parameters.
* @throws Exception
*/
@Test
public void getReplicasWithBadInputTest() throws Exception {
// bad input - invalid blob id.
RestRequest restRequest = createRestRequest(RestMethod.GET, "12345/" + RestUtils.SubResource.Replicas, null, null);
verifyOperationFailure(restRequest, RestServiceErrorCode.BadRequest);
// bad input - invalid blob id for this cluster map.
String blobId = "AAEAAQAAAAAAAADFAAAAJDMyYWZiOTJmLTBkNDYtNDQyNS1iYzU0LWEwMWQ1Yzg3OTJkZQ.gif";
restRequest = createRestRequest(RestMethod.GET, blobId + "/" + RestUtils.SubResource.Replicas, null, null);
verifyOperationFailure(restRequest, RestServiceErrorCode.BadRequest);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method doListNamedBlobsTest.
/**
* @param prefix the prefix to set in the request params.
* @param pageToken the page token to set in the request params.
* @param pageToReturn the page that {@link NamedBlobDb} should return, or {@code null} if it should throw an error.
* @param expectedErrorCode if non-null, check for this error code instead of a successful response.
* @throws Exception
*/
private void doListNamedBlobsTest(String prefix, String pageToken, Page<NamedBlobRecord> pageToReturn, RestServiceErrorCode expectedErrorCode) throws Exception {
reset(namedBlobDb);
String path = String.join("/", Operations.NAMED_BLOB, refAccount.getName(), refContainer.getName());
if (prefix != null) {
path += "?" + NamedBlobPath.PREFIX_PARAM + "=" + prefix;
}
if (pageToken != null) {
path += "&" + NamedBlobPath.PAGE_PARAM + "=" + pageToken;
}
RestRequest restRequest = createRestRequest(RestMethod.GET, path, null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
if (pageToReturn != null) {
when(namedBlobDb.list(any(), any(), any(), any())).thenReturn(CompletableFuture.completedFuture(pageToReturn));
} else {
CompletableFuture<Page<NamedBlobRecord>> future = new CompletableFuture<>();
future.completeExceptionally(new RestServiceException("NamedBlobDb error", expectedErrorCode));
when(namedBlobDb.list(any(), any(), any(), any())).thenReturn(future);
}
if (expectedErrorCode == null) {
assertNotNull("pageToReturn should be set", pageToReturn);
doOperation(restRequest, restResponseChannel);
verify(namedBlobDb).list(refAccount.getName(), refContainer.getName(), prefix, pageToken);
Page<NamedBlobListEntry> response = Page.fromJson(new JSONObject(new String(restResponseChannel.getResponseBody())), NamedBlobListEntry::new);
assertEquals("Unexpected blobs returned", pageToReturn.getEntries().stream().map(NamedBlobListEntry::new).collect(Collectors.toList()), response.getEntries());
assertEquals("Unexpected nextPageToken", pageToReturn.getNextPageToken(), response.getNextPageToken());
} else {
TestUtils.assertException(RestServiceException.class, () -> doOperation(restRequest, restResponseChannel), rse -> assertEquals("Unexpected error code", expectedErrorCode, rse.getErrorCode()));
}
}
Aggregations