From ef7f0a9d623a2b899e69d348fd308e94621bff3a Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Tue, 3 Sep 2024 17:19:35 -0600 Subject: [PATCH] Removed ConfigProvider, since we will be injecting the config we don't need a provider. And refactored code around that, also divided the main entry point from the ActualApp so we can only ignore the entry point in the unit test coverage. Added Missing unit tests Signed-off-by: Alfredo Gutierrez --- codecov.yml | 1 + .../block/simulator/BlockStreamSimulator.java | 72 ++++++------------- .../simulator/BlockStreamSimulatorApp.java | 71 ++++++++++++++++++ ...lockStreamSimulatorInjectionComponent.java | 2 +- .../simulator/config/ConfigProviderImpl.java | 64 ----------------- .../simulator/BlockStreamSimulatorTest.java | 6 +- .../config/ConfigInjectionModuleTest.java | 66 +++++++++++++++++ .../config/types/GenerationModeTest.java} | 25 ++++--- .../generator/BlockStreamManagerTest.java | 30 ++++++++ 9 files changed, 205 insertions(+), 132 deletions(-) create mode 100644 simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java delete mode 100644 simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java create mode 100644 simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java rename simulator/src/{main/java/com/hedera/block/simulator/config/ConfigProvider.java => test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java} (55%) create mode 100644 simulator/src/test/java/com/hedera/block/simulator/generator/BlockStreamManagerTest.java diff --git a/codecov.yml b/codecov.yml index e21c9366..620cc39a 100644 --- a/codecov.yml +++ b/codecov.yml @@ -21,3 +21,4 @@ coverage: ignore: - "server/src/main/java/com/hedera/block/server/Server.java" - "server/src/main/java/com/hedera/block/server/Translator.java" + - "simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java" diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java index d7dd10d8..5344f689 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -16,75 +16,45 @@ package com.hedera.block.simulator; -import com.hedera.block.simulator.config.ConfigProvider; -import com.hedera.block.simulator.config.ConfigProviderImpl; -import com.hedera.block.simulator.generator.BlockStreamManager; import com.swirlds.config.api.Configuration; -import edu.umd.cs.findbugs.annotations.NonNull; +import com.swirlds.config.api.ConfigurationBuilder; +import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; +import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; +import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource; +import java.io.IOException; import java.lang.System.Logger; -import javax.inject.Inject; +import java.nio.file.Path; /** The BlockStreamSimulator class defines the simulator for the block stream. */ public class BlockStreamSimulator { private static final Logger LOGGER = System.getLogger(BlockStreamSimulator.class.getName()); - Configuration configuration; - BlockStreamManager blockStreamManager; - boolean isRunning = false; - - /** - * Creates a new BlockStreamSimulator instance. - * - * @param configuration the configuration to be used by the block stream simulator - * @param blockStreamManager the block stream manager to be used by the block stream simulator - */ - @Inject - public BlockStreamSimulator( - @NonNull Configuration configuration, @NonNull BlockStreamManager blockStreamManager) { - this.configuration = configuration; - this.blockStreamManager = blockStreamManager; - } + /** This constructor should not be instantiated. */ + private BlockStreamSimulator() {} /** * The main entry point for the block stream simulator. * * @param args the arguments to be passed to the block stream simulator + * @throws IOException if an I/O error occurs */ - public static void main(String[] args) { + public static void main(String[] args) throws IOException { LOGGER.log(Logger.Level.INFO, "Starting Block Stream Simulator"); - ConfigProvider configProvider = new ConfigProviderImpl(); - Configuration configuration = configProvider.getConfiguration(); - BlockStreamSimulatorInjectionComponent DIComponent = - DaggerBlockStreamSimulatorInjectionComponent.factory().create(configuration); - - BlockStreamSimulator blockStreamSimulator = DIComponent.getBlockStreamSimulator(); - blockStreamSimulator.start(); - } - - /** Starts the block stream simulator. */ - public void start() { - - // use blockStreamManager to get block stream + ConfigurationBuilder configurationBuilder = + ConfigurationBuilder.create() + .withSource(SystemEnvironmentConfigSource.getInstance()) + .withSource(SystemPropertiesConfigSource.getInstance()) + .withSource(new ClasspathFileConfigSource(Path.of("app.properties"))) + .autoDiscoverExtensions(); - // use PublishStreamGrpcClient to stream it to the block-node. - isRunning = true; - LOGGER.log(Logger.Level.INFO, "Block Stream Simulator has started"); - } + Configuration configuration = configurationBuilder.build(); - /** - * Returns whether the block stream simulator is running. - * - * @return true if the block stream simulator is running, false otherwise - */ - public boolean isRunning() { - return isRunning; - } + BlockStreamSimulatorInjectionComponent DIComponent = + DaggerBlockStreamSimulatorInjectionComponent.factory().create(configuration); - /** Stops the block stream simulator. */ - public void stop() { - isRunning = false; - LOGGER.log(Logger.Level.INFO, "Block Stream Simulator has stopped"); + BlockStreamSimulatorApp blockStreamSimulatorApp = DIComponent.getBlockStreamSimulatorApp(); + blockStreamSimulatorApp.start(); } } diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java new file mode 100644 index 00000000..7c1f6cad --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorApp.java @@ -0,0 +1,71 @@ +/* + * 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.simulator; + +import com.hedera.block.simulator.generator.BlockStreamManager; +import com.swirlds.config.api.Configuration; +import edu.umd.cs.findbugs.annotations.NonNull; +import javax.inject.Inject; + +/** BlockStream Simulator App */ +public class BlockStreamSimulatorApp { + + private static final System.Logger LOGGER = + System.getLogger(BlockStreamSimulatorApp.class.getName()); + + Configuration configuration; + BlockStreamManager blockStreamManager; + boolean isRunning = false; + + /** + * Creates a new BlockStreamSimulatorApp instance. + * + * @param configuration the configuration to be used by the block stream simulator + * @param blockStreamManager the block stream manager to be used by the block stream simulator + */ + @Inject + public BlockStreamSimulatorApp( + @NonNull Configuration configuration, @NonNull BlockStreamManager blockStreamManager) { + this.configuration = configuration; + this.blockStreamManager = blockStreamManager; + } + + /** Starts the block stream simulator. */ + public void start() { + + // use blockStreamManager to get block stream + + // use PublishStreamGrpcClient to stream it to the block-node. + isRunning = true; + LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has started"); + } + + /** + * Returns whether the block stream simulator is running. + * + * @return true if the block stream simulator is running, false otherwise + */ + public boolean isRunning() { + return isRunning; + } + + /** Stops the block stream simulator. */ + public void stop() { + isRunning = false; + LOGGER.log(System.Logger.Level.INFO, "Block Stream Simulator has stopped"); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java index ef8e4fe8..cf5589e1 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java @@ -37,7 +37,7 @@ public interface BlockStreamSimulatorInjectionComponent { * * @return the block stream simulator */ - BlockStreamSimulator getBlockStreamSimulator(); + BlockStreamSimulatorApp getBlockStreamSimulatorApp(); /** The factory used to create the block stream simulator injection component. */ @Component.Factory diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java deleted file mode 100644 index ddd89ce7..00000000 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProviderImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.simulator.config; - -import com.swirlds.config.api.Configuration; -import com.swirlds.config.api.ConfigurationBuilder; -import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; -import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; -import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource; -import edu.umd.cs.findbugs.annotations.NonNull; -import java.io.IOException; -import java.nio.file.Path; - -/** The ConfigProviderImpl class provides the implementation for the configuration provider. */ -public class ConfigProviderImpl implements ConfigProvider { - private static final System.Logger LOGGER = - System.getLogger(ConfigProviderImpl.class.getName()); - private final Configuration configuration; - - /** Creates a new ConfigProviderImpl instance. */ - public ConfigProviderImpl() { - final var builder = createConfigurationBuilder(); - configuration = builder.build(); - } - - /** - * Returns the configuration. - * - * @return the configuration - */ - @NonNull - @Override - public Configuration getConfiguration() { - return configuration; - } - - private ConfigurationBuilder createConfigurationBuilder() { - try { - return ConfigurationBuilder.create() - .withSource(SystemEnvironmentConfigSource.getInstance()) - .withSource(SystemPropertiesConfigSource.getInstance()) - .withSource(new ClasspathFileConfigSource(Path.of("app.properties"))) - .autoDiscoverExtensions(); - - } catch (IOException e) { - LOGGER.log(System.Logger.Level.ERROR, "Failed to create configuration builder", e); - throw new RuntimeException(e); - } - } -} diff --git a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java index 71bd1b17..2572d6b0 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java @@ -29,17 +29,17 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -class BlockStreamSimulatorTest { +class BlockStreamSimulatorAppTest { @Mock private Configuration configuration; @Mock private BlockStreamManager blockStreamManager; - @InjectMocks private BlockStreamSimulator blockStreamSimulator; + @InjectMocks private BlockStreamSimulatorApp blockStreamSimulator; @BeforeEach void setUp() { - blockStreamSimulator = new BlockStreamSimulator(configuration, blockStreamManager); + blockStreamSimulator = new BlockStreamSimulatorApp(configuration, blockStreamManager); } @AfterEach diff --git a/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java b/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java new file mode 100644 index 00000000..84846eae --- /dev/null +++ b/simulator/src/test/java/com/hedera/block/simulator/config/ConfigInjectionModuleTest.java @@ -0,0 +1,66 @@ +/* + * 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.simulator.config; + +import com.hedera.block.simulator.config.data.BlockStreamConfig; +import com.hedera.block.simulator.config.data.GrpcConfig; +import com.hedera.block.simulator.config.types.GenerationMode; +import com.swirlds.config.api.Configuration; +import com.swirlds.config.api.ConfigurationBuilder; +import com.swirlds.config.extensions.sources.ClasspathFileConfigSource; +import com.swirlds.config.extensions.sources.SystemEnvironmentConfigSource; +import com.swirlds.config.extensions.sources.SystemPropertiesConfigSource; +import java.io.IOException; +import java.nio.file.Path; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class ConfigInjectionModuleTest { + + static Configuration configuration; + + @BeforeAll + static void setUpAll() throws IOException { + configuration = + ConfigurationBuilder.create() + .withSource(SystemEnvironmentConfigSource.getInstance()) + .withSource(SystemPropertiesConfigSource.getInstance()) + .withSource(new ClasspathFileConfigSource(Path.of("app.properties"))) + .autoDiscoverExtensions() + .build(); + } + + @Test + void provideBlockStreamConfig() { + + BlockStreamConfig blockStreamConfig = + ConfigInjectionModule.provideBlockStreamConfig(configuration); + + Assertions.assertNotNull(blockStreamConfig); + Assertions.assertEquals(GenerationMode.SELF, blockStreamConfig.generationMode()); + } + + @Test + void provideGrpcConfig() { + GrpcConfig grpcConfig = ConfigInjectionModule.provideGrpcConfig(configuration); + + Assertions.assertNotNull(grpcConfig); + Assertions.assertEquals("localhost", grpcConfig.serverAddress()); + Assertions.assertEquals(8080, grpcConfig.port()); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java b/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java similarity index 55% rename from simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java rename to simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java index 5a22e44c..b78a41de 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigProvider.java +++ b/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java @@ -14,20 +14,19 @@ * limitations under the License. */ -package com.hedera.block.simulator.config; +package com.hedera.block.simulator.config.types; -import com.swirlds.config.api.Configuration; -import edu.umd.cs.findbugs.annotations.NonNull; +import static org.junit.jupiter.api.Assertions.*; -/** The provider used to get the configuration for the simulator. */ -public interface ConfigProvider { +import org.junit.jupiter.api.Test; - /** - * Gets the configuration for the simulator, specifies the mode of generation, the mode of - * simulator and every configurable detail. - * - * @return the configuration - */ - @NonNull - Configuration getConfiguration(); +class GenerationModeTest { + + @Test + void testGenerationMode() { + GenerationMode mode = GenerationMode.FOLDER; + assertEquals(GenerationMode.FOLDER, mode); + mode = GenerationMode.SELF; + assertEquals(GenerationMode.SELF, mode); + } } diff --git a/simulator/src/test/java/com/hedera/block/simulator/generator/BlockStreamManagerTest.java b/simulator/src/test/java/com/hedera/block/simulator/generator/BlockStreamManagerTest.java new file mode 100644 index 00000000..0e480288 --- /dev/null +++ b/simulator/src/test/java/com/hedera/block/simulator/generator/BlockStreamManagerTest.java @@ -0,0 +1,30 @@ +/* + * 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.simulator.generator; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class BlockStreamManagerTest { + + @Test + void getNextBlock() { + BlockStreamManager blockStreamManager = new MockBlockStreamManagerImpl(); + assertNotNull(blockStreamManager.getNextBlock()); + } +}