Skip to content

Commit

Permalink
addressing warnings and adding unit test for Chunkify Util
Browse files Browse the repository at this point in the history
Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 committed Oct 22, 2024
1 parent 0092362 commit 5369431
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public final class ChunkUtils {
/** Chunks a collection into a list of lists of the specified size.
* @param collection the collection to chunk, if the collection is empty, an empty list is returned.
* @param chunkSize the size of each chunk
* @param <T> the type of the collection
* @return a list of lists of the specified size
* */
public static <T> List<List<T>> chunkify(
@NonNull final Collection<T> collection, final int chunkSize) {
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Module info for common module.
*/
module com.hedera.block.common {
exports com.hedera.block.common.constants;
exports com.hedera.block.common.utils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed 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 com.hedera.block.common.utils;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;

public class ChunkUtilsTest {

@Test
public void testEmptyCollection() {
List<Integer> emptyList = Collections.emptyList();
List<List<Integer>> chunks = ChunkUtils.chunkify(emptyList, 3);
assertTrue(chunks.isEmpty(), "Chunks of empty collection should be empty");
}

@Test
public void testChunkSizeZero() {
List<Integer> list = Arrays.asList(1, 2, 3);
Exception exception =
assertThrows(IllegalArgumentException.class, () -> ChunkUtils.chunkify(list, 0));
assertEquals("Chunk size must be greater than 0", exception.getMessage());
}

@Test
public void testChunkSizeNegative() {
List<Integer> list = Arrays.asList(1, 2, 3);
Exception exception =
assertThrows(IllegalArgumentException.class, () -> ChunkUtils.chunkify(list, -1));
assertEquals("Chunk size must be greater than 0", exception.getMessage());
}

@Test
public void testChunkifyCollectionSmallerThanChunkSize() {
List<Integer> list = Arrays.asList(1, 2);
List<List<Integer>> chunks = ChunkUtils.chunkify(list, 5);
assertEquals(1, chunks.size(), "Should return one chunk");
assertEquals(list, chunks.get(0), "Chunk should contain the entire collection");
}

@Test
public void testChunkifyCollectionExactlyDivisible() {
List<Integer> list = Arrays.asList(1, 2, 3, 4);
List<List<Integer>> chunks = ChunkUtils.chunkify(list, 2);
assertEquals(2, chunks.size(), "Should return two chunks");
assertEquals(Arrays.asList(1, 2), chunks.get(0), "First chunk mismatch");
assertEquals(Arrays.asList(3, 4), chunks.get(1), "Second chunk mismatch");
}

@Test
public void testChunkifyCollectionNotExactlyDivisible() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> chunks = ChunkUtils.chunkify(list, 2);
assertEquals(3, chunks.size(), "Should return three chunks");
assertEquals(Arrays.asList(1, 2), chunks.get(0), "First chunk mismatch");
assertEquals(Arrays.asList(3, 4), chunks.get(1), "Second chunk mismatch");
assertEquals(Arrays.asList(5), chunks.get(2), "Third chunk mismatch");
}

@Test
public void testNullCollection() {
assertThrows(NullPointerException.class, () -> ChunkUtils.chunkify(null, 3));
}
}
2 changes: 2 additions & 0 deletions suites/src/main/java/com/hedera/block/suites/BaseSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public BaseSuite() {
* Setup method to be executed before all tests.
*
* <p>This method initializes the Block Node server container using Testcontainers.
*
* @throws IOException if an I/O error occurs
*/
@BeforeAll
public static void setup() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.hedera.block.suites.BaseSuite;
import org.junit.jupiter.api.DisplayName;

/** Positive Data Persistence Tests */
@DisplayName("Positive Data Persistence Tests")
public class PositiveDataPersistenceTests extends BaseSuite {
/** Default constructor for the {@link PositiveDataPersistenceTests} class. */
Expand Down

0 comments on commit 5369431

Please sign in to comment.