From 365e37b1ef623c984f454f3d8288be4419fbd22c Mon Sep 17 00:00:00 2001 From: Sophie Guo Date: Sun, 6 Oct 2024 09:47:41 -0700 Subject: [PATCH] Adding unit test --- .../FrontendRestRequestServiceTest.java | 43 +++++++++++++++++++ .../frontend/NamedBlobPutHandlerTest.java | 27 ------------ 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/ambry-frontend/src/test/java/com/github/ambry/frontend/FrontendRestRequestServiceTest.java b/ambry-frontend/src/test/java/com/github/ambry/frontend/FrontendRestRequestServiceTest.java index 4289252275..321e524fe2 100644 --- a/ambry-frontend/src/test/java/com/github/ambry/frontend/FrontendRestRequestServiceTest.java +++ b/ambry-frontend/src/test/java/com/github/ambry/frontend/FrontendRestRequestServiceTest.java @@ -321,6 +321,49 @@ public void useServiceWithoutStartTest() throws Exception { } } + @Test + public void testNamedBlobPut() throws Exception { + Account testAccount = new ArrayList<>(accountService.getAllAccounts()).get(1); + Container testContainer = new ArrayList<>(testAccount.getAllContainers()).get(1); + String blobName = "blobName"; + String namedBlobPathUri = + NAMED_BLOB_PREFIX + SLASH + testAccount.getName() + SLASH + testContainer.getName() + SLASH + blobName; + ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(10)); + List body = new LinkedList<>(); + body = new LinkedList<>(); + body.add(content); + body.add(null); + JSONObject headers = new JSONObject().put(RestUtils.Headers.TARGET_ACCOUNT_NAME, testAccount.getName()) + .put(RestUtils.Headers.TARGET_CONTAINER_NAME, testContainer.getName()); + setAmbryHeadersForPut(headers, -1, testContainer.isCacheable(), "test", "application/octet-stream", "owner", null, + null, null); + RestRequest restRequest = createRestRequest(RestMethod.PUT, namedBlobPathUri, headers, body); + BlobProperties blobProperties = + new BlobProperties(0, testAccount.getName(), "owner", "image/gif", false, 7200, testAccount.getId(), + testContainer.getId(), false, null, null, null); + ReadableStreamChannel byteBufferContent = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(10)); + String blobIdFromRouter = + router.putBlobWithIdVersion(blobProperties, new byte[0], byteBufferContent, BlobId.BLOB_ID_V6).get(); + String blobIdWithClusterName = "/" + CLUSTER_NAME + "/" + blobIdFromRouter + ".bin"; + reset(namedBlobDb); + NamedBlobRecord namedBlobRecord = + new NamedBlobRecord(testAccount.getName(), testContainer.getName(), blobName, blobIdWithClusterName, 3600); + NamedBlobRecord namedBlobRecordAfterTtlUpdate = + new NamedBlobRecord(testAccount.getName(), testContainer.getName(), blobName, blobIdWithClusterName, + Utils.Infinite_Time); + when(namedBlobDb.put(any(), any(), any())).thenReturn( + CompletableFuture.completedFuture(new PutResult(namedBlobRecord))); + when(namedBlobDb.get(namedBlobRecord.getAccountName(), namedBlobRecord.getContainerName(), blobName, + GetOption.None)).thenReturn(CompletableFuture.completedFuture(namedBlobRecord)); + when(namedBlobDb.updateBlobTtlAndStateToReady(any())).thenReturn( + CompletableFuture.completedFuture(new PutResult(namedBlobRecordAfterTtlUpdate))); + + MockRestResponseChannel restResponseChannel = new MockRestResponseChannel(); + doOperation(restRequest, restResponseChannel); + assertEquals("Unexpected response status", ResponseStatus.Created, restResponseChannel.getStatus()); + assertEquals("Unexpected blob Id", blobIdWithClusterName, restResponseChannel.getHeader(LOCATION)); + } + /** * Checks for reactions of all methods in {@link FrontendRestRequestService} to null arguments. * @throws Exception diff --git a/ambry-frontend/src/test/java/com/github/ambry/frontend/NamedBlobPutHandlerTest.java b/ambry-frontend/src/test/java/com/github/ambry/frontend/NamedBlobPutHandlerTest.java index fbd56b5ae5..162619b306 100644 --- a/ambry-frontend/src/test/java/com/github/ambry/frontend/NamedBlobPutHandlerTest.java +++ b/ambry-frontend/src/test/java/com/github/ambry/frontend/NamedBlobPutHandlerTest.java @@ -96,7 +96,6 @@ public class NamedBlobPutHandlerTest { private static final String BLOBNAME = "ambry_blob_name"; private static final String DATASET_NAME = "testDataset"; private static final String VERSION = "1"; - private static final String PATH_PREFIX_TO_REMOVE = "/media"; private final NamedBlobDb namedBlobDb; @@ -193,26 +192,6 @@ public void updateNamedBlobAllowedTest() throws Exception { assertEquals("Unexpected response status", restResponseChannel.getStatus(), ResponseStatus.Ok); } - @Test - public void testStripPrefixAndExtension() throws Exception { - String EXTENSION = ".bin"; - Properties properties = new Properties(); - properties.setProperty("frontend.path.prefixes.to.remove", PATH_PREFIX_TO_REMOVE); - initNamedBlobPutHandler(properties); - - String blobId = CLUSTER_NAME + CONVERTED_ID + EXTENSION; - assertEquals("Blob Id should not have prefix and extention", RestUtils.stripSlashAndExtensionFromId(CONVERTED_ID), - stripPrefixAndExtension(blobId)); - - blobId = PATH_PREFIX_TO_REMOVE + "/" + CLUSTER_NAME + CONVERTED_ID + EXTENSION; - assertEquals("Blob Id should not have prefix and extention", RestUtils.stripSlashAndExtensionFromId(CONVERTED_ID), - stripPrefixAndExtension(blobId)); - - blobId = CONVERTED_ID + EXTENSION; - assertEquals("Blob Id should not have prefix and extention", RestUtils.stripSlashAndExtensionFromId(CONVERTED_ID), - stripPrefixAndExtension(blobId)); - } - /** * Do NOT allow upsert for container with NamedBlobMode = NO_UPDATE */ @@ -691,10 +670,4 @@ private RestRequest getRestRequest(JSONObject headers, String path, byte[] reque RequestPath.parse(request, frontendConfig.pathPrefixesToRemove, CLUSTER_NAME)); return request; } - - public String stripPrefixAndExtension(String blobId) throws RestServiceException { - return RestUtils.stripSlashAndExtensionFromId( - RequestPath.parse(blobId, Collections.emptyMap(), frontendConfig.pathPrefixesToRemove, CLUSTER_NAME) - .getOperationOrBlobId(false)); - } }