/
ilyaBu
/
WebSpringJava
Обзор
Документация
Войти
/
ilyaBu
/
WebSpringJava
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/ru/gb/spring/diplomawebjavaspringgb/service/impl/PdfGenerateServiceImplTest.java
131 строка
4 KB
IliaBu
Update code
13 июл 2024, 17:50
13 июл 2024, 17:50
beaa8ef
Код
Авторство
О чём код?
package ru.gb.spring.diplomawebjavaspringgb.service.impl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import org.springframework.ui.Model; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class PdfGenerateServiceImplTest { @Mock private TemplateEngine templateEngine; @Mock private Model model; @InjectMocks private PdfGenerateServiceImpl pdfGenerateService; /** * Инициализирует макеты объектов, используемые в тестах. */ @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); } /** * Тестовый метод генерирует PDF-файл на основе заданного шаблона и данных. */ @Test void testGeneratePdfFile() { String templateName = "template"; Map<String, Object> data = new HashMap<>(); data.put("key", "value"); String pdfFileName = "test.pdf"; when(templateEngine.process(eq(templateName), any(Context.class))).thenReturn("<html><body>Test</body></html>"); pdfGenerateService.generatePdfFile(templateName, data, pdfFileName); File pdfFile = new File(PdfGenerateServiceImpl.path + pdfFileName); assertTrue(pdfFile.exists()); assertTrue(pdfFile.length() > 0); pdfFile.delete(); } /** * Тестовый метод проверяет загрузку в модель списка загружаемых файлов. */ @Test void testLoadListDownloads() { File directory = new File(PdfGenerateServiceImpl.path); if (!directory.exists()) { directory.mkdir(); } File testFile = new File(PdfGenerateServiceImpl.path + "testfile.txt"); assertFalse(testFile.exists()); try { testFile.createNewFile(); Files.write(testFile.toPath(), "test content".getBytes()); } catch (IOException e) { e.printStackTrace(); } pdfGenerateService.loadListDownloads(model); verify(model).addAttribute(eq("list"), anyList()); testFile.delete(); } /** * Тестовый метод проверяет загрузку файла по указанному пути. */ @Test void testDownloadFile() { String fileName = "testfile.txt"; File testFile = new File(PdfGenerateServiceImpl.path + fileName); try { testFile.createNewFile(); Files.write(testFile.toPath(), "test content".getBytes()); } catch (IOException e) { e.printStackTrace(); } ResponseEntity<Resource> responseEntity = pdfGenerateService.downloadFile(fileName); assertNotNull(responseEntity); assertEquals(200, responseEntity.getStatusCode().value()); assertTrue(responseEntity.getBody() instanceof ByteArrayResource); testFile.delete(); assertFalse(testFile.exists()); } /** * Тестовый метод проверяет удаление файла из указанного каталога. */ @Test void testDelete() { String fileName = "testfile.txt"; File testFile = new File(PdfGenerateServiceImpl.path + fileName); try { testFile.createNewFile(); Files.write(testFile.toPath(), "test content".getBytes()); } catch (IOException e) { e.printStackTrace(); } String result = pdfGenerateService.delete(fileName); assertEquals("redirect:/list-parts", result); assertFalse(testFile.exists()); } }