Skip to content

Commit

Permalink
Added UT for missing InjectionModules, removed unneeded configuration…
Browse files Browse the repository at this point in the history
… definition from ConfigExtensions

Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 committed Aug 25, 2024
1 parent 1b2690c commit 689b6fc
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.auto.service.AutoService;
import com.hedera.block.server.consumer.ConsumerConfig;
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
import com.swirlds.common.config.BasicCommonConfig;
import com.swirlds.common.metrics.config.MetricsConfig;
import com.swirlds.common.metrics.platform.prometheus.PrometheusConfig;
import com.swirlds.config.api.ConfigurationExtension;
Expand All @@ -44,7 +43,6 @@ public BlockNodeConfigExtension() {
@Override
public Set<Class<? extends Record>> getConfigDataTypes() {
return Set.of(
BasicCommonConfig.class,
MetricsConfig.class,
PrometheusConfig.class,
ConsumerConfig.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.hedera.block.server.consumer.ConsumerConfig;
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
import com.swirlds.common.config.BasicCommonConfig;
import com.swirlds.common.metrics.config.MetricsConfig;
import com.swirlds.common.metrics.platform.prometheus.PrometheusConfig;
import com.swirlds.config.api.Configuration;
Expand Down Expand Up @@ -82,16 +81,4 @@ static PrometheusConfig providePrometheusConfig(@NonNull Configuration configura
static ConsumerConfig provideConsumerConfig(@NonNull Configuration configuration) {
return configuration.getConfigData(ConsumerConfig.class);
}

/**
* Provides a basic common configuration singleton using the configuration.
*
* @param configuration is the configuration singleton
* @return a basic common configuration singleton
*/
@Singleton
@Provides
static BasicCommonConfig provideBasicCommonConfig(@NonNull Configuration configuration) {
return configuration.getConfigData(BasicCommonConfig.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,66 @@
package com.hedera.block.server;

import com.hedera.block.server.config.BlockNodeContext;
import com.hedera.block.server.data.ObjectEvent;
import com.hedera.block.server.mediator.StreamMediator;
import com.hedera.block.server.metrics.MetricsService;
import com.hedera.block.server.persistence.storage.read.BlockReader;
import com.hedera.block.server.util.TestConfigUtil;
import com.hedera.hapi.block.SubscribeStreamResponse;
import com.hedera.hapi.block.stream.Block;
import com.hedera.hapi.block.stream.BlockItem;
import com.swirlds.config.api.Configuration;
import io.helidon.webserver.WebServerConfig;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

class BlockNodeAppInjectionModuleTest {

@Mock private StreamMediator<BlockItem, ObjectEvent<SubscribeStreamResponse>> streamMediator;

@Mock private BlockReader<Block> blockReader;

@Mock private ServiceStatus serviceStatus;

private BlockNodeContext blockNodeContext;

@BeforeEach
void setUp() throws IOException {
blockNodeContext = TestConfigUtil.getTestBlockNodeContext();
}

@Test
void testProvideBlockNodeContext() {
Configuration configuration = blockNodeContext.configuration();
MetricsService metricsService = blockNodeContext.metricsService();

BlockNodeContext providedBlockNodeContext =
BlockNodeAppInjectionModule.provideBlockNodeContext(configuration, metricsService);

Assertions.assertEquals(blockNodeContext, providedBlockNodeContext);
Assertions.assertEquals(
blockNodeContext.configuration(), providedBlockNodeContext.configuration());
Assertions.assertEquals(
blockNodeContext.metricsService(), providedBlockNodeContext.metricsService());
}

@Test
void testProvideBlockStreamService() {
BlockStreamService blockStreamService =
BlockNodeAppInjectionModule.provideBlockStreamService(
streamMediator, blockReader, serviceStatus, blockNodeContext);

Assertions.assertNotNull(blockStreamService);
}

@Test
void testProvideBlockNodeContext() throws IOException {
BlockNodeContext blockNodeContext = TestConfigUtil.getTestBlockNodeContext();
void testProvideWebServerConfigBuilder() {
WebServerConfig.Builder webServerConfigBuilder =
BlockNodeAppInjectionModule.provideWebServerConfigBuilder();

Assertions.assertNotNull(blockNodeContext);
Assertions.assertNotNull(blockNodeContext.configuration());
Assertions.assertNotNull(blockNodeContext.metricsService());
Assertions.assertNotNull(webServerConfigBuilder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.server.config;

import static org.junit.jupiter.api.Assertions.*;

import com.hedera.block.server.consumer.ConsumerConfig;
import com.hedera.block.server.persistence.storage.PersistenceStorageConfig;
import com.hedera.block.server.util.TestConfigUtil;
import com.swirlds.common.metrics.config.MetricsConfig;
import com.swirlds.common.metrics.platform.prometheus.PrometheusConfig;
import com.swirlds.config.api.Configuration;
import java.io.IOException;
import org.junit.jupiter.api.Test;

class ConfigInjectionModuleTest {

@Test
void testProvidePersistenceStorageConfig() throws IOException {

BlockNodeContext context = TestConfigUtil.getTestBlockNodeContext();
Configuration configuration = context.configuration();
PersistenceStorageConfig persistenceStorageConfig =
configuration.getConfigData(PersistenceStorageConfig.class);

// Call the method under test
PersistenceStorageConfig providedConfig =
ConfigInjectionModule.providePersistenceStorageConfig(configuration);

// Verify that the correct config data is returned
assertNotNull(providedConfig);
assertSame(persistenceStorageConfig, providedConfig);
}

@Test
void testProvideMetricsConfig() throws IOException {

BlockNodeContext context = TestConfigUtil.getTestBlockNodeContext();
Configuration configuration = context.configuration();
MetricsConfig metricsConfig = configuration.getConfigData(MetricsConfig.class);

// Call the method under test
MetricsConfig providedConfig = ConfigInjectionModule.provideMetricsConfig(configuration);

// Verify that the correct config data is returned
assertNotNull(providedConfig);
assertSame(metricsConfig, providedConfig);
}

@Test
void testProvidePrometheusConfig() throws IOException {

BlockNodeContext context = TestConfigUtil.getTestBlockNodeContext();
Configuration configuration = context.configuration();
PrometheusConfig prometheusConfig = configuration.getConfigData(PrometheusConfig.class);

// Call the method under test
PrometheusConfig providedConfig =
ConfigInjectionModule.providePrometheusConfig(configuration);

// Verify that the correct config data is returned
assertNotNull(providedConfig);
assertSame(prometheusConfig, providedConfig);
}

@Test
void testProvideConsumerConfig() throws IOException {

BlockNodeContext context = TestConfigUtil.getTestBlockNodeContext();
Configuration configuration = context.configuration();
ConsumerConfig consumerConfig = configuration.getConfigData(ConsumerConfig.class);

// Call the method under test
ConsumerConfig providedConfig = ConfigInjectionModule.provideConsumerConfig(configuration);

// Verify that the correct config data is returned
assertNotNull(providedConfig);
assertSame(consumerConfig, providedConfig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.server.metrics;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.hedera.block.server.config.BlockNodeContext;
import com.hedera.block.server.util.TestConfigUtil;
import com.swirlds.config.api.Configuration;
import com.swirlds.metrics.api.Metrics;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class MetricsInjectionModuleTest {

@Mock private Metrics metrics;

@Test
void testProvideMetricsService() {
// Call the method under test
MetricsService metricsService = MetricsInjectionModule.provideMetricsService(metrics);

// Verify that the metricsService is correctly instantiated
assertNotNull(metricsService);
}

@Test
void testProvideMetrics() throws IOException {
BlockNodeContext context = TestConfigUtil.getTestBlockNodeContext();
Configuration configuration = context.configuration();

// Call the method under test
Metrics providedMetrics = MetricsInjectionModule.provideMetrics(configuration);

assertNotNull(providedMetrics);
}
}

0 comments on commit 689b6fc

Please sign in to comment.