diff --git a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java index d2f7d58f..0a51aae2 100644 --- a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java +++ b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java @@ -51,13 +51,17 @@ public class PatientRecordController extends BaseController { private final ExcelRecordConverter excelRecordConverter; private final RestTemplate restTemplate; private final ConfigReader configReader; + private ObjectMapper objectMapper; - public PatientRecordController(PatientRecordService recordService, ApplicationEventPublisher eventPublisher, ExcelRecordConverter excelRecordConverter, RestTemplate restTemplate, ConfigReader configReader) { + public PatientRecordController(PatientRecordService recordService, ApplicationEventPublisher eventPublisher, + ExcelRecordConverter excelRecordConverter, RestTemplate restTemplate, + ConfigReader configReader, ObjectMapper objectMapper) { this.recordService = recordService; this.eventPublisher = eventPublisher; this.excelRecordConverter = excelRecordConverter; this.restTemplate = restTemplate; this.configReader = configReader; + this.objectMapper = objectMapper; } @PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)") @@ -167,7 +171,7 @@ public RecordImportResult importRecordsJson(@RequestPart("file") MultipartFile f if(file.isEmpty()) throw new IllegalArgumentException("Cannot import records, missing input file"); try { - records = new ObjectMapper().readValue(file.getBytes(), new TypeReference>(){}); + records = objectMapper.readValue(file.getBytes(), new TypeReference>(){}); } catch (IOException e) { throw new RuntimeException("Failed to parse JSON content", e); } diff --git a/src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java b/src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java index 83eb8965..08c7a98f 100644 --- a/src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java +++ b/src/main/java/cz/cvut/kbss/study/service/PatientRecordService.java @@ -72,6 +72,7 @@ public interface PatientRecordService extends BaseService { * current user is set as the record's author. * * @param records Records to import + * @param targetPhase Phase to be set to all imported records. * @return Instance representing the import result * @throws cz.cvut.kbss.study.exception.RecordAuthorNotFoundException Thrown when importing a record whose author * does not exist in this application instance's diff --git a/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java index 7fca2a90..61a6537c 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java @@ -24,6 +24,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; @@ -65,6 +66,9 @@ public class PatientRecordControllerTest extends BaseControllerTestRunner { @Mock private ApplicationEventPublisher eventPublisherMock; + @Spy + private ObjectMapper objectMapper = Environment.getObjectMapper(); + @InjectMocks private PatientRecordController controller; @@ -365,8 +369,16 @@ void importRecordsJsonImportsSpecifiedRecordsWithSpecifiedPhaseAndReturnsImportR final RecordPhase targetPhase = RecordPhase.values()[Generator.randomInt(0, RecordPhase.values().length)]; when(patientRecordServiceMock.importRecords(anyList(), any(RecordPhase.class))).thenReturn(importResult); - mockMvc.perform(post("/records/import/json").content(toJson(records)).contentType(MediaType.MULTIPART_FORM_DATA_VALUE) - .param("phase", targetPhase.getIri())).andExpect(status().isOk()); + MockMultipartFile file = new MockMultipartFile("file", "records.json", + MediaType.MULTIPART_FORM_DATA_VALUE, toJson(records).getBytes()); + + mockMvc.perform( + multipart("/records/import/json") + .file(file) + .contentType(MediaType.MULTIPART_FORM_DATA_VALUE) + .param("phase", targetPhase.getIri()) + ).andExpect(status().isOk()); + verify(patientRecordServiceMock).importRecords(anyList(), eq(targetPhase)); }