/
Chebarik
/
PackageManagement
Обзор
Документация
Войти
/
Chebarik
/
PackageManagement
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/com/packagemanagement/presentation/controller/PackageControllerTest.java
314 строк
13 KB
Николай Мехоношин
added unit tests
13 янв 2026, 20:50
13 янв 2026, 20:50
d22b6a7
Код
Авторство
О чём код?
package com.packagemanagement.presentation.controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.packagemanagement.application.dto.PackageDto; import com.packagemanagement.application.dto.VersionDto; import com.packagemanagement.application.service.PackageApplicationService; import com.packagemanagement.domain.exception.PackageAlreadyExistsException; import com.packagemanagement.domain.exception.PackageNotFoundException; import com.packagemanagement.domain.exception.VersionNotFoundException; import com.packagemanagement.domain.model.PackageFormat; import com.packagemanagement.presentation.exception.GlobalExceptionHandler; import com.packagemanagement.presentation.request.CreatePackageRequest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import java.util.List; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @ExtendWith(MockitoExtension.class) @DisplayName("PackageController") class PackageControllerTest { private MockMvc mockMvc; private ObjectMapper objectMapper; @Mock private PackageApplicationService packageService; @InjectMocks private PackageController packageController; @BeforeEach void setUp() { objectMapper = new ObjectMapper(); mockMvc = MockMvcBuilders.standaloneSetup(packageController) .setControllerAdvice(new GlobalExceptionHandler()) .build(); } @Nested @DisplayName("POST /api/packages") class CreatePackage { @Test @DisplayName("should create package with valid request") void shouldCreatePackageWithValidRequest() throws Exception { CreatePackageRequest request = new CreatePackageRequest( "my-package", "Test description", "NUGET" ); PackageDto responseDto = new PackageDto( "pkg-id", "my-package", "Test description", "NUGET", List.of(), null, "2024-01-01T00:00:00Z", "2024-01-01T00:00:00Z" ); when(packageService.createPackage(eq("my-package"), eq("Test description"), eq(PackageFormat.NUGET))) .thenReturn(responseDto); mockMvc.perform(post("/api/packages") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isCreated()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.data.name").value("my-package")) .andExpect(jsonPath("$.data.format").value("NUGET")); } @Test @DisplayName("should return 409 when package already exists") void shouldReturn409WhenPackageExists() throws Exception { CreatePackageRequest request = new CreatePackageRequest( "existing-package", "Description", "NUGET" ); when(packageService.createPackage(anyString(), anyString(), any())) .thenThrow(new PackageAlreadyExistsException("existing-package")); mockMvc.perform(post("/api/packages") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isConflict()) .andExpect(jsonPath("$.success").value(false)); } } @Nested @DisplayName("POST /api/packages/{id}/versions") class UploadVersion { @Test @DisplayName("should upload version successfully") void shouldUploadVersionSuccessfully() throws Exception { MockMultipartFile file = new MockMultipartFile( "file", "package.nupkg", "application/octet-stream", "content".getBytes() ); VersionDto responseDto = new VersionDto( "v-id", "1.0.0", "2024-01-01T00:00:00Z", 7L, "PUBLISHED" ); when(packageService.uploadVersion(eq("pkg-id"), eq("1.0.0"), any(byte[].class), eq("package.nupkg"))) .thenReturn(responseDto); mockMvc.perform(multipart("/api/packages/pkg-id/versions") .file(file) .param("versionNumber", "1.0.0")) .andExpect(status().isCreated()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.data.versionNumber").value("1.0.0")); } @Test @DisplayName("should return 404 when package not found") void shouldReturn404WhenPackageNotFound() throws Exception { MockMultipartFile file = new MockMultipartFile( "file", "package.nupkg", "application/octet-stream", "content".getBytes() ); when(packageService.uploadVersion(anyString(), anyString(), any(), anyString())) .thenThrow(new PackageNotFoundException("non-existent")); mockMvc.perform(multipart("/api/packages/non-existent/versions") .file(file) .param("versionNumber", "1.0.0")) .andExpect(status().isNotFound()); } } @Nested @DisplayName("GET /api/packages") class GetAllPackages { @Test @DisplayName("should return paginated packages") void shouldReturnPaginatedPackages() throws Exception { PackageDto pkg1 = new PackageDto( "id-1", "package-1", "Desc", "NUGET", List.of(), "1.0.0", "2024-01-01T00:00:00Z", "2024-01-01T00:00:00Z" ); when(packageService.getAllPackages(0, 30)).thenReturn(List.of(pkg1)); when(packageService.getPackageCount()).thenReturn(1L); mockMvc.perform(get("/api/packages")) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.data.items").isArray()) .andExpect(jsonPath("$.data.items[0].name").value("package-1")) .andExpect(jsonPath("$.data.totalItems").value(1)); } @Test @DisplayName("should accept pagination parameters") void shouldAcceptPaginationParameters() throws Exception { when(packageService.getAllPackages(2, 10)).thenReturn(List.of()); when(packageService.getPackageCount()).thenReturn(0L); mockMvc.perform(get("/api/packages") .param("page", "2") .param("size", "10")) .andExpect(status().isOk()); verify(packageService).getAllPackages(2, 10); } } @Nested @DisplayName("GET /api/packages/search") class SearchPackages { @Test @DisplayName("should search packages by query") void shouldSearchPackagesByQuery() throws Exception { PackageDto pkg = new PackageDto( "id-1", "search-result", "Desc", "NUGET", List.of(), "1.0.0", "2024-01-01T00:00:00Z", "2024-01-01T00:00:00Z" ); when(packageService.searchPackages("search")).thenReturn(List.of(pkg)); mockMvc.perform(get("/api/packages/search") .param("query", "search")) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.data[0].name").value("search-result")); } } @Nested @DisplayName("GET /api/packages/{id}") class GetPackage { @Test @DisplayName("should return package by id") void shouldReturnPackageById() throws Exception { PackageDto pkg = new PackageDto( "pkg-id", "my-package", "Description", "NUGET", List.of(), "1.0.0", "2024-01-01T00:00:00Z", "2024-01-01T00:00:00Z" ); when(packageService.getPackage("pkg-id")).thenReturn(pkg); mockMvc.perform(get("/api/packages/pkg-id")) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.data.id").value("pkg-id")) .andExpect(jsonPath("$.data.name").value("my-package")); } @Test @DisplayName("should return 404 when not found") void shouldReturn404WhenNotFound() throws Exception { when(packageService.getPackage("non-existent")) .thenThrow(new PackageNotFoundException("non-existent")); mockMvc.perform(get("/api/packages/non-existent")) .andExpect(status().isNotFound()) .andExpect(jsonPath("$.success").value(false)); } } @Nested @DisplayName("GET /api/packages/{id}/versions/{version}/download") class DownloadVersion { @Test @DisplayName("should download version successfully") void shouldDownloadVersionSuccessfully() throws Exception { byte[] content = "package content".getBytes(); PackageDto pkg = new PackageDto( "pkg-id", "my-package", "Desc", "NUGET", List.of(), "1.0.0", "2024-01-01T00:00:00Z", "2024-01-01T00:00:00Z" ); when(packageService.downloadVersion("pkg-id", "1.0.0")).thenReturn(content); when(packageService.getPackage("pkg-id")).thenReturn(pkg); mockMvc.perform(get("/api/packages/pkg-id/versions/1.0.0/download")) .andExpect(status().isOk()) .andExpect(header().string("Content-Disposition", "form-data; name=\"attachment\"; filename=\"my-package-1.0.0.nupkg\"")) .andExpect(content().bytes(content)); } @Test @DisplayName("should return 404 when version not found") void shouldReturn404WhenVersionNotFound() throws Exception { when(packageService.downloadVersion("pkg-id", "1.0.0")) .thenThrow(new VersionNotFoundException("pkg-id", "1.0.0")); mockMvc.perform(get("/api/packages/pkg-id/versions/1.0.0/download")) .andExpect(status().isNotFound()); } } @Nested @DisplayName("DELETE /api/packages/{id}") class DeletePackage { @Test @DisplayName("should delete package successfully") void shouldDeletePackageSuccessfully() throws Exception { doNothing().when(packageService).deletePackage("pkg-id"); mockMvc.perform(delete("/api/packages/pkg-id")) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.message").value("Package deleted successfully")); verify(packageService).deletePackage("pkg-id"); } @Test @DisplayName("should return 404 when package not found") void shouldReturn404WhenPackageNotFound() throws Exception { doThrow(new PackageNotFoundException("non-existent")) .when(packageService).deletePackage("non-existent"); mockMvc.perform(delete("/api/packages/non-existent")) .andExpect(status().isNotFound()); } } @Nested @DisplayName("DELETE /api/packages/{id}/versions/{version}") class DeleteVersion { @Test @DisplayName("should delete version successfully") void shouldDeleteVersionSuccessfully() throws Exception { doNothing().when(packageService).deleteVersion("pkg-id", "1.0.0"); mockMvc.perform(delete("/api/packages/pkg-id/versions/1.0.0")) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.message").value("Version deleted successfully")); verify(packageService).deleteVersion("pkg-id", "1.0.0"); } } }