package com.ruoyi.account.controller;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.ruoyi.account.api.dto.ChargingCardAuthDTO;
|
import com.ruoyi.account.api.dto.ChargingCardDeductDTO;
|
import com.ruoyi.account.api.dto.ChargingCardExcelDTO;
|
import com.ruoyi.account.api.dto.ChargingCardQueryDTO;
|
import com.ruoyi.account.api.dto.ChargingCardSaveDTO;
|
import com.ruoyi.account.api.dto.ChargingCardStatusDTO;
|
import com.ruoyi.account.api.model.TChargingCard;
|
import com.ruoyi.account.api.vo.ChargingCardDeductVO;
|
import com.ruoyi.account.service.TChargingCardService;
|
import com.ruoyi.common.core.domain.R;
|
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.Test;
|
import org.mockito.Mock;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.springframework.mock.web.MockHttpServletResponse;
|
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.test.util.ReflectionTestUtils;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
import java.util.Collections;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.when;
|
|
@ExtendWith(MockitoExtension.class)
|
class TChargingCardControllerTest {
|
|
@Mock
|
private TChargingCardService service;
|
|
private TChargingCardController controller;
|
|
@BeforeEach
|
void setUp() {
|
controller = new TChargingCardController();
|
ReflectionTestUtils.setField(controller, "chargingCardService", service);
|
}
|
|
@Test
|
void pageDelegatesToService() {
|
ChargingCardQueryDTO query = new ChargingCardQueryDTO();
|
Page<TChargingCard> expected = new Page<>(1, 10);
|
when(service.selectCardPage(query)).thenReturn(expected);
|
|
R<?> result = controller.page(query);
|
|
assertEquals(200, result.getCode());
|
assertEquals(expected, result.getData());
|
verify(service).selectCardPage(query);
|
}
|
|
@Test
|
void addEditDeleteAndStatusDelegateToService() {
|
ChargingCardSaveDTO save = new ChargingCardSaveDTO();
|
ChargingCardStatusDTO status = new ChargingCardStatusDTO();
|
when(service.saveCard(save)).thenReturn(true);
|
when(service.updateCard(save)).thenReturn(true);
|
when(service.deleteCards(Collections.singletonList(1L))).thenReturn(true);
|
when(service.updateStatus(status)).thenReturn(true);
|
|
assertTrue(controller.add(save).getData());
|
assertTrue(controller.edit(save).getData());
|
assertTrue(controller.delete(Collections.singletonList(1L)).getData());
|
assertTrue(controller.status(status).getData());
|
|
verify(service).saveCard(save);
|
verify(service).updateCard(save);
|
verify(service).deleteCards(Collections.singletonList(1L));
|
verify(service).updateStatus(status);
|
}
|
|
@Test
|
void exportWritesExcelResponse() {
|
ChargingCardQueryDTO query = new ChargingCardQueryDTO();
|
ChargingCardExcelDTO card = new ChargingCardExcelDTO();
|
card.setCardNo("CARD-1");
|
when(service.selectExportList(query)).thenReturn(Collections.singletonList(card));
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
controller.export(response, query);
|
|
assertEquals(200, response.getStatus());
|
assertTrue(response.getContentAsByteArray().length > 0);
|
assertTrue(response.getContentType().contains("application/"));
|
verify(service).selectExportList(query);
|
}
|
|
@Test
|
void importTemplateWritesExcelResponse() {
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
controller.importTemplate(response);
|
|
assertEquals(200, response.getStatus());
|
assertTrue(response.getContentAsByteArray().length > 0);
|
}
|
|
@Test
|
void importDataReadsFileAndDelegates() throws Exception {
|
MockMultipartFile file = new MockMultipartFile("file", "cards.xlsx",
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", excelBytes());
|
when(service.importCards(any())).thenReturn("成功导入1条卡片数据");
|
|
R<String> result = controller.importData(file);
|
|
assertEquals(200, result.getCode());
|
assertEquals("成功导入1条卡片数据", result.getData());
|
verify(service).importCards(any());
|
}
|
|
@Test
|
void innerVerifyChargingDelegatesToService() {
|
ChargingCardAuthDTO dto = new ChargingCardAuthDTO();
|
dto.setCardNo("CARD-1");
|
TChargingCard card = new TChargingCard();
|
card.setId(1L);
|
when(service.verifyCardForCharging(dto)).thenReturn(card);
|
|
R<TChargingCard> result = controller.verifyCharging(dto);
|
|
assertEquals(200, result.getCode());
|
assertEquals(card, result.getData());
|
verify(service).verifyCardForCharging(dto);
|
}
|
|
@Test
|
void innerLockAndUnlockDelegateToService() {
|
when(service.lockCardForCharging(1L, "SD20260909001")).thenReturn(true);
|
when(service.unlockCardForCharging(1L, "SD20260909001")).thenReturn(true);
|
|
assertTrue(controller.lockForCharging(1L, "SD20260909001").getData());
|
assertTrue(controller.unlockForCharging(1L, "SD20260909001").getData());
|
verify(service).lockCardForCharging(1L, "SD20260909001");
|
verify(service).unlockCardForCharging(1L, "SD20260909001");
|
}
|
|
@Test
|
void innerDeductedOrderCodesDelegatesToService() {
|
java.util.List<String> orderCodes = Collections.singletonList("SD20260909001");
|
when(service.listDeductedOrderCodes(orderCodes)).thenReturn(orderCodes);
|
|
R<java.util.List<String>> result = controller.listDeductedOrderCodes(orderCodes);
|
|
assertEquals(200, result.getCode());
|
assertEquals(orderCodes, result.getData());
|
verify(service).listDeductedOrderCodes(orderCodes);
|
}
|
|
@Test
|
void innerDeductChargingDelegatesToService() {
|
ChargingCardDeductDTO dto = new ChargingCardDeductDTO();
|
dto.setCardId(1L);
|
dto.setOrderCode("SD20260909001");
|
dto.setAmount(java.math.BigDecimal.TEN);
|
ChargingCardDeductVO vo = new ChargingCardDeductVO();
|
vo.setDuplicated(false);
|
when(service.deductBalanceForCharging(dto)).thenReturn(vo);
|
|
R<ChargingCardDeductVO> result = controller.deductForCharging(dto);
|
|
assertEquals(200, result.getCode());
|
assertEquals(vo, result.getData());
|
verify(service).deductBalanceForCharging(dto);
|
}
|
|
private byte[] excelBytes() throws Exception {
|
org.apache.poi.xssf.usermodel.XSSFWorkbook workbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook();
|
try {
|
org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet("卡片数据");
|
sheet.createRow(0).createCell(0).setCellValue("卡号");
|
sheet.createRow(1).createCell(0).setCellValue("CARD-1");
|
java.io.ByteArrayOutputStream output = new java.io.ByteArrayOutputStream();
|
workbook.write(output);
|
return output.toByteArray();
|
} finally {
|
workbook.close();
|
}
|
}
|
}
|