package at.fhtw.swen3.paperless.review import at.fhtw.swen3.paperless.documenttype.mapper.DocumentTypeMapper import at.fhtw.swen3.paperless.documenttype.model.DocumentType import at.fhtw.swen3.paperless.documenttype.service.DocumentTypeService import at.fhtw.swen3.paperless.dto.UpdateDocumentTypeRequest import at.fhtw.swen3.paperless.user.entity.UserEntity import at.fhtw.swen3.paperless.user.persistence.repository.UserRepository import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc import org.springframework.mock.web.MockMultipartFile import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart import java.util.UUID // Diagnostic assertions express the desired behavior. Failures reproduce baseline defects. // Uses isolated H2 and a test principal; it does not exercise the real bearer login chain. @SpringBootTest(properties = ["spring.datasource.url=jdbc:h2:mem:review-probes;DB_CLOSE_DELAY=-1"]) @AutoConfigureMockMvc class ReviewRegressionTest( @Autowired private val mvc: MockMvc, @Autowired private val users: UserRepository, @Autowired private val types: DocumentTypeService, @Autowired private val mapper: DocumentTypeMapper ) { private fun actor(): UUID = requireNotNull(users.save(UserEntity( username = "review-" + UUID.randomUUID(), password = "test-only-password" )).id) @Test fun invalid_uuid_is_a_client_error() { val response = mvc.perform(get("/api/v1/documents/not-a-uuid") .with(user(actor().toString()))).andReturn().response assertThat(response.status).describedAs("invalid UUID must be HTTP 400").isEqualTo(400) } @Test fun invalid_page_number_is_a_client_error() { val response = mvc.perform(get("/api/v1/documents").param("page", "not-a-number") .with(user(actor().toString()))).andReturn().response assertThat(response.status).describedAs("invalid page must be HTTP 400").isEqualTo(400) } @Test fun non_pdf_upload_is_rejected() { val response = mvc.perform(multipart("/api/v1/documents") .file(MockMultipartFile("document", "notes.txt", "text/plain", "not a PDF".toByteArray())) .param("title", "Review plain text").with(user(actor().toString()))).andReturn().response assertThat(response.status).describedAs("PDF-only API must reject text/plain").isBetween(400, 499) } @Test fun empty_upload_is_rejected() { val response = mvc.perform(multipart("/api/v1/documents") .file(MockMultipartFile("document", "empty.pdf", "application/pdf", byteArrayOf())) .param("title", "Review empty PDF").with(user(actor().toString()))).andReturn().response assertThat(response.status).describedAs("an empty file is not a PDF").isBetween(400, 499) } @Test fun partial_update_does_not_overwrite_an_intervening_description_change() { val original = types.createDocumentType(DocumentType(null, "initial-" + UUID.randomUUID(), "original")) val id = requireNotNull(original.id) // Request A reads, then request B commits before A reaches its update transaction. val snapshotA = types.getDocumentTypeById(id) types.updateDocumentType(id, snapshotA.copy(description = "changed by B")) val current = types.getDocumentTypeById(id) assertThat(current.version).isGreaterThan(snapshotA.version) // This is exactly the DTO merge performed in DocumentTypeController outside the write transaction. val requestA = mapper.applyUpdate(snapshotA, UpdateDocumentTypeRequest(name = "renamed-" + UUID.randomUUID())) val saved = types.updateDocumentType(id, requestA) assertThat(saved.description).describedAs("A omitted description; B's change must survive") .isEqualTo("changed by B") } }