ChenSammi commented on code in PR #8557: URL: https://github.com/apache/ozone/pull/8557#discussion_r2165827961
########## hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestLifecycleConfigurationPut.java: ########## @@ -0,0 +1,502 @@ +/* + * 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.hadoop.ozone.s3.endpoint; + +import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; +import static java.net.HttpURLConnection.HTTP_FORBIDDEN; +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; +import static java.net.HttpURLConnection.HTTP_OK; +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.ACCESS_DENIED; +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.INVALID_REQUEST; +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.MALFORMED_XML; +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.NO_SUCH_BUCKET; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.function.Supplier; +import javax.ws.rs.core.HttpHeaders; +import org.apache.hadoop.ozone.client.BucketArgs; +import org.apache.hadoop.ozone.client.ObjectStore; +import org.apache.hadoop.ozone.client.OzoneClient; +import org.apache.hadoop.ozone.client.OzoneClientStub; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +/** + * Testing for PutBucketLifecycleConfiguration. + */ +public class TestLifecycleConfigurationPut { + + private OzoneClient clientStub; + private BucketEndpoint bucketEndpoint; + + @BeforeEach + public void setup() throws Exception { + clientStub = new OzoneClientStub(); + bucketEndpoint = EndpointBuilder.newBucketEndpointBuilder() + .setClient(clientStub) + .build(); + ObjectStore objectStore = clientStub.getObjectStore(); + BucketArgs bucketArgs = BucketArgs.newBuilder().setOwner("owner").build(); + objectStore.createVolume("s3v"); + objectStore.getS3Volume().createBucket("bucket1", bucketArgs); + } + + @Test + public void testLifecycleConfigurationFailWithEmptyBody() throws Exception { + try { + bucketEndpoint.put("bucket1", null, "", null, null); + fail(); + } catch (OS3Exception ex) { + assertEquals(HTTP_BAD_REQUEST, ex.getHttpCode()); + assertEquals(MALFORMED_XML.getCode(), ex.getCode()); + } + } + + @Test + public void testLifecycleConfigurationFailWithNonExistentBucket() + throws Exception { + try { + bucketEndpoint.put("nonexistentbucket", null, "", null, onePrefix()); + fail(); + } catch (OS3Exception ex) { + assertEquals(HTTP_NOT_FOUND, ex.getHttpCode()); + assertEquals(NO_SUCH_BUCKET.getCode(), ex.getCode()); + } + } + + @Test + public void testCreateInvalidLifecycleConfiguration() throws Exception { + testInvalidLifecycleConfiguration(TestLifecycleConfigurationPut::withoutAction, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + testInvalidLifecycleConfiguration(TestLifecycleConfigurationPut::withoutFilter, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + testInvalidLifecycleConfiguration(this::useDuplicateTagInAndOperator, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + testInvalidLifecycleConfiguration(this::usePrefixTagWithoutAndOperatorInFilter, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + testInvalidLifecycleConfiguration(this::usePrefixAndOperatorCoExistInFilter, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + testInvalidLifecycleConfiguration(this::usePrefixFilterCoExist, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + testInvalidLifecycleConfiguration(this::useAndOperatorOnlyOnePrefix, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + testInvalidLifecycleConfiguration(this::useAndOperatorOnlyOneTag, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + testInvalidLifecycleConfiguration(this::useEmptyAndOperator, + HTTP_BAD_REQUEST, INVALID_REQUEST.getCode()); + } + + private void testInvalidLifecycleConfiguration(Supplier<InputStream> inputStream, + int expectedHttpCode, String expectedErrorCode) throws Exception { + try { + bucketEndpoint.put("bucket1", null, "", null, inputStream.get()); + fail("Expected an OS3Exception to be thrown"); + } catch (OS3Exception ex) { + assertEquals(expectedHttpCode, ex.getHttpCode()); + assertEquals(expectedErrorCode, ex.getCode()); + } + } + + @Test + public void testCreateInvalidExpirationDateLCC() throws Exception { + try { + String xml = ("<LifecycleConfiguration xmlns=\"http://s3.amazonaws" + + ".com/doc/2006-03-01/\">" + + "<Rule>" + + "<ID>remove logs after 30 days</ID>" + + "<Prefix>prefix/</Prefix>" + + "<Status>Enabled</Status>" + + "<Expiration><Date>2023-03-03</Date></Expiration>" + + "</Rule>" + + "</LifecycleConfiguration>"); + + bucketEndpoint.put("bucket1", null, "", null, + new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))); + fail(); + } catch (OS3Exception ex) { + assertEquals(HTTP_BAD_REQUEST, ex.getHttpCode()); + assertEquals(INVALID_REQUEST.getCode(), ex.getCode()); + } + } + + @Test + public void testCreateValidLifecycleConfiguration() throws Exception { Review Comment: Can you rearrange the function order, that each InputStream supplier can close to their caller for a easy review? -- 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. To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For additional commands, e-mail: issues-h...@ozone.apache.org