Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public void afterCompletion(int status) {
}
}

@Transactional(readOnly = true)
public List<DocumentDTO> listDocuments(Actor actor, UUID caseId) {
CaseEntity caseEntity = caseRepository.findById(caseId)
.orElseThrow(() -> new BadRequestException("Case not found"));
Expand All @@ -153,6 +154,7 @@ public List<DocumentDTO> listDocuments(Actor actor, UUID caseId) {
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public S3Resource downloadDocument(Actor actor, UUID documentId) {
DocumentEntity entity = documentRepository.findById(documentId)
.orElseThrow(() -> new BadRequestException("Document not found"));
Expand Down Expand Up @@ -204,6 +206,7 @@ public void afterCommit() {
}


@Transactional(readOnly = true)
public DocumentEntity getEntity(Actor actor, UUID documentId) {
DocumentEntity entity = documentRepository.findById(documentId)
.orElseThrow(() -> new BadRequestException("Document not found"));
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ spring.datasource.username=arende
spring.datasource.password=arende

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.open-in-view=false
spring.sql.init.mode=always
spring.jpa.defer-datasource-initialization=true
spring.datasource.driver-class-name=org.postgresql.Driver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import io.awspring.cloud.s3.S3Resource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.UUID;

Expand All @@ -32,6 +35,9 @@
import static org.mockito.Mockito.verify;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
Expand Down Expand Up @@ -103,6 +109,37 @@ void uploadDocument_shouldSucceed() throws Exception {
verify(s3Template).upload(eq("test-documents"), any(), any(InputStream.class), any(ObjectMetadata.class));
}

@Test
@WithMockUser(username = "manager", roles = {"MANAGER"})
void listDocuments_shouldReturnDocumentsWithoutLazyLoadingException() throws Exception {
MockMultipartFile file = new MockMultipartFile("file", "test.txt", MediaType.TEXT_PLAIN_VALUE, "hello".getBytes());
mockMvc.perform(multipart("/ui/cases/{caseId}/documents/upload", caseId)
.file(file).with(csrf()))
.andExpect(status().is3xxRedirection());

mockMvc.perform(get("/ui/cases/{caseId}", caseId))
.andExpect(status().isOk());
}

@Test
@WithMockUser(username = "manager", roles = {"MANAGER"})
void downloadDocument_shouldSucceedWithoutLazyLoadingException() throws Exception {
MockMultipartFile file = new MockMultipartFile("file", "test.txt", MediaType.TEXT_PLAIN_VALUE, "hello".getBytes());
mockMvc.perform(multipart("/ui/cases/{caseId}/documents/upload", caseId)
.file(file).with(csrf()))
.andExpect(status().is3xxRedirection());

UUID docId = documentRepository.findAllByCaseEntityId(caseId).get(0).getId();

S3Resource mockResource = mock(S3Resource.class);
when(mockResource.getInputStream()).thenReturn(new ByteArrayInputStream("content".getBytes()));
when(mockResource.contentLength()).thenReturn(7L);
when(s3Template.download(any(), any())).thenReturn(mockResource);

mockMvc.perform(get("/ui/cases/{caseId}/documents/{docId}/download", caseId, docId))
.andExpect(status().isOk());
}

@Test
@WithMockUser(username = "other", roles = {"DOCTOR"})
void uploadDocument_shouldDenyUnauthorized() throws Exception {
Expand Down