diff --git a/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/AbstractEnumConfigConverter.java b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/AbstractEnumConfigConverter.java new file mode 100644 index 000000000000..92cbfc842ce8 --- /dev/null +++ b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/AbstractEnumConfigConverter.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import com.swirlds.config.api.converter.ConfigConverter; +import edu.umd.cs.findbugs.annotations.NonNull; +import java.util.Objects; + +/** + * Abstract class to support {@link ConfigConverter} for {@link Enum} types. + * + * @param type of the enum + */ +public abstract class AbstractEnumConfigConverter> implements ConfigConverter { + + @Override + public E convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + final Class enumType = getEnumType(); + Objects.requireNonNull(enumType, "enumType"); + return Enum.valueOf(enumType, value); + } + + /** + * Returns the {@link Class} of the {@link Enum} type. + * + * @return the {@link Class} of the {@link Enum} type + */ + @NonNull + protected abstract Class getEnumType(); +} diff --git a/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/AccountIDConverter.java b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/AccountIDConverter.java new file mode 100644 index 000000000000..e5b8508fc593 --- /dev/null +++ b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/AccountIDConverter.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import com.hedera.hapi.node.base.AccountID; +import com.swirlds.config.api.converter.ConfigConverter; +import java.util.stream.Stream; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link AccountID}. + */ +public class AccountIDConverter implements ConfigConverter { + + @Override + public AccountID convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + try { + final long[] nums = + Stream.of(value.split("[.]")).mapToLong(Long::parseLong).toArray(); + if (nums.length != 3) { + throw new IllegalArgumentException("Does not match pattern 'A.B.C'"); + } + if (nums[0] < 0) { + throw new IllegalArgumentException("Shared num of value is negative"); + } + if (nums[1] < 0) { + throw new IllegalArgumentException("Realm num of value is negative"); + } + if (nums[2] < 0) { + throw new IllegalArgumentException("Account num of value is negative"); + } + return AccountID.newBuilder() + .shardNum(nums[0]) + .realmNum(nums[1]) + .accountNum(nums[2]) + .build(); + } catch (final Exception e) { + throw new IllegalArgumentException("'" + value + "' can not be parsed to " + AccountID.class.getName(), e); + } + } +} diff --git a/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/ContractIDConverter.java b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/ContractIDConverter.java new file mode 100644 index 000000000000..0b829f758b80 --- /dev/null +++ b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/ContractIDConverter.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import com.hedera.hapi.node.base.ContractID; +import com.swirlds.config.api.converter.ConfigConverter; +import java.util.stream.Stream; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link ContractID}. + */ +public class ContractIDConverter implements ConfigConverter { + + @Override + public ContractID convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + try { + final long[] nums = + Stream.of(value.split("[.]")).mapToLong(Long::valueOf).toArray(); + if (nums.length != 3) { + throw new IllegalArgumentException("Does not match pattern 'A.B.C'"); + } + if (nums[0] < 0) { + throw new IllegalArgumentException("Shared num of value is negative"); + } + if (nums[1] < 0) { + throw new IllegalArgumentException("Realm num of value is negative"); + } + if (nums[2] < 0) { + throw new IllegalArgumentException("Account num of value is negative"); + } + return ContractID.newBuilder() + .shardNum(nums[0]) + .realmNum(nums[1]) + .contractNum(nums[2]) + .build(); + } catch (final Exception e) { + throw new IllegalArgumentException("'" + value + "' can not be parsed to " + ContractID.class.getName(), e); + } + } +} diff --git a/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/FileIDConverter.java b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/FileIDConverter.java new file mode 100644 index 000000000000..ce014a4d423d --- /dev/null +++ b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/FileIDConverter.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import com.hedera.hapi.node.base.FileID; +import com.swirlds.config.api.converter.ConfigConverter; +import java.util.stream.Stream; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link FileID}. + */ +public class FileIDConverter implements ConfigConverter { + + @Override + public FileID convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + try { + final long[] nums = + Stream.of(value.split("[.]")).mapToLong(Long::parseLong).toArray(); + if (nums.length != 3) { + throw new IllegalArgumentException("Does not match pattern 'A.B.C'"); + } + if (nums[0] < 0) { + throw new IllegalArgumentException("Shared num of value is negative"); + } + if (nums[1] < 0) { + throw new IllegalArgumentException("Realm num of value is negative"); + } + if (nums[2] < 0) { + throw new IllegalArgumentException("File num of value is negative"); + } + return FileID.newBuilder() + .shardNum(nums[0]) + .realmNum(nums[1]) + .fileNum(nums[2]) + .build(); + } catch (final Exception e) { + throw new IllegalArgumentException("'" + value + "' can not be parsed to " + FileID.class.getName(), e); + } + } +} diff --git a/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/HederaFunctionalityConverter.java b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/HederaFunctionalityConverter.java new file mode 100644 index 000000000000..9a911cd642bf --- /dev/null +++ b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/HederaFunctionalityConverter.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import com.hedera.hapi.node.base.HederaFunctionality; +import com.swirlds.config.api.converter.ConfigConverter; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link HederaFunctionality}. Based on + * https://github.com/hashgraph/hedera-services/issues/6106 we need to add {@code implements ConfigConverter} to the + * class for now. + */ +public class HederaFunctionalityConverter extends AbstractEnumConfigConverter + implements ConfigConverter { + + @Override + protected Class getEnumType() { + return HederaFunctionality.class; + } +} diff --git a/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/ProfileConverter.java b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/ProfileConverter.java new file mode 100644 index 000000000000..f79cfe5339fc --- /dev/null +++ b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/ProfileConverter.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import com.hedera.node.app.spi.config.Profile; +import com.swirlds.config.api.converter.ConfigConverter; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link Profile}. + */ +public class ProfileConverter implements ConfigConverter { + + @Override + public Profile convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + + try { + final int i = Integer.parseInt(value); + if (i == 0) { + return Profile.DEV; + } else if (i == 1) { + return Profile.PROD; + } else if (i == 2) { + return Profile.TEST; + } + } catch (final Exception e) { + // ignore + } + + return Profile.valueOf(value.toUpperCase()); + } +} diff --git a/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/SidecarTypeConverter.java b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/SidecarTypeConverter.java new file mode 100644 index 000000000000..98946a4703cb --- /dev/null +++ b/hedera-node/hedera-app-spi/src/main/java/com/hedera/node/app/spi/config/converter/SidecarTypeConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import com.hedera.hapi.streams.SidecarType; +import com.swirlds.config.api.converter.ConfigConverter; +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link SidecarType}. Based on + * https://github.com/hashgraph/hedera-services/issues/6106 we need to add {@code implements ConfigConverter} to the + * class for now. + */ +public class SidecarTypeConverter extends AbstractEnumConfigConverter + implements ConfigConverter { + + @NonNull + @Override + protected Class getEnumType() { + return SidecarType.class; + } +} diff --git a/hedera-node/hedera-app-spi/src/main/java/module-info.java b/hedera-node/hedera-app-spi/src/main/java/module-info.java index e64d8cf1a44e..19d1ce621a7c 100644 --- a/hedera-node/hedera-app-spi/src/main/java/module-info.java +++ b/hedera-node/hedera-app-spi/src/main/java/module-info.java @@ -17,5 +17,6 @@ exports com.hedera.node.app.spi.validation; exports com.hedera.node.app.spi.accounts; exports com.hedera.node.app.spi.info; + exports com.hedera.node.app.spi.config.converter; exports com.hedera.node.app.spi.config.types; } diff --git a/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/AbstractEnumConfigConverterTest.java b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/AbstractEnumConfigConverterTest.java new file mode 100644 index 000000000000..e94ae8189d5a --- /dev/null +++ b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/AbstractEnumConfigConverterTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.swirlds.common.config.sources.SimpleConfigSource; +import com.swirlds.config.api.Configuration; +import com.swirlds.config.api.ConfigurationBuilder; +import com.swirlds.config.api.converter.ConfigConverter; +import java.lang.annotation.ElementType; +import java.lang.annotation.RetentionPolicy; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +class AbstractEnumConfigConverterTest { + + /** + * Based on https://github.com/hashgraph/hedera-services/issues/6106 we currently need to add ConfigConverter + * explicitly + */ + private static class RetentionPolicyConverter extends AbstractEnumConfigConverter + implements ConfigConverter { + + @Override + protected Class getEnumType() { + return RetentionPolicy.class; + } + } + + /** + * Based on https://github.com/hashgraph/hedera-services/issues/6106 we currently need to add ConfigConverter + * explicitly + */ + private static class ElementTypeConverter extends AbstractEnumConfigConverter + implements ConfigConverter { + + @Override + protected Class getEnumType() { + return ElementType.class; + } + } + + @Test + void testNullValue() { + // given + final RetentionPolicyConverter converter = new RetentionPolicyConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testInvalidValue() { + // given + final RetentionPolicyConverter converter = new RetentionPolicyConverter(); + + // then + assertThatThrownBy(() -> converter.convert("not-supported-value")).isInstanceOf(IllegalArgumentException.class); + } + + @ParameterizedTest + @EnumSource(value = RetentionPolicy.class) + void testValidValue(final RetentionPolicy value) { + // given + final RetentionPolicyConverter converter = new RetentionPolicyConverter(); + + // when + final RetentionPolicy source = converter.convert(value.name()); + + // then + assertThat(source).isEqualTo(value); + } + + @Test + void testIntegration() { + // given + final Configuration configuration = ConfigurationBuilder.create() + .withSource(new SimpleConfigSource("retention-policy", "SOURCE")) + .withSource(new SimpleConfigSource("element-type", "TYPE")) + .withConverter(new RetentionPolicyConverter()) + .withConverter(new ElementTypeConverter()) + .build(); + + // when + final RetentionPolicy source = configuration.getValue("retention-policy", RetentionPolicy.class); + final ElementType type = configuration.getValue("element-type", ElementType.class); + + // then + assertThat(source).isEqualTo(RetentionPolicy.SOURCE); + assertThat(type).isEqualTo(ElementType.TYPE); + } +} diff --git a/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/AccountIDConverterTest.java b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/AccountIDConverterTest.java new file mode 100644 index 000000000000..5e6ba491973b --- /dev/null +++ b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/AccountIDConverterTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class AccountIDConverterTest { + + @Test + void testNullParam() { + // given + final AccountIDConverter converter = new AccountIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @ParameterizedTest + @ValueSource( + strings = { + "", " ", " ", "a.b.b", "1.b.c", "1.2.c", "a.2.3", "1", "1.2", ".1.2.3", "..1.2.3", ".1.2.3.", "1.2.3.4" + }) + void testAllNotParseable(final String input) { + // given + final AccountIDConverter converter = new AccountIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(input)).isInstanceOf(IllegalArgumentException.class); + } + + /** + * The given tests does not work. From my point of view the expected behavior is to throw an exception but the + * values would be converted without an issue. We should have a look at this in future. + * + * @param input + */ + @Disabled + @ParameterizedTest + @ValueSource(strings = {"1.2.3.", "1.2.3.."}) + void testEdgeCasesForDiscussion(final String input) { + // given + final AccountIDConverter converter = new AccountIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(input)).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void testSimpleValue() { + // given + final AccountIDConverter converter = new AccountIDConverter(); + final String value = "1.2.3"; + + // when + final var result = converter.convert(value); + + // then + assertThat(result).isNotNull(); + assertThat(result.shardNum()).isEqualTo(1L); + assertThat(result.realmNum()).isEqualTo(2L); + assertThat(result.accountNum()).isEqualTo(3L); + } + + @Test + void testLongValues() { + // given + final AccountIDConverter converter = new AccountIDConverter(); + final String value = Long.MAX_VALUE + "." + Long.MAX_VALUE + ".0"; + + // when + final var result = converter.convert(value); + + // then + assertThat(result).isNotNull(); + assertThat(result.shardNum()).isEqualTo(Long.MAX_VALUE); + assertThat(result.realmNum()).isEqualTo(Long.MAX_VALUE); + assertThat(result.accountNum()).isZero(); + } +} diff --git a/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/ContractIDConverterTest.java b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/ContractIDConverterTest.java new file mode 100644 index 000000000000..56ea472e67c3 --- /dev/null +++ b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/ContractIDConverterTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class ContractIDConverterTest { + + @Test + void testNullParam() { + // given + final ContractIDConverter converter = new ContractIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @ParameterizedTest + @ValueSource( + strings = { + "", " ", " ", "a.b.b", "1.b.c", "1.2.c", "a.2.3", "1", "1.2", ".1.2.3", "..1.2.3", ".1.2.3.", "1.2.3.4" + }) + void testAllNotParseable(final String input) { + // given + final ContractIDConverter converter = new ContractIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(input)).isInstanceOf(IllegalArgumentException.class); + } + + @Disabled + @ParameterizedTest + @ValueSource(strings = {"1.2.3.", "1.2.3.."}) + void testEdgeCasesForDiscussion(final String input) { + // given + final ContractIDConverter converter = new ContractIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(input)).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void testSimpleValue() { + // given + final ContractIDConverter converter = new ContractIDConverter(); + final String value = "1.2.3"; + + // when + final var result = converter.convert(value); + + // then + assertThat(result).isNotNull(); + assertThat(result.shardNum()).isEqualTo(1L); + assertThat(result.realmNum()).isEqualTo(2L); + assertThat(result.contractNum()).isEqualTo(3L); + } + + @Test + void testLongValues() { + // given + final ContractIDConverter converter = new ContractIDConverter(); + final String value = Long.MAX_VALUE + "." + Long.MAX_VALUE + ".0"; + + // when + final var result = converter.convert(value); + + // then + assertThat(result).isNotNull(); + assertThat(result.shardNum()).isEqualTo(Long.MAX_VALUE); + assertThat(result.realmNum()).isEqualTo(Long.MAX_VALUE); + assertThat(result.contractNum()).isZero(); + } +} diff --git a/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/FileIDConverterTest.java b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/FileIDConverterTest.java new file mode 100644 index 000000000000..64115f08d66e --- /dev/null +++ b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/FileIDConverterTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class FileIDConverterTest { + + @Test + void testNullParam() { + // given + final FileIDConverter converter = new FileIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @ParameterizedTest + @ValueSource( + strings = { + "", " ", " ", "a.b.b", "1.b.c", "1.2.c", "a.2.3", "1", "1.2", ".1.2.3", "..1.2.3", ".1.2.3.", "1.2.3.4" + }) + void testAllNotParseable(final String input) { + // given + final FileIDConverter converter = new FileIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(input)).isInstanceOf(IllegalArgumentException.class); + } + + @Disabled + @ParameterizedTest + @ValueSource(strings = {"1.2.3.", "1.2.3.."}) + void testEdgeCasesForDiscussion(final String input) { + // given + final FileIDConverter converter = new FileIDConverter(); + + // then + assertThatThrownBy(() -> converter.convert(input)).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void testSimpleValue() { + // given + final FileIDConverter converter = new FileIDConverter(); + final String value = "1.2.3"; + + // when + final var result = converter.convert(value); + + // then + assertThat(result).isNotNull(); + assertThat(result.shardNum()).isEqualTo(1L); + assertThat(result.realmNum()).isEqualTo(2L); + assertThat(result.fileNum()).isEqualTo(3L); + } + + @Test + void testLongValues() { + // given + final FileIDConverter converter = new FileIDConverter(); + final String value = Long.MAX_VALUE + "." + Long.MAX_VALUE + ".0"; + + // when + final var result = converter.convert(value); + + // then + assertThat(result).isNotNull(); + assertThat(result.shardNum()).isEqualTo(Long.MAX_VALUE); + assertThat(result.realmNum()).isEqualTo(Long.MAX_VALUE); + assertThat(result.fileNum()).isZero(); + } +} diff --git a/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/HederaFunctionalityConverterTest.java b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/HederaFunctionalityConverterTest.java new file mode 100644 index 000000000000..b9600a47bfac --- /dev/null +++ b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/HederaFunctionalityConverterTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.hapi.node.base.HederaFunctionality; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +class HederaFunctionalityConverterTest { + + @Test + void testNullParam() { + // given + final HederaFunctionalityConverter converter = new HederaFunctionalityConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testInvalidParam() { + // given + final HederaFunctionalityConverter converter = new HederaFunctionalityConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } + + @ParameterizedTest + @EnumSource(HederaFunctionality.class) + void testValidParam(final HederaFunctionality functionality) { + // given + final HederaFunctionalityConverter converter = new HederaFunctionalityConverter(); + + // when + final HederaFunctionality cryptoTransfer = converter.convert(functionality.name()); + + // then + assertThat(cryptoTransfer).isEqualTo(functionality); + } +} diff --git a/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/ProfileConverterTest.java b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/ProfileConverterTest.java new file mode 100644 index 000000000000..fd60b28413bd --- /dev/null +++ b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/ProfileConverterTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.spi.config.Profile; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +class ProfileConverterTest { + + @Test + void testNullParam() { + // given + final ProfileConverter converter = new ProfileConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testInvalidParam() { + // given + final ProfileConverter converter = new ProfileConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } + + @ParameterizedTest + @CsvSource({ + "DEV, DEV", + "TEST, TEST", + "PROD, PROD", + "dev, DEV", + "test, TEST", + "prod, PROD", + "dEv, DEV", + "tEst, TEST", + "pRod, PROD", + "0,DEV", + "2,TEST", + "1,PROD" + }) + void testValidParam(final String input, final String enumName) { + // given + final ProfileConverter converter = new ProfileConverter(); + final Profile expected = Profile.valueOf(enumName); + + // when + final Profile cryptoTransfer = converter.convert(input); + + // then + assertThat(cryptoTransfer).isEqualTo(expected); + } +} diff --git a/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/SidecarTypeConverterTest.java b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/SidecarTypeConverterTest.java new file mode 100644 index 000000000000..85f1de6bf6a4 --- /dev/null +++ b/hedera-node/hedera-app-spi/src/test/java/com/hedera/node/app/spi/config/converter/SidecarTypeConverterTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2023 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.node.app.spi.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.hapi.streams.SidecarType; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +class SidecarTypeConverterTest { + + @Test + void testNullParam() { + // given + final SidecarTypeConverter converter = new SidecarTypeConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testInvalidParam() { + // given + final SidecarTypeConverter converter = new SidecarTypeConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } + + @ParameterizedTest + @EnumSource(SidecarType.class) + void testValidParam(final SidecarType value) { + // given + final SidecarTypeConverter converter = new SidecarTypeConverter(); + + // when + final SidecarType sidecarType = converter.convert(value.name()); + + // then + assertThat(sidecarType).isEqualTo(value); + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/CongestionMultipliersConverter.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/CongestionMultipliersConverter.java new file mode 100644 index 000000000000..e2f12cb290a6 --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/CongestionMultipliersConverter.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import com.hedera.node.app.service.mono.fees.calculation.CongestionMultipliers; +import com.swirlds.config.api.converter.ConfigConverter; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link CongestionMultipliers}. + */ +public class CongestionMultipliersConverter implements ConfigConverter { + + @Override + public CongestionMultipliers convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + return CongestionMultipliers.from(value); + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/EntityScaleFactorsConverter.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/EntityScaleFactorsConverter.java new file mode 100644 index 000000000000..5751dca7c66c --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/EntityScaleFactorsConverter.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import com.hedera.node.app.service.mono.fees.calculation.EntityScaleFactors; +import com.swirlds.config.api.converter.ConfigConverter; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link EntityScaleFactors}. + */ +public class EntityScaleFactorsConverter implements ConfigConverter { + + @Override + public EntityScaleFactors convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + return EntityScaleFactors.from(value); + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/EntityTypeConverter.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/EntityTypeConverter.java new file mode 100644 index 000000000000..f17a327b645c --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/EntityTypeConverter.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import com.hedera.node.app.service.mono.context.properties.EntityType; +import com.hedera.node.app.spi.config.converter.AbstractEnumConfigConverter; +import com.swirlds.config.api.converter.ConfigConverter; +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link EntityType}. Based on + * https://github.com/hashgraph/hedera-services/issues/6106 we need to add {@code implements ConfigConverter} to the + * class for now. + */ +public class EntityTypeConverter extends AbstractEnumConfigConverter + implements ConfigConverter { + + @NonNull + @Override + protected Class getEnumType() { + return EntityType.class; + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/KnownBlockValuesConverter.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/KnownBlockValuesConverter.java new file mode 100644 index 000000000000..ebacab5ac2cf --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/KnownBlockValuesConverter.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import com.hedera.node.app.hapi.utils.sysfiles.domain.KnownBlockValues; +import com.swirlds.config.api.converter.ConfigConverter; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link KnownBlockValues}. + */ +public class KnownBlockValuesConverter implements ConfigConverter { + + @Override + public KnownBlockValues convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + return KnownBlockValues.from(value); + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/LegacyContractIdActivationsConverter.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/LegacyContractIdActivationsConverter.java new file mode 100644 index 000000000000..79e556cac9bf --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/LegacyContractIdActivationsConverter.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import com.hedera.node.app.service.mono.keys.LegacyContractIdActivations; +import com.swirlds.config.api.converter.ConfigConverter; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link LegacyContractIdActivations}. + */ +public class LegacyContractIdActivationsConverter implements ConfigConverter { + + @Override + public LegacyContractIdActivations convert(final String value) + throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + return LegacyContractIdActivations.from(value); + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/MapAccessTypeConverter.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/MapAccessTypeConverter.java new file mode 100644 index 000000000000..47abe883a375 --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/MapAccessTypeConverter.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import com.hedera.node.app.service.mono.throttling.MapAccessType; +import com.hedera.node.app.spi.config.converter.AbstractEnumConfigConverter; +import com.swirlds.config.api.converter.ConfigConverter; +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link MapAccessType}. Based on + * https://github.com/hashgraph/hedera-services/issues/6106 we need to add {@code implements ConfigConverter} to the + * class for now. + */ +public class MapAccessTypeConverter extends AbstractEnumConfigConverter + implements ConfigConverter { + @NonNull + @Override + protected Class getEnumType() { + return MapAccessType.class; + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/RecomputeTypeConverter.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/RecomputeTypeConverter.java new file mode 100644 index 000000000000..65ccd9a9a035 --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/RecomputeTypeConverter.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import com.hedera.node.app.service.mono.ledger.accounts.staking.StakeStartupHelper.RecomputeType; +import com.hedera.node.app.spi.config.converter.AbstractEnumConfigConverter; +import com.swirlds.config.api.converter.ConfigConverter; +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link RecomputeType}. Based on + * https://github.com/hashgraph/hedera-services/issues/6106 we need to add {@code implements ConfigConverter} to the + * class for now. + */ +public class RecomputeTypeConverter extends AbstractEnumConfigConverter + implements ConfigConverter { + + @NonNull + @Override + protected Class getEnumType() { + return RecomputeType.class; + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/ScaleFactorConverter.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/ScaleFactorConverter.java new file mode 100644 index 000000000000..5e9be55f207b --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/converter/ScaleFactorConverter.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import com.hedera.node.app.hapi.utils.sysfiles.domain.throttling.ScaleFactor; +import com.swirlds.config.api.converter.ConfigConverter; + +/** + * Config api {@link ConfigConverter} implementation for the type {@link ScaleFactor}. + */ +public class ScaleFactorConverter implements ConfigConverter { + + @Override + public ScaleFactor convert(final String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("null can not be converted"); + } + return ScaleFactor.from(value); + } +} diff --git a/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/source/PropertySourceBasedConfigSource.java b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/source/PropertySourceBasedConfigSource.java new file mode 100644 index 000000000000..983fc895367b --- /dev/null +++ b/hedera-node/hedera-app/src/main/java/com/hedera/node/app/config/source/PropertySourceBasedConfigSource.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2023 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.node.app.config.source; + +import com.hedera.node.app.service.mono.context.properties.PropertySource; +import com.swirlds.config.api.source.ConfigSource; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.Set; + +/** + * A {@link ConfigSource} that wraps a {@link PropertySource} and redirects all calls to the {@link PropertySource}. + */ +public class PropertySourceBasedConfigSource implements ConfigSource { + + private final PropertySource propertySource; + + /** + * Creates a new instance + * + * @param propertySource the property source + */ + public PropertySourceBasedConfigSource(@NonNull final PropertySource propertySource) { + this.propertySource = Objects.requireNonNull(propertySource, "propertySource"); + } + + @Override + @NonNull + public Set getPropertyNames() { + return propertySource.allPropertyNames(); + } + + @Override + @Nullable + public String getValue(@NonNull final String name) throws NoSuchElementException { + return propertySource.getRawValue(name); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/PropertySourceBasedConfigTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/PropertySourceBasedConfigTest.java new file mode 100644 index 000000000000..c530faea2b7c --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/PropertySourceBasedConfigTest.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2023 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.node.app.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.hedera.hapi.node.base.AccountID; +import com.hedera.hapi.node.base.ContractID; +import com.hedera.hapi.node.base.FileID; +import com.hedera.hapi.node.base.HederaFunctionality; +import com.hedera.hapi.streams.SidecarType; +import com.hedera.node.app.config.converter.CongestionMultipliersConverter; +import com.hedera.node.app.config.converter.EntityScaleFactorsConverter; +import com.hedera.node.app.config.converter.EntityTypeConverter; +import com.hedera.node.app.config.converter.KnownBlockValuesConverter; +import com.hedera.node.app.config.converter.LegacyContractIdActivationsConverter; +import com.hedera.node.app.config.converter.MapAccessTypeConverter; +import com.hedera.node.app.config.converter.RecomputeTypeConverter; +import com.hedera.node.app.config.converter.ScaleFactorConverter; +import com.hedera.node.app.config.source.PropertySourceBasedConfigSource; +import com.hedera.node.app.hapi.utils.sysfiles.domain.KnownBlockValues; +import com.hedera.node.app.hapi.utils.sysfiles.domain.throttling.ScaleFactor; +import com.hedera.node.app.service.mono.context.properties.EntityType; +import com.hedera.node.app.service.mono.context.properties.PropertySource; +import com.hedera.node.app.service.mono.fees.calculation.CongestionMultipliers; +import com.hedera.node.app.service.mono.fees.calculation.EntityScaleFactors; +import com.hedera.node.app.service.mono.keys.LegacyContractIdActivations; +import com.hedera.node.app.service.mono.ledger.accounts.staking.StakeStartupHelper.RecomputeType; +import com.hedera.node.app.service.mono.throttling.MapAccessType; +import com.hedera.node.app.spi.config.Profile; +import com.hedera.node.app.spi.config.converter.AccountIDConverter; +import com.hedera.node.app.spi.config.converter.ContractIDConverter; +import com.hedera.node.app.spi.config.converter.FileIDConverter; +import com.hedera.node.app.spi.config.converter.HederaFunctionalityConverter; +import com.hedera.node.app.spi.config.converter.ProfileConverter; +import com.hedera.node.app.spi.config.converter.SidecarTypeConverter; +import com.swirlds.config.api.Configuration; +import com.swirlds.config.api.ConfigurationBuilder; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.BDDMockito; +import org.mockito.Mock; +import org.mockito.Mock.Strictness; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class PropertySourceBasedConfigTest { + + @Mock(strictness = Strictness.LENIENT) + private PropertySource propertySource; + + private final Map rawData = new HashMap<>(); + + @BeforeEach + void configureMockForConfigData() { + rawData.put("test.congestionMultipliers", "90,11x,95,27x,99,103x"); + rawData.put("test.entityScaleFactors", "DEFAULT(32,7:1)"); + rawData.put("test.entityType", "CONTRACT"); + rawData.put("test.knownBlockValues", "c9e37a7a454638ca62662bd1a06de49ef40b3444203fe329bbc81363604ea7f8@666"); + rawData.put("test.legacyContractIdActivations", "1058134by[1062784|2173895],857111by[522000|365365]"); + rawData.put("test.mapAccessType", "ACCOUNTS_GET"); + rawData.put("test.recomputeType", "PENDING_REWARDS"); + rawData.put("test.scaleFactor", "1:3"); + rawData.put("test.accountID", "1.2.3"); + rawData.put("test.contractID", "1.2.3"); + rawData.put("test.fileID", "1.2.3"); + rawData.put("test.hederaFunctionality", "CRYPTO_TRANSFER"); + rawData.put("test.profile", "DEV"); + rawData.put("test.sidecarType", "CONTRACT_ACTION"); + + BDDMockito.given(propertySource.allPropertyNames()).willReturn(rawData.keySet()); + rawData.forEach((key, value) -> + BDDMockito.given(propertySource.getRawValue(key)).willReturn(value)); + } + + @Test + void testConfig() { + // given + final Configuration configuration = ConfigurationBuilder.create() + .withConverter(new CongestionMultipliersConverter()) + .withConverter(new EntityScaleFactorsConverter()) + .withConverter(new EntityTypeConverter()) + .withConverter(new KnownBlockValuesConverter()) + .withConverter(new LegacyContractIdActivationsConverter()) + .withConverter(new MapAccessTypeConverter()) + .withConverter(new RecomputeTypeConverter()) + .withConverter(new ScaleFactorConverter()) + .withConverter(new AccountIDConverter()) + .withConverter(new ContractIDConverter()) + .withConverter(new FileIDConverter()) + .withConverter(new HederaFunctionalityConverter()) + .withConverter(new ProfileConverter()) + .withConverter(new SidecarTypeConverter()) + .withSource(new PropertySourceBasedConfigSource(propertySource)) + .build(); + + // when + final CongestionMultipliers congestionMultipliers = + configuration.getValue("test.congestionMultipliers", CongestionMultipliers.class); + final EntityScaleFactors entityScaleFactors = + configuration.getValue("test.entityScaleFactors", EntityScaleFactors.class); + final EntityType entityType = configuration.getValue("test.entityType", EntityType.class); + final KnownBlockValues knownBlockValues = + configuration.getValue("test.knownBlockValues", KnownBlockValues.class); + final LegacyContractIdActivations legacyContractIdActivations = + configuration.getValue("test.legacyContractIdActivations", LegacyContractIdActivations.class); + final MapAccessType mapAccessType = configuration.getValue("test.mapAccessType", MapAccessType.class); + final RecomputeType recomputeType = configuration.getValue("test.recomputeType", RecomputeType.class); + final ScaleFactor scaleFactor = configuration.getValue("test.scaleFactor", ScaleFactor.class); + final AccountID accountID = configuration.getValue("test.accountID", AccountID.class); + final ContractID contractID = configuration.getValue("test.contractID", ContractID.class); + final FileID fileID = configuration.getValue("test.fileID", FileID.class); + final HederaFunctionality hederaFunctionality = + configuration.getValue("test.hederaFunctionality", HederaFunctionality.class); + final Profile profile = configuration.getValue("test.profile", Profile.class); + final SidecarType sidecarType = configuration.getValue("test.sidecarType", SidecarType.class); + + // then + assertThat(congestionMultipliers).isNotNull(); + assertThat(entityScaleFactors).isNotNull(); + assertThat(entityType).isNotNull(); + assertThat(knownBlockValues).isNotNull(); + assertThat(legacyContractIdActivations).isNotNull(); + assertThat(mapAccessType).isNotNull(); + assertThat(recomputeType).isNotNull(); + assertThat(scaleFactor).isNotNull(); + assertThat(accountID).isNotNull(); + assertThat(contractID).isNotNull(); + assertThat(fileID).isNotNull(); + assertThat(hederaFunctionality).isNotNull(); + assertThat(profile).isNotNull(); + assertThat(sidecarType).isNotNull(); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/CongestionMultipliersConverterTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/CongestionMultipliersConverterTest.java new file mode 100644 index 000000000000..45e0d508cb91 --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/CongestionMultipliersConverterTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.service.mono.fees.calculation.CongestionMultipliers; +import org.junit.jupiter.api.Test; + +class CongestionMultipliersConverterTest { + + @Test + void testNullValue() { + // given + final CongestionMultipliersConverter converter = new CongestionMultipliersConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testValidValue() { + // given + final CongestionMultipliersConverter converter = new CongestionMultipliersConverter(); + final String input = "90,10x,95,25x,99,100x"; + + // when + final CongestionMultipliers multipliers = converter.convert(input); + + // then + assertThat(multipliers).isNotNull(); + assertThat(multipliers.usagePercentTriggers()).containsExactly(90, 95, 99); + assertThat(multipliers.multipliers()).containsExactly(10L, 25L, 100L); + } + + @Test + void testInvalidValue() { + // given + final CongestionMultipliersConverter converter = new CongestionMultipliersConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/EntityScaleFactorsConverterTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/EntityScaleFactorsConverterTest.java new file mode 100644 index 000000000000..748b815318c6 --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/EntityScaleFactorsConverterTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.hapi.utils.sysfiles.domain.throttling.ScaleFactor; +import com.hedera.node.app.service.mono.fees.calculation.EntityScaleFactors; +import org.junit.jupiter.api.Test; + +class EntityScaleFactorsConverterTest { + + @Test + void testNullValue() { + // given + final EntityScaleFactorsConverter converter = new EntityScaleFactorsConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testValidValue() { + // given + final EntityScaleFactorsConverter converter = new EntityScaleFactorsConverter(); + final String input = "DEFAULT(90,10:1,95,25:1,99,100:1)"; + + // when + final EntityScaleFactors entityScaleFactors = converter.convert(input); + + // then + assertThat(entityScaleFactors).isNotNull(); + assertThat(entityScaleFactors.typeScaleFactors()).isEmpty(); + assertThat(entityScaleFactors.defaultScaleFactors()).isNotNull(); + assertThat(entityScaleFactors.defaultScaleFactors().usagePercentTriggers()) + .containsExactly(90, 95, 99); + assertThat(entityScaleFactors.defaultScaleFactors().scaleFactors()) + .containsExactly(ScaleFactor.from("10:1"), ScaleFactor.from("25:1"), ScaleFactor.from("100:1")); + } + + @Test + void testEmptyValue() { + // given + final EntityScaleFactorsConverter converter = new EntityScaleFactorsConverter(); + final String input = ""; + + // when + final EntityScaleFactors entityScaleFactors = converter.convert(input); + + // then + assertThat(entityScaleFactors).isNotNull(); + assertThat(entityScaleFactors.typeScaleFactors()).isEmpty(); + assertThat(entityScaleFactors.defaultScaleFactors()).isNotNull(); + assertThat(entityScaleFactors.defaultScaleFactors().usagePercentTriggers()) + .containsExactly(0); + assertThat(entityScaleFactors.defaultScaleFactors().scaleFactors()).containsExactly(ScaleFactor.from("1:1")); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/EntityTypeConverterTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/EntityTypeConverterTest.java new file mode 100644 index 000000000000..e6bf1315636e --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/EntityTypeConverterTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.service.mono.context.properties.EntityType; +import org.junit.jupiter.api.Test; + +class EntityTypeConverterTest { + + @Test + void testNullParam() { + // given + final EntityTypeConverter converter = new EntityTypeConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testInvalidParam() { + // given + final EntityTypeConverter converter = new EntityTypeConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void testValidParam() { + // given + final EntityTypeConverter converter = new EntityTypeConverter(); + + // when + final EntityType entityType = converter.convert("ACCOUNT"); + + // then + assertThat(entityType).isEqualTo(EntityType.ACCOUNT); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/KnownBlockValuesConverterTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/KnownBlockValuesConverterTest.java new file mode 100644 index 000000000000..03e0a094b947 --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/KnownBlockValuesConverterTest.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.hapi.utils.sysfiles.domain.KnownBlockValues; +import org.junit.jupiter.api.Test; + +class KnownBlockValuesConverterTest { + + @Test + void testNullValue() { + // given + final KnownBlockValuesConverter converter = new KnownBlockValuesConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testValidValue() { + // given + final KnownBlockValuesConverter converter = new KnownBlockValuesConverter(); + final String input = "c9e37a7a454638ca62662bd1a06de49ef40b3444203fe329bbc81363604ea7f8@666"; + + // when + final KnownBlockValues knownBlockValues = converter.convert(input); + + // then + assertThat(knownBlockValues).isNotNull(); + assertThat(knownBlockValues.number()).isEqualTo(666L); + assertThat(knownBlockValues.hash()) + .asHexString() + .isEqualTo("C9E37A7A454638CA62662BD1A06DE49EF40B3444203FE329BBC81363604EA7F8"); + } + + @Test + void testInvalidValue() { + // given + final KnownBlockValuesConverter converter = new KnownBlockValuesConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/LegacyContractIdActivationsConverterTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/LegacyContractIdActivationsConverterTest.java new file mode 100644 index 000000000000..13ce589f6f19 --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/LegacyContractIdActivationsConverterTest.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.service.mono.keys.LegacyContractIdActivations; +import java.util.Set; +import org.hyperledger.besu.datatypes.Address; +import org.junit.jupiter.api.Test; + +class LegacyContractIdActivationsConverterTest { + + @Test + void testNullValue() { + // given + final LegacyContractIdActivationsConverter converter = new LegacyContractIdActivationsConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testValidValue() { + // given + final LegacyContractIdActivationsConverter converter = new LegacyContractIdActivationsConverter(); + final String input = "1058134by[1062784]"; + + // when + final LegacyContractIdActivations contractIdActivations = converter.convert(input); + + // then + assertThat(contractIdActivations).isNotNull(); + assertThat(contractIdActivations.privilegedContracts()) + .hasSize(1) + .containsEntry( + Address.fromHexString("0x0000000000000000000000000000000000102556"), + Set.of(Address.fromHexString("0x0000000000000000000000000000000000103780"))); + } + + @Test + void testInvalidValue() { + // given + final LegacyContractIdActivationsConverter converter = new LegacyContractIdActivationsConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/MapAccessTypeConverterTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/MapAccessTypeConverterTest.java new file mode 100644 index 000000000000..2a9b59504ece --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/MapAccessTypeConverterTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.service.mono.throttling.MapAccessType; +import org.junit.jupiter.api.Test; + +class MapAccessTypeConverterTest { + + @Test + void testNullParam() { + // given + final MapAccessTypeConverter converter = new MapAccessTypeConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testInvalidParam() { + // given + final MapAccessTypeConverter converter = new MapAccessTypeConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void testValidParam() { + // given + final MapAccessTypeConverter converter = new MapAccessTypeConverter(); + + // when + final MapAccessType entityType = converter.convert("ACCOUNTS_GET"); + + // then + assertThat(entityType).isEqualTo(MapAccessType.ACCOUNTS_GET); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/RecomputeTypeConverterTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/RecomputeTypeConverterTest.java new file mode 100644 index 000000000000..3db782523f62 --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/RecomputeTypeConverterTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.service.mono.ledger.accounts.staking.StakeStartupHelper.RecomputeType; +import org.junit.jupiter.api.Test; + +class RecomputeTypeConverterTest { + + @Test + void testNullParam() { + // given + final RecomputeTypeConverter converter = new RecomputeTypeConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testInvalidParam() { + // given + final RecomputeTypeConverter converter = new RecomputeTypeConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void testValidParam() { + // given + final RecomputeTypeConverter converter = new RecomputeTypeConverter(); + + // when + final RecomputeType entityType = converter.convert("NODE_STAKES"); + + // then + assertThat(entityType).isEqualTo(RecomputeType.NODE_STAKES); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/ScaleFactorConverterTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/ScaleFactorConverterTest.java new file mode 100644 index 000000000000..4a22f1f22e53 --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/converter/ScaleFactorConverterTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2023 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.node.app.config.converter; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.hapi.utils.sysfiles.domain.throttling.ScaleFactor; +import org.junit.jupiter.api.Test; + +class ScaleFactorConverterTest { + + @Test + void testNullValue() { + // given + final ScaleFactorConverter converter = new ScaleFactorConverter(); + + // then + assertThatThrownBy(() -> converter.convert(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testValidValue() { + // given + final ScaleFactorConverter converter = new ScaleFactorConverter(); + final String input = "1:3"; + + // when + final ScaleFactor scaleFactor = converter.convert(input); + + // then + assertThat(scaleFactor).isNotNull(); + assertThat(scaleFactor.numerator()).isEqualTo(1); + assertThat(scaleFactor.denominator()).isEqualTo(3); + } + + @Test + void testInvalidValue() { + // given + final ScaleFactorConverter converter = new ScaleFactorConverter(); + + // then + assertThatThrownBy(() -> converter.convert("null")).isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/source/PropertySourceBasedConfigSourceTest.java b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/source/PropertySourceBasedConfigSourceTest.java new file mode 100644 index 000000000000..01a9adb0057d --- /dev/null +++ b/hedera-node/hedera-app/src/test/java/com/hedera/node/app/config/source/PropertySourceBasedConfigSourceTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2023 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.node.app.config.source; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.hedera.node.app.service.mono.context.properties.PropertySource; +import java.util.Set; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.BDDMockito; +import org.mockito.Mock; +import org.mockito.Mock.Strictness; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class PropertySourceBasedConfigSourceTest { + + @Mock(strictness = Strictness.LENIENT) + private PropertySource propertySource; + + @BeforeEach + void configureMockForConfigData() { + BDDMockito.given(propertySource.allPropertyNames()).willReturn(Set.of("a", "b", "c")); + BDDMockito.given(propertySource.getRawValue("a")).willReturn("result-a"); + BDDMockito.given(propertySource.getRawValue("b")).willReturn("result-b"); + BDDMockito.given(propertySource.getRawValue("c")).willReturn("result-c"); + } + + @Test + void testNullParam() { + assertThatThrownBy(() -> new PropertySourceBasedConfigSource(null)).isInstanceOf(NullPointerException.class); + } + + @Test + void testUsageParam() { + final PropertySourceBasedConfigSource configSource = new PropertySourceBasedConfigSource(propertySource); + + assertThat(configSource.getPropertyNames()).hasSize(3).contains("a", "b", "c"); + assertThat(configSource.getValue("a")).isEqualTo("result-a"); + assertThat(configSource.getValue("b")).isEqualTo("result-b"); + assertThat(configSource.getValue("c")).isEqualTo("result-c"); + } +}