diff --git a/common/src/main/java/com/hedera/block/common/utils/FileUtilities.java b/common/src/main/java/com/hedera/block/common/utils/FileUtilities.java index db82a7bdc..dcbaf5662 100644 --- a/common/src/main/java/com/hedera/block/common/utils/FileUtilities.java +++ b/common/src/main/java/com/hedera/block/common/utils/FileUtilities.java @@ -97,7 +97,7 @@ public static void createFolderPathIfNotExists( Objects.requireNonNull(toCreate); Objects.requireNonNull(logLevel); Objects.requireNonNull(permissions); - StringUtilities.requireNotBlank(semanticPathName); + Preconditions.requireNotBlank(semanticPathName); if (Files.notExists(toCreate)) { Files.createDirectories(toCreate, permissions); final String logMessage = "Created [%s] at '%s'".formatted(semanticPathName, toCreate); diff --git a/common/src/main/java/com/hedera/block/common/utils/MathUtilities.java b/common/src/main/java/com/hedera/block/common/utils/MathUtilities.java new file mode 100644 index 000000000..0084d526e --- /dev/null +++ b/common/src/main/java/com/hedera/block/common/utils/MathUtilities.java @@ -0,0 +1,38 @@ +/* + * 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; + +/** + * A utility class that deals with logic related to Mathematics. + */ +public final class MathUtilities { + /** + * This method checks if the given number is a power of two. + * + * @param toCheck the number to check if it is a power of two + * @return {@code true} if the given number is a power of two + */ + public static boolean isPowerOfTwo(final int toCheck) { + // mathematically powers of two are always positive numbers, so if the + // input is negative or zero, it is not a power of two, and we do not + // need to trigger the second check, hence we return false immediately + // by short-circuiting the logical AND operation + return (0 < toCheck) && ((toCheck & (toCheck - 1)) == 0); + } + + private MathUtilities() {} +} diff --git a/common/src/main/java/com/hedera/block/common/utils/Preconditions.java b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java new file mode 100644 index 000000000..13910978d --- /dev/null +++ b/common/src/main/java/com/hedera/block/common/utils/Preconditions.java @@ -0,0 +1,136 @@ +/* + * 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 java.util.Objects; + +/** A utility class used to assert various preconditions. */ +public final class Preconditions { + /** + * This method asserts a given {@link String} is not blank, meaning it is + * not {@code null} or does not contain only whitespaces as defined by + * {@link String#isBlank()}. If the given {@link String} is not blank, then + * we return it, else we throw {@link IllegalArgumentException}. + * + * @param toCheck a {@link String} to be checked if is blank as defined + * above + * @return the {@link String} to be checked if it is not blank as defined + * above + * @throws IllegalArgumentException if the input {@link String} to be + * checked is blank + */ + public static String requireNotBlank(final String toCheck) { + return requireNotBlank(toCheck, null); + } + + /** + * This method asserts a given {@link String} is not blank, meaning it is + * not {@code null} or does not contain only whitespaces as defined by + * {@link String#isBlank()}. If the given {@link String} is not blank, then + * we return it, else we throw {@link IllegalArgumentException}. + * + * @param toCheck a {@link String} to be checked if is blank as defined + * above + * @param errorMessage the error message to be used in the exception if the + * input {@link String} to be checked is blank, if null, a default message + * @return the {@link String} to be checked if it is not blank as defined + * above + * @throws IllegalArgumentException if the input {@link String} to be + * checked is blank + */ + public static String requireNotBlank(final String toCheck, final String errorMessage) { + if (StringUtilities.isBlank(toCheck)) { + final String message = + Objects.isNull(errorMessage) ? "The input String is required to be non-blank." : errorMessage; + throw new IllegalArgumentException(message); + } else { + return toCheck; + } + } + + /** + * This method asserts a given integer is a positive. An integer is positive + * if it is NOT equal to zero and is greater than zero. + * + * @param toCheck the number to check if it is a positive power of two + * @return the number to check if it is positive + * @throws IllegalArgumentException if the input number to check is not + * positive + */ + public static int requirePositive(final int toCheck) { + return requirePositive(toCheck, null); + } + + /** + * This method asserts a given integer is a positive. An integer is positive + * if it is NOT equal to zero and is greater than zero. + * + * @param toCheck the integer to check if it is a positive power of two + * @param errorMessage the error message to be used in the exception if the + * input integer to check is not positive, if null, a default message will + * be used + * @return the number to check if it is positive + * @throws IllegalArgumentException if the input number to check is not + * positive + */ + public static int requirePositive(final int toCheck, final String errorMessage) { + if (0 >= toCheck) { + final String message = Objects.isNull(errorMessage) + ? "The input integer [%d] is required be positive.".formatted(toCheck) + : errorMessage; + throw new IllegalArgumentException(message); + } else { + return toCheck; + } + } + + /** + * This method asserts a given integer is a power of two. + * + * @param toCheck the number to check if it is a power of two + * @return the number to check if it is a power of two + * @throws IllegalArgumentException if the input number to check is not a + * power of two + */ + public static int requirePowerOfTwo(final int toCheck) { + return requirePowerOfTwo(toCheck, null); + } + + /** + * This method asserts a given integer is a power of two. + * + * @param toCheck the number to check if it is a power of two + * @param errorMessage the error message to be used in the exception if the + * input integer to check is not a power of two, if null, a default message + * will be used + * @return the number to check if it is a power of two + * @throws IllegalArgumentException if the input number to check is not a + * power of two + */ + public static int requirePowerOfTwo(final int toCheck, final String errorMessage) { + if (!MathUtilities.isPowerOfTwo(toCheck)) { + final String message = Objects.isNull(errorMessage) + ? "The input integer [%d] is required to be a power of two.".formatted(toCheck) + : errorMessage; + throw new IllegalArgumentException(message); + } else { + return toCheck; + } + } + + private Preconditions() {} +} diff --git a/common/src/main/java/com/hedera/block/common/utils/StringUtilities.java b/common/src/main/java/com/hedera/block/common/utils/StringUtilities.java index 4977fdcae..d5d24ff4b 100644 --- a/common/src/main/java/com/hedera/block/common/utils/StringUtilities.java +++ b/common/src/main/java/com/hedera/block/common/utils/StringUtilities.java @@ -16,26 +16,23 @@ package com.hedera.block.common.utils; -import edu.umd.cs.findbugs.annotations.NonNull; import java.util.Objects; /** A utility class that deals with logic related to Strings. */ public final class StringUtilities { /** - * This method checks if a given {@link String} is blank, meaning if it is {@code null} or - * contains only whitespaces as defined by {@link String#isBlank()}. If the given {@link String} - * is not blank, then we return it, else we throw {@link IllegalArgumentException}. + * This method takes an input {@link String} and checks if it is blank. + * A {@link String} is considered blank if it is either {@code null} or + * contains only whitespace characters as defined by + * {@link String#isBlank()}. * - * @param toCheck a {@link String} to be checked if is blank as defined above - * @return the {@link String} to be checked if it is not blank as defined above - * @throws IllegalArgumentException if the input {@link String} to be checked is blank + * @param toCheck a {@link String} to check if it is blank + * @return {@code true} if the given {@link String} to check is either + * {@code null} or contains only whitespace characters, {@code false} + * otherwise */ - public static String requireNotBlank(@NonNull final String toCheck) { - if (Objects.requireNonNull(toCheck).isBlank()) { - throw new IllegalArgumentException("A String required to be non-blank is blank."); - } else { - return toCheck; - } + public static boolean isBlank(final String toCheck) { + return Objects.isNull(toCheck) || toCheck.isBlank(); } private StringUtilities() {} diff --git a/common/src/test/java/com/hedera/block/common/CommonsTestUtility.java b/common/src/test/java/com/hedera/block/common/CommonsTestUtility.java new file mode 100644 index 000000000..00c113be3 --- /dev/null +++ b/common/src/test/java/com/hedera/block/common/CommonsTestUtility.java @@ -0,0 +1,205 @@ +/* + * 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; + +import java.util.stream.Stream; +import org.junit.jupiter.params.provider.Arguments; + +/** + * Utilities for testing the common module. + */ +public final class CommonsTestUtility { + /** + * Some simple non-blank strings only with spaces and new lines and tabs (there are more whitespace chars). + */ + public static Stream nonBlankStrings() { + return Stream.of( + Arguments.of("a"), + Arguments.of(" a"), + Arguments.of("a "), + Arguments.of(" a "), + Arguments.of("a b"), + Arguments.of("\na"), // new line + Arguments.of("\ta"), // tab + Arguments.of("\fa"), // form feed + Arguments.of("\ra"), // carriage return + Arguments.of("\u000Ba"), // vertical tab + Arguments.of("\u001Ca"), // file separator + Arguments.of("\u001Da"), // group separator + Arguments.of("\u001Ea"), // record separator + Arguments.of("\u001Fa") // unit separator + ); + } + + /** + * Some simple blank strings. + */ + public static Stream blankStrings() { + return Stream.of( + Arguments.of((String) null), + Arguments.of(""), + Arguments.of(" "), + Arguments.of("\n"), // new line + Arguments.of("\t"), // tab + Arguments.of("\f"), // form feed + Arguments.of("\r"), // carriage return + Arguments.of("\u000B"), // vertical tab + Arguments.of("\u001C"), // file separator + Arguments.of("\u001D"), // group separator + Arguments.of("\u001E"), // record separator + Arguments.of("\u001F") // unit separator + ); + } + + /** + * Some valid power of two integers. + */ + public static Stream powerOfTwoIntegers() { + return Stream.of( + Arguments.of(1), + Arguments.of(2), + Arguments.of(4), + Arguments.of(8), + Arguments.of(16), + Arguments.of(32), + Arguments.of(64), + Arguments.of(128), + Arguments.of(256), + Arguments.of(512), + Arguments.of(1_024), + Arguments.of(2_048), + Arguments.of(4_096), + Arguments.of(8_192), + Arguments.of(16_384), + Arguments.of(32_768), + Arguments.of(65_536), + Arguments.of(131_072), + Arguments.of(262_144), + Arguments.of(524_288), + Arguments.of(1_048_576), + Arguments.of(2_097_152), + Arguments.of(4_194_304), + Arguments.of(8_388_608), + Arguments.of(16_777_216), + Arguments.of(33_554_432), + Arguments.of(67_108_864), + Arguments.of(134_217_728), + Arguments.of(268_435_456), + Arguments.of(536_870_912), + Arguments.of(1_073_741_824)); + } + + /** + * Some power of two integers, but with negative sign. + */ + public static Stream negativePowerOfTwoIntegers() { + return Stream.of( + Arguments.of(-1), + Arguments.of(-2), + Arguments.of(-4), + Arguments.of(-8), + Arguments.of(-16), + Arguments.of(-32), + Arguments.of(-64), + Arguments.of(-128), + Arguments.of(-256), + Arguments.of(-512), + Arguments.of(-1_024), + Arguments.of(-2_048), + Arguments.of(-4_096), + Arguments.of(-8_192), + Arguments.of(-16_384), + Arguments.of(-32_768), + Arguments.of(-65_536), + Arguments.of(-131_072), + Arguments.of(-262_144), + Arguments.of(-524_288), + Arguments.of(-1_048_576), + Arguments.of(-2_097_152), + Arguments.of(-4_194_304), + Arguments.of(-8_388_608), + Arguments.of(-16_777_216), + Arguments.of(-33_554_432), + Arguments.of(-67_108_864), + Arguments.of(-134_217_728), + Arguments.of(-268_435_456), + Arguments.of(-536_870_912), + Arguments.of(-1_073_741_824)); + } + + /** + * Some non power of two integers. + */ + public static Stream nonPowerOfTwoIntegers() { + return Stream.of( + Arguments.of(0), + Arguments.of(3), + Arguments.of(5), + Arguments.of(6), + Arguments.of(7), + Arguments.of(9), + Arguments.of(10), + Arguments.of(11), + Arguments.of(511), + Arguments.of(1_023), + Arguments.of(4_097), + Arguments.of(16_381), + Arguments.of(65_535), + Arguments.of(524_287), + Arguments.of(33_554_431), + Arguments.of(1_073_741_825)); + } + + /** + * Some positive integers. + */ + public static Stream positiveIntegers() { + return Stream.of( + Arguments.of(1), + Arguments.of(2), + Arguments.of(3), + Arguments.of(4), + Arguments.of(5), + Arguments.of(100), + Arguments.of(1_000), + Arguments.of(10_000), + Arguments.of(100_000), + Arguments.of(1_000_000), + Arguments.of(10_000_000)); + } + + /** + * Zero and some negative integers. + */ + public static Stream zeroAndNegativeIntegers() { + return Stream.of( + Arguments.of(0), + Arguments.of(-1), + Arguments.of(-2), + Arguments.of(-3), + Arguments.of(-4), + Arguments.of(-5), + Arguments.of(-100), + Arguments.of(-1_000), + Arguments.of(-10_000), + Arguments.of(-100_000), + Arguments.of(-1_000_000), + Arguments.of(-10_000_000)); + } + + private CommonsTestUtility() {} +} diff --git a/common/src/test/java/com/hedera/block/common/utils/MathUtilitiesTest.java b/common/src/test/java/com/hedera/block/common/utils/MathUtilitiesTest.java new file mode 100644 index 000000000..034c1c88e --- /dev/null +++ b/common/src/test/java/com/hedera/block/common/utils/MathUtilitiesTest.java @@ -0,0 +1,52 @@ +/* + * 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.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * Tests for {@link MathUtilities} functionality. + */ +class MathUtilitiesTest { + /** + * This test aims to verify that the {@link MathUtilities#isPowerOfTwo(int)} + * returns {@code true} if the input number is a power of two. + */ + @ParameterizedTest + @MethodSource("com.hedera.block.common.CommonsTestUtility#powerOfTwoIntegers") + void testIsPowerOfTwoPass(final int toTest) { + final boolean actual = MathUtilities.isPowerOfTwo(toTest); + assertThat(actual).isTrue(); + } + + /** + * This test aims to verify that the {@link MathUtilities#isPowerOfTwo(int)} + * returns {@code false} if the input number is not a power of two. + */ + @ParameterizedTest + @MethodSource({ + "com.hedera.block.common.CommonsTestUtility#nonPowerOfTwoIntegers", + "com.hedera.block.common.CommonsTestUtility#negativePowerOfTwoIntegers" + }) + void testIsPowerOfTwoFail(final int toTest) { + final boolean actual = MathUtilities.isPowerOfTwo(toTest); + assertThat(actual).isFalse(); + } +} diff --git a/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java b/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java new file mode 100644 index 000000000..cbdd17c9f --- /dev/null +++ b/common/src/test/java/com/hedera/block/common/utils/PreconditionsTest.java @@ -0,0 +1,151 @@ +/* + * 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.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +import java.util.function.Consumer; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * Tests for {@link Preconditions} functionality. + */ +class PreconditionsTest { + /** + * This test aims to verify that the + * {@link Preconditions#requireNotBlank(String)} will return the input + * 'toTest' parameter if the non-blank check passes. Test includes + * overloads. + * + * @param toTest parameterized, the string to test + */ + @ParameterizedTest + @MethodSource("com.hedera.block.common.CommonsTestUtility#nonBlankStrings") + void testRequireNotBlankPass(final String toTest) { + final Consumer asserts = + actual -> assertThat(actual).isNotNull().isNotBlank().isEqualTo(toTest); + + final String actual = Preconditions.requireNotBlank(toTest); + assertThat(actual).satisfies(asserts); + + final String actualOverload = Preconditions.requireNotBlank(toTest, "test error message"); + assertThat(actualOverload).satisfies(asserts); + } + + /** + * This test aims to verify that the + * {@link Preconditions#requireNotBlank(String)} will throw an + * {@link IllegalArgumentException} if the non-blank check fails. Test + * includes overloads. + * + * @param toTest parameterized, the string to test + */ + @ParameterizedTest + @MethodSource("com.hedera.block.common.CommonsTestUtility#blankStrings") + void testRequireNotBlankFail(final String toTest) { + assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requireNotBlank(toTest)); + + final String testErrorMessage = "test error message"; + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requireNotBlank(toTest, testErrorMessage)) + .withMessage(testErrorMessage); + } + + /** + * This test aims to verify that the + * {@link Preconditions#requirePositive(int)} will return the input 'toTest' + * parameter if the positive check passes. Test includes overloads. + * + * @param toTest parameterized, the number to test + */ + @ParameterizedTest + @MethodSource("com.hedera.block.common.CommonsTestUtility#positiveIntegers") + void testRequirePositivePass(final int toTest) { + final Consumer asserts = + actual -> assertThat(actual).isPositive().isEqualTo(toTest); + + final int actual = Preconditions.requirePositive(toTest); + assertThat(actual).satisfies(asserts); + + final int actualOverload = Preconditions.requirePositive(toTest, "test error message"); + assertThat(actualOverload).satisfies(asserts); + } + + /** + * This test aims to verify that the + * {@link Preconditions#requirePositive(int)} will throw an + * {@link IllegalArgumentException} if the positive check fails. Test + * includes overloads. + * + * @param toTest parameterized, the number to test + */ + @ParameterizedTest + @MethodSource("com.hedera.block.common.CommonsTestUtility#zeroAndNegativeIntegers") + void testRequirePositiveFail(final int toTest) { + assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requirePositive(toTest)); + + final String testErrorMessage = "test error message"; + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requirePositive(toTest, testErrorMessage)) + .withMessage(testErrorMessage); + } + + /** + * This test aims to verify that the + * {@link Preconditions#requirePowerOfTwo(int)} will return the input + * 'toTest' parameter if the power of two check passes. Test includes + * overloads. + * + * @param toTest parameterized, the number to test + */ + @ParameterizedTest + @MethodSource("com.hedera.block.common.CommonsTestUtility#powerOfTwoIntegers") + void testRequirePowerOfTwoPass(final int toTest) { + final Consumer asserts = + actual -> assertThat(actual).isPositive().isEqualTo(toTest); + + final int actual = Preconditions.requirePowerOfTwo(toTest); + assertThat(actual).satisfies(asserts); + + final int actualOverload = Preconditions.requirePowerOfTwo(toTest, "test error message"); + assertThat(actualOverload).satisfies(asserts); + } + + /** + * This test aims to verify that the + * {@link Preconditions#requirePowerOfTwo(int)} will throw an + * {@link IllegalArgumentException} if the power of two check fails. Test + * includes overloads. + * + * @param toTest parameterized, the number to test + */ + @ParameterizedTest + @MethodSource({ + "com.hedera.block.common.CommonsTestUtility#nonPowerOfTwoIntegers", + "com.hedera.block.common.CommonsTestUtility#negativePowerOfTwoIntegers" + }) + void testRequirePowerOfTwoFail(final int toTest) { + assertThatIllegalArgumentException().isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest)); + + final String testErrorMessage = "test error message"; + assertThatIllegalArgumentException() + .isThrownBy(() -> Preconditions.requirePowerOfTwo(toTest, testErrorMessage)) + .withMessage(testErrorMessage); + } +} diff --git a/common/src/test/java/com/hedera/block/common/utils/StringUtilitiesTest.java b/common/src/test/java/com/hedera/block/common/utils/StringUtilitiesTest.java index 60777925a..2619fb87a 100644 --- a/common/src/test/java/com/hedera/block/common/utils/StringUtilitiesTest.java +++ b/common/src/test/java/com/hedera/block/common/utils/StringUtilitiesTest.java @@ -17,13 +17,8 @@ package com.hedera.block.common.utils; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import java.util.stream.Stream; -import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; /** @@ -31,78 +26,26 @@ */ class StringUtilitiesTest { /** - * This test aims to verify that the {@link StringUtilities#requireNotBlank(String)} will return the input - * 'toTest' parameter if the non-blank check passes. + * This test aims to verify that the {@link StringUtilities#isBlank(String)} + * returns {@code true} if the input string is blank. * - * @param toTest parameterized, the string to test + * @param toTest parameterized, the String to test */ @ParameterizedTest - @MethodSource("nonBlankStrings") + @MethodSource("com.hedera.block.common.CommonsTestUtility#blankStrings") void testRequireNotBlankPass(final String toTest) { - final String actual = StringUtilities.requireNotBlank(toTest); - assertThat(actual).isNotNull().isNotBlank().isEqualTo(toTest); + assertThat(StringUtilities.isBlank(toTest)).isTrue(); } /** - * This test aims to verify that the {@link StringUtilities#requireNotBlank(String)} will throw an - * {@link IllegalArgumentException} if the non-blank check fails. + * This test aims to verify that the {@link StringUtilities#isBlank(String)} + * returns {@code false} if the input string is not blank. * * @param toTest parameterized, the string to test */ @ParameterizedTest - @MethodSource("blankStrings") + @MethodSource("com.hedera.block.common.CommonsTestUtility#nonBlankStrings") void testRequireNotBlankFail(final String toTest) { - assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> StringUtilities.requireNotBlank(toTest)); - } - - /** - * This test aims to verify that the {@link StringUtilities#requireNotBlank(String)} will throw a - * {@link NullPointerException} if the input string to test is null. - */ - @Test - void test_requireNotBlank_ThrowsNullPointerExceptionIfInputStringIsNull() { - assertThatNullPointerException().isThrownBy(() -> StringUtilities.requireNotBlank(null)); - } - - /** - * Some simple non-blank strings only with spaces and new lines and tabs (there are more whitespace chars). - */ - private static Stream nonBlankStrings() { - return Stream.of( - Arguments.of("a"), - Arguments.of(" a"), - Arguments.of("a "), - Arguments.of(" a "), - Arguments.of("a b"), - Arguments.of("\na"), // new line - Arguments.of("\ta"), // tab - Arguments.of("\fa"), // form feed - Arguments.of("\ra"), // carriage return - Arguments.of("\u000Ba"), // vertical tab - Arguments.of("\u001Ca"), // file separator - Arguments.of("\u001Da"), // group separator - Arguments.of("\u001Ea"), // record separator - Arguments.of("\u001Fa") // unit separator - ); - } - - /** - * Some simple blank strings. - */ - private static Stream blankStrings() { - return Stream.of( - Arguments.of(""), - Arguments.of(" "), - Arguments.of("\n"), // new line - Arguments.of("\t"), // tab - Arguments.of("\f"), // form feed - Arguments.of("\r"), // carriage return - Arguments.of("\u000B"), // vertical tab - Arguments.of("\u001C"), // file separator - Arguments.of("\u001D"), // group separator - Arguments.of("\u001E"), // record separator - Arguments.of("\u001F") // unit separator - ); + assertThat(StringUtilities.isBlank(toTest)).isFalse(); } } diff --git a/server/src/main/java/com/hedera/block/server/mediator/MediatorConfig.java b/server/src/main/java/com/hedera/block/server/mediator/MediatorConfig.java index b0dc7dc28..6c2f939ed 100644 --- a/server/src/main/java/com/hedera/block/server/mediator/MediatorConfig.java +++ b/server/src/main/java/com/hedera/block/server/mediator/MediatorConfig.java @@ -18,6 +18,7 @@ import static java.lang.System.Logger.Level.INFO; +import com.hedera.block.common.utils.Preconditions; import com.swirlds.config.api.ConfigData; import com.swirlds.config.api.ConfigProperty; @@ -43,14 +44,8 @@ public record MediatorConfig( * @throws IllegalArgumentException if the configuration is invalid */ public MediatorConfig { - if (ringBufferSize <= 0) { - throw new IllegalArgumentException("Ring buffer size must be greater than 0"); - } - - if ((ringBufferSize & (ringBufferSize - 1)) != 0) { - throw new IllegalArgumentException("Ring buffer size must be a power of 2"); - } - + Preconditions.requirePositive(ringBufferSize, "Mediator Ring Buffer Size must be positive"); + Preconditions.requirePowerOfTwo(ringBufferSize, "Mediator Ring Buffer Size must be a power of 2"); LOGGER.log(INFO, "Mediator configuration mediator.ringBufferSize: " + ringBufferSize); LOGGER.log(INFO, "Mediator configuration mediator.type: " + type); } diff --git a/server/src/main/java/com/hedera/block/server/notifier/NotifierConfig.java b/server/src/main/java/com/hedera/block/server/notifier/NotifierConfig.java index a55ac0081..3b753d9b6 100644 --- a/server/src/main/java/com/hedera/block/server/notifier/NotifierConfig.java +++ b/server/src/main/java/com/hedera/block/server/notifier/NotifierConfig.java @@ -18,6 +18,7 @@ import static java.lang.System.Logger.Level.INFO; +import com.hedera.block.common.utils.Preconditions; import com.swirlds.config.api.ConfigData; import com.swirlds.config.api.ConfigProperty; @@ -38,14 +39,8 @@ public record NotifierConfig(@ConfigProperty(defaultValue = "1024") int ringBuff * @throws IllegalArgumentException if the configuration is invalid */ public NotifierConfig { - if (ringBufferSize <= 0) { - throw new IllegalArgumentException("Ring buffer size must be greater than 0"); - } - - if ((ringBufferSize & (ringBufferSize - 1)) != 0) { - throw new IllegalArgumentException("Ring buffer size must be a power of 2"); - } - + Preconditions.requirePositive(ringBufferSize, "Notifier Ring Buffer Size must be positive"); + Preconditions.requirePowerOfTwo(ringBufferSize, "Notifier Ring Buffer Size must be a power of 2"); LOGGER.log(INFO, "Notifier configuration notifier.ringBufferSize: " + ringBufferSize); } } diff --git a/server/src/test/java/com/hedera/block/server/mediator/MediatorConfigTest.java b/server/src/test/java/com/hedera/block/server/mediator/MediatorConfigTest.java index 582e7037b..c30eadd3a 100644 --- a/server/src/test/java/com/hedera/block/server/mediator/MediatorConfigTest.java +++ b/server/src/test/java/com/hedera/block/server/mediator/MediatorConfigTest.java @@ -34,7 +34,7 @@ public void testMediatorConfig_happyPath() { public void testMediatorConfig_negativeRingBufferSize() { IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new MediatorConfig(-1, "")); - assertEquals("Ring buffer size must be greater than 0", exception.getMessage()); + assertEquals("Mediator Ring Buffer Size must be positive", exception.getMessage()); } @Test @@ -52,7 +52,7 @@ public void testMediatorConfig_powerOf2Values() { for (int powerOf2Value : powerOf2Values) { IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new MediatorConfig(powerOf2Value + 1, "")); - assertEquals("Ring buffer size must be a power of 2", exception.getMessage()); + assertEquals("Mediator Ring Buffer Size must be a power of 2", exception.getMessage()); } } } diff --git a/server/src/test/java/com/hedera/block/server/notifier/NotifierConfigTest.java b/server/src/test/java/com/hedera/block/server/notifier/NotifierConfigTest.java index 329da42e0..8852284af 100644 --- a/server/src/test/java/com/hedera/block/server/notifier/NotifierConfigTest.java +++ b/server/src/test/java/com/hedera/block/server/notifier/NotifierConfigTest.java @@ -32,9 +32,8 @@ public void testNotifierConfig_happyPath() { @Test public void testNotifierConfig_negativeRingBufferSize() { - IllegalArgumentException exception = - assertThrows(IllegalArgumentException.class, () -> new NotifierConfig(-1)); - assertEquals("Ring buffer size must be greater than 0", exception.getMessage()); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new NotifierConfig(-1)); + assertEquals("Notifier Ring Buffer Size must be positive", exception.getMessage()); } @Test @@ -50,10 +49,8 @@ public void testMediatorConfig_powerOf2Values() { // Test the non-power of 2 values for (int powerOf2Value : powerOf2Values) { IllegalArgumentException exception = - assertThrows( - IllegalArgumentException.class, - () -> new NotifierConfig(powerOf2Value + 1)); - assertEquals("Ring buffer size must be a power of 2", exception.getMessage()); + assertThrows(IllegalArgumentException.class, () -> new NotifierConfig(powerOf2Value + 1)); + assertEquals("Notifier Ring Buffer Size must be a power of 2", exception.getMessage()); } } }