Skip to content

Commit 3d236ad

Browse files
committed
test: add unit tests and xml tests
1 parent b2fa2cb commit 3d236ad

6 files changed

Lines changed: 603 additions & 1 deletion

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
}

hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLCRule.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,113 @@ public void testProtobufConversion() throws OMException {
318318
assertEquals(30, ruleFromProto3.getExpiration().getDays());
319319
assertNull(ruleFromProto3.getFilter());
320320
}
321+
322+
@Test
323+
public void testRuleWithAbortIncompleteMultipartUpload() throws OMException {
324+
long currentTime = System.currentTimeMillis();
325+
326+
// Test rule with only AbortIncompleteMultipartUpload action
327+
OmLCAbortIncompleteMultipartUpload abortAction =
328+
new OmLCAbortIncompleteMultipartUpload.Builder()
329+
.setDaysAfterInitiation(7)
330+
.build();
331+
332+
OmLCRule.Builder rule1 = new OmLCRule.Builder()
333+
.setId("abort-incomplete-uploads")
334+
.setEnabled(true)
335+
.setPrefix("uploads/")
336+
.setAction(abortAction);
337+
338+
OmLCRule builtRule = rule1.build();
339+
assertDoesNotThrow(() -> builtRule.valid(BucketLayout.DEFAULT, currentTime));
340+
assertNotNull(builtRule.getAbortIncompleteMultipartUpload());
341+
assertEquals(7, builtRule.getAbortIncompleteMultipartUpload().getDaysAfterInitiation());
342+
}
343+
344+
@Test
345+
public void testRuleWithBothExpirationAndAbortActions() throws OMException {
346+
long currentTime = System.currentTimeMillis();
347+
348+
OmLCExpiration expiration = new OmLCExpiration.Builder()
349+
.setDays(30)
350+
.build();
351+
352+
OmLCAbortIncompleteMultipartUpload abortAction =
353+
new OmLCAbortIncompleteMultipartUpload.Builder()
354+
.setDaysAfterInitiation(7)
355+
.build();
356+
357+
OmLCRule.Builder rule = new OmLCRule.Builder()
358+
.setId("combined-rule")
359+
.setEnabled(true)
360+
.setPrefix("temp/")
361+
.addAction(expiration)
362+
.addAction(abortAction);
363+
364+
OmLCRule builtRule = rule.build();
365+
assertDoesNotThrow(() -> builtRule.valid(BucketLayout.DEFAULT, currentTime));
366+
assertNotNull(builtRule.getExpiration());
367+
assertNotNull(builtRule.getAbortIncompleteMultipartUpload());
368+
assertEquals(30, builtRule.getExpiration().getDays());
369+
assertEquals(7, builtRule.getAbortIncompleteMultipartUpload().getDaysAfterInitiation());
370+
}
371+
372+
@Test
373+
public void testProtobufConversionWithAbortAction() throws OMException {
374+
long currentTime = System.currentTimeMillis();
375+
376+
OmLCAbortIncompleteMultipartUpload abortAction =
377+
new OmLCAbortIncompleteMultipartUpload.Builder()
378+
.setDaysAfterInitiation(14)
379+
.build();
380+
381+
OmLCRule originalRule = new OmLCRule.Builder()
382+
.setId("test-abort-rule")
383+
.setEnabled(true)
384+
.setPrefix("multipart/")
385+
.setAction(abortAction)
386+
.build();
387+
388+
LifecycleRule proto = originalRule.getProtobuf();
389+
390+
OmLCRule ruleFromProto = OmLCRule.getFromProtobuf(proto, BucketLayout.DEFAULT);
391+
assertEquals("test-abort-rule", ruleFromProto.getId());
392+
assertTrue(ruleFromProto.isEnabled());
393+
assertEquals("multipart/", ruleFromProto.getPrefix());
394+
assertNotNull(ruleFromProto.getAbortIncompleteMultipartUpload());
395+
assertEquals(14, ruleFromProto.getAbortIncompleteMultipartUpload().getDaysAfterInitiation());
396+
}
397+
398+
@Test
399+
public void testProtobufConversionWithBothActions() throws OMException {
400+
long currentTime = System.currentTimeMillis();
401+
402+
OmLCExpiration expiration = new OmLCExpiration.Builder()
403+
.setDays(60)
404+
.build();
405+
406+
OmLCAbortIncompleteMultipartUpload abortAction =
407+
new OmLCAbortIncompleteMultipartUpload.Builder()
408+
.setDaysAfterInitiation(5)
409+
.build();
410+
411+
OmLCRule originalRule = new OmLCRule.Builder()
412+
.setId("combined-actions")
413+
.setEnabled(true)
414+
.setPrefix("data/")
415+
.addAction(expiration)
416+
.addAction(abortAction)
417+
.build();
418+
419+
LifecycleRule proto = originalRule.getProtobuf();
420+
421+
OmLCRule ruleFromProto = OmLCRule.getFromProtobuf(proto, BucketLayout.DEFAULT);
422+
assertEquals("combined-actions", ruleFromProto.getId());
423+
assertTrue(ruleFromProto.isEnabled());
424+
assertEquals("data/", ruleFromProto.getPrefix());
425+
assertNotNull(ruleFromProto.getExpiration());
426+
assertNotNull(ruleFromProto.getAbortIncompleteMultipartUpload());
427+
assertEquals(60, ruleFromProto.getExpiration().getDays());
428+
assertEquals(5, ruleFromProto.getAbortIncompleteMultipartUpload().getDaysAfterInitiation());
429+
}
321430
}

0 commit comments

Comments
 (0)