|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hadoop.ozone.om.helpers; |
| 19 | + |
| 20 | +import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.INVALID_REQUEST; |
| 21 | +import static org.apache.hadoop.ozone.om.helpers.OMLCUtils.assertOMException; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 26 | + |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import org.apache.hadoop.ozone.om.exceptions.OMException; |
| 29 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.AbortIncompleteMultipartUpload; |
| 30 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.LifecycleAction; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + * Test OmLCAbortIncompleteMultipartUpload. |
| 35 | + */ |
| 36 | +class TestOmLCAbortIncompleteMultipartUpload { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testCreateValidAbortIncompleteMultipartUpload() { |
| 40 | + long currentTime = System.currentTimeMillis(); |
| 41 | + |
| 42 | + OmLCAbortIncompleteMultipartUpload.Builder abort1 = |
| 43 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 44 | + .setDaysAfterInitiation(1); |
| 45 | + assertDoesNotThrow(() -> abort1.build().valid(currentTime)); |
| 46 | + |
| 47 | + OmLCAbortIncompleteMultipartUpload.Builder abort2 = |
| 48 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 49 | + .setDaysAfterInitiation(7); |
| 50 | + assertDoesNotThrow(() -> abort2.build().valid(currentTime)); |
| 51 | + |
| 52 | + OmLCAbortIncompleteMultipartUpload.Builder abort3 = |
| 53 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 54 | + .setDaysAfterInitiation(365); |
| 55 | + assertDoesNotThrow(() -> abort3.build().valid(currentTime)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testCreateInvalidAbortIncompleteMultipartUpload() { |
| 60 | + long currentTime = System.currentTimeMillis(); |
| 61 | + |
| 62 | + // Null days should fail |
| 63 | + OmLCAbortIncompleteMultipartUpload.Builder abort1 = |
| 64 | + new OmLCAbortIncompleteMultipartUpload.Builder(); |
| 65 | + assertOMException(() -> abort1.build().valid(currentTime), INVALID_REQUEST, |
| 66 | + "must be specified for AbortIncompleteMultipartUpload action"); |
| 67 | + |
| 68 | + // Zero days should fail |
| 69 | + OmLCAbortIncompleteMultipartUpload.Builder abort2 = |
| 70 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 71 | + .setDaysAfterInitiation(0); |
| 72 | + assertOMException(() -> abort2.build().valid(currentTime), INVALID_REQUEST, |
| 73 | + "must be a positive integer greater than zero"); |
| 74 | + |
| 75 | + // Negative days should fail |
| 76 | + OmLCAbortIncompleteMultipartUpload.Builder abort3 = |
| 77 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 78 | + .setDaysAfterInitiation(-1); |
| 79 | + assertOMException(() -> abort3.build().valid(currentTime), INVALID_REQUEST, |
| 80 | + "must be a positive integer greater than zero"); |
| 81 | + |
| 82 | + OmLCAbortIncompleteMultipartUpload.Builder abort4 = |
| 83 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 84 | + .setDaysAfterInitiation(-100); |
| 85 | + assertOMException(() -> abort4.build().valid(currentTime), INVALID_REQUEST, |
| 86 | + "must be a positive integer greater than zero"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testShouldAbort() throws OMException { |
| 91 | + long currentTime = System.currentTimeMillis(); |
| 92 | + |
| 93 | + // Upload created 10 days ago |
| 94 | + long uploadCreationTime = currentTime - TimeUnit.DAYS.toMillis(10); |
| 95 | + |
| 96 | + // Rule: abort after 7 days - should abort |
| 97 | + OmLCAbortIncompleteMultipartUpload abort7Days = |
| 98 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 99 | + .setDaysAfterInitiation(7) |
| 100 | + .build(); |
| 101 | + abort7Days.valid(currentTime); |
| 102 | + assertTrue(abort7Days.shouldAbort(uploadCreationTime), |
| 103 | + "Upload created 10 days ago should be aborted with 7-day threshold"); |
| 104 | + |
| 105 | + // Rule: abort after 15 days - should NOT abort |
| 106 | + OmLCAbortIncompleteMultipartUpload abort15Days = |
| 107 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 108 | + .setDaysAfterInitiation(15) |
| 109 | + .build(); |
| 110 | + abort15Days.valid(currentTime); |
| 111 | + assertFalse(abort15Days.shouldAbort(uploadCreationTime), |
| 112 | + "Upload created 10 days ago should NOT be aborted with 15-day threshold"); |
| 113 | + |
| 114 | + // Upload created 1 hour ago - should NOT abort |
| 115 | + long recentUploadTime = currentTime - TimeUnit.HOURS.toMillis(1); |
| 116 | + OmLCAbortIncompleteMultipartUpload abort1Day = |
| 117 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 118 | + .setDaysAfterInitiation(1) |
| 119 | + .build(); |
| 120 | + abort1Day.valid(currentTime); |
| 121 | + assertFalse(abort1Day.shouldAbort(recentUploadTime), |
| 122 | + "Upload created 1 hour ago should NOT be aborted with 1-day threshold"); |
| 123 | + |
| 124 | + // Upload created 1 day + 1 second ago - should abort |
| 125 | + long moreThanOneDay = currentTime - TimeUnit.DAYS.toMillis(1) - TimeUnit.SECONDS.toMillis(1); |
| 126 | + assertTrue(abort1Day.shouldAbort(moreThanOneDay), |
| 127 | + "Upload created more than 1 day ago should be aborted"); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testProtobufConversion() throws OMException { |
| 132 | + long currentTime = System.currentTimeMillis(); |
| 133 | + |
| 134 | + // Create with 7 days |
| 135 | + OmLCAbortIncompleteMultipartUpload original = |
| 136 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 137 | + .setDaysAfterInitiation(7) |
| 138 | + .build(); |
| 139 | + |
| 140 | + // Convert to protobuf |
| 141 | + LifecycleAction proto = original.getProtobuf(); |
| 142 | + assertTrue(proto.hasAbortIncompleteMultipartUpload(), |
| 143 | + "Protobuf should have AbortIncompleteMultipartUpload"); |
| 144 | + |
| 145 | + AbortIncompleteMultipartUpload abortProto = proto.getAbortIncompleteMultipartUpload(); |
| 146 | + assertEquals(7, abortProto.getDaysAfterInitiation(), |
| 147 | + "Days should be preserved in protobuf"); |
| 148 | + |
| 149 | + // Convert back from protobuf |
| 150 | + OmLCAbortIncompleteMultipartUpload fromProto = |
| 151 | + OmLCAbortIncompleteMultipartUpload.getFromProtobuf(abortProto); |
| 152 | + assertEquals(7, fromProto.getDaysAfterInitiation(), |
| 153 | + "Days should be preserved after protobuf round-trip"); |
| 154 | + |
| 155 | + // Validate the converted object |
| 156 | + assertDoesNotThrow(() -> fromProto.valid(currentTime), |
| 157 | + "Object from protobuf should be valid"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testActionType() { |
| 162 | + OmLCAbortIncompleteMultipartUpload abort = |
| 163 | + new OmLCAbortIncompleteMultipartUpload.Builder() |
| 164 | + .setDaysAfterInitiation(7) |
| 165 | + .build(); |
| 166 | + |
| 167 | + assertEquals(OmLCAction.ActionType.ABORT_INCOMPLETE_MULTIPART_UPLOAD, |
| 168 | + abort.getActionType(), |
| 169 | + "Action type should be ABORT_INCOMPLETE_MULTIPART_UPLOAD"); |
| 170 | + } |
| 171 | +} |
0 commit comments