|
| 1 | +package org.cardanofoundation.rosetta.api.network.service; |
| 2 | + |
| 3 | +import org.cardanofoundation.rosetta.api.block.model.domain.BlockIdentifierExtended; |
| 4 | +import org.cardanofoundation.rosetta.api.block.service.LedgerBlockService; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.DisplayName; |
| 7 | +import org.junit.jupiter.api.Nested; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | +import org.mockito.InjectMocks; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 13 | +import org.openapitools.client.model.SyncStatus; |
| 14 | +import org.springframework.boot.actuate.health.Health; |
| 15 | +import org.springframework.boot.actuate.health.Status; |
| 16 | + |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +@ExtendWith(MockitoExtension.class) |
| 23 | +class SyncStatusHealthIndicatorTest { |
| 24 | + |
| 25 | + @Mock |
| 26 | + private SyncStatusService syncStatusService; |
| 27 | + |
| 28 | + @Mock |
| 29 | + private LedgerBlockService ledgerBlockService; |
| 30 | + |
| 31 | + @InjectMocks |
| 32 | + private SyncStatusHealthIndicator indicator; |
| 33 | + |
| 34 | + private BlockIdentifierExtended latestBlock; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + latestBlock = BlockIdentifierExtended.builder() |
| 39 | + .slot(100_000_000L) |
| 40 | + .number(10_000_000L) |
| 41 | + .hash("abc123") |
| 42 | + .build(); |
| 43 | + when(ledgerBlockService.findLatestBlockIdentifier()).thenReturn(latestBlock); |
| 44 | + } |
| 45 | + |
| 46 | + @Nested |
| 47 | + @DisplayName("When sync stage is LIVE") |
| 48 | + class WhenStageLive { |
| 49 | + |
| 50 | + @Test |
| 51 | + @DisplayName("health() returns UP status") |
| 52 | + void returnsHealthUp() { |
| 53 | + var syncStatus = SyncStatus.builder() |
| 54 | + .synced(true) |
| 55 | + .stage("LIVE") |
| 56 | + .currentIndex(100_000_000L) |
| 57 | + .targetIndex(100_000_050L) |
| 58 | + .build(); |
| 59 | + when(syncStatusService.calculateSyncStatus(latestBlock)).thenReturn(Optional.of(syncStatus)); |
| 60 | + |
| 61 | + Health health = indicator.health(); |
| 62 | + |
| 63 | + assertThat(health.getStatus()).isEqualTo(Status.UP); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @DisplayName("health() includes stage, synced, currentIndex, targetIndex details") |
| 68 | + void includesSyncDetails() { |
| 69 | + var syncStatus = SyncStatus.builder() |
| 70 | + .synced(true) |
| 71 | + .stage("LIVE") |
| 72 | + .currentIndex(100_000_000L) |
| 73 | + .targetIndex(100_000_050L) |
| 74 | + .build(); |
| 75 | + when(syncStatusService.calculateSyncStatus(latestBlock)).thenReturn(Optional.of(syncStatus)); |
| 76 | + |
| 77 | + Health health = indicator.health(); |
| 78 | + |
| 79 | + assertThat(health.getDetails()) |
| 80 | + .containsEntry("stage", "LIVE") |
| 81 | + .containsEntry("synced", true) |
| 82 | + .containsEntry("currentIndex", 100_000_000L) |
| 83 | + .containsEntry("targetIndex", 100_000_050L); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Nested |
| 88 | + @DisplayName("When sync stage is SYNCING") |
| 89 | + class WhenStageSyncing { |
| 90 | + |
| 91 | + @Test |
| 92 | + @DisplayName("health() returns OUT_OF_SERVICE status") |
| 93 | + void returnsHealthOutOfService() { |
| 94 | + var syncStatus = SyncStatus.builder() |
| 95 | + .synced(false) |
| 96 | + .stage("SYNCING") |
| 97 | + .currentIndex(50_000_000L) |
| 98 | + .targetIndex(100_000_000L) |
| 99 | + .build(); |
| 100 | + when(syncStatusService.calculateSyncStatus(latestBlock)).thenReturn(Optional.of(syncStatus)); |
| 101 | + |
| 102 | + Health health = indicator.health(); |
| 103 | + |
| 104 | + assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @DisplayName("health() includes stage and synced=false details") |
| 109 | + void includesSyncDetails() { |
| 110 | + var syncStatus = SyncStatus.builder() |
| 111 | + .synced(false) |
| 112 | + .stage("SYNCING") |
| 113 | + .currentIndex(50_000_000L) |
| 114 | + .targetIndex(100_000_000L) |
| 115 | + .build(); |
| 116 | + when(syncStatusService.calculateSyncStatus(latestBlock)).thenReturn(Optional.of(syncStatus)); |
| 117 | + |
| 118 | + Health health = indicator.health(); |
| 119 | + |
| 120 | + assertThat(health.getDetails()) |
| 121 | + .containsEntry("stage", "SYNCING") |
| 122 | + .containsEntry("synced", false); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Nested |
| 127 | + @DisplayName("When sync stage is APPLYING_INDEXES") |
| 128 | + class WhenStageApplyingIndexes { |
| 129 | + |
| 130 | + @Test |
| 131 | + @DisplayName("health() returns OUT_OF_SERVICE status") |
| 132 | + void returnsHealthOutOfService() { |
| 133 | + var syncStatus = SyncStatus.builder() |
| 134 | + .synced(false) |
| 135 | + .stage("APPLYING_INDEXES") |
| 136 | + .currentIndex(100_000_000L) |
| 137 | + .targetIndex(100_000_000L) |
| 138 | + .build(); |
| 139 | + when(syncStatusService.calculateSyncStatus(latestBlock)).thenReturn(Optional.of(syncStatus)); |
| 140 | + |
| 141 | + Health health = indicator.health(); |
| 142 | + |
| 143 | + assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + @DisplayName("health() includes stage=APPLYING_INDEXES in details") |
| 148 | + void includesApplyingIndexesStage() { |
| 149 | + var syncStatus = SyncStatus.builder() |
| 150 | + .synced(false) |
| 151 | + .stage("APPLYING_INDEXES") |
| 152 | + .currentIndex(100_000_000L) |
| 153 | + .targetIndex(100_000_000L) |
| 154 | + .build(); |
| 155 | + when(syncStatusService.calculateSyncStatus(latestBlock)).thenReturn(Optional.of(syncStatus)); |
| 156 | + |
| 157 | + Health health = indicator.health(); |
| 158 | + |
| 159 | + assertThat(health.getDetails()).containsEntry("stage", "APPLYING_INDEXES"); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Nested |
| 164 | + @DisplayName("When sync status Optional is empty") |
| 165 | + class WhenSyncStatusEmpty { |
| 166 | + |
| 167 | + @Test |
| 168 | + @DisplayName("health() returns OUT_OF_SERVICE status") |
| 169 | + void returnsHealthOutOfService() { |
| 170 | + when(syncStatusService.calculateSyncStatus(latestBlock)).thenReturn(Optional.empty()); |
| 171 | + |
| 172 | + Health health = indicator.health(); |
| 173 | + |
| 174 | + assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + @DisplayName("health() includes a 'reason' detail explaining unavailability") |
| 179 | + void includesReasonDetail() { |
| 180 | + when(syncStatusService.calculateSyncStatus(latestBlock)).thenReturn(Optional.empty()); |
| 181 | + |
| 182 | + Health health = indicator.health(); |
| 183 | + |
| 184 | + assertThat(health.getDetails()).containsKey("reason"); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments