Search in sources :

Example 46 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method getReplicasTest.

/**
 * Tests {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)}
 * <p/>
 * For each {@link PartitionId} in the {@link ClusterMap}, a {@link BlobId} is created. The replica list returned from
 * {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)}is checked for equality against a locally
 * obtained replica list.
 * @throws Exception
 */
@Test
public void getReplicasTest() throws Exception {
    List<? extends PartitionId> partitionIds = clusterMap.getWritablePartitionIds(null);
    for (PartitionId partitionId : partitionIds) {
        String originalReplicaStr = partitionId.getReplicaIds().toString().replace(", ", ",");
        BlobId blobId = new BlobId(blobIdVersion, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, partitionId, false, BlobId.BlobDataType.DATACHUNK);
        RestRequest restRequest = createRestRequest(RestMethod.GET, blobId.getID() + "/" + RestUtils.SubResource.Replicas, null, null);
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        doOperation(restRequest, restResponseChannel);
        JSONObject response = new JSONObject(new String(restResponseChannel.getResponseBody()));
        String returnedReplicasStr = response.get(GetReplicasHandler.REPLICAS_KEY).toString().replace("\"", "");
        assertEquals("Replica IDs returned for the BlobId do no match with the replicas IDs of partition", originalReplicaStr, returnedReplicasStr);
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) PartitionId(com.github.ambry.clustermap.PartitionId) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) BlobId(com.github.ambry.commons.BlobId) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 47 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method getUserMetadataAndVerify.

/**
 * Gets the user metadata 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.
 * @throws Exception
 */
private void getUserMetadataAndVerify(String blobId, GetOption getOption, JSONObject expectedHeaders) 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.UserMetadata, 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));
    verifyUserMetadataHeaders(expectedHeaders, restResponseChannel);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 48 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method submitResponseTest.

/**
 * Tests
 * {@link FrontendRestRequestService#submitResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)}.
 * @throws JSONException
 * @throws UnsupportedEncodingException
 * @throws URISyntaxException
 */
@Test
public void submitResponseTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
    String exceptionMsg = TestUtils.getRandomString(10);
    responseHandler.shutdown();
    // handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
    try {
        // there is an exception already.
        RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        assertTrue("RestRequest channel is not open", restRequest.isOpen());
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, new RuntimeException(exceptionMsg));
        assertEquals("Unexpected exception message", exceptionMsg, restResponseChannel.getException().getMessage());
        // there is no exception and exception thrown when the response is submitted.
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        assertTrue("RestRequest channel is not open", restRequest.isOpen());
        restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel response = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
        assertTrue("Response channel is not open", response.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, response, null);
        assertNotNull("There is no cause of failure", restResponseChannel.getException());
        // resources should have been cleaned up.
        assertFalse("Response channel is not cleaned up", response.isOpen());
    } finally {
        frontendRestRequestService.setupResponseHandler(responseHandler);
        responseHandler.start();
    }
    // verify tracking infos are attached accordingly.
    RestRequest restRequest;
    MockRestResponseChannel restResponseChannel;
    for (String header : RestUtils.TrackingHeaders.TRACKING_HEADERS) {
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        restResponseChannel = new MockRestResponseChannel();
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
        assertTrue("Response header should not contain tracking info", restResponseChannel.getHeader(header) == null);
    }
    restRequest = createRestRequest(RestMethod.GET, "/", null, null);
    restRequest.setArg(RestUtils.InternalKeys.SEND_TRACKING_INFO, new Boolean(true));
    restResponseChannel = new MockRestResponseChannel();
    frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
    assertEquals("Unexpected or missing tracking info", datacenterName, restResponseChannel.getHeader(RestUtils.TrackingHeaders.DATACENTER_NAME));
    assertEquals("Unexpected or missing tracking info", hostname, restResponseChannel.getHeader(RestUtils.TrackingHeaders.FRONTEND_NAME));
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 49 with MockRestResponseChannel

use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method verifyAccountAndContainerFromBlobId.

/**
 * Verifies presence of {@link Account} and {@link Container} injected into {@link RestRequest} using a
 * blobId string, for get/head/delete operations.
 * @param blobId The blobId string to get/head/delete.
 * @param expectedAccount The expected {@link Account} to verify its presence in {@link RestRequest}.
 * @param expectedContainer The expected {@link Container} to verify its presence in {@link RestRequest}.
 * @param expectedRestErrorCode The expected {@link RestServiceErrorCode} to verify.
 * @throws Exception
 */
private void verifyAccountAndContainerFromBlobId(String blobId, Account expectedAccount, Container expectedContainer, RestServiceErrorCode expectedRestErrorCode) throws Exception {
    if (blobId.startsWith("/")) {
        blobId = blobId.substring(1);
    }
    // PUT is verified in the tests of the individual handlers.
    for (RestMethod restMethod : Lists.newArrayList(RestMethod.GET, RestMethod.HEAD, RestMethod.DELETE)) {
        RestRequest restRequest = createRestRequest(restMethod, "/" + blobId, null, null);
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        try {
            doOperation(restRequest, restResponseChannel);
            if (expectedRestErrorCode != null) {
                fail("Should have thrown");
            }
        } catch (RestServiceException e) {
            assertEquals("Wrong RestServiceErrorCode", expectedRestErrorCode, e.getErrorCode());
        }
        BlobId deserializedId = new BlobId(blobId, clusterMap);
        // Because BlobInfo is not fetched on deletes, V1 Blob IDs will never be reassigned to a known account/container.
        boolean alwaysExpectUnknown = restMethod == RestMethod.DELETE && deserializedId.getAccountId() == Account.UNKNOWN_ACCOUNT_ID && deserializedId.getContainerId() == Container.UNKNOWN_CONTAINER_ID;
        assertEquals("Wrong account object in RestRequest's args", alwaysExpectUnknown ? InMemAccountService.UNKNOWN_ACCOUNT : expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
        assertEquals("Wrong container object in RestRequest's args", alwaysExpectUnknown ? Container.UNKNOWN_CONTAINER : expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) BlobId(com.github.ambry.commons.BlobId) RestMethod(com.github.ambry.rest.RestMethod)

Example 50 with MockRestResponseChannel

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());
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) RestMethod(com.github.ambry.rest.RestMethod) Method(java.lang.reflect.Method) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)72 RestRequest (com.github.ambry.rest.RestRequest)57 MockRestRequest (com.github.ambry.rest.MockRestRequest)51 JSONObject (org.json.JSONObject)39 RestServiceException (com.github.ambry.rest.RestServiceException)36 Test (org.junit.Test)34 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)27 RestMethod (com.github.ambry.rest.RestMethod)19 Account (com.github.ambry.account.Account)18 FutureResult (com.github.ambry.router.FutureResult)17 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)17 ExecutionException (java.util.concurrent.ExecutionException)17 StorageStatsUtilTest (com.github.ambry.server.StorageStatsUtilTest)16 RestUtils (com.github.ambry.rest.RestUtils)15 ByteBuffer (java.nio.ByteBuffer)15 MetricRegistry (com.codahale.metrics.MetricRegistry)14 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)14 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)14 TestUtils (com.github.ambry.utils.TestUtils)14 TimeUnit (java.util.concurrent.TimeUnit)14