use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class AmbrySecurityServiceTest method testPostBlob.
/**
* Tests {@link AmbrySecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)} for
* {@link RestMethod#POST}.
* @throws Exception
*/
private void testPostBlob() throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
RestRequest restRequest = createRestRequest(RestMethod.POST, "/", null);
securityService.processResponse(restRequest, restResponseChannel, DEFAULT_INFO).get();
Assert.assertEquals("ProcessResponse status should have been set", ResponseStatus.Created, restResponseChannel.getStatus());
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
Assert.assertEquals("Creation time should have been set correctly", RestUtils.toSecondsPrecisionInMs(DEFAULT_INFO.getBlobProperties().getCreationTimeInMs()), RestUtils.getTimeFromDateString(restResponseChannel.getHeader(RestUtils.Headers.CREATION_TIME)).longValue());
Assert.assertEquals("Content-Length should have been 0", "0", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH));
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method oldStyleUserMetadataTest.
/**
* Tests how metadata that has not been POSTed in the form of headers is returned.
* @throws Exception
*/
@Test
public void oldStyleUserMetadataTest() throws Exception {
ByteBuffer content = ByteBuffer.allocate(0);
BlobProperties blobProperties = new BlobProperties(0, "userMetadataTestOldStyleServiceID", Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, false);
byte[] usermetadata = TestUtils.getRandomBytes(25);
String blobId = router.putBlob(blobProperties, usermetadata, new ByteBufferReadableStreamChannel(content), new PutBlobOptionsBuilder().build()).get();
RestUtils.SubResource[] subResources = { RestUtils.SubResource.UserMetadata, RestUtils.SubResource.BlobInfo };
for (RestUtils.SubResource subResource : subResources) {
RestRequest restRequest = createRestRequest(RestMethod.GET, blobId + "/" + subResource, null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status for " + subResource, ResponseStatus.Ok, restResponseChannel.getStatus());
assertEquals("Unexpected Content-Type for " + subResource, "application/octet-stream", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Unexpected Content-Length for " + subResource, usermetadata.length, Integer.parseInt(restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
assertArrayEquals("Unexpected user metadata for " + subResource, usermetadata, restResponseChannel.getResponseBody());
}
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getBlobInfoAndVerify.
/**
* Gets the blob info of the blob with blob ID {@code blobId} and verifies them against what is expected.
* @param blobId the blob ID of the blob to HEAD.
* @param getOption the options to use while getting the blob.
* @param expectedHeaders the expected headers in the response.
* @param expectedAccount the expected account in the rest request.
* @param expectedContainer the expected container in the rest request.
* @throws Exception
*/
private void getBlobInfoAndVerify(String blobId, GetOption getOption, JSONObject expectedHeaders, Account expectedAccount, Container expectedContainer) throws Exception {
JSONObject headers = new JSONObject();
if (getOption != null) {
headers.put(RestUtils.Headers.GET_OPTION, getOption.toString());
}
RestRequest restRequest = createRestRequest(RestMethod.GET, blobId + "/" + RestUtils.SubResource.BlobInfo, headers, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(restRequest, restResponseChannel);
assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
checkCommonGetHeadHeaders(restResponseChannel);
assertEquals("Content-Length is not 0", "0", restResponseChannel.getHeader(RestUtils.Headers.CONTENT_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));
verifyBlobProperties(expectedHeaders, restResponseChannel);
verifyUserMetadataHeaders(expectedHeaders, restResponseChannel);
verifyAccountAndContainerHeaders(restResponseChannel, expectedAccount, expectedContainer);
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyOperationFailure.
/**
* Verifies that the operation specified by {@code restRequest} fails with {@code errorCode}.
* @param restRequest the {@link RestRequest} that should fail
* @param errorCode the {@link RestServiceErrorCode} expected
* @return the {@link MockRestResponseChannel} used for the operation
* @throws Exception
*/
private MockRestResponseChannel verifyOperationFailure(RestRequest restRequest, RestServiceErrorCode errorCode) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
try {
doOperation(restRequest, restResponseChannel);
fail("Operation should have failed");
} catch (RestServiceException e) {
assertEquals("Op should have failed with a specific error code", errorCode, e.getErrorCode());
}
return restResponseChannel;
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method getPeersTest.
/**
* Tests the handling of {@link Operations#GET_PEERS} requests.
* @throws Exception
*/
@Test
public void getPeersTest() throws Exception {
frontendRestRequestService.shutdown();
TailoredPeersClusterMap clusterMap = new TailoredPeersClusterMap();
frontendRestRequestService = new FrontendRestRequestService(frontendConfig, frontendMetrics, router, clusterMap, idConverterFactory, securityServiceFactory, urlSigningService, idSigningService, null, accountService, accountAndContainerInjector, datacenterName, hostname, clusterName, accountStatsStore, QUOTA_MANAGER);
frontendRestRequestService.setupResponseHandler(responseHandler);
frontendRestRequestService.start();
// test good requests
for (String datanode : TailoredPeersClusterMap.DATANODE_NAMES) {
String[] parts = datanode.split(":");
String baseUri = Operations.GET_PEERS + "?" + GetPeersHandler.NAME_QUERY_PARAM + "=" + parts[0] + "&" + GetPeersHandler.PORT_QUERY_PARAM + "=" + parts[1];
String[] uris = { baseUri, "/" + baseUri };
for (String uri : uris) {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
doOperation(createRestRequest(RestMethod.GET, uri, null, null), restResponseChannel);
byte[] peerStrBytes = restResponseChannel.getResponseBody();
Set<String> peersFromResponse = GetPeersHandlerTest.getPeersFromResponse(new JSONObject(new String(peerStrBytes)));
Set<String> expectedPeers = clusterMap.getPeers(datanode);
assertEquals("Peer list returned does not match expected for " + datanode, expectedPeers, peersFromResponse);
}
}
// test one bad request
RestRequest restRequest = createRestRequest(RestMethod.GET, Operations.GET_PEERS, null, null);
verifyOperationFailure(restRequest, RestServiceErrorCode.MissingArgs);
}
Aggregations