Skip to content

Commit

Permalink
Add more tests and fix code smells
Browse files Browse the repository at this point in the history
Signed-off-by: Bilyana Gospodinova <bilyana.gospodinova14@gmail.com>
  • Loading branch information
bilyana-gospodinova committed Nov 21, 2024
1 parent e57a0bc commit f67a110
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import jakarta.inject.Named;
import java.time.InstantSource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -127,8 +128,8 @@ private void init() {
networkInfo,
new MetricsImpl());

final var writableStates = this.getWritableStates(FileService.NAME);
final var files = writableStates.<FileID, File>get(V0490FileSchema.BLOBS_KEY);
final var fileServiceStates = this.getWritableStates(FileService.NAME);
final var files = fileServiceStates.<FileID, File>get(V0490FileSchema.BLOBS_KEY);
genesisContentProviders(networkInfo, bootstrapConfig).forEach((fileNum, provider) -> {
final var fileId = createFileID(fileNum, bootstrapConfig);
files.put(
Expand All @@ -139,7 +140,7 @@ private void init() {
.contents(provider.apply(bootstrapConfig))
.build());
});
((CommittableWritableStates) writableStates).commit();
((CommittableWritableStates) fileServiceStates).commit();
}

public MirrorNodeState addService(@NonNull final String serviceName, @NonNull final Map<String, ?> dataSources) {
Expand Down Expand Up @@ -227,7 +228,7 @@ public WritableStates getWritableStates(@Nonnull String serviceName) {
data.put(
stateName,
withAnyRegisteredListeners(serviceName, new ListWritableQueueState<>(stateName, queue)));
} else if (state instanceof Map<?, ?> map) {
} else if (state instanceof Map<?, ?>) {
data.put(
stateName,
withAnyRegisteredListeners(
Expand Down Expand Up @@ -361,6 +362,11 @@ void setWritableStates(final Map<String, WritableStates> writableStates) {
this.writableStates.putAll(writableStates);
}

@VisibleForTesting
Map<String, Map<String, Object>> getStates() {
return Collections.unmodifiableMap(states);
}

private void registerServices(ServicesRegistry servicesRegistry) {
// Register all service schema RuntimeConstructable factories before platform init
final var appContext = new AppContextImpl(InstantSource.system(), signatureVerifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,54 @@ public class MetricsImpl implements Metrics {
@Nullable
@Override
public Metric getMetric(@Nonnull String category, @Nonnull String name) {
return null;
throw new UnsupportedOperationException();

Check warning on line 31 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L31

Added line #L31 was not covered by tests
}

@Nonnull
@Override
public Collection<Metric> findMetricsByCategory(@Nonnull String category) {
return null;
throw new UnsupportedOperationException();

Check warning on line 37 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L37

Added line #L37 was not covered by tests
}

@Nonnull
@Override
public Collection<Metric> getAll() {
return null;
throw new UnsupportedOperationException();

Check warning on line 43 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L43

Added line #L43 was not covered by tests
}

@Nonnull
@Override
public <T extends Metric> T getOrCreate(@Nonnull MetricConfig<T, ?> config) {
return null;
throw new UnsupportedOperationException();

Check warning on line 49 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L49

Added line #L49 was not covered by tests
}

@Override
public void remove(@Nonnull String category, @Nonnull String name) {}
public void remove(@Nonnull String category, @Nonnull String name) {
throw new UnsupportedOperationException();

Check warning on line 54 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L54

Added line #L54 was not covered by tests
}

@Override
public void remove(@Nonnull Metric metric) {}
public void remove(@Nonnull Metric metric) {
throw new UnsupportedOperationException();

Check warning on line 59 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L59

Added line #L59 was not covered by tests
}

@Override
public void remove(@Nonnull MetricConfig<?, ?> config) {}
public void remove(@Nonnull MetricConfig<?, ?> config) {
throw new UnsupportedOperationException();

Check warning on line 64 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L64

Added line #L64 was not covered by tests
}

@Override
public void addUpdater(@Nonnull Runnable updater) {}
public void addUpdater(@Nonnull Runnable updater) {
throw new UnsupportedOperationException();

Check warning on line 69 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L69

Added line #L69 was not covered by tests
}

@Override
public void removeUpdater(@Nonnull Runnable updater) {}
public void removeUpdater(@Nonnull Runnable updater) {
throw new UnsupportedOperationException();

Check warning on line 74 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L74

Added line #L74 was not covered by tests
}

@Override
public void start() {}
public void start() {
throw new UnsupportedOperationException();

Check warning on line 79 in hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/mirror/web3/state/components/MetricsImpl.java#L79

Added line #L79 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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.mirror.web3.state;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import com.hedera.mirror.web3.Web3IntegrationTest;
import com.hedera.node.app.fees.FeeService;
import com.hedera.node.app.ids.EntityIdService;
import com.hedera.node.app.records.BlockRecordService;
import com.hedera.node.app.service.contract.ContractService;
import com.hedera.node.app.service.contract.impl.ContractServiceImpl;
import com.hedera.node.app.service.file.FileService;
import com.hedera.node.app.service.file.impl.FileServiceImpl;
import com.hedera.node.app.service.token.TokenService;
import com.hedera.node.app.service.token.impl.TokenServiceImpl;
import com.hedera.node.app.services.ServicesRegistry;
import com.hedera.node.app.state.recordcache.RecordCacheService;
import com.hedera.node.app.throttle.CongestionThrottleService;
import com.swirlds.state.spi.Service;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;

@RequiredArgsConstructor
public class MirrorNodeStateIntegrationTest extends Web3IntegrationTest {

private final MirrorNodeState mirrorNodeState;
private final ServicesRegistry servicesRegistry;

@Test
void verifyMirrorNodeStateHasRegisteredServices() {
Set<Class<? extends Service>> expectedServices = new HashSet<>(List.of(
EntityIdService.class,
TokenServiceImpl.class,
FileServiceImpl.class,
ContractServiceImpl.class,
BlockRecordService.class,
FeeService.class,
CongestionThrottleService.class,
RecordCacheService.class));

final var registeredServices = servicesRegistry.registrations();
assertThat(registeredServices.size()).isEqualTo(expectedServices.size());

for (var expectedService : expectedServices) {
assertThat(registeredServices.stream()
.anyMatch(registration ->
registration.service().getClass().equals(expectedService)))
.isTrue();
}
}

@Test
void verifyServicesHaveAssignedDataSources() {
final var states = mirrorNodeState.getStates();

// BlockRecordService
Map<String, Class<?>> blockRecordServiceDataSources = Map.of(
"BLOCKS", AtomicReference.class,
"RUNNING_HASHES", AtomicReference.class);
verifyServiceDataSources(states, BlockRecordService.NAME, blockRecordServiceDataSources);

// FileService
Map<String, Class<?>> fileServiceDataSources = Map.of("FILES", Map.class);
verifyServiceDataSources(states, FileService.NAME, fileServiceDataSources);

// CongestionThrottleService
Map<String, Class<?>> congestionThrottleServiceDataSources = Map.of(
"THROTTLE_USAGE_SNAPSHOTS", AtomicReference.class,
"CONGESTION_LEVEL_STARTS", AtomicReference.class);
verifyServiceDataSources(states, CongestionThrottleService.NAME, congestionThrottleServiceDataSources);

// FeeService
Map<String, Class<?>> feeServiceDataSources = Map.of("MIDNIGHT_RATES", AtomicReference.class);
verifyServiceDataSources(states, FeeService.NAME, feeServiceDataSources);

// ContractService
Map<String, Class<?>> contractServiceDataSources = Map.of(
"BYTECODE", Map.class,
"STORAGE", Map.class);
verifyServiceDataSources(states, ContractService.NAME, contractServiceDataSources);

// RecordCacheService
Map<String, Class<?>> recordCacheServiceDataSources = Map.of("TransactionReceiptQueue", Deque.class);
verifyServiceDataSources(states, RecordCacheService.NAME, recordCacheServiceDataSources);

// EntityIdService
Map<String, Class<?>> entityIdServiceDataSources = Map.of("ENTITY_ID", AtomicReference.class);
verifyServiceDataSources(states, EntityIdService.NAME, entityIdServiceDataSources);

// TokenService
Map<String, Class<?>> tokenServiceDataSources = Map.of(
"ACCOUNTS", Map.class,
"PENDING_AIRDROPS", Map.class,
"ALIASES", Map.class,
"NFTS", Map.class,
"TOKENS", Map.class,
"TOKEN_RELS", Map.class,
"STAKING_NETWORK_REWARDS", AtomicReference.class);
verifyServiceDataSources(states, TokenService.NAME, tokenServiceDataSources);
}

private void verifyServiceDataSources(
Map<String, Map<String, Object>> states, String serviceName, Map<String, Class<?>> expectedDataSources) {
final var serviceState = states.get(serviceName);
assertThat(serviceState).isNotNull();
expectedDataSources.forEach((key, type) -> {
assertThat(serviceState.containsKey(key)).isTrue();
assertThat(serviceState.get(key)).isInstanceOf(type);
});
}
}

0 comments on commit f67a110

Please sign in to comment.