| | |
| | | package com.ruoyi.other.emind.service; |
| | | |
| | | import com.ruoyi.other.emind.domain.EmindStation; |
| | | import com.ruoyi.other.emind.dto.EmindCurrentPriceDto; |
| | | import com.ruoyi.other.emind.dto.EmindIncomeHistoryDto; |
| | | import com.ruoyi.other.emind.dto.EmindMainTotalDto; |
| | | import com.ruoyi.other.emind.dto.EmindMeterTotalDto; |
| | | import com.ruoyi.other.emind.mapper.EmindStationMapper; |
| | | import com.ruoyi.other.emind.mapper.EmindStorageDashboardMapper; |
| | | import com.ruoyi.other.emind.util.EmindCloudApiUtil; |
| | |
| | | import org.mockito.junit.jupiter.MockitoExtension; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | |
| | | import static org.junit.jupiter.api.Assertions.assertEquals; |
| | |
| | | @Mock private EmindStorageDashboardMapper mapper; |
| | | |
| | | @Test |
| | | void storesCalculatedTodayIncomeInSnapshot() { |
| | | void storesTodayIncomeFromDailyHistoryInSnapshot() { |
| | | EmindStation station = station(); |
| | | when(stationMapper.selectRemoteActiveStations()).thenReturn(Collections.singletonList(station)); |
| | | EmindMainTotalDto main = new EmindMainTotalDto(); |
| | | when(api.getMainTotal(758L)).thenReturn(main); |
| | | when(api.getMeterTotals(758L)).thenReturn(Collections.singletonList(meter())); |
| | | when(api.getCurrentPrice(758L)).thenReturn(price()); |
| | | when(api.getIncomeHistory(eq(758L), any(), any())).thenReturn(Collections.emptyList()); |
| | | when(api.getIncomeHistory(eq(758L), any(), any())).thenReturn(Arrays.asList( |
| | | income(LocalDate.now(), "12.34"), income(LocalDate.now().minusDays(1), "130.00"))); |
| | | |
| | | assertEquals(1, new EmindStorageDashboardSyncService(api, stationMapper, mapper).syncDashboardData()); |
| | | |
| | | ArgumentCaptor<BigDecimal> income = ArgumentCaptor.forClass(BigDecimal.class); |
| | | verify(mapper).upsertSnapshot(eq(station), eq(main), income.capture(), any()); |
| | | assertEquals(new BigDecimal("130.00"), income.getValue()); |
| | | assertEquals(new BigDecimal("12.34"), income.getValue()); |
| | | verify(api, never()).getMeterTotals(any()); |
| | | verify(api, never()).getCurrentPrice(any()); |
| | | } |
| | | |
| | | @Test |
| | | void priceFailureDoesNotWritePartialSnapshot() { |
| | | void historyFailureDoesNotWritePartialSnapshot() { |
| | | when(stationMapper.selectRemoteActiveStations()).thenReturn(Collections.singletonList(station())); |
| | | when(api.getMainTotal(758L)).thenReturn(new EmindMainTotalDto()); |
| | | when(api.getMeterTotals(758L)).thenReturn(Collections.singletonList(meter())); |
| | | when(api.getCurrentPrice(758L)).thenThrow(new IllegalStateException("price unavailable")); |
| | | when(api.getIncomeHistory(eq(758L), any(), any())).thenThrow(new IllegalStateException("history unavailable")); |
| | | |
| | | assertEquals(0, new EmindStorageDashboardSyncService(api, stationMapper, mapper).syncDashboardData()); |
| | | |
| | |
| | | return station; |
| | | } |
| | | |
| | | private EmindMeterTotalDto meter() { |
| | | EmindMeterTotalDto meter = new EmindMeterTotalDto(); |
| | | meter.setMeterType(3); |
| | | meter.setChargedPeak(new BigDecimal("100")); |
| | | meter.setDischargedPeak(new BigDecimal("200")); |
| | | return meter; |
| | | } |
| | | |
| | | private EmindCurrentPriceDto price() { |
| | | EmindCurrentPriceDto price = new EmindCurrentPriceDto(); |
| | | price.setInPeakPrice(new BigDecimal("0.30")); |
| | | price.setOutPeakPrice(new BigDecimal("0.80")); |
| | | return price; |
| | | private EmindIncomeHistoryDto income(LocalDate date, String value) { |
| | | EmindIncomeHistoryDto income = new EmindIncomeHistoryDto(); |
| | | income.setDate(date); |
| | | income.setIncome(new BigDecimal(value)); |
| | | return income; |
| | | } |
| | | } |