use of com.github.ambry.named.DeleteResult in project ambry by linkedin.
the class AmbryIdConverterFactoryTest method ambryIdConverterTest.
/**
* Tests the instantiation and use of the {@link IdConverter} instance returned through the
* {@link AmbryIdConverterFactory}.
* @throws Exception
*/
@Test
public void ambryIdConverterTest() throws Exception {
// dud properties. server should pick up defaults
Properties properties = new Properties();
VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
IdSigningService idSigningService = mock(IdSigningService.class);
AmbryIdConverterFactory ambryIdConverterFactory = new AmbryIdConverterFactory(verifiableProperties, new MetricRegistry(), idSigningService, null);
IdConverter idConverter = ambryIdConverterFactory.getIdConverter();
assertNotNull("No IdConverter returned", idConverter);
String input = TestUtils.getRandomString(10);
String inputWithLeadingSlash = "/" + input;
// GET
// without leading slash
reset(idSigningService);
testConversion(idConverter, RestMethod.GET, null, input, input);
verify(idSigningService, never()).parseSignedId(any());
// with leading slash
reset(idSigningService);
testConversion(idConverter, RestMethod.GET, null, input, inputWithLeadingSlash);
verify(idSigningService, never()).parseSignedId(any());
// with signed ID input.
String idFromParsedSignedId = "parsedId" + input;
reset(idSigningService);
when(idSigningService.isIdSigned(any())).thenReturn(true);
when(idSigningService.parseSignedId(any())).thenReturn(new Pair<>(idFromParsedSignedId, Collections.emptyMap()));
testConversion(idConverter, RestMethod.GET, null, idFromParsedSignedId, inputWithLeadingSlash);
verify(idSigningService).parseSignedId(input);
// test signed id parsing exception
reset(idSigningService);
when(idSigningService.isIdSigned(any())).thenReturn(true);
when(idSigningService.parseSignedId(any())).thenThrow(new RestServiceException("expected", RestServiceErrorCode.InternalServerError));
testConversionFailure(idConverter, new MockRestRequest(MockRestRequest.DUMMY_DATA, null), input, RestServiceErrorCode.InternalServerError);
verify(idSigningService).parseSignedId(input);
// with named blob
NamedBlobDb namedBlobDb = mock(NamedBlobDb.class);
ambryIdConverterFactory = new AmbryIdConverterFactory(verifiableProperties, new MetricRegistry(), idSigningService, namedBlobDb);
idConverter = ambryIdConverterFactory.getIdConverter();
assertNotNull("No IdConverter returned", idConverter);
String outputId = "dummy-id";
reset(idSigningService);
reset(namedBlobDb);
when(namedBlobDb.get(any(), any(), any())).thenReturn(CompletableFuture.completedFuture(new NamedBlobRecord("", "", "", outputId, Utils.Infinite_Time)));
testConversion(idConverter, RestMethod.GET, null, outputId, NAMED_BLOB_PATH);
verify(namedBlobDb).get(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME);
reset(idSigningService);
reset(namedBlobDb);
when(namedBlobDb.delete(any(), any(), any())).thenReturn(CompletableFuture.completedFuture(new DeleteResult(outputId, false)));
testConversion(idConverter, RestMethod.DELETE, null, outputId, NAMED_BLOB_PATH);
verify(namedBlobDb).delete(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME);
// POST
// without leading slash (there will be no leading slashes returned from the Router)
reset(idSigningService);
testConversion(idConverter, RestMethod.POST, null, inputWithLeadingSlash, input);
verify(idSigningService, never()).getSignedId(any(), any());
// with signed id metadata set, requires signed ID.
String signedId = "signedId/" + input;
reset(idSigningService);
when(idSigningService.getSignedId(any(), any())).thenReturn(signedId);
Map<String, String> signedIdMetadata = Collections.singletonMap("test-key", "test-value");
testConversion(idConverter, RestMethod.POST, signedIdMetadata, "/" + signedId, input);
verify(idSigningService).getSignedId(input, signedIdMetadata);
idConverter.close();
testConversionFailure(idConverter, new MockRestRequest(MockRestRequest.DUMMY_DATA, null), input, RestServiceErrorCode.ServiceUnavailable);
}
Aggregations