zachjsh commented on a change in pull request #9459: Ability to Delete task logs and segments from S3 URL: https://github.com/apache/druid/pull/9459#discussion_r388805794
########## File path: extensions-core/s3-extensions/src/test/java/org/apache/druid/storage/s3/S3TestUtils.java ########## @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.storage.s3; + +import com.amazonaws.services.s3.model.DeleteObjectsRequest; +import com.amazonaws.services.s3.model.ListObjectsV2Request; +import com.amazonaws.services.s3.model.ListObjectsV2Result; +import com.amazonaws.services.s3.model.S3ObjectSummary; +import junit.framework.AssertionFailedError; +import org.apache.commons.collections4.map.HashedMap; +import org.easymock.EasyMock; +import org.easymock.EasyMockSupport; +import org.easymock.IArgumentMatcher; +import org.easymock.IExpectationSetters; + +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class S3TestUtils extends EasyMockSupport +{ + public static ListObjectsV2Request listObjectsV2RequestArgumentMatcher(ListObjectsV2Request listObjectsV2Request) + { + EasyMock.reportMatcher(new IArgumentMatcher() + { + @Override + public boolean matches(Object argument) + { + + return argument instanceof ListObjectsV2Request + && listObjectsV2Request.getBucketName().equals(((ListObjectsV2Request) argument).getBucketName()) + && listObjectsV2Request.getPrefix().equals(((ListObjectsV2Request) argument).getPrefix()) + && ((listObjectsV2Request.getContinuationToken() == null + && ((ListObjectsV2Request) argument).getContinuationToken() == null) + || (listObjectsV2Request.getContinuationToken() + .equals(((ListObjectsV2Request) argument).getContinuationToken()))) + && listObjectsV2Request.getMaxKeys().equals(((ListObjectsV2Request) argument).getMaxKeys()); + } + + @Override + public void appendTo(StringBuffer buffer) + { + String str = "ListObjectsV2Request(\"bucketName:\" \"" + + listObjectsV2Request.getBucketName() + + "\", \"prefix:\"" + + listObjectsV2Request.getPrefix() + + "\", \"continuationToken:\"" + + listObjectsV2Request.getContinuationToken() + + "\", \"maxKeys:\"" + + listObjectsV2Request.getMaxKeys() + + "\")"; + buffer.append(str); + } + }); + return null; + } + + public static DeleteObjectsRequest deleteObjectsRequestArgumentMatcher(DeleteObjectsRequest deleteObjectsRequest) + { + EasyMock.reportMatcher(new IArgumentMatcher() + { + @Override + public boolean matches(Object argument) + { + + boolean matches = argument instanceof DeleteObjectsRequest + && deleteObjectsRequest.getBucketName() + .equals(((DeleteObjectsRequest) argument).getBucketName()) + && deleteObjectsRequest.getKeys().size() == ((DeleteObjectsRequest) argument).getKeys() + .size(); + if (matches) { + Map<String, String> expectedKeysAndVersions = deleteObjectsRequest.getKeys().stream().collect( + Collectors.toMap(DeleteObjectsRequest.KeyVersion::getKey, x -> { + return x.getVersion() == null ? "null" : x.getVersion(); + })); + Map<String, String> actualKeysAndVersions = ((DeleteObjectsRequest) argument).getKeys().stream().collect( + Collectors.toMap(DeleteObjectsRequest.KeyVersion::getKey, x -> { + return x.getVersion() == null ? "null" : x.getVersion(); + })); + matches = expectedKeysAndVersions.equals(actualKeysAndVersions); + } + return matches; + } + + @Override + public void appendTo(StringBuffer buffer) + { + String str = "DeleteObjectsRequest(\"bucketName:\" \"" + + deleteObjectsRequest.getBucketName() + + "\", \"keys:\"" + + deleteObjectsRequest.getKeys() + + "\")"; + buffer.append(str); + } + }); + return null; + } + + public static S3ObjectSummary mockS3ObjectSummary(long lastModified, String key) + { + S3ObjectSummary objectSummary = EasyMock.createMock(S3ObjectSummary.class); + EasyMock.expect(objectSummary.getLastModified()).andReturn(new Date(lastModified)); + EasyMock.expectLastCall().anyTimes(); + EasyMock.expect(objectSummary.getKey()).andReturn(key); + EasyMock.expectLastCall().anyTimes(); + return objectSummary; + } + + public static ListObjectsV2Request mockRequest( + String bucket, + String prefix, + int maxKeys, + String continuationToken + ) + { + ListObjectsV2Request request = EasyMock.createMock(ListObjectsV2Request.class); + EasyMock.expect(request.getBucketName()).andReturn(bucket); + EasyMock.expectLastCall().anyTimes(); Review comment: Yes, you're right, noticing it in other places in the code. I will simplify, thanks ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
