Skip to content

Commit

Permalink
Removed ConfigProvider, since we will be injecting the config we don'…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
AlfredoG87 committed Sep 3, 2024
1 parent 5384ffe commit ef7f0a9
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 132 deletions.
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit ef7f0a9

Please sign in to comment.