Search in sources :

Example 61 with MockRestResponseChannel

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));
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 62 with MockRestResponseChannel

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));
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 63 with MockRestResponseChannel

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

the class GetReplicasHandlerTest method getReplicasWithBadInputTest.

/**
 * Tests reactions of the {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)} operation to bad input -
 * specifically if we do not include required parameters.
 */
@Test
public void getReplicasWithBadInputTest() {
    // bad input - invalid blob id.
    try {
        getReplicasHandler.getReplicas("12345", new MockRestResponseChannel());
        fail("Exception should have been thrown because the blobid is invalid");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.NotFound, e.getErrorCode());
    }
    // bad input - invalid blob id for this cluster map.
    String blobId = "AAEAAQAAAAAAAADFAAAAJDMyYWZiOTJmLTBkNDYtNDQyNS1iYzU0LWEwMWQ1Yzg3OTJkZQ.gif";
    try {
        getReplicasHandler.getReplicas(blobId, new MockRestResponseChannel());
        fail("Exception should have been thrown because the blobid is invalid");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.NotFound, e.getErrorCode());
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 64 with MockRestResponseChannel

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

the class GetReplicasHandlerTest method getReplicasTest.

/**
 * Tests {@link GetReplicasHandler#getReplicas(String, RestResponseChannel)}
 * <p/>
 * For the 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 = CLUSTER_MAP.getWritablePartitionIds(null);
    for (PartitionId partitionId : partitionIds) {
        String originalReplicaStr = partitionId.getReplicaIds().toString().replace(", ", ",");
        BlobId blobId = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, partitionId, false, BlobId.BlobDataType.DATACHUNK);
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel channel = getReplicasHandler.getReplicas(blobId.getID(), restResponseChannel);
        assertEquals("Unexpected response status", ResponseStatus.Ok, restResponseChannel.getStatus());
        assertEquals("Unexpected Content-Type", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
        String returnedReplicasStr = RestTestUtils.getJsonizedResponseBody(channel).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 : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) PartitionId(com.github.ambry.clustermap.PartitionId) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) BlobId(com.github.ambry.commons.BlobId) Test(org.junit.Test)

Example 65 with MockRestResponseChannel

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

the class GetStatsReportHandlerTest method handleGoodCaseTest.

@Test
public void handleGoodCaseTest() throws Exception {
    AggregatedAccountStorageStats aggregatedAccountStorageStats = new AggregatedAccountStorageStats(StorageStatsUtilTest.generateRandomAggregatedAccountStorageStats((short) 1, 10, 10, 1000L, 2, 100));
    doAnswer(invocation -> {
        String clusterName = invocation.getArgument(0);
        if (clusterName.equals(CLUSTER_NAME)) {
            return aggregatedAccountStorageStats;
        } else {
            return null;
        }
    }).when(accountStatsStore).queryAggregatedAccountStorageStatsByClusterName(anyString());
    RestRequest restRequest = createRestRequest(CLUSTER_NAME, StatsReportType.ACCOUNT_REPORT.name());
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    ReadableStreamChannel channel = sendRequestGetResponse(restRequest, restResponseChannel);
    assertNotNull("There should be a response", channel);
    assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
    assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
    assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
    assertEquals("Storage stats mismatch", aggregatedAccountStorageStats.getStorageStats(), mapper.readValue(RestTestUtils.getResponseBody(channel), AggregatedAccountStorageStats.class).getStorageStats());
    AggregatedPartitionClassStorageStats aggregatedPartitionClassStorageStats = new AggregatedPartitionClassStorageStats(StorageStatsUtilTest.generateRandomAggregatedPartitionClassStorageStats(new String[] { "default", "newClass" }, (short) 1, 10, 10, 1000L, 2, 100));
    doAnswer(invocation -> {
        String clusterName = invocation.getArgument(0);
        if (clusterName.equals(CLUSTER_NAME)) {
            return aggregatedPartitionClassStorageStats;
        } else {
            return null;
        }
    }).when(accountStatsStore).queryAggregatedPartitionClassStorageStatsByClusterName(anyString());
    restRequest = createRestRequest(CLUSTER_NAME, StatsReportType.PARTITION_CLASS_REPORT.name());
    restResponseChannel = new MockRestResponseChannel();
    channel = sendRequestGetResponse(restRequest, restResponseChannel);
    assertNotNull("There should be a response", channel);
    assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
    assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
    assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
    assertEquals("Storage stats mismatch", aggregatedPartitionClassStorageStats.getStorageStats(), mapper.readValue(RestTestUtils.getResponseBody(channel), AggregatedPartitionClassStorageStats.class).getStorageStats());
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) AggregatedAccountStorageStats(com.github.ambry.server.storagestats.AggregatedAccountStorageStats) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) AggregatedPartitionClassStorageStats(com.github.ambry.server.storagestats.AggregatedPartitionClassStorageStats) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

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