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 GetAccountsHandlerTest method validRequestsTest.
/**
* Test valid request cases.
* @throws Exception
*/
@Test
public void validRequestsTest() throws Exception {
Account account = accountService.createAndAddRandomAccount();
ThrowingBiConsumer<RestRequest, Collection<Account>> testAction = (request, expectedAccounts) -> {
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = sendRequestGetResponse(request, restResponseChannel);
assertNotNull("There should be a response", channel);
Assert.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)));
RetainingAsyncWritableChannel asyncWritableChannel = new RetainingAsyncWritableChannel((int) channel.getSize());
channel.readInto(asyncWritableChannel, null).get();
assertEquals("Accounts do not match", new HashSet<>(expectedAccounts), new HashSet<>(AccountCollectionSerde.accountsFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream())));
};
testAction.accept(createRestRequest(null, null, null, Operations.ACCOUNTS), accountService.getAllAccounts());
testAction.accept(createRestRequest(account.getName(), null, null, Operations.ACCOUNTS), Collections.singleton(account));
testAction.accept(createRestRequest(null, Short.toString(account.getId()), null, Operations.ACCOUNTS), Collections.singleton(account));
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class GetAccountsHandlerTest method getSingleContainerFailureTest.
/**
* Test failure case of getting single container.
* @throws Exception
*/
@Test
public void getSingleContainerFailureTest() throws Exception {
ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals("Unexpected error code", expectedErrorCode, e.getErrorCode()));
};
// 1. invalid header (i.e. missing container name)
testAction.accept(createRestRequest("test-account", null, null, Operations.ACCOUNTS_CONTAINERS), RestServiceErrorCode.MissingArgs);
// 2. account not found
testAction.accept(createRestRequest("fake-account", null, "fake-container", Operations.ACCOUNTS_CONTAINERS), RestServiceErrorCode.NotFound);
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class GetAccountsHandlerTest method badRequestsTest.
/**
* Test bad request cases.
* @throws Exception
*/
@Test
public void badRequestsTest() throws Exception {
Account existingAccount = accountService.createAndAddRandomAccount();
Account nonExistentAccount = accountService.generateRandomAccount();
ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals("Unexpected error code", expectedErrorCode, e.getErrorCode()));
};
// cannot supply both ID and name
testAction.accept(createRestRequest(existingAccount.getName(), Short.toString(existingAccount.getId()), null, Operations.ACCOUNTS), RestServiceErrorCode.BadRequest);
// non-numerical ID
testAction.accept(createRestRequest(null, "ABC", null, Operations.ACCOUNTS), RestServiceErrorCode.InvalidArgs);
// account that doesn't exist
testAction.accept(createRestRequest(nonExistentAccount.getName(), null, null, Operations.ACCOUNTS), RestServiceErrorCode.NotFound);
testAction.accept(createRestRequest(null, Short.toString(nonExistentAccount.getId()), null, Operations.ACCOUNTS), RestServiceErrorCode.NotFound);
}
use of com.github.ambry.rest.MockRestResponseChannel in project ambry by linkedin.
the class TailoredPeersClusterMap method handleGoodCaseTest.
/**
* Tests for the good cases where the datanodes are correct. The peers obtained from the response is compared
* against the ground truth.
* @throws Exception
*/
@Test
public void handleGoodCaseTest() throws Exception {
for (String datanode : TailoredPeersClusterMap.DATANODE_NAMES) {
RestRequest restRequest = getRestRequest(datanode);
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = sendRequestGetResponse(restRequest, restResponseChannel);
assertNotNull("There should be a response", channel);
Assert.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)));
Set<String> expectedPeers = clusterMap.getPeers(datanode);
Set<String> peersFromResponse = getPeersFromResponse(RestTestUtils.getJsonizedResponseBody(channel));
assertEquals("Peer list returned does not match expected for " + datanode, expectedPeers, peersFromResponse);
}
}
Aggregations