From c8a4fe9611d091172c7467cebc4790f7a5a1d58c Mon Sep 17 00:00:00 2001 From: Sotatek-ThinhVu Date: Tue, 16 Apr 2024 16:12:50 +0700 Subject: [PATCH] test: update unit test for DRep services --- .../model/request/drep/DRepFilterRequest.java | 2 + .../api/controller/DrepControllerTest.java | 66 +++++++++++++++++ .../explorer/api/service/DRepServiceTest.java | 70 +++++++++++++++++++ 3 files changed, 138 insertions(+) diff --git a/src/main/java/org/cardanofoundation/explorer/api/model/request/drep/DRepFilterRequest.java b/src/main/java/org/cardanofoundation/explorer/api/model/request/drep/DRepFilterRequest.java index 3a1cd8e48..b4a5dc8b0 100644 --- a/src/main/java/org/cardanofoundation/explorer/api/model/request/drep/DRepFilterRequest.java +++ b/src/main/java/org/cardanofoundation/explorer/api/model/request/drep/DRepFilterRequest.java @@ -7,6 +7,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.experimental.SuperBuilder; import org.cardanofoundation.explorer.common.entity.enumeration.DRepStatus; import org.cardanofoundation.explorer.common.validation.date.DatePattern; @@ -16,6 +17,7 @@ @Setter @AllArgsConstructor @NoArgsConstructor +@SuperBuilder public class DRepFilterRequest { private String drepIdOrHash; diff --git a/src/test/java/org/cardanofoundation/explorer/api/controller/DrepControllerTest.java b/src/test/java/org/cardanofoundation/explorer/api/controller/DrepControllerTest.java index 150a9d820..65335aee4 100644 --- a/src/test/java/org/cardanofoundation/explorer/api/controller/DrepControllerTest.java +++ b/src/test/java/org/cardanofoundation/explorer/api/controller/DrepControllerTest.java @@ -34,9 +34,12 @@ import org.cardanofoundation.explorer.api.controller.advice.GlobalRestControllerExceptionHandler; import org.cardanofoundation.explorer.api.interceptor.AuthInterceptor; import org.cardanofoundation.explorer.api.interceptor.auth.RoleFilterMapper; +import org.cardanofoundation.explorer.api.model.request.drep.DRepFilterRequest; import org.cardanofoundation.explorer.api.model.response.BaseFilterResponse; import org.cardanofoundation.explorer.api.model.response.drep.DRepCertificateHistoryResponse; import org.cardanofoundation.explorer.api.model.response.drep.DRepDetailsResponse; +import org.cardanofoundation.explorer.api.model.response.drep.DRepFilterResponse; +import org.cardanofoundation.explorer.api.model.response.drep.DRepOverviewResponse; import org.cardanofoundation.explorer.api.model.response.drep.VotingProcedureChartResponse; import org.cardanofoundation.explorer.api.service.DRepService; import org.cardanofoundation.explorer.common.entity.enumeration.GovActionType; @@ -200,4 +203,67 @@ void testGetDetailsOfDRep() throws Exception { .string(containsString("43a0e2e2d6bf1d0c48b0eb1744fb853407c6b94f2de79f0508c5962e"))) .andExpect(jsonPath("$.delegators").value(100)); } + + @Test + void testDRepOverview() throws Exception { + + DRepOverviewResponse response = + DRepOverviewResponse.builder() + .totalDReps(12L) + .activeDReps(5L) + .inactiveDReps(5L) + .retiredDReps(0L) + .epochNo(200) + .abstainDReps(2L) + .build(); + when(dRepService.getDRepOverview()).thenReturn(response); + mockMvc + .perform(get("/api/v1/dreps/overview").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.totalDReps").value(12L)); + } + + @Test + void testDRepFilter() throws Exception { + DRepFilterRequest filter = + DRepFilterRequest.builder() + .activeStakeFrom(BigInteger.TEN) + .activeStakeTo(BigInteger.valueOf(100)) + .votingPowerFrom(0.5) + .votingPowerTo(1.0) + .build(); + + DRepFilterResponse response1 = + DRepFilterResponse.builder() + .drepId("dRepId1") + .drepHash("dRepHash1") + .activeVoteStake(BigInteger.TEN) + .votingPower(0.6) + .build(); + + DRepFilterResponse response2 = + DRepFilterResponse.builder() + .drepId("dRepId2") + .drepHash("dRepHash2") + .activeVoteStake(BigInteger.valueOf(20)) + .votingPower(0.7) + .build(); + + when(dRepService.getDRepsByFilter(any(), any(Pageable.class))) + .thenReturn(new BaseFilterResponse<>(List.of(response1, response2), 2L)); + + mockMvc + .perform( + get("/api/v1/dreps/filter") + .param("page", "0") + .param("size", "20") + .param("activeStakeFrom", filter.getActiveStakeFrom().toString()) + .param("activeStakeTo", filter.getActiveStakeTo().toString()) + .param("votingPowerFrom", filter.getVotingPowerFrom().toString()) + .param("votingPowerTo", filter.getVotingPowerTo().toString()) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data.length()").value(2L)) + .andExpect(content().string(containsString("dRepId1"))); + } } diff --git a/src/test/java/org/cardanofoundation/explorer/api/service/DRepServiceTest.java b/src/test/java/org/cardanofoundation/explorer/api/service/DRepServiceTest.java index b76421438..c51014b35 100644 --- a/src/test/java/org/cardanofoundation/explorer/api/service/DRepServiceTest.java +++ b/src/test/java/org/cardanofoundation/explorer/api/service/DRepServiceTest.java @@ -1,12 +1,16 @@ package org.cardanofoundation.explorer.api.service; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import java.math.BigInteger; +import java.time.LocalDateTime; import java.util.List; import java.util.Optional; +import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.mapstruct.factory.Mappers; @@ -22,7 +26,10 @@ import org.cardanofoundation.explorer.api.mapper.DRepCertificateMapper; import org.cardanofoundation.explorer.api.mapper.DRepMapper; +import org.cardanofoundation.explorer.api.model.request.drep.DRepFilterRequest; +import org.cardanofoundation.explorer.api.model.response.dashboard.EpochSummary; import org.cardanofoundation.explorer.api.model.response.drep.projection.DRepCertificateProjection; +import org.cardanofoundation.explorer.api.model.response.drep.projection.DRepStatusCountProjection; import org.cardanofoundation.explorer.api.projection.VotingProcedureProjection; import org.cardanofoundation.explorer.api.repository.explorer.DrepInfoRepository; import org.cardanofoundation.explorer.api.repository.ledgersync.DRepRegistrationRepository; @@ -31,6 +38,7 @@ import org.cardanofoundation.explorer.api.repository.ledgersync.VotingProcedureRepository; import org.cardanofoundation.explorer.api.service.impl.DRepServiceImpl; import org.cardanofoundation.explorer.common.entity.enumeration.DRepActionType; +import org.cardanofoundation.explorer.common.entity.enumeration.DRepStatus; import org.cardanofoundation.explorer.common.entity.enumeration.GovActionType; import org.cardanofoundation.explorer.common.entity.enumeration.Vote; import org.cardanofoundation.explorer.common.entity.explorer.DRepInfo; @@ -42,6 +50,7 @@ public class DRepServiceTest { @Mock VotingProcedureRepository votingProcedureRepository; @Mock LatestVotingProcedureRepository latestVotingProcedureRepository; @Mock GovernanceActionRepository governanceActionRepository; + @Mock EpochService epochService; @InjectMocks DRepServiceImpl dRepCertificateService; @Spy @@ -160,4 +169,65 @@ public void testGetVoteProcedureChart() { Assertions.assertEquals(1, actual.getNumberOfAbstainVotes()); Assertions.assertEquals(1, actual.getNumberOfNoVotes()); } + + @Test + void testGetDRepOverview() { + EpochSummary epochSummary = + EpochSummary.builder().no(100).slot(1000).endTime(LocalDateTime.now().minusDays(5)).build(); + + DRepStatusCountProjection projection1 = Mockito.mock(DRepStatusCountProjection.class); + when(projection1.getStatus()).thenReturn(DRepStatus.ACTIVE); + when(projection1.getCnt()).thenReturn(10L); + + DRepStatusCountProjection projection2 = Mockito.mock(DRepStatusCountProjection.class); + when(projection2.getStatus()).thenReturn(DRepStatus.INACTIVE); + when(projection2.getCnt()).thenReturn(5L); + + when(drepInfoRepository.getDRepStatusCount()).thenReturn(List.of(projection1, projection2)); + when(drepInfoRepository.getDelegateCount()).thenReturn(100L); + when(epochService.getCurrentEpochSummary()).thenReturn(epochSummary); + + var actual = dRepCertificateService.getDRepOverview(); + + Assertions.assertEquals(10, actual.getActiveDReps()); + Assertions.assertEquals(5, actual.getInactiveDReps()); + Assertions.assertEquals(100, actual.getDelegators()); + Assertions.assertEquals(100, actual.getEpochNo()); + } + + @Test + void testGetDRepListByFilter() { + DRepFilterRequest filter = + DRepFilterRequest.builder() + .activeStakeFrom(BigInteger.ONE) + .activeStakeTo(BigInteger.TEN) + .votingPowerFrom(0.5) + .votingPowerTo(1.0) + .drepStatus(DRepStatus.ACTIVE) + .build(); + + DRepInfo response1 = + DRepInfo.builder() + .votingPower(0.6) + .activeVoteStake(BigInteger.TWO) + .status(DRepStatus.ACTIVE) + .build(); + + DRepInfo response2 = + DRepInfo.builder() + .votingPower(0.7) + .activeVoteStake(BigInteger.TWO) + .status(DRepStatus.ACTIVE) + .build(); + + when(drepInfoRepository.getDRepInfoByFilterRequest( + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(Pageable.class))) + .thenReturn(new PageImpl<>(List.of(response1, response2))); + + var actual = dRepCertificateService.getDRepsByFilter(filter, PageRequest.of(0, 2)); + + Assertions.assertEquals(2, actual.getTotalItems()); + Assertions.assertEquals(0.6, actual.getData().get(0).getVotingPower()); + Assertions.assertEquals(BigInteger.TWO, actual.getData().get(1).getActiveVoteStake()); + } }