Skip to content

Commit fac4e7e

Browse files
committed
test: add TestNSSummaryCodec
1 parent 991bbac commit fac4e7e

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.recon.codec;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22+
23+
import java.util.HashSet;
24+
import java.util.Set;
25+
26+
import org.apache.hadoop.hdds.utils.db.Codec;
27+
import org.apache.hadoop.hdds.utils.db.CodecBuffer;
28+
import org.apache.hadoop.ozone.recon.ReconConstants;
29+
import org.apache.hadoop.ozone.recon.api.types.NSSummary;
30+
import org.junit.jupiter.api.Test;
31+
32+
/**
33+
* Tests for NSSummaryCodec CodecBuffer implementation.
34+
*/
35+
public class TestNSSummaryCodec {
36+
37+
private final Codec<NSSummary> codec = NSSummaryCodec.get();
38+
39+
@Test
40+
public void testCodecBufferRoundTrip() throws Exception {
41+
NSSummary original = createTestNSSummary();
42+
43+
CodecBuffer buffer = codec.toCodecBuffer(original, CodecBuffer.Allocator.DIRECT);
44+
NSSummary decoded = codec.fromCodecBuffer(buffer);
45+
46+
assertNSSummaryEquals(original, decoded);
47+
}
48+
49+
@Test
50+
public void testCodecBufferEmptyDirectory() throws Exception {
51+
NSSummary original = new NSSummary();
52+
original.setDirName("/empty");
53+
original.setParentId(42L);
54+
55+
CodecBuffer buffer = codec.toCodecBuffer(original, CodecBuffer.Allocator.DIRECT);
56+
NSSummary decoded = codec.fromCodecBuffer(buffer);
57+
58+
assertNSSummaryEquals(original, decoded);
59+
}
60+
61+
@Test
62+
public void testCodecBufferLargeDirectory() throws Exception {
63+
NSSummary original = new NSSummary();
64+
original.setDirName("/large");
65+
original.setNumOfFiles(10000);
66+
original.setSizeOfFiles(1024L * 1024L * 100L); // 100MB
67+
original.setParentId(999L);
68+
69+
Set<Long> childDirs = new HashSet<>();
70+
for (long i = 1; i <= 1000; i++) {
71+
childDirs.add(i);
72+
}
73+
original.setChildDir(childDirs);
74+
75+
int[] buckets = new int[ReconConstants.NUM_OF_FILE_SIZE_BINS];
76+
for (int i = 0; i < buckets.length; i++) {
77+
buckets[i] = i * 100;
78+
}
79+
original.setFileSizeBucket(buckets);
80+
81+
CodecBuffer buffer = codec.toCodecBuffer(original, CodecBuffer.Allocator.DIRECT);
82+
NSSummary decoded = codec.fromCodecBuffer(buffer);
83+
84+
assertNSSummaryEquals(original, decoded);
85+
}
86+
87+
private NSSummary createTestNSSummary() {
88+
NSSummary summary = new NSSummary();
89+
summary.setDirName("/test/directory");
90+
summary.setNumOfFiles(100);
91+
summary.setSizeOfFiles(1024L * 512L); // 512KB
92+
summary.setParentId(42L);
93+
94+
Set<Long> childDirs = new HashSet<>();
95+
childDirs.add(1L);
96+
childDirs.add(2L);
97+
childDirs.add(3L);
98+
summary.setChildDir(childDirs);
99+
100+
int[] buckets = new int[ReconConstants.NUM_OF_FILE_SIZE_BINS];
101+
for (int i = 0; i < buckets.length; i++) {
102+
buckets[i] = i * 10;
103+
}
104+
summary.setFileSizeBucket(buckets);
105+
106+
return summary;
107+
}
108+
109+
private void assertNSSummaryEquals(NSSummary expected, NSSummary actual) {
110+
assertEquals(expected.getDirName(), actual.getDirName());
111+
assertEquals(expected.getNumOfFiles(), actual.getNumOfFiles());
112+
assertEquals(expected.getSizeOfFiles(), actual.getSizeOfFiles());
113+
assertEquals(expected.getParentId(), actual.getParentId());
114+
assertEquals(expected.getChildDir(), actual.getChildDir());
115+
assertArrayEquals(expected.getFileSizeBucket(), actual.getFileSizeBucket());
116+
}
117+
}

0 commit comments

Comments
 (0)