diff --git a/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts b/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts new file mode 100644 index 000000000..f5361b330 --- /dev/null +++ b/buildSrc/src/main/kotlin/com.hedera.block.simulator.gradle.kts @@ -0,0 +1,25 @@ +/* + * 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. + */ + +plugins { + id("application") + id("com.hedera.block.conventions") + id("me.champeau.jmh") +} + +val maven = publishing.publications.create("maven") { from(components["java"]) } + +signing.sign(maven) diff --git a/codecov.yml b/codecov.yml index e21c93660..620cc39a3 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/settings.gradle.kts b/settings.gradle.kts index 4770f3462..5cdb9b67e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -21,6 +21,7 @@ plugins { // Include the subprojects include(":stream") include(":server") +include(":simulator") includeBuild(".") // https://github.com/gradle/gradle/issues/21490#issuecomment-1458887481 diff --git a/simulator/build.gradle.kts b/simulator/build.gradle.kts new file mode 100644 index 000000000..54d8ab765 --- /dev/null +++ b/simulator/build.gradle.kts @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2022-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. + */ + +plugins { + id("application") + id("com.hedera.block.simulator") +} + +description = "Hedera Block Stream Simulator" + +application { + mainModule = "com.hedera.block.simulator" + mainClass = "com.hedera.block.simulator.BlockStreamSimulator" +} + +mainModuleInfo { + annotationProcessor("dagger.compiler") + annotationProcessor("com.google.auto.service.processor") + runtimeOnly("com.swirlds.config.impl") +} + +testModuleInfo { + requires("org.junit.jupiter.api") + requires("org.mockito") + requires("org.mockito.junit.jupiter") + requiresStatic("com.github.spotbugs.annotations") +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java new file mode 100644 index 000000000..c376d2778 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulator.java @@ -0,0 +1,62 @@ +/* + * 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 static java.lang.System.Logger.Level.INFO; + +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.lang.System.Logger; +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()); + + /** 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) throws IOException { + + LOGGER.log(INFO, "Starting Block Stream Simulator"); + + ConfigurationBuilder configurationBuilder = + ConfigurationBuilder.create() + .withSource(SystemEnvironmentConfigSource.getInstance()) + .withSource(SystemPropertiesConfigSource.getInstance()) + .withSource(new ClasspathFileConfigSource(Path.of("app.properties"))) + .autoDiscoverExtensions(); + + Configuration configuration = configurationBuilder.build(); + + BlockStreamSimulatorInjectionComponent DIComponent = + DaggerBlockStreamSimulatorInjectionComponent.factory().create(configuration); + + 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 000000000..7c1f6cad2 --- /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 new file mode 100644 index 000000000..cf5589e1a --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/BlockStreamSimulatorInjectionComponent.java @@ -0,0 +1,53 @@ +/* + * 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.config.ConfigInjectionModule; +import com.hedera.block.simulator.generator.GeneratorInjectionModule; +import com.swirlds.config.api.Configuration; +import dagger.BindsInstance; +import dagger.Component; +import javax.inject.Singleton; + +/** The component used to inject the block stream simulator into the application. */ +@Singleton +@Component( + modules = { + ConfigInjectionModule.class, + GeneratorInjectionModule.class, + }) +public interface BlockStreamSimulatorInjectionComponent { + + /** + * Gets the block stream simulator. + * + * @return the block stream simulator + */ + BlockStreamSimulatorApp getBlockStreamSimulatorApp(); + + /** The factory used to create the block stream simulator injection component. */ + @Component.Factory + interface Factory { + /** + * Creates the block stream simulator injection component. + * + * @param configuration the configuration to be used by the block stream simulator + * @return the block stream simulator injection component + */ + BlockStreamSimulatorInjectionComponent create(@BindsInstance Configuration configuration); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java new file mode 100644 index 000000000..ff2d4efb3 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/ConfigInjectionModule.java @@ -0,0 +1,53 @@ +/* + * 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.swirlds.config.api.Configuration; +import dagger.Module; +import dagger.Provides; +import javax.inject.Singleton; + +/** The module used to inject the configuration data into the application. */ +@Module +public interface ConfigInjectionModule { + + /** + * Provides the block stream configuration. + * + * @param configuration the configuration to be used by the block stream + * @return the block stream configuration + */ + @Singleton + @Provides + static BlockStreamConfig provideBlockStreamConfig(Configuration configuration) { + return configuration.getConfigData(BlockStreamConfig.class); + } + + /** + * Provides the gRPC configuration. + * + * @param configuration the configuration to be used by the gRPC + * @return the gRPC configuration + */ + @Singleton + @Provides + static GrpcConfig provideGrpcConfig(Configuration configuration) { + return configuration.getConfigData(GrpcConfig.class); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java b/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java new file mode 100644 index 000000000..61f526ca8 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/SimulatorConfigExtension.java @@ -0,0 +1,40 @@ +/* + * 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.google.auto.service.AutoService; +import com.hedera.block.simulator.config.data.BlockStreamConfig; +import com.hedera.block.simulator.config.data.GrpcConfig; +import com.swirlds.config.api.ConfigurationExtension; +import edu.umd.cs.findbugs.annotations.NonNull; +import java.util.Set; + +/** Sets up configuration for services. */ +@AutoService(ConfigurationExtension.class) +public class SimulatorConfigExtension implements ConfigurationExtension { + + /** Explicitly defined constructor. */ + public SimulatorConfigExtension() { + super(); + } + + @NonNull + @Override + public Set> getConfigDataTypes() { + return Set.of(BlockStreamConfig.class, GrpcConfig.class); + } +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.java new file mode 100644 index 000000000..d0107b988 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/BlockStreamConfig.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.config.data; + +import com.hedera.block.simulator.config.types.GenerationMode; +import com.swirlds.config.api.ConfigData; +import com.swirlds.config.api.ConfigProperty; + +/** + * The BlockStreamConfig class defines the configuration data for the block stream. + * + * @param generationMode the mode of generation for the block stream + */ +@ConfigData("blockStream") +public record BlockStreamConfig( + @ConfigProperty(defaultValue = "DIR") GenerationMode generationMode) {} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java new file mode 100644 index 000000000..dea9e07d2 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/data/GrpcConfig.java @@ -0,0 +1,33 @@ +/* + * 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.data; + +import com.swirlds.config.api.ConfigData; +import com.swirlds.config.api.ConfigProperty; +import com.swirlds.config.api.validation.annotation.Max; +import com.swirlds.config.api.validation.annotation.Min; + +/** + * The GrpcConfig class defines the configuration data for the gRPC client. + * + * @param serverAddress the address of the gRPC server + * @param port the port of the gRPC server + */ +@ConfigData("grpc") +public record GrpcConfig( + @ConfigProperty(defaultValue = "localhost") String serverAddress, + @ConfigProperty(defaultValue = "8080") @Min(0) @Max(65535) int port) {} diff --git a/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java new file mode 100644 index 000000000..cf61bdaab --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/config/types/GenerationMode.java @@ -0,0 +1,25 @@ +/* + * 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.types; + +/** The GenerationMode enum defines the modes of generation for the block stream. */ +public enum GenerationMode { + /** Reads Blocks from a Folder. */ + DIR, + /** Generates Blocks from rules */ + ADHOC +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.java new file mode 100644 index 000000000..74e744a4c --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/BlockStreamManager.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 com.hedera.hapi.block.stream.Block; + +/** this interface defines the contract for managing the block stream. */ +public interface BlockStreamManager { + + /** + * Gets the next block in the block stream. + * + * @return the next block in the block stream + */ + Block getNextBlock(); +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java b/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java new file mode 100644 index 000000000..95266dfc8 --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/GeneratorInjectionModule.java @@ -0,0 +1,36 @@ +/* + * 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 dagger.Binds; +import dagger.Module; +import javax.inject.Singleton; + +/** The module used to inject the block stream manager. */ +@Module +public interface GeneratorInjectionModule { + + /** + * Provides the block stream manager. + * + * @param blockStreamManager the block stream manager to be used + * @return the block stream manager + */ + @Singleton + @Binds + BlockStreamManager bindBlockStreamManager(MockBlockStreamManagerImpl blockStreamManager); +} diff --git a/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java b/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java new file mode 100644 index 000000000..7f4ae0d3a --- /dev/null +++ b/simulator/src/main/java/com/hedera/block/simulator/generator/MockBlockStreamManagerImpl.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 Hedera Hashgraph, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hedera.block.simulator.generator; + +import com.hedera.hapi.block.stream.Block; +import javax.inject.Inject; + +/** A mock implementation of the BlockStreamManager interface. */ +public class MockBlockStreamManagerImpl implements BlockStreamManager { + + /** Constructs a new MockBlockStreamManagerImpl. */ + @Inject + public MockBlockStreamManagerImpl() {} + + /** + * Gets the next block in the block stream. + * + * @return the next block in the block stream + */ + @Override + public Block getNextBlock() { + return Block.newBuilder().build(); + } +} diff --git a/simulator/src/main/java/module-info.java b/simulator/src/main/java/module-info.java new file mode 100644 index 000000000..9051122a0 --- /dev/null +++ b/simulator/src/main/java/module-info.java @@ -0,0 +1,18 @@ +import com.hedera.block.simulator.config.SimulatorConfigExtension; + +/** Runtime module of the simulator. */ +module com.hedera.block.simulator { + exports com.hedera.block.simulator.config.data; + + requires static com.github.spotbugs.annotations; + requires static com.google.auto.service; + requires com.hedera.block.stream; + // requires com.hedera.pbj.runtime; // leaving it here since it will be needed soon. + requires com.swirlds.config.api; + requires com.swirlds.config.extensions; + requires dagger; + requires javax.inject; + + provides com.swirlds.config.api.ConfigurationExtension with + SimulatorConfigExtension; +} diff --git a/simulator/src/main/resources/app.properties b/simulator/src/main/resources/app.properties new file mode 100644 index 000000000..2ce7c6a76 --- /dev/null +++ b/simulator/src/main/resources/app.properties @@ -0,0 +1 @@ +blockStream.folderRootPath=/Users/user/Projects/hedera-block-node/simulator/src/main/resources/block-0.0.3/ diff --git a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java new file mode 100644 index 000000000..2572d6b02 --- /dev/null +++ b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java @@ -0,0 +1,60 @@ +/* + * 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 static org.junit.jupiter.api.Assertions.*; + +import com.hedera.block.simulator.generator.BlockStreamManager; +import com.swirlds.config.api.Configuration; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class BlockStreamSimulatorAppTest { + + @Mock private Configuration configuration; + + @Mock private BlockStreamManager blockStreamManager; + + @InjectMocks private BlockStreamSimulatorApp blockStreamSimulator; + + @BeforeEach + void setUp() { + blockStreamSimulator = new BlockStreamSimulatorApp(configuration, blockStreamManager); + } + + @AfterEach + void tearDown() { + blockStreamSimulator.stop(); + } + + @Test + void start_logsStartedMessage() { + blockStreamSimulator.start(); + assertTrue(blockStreamSimulator.isRunning()); + } + + @Test + void stop_doesNotThrowException() { + assertDoesNotThrow(() -> blockStreamSimulator.stop()); + } +} 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 000000000..4b8f2dbae --- /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.DIR, 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/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java b/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java new file mode 100644 index 000000000..1a4448ffd --- /dev/null +++ b/simulator/src/test/java/com/hedera/block/simulator/config/types/GenerationModeTest.java @@ -0,0 +1,32 @@ +/* + * 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.types; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class GenerationModeTest { + + @Test + void testGenerationMode() { + GenerationMode mode = GenerationMode.DIR; + assertEquals(GenerationMode.DIR, mode); + mode = GenerationMode.ADHOC; + assertEquals(GenerationMode.ADHOC, 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 000000000..0e4802887 --- /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()); + } +}