Skip to content

Commit

Permalink
test: update unit test for DRep services
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-ThinhVu committed Apr 16, 2024
1 parent 6324402 commit c8a4fe9
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
Expand Up @@ -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;
Expand All @@ -16,6 +17,7 @@
@Setter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class DRepFilterRequest {

private String drepIdOrHash;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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")));
}
}
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -42,6 +50,7 @@ public class DRepServiceTest {
@Mock VotingProcedureRepository votingProcedureRepository;
@Mock LatestVotingProcedureRepository latestVotingProcedureRepository;
@Mock GovernanceActionRepository governanceActionRepository;
@Mock EpochService epochService;
@InjectMocks DRepServiceImpl dRepCertificateService;

@Spy
Expand Down Expand Up @@ -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());
}
}

0 comments on commit c8a4fe9

Please sign in to comment.